comctl32: A couple fixes for tab icon offsets.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
37 #include <wine/debug.h>
38 #include <wine/list.h>
39
40 #include "explorer_private.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(systray);
43
44 #define IS_OPTION_FALSE(ch) \
45     ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
46
47 const static WCHAR adaptor_classname[] = /* Adaptor */ {'A','d','a','p','t','o','r',0};
48
49 /* tray state */
50 struct tray
51 {
52     HWND           window;
53     struct list    icons;
54 };
55
56 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
57 struct icon
58 {
59     struct list    entry;
60     HICON          image;    /* the image to render */
61     HWND           owner;    /* the HWND passed in to the Shell_NotifyIcon call */
62     HWND           window;   /* the adaptor window */
63     UINT           id;       /* the unique id given by the app */
64     UINT           callback_message;
65 };
66
67 static struct tray tray;
68 static BOOL hide_systray;
69
70 /* adaptor code */
71
72 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
73 /* space around icon (forces icon to center of KDE systray area) */
74 #define ICON_BORDER  4
75
76 static LRESULT WINAPI adaptor_wndproc(HWND window, UINT msg,
77                                       WPARAM wparam, LPARAM lparam)
78 {
79     struct icon *icon = NULL;
80     BOOL ret;
81
82     WINE_TRACE("hwnd=%p, msg=0x%x\n", window, msg);
83
84     /* set the icon data for the window from the data passed into CreateWindow */
85     if (msg == WM_NCCREATE)
86         SetWindowLongPtrW(window, GWLP_USERDATA, (LPARAM)((const CREATESTRUCT *)lparam)->lpCreateParams);
87
88     icon = (struct icon *) GetWindowLongPtr(window, GWLP_USERDATA);
89
90     switch (msg)
91     {
92         case WM_PAINT:
93         {
94             RECT rc;
95             int top;
96             PAINTSTRUCT  ps;
97             HDC          hdc;
98
99             WINE_TRACE("painting\n");
100
101             hdc = BeginPaint(window, &ps);
102             GetClientRect(window, &rc);
103
104             /* calculate top so we can deal with arbitrary sized trays */
105             top = ((rc.bottom-rc.top)/2) - ((ICON_SIZE)/2);
106
107             DrawIconEx(hdc, (ICON_BORDER/2), top, icon->image,
108                        ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
109
110             EndPaint(window, &ps);
111             break;
112         }
113
114         case WM_MOUSEMOVE:
115         case WM_LBUTTONDOWN:
116         case WM_LBUTTONUP:
117         case WM_RBUTTONDOWN:
118         case WM_RBUTTONUP:
119         case WM_MBUTTONDOWN:
120         case WM_MBUTTONUP:
121         case WM_LBUTTONDBLCLK:
122         case WM_RBUTTONDBLCLK:
123         case WM_MBUTTONDBLCLK:
124         {
125             /* notify the owner hwnd of the message */
126             WINE_TRACE("relaying 0x%x\n", msg);
127             ret = PostMessage(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
128             if (!ret && (GetLastError() == ERROR_INVALID_HANDLE))
129             {
130                 WINE_WARN("application window was destroyed without removing "
131                           "notification icon, removing automatically\n");
132                 DestroyWindow(window);
133             }
134             return 0;
135         }
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
146     return DefWindowProc(window, msg, wparam, lparam);
147 }
148
149
150 /* listener code */
151
152 static struct icon *get_icon(HWND owner, UINT id)
153 {
154     struct icon *this;
155
156     /* search for the icon */
157     LIST_FOR_EACH_ENTRY( this, &tray.icons, struct icon, entry )
158         if ((this->id == id) && (this->owner = owner)) return this;
159
160     return NULL;
161 }
162
163 static void modify_icon(const NOTIFYICONDATAW *nid)
164 {
165     struct icon    *icon;
166
167     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
168
169     /* demarshal the request from the NID */
170     icon = get_icon(nid->hWnd, nid->uID);
171     if (!icon)
172     {
173         WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
174         return;
175     }
176
177     if (nid->uFlags & NIF_ICON)
178     {
179         if (icon->image) DestroyIcon(icon->image);
180         icon->image = CopyIcon(nid->hIcon);
181
182         RedrawWindow(icon->window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
183     }
184
185     if (nid->uFlags & NIF_MESSAGE)
186     {
187         icon->callback_message = nid->uCallbackMessage;
188     }
189 }
190
191 static void add_icon(const NOTIFYICONDATAW *nid)
192 {
193     RECT rect;
194     struct icon  *icon;
195     const static 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};
196
197     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
198
199     if ((icon = get_icon(nid->hWnd, nid->uID)))
200     {
201         WINE_WARN("duplicate tray icon add, buggy app?\n");
202         return;
203     }
204
205     if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
206     {
207         WINE_ERR("out of memory\n");
208         return;
209     }
210
211     icon->id    = nid->uID;
212     icon->owner = nid->hWnd;
213     icon->image = NULL;
214
215     rect.left = 0;
216     rect.top = 0;
217     rect.right = GetSystemMetrics(SM_CXSMICON) + ICON_BORDER;
218     rect.bottom = GetSystemMetrics(SM_CYSMICON) + ICON_BORDER;
219     AdjustWindowRect(&rect, WS_CLIPSIBLINGS | WS_CAPTION, FALSE);
220
221     /* create the adaptor window */
222     icon->window = CreateWindowEx(WS_EX_TRAYWINDOW, adaptor_classname,
223                                   adaptor_windowname,
224                                   WS_CLIPSIBLINGS | WS_CAPTION,
225                                   CW_USEDEFAULT, CW_USEDEFAULT,
226                                   rect.right - rect.left,
227                                   rect.bottom - rect.top,
228                                   NULL, NULL, NULL, icon);
229
230     if (!hide_systray)
231         ShowWindow(icon->window, SW_SHOWNA);
232
233     list_add_tail(&tray.icons, &icon->entry);
234
235     modify_icon(nid);
236 }
237
238 static void delete_icon(const NOTIFYICONDATAW *nid)
239 {
240     struct icon *icon = get_icon(nid->hWnd, nid->uID);
241
242     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
243    
244     if (!icon)
245     {
246         WINE_ERR("invalid tray icon ID specified: %ud\n", nid->uID);
247         return;
248     }
249
250     DestroyWindow(icon->window);
251 }
252
253 static void handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
254 {
255     NOTIFYICONDATAW nid;
256
257     if (cds->cbData < sizeof(nid)) return;
258     memcpy(&nid, cds->lpData, sizeof(nid));
259
260     /* FIXME: if statement only needed because we don't support interprocess
261      * icon handles */
262     if (nid.uFlags & NIF_ICON)
263     {
264         LONG cbMaskBits;
265         LONG cbColourBits;
266         BITMAP bmMask;
267         BITMAP bmColour;
268         const char *buffer = cds->lpData;
269
270         buffer += sizeof(nid);
271
272         if (cds->cbData < sizeof(nid) + 2 * sizeof(BITMAP))
273         {
274             WINE_ERR("buffer underflow\n");
275             return;
276         }
277
278         memcpy(&bmMask, buffer, sizeof(bmMask));
279         buffer += sizeof(bmMask);
280         memcpy(&bmColour, buffer, sizeof(bmColour));
281         buffer += sizeof(bmColour);
282
283         cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
284         cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
285
286         if (cds->cbData < sizeof(nid) + 2 * sizeof(BITMAP) + cbMaskBits + cbColourBits)
287         {
288             WINE_ERR("buffer underflow\n");
289             return;
290         }
291
292         /* sanity check */
293         if ((bmColour.bmWidth != bmMask.bmWidth) || (bmColour.bmHeight != bmMask.bmHeight))
294         {
295             WINE_ERR("colour and mask bitmaps aren't consistent\n");
296             return;
297         }
298
299         nid.hIcon = CreateIcon(NULL, bmColour.bmWidth, bmColour.bmHeight,
300                                bmColour.bmPlanes, bmColour.bmBitsPixel,
301                                buffer, buffer + cbMaskBits);
302     }
303
304     switch (cds->dwData)
305     {
306     case NIM_ADD:
307         add_icon(&nid);
308         break;
309     case NIM_DELETE:
310         delete_icon(&nid);
311         break;
312     case NIM_MODIFY:
313         modify_icon(&nid);
314         break;
315     default:
316         WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
317         break;
318     }
319
320     /* FIXME: if statement only needed because we don't support interprocess
321      * icon handles */
322     if (nid.uFlags & NIF_ICON)
323         DestroyIcon(nid.hIcon);
324 }
325
326 static LRESULT WINAPI listener_wndproc(HWND window, UINT msg,
327                                        WPARAM wparam, LPARAM lparam)
328 {
329     if (msg == WM_COPYDATA)
330         handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
331
332     return DefWindowProc(window, msg, wparam, lparam);
333 }
334
335
336 static BOOL is_systray_hidden(void)
337 {
338     const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
339                                           'X','1','1',' ','D','r','i','v','e','r',0};
340     const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
341     HKEY hkey;
342     BOOL ret = FALSE;
343
344     /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
345     if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
346     {
347         WCHAR value[10];
348         DWORD type, size = sizeof(value);
349         if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
350         {
351             ret = IS_OPTION_FALSE(value[0]);
352         }
353         RegCloseKey(hkey);
354     }
355     return ret;
356 }
357
358 /* this function creates the the listener window */
359 void initialize_systray(void)
360 {
361     WNDCLASSEX class;
362     static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
363     static const WCHAR winname[]   = /* Wine Systray Listener */
364         {'W','i','n','e',' ','S','y','s','t','r','a','y',' ','L','i','s','t','e','n','e','r',0};
365
366     WINE_TRACE("initiaizing\n");
367
368     hide_systray = is_systray_hidden();
369
370     list_init(&tray.icons);
371
372     /* register the systray listener window class */
373     ZeroMemory(&class, sizeof(class));
374     class.cbSize        = sizeof(class);
375     class.lpfnWndProc   = &listener_wndproc;
376     class.hInstance     = NULL;
377     class.hIcon         = LoadIcon(0, IDI_WINLOGO);
378     class.hCursor       = LoadCursor(0, IDC_ARROW);
379     class.hbrBackground = (HBRUSH) COLOR_WINDOW;
380     class.lpszClassName = (WCHAR *) &classname;
381
382     if (!RegisterClassEx(&class))
383     {
384         WINE_ERR("Could not register SysTray window class\n");
385         return;
386     }
387
388     /* now register the adaptor window class */
389     ZeroMemory(&class, sizeof(class));
390     class.cbSize        = sizeof(class);
391     class.lpfnWndProc   = adaptor_wndproc;
392     class.hInstance     = NULL;
393     class.hIcon         = LoadIcon(0, IDI_WINLOGO);
394     class.hCursor       = LoadCursor(0, IDC_ARROW);
395     class.hbrBackground = (HBRUSH) COLOR_WINDOW;
396     class.lpszClassName = adaptor_classname;
397     class.style         = CS_SAVEBITS | CS_DBLCLKS;
398
399     if (!RegisterClassEx(&class))
400     {
401         WINE_ERR("Could not register adaptor class\n");
402         return;
403     }
404
405     tray.window = CreateWindow(classname, winname, WS_OVERLAPPED,
406                                CW_USEDEFAULT, CW_USEDEFAULT,
407                                0, 0, 0, 0, 0, 0);
408
409     if (!tray.window)
410     {
411         WINE_ERR("Could not create tray window\n");
412         return;
413     }
414 }