shell32: SHELL32_GetItemAttributes()
[wine] / dlls / shell32 / systray.c
1 /*
2  *      Systray
3  *
4  *      Copyright 1999 Kai Morich       <kai.morich@bigfoot.de>
5  *
6  *  Manage the systray window. That it actually appears in the docking
7  *  area of KDE is handled in dlls/x11drv/window.c,
8  *  X11DRV_set_wm_hints using KWM_DOCKWINDOW.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winnls.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "shlobj.h"
39 #include "shellapi.h"
40 #include "shell32_main.h"
41 #include "commctrl.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 typedef struct SystrayItem {
47   HWND                  hWnd;
48   HWND                  hWndToolTip;
49   NOTIFYICONDATAW       notifyIcon;
50   struct SystrayItem    *nextTrayItem;
51 } SystrayItem;
52
53 static SystrayItem *systray=NULL;
54 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
55
56
57 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
58 /* space around icon (forces icon to center of KDE systray area) */
59 #define ICON_BORDER  4
60
61
62
63 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAW pnid1, PNOTIFYICONDATAW pnid2)
64 {
65   if (pnid1->hWnd != pnid2->hWnd) return FALSE;
66   if (pnid1->uID  != pnid2->uID)  return FALSE;
67   return TRUE;
68 }
69
70
71 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
72 {
73   if(ptrayItem->notifyIcon.hIcon)
74      DestroyIcon(ptrayItem->notifyIcon.hIcon);
75   if(ptrayItem->hWndToolTip)
76       DestroyWindow(ptrayItem->hWndToolTip);
77   if(ptrayItem->hWnd)
78     DestroyWindow(ptrayItem->hWnd);
79   return;
80 }
81
82
83 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAW pnid)
84 {
85   SystrayItem **ptrayItem = &systray;
86
87   while (*ptrayItem) {
88     if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
89       SystrayItem *next = (*ptrayItem)->nextTrayItem;
90       TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, debugstr_w((*ptrayItem)->notifyIcon.szTip));
91       SYSTRAY_ItemTerm(*ptrayItem);
92
93       HeapFree(GetProcessHeap(),0,*ptrayItem);
94       *ptrayItem = next;
95
96       return TRUE;
97     }
98     ptrayItem = &((*ptrayItem)->nextTrayItem);
99   }
100
101   return FALSE; /* not found */
102 }
103
104 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
105 {
106   HDC hdc;
107   PAINTSTRUCT ps;
108
109   switch (message) {
110   case WM_PAINT:
111   {
112     RECT rc;
113     SystrayItem  *ptrayItem = systray;
114
115     while (ptrayItem) {
116       if (ptrayItem->hWnd==hWnd) {
117         if (ptrayItem->notifyIcon.hIcon) {
118           hdc = BeginPaint(hWnd, &ps);
119           GetClientRect(hWnd, &rc);
120           if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
121                           ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
122             ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
123             SYSTRAY_Delete(&ptrayItem->notifyIcon);
124           }
125         }
126         break;
127       }
128       ptrayItem = ptrayItem->nextTrayItem;
129     }
130     EndPaint(hWnd, &ps);
131   }
132   break;
133
134   case WM_MOUSEMOVE:
135   case WM_LBUTTONDOWN:
136   case WM_LBUTTONUP:
137   case WM_RBUTTONDOWN:
138   case WM_RBUTTONUP:
139   case WM_MBUTTONDOWN:
140   case WM_MBUTTONUP:
141   {
142     MSG msg;
143     SystrayItem *ptrayItem = systray;
144
145     while ( ptrayItem ) {
146       if (ptrayItem->hWnd == hWnd) {
147         msg.hwnd=hWnd;
148         msg.message=message;
149         msg.wParam=wParam;
150         msg.lParam=lParam;
151         msg.time = GetMessageTime ();
152         msg.pt.x = LOWORD(GetMessagePos ());
153         msg.pt.y = HIWORD(GetMessagePos ());
154
155         SendMessageW(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
156       }
157       ptrayItem = ptrayItem->nextTrayItem;
158     }
159   }
160   /* fall through */
161
162   case WM_LBUTTONDBLCLK:
163   case WM_RBUTTONDBLCLK:
164   case WM_MBUTTONDBLCLK:
165   {
166     SystrayItem *ptrayItem = systray;
167
168     while (ptrayItem) {
169       if (ptrayItem->hWnd == hWnd) {
170         if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
171           if (!PostMessageW(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
172                             (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
173               ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
174               SYSTRAY_Delete(&ptrayItem->notifyIcon);
175             }
176         }
177         break;
178       }
179       ptrayItem = ptrayItem->nextTrayItem;
180     }
181   }
182   break;
183
184   default:
185     return (DefWindowProcW(hWnd, message, wParam, lParam));
186   }
187   return (0);
188
189 }
190
191
192 static BOOL SYSTRAY_RegisterClass(void)
193 {
194   WNDCLASSW  wc;
195   static const WCHAR WineSystrayW[] = { 'W','i','n','e','S','y','s','t','r','a','y',0 };
196
197   wc.style         = CS_SAVEBITS|CS_DBLCLKS;
198   wc.lpfnWndProc   = SYSTRAY_WndProc;
199   wc.cbClsExtra    = 0;
200   wc.cbWndExtra    = 0;
201   wc.hInstance     = 0;
202   wc.hIcon         = 0;
203   wc.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
204   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
205   wc.lpszMenuName  = NULL;
206   wc.lpszClassName = WineSystrayW;
207
208   if (!RegisterClassW(&wc)) {
209     ERR("RegisterClass(WineSystray) failed\n");
210     return FALSE;
211   }
212   return TRUE;
213 }
214
215
216 static BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
217 {
218   RECT rect;
219   static const WCHAR WineSystrayW[] = { 'W','i','n','e','S','y','s','t','r','a','y',0 };
220   static const WCHAR Wine_SystrayW[] = { 'W','i','n','e','-','S','y','s','t','r','a','y',0 };
221
222   /* Register the class if this is our first tray item. */
223   if ( firstSystray ) {
224     firstSystray = FALSE;
225     if ( !SYSTRAY_RegisterClass() ) {
226       ERR( "RegisterClass(WineSystray) failed\n" );
227       return FALSE;
228     }
229   }
230
231   /* Initialize the window size. */
232   rect.left   = 0;
233   rect.top    = 0;
234   rect.right  = ICON_SIZE+2*ICON_BORDER;
235   rect.bottom = ICON_SIZE+2*ICON_BORDER;
236
237   ZeroMemory( ptrayItem, sizeof(SystrayItem) );
238   /* Create tray window for icon. */
239   ptrayItem->hWnd = CreateWindowExW( WS_EX_TRAYWINDOW,
240                                 WineSystrayW, Wine_SystrayW,
241                                 WS_VISIBLE,
242                                 CW_USEDEFAULT, CW_USEDEFAULT,
243                                 rect.right-rect.left, rect.bottom-rect.top,
244                                 0, 0, 0, 0 );
245   if ( !ptrayItem->hWnd ) {
246     ERR( "CreateWindow(WineSystray) failed\n" );
247     return FALSE;
248   }
249
250   /* Create tooltip for icon. */
251   ptrayItem->hWndToolTip = CreateWindowW( TOOLTIPS_CLASSW,NULL,TTS_ALWAYSTIP,
252                                      CW_USEDEFAULT, CW_USEDEFAULT,
253                                      CW_USEDEFAULT, CW_USEDEFAULT,
254                                      ptrayItem->hWnd, 0, 0, 0 );
255   if ( !ptrayItem->hWndToolTip ) {
256     ERR( "CreateWindow(TOOLTIP) failed\n" );
257     return FALSE;
258   }
259   return TRUE;
260 }
261
262
263 static void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
264 {
265   ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
266 }
267
268
269 static void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
270 {
271   if(ptrayItem->notifyIcon.hIcon)
272     DestroyIcon(ptrayItem->notifyIcon.hIcon);
273   ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
274   InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
275 }
276
277
278 static void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, const WCHAR* szTip, int modify)
279 {
280   TTTOOLINFOW ti;
281
282   lstrcpynW(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip)/sizeof(WCHAR));
283
284   ti.cbSize = sizeof(TTTOOLINFOW);
285   ti.uFlags = 0;
286   ti.hwnd = ptrayItem->hWnd;
287   ti.hinst = 0;
288   ti.uId = 0;
289   ti.lpszText = ptrayItem->notifyIcon.szTip;
290   ti.rect.left   = 0;
291   ti.rect.top    = 0;
292   ti.rect.right  = ICON_SIZE+2*ICON_BORDER;
293   ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
294
295   if(modify)
296     SendMessageW(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
297   else
298     SendMessageW(ptrayItem->hWndToolTip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
299 }
300
301
302 static BOOL SYSTRAY_Add(PNOTIFYICONDATAW pnid)
303 {
304   SystrayItem **ptrayItem = &systray;
305   static const WCHAR emptyW[] = { 0 };
306
307   /* Find last element. */
308   while( *ptrayItem ) {
309     if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
310       return FALSE;
311     ptrayItem = &((*ptrayItem)->nextTrayItem);
312   }
313   /* Allocate SystrayItem for element and add to end of list. */
314   (*ptrayItem) = HeapAlloc(GetProcessHeap(),0,sizeof(SystrayItem));
315
316   /* Initialize and set data for the tray element. */
317   SYSTRAY_ItemInit( (*ptrayItem) );
318   (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
319   (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
320   SYSTRAY_ItemSetIcon   (*ptrayItem, (pnid->uFlags&NIF_ICON)   ?pnid->hIcon           :0);
321   SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
322   SYSTRAY_ItemSetTip    (*ptrayItem, (pnid->uFlags&NIF_TIP)    ?pnid->szTip           :emptyW, FALSE);
323
324   TRACE("%p: %p %s\n",  (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
325                                           debugstr_w((*ptrayItem)->notifyIcon.szTip));
326   return TRUE;
327 }
328
329
330 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAW pnid)
331 {
332   SystrayItem *ptrayItem = systray;
333
334   while ( ptrayItem ) {
335     if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
336       if (pnid->uFlags & NIF_ICON)
337         SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
338       if (pnid->uFlags & NIF_MESSAGE)
339         SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
340       if (pnid->uFlags & NIF_TIP)
341         SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
342
343       TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, debugstr_w(ptrayItem->notifyIcon.szTip));
344       return TRUE;
345     }
346     ptrayItem = ptrayItem->nextTrayItem;
347   }
348   return FALSE; /* not found */
349 }
350
351
352 /*************************************************************************
353  *
354  */
355 BOOL SYSTRAY_Init(void)
356 {
357   return TRUE;
358 }
359
360 /*************************************************************************
361  * Shell_NotifyIconW                    [SHELL32.298]
362  */
363 BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW pnid )
364 {
365   BOOL flag=FALSE;
366   TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
367   switch(dwMessage) {
368   case NIM_ADD:
369     flag = SYSTRAY_Add(pnid);
370     break;
371   case NIM_MODIFY:
372     flag = SYSTRAY_Modify(pnid);
373     break;
374   case NIM_DELETE:
375     flag = SYSTRAY_Delete(pnid);
376     break;
377   }
378   TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
379   return flag;
380 }
381
382 /*************************************************************************
383  * Shell_NotifyIconA                    [SHELL32.297]
384  * Shell_NotifyIcon                     [SHELL32.296]
385  */
386 BOOL WINAPI Shell_NotifyIconA (DWORD dwMessage, PNOTIFYICONDATAA pnid )
387 {
388         BOOL ret;
389
390         PNOTIFYICONDATAW p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAW));
391         memcpy(p, pnid, sizeof(NOTIFYICONDATAW));
392         MultiByteToWideChar( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip)/sizeof(WCHAR) );
393         p->szTip[sizeof(p->szTip)/sizeof(WCHAR)-1] = 0;
394
395         ret = Shell_NotifyIconW(dwMessage, p );
396
397         HeapFree(GetProcessHeap(),0,p);
398         return ret;
399 }