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 #include "winclipboard/winclipboard.h"
40 
41 /*
42  * Initialize the tray icon
43  */
44 
45 void
winInitNotifyIcon(winPrivScreenPtr pScreenPriv)46 winInitNotifyIcon(winPrivScreenPtr pScreenPriv)
47 {
48     winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
49     NOTIFYICONDATA nid = { 0 };
50 
51     nid.cbSize = sizeof(NOTIFYICONDATA);
52     nid.hWnd = pScreenPriv->hwndScreen;
53     nid.uID = pScreenInfo->dwScreen;
54     nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
55     nid.uCallbackMessage = WM_TRAYICON;
56     nid.hIcon = winTaskbarIcon();
57 
58     /* Save handle to the icon so it can be freed later */
59     pScreenPriv->hiconNotifyIcon = nid.hIcon;
60 
61     /* Set display and screen-specific tooltip text */
62     snprintf(nid.szTip,
63              sizeof(nid.szTip),
64              PROJECT_NAME " Server:%s.%d",
65              display, (int) pScreenInfo->dwScreen);
66 
67     /* Add the tray icon */
68     if (!Shell_NotifyIcon(NIM_ADD, &nid))
69         ErrorF("winInitNotifyIcon - Shell_NotifyIcon Failed\n");
70 }
71 
72 /*
73  * Delete the tray icon
74  */
75 
76 void
winDeleteNotifyIcon(winPrivScreenPtr pScreenPriv)77 winDeleteNotifyIcon(winPrivScreenPtr pScreenPriv)
78 {
79     winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
80     NOTIFYICONDATA nid = { 0 };
81 
82 #if 0
83     ErrorF("winDeleteNotifyIcon\n");
84 #endif
85 
86     nid.cbSize = sizeof(NOTIFYICONDATA);
87     nid.hWnd = pScreenPriv->hwndScreen;
88     nid.uID = pScreenInfo->dwScreen;
89 
90     /* Delete the tray icon */
91     if (!Shell_NotifyIcon(NIM_DELETE, &nid)) {
92         ErrorF("winDeleteNotifyIcon - Shell_NotifyIcon failed\n");
93         return;
94     }
95 
96     /* Free the icon that was loaded */
97     if (pScreenPriv->hiconNotifyIcon != NULL
98         && DestroyIcon(pScreenPriv->hiconNotifyIcon) == 0) {
99         ErrorF("winDeleteNotifyIcon - DestroyIcon failed\n");
100     }
101     pScreenPriv->hiconNotifyIcon = NULL;
102 }
103 
104 /*
105  * Process messages intended for the tray icon
106  */
107 
108 LRESULT
winHandleIconMessage(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam,winPrivScreenPtr pScreenPriv)109 winHandleIconMessage(HWND hwnd, UINT message,
110                      WPARAM wParam, LPARAM lParam, winPrivScreenPtr pScreenPriv)
111 {
112     winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo;
113 
114     switch (lParam) {
115     case WM_LBUTTONUP:
116         /* Restack and bring all windows to top */
117         SetForegroundWindow (pScreenPriv->hwndScreen);
118 
119 #ifdef XWIN_MULTIWINDOWEXTWM
120         if (pScreenInfo->fMWExtWM)
121             winMWExtWMRestackWindows(pScreenInfo->pScreen);
122 #endif
123         break;
124 
125     case WM_LBUTTONDBLCLK:
126         /* Display Exit dialog box */
127         winDisplayExitDialog(pScreenPriv);
128         break;
129 
130     case WM_RBUTTONUP:
131     {
132         POINT ptCursor;
133         HMENU hmenuPopup;
134         HMENU hmenuTray;
135 
136         /* Get cursor position */
137         GetCursorPos(&ptCursor);
138 
139         /* Load tray icon menu resource */
140         hmenuPopup = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDM_TRAYICON_MENU));
141         if (!hmenuPopup)
142             ErrorF("winHandleIconMessage - LoadMenu failed\n");
143 
144         /* Get actual tray icon menu */
145         hmenuTray = GetSubMenu(hmenuPopup, 0);
146 
147         /* Check for MultiWindow mode */
148         if (pScreenInfo->fMultiWindow) {
149             MENUITEMINFO mii = { 0 };
150 
151             /* Root is shown, remove the check box */
152 
153             /* Setup menu item info structure */
154             mii.cbSize = sizeof(MENUITEMINFO);
155             mii.fMask = MIIM_STATE;
156             mii.fState = MFS_CHECKED;
157 
158             /* Unheck box if root is shown */
159             if (pScreenPriv->fRootWindowShown)
160                 mii.fState = MFS_UNCHECKED;
161 
162             /* Set menu state */
163             SetMenuItemInfo(hmenuTray, ID_APP_HIDE_ROOT, FALSE, &mii);
164         }
165         else
166         {
167             /* Remove Hide Root Window button */
168             RemoveMenu(hmenuTray, ID_APP_HIDE_ROOT, MF_BYCOMMAND);
169         }
170 
171         if (g_fClipboard) {
172             /* Set menu state to indicate if 'Monitor Primary' is enabled or not */
173             MENUITEMINFO mii = { 0 };
174             mii.cbSize = sizeof(MENUITEMINFO);
175             mii.fMask = MIIM_STATE;
176             mii.fState = fPrimarySelection ? MFS_CHECKED : MFS_UNCHECKED;
177             SetMenuItemInfo(hmenuTray, ID_APP_MONITOR_PRIMARY, FALSE, &mii);
178         }
179         else {
180             /* Remove 'Monitor Primary' menu item */
181             RemoveMenu(hmenuTray, ID_APP_MONITOR_PRIMARY, MF_BYCOMMAND);
182         }
183 
184         SetupRootMenu(hmenuTray);
185 
186         /*
187          * NOTE: This three-step procedure is required for
188          * proper popup menu operation.  Without the
189          * call to SetForegroundWindow the
190          * popup menu will often not disappear when you click
191          * outside of it.  Without the PostMessage the second
192          * time you display the popup menu it might immediately
193          * disappear.
194          */
195         SetForegroundWindow(hwnd);
196         TrackPopupMenuEx(hmenuTray,
197                          TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON,
198                          ptCursor.x, ptCursor.y, hwnd, NULL);
199         PostMessage(hwnd, WM_NULL, 0, 0);
200 
201         /* Free menu */
202         DestroyMenu(hmenuPopup);
203     }
204         break;
205     }
206 
207     return 0;
208 }
209