1706f2543Smrg/*
2706f2543Smrg *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved.
3706f2543Smrg *
4706f2543Smrg *Permission is hereby granted, free of charge, to any person obtaining
5706f2543Smrg * a copy of this software and associated documentation files (the
6706f2543Smrg *"Software"), to deal in the Software without restriction, including
7706f2543Smrg *without limitation the rights to use, copy, modify, merge, publish,
8706f2543Smrg *distribute, sublicense, and/or sell copies of the Software, and to
9706f2543Smrg *permit persons to whom the Software is furnished to do so, subject to
10706f2543Smrg *the following conditions:
11706f2543Smrg *
12706f2543Smrg *The above copyright notice and this permission notice shall be
13706f2543Smrg *included in all copies or substantial portions of the Software.
14706f2543Smrg *
15706f2543Smrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16706f2543Smrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17706f2543Smrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18706f2543Smrg *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR
19706f2543Smrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20706f2543Smrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21706f2543Smrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22706f2543Smrg *
23706f2543Smrg *Except as contained in this notice, the name of the XFree86 Project
24706f2543Smrg *shall not be used in advertising or otherwise to promote the sale, use
25706f2543Smrg *or other dealings in this Software without prior written authorization
26706f2543Smrg *from the XFree86 Project.
27706f2543Smrg *
28706f2543Smrg * Authors:	Early Ehlinger
29706f2543Smrg *		Harold L Hunt II
30706f2543Smrg */
31706f2543Smrg
32706f2543Smrg#ifdef HAVE_XWIN_CONFIG_H
33706f2543Smrg#include <xwin-config.h>
34706f2543Smrg#endif
35706f2543Smrg#include "win.h"
36706f2543Smrg#include <shellapi.h>
37706f2543Smrg#include "winprefs.h"
38706f2543Smrg
39706f2543Smrg/*
40706f2543Smrg * Initialize the tray icon
41706f2543Smrg */
42706f2543Smrg
43706f2543Smrgvoid
44706f2543SmrgwinInitNotifyIcon (winPrivScreenPtr pScreenPriv)
45706f2543Smrg{
46706f2543Smrg  winScreenInfo		*pScreenInfo = pScreenPriv->pScreenInfo;
47706f2543Smrg  NOTIFYICONDATA	nid = {0};
48706f2543Smrg
49706f2543Smrg  nid.cbSize = sizeof (NOTIFYICONDATA);
50706f2543Smrg  nid.hWnd = pScreenPriv->hwndScreen;
51706f2543Smrg  nid.uID = pScreenInfo->dwScreen;
52706f2543Smrg  nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
53706f2543Smrg  nid.uCallbackMessage = WM_TRAYICON;
54706f2543Smrg  nid.hIcon = winTaskbarIcon ();
55706f2543Smrg
56706f2543Smrg  /* Save handle to the icon so it can be freed later */
57706f2543Smrg  pScreenPriv->hiconNotifyIcon = nid.hIcon;
58706f2543Smrg
59706f2543Smrg  /* Set display and screen-specific tooltip text */
60706f2543Smrg  snprintf (nid.szTip,
61706f2543Smrg	    sizeof (nid.szTip),
62706f2543Smrg	    PROJECT_NAME " Server:%s.%d",
63706f2543Smrg	    display,
64706f2543Smrg	    (int) pScreenInfo->dwScreen);
65706f2543Smrg
66706f2543Smrg  /* Add the tray icon */
67706f2543Smrg  if (!Shell_NotifyIcon (NIM_ADD, &nid))
68706f2543Smrg    ErrorF ("winInitNotifyIcon - Shell_NotifyIcon Failed\n");
69706f2543Smrg}
70706f2543Smrg
71706f2543Smrg
72706f2543Smrg/*
73706f2543Smrg * Delete the tray icon
74706f2543Smrg */
75706f2543Smrg
76706f2543Smrgvoid
77706f2543SmrgwinDeleteNotifyIcon (winPrivScreenPtr pScreenPriv)
78706f2543Smrg{
79706f2543Smrg  winScreenInfo		*pScreenInfo = pScreenPriv->pScreenInfo;
80706f2543Smrg  NOTIFYICONDATA	nid = {0};
81706f2543Smrg
82706f2543Smrg#if 0
83706f2543Smrg  ErrorF ("winDeleteNotifyIcon\n");
84706f2543Smrg#endif
85706f2543Smrg
86706f2543Smrg  nid.cbSize = sizeof (NOTIFYICONDATA);
87706f2543Smrg  nid.hWnd = pScreenPriv->hwndScreen;
88706f2543Smrg  nid.uID = pScreenInfo->dwScreen;
89706f2543Smrg
90706f2543Smrg  /* Delete the tray icon */
91706f2543Smrg  if (!Shell_NotifyIcon (NIM_DELETE, &nid))
92706f2543Smrg    {
93706f2543Smrg      ErrorF ("winDeleteNotifyIcon - Shell_NotifyIcon failed\n");
94706f2543Smrg      return;
95706f2543Smrg    }
96706f2543Smrg
97706f2543Smrg  /* Free the icon that was loaded */
98706f2543Smrg  if (pScreenPriv->hiconNotifyIcon != NULL
99706f2543Smrg      && DestroyIcon (pScreenPriv->hiconNotifyIcon) == 0)
100706f2543Smrg    {
101706f2543Smrg      ErrorF ("winDeleteNotifyIcon - DestroyIcon failed\n");
102706f2543Smrg    }
103706f2543Smrg  pScreenPriv->hiconNotifyIcon = NULL;
104706f2543Smrg}
105706f2543Smrg
106706f2543Smrg
107706f2543Smrg/*
108706f2543Smrg * Process messages intended for the tray icon
109706f2543Smrg */
110706f2543Smrg
111706f2543SmrgLRESULT
112706f2543SmrgwinHandleIconMessage (HWND hwnd, UINT message,
113706f2543Smrg		      WPARAM wParam, LPARAM lParam,
114706f2543Smrg		      winPrivScreenPtr pScreenPriv)
115706f2543Smrg{
116706f2543Smrg#if defined(XWIN_MULTIWINDOWEXTWM) || defined(XWIN_MULTIWINDOW)
117706f2543Smrg  winScreenInfo		*pScreenInfo = pScreenPriv->pScreenInfo;
118706f2543Smrg#endif
119706f2543Smrg
120706f2543Smrg  switch (lParam)
121706f2543Smrg    {
122706f2543Smrg    case WM_LBUTTONUP:
123706f2543Smrg      /* Restack and bring all windows to top */
124706f2543Smrg      SetForegroundWindow (hwnd);
125706f2543Smrg
126706f2543Smrg#ifdef XWIN_MULTIWINDOWEXTWM
127706f2543Smrg      if (pScreenInfo->fMWExtWM)
128706f2543Smrg	winMWExtWMRestackWindows (pScreenInfo->pScreen);
129706f2543Smrg#endif
130706f2543Smrg      break;
131706f2543Smrg
132706f2543Smrg    case WM_LBUTTONDBLCLK:
133706f2543Smrg      /* Display Exit dialog box */
134706f2543Smrg      winDisplayExitDialog (pScreenPriv);
135706f2543Smrg      break;
136706f2543Smrg
137706f2543Smrg    case WM_RBUTTONUP:
138706f2543Smrg      {
139706f2543Smrg	POINT		ptCursor;
140706f2543Smrg	HMENU		hmenuPopup;
141706f2543Smrg	HMENU		hmenuTray;
142706f2543Smrg
143706f2543Smrg	/* Get cursor position */
144706f2543Smrg	GetCursorPos (&ptCursor);
145706f2543Smrg
146706f2543Smrg	/* Load tray icon menu resource */
147706f2543Smrg	hmenuPopup = LoadMenu (g_hInstance,
148706f2543Smrg			       MAKEINTRESOURCE(IDM_TRAYICON_MENU));
149706f2543Smrg	if (!hmenuPopup)
150706f2543Smrg	  ErrorF ("winHandleIconMessage - LoadMenu failed\n");
151706f2543Smrg
152706f2543Smrg	/* Get actual tray icon menu */
153706f2543Smrg	hmenuTray = GetSubMenu (hmenuPopup, 0);
154706f2543Smrg
155706f2543Smrg#ifdef XWIN_MULTIWINDOW
156706f2543Smrg	/* Check for MultiWindow mode */
157706f2543Smrg	if (pScreenInfo->fMultiWindow)
158706f2543Smrg	  {
159706f2543Smrg	    MENUITEMINFO		mii = {0};
160706f2543Smrg
161706f2543Smrg	    /* Root is shown, remove the check box */
162706f2543Smrg
163706f2543Smrg	    /* Setup menu item info structure */
164706f2543Smrg	    mii.cbSize = sizeof (MENUITEMINFO);
165706f2543Smrg	    mii.fMask = MIIM_STATE;
166706f2543Smrg	    mii.fState = MFS_CHECKED;
167706f2543Smrg
168706f2543Smrg	    /* Unheck box if root is shown */
169706f2543Smrg	    if (pScreenPriv->fRootWindowShown)
170706f2543Smrg	      mii.fState = MFS_UNCHECKED;
171706f2543Smrg
172706f2543Smrg	    /* Set menu state */
173706f2543Smrg	    SetMenuItemInfo (hmenuTray, ID_APP_HIDE_ROOT, FALSE, &mii);
174706f2543Smrg	  }
175706f2543Smrg	else
176706f2543Smrg#endif
177706f2543Smrg	  {
178706f2543Smrg	    /* Remove Hide Root Window button */
179706f2543Smrg	    RemoveMenu (hmenuTray,
180706f2543Smrg			ID_APP_HIDE_ROOT,
181706f2543Smrg			MF_BYCOMMAND);
182706f2543Smrg	  }
183706f2543Smrg
184706f2543Smrg	SetupRootMenu ((unsigned long)hmenuTray);
185706f2543Smrg
186706f2543Smrg	/*
187706f2543Smrg	 * NOTE: This three-step procedure is required for
188706f2543Smrg	 * proper popup menu operation.  Without the
189706f2543Smrg	 * call to SetForegroundWindow the
190706f2543Smrg	 * popup menu will often not disappear when you click
191706f2543Smrg	 * outside of it.  Without the PostMessage the second
192706f2543Smrg	 * time you display the popup menu it might immediately
193706f2543Smrg	 * disappear.
194706f2543Smrg	 */
195706f2543Smrg	SetForegroundWindow (hwnd);
196706f2543Smrg	TrackPopupMenuEx (hmenuTray,
197706f2543Smrg			  TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON,
198706f2543Smrg			  ptCursor.x, ptCursor.y,
199706f2543Smrg			  hwnd,
200706f2543Smrg			  NULL);
201706f2543Smrg	PostMessage (hwnd, WM_NULL, 0, 0);
202706f2543Smrg
203706f2543Smrg	/* Free menu */
204706f2543Smrg	DestroyMenu (hmenuPopup);
205706f2543Smrg      }
206706f2543Smrg      break;
207706f2543Smrg    }
208706f2543Smrg
209706f2543Smrg  return 0;
210706f2543Smrg}
211