wintrayicon.c revision 35c4bbdf
1/*
2 *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
3 *
4 *Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 *"Software"), to deal in the Software without restriction, including
7 *without limitation the rights to use, copy, modify, merge, publish,
8 *distribute, sublicense, and/or sell copies of the Software, and to
9 *permit persons to whom the Software is furnished to do so, subject to
10 *the following conditions:
11 *
12 *The above copyright notice and this permission notice shall be
13 *included in all copies or substantial portions of the Software.
14 *
15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
19 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 *Except as contained in this notice, the name of the XFree86 Project
24 *shall not be used in advertising or otherwise to promote the sale, use
25 *or other dealings in this Software without prior written authorization
26 *from the XFree86 Project.
27 *
28 * Authors:	Early Ehlinger
29 *		Harold L Hunt II
30 */
31
32#ifdef HAVE_XWIN_CONFIG_H
33#include <xwin-config.h>
34#endif
35
36#include "win.h"
37#include <shellapi.h>
38#include "winprefs.h"
39#ifdef XWIN_CLIPBOARD
40#include "winclipboard/winclipboard.h"
41#endif
42
43/*
44 * Initialize the tray icon
45 */
46
47void
48winInitNotifyIcon(winPrivScreenPtr pScreenPriv)
49{
50    winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
51    NOTIFYICONDATA nid = { 0 };
52
53    nid.cbSize = sizeof(NOTIFYICONDATA);
54    nid.hWnd = pScreenPriv->hwndScreen;
55    nid.uID = pScreenInfo->dwScreen;
56    nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
57    nid.uCallbackMessage = WM_TRAYICON;
58    nid.hIcon = winTaskbarIcon();
59
60    /* Save handle to the icon so it can be freed later */
61    pScreenPriv->hiconNotifyIcon = nid.hIcon;
62
63    /* Set display and screen-specific tooltip text */
64    snprintf(nid.szTip,
65             sizeof(nid.szTip),
66             PROJECT_NAME " Server:%s.%d",
67             display, (int) pScreenInfo->dwScreen);
68
69    /* Add the tray icon */
70    if (!Shell_NotifyIcon(NIM_ADD, &nid))
71        ErrorF("winInitNotifyIcon - Shell_NotifyIcon Failed\n");
72}
73
74/*
75 * Delete the tray icon
76 */
77
78void
79winDeleteNotifyIcon(winPrivScreenPtr pScreenPriv)
80{
81    winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
82    NOTIFYICONDATA nid = { 0 };
83
84#if 0
85    ErrorF("winDeleteNotifyIcon\n");
86#endif
87
88    nid.cbSize = sizeof(NOTIFYICONDATA);
89    nid.hWnd = pScreenPriv->hwndScreen;
90    nid.uID = pScreenInfo->dwScreen;
91
92    /* Delete the tray icon */
93    if (!Shell_NotifyIcon(NIM_DELETE, &nid)) {
94        ErrorF("winDeleteNotifyIcon - Shell_NotifyIcon failed\n");
95        return;
96    }
97
98    /* Free the icon that was loaded */
99    if (pScreenPriv->hiconNotifyIcon != NULL
100        && DestroyIcon(pScreenPriv->hiconNotifyIcon) == 0) {
101        ErrorF("winDeleteNotifyIcon - DestroyIcon failed\n");
102    }
103    pScreenPriv->hiconNotifyIcon = NULL;
104}
105
106/*
107 * Process messages intended for the tray icon
108 */
109
110LRESULT
111winHandleIconMessage(HWND hwnd, UINT message,
112                     WPARAM wParam, LPARAM lParam, winPrivScreenPtr pScreenPriv)
113{
114#if defined(XWIN_MULTIWINDOWEXTWM) || defined(XWIN_MULTIWINDOW)
115    winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
116#endif
117
118    switch (lParam) {
119    case WM_LBUTTONUP:
120        /* Restack and bring all windows to top */
121        SetForegroundWindow (pScreenPriv->hwndScreen);
122
123#ifdef XWIN_MULTIWINDOWEXTWM
124        if (pScreenInfo->fMWExtWM)
125            winMWExtWMRestackWindows(pScreenInfo->pScreen);
126#endif
127        break;
128
129    case WM_LBUTTONDBLCLK:
130        /* Display Exit dialog box */
131        winDisplayExitDialog(pScreenPriv);
132        break;
133
134    case WM_RBUTTONUP:
135    {
136        POINT ptCursor;
137        HMENU hmenuPopup;
138        HMENU hmenuTray;
139
140        /* Get cursor position */
141        GetCursorPos(&ptCursor);
142
143        /* Load tray icon menu resource */
144        hmenuPopup = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDM_TRAYICON_MENU));
145        if (!hmenuPopup)
146            ErrorF("winHandleIconMessage - LoadMenu failed\n");
147
148        /* Get actual tray icon menu */
149        hmenuTray = GetSubMenu(hmenuPopup, 0);
150
151#ifdef XWIN_MULTIWINDOW
152        /* Check for MultiWindow mode */
153        if (pScreenInfo->fMultiWindow) {
154            MENUITEMINFO mii = { 0 };
155
156            /* Root is shown, remove the check box */
157
158            /* Setup menu item info structure */
159            mii.cbSize = sizeof(MENUITEMINFO);
160            mii.fMask = MIIM_STATE;
161            mii.fState = MFS_CHECKED;
162
163            /* Unheck box if root is shown */
164            if (pScreenPriv->fRootWindowShown)
165                mii.fState = MFS_UNCHECKED;
166
167            /* Set menu state */
168            SetMenuItemInfo(hmenuTray, ID_APP_HIDE_ROOT, FALSE, &mii);
169        }
170        else
171#endif
172        {
173            /* Remove Hide Root Window button */
174            RemoveMenu(hmenuTray, ID_APP_HIDE_ROOT, MF_BYCOMMAND);
175        }
176
177#ifdef XWIN_CLIPBOARD
178        if (g_fClipboard) {
179            /* Set menu state to indicate if 'Monitor Primary' is enabled or not */
180            MENUITEMINFO mii = { 0 };
181            mii.cbSize = sizeof(MENUITEMINFO);
182            mii.fMask = MIIM_STATE;
183            mii.fState = fPrimarySelection ? MFS_CHECKED : MFS_UNCHECKED;
184            SetMenuItemInfo(hmenuTray, ID_APP_MONITOR_PRIMARY, FALSE, &mii);
185        }
186        else {
187            /* Remove 'Monitor Primary' menu item */
188            RemoveMenu(hmenuTray, ID_APP_MONITOR_PRIMARY, MF_BYCOMMAND);
189        }
190#endif
191
192        SetupRootMenu(hmenuTray);
193
194        /*
195         * NOTE: This three-step procedure is required for
196         * proper popup menu operation.  Without the
197         * call to SetForegroundWindow the
198         * popup menu will often not disappear when you click
199         * outside of it.  Without the PostMessage the second
200         * time you display the popup menu it might immediately
201         * disappear.
202         */
203        SetForegroundWindow(hwnd);
204        TrackPopupMenuEx(hmenuTray,
205                         TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON,
206                         ptCursor.x, ptCursor.y, hwnd, NULL);
207        PostMessage(hwnd, WM_NULL, 0, 0);
208
209        /* Free menu */
210        DestroyMenu(hmenuPopup);
211    }
212        break;
213    }
214
215    return 0;
216}
217