wined3d: Remove some unneeded context cache members from the device.
[wine/multimedia.git] / programs / explorer / systray.c
blob7fc66fd0e255d32eaf9d8d3e3260ef19e74591ad
1 /*
2 * Copyright (C) 2004 Mike Hearn, for CodeWeavers
3 * Copyright (C) 2005 Robert Shearman
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* There are two types of window involved here. The first is the
21 * listener window. This is like the taskbar in Windows. It doesn't
22 * ever appear on-screen in our implementation, instead we create
23 * individual mini "adaptor" windows which are docked by the native
24 * systray host.
26 * In future for those who don't have a systray we could make the
27 * listener window more clever so it can draw itself like the Windows
28 * tray area does (with a clock and stuff).
31 #include <assert.h>
33 #define UNICODE
34 #define _WIN32_IE 0x500
35 #include <windows.h>
36 #include <commctrl.h>
38 #include <wine/debug.h>
39 #include <wine/list.h>
41 #include "explorer_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(systray);
45 #define IS_OPTION_FALSE(ch) \
46 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
48 static const WCHAR adaptor_classname[] = /* Adaptor */ {'A','d','a','p','t','o','r',0};
50 /* tray state */
51 struct tray
53 HWND window;
54 struct list icons;
57 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
58 struct icon
60 struct list entry;
61 HICON image; /* the image to render */
62 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
63 HWND window; /* the adaptor window */
64 HWND tooltip; /* Icon tooltip */
65 UINT id; /* the unique id given by the app */
66 UINT callback_message;
69 static struct tray tray;
70 static BOOL hide_systray;
72 /* adaptor code */
74 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
75 /* space around icon (forces icon to center of KDE systray area) */
76 #define ICON_BORDER 4
78 static LRESULT WINAPI adaptor_wndproc(HWND window, UINT msg,
79 WPARAM wparam, LPARAM lparam)
81 struct icon *icon = NULL;
82 BOOL ret;
84 WINE_TRACE("hwnd=%p, msg=0x%x\n", window, msg);
86 /* set the icon data for the window from the data passed into CreateWindow */
87 if (msg == WM_NCCREATE)
88 SetWindowLongPtrW(window, GWLP_USERDATA, (LPARAM)((const CREATESTRUCT *)lparam)->lpCreateParams);
90 icon = (struct icon *) GetWindowLongPtr(window, GWLP_USERDATA);
92 switch (msg)
94 case WM_PAINT:
96 RECT rc;
97 int top;
98 PAINTSTRUCT ps;
99 HDC hdc;
101 WINE_TRACE("painting\n");
103 hdc = BeginPaint(window, &ps);
104 GetClientRect(window, &rc);
106 /* calculate top so we can deal with arbitrary sized trays */
107 top = ((rc.bottom-rc.top)/2) - ((ICON_SIZE)/2);
109 DrawIconEx(hdc, (ICON_BORDER/2), top, icon->image,
110 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
112 EndPaint(window, &ps);
113 break;
116 case WM_MOUSEMOVE:
117 case WM_LBUTTONDOWN:
118 case WM_LBUTTONUP:
119 case WM_RBUTTONDOWN:
120 case WM_RBUTTONUP:
121 case WM_MBUTTONDOWN:
122 case WM_MBUTTONUP:
123 case WM_LBUTTONDBLCLK:
124 case WM_RBUTTONDBLCLK:
125 case WM_MBUTTONDBLCLK:
127 /* notify the owner hwnd of the message */
128 WINE_TRACE("relaying 0x%x\n", msg);
129 ret = PostMessage(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
130 if (!ret && (GetLastError() == ERROR_INVALID_HANDLE))
132 WINE_WARN("application window was destroyed without removing "
133 "notification icon, removing automatically\n");
134 DestroyWindow(window);
136 return 0;
139 case WM_NCDESTROY:
140 SetWindowLongPtr(window, GWLP_USERDATA, 0);
142 list_remove(&icon->entry);
143 DestroyIcon(icon->image);
144 HeapFree(GetProcessHeap(), 0, icon);
145 break;
148 return DefWindowProc(window, msg, wparam, lparam);
152 /* listener code */
154 static struct icon *get_icon(HWND owner, UINT id)
156 struct icon *this;
158 /* search for the icon */
159 LIST_FOR_EACH_ENTRY( this, &tray.icons, struct icon, entry )
160 if ((this->id == id) && (this->owner == owner)) return this;
162 return NULL;
165 static void set_tooltip(struct icon *icon, WCHAR *szTip, BOOL modify)
167 TTTOOLINFOW ti;
169 ti.cbSize = sizeof(TTTOOLINFOW);
170 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
171 ti.hwnd = icon->window;
172 ti.hinst = 0;
173 ti.uId = (UINT_PTR)icon->window;
174 ti.lpszText = szTip;
175 ti.lParam = 0;
176 ti.lpReserved = NULL;
178 if (modify)
179 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
180 else
181 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
184 static void modify_icon(NOTIFYICONDATAW *nid, BOOL modify_tooltip)
186 struct icon *icon;
188 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
190 /* demarshal the request from the NID */
191 icon = get_icon(nid->hWnd, nid->uID);
192 if (!icon)
194 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
195 return;
198 if (nid->uFlags & NIF_ICON)
200 if (icon->image) DestroyIcon(icon->image);
201 icon->image = CopyIcon(nid->hIcon);
203 RedrawWindow(icon->window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
206 if (nid->uFlags & NIF_MESSAGE)
208 icon->callback_message = nid->uCallbackMessage;
210 if (nid->uFlags & NIF_TIP)
212 set_tooltip(icon, nid->szTip, modify_tooltip);
216 static void add_icon(NOTIFYICONDATAW *nid)
218 RECT rect;
219 struct icon *icon;
220 static const WCHAR adaptor_windowname[] = /* Wine System Tray Adaptor */ {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',' ','A','d','a','p','t','o','r',0};
221 static BOOL tooltps_initialized = FALSE;
223 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
225 if ((icon = get_icon(nid->hWnd, nid->uID)))
227 WINE_WARN("duplicate tray icon add, buggy app?\n");
228 return;
231 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
233 WINE_ERR("out of memory\n");
234 return;
237 icon->id = nid->uID;
238 icon->owner = nid->hWnd;
239 icon->image = NULL;
241 rect.left = 0;
242 rect.top = 0;
243 rect.right = GetSystemMetrics(SM_CXSMICON) + ICON_BORDER;
244 rect.bottom = GetSystemMetrics(SM_CYSMICON) + ICON_BORDER;
245 AdjustWindowRect(&rect, WS_CLIPSIBLINGS | WS_CAPTION, FALSE);
247 /* create the adaptor window */
248 icon->window = CreateWindowEx(WS_EX_TRAYWINDOW, adaptor_classname,
249 adaptor_windowname,
250 WS_CLIPSIBLINGS | WS_CAPTION,
251 CW_USEDEFAULT, CW_USEDEFAULT,
252 rect.right - rect.left,
253 rect.bottom - rect.top,
254 NULL, NULL, NULL, icon);
256 if (!hide_systray)
257 ShowWindow(icon->window, SW_SHOWNA);
259 /* create icon tooltip */
261 /* Register tooltip classes if this is the first icon */
262 if (!tooltps_initialized)
264 INITCOMMONCONTROLSEX init_tooltip;
266 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
267 init_tooltip.dwICC = ICC_TAB_CLASSES;
269 InitCommonControlsEx(&init_tooltip);
270 tooltps_initialized = TRUE;
273 icon->tooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
274 WS_POPUP | TTS_ALWAYSTIP,
275 CW_USEDEFAULT, CW_USEDEFAULT,
276 CW_USEDEFAULT, CW_USEDEFAULT,
277 icon->window, NULL, NULL, NULL);
279 list_add_tail(&tray.icons, &icon->entry);
281 modify_icon(nid, FALSE);
284 static void delete_icon(const NOTIFYICONDATAW *nid)
286 struct icon *icon = get_icon(nid->hWnd, nid->uID);
288 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
290 if (!icon)
292 WINE_ERR("invalid tray icon ID specified: %ud\n", nid->uID);
293 return;
296 DestroyWindow(icon->tooltip);
297 DestroyWindow(icon->window);
300 static void handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
302 NOTIFYICONDATAW nid;
304 if (cds->cbData < sizeof(nid)) return;
305 memcpy(&nid, cds->lpData, sizeof(nid));
307 /* FIXME: if statement only needed because we don't support interprocess
308 * icon handles */
309 if ((nid.uFlags & NIF_ICON) && (cds->cbData >= sizeof(nid) + 2 * sizeof(BITMAP)))
311 LONG cbMaskBits;
312 LONG cbColourBits;
313 BITMAP bmMask;
314 BITMAP bmColour;
315 const char *buffer = cds->lpData;
317 buffer += sizeof(nid);
319 memcpy(&bmMask, buffer, sizeof(bmMask));
320 buffer += sizeof(bmMask);
321 memcpy(&bmColour, buffer, sizeof(bmColour));
322 buffer += sizeof(bmColour);
324 cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
325 cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
327 if (cds->cbData < sizeof(nid) + 2 * sizeof(BITMAP) + cbMaskBits + cbColourBits)
329 WINE_ERR("buffer underflow\n");
330 return;
333 /* sanity check */
334 if ((bmColour.bmWidth != bmMask.bmWidth) || (bmColour.bmHeight != bmMask.bmHeight))
336 WINE_ERR("colour and mask bitmaps aren't consistent\n");
337 return;
340 nid.hIcon = CreateIcon(NULL, bmColour.bmWidth, bmColour.bmHeight,
341 bmColour.bmPlanes, bmColour.bmBitsPixel,
342 buffer, buffer + cbMaskBits);
345 switch (cds->dwData)
347 case NIM_ADD:
348 add_icon(&nid);
349 break;
350 case NIM_DELETE:
351 delete_icon(&nid);
352 break;
353 case NIM_MODIFY:
354 modify_icon(&nid, TRUE);
355 break;
356 default:
357 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
358 break;
361 /* FIXME: if statement only needed because we don't support interprocess
362 * icon handles */
363 if (nid.uFlags & NIF_ICON)
364 DestroyIcon(nid.hIcon);
367 static LRESULT WINAPI listener_wndproc(HWND window, UINT msg,
368 WPARAM wparam, LPARAM lparam)
370 if (msg == WM_COPYDATA)
371 handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
373 return DefWindowProc(window, msg, wparam, lparam);
377 static BOOL is_systray_hidden(void)
379 const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
380 'X','1','1',' ','D','r','i','v','e','r',0};
381 const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
382 HKEY hkey;
383 BOOL ret = FALSE;
385 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
386 if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
388 WCHAR value[10];
389 DWORD type, size = sizeof(value);
390 if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
392 ret = IS_OPTION_FALSE(value[0]);
394 RegCloseKey(hkey);
396 return ret;
399 /* this function creates the the listener window */
400 void initialize_systray(void)
402 WNDCLASSEX class;
403 static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
404 static const WCHAR winname[] = /* Wine Systray Listener */
405 {'W','i','n','e',' ','S','y','s','t','r','a','y',' ','L','i','s','t','e','n','e','r',0};
407 WINE_TRACE("initiaizing\n");
409 hide_systray = is_systray_hidden();
411 list_init(&tray.icons);
413 /* register the systray listener window class */
414 ZeroMemory(&class, sizeof(class));
415 class.cbSize = sizeof(class);
416 class.lpfnWndProc = &listener_wndproc;
417 class.hInstance = NULL;
418 class.hIcon = LoadIcon(0, IDI_WINLOGO);
419 class.hCursor = LoadCursor(0, IDC_ARROW);
420 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
421 class.lpszClassName = (WCHAR *) &classname;
423 if (!RegisterClassEx(&class))
425 WINE_ERR("Could not register SysTray window class\n");
426 return;
429 /* now register the adaptor window class */
430 ZeroMemory(&class, sizeof(class));
431 class.cbSize = sizeof(class);
432 class.lpfnWndProc = adaptor_wndproc;
433 class.hInstance = NULL;
434 class.hIcon = LoadIcon(0, IDI_WINLOGO);
435 class.hCursor = LoadCursor(0, IDC_ARROW);
436 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
437 class.lpszClassName = adaptor_classname;
438 class.style = CS_SAVEBITS | CS_DBLCLKS;
440 if (!RegisterClassEx(&class))
442 WINE_ERR("Could not register adaptor class\n");
443 return;
446 tray.window = CreateWindow(classname, winname, WS_OVERLAPPED,
447 CW_USEDEFAULT, CW_USEDEFAULT,
448 0, 0, 0, 0, 0, 0);
450 if (!tray.window)
452 WINE_ERR("Could not create tray window\n");
453 return;