wined3d: Use native shader limits instead of the maximum the driver can handle in...
[wine] / programs / explorer / systray.c
1 /*
2  * Copyright (C) 2004 Mike Hearn, for CodeWeavers
3  * Copyright (C) 2005 Robert Shearman
4  *
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.
9  *
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.
14  *
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
18  */
19
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.
25  *
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).
29  */
30
31 #include <assert.h>
32
33 #define UNICODE
34 #define _WIN32_IE 0x500
35 #include <windows.h>
36 #include <commctrl.h>
37
38 #include <wine/debug.h>
39 #include <wine/list.h>
40
41 #include "explorer_private.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(systray);
44
45 #define IS_OPTION_FALSE(ch) \
46     ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
47
48 static const WCHAR adaptor_classname[] = /* Adaptor */ {'A','d','a','p','t','o','r',0};
49
50 /* tray state */
51 struct tray
52 {
53     HWND           window;
54     struct list    icons;
55 };
56
57 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
58 struct icon
59 {
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;
67 };
68
69 static struct tray tray;
70 static BOOL hide_systray;
71
72 /* adaptor code */
73
74 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
75 /* space around icon (forces icon to center of KDE systray area) */
76 #define ICON_BORDER  4
77
78 static LRESULT WINAPI adaptor_wndproc(HWND window, UINT msg,
79                                       WPARAM wparam, LPARAM lparam)
80 {
81     struct icon *icon = NULL;
82     BOOL ret;
83
84     WINE_TRACE("hwnd=%p, msg=0x%x\n", window, msg);
85
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);
89
90     icon = (struct icon *) GetWindowLongPtr(window, GWLP_USERDATA);
91
92     switch (msg)
93     {
94         case WM_PAINT:
95         {
96             RECT rc;
97             int top;
98             PAINTSTRUCT  ps;
99             HDC          hdc;
100
101             WINE_TRACE("painting\n");
102
103             hdc = BeginPaint(window, &ps);
104             GetClientRect(window, &rc);
105
106             /* calculate top so we can deal with arbitrary sized trays */
107             top = ((rc.bottom-rc.top)/2) - ((ICON_SIZE)/2);
108
109             DrawIconEx(hdc, (ICON_BORDER/2), top, icon->image,
110                        ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
111
112             EndPaint(window, &ps);
113             break;
114         }
115
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:
126             /* notify the owner hwnd of the message */
127             WINE_TRACE("relaying 0x%x\n", msg);
128             ret = PostMessage(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
129             if (!ret && (GetLastError() == ERROR_INVALID_HANDLE))
130             {
131                 WINE_WARN("application window was destroyed without removing "
132                           "notification icon, removing automatically\n");
133                 DestroyWindow(window);
134             }
135             break;
136
137         case WM_NCDESTROY:
138             SetWindowLongPtr(window, GWLP_USERDATA, 0);
139
140             list_remove(&icon->entry);
141             DestroyIcon(icon->image);
142             HeapFree(GetProcessHeap(), 0, icon);
143             break;
144
145         default:
146             return DefWindowProc(window, msg, wparam, lparam);
147     }
148
149     return 0;
150 }
151
152
153 /* listener code */
154
155 static struct icon *get_icon(HWND owner, UINT id)
156 {
157     struct icon *this;
158
159     /* search for the icon */
160     LIST_FOR_EACH_ENTRY( this, &tray.icons, struct icon, entry )
161         if ((this->id == id) && (this->owner == owner)) return this;
162
163     return NULL;
164 }
165
166 static void set_tooltip(struct icon *icon, WCHAR *szTip, BOOL modify)
167 {
168     TTTOOLINFOW ti;
169
170     ti.cbSize = sizeof(TTTOOLINFOW);
171     ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
172     ti.hwnd = icon->window;
173     ti.hinst = 0;
174     ti.uId = (UINT_PTR)icon->window;
175     ti.lpszText = szTip;
176     ti.lParam = 0;
177     ti.lpReserved = NULL;
178
179     if (modify)
180         SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
181     else
182         SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
183 }
184
185 static void modify_icon(NOTIFYICONDATAW *nid, BOOL modify_tooltip)
186 {
187     struct icon    *icon;
188
189     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
190
191     /* demarshal the request from the NID */
192     icon = get_icon(nid->hWnd, nid->uID);
193     if (!icon)
194     {
195         WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
196         return;
197     }
198
199     if (nid->uFlags & NIF_ICON)
200     {
201         if (icon->image) DestroyIcon(icon->image);
202         icon->image = CopyIcon(nid->hIcon);
203
204         RedrawWindow(icon->window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
205     }
206
207     if (nid->uFlags & NIF_MESSAGE)
208     {
209         icon->callback_message = nid->uCallbackMessage;
210     }
211     if (nid->uFlags & NIF_TIP)
212     {
213         set_tooltip(icon, nid->szTip, modify_tooltip);
214     }
215 }
216
217 static void add_icon(NOTIFYICONDATAW *nid)
218 {
219     RECT rect;
220     struct icon  *icon;
221     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};
222     static BOOL tooltps_initialized = FALSE;
223
224     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
225
226     if ((icon = get_icon(nid->hWnd, nid->uID)))
227     {
228         WINE_WARN("duplicate tray icon add, buggy app?\n");
229         return;
230     }
231
232     if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
233     {
234         WINE_ERR("out of memory\n");
235         return;
236     }
237
238     icon->id    = nid->uID;
239     icon->owner = nid->hWnd;
240     icon->image = NULL;
241
242     rect.left = 0;
243     rect.top = 0;
244     rect.right = GetSystemMetrics(SM_CXSMICON) + ICON_BORDER;
245     rect.bottom = GetSystemMetrics(SM_CYSMICON) + ICON_BORDER;
246     AdjustWindowRect(&rect, WS_CLIPSIBLINGS | WS_CAPTION, FALSE);
247
248     /* create the adaptor window */
249     icon->window = CreateWindowEx(WS_EX_TRAYWINDOW, adaptor_classname,
250                                   adaptor_windowname,
251                                   WS_CLIPSIBLINGS | WS_CAPTION,
252                                   CW_USEDEFAULT, CW_USEDEFAULT,
253                                   rect.right - rect.left,
254                                   rect.bottom - rect.top,
255                                   NULL, NULL, NULL, icon);
256
257     if (!hide_systray)
258         ShowWindow(icon->window, SW_SHOWNA);
259
260     /* create icon tooltip */
261
262     /* Register tooltip classes if this is the first icon */
263     if (!tooltps_initialized)
264     {
265         INITCOMMONCONTROLSEX init_tooltip;
266
267         init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
268         init_tooltip.dwICC = ICC_TAB_CLASSES;
269
270         InitCommonControlsEx(&init_tooltip);
271         tooltps_initialized = TRUE;
272     }
273
274     icon->tooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
275                                    WS_POPUP | TTS_ALWAYSTIP,
276                                    CW_USEDEFAULT, CW_USEDEFAULT,
277                                    CW_USEDEFAULT, CW_USEDEFAULT,
278                                    icon->window, NULL, NULL, NULL);
279
280     list_add_tail(&tray.icons, &icon->entry);
281
282     modify_icon(nid, FALSE);
283 }
284
285 static void delete_icon(const NOTIFYICONDATAW *nid)
286 {
287     struct icon *icon = get_icon(nid->hWnd, nid->uID);
288
289     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
290    
291     if (!icon)
292     {
293         WINE_ERR("invalid tray icon ID specified: %u\n", nid->uID);
294         return;
295     }
296
297     DestroyWindow(icon->tooltip);
298     DestroyWindow(icon->window);
299 }
300
301 static void handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
302 {
303     NOTIFYICONDATAW nid;
304     DWORD cbSize;
305
306     if (cds->cbData < NOTIFYICONDATAW_V1_SIZE) return;
307     cbSize = ((PNOTIFYICONDATA)cds->lpData)->cbSize;
308     if (cbSize < NOTIFYICONDATAW_V1_SIZE) return;
309
310     ZeroMemory(&nid, sizeof(nid));
311     memcpy(&nid, cds->lpData, min(sizeof(nid), cbSize));
312
313     /* FIXME: if statement only needed because we don't support interprocess
314      * icon handles */
315     if ((nid.uFlags & NIF_ICON) && (cds->cbData >= nid.cbSize + 2 * sizeof(BITMAP)))
316     {
317         LONG cbMaskBits;
318         LONG cbColourBits;
319         BITMAP bmMask;
320         BITMAP bmColour;
321         const char *buffer = cds->lpData;
322
323         buffer += nid.cbSize;
324
325         memcpy(&bmMask, buffer, sizeof(bmMask));
326         buffer += sizeof(bmMask);
327         memcpy(&bmColour, buffer, sizeof(bmColour));
328         buffer += sizeof(bmColour);
329
330         cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
331         cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
332
333         if (cds->cbData < nid.cbSize + 2 * sizeof(BITMAP) + cbMaskBits + cbColourBits)
334         {
335             WINE_ERR("buffer underflow\n");
336             return;
337         }
338
339         /* sanity check */
340         if ((bmColour.bmWidth != bmMask.bmWidth) || (bmColour.bmHeight != bmMask.bmHeight))
341         {
342             WINE_ERR("colour and mask bitmaps aren't consistent\n");
343             return;
344         }
345
346         nid.hIcon = CreateIcon(NULL, bmColour.bmWidth, bmColour.bmHeight,
347                                bmColour.bmPlanes, bmColour.bmBitsPixel,
348                                buffer, buffer + cbMaskBits);
349     }
350
351     switch (cds->dwData)
352     {
353     case NIM_ADD:
354         add_icon(&nid);
355         break;
356     case NIM_DELETE:
357         delete_icon(&nid);
358         break;
359     case NIM_MODIFY:
360         modify_icon(&nid, TRUE);
361         break;
362     default:
363         WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
364         break;
365     }
366
367     /* FIXME: if statement only needed because we don't support interprocess
368      * icon handles */
369     if (nid.uFlags & NIF_ICON)
370         DestroyIcon(nid.hIcon);
371 }
372
373 static LRESULT WINAPI listener_wndproc(HWND window, UINT msg,
374                                        WPARAM wparam, LPARAM lparam)
375 {
376     if (msg == WM_COPYDATA)
377         handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
378
379     return DefWindowProc(window, msg, wparam, lparam);
380 }
381
382
383 static BOOL is_systray_hidden(void)
384 {
385     const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
386                                           'X','1','1',' ','D','r','i','v','e','r',0};
387     const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
388     HKEY hkey;
389     BOOL ret = FALSE;
390
391     /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
392     if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
393     {
394         WCHAR value[10];
395         DWORD type, size = sizeof(value);
396         if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
397         {
398             ret = IS_OPTION_FALSE(value[0]);
399         }
400         RegCloseKey(hkey);
401     }
402     return ret;
403 }
404
405 /* this function creates the listener window */
406 void initialize_systray(void)
407 {
408     WNDCLASSEX class;
409     static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
410     static const WCHAR winname[]   = /* Wine Systray Listener */
411         {'W','i','n','e',' ','S','y','s','t','r','a','y',' ','L','i','s','t','e','n','e','r',0};
412
413     WINE_TRACE("initiaizing\n");
414
415     hide_systray = is_systray_hidden();
416
417     list_init(&tray.icons);
418
419     /* register the systray listener window class */
420     ZeroMemory(&class, sizeof(class));
421     class.cbSize        = sizeof(class);
422     class.lpfnWndProc   = &listener_wndproc;
423     class.hInstance     = NULL;
424     class.hIcon         = LoadIcon(0, IDI_WINLOGO);
425     class.hCursor       = LoadCursor(0, IDC_ARROW);
426     class.hbrBackground = (HBRUSH) COLOR_WINDOW;
427     class.lpszClassName = (WCHAR *) &classname;
428
429     if (!RegisterClassEx(&class))
430     {
431         WINE_ERR("Could not register SysTray window class\n");
432         return;
433     }
434
435     /* now register the adaptor window class */
436     ZeroMemory(&class, sizeof(class));
437     class.cbSize        = sizeof(class);
438     class.lpfnWndProc   = adaptor_wndproc;
439     class.hInstance     = NULL;
440     class.hIcon         = LoadIcon(0, IDI_WINLOGO);
441     class.hCursor       = LoadCursor(0, IDC_ARROW);
442     class.hbrBackground = (HBRUSH) COLOR_WINDOW;
443     class.lpszClassName = adaptor_classname;
444     class.style         = CS_SAVEBITS | CS_DBLCLKS;
445
446     if (!RegisterClassEx(&class))
447     {
448         WINE_ERR("Could not register adaptor class\n");
449         return;
450     }
451
452     tray.window = CreateWindow(classname, winname, WS_OVERLAPPED,
453                                CW_USEDEFAULT, CW_USEDEFAULT,
454                                0, 0, 0, 0, 0, 0);
455
456     if (!tray.window)
457     {
458         WINE_ERR("Could not create tray window\n");
459         return;
460     }
461 }