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
45void
46winInitNotifyIcon(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
76void
77winDeleteNotifyIcon(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
108LRESULT
109winHandleIconMessage(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        break;
119
120    case WM_LBUTTONDBLCLK:
121        /* Display Exit dialog box */
122        winDisplayExitDialog(pScreenPriv);
123        break;
124
125    case WM_RBUTTONUP:
126    {
127        POINT ptCursor;
128        HMENU hmenuPopup;
129        HMENU hmenuTray;
130
131        /* Get cursor position */
132        GetCursorPos(&ptCursor);
133
134        /* Load tray icon menu resource */
135        hmenuPopup = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDM_TRAYICON_MENU));
136        if (!hmenuPopup)
137            ErrorF("winHandleIconMessage - LoadMenu failed\n");
138
139        /* Get actual tray icon menu */
140        hmenuTray = GetSubMenu(hmenuPopup, 0);
141
142        /* Check for MultiWindow mode */
143        if (pScreenInfo->fMultiWindow) {
144            MENUITEMINFO mii = { 0 };
145
146            /* Root is shown, remove the check box */
147
148            /* Setup menu item info structure */
149            mii.cbSize = sizeof(MENUITEMINFO);
150            mii.fMask = MIIM_STATE;
151            mii.fState = MFS_CHECKED;
152
153            /* Unheck box if root is shown */
154            if (pScreenPriv->fRootWindowShown)
155                mii.fState = MFS_UNCHECKED;
156
157            /* Set menu state */
158            SetMenuItemInfo(hmenuTray, ID_APP_HIDE_ROOT, FALSE, &mii);
159        }
160        else
161        {
162            /* Remove Hide Root Window button */
163            RemoveMenu(hmenuTray, ID_APP_HIDE_ROOT, MF_BYCOMMAND);
164        }
165
166        if (g_fClipboard) {
167            /* Set menu state to indicate if 'Monitor Primary' is enabled or not */
168            MENUITEMINFO mii = { 0 };
169            mii.cbSize = sizeof(MENUITEMINFO);
170            mii.fMask = MIIM_STATE;
171            mii.fState = fPrimarySelection ? MFS_CHECKED : MFS_UNCHECKED;
172            SetMenuItemInfo(hmenuTray, ID_APP_MONITOR_PRIMARY, FALSE, &mii);
173        }
174        else {
175            /* Remove 'Monitor Primary' menu item */
176            RemoveMenu(hmenuTray, ID_APP_MONITOR_PRIMARY, MF_BYCOMMAND);
177        }
178
179        SetupRootMenu(hmenuTray);
180
181        /*
182         * NOTE: This three-step procedure is required for
183         * proper popup menu operation.  Without the
184         * call to SetForegroundWindow the
185         * popup menu will often not disappear when you click
186         * outside of it.  Without the PostMessage the second
187         * time you display the popup menu it might immediately
188         * disappear.
189         */
190        SetForegroundWindow(hwnd);
191        TrackPopupMenuEx(hmenuTray,
192                         TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON,
193                         ptCursor.x, ptCursor.y, hwnd, NULL);
194        PostMessage(hwnd, WM_NULL, 0, 0);
195
196        /* Free menu */
197        DestroyMenu(hmenuPopup);
198    }
199        break;
200    }
201
202    return 0;
203}
204