winex11: Separate the XIM process-wide setup from the IME creation.
[wine] / dlls / winex11.drv / systray.c
1 /*
2  * X11 system tray management
3  *
4  * Copyright (C) 2004 Mike Hearn, for CodeWeavers
5  * Copyright (C) 2005 Robert Shearman
6  * Copyright (C) 2008 Alexandre Julliard
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #include <X11/Xlib.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "commctrl.h"
40 #include "shellapi.h"
41
42 #include "x11drv.h"
43 #include "wine/list.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(systray);
47
48 /* an individual systray icon */
49 struct tray_icon
50 {
51     struct list    entry;
52     HICON          image;    /* the image to render */
53     HWND           owner;    /* the HWND passed in to the Shell_NotifyIcon call */
54     HWND           window;   /* the adaptor window */
55     HWND           tooltip;  /* Icon tooltip */
56     UINT           id;       /* the unique id given by the app */
57     UINT           callback_message;
58     WCHAR          tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
59 };
60
61 static struct list icon_list = LIST_INIT( icon_list );
62
63 static const WCHAR tray_classname[] = {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','w','i','n','d','o','w',0};
64
65 static BOOL delete_icon( struct tray_icon *icon );
66
67 #define SYSTEM_TRAY_REQUEST_DOCK  0
68 #define SYSTEM_TRAY_BEGIN_MESSAGE   1
69 #define SYSTEM_TRAY_CANCEL_MESSAGE  2
70
71 #define ICON_BORDER 2
72
73 /* retrieves icon record by owner window and ID */
74 static struct tray_icon *get_icon(HWND owner, UINT id)
75 {
76     struct tray_icon *this;
77
78     LIST_FOR_EACH_ENTRY( this, &icon_list, struct tray_icon, entry )
79         if ((this->id == id) && (this->owner == owner)) return this;
80     return NULL;
81 }
82
83 /* create tooltip window for icon */
84 static void create_tooltip(struct tray_icon *icon)
85 {
86     static BOOL tooltips_initialized = FALSE;
87
88     if (!tooltips_initialized)
89     {
90         INITCOMMONCONTROLSEX init_tooltip;
91
92         init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
93         init_tooltip.dwICC = ICC_TAB_CLASSES;
94
95         InitCommonControlsEx(&init_tooltip);
96         tooltips_initialized = TRUE;
97     }
98
99     icon->tooltip = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
100                                      WS_POPUP | TTS_ALWAYSTIP,
101                                      CW_USEDEFAULT, CW_USEDEFAULT,
102                                      CW_USEDEFAULT, CW_USEDEFAULT,
103                                      icon->window, NULL, NULL, NULL);
104     if (icon->tooltip)
105     {
106         TTTOOLINFOW ti;
107         ZeroMemory(&ti, sizeof(ti));
108         ti.cbSize = sizeof(TTTOOLINFOW);
109         ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
110         ti.hwnd = icon->window;
111         ti.uId = (UINT_PTR)icon->window;
112         ti.lpszText = icon->tiptext;
113         SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
114     }
115 }
116
117 /* synchronize tooltip text with tooltip window */
118 static void update_tooltip_text(struct tray_icon *icon)
119 {
120     TTTOOLINFOW ti;
121
122     ZeroMemory(&ti, sizeof(ti));
123     ti.cbSize = sizeof(TTTOOLINFOW);
124     ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
125     ti.hwnd = icon->window;
126     ti.uId = (UINT_PTR)icon->window;
127     ti.lpszText = icon->tiptext;
128
129     SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
130 }
131
132 /* window procedure for the tray window */
133 static LRESULT WINAPI tray_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
134 {
135     struct tray_icon *icon = NULL;
136     BOOL ret;
137
138     WINE_TRACE("hwnd=%p, msg=0x%x\n", hwnd, msg);
139
140     /* set the icon data for the window from the data passed into CreateWindow */
141     if (msg == WM_NCCREATE)
142         SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LPARAM)((const CREATESTRUCTW *)lparam)->lpCreateParams);
143
144     icon = (struct tray_icon *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
145
146     switch (msg)
147     {
148     case WM_PAINT:
149         {
150             PAINTSTRUCT ps;
151             RECT rc;
152             HDC hdc;
153             int cx = GetSystemMetrics( SM_CXSMICON );
154             int cy = GetSystemMetrics( SM_CYSMICON );
155
156             hdc = BeginPaint(hwnd, &ps);
157             GetClientRect(hwnd, &rc);
158             TRACE("painting rect %s\n", wine_dbgstr_rect(&rc));
159             DrawIconEx( hdc, (rc.left + rc.right - cx) / 2, (rc.top + rc.bottom - cy) / 2,
160                         icon->image, cx, cy, 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
161             EndPaint(hwnd, &ps);
162             break;
163         }
164
165     case WM_MOUSEMOVE:
166     case WM_LBUTTONDOWN:
167     case WM_LBUTTONUP:
168     case WM_RBUTTONDOWN:
169     case WM_RBUTTONUP:
170     case WM_MBUTTONDOWN:
171     case WM_MBUTTONUP:
172     case WM_LBUTTONDBLCLK:
173     case WM_RBUTTONDBLCLK:
174     case WM_MBUTTONDBLCLK:
175         /* notify the owner hwnd of the message */
176         TRACE("relaying 0x%x\n", msg);
177         ret = PostMessageW(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
178         if (!ret && (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
179         {
180             WARN( "application window was destroyed, removing icon %u\n", icon->id );
181             delete_icon( icon );
182         }
183         break;
184
185     case WM_TIMER:
186         if (!IsWindow( icon->owner )) delete_icon( icon );
187         return 0;
188
189     default:
190         return DefWindowProcW(hwnd, msg, wparam, lparam);
191     }
192     return 0;
193 }
194
195 /* find the X11 window owner the system tray selection */
196 static Window get_systray_selection_owner( Display *display )
197 {
198     static Atom systray_atom;
199     Window ret;
200
201     if (root_window != DefaultRootWindow( display )) return 0;
202
203     wine_tsx11_lock();
204     if (!systray_atom)
205     {
206         if (DefaultScreen( display ) == 0)
207             systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
208         else
209         {
210             char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
211             sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
212             systray_atom = XInternAtom( display, systray_buffer, False );
213         }
214     }
215     ret = XGetSelectionOwner( display, systray_atom );
216     wine_tsx11_unlock();
217     return ret;
218 }
219
220
221 /* dock the given X window with the NETWM system tray */
222 static void dock_systray_window( HWND hwnd, Window systray_window )
223 {
224     Display *display = thread_display();
225     struct x11drv_win_data *data;
226     XEvent ev;
227     XSetWindowAttributes attr;
228
229     if (!(data = X11DRV_get_win_data( hwnd )) &&
230         !(data = X11DRV_create_win_data( hwnd ))) return;
231
232     TRACE( "icon window %p/%lx managed %u\n", data->hwnd, data->whole_window, data->managed );
233
234     make_window_embedded( display, data );
235
236     /* send the docking request message */
237     ev.xclient.type = ClientMessage;
238     ev.xclient.window = systray_window;
239     ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
240     ev.xclient.format = 32;
241     ev.xclient.data.l[0] = CurrentTime;
242     ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
243     ev.xclient.data.l[2] = data->whole_window;
244     ev.xclient.data.l[3] = 0;
245     ev.xclient.data.l[4] = 0;
246     wine_tsx11_lock();
247     XSendEvent( display, systray_window, False, NoEventMask, &ev );
248     attr.background_pixmap = ParentRelative;
249     attr.bit_gravity = ForgetGravity;
250     XChangeWindowAttributes( display, data->whole_window, CWBackPixmap | CWBitGravity, &attr );
251     XChangeWindowAttributes( display, data->client_window, CWBackPixmap | CWBitGravity, &attr );
252     wine_tsx11_unlock();
253 }
254
255
256 /* hide a tray icon */
257 static BOOL hide_icon( struct tray_icon *icon )
258 {
259     TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
260
261     if (!icon->window) return TRUE;  /* already hidden */
262
263     DestroyWindow(icon->window);
264     DestroyWindow(icon->tooltip);
265     icon->window = 0;
266     icon->tooltip = 0;
267     return TRUE;
268 }
269
270 /* make the icon visible */
271 static BOOL show_icon( struct tray_icon *icon )
272 {
273     RECT rect;
274     static BOOL class_registered;
275     Window systray_window;
276
277     TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
278
279     if (icon->window) return TRUE;  /* already shown */
280
281     if (!class_registered)
282     {
283         WNDCLASSEXW class;
284
285         ZeroMemory( &class, sizeof(class) );
286         class.cbSize        = sizeof(class);
287         class.lpfnWndProc   = tray_wndproc;
288         class.hCursor       = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
289         class.lpszClassName = tray_classname;
290         class.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
291
292         if (!RegisterClassExW(&class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
293         {
294             WINE_ERR( "Could not register tray window class\n" );
295             return FALSE;
296         }
297         class_registered = TRUE;
298     }
299
300     if (!(systray_window = get_systray_selection_owner( thread_display() ))) return FALSE;
301
302     rect.left = 0;
303     rect.top = 0;
304     rect.right = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
305     rect.bottom = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
306
307     icon->window = CreateWindowExW( WS_EX_APPWINDOW, tray_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
308                                     CW_USEDEFAULT, CW_USEDEFAULT,
309                                     rect.right - rect.left, rect.bottom - rect.top,
310                                     NULL, NULL, NULL, icon );
311     create_tooltip( icon );
312     dock_systray_window( icon->window, systray_window );
313     SetTimer( icon->window, 1, 1000, NULL );
314     ShowWindow( icon->window, SW_SHOWNA );
315     return TRUE;
316 }
317
318 /* Modifies an existing icon record */
319 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
320 {
321     TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
322
323     if ((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))
324     {
325         if (nid->dwState & NIS_HIDDEN) hide_icon( icon );
326         else show_icon( icon );
327     }
328
329     if (nid->uFlags & NIF_ICON)
330     {
331         if (icon->image) DestroyIcon(icon->image);
332         icon->image = CopyIcon(nid->hIcon);
333         if (icon->window)
334         {
335             struct x11drv_win_data *data = X11DRV_get_win_data( icon->window );
336             if (data) XClearArea( gdi_display, data->client_window, 0, 0, 0, 0, True );
337         }
338     }
339
340     if (nid->uFlags & NIF_MESSAGE)
341     {
342         icon->callback_message = nid->uCallbackMessage;
343     }
344     if (nid->uFlags & NIF_TIP)
345     {
346         lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
347         if (icon->tooltip) update_tooltip_text(icon);
348     }
349     if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
350     {
351         FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
352     }
353     return TRUE;
354 }
355
356 /* Adds a new icon record to the list */
357 static BOOL add_icon(NOTIFYICONDATAW *nid)
358 {
359     struct tray_icon  *icon;
360
361     WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
362
363     if ((icon = get_icon(nid->hWnd, nid->uID)))
364     {
365         WINE_WARN("duplicate tray icon add, buggy app?\n");
366         return FALSE;
367     }
368
369     if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
370     {
371         WINE_ERR("out of memory\n");
372         return FALSE;
373     }
374
375     ZeroMemory(icon, sizeof(struct tray_icon));
376     icon->id     = nid->uID;
377     icon->owner  = nid->hWnd;
378
379     list_add_tail(&icon_list, &icon->entry);
380
381     /* if hidden state is specified, modify_icon will take care of it */
382     if (!((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN)))
383         show_icon( icon );
384
385     return modify_icon( icon, nid );
386 }
387
388 /* delete tray icon window and icon structure */
389 static BOOL delete_icon( struct tray_icon *icon )
390 {
391     hide_icon( icon );
392     list_remove( &icon->entry );
393     DestroyIcon( icon->image );
394     HeapFree( GetProcessHeap(), 0, icon );
395     return TRUE;
396 }
397
398
399 /***********************************************************************
400  *              wine_notify_icon   (X11DRV.@)
401  *
402  * Driver-side implementation of Shell_NotifyIcon.
403  */
404 BOOL wine_notify_icon( DWORD msg, NOTIFYICONDATAW *data )
405 {
406     BOOL ret = FALSE;
407     struct tray_icon *icon;
408     Window owner;
409
410     switch (msg)
411     {
412     case NIM_ADD:
413         if ((owner = get_systray_selection_owner( thread_display() ))) ret = add_icon( data );
414         break;
415     case NIM_DELETE:
416         if ((icon = get_icon( data->hWnd, data->uID ))) ret = delete_icon( icon );
417         break;
418     case NIM_MODIFY:
419         if ((icon = get_icon( data->hWnd, data->uID ))) ret = modify_icon( icon, data );
420         break;
421     default:
422         FIXME( "unhandled tray message: %u\n", msg );
423         break;
424     }
425     return ret;
426 }