The "colors" parameter of SetDIBColorTable should be CONST.
[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   NOTIFYICONDATAA       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 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
57
58
59 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
60 /* space around icon (forces icon to center of KDE systray area) */
61 #define ICON_BORDER  4
62
63
64
65 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
66 {
67   if (pnid1->hWnd != pnid2->hWnd) return FALSE;
68   if (pnid1->uID  != pnid2->uID)  return FALSE;
69   return TRUE;
70 }
71
72 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
73 {
74   HDC hdc;
75   PAINTSTRUCT ps;
76
77   switch (message) {
78   case WM_PAINT:
79   {
80     RECT rc;
81     SystrayItem  *ptrayItem = systray;
82
83     while (ptrayItem) {
84       if (ptrayItem->hWnd==hWnd) {
85         if (ptrayItem->notifyIcon.hIcon) {
86           hdc = BeginPaint(hWnd, &ps);
87           GetClientRect(hWnd, &rc);
88           if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
89                           ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
90             ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
91             SYSTRAY_Delete(&ptrayItem->notifyIcon);
92           }
93         }
94         break;
95       }
96       ptrayItem = ptrayItem->nextTrayItem;
97     }
98     EndPaint(hWnd, &ps);
99   }
100   break;
101
102   case WM_MOUSEMOVE:
103   case WM_LBUTTONDOWN:
104   case WM_LBUTTONUP:
105   case WM_RBUTTONDOWN:
106   case WM_RBUTTONUP:
107   case WM_MBUTTONDOWN:
108   case WM_MBUTTONUP:
109   {
110     MSG msg;
111     SystrayItem *ptrayItem = systray;
112
113     while ( ptrayItem ) {
114       if (ptrayItem->hWnd == hWnd) {
115         msg.hwnd=hWnd;
116         msg.message=message;
117         msg.wParam=wParam;
118         msg.lParam=lParam;
119         msg.time = GetMessageTime ();
120         msg.pt.x = LOWORD(GetMessagePos ());
121         msg.pt.y = HIWORD(GetMessagePos ());
122
123         SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
124       }
125       ptrayItem = ptrayItem->nextTrayItem;
126     }
127   }
128   /* fall through */
129
130   case WM_LBUTTONDBLCLK:
131   case WM_RBUTTONDBLCLK:
132   case WM_MBUTTONDBLCLK:
133   {
134     SystrayItem *ptrayItem = systray;
135
136     while (ptrayItem) {
137       if (ptrayItem->hWnd == hWnd) {
138         if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
139           if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
140                             (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
141               ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
142               SYSTRAY_Delete(&ptrayItem->notifyIcon);
143             }
144         }
145         break;
146       }
147       ptrayItem = ptrayItem->nextTrayItem;
148     }
149   }
150   break;
151
152   default:
153     return (DefWindowProcA(hWnd, message, wParam, lParam));
154   }
155   return (0);
156
157 }
158
159
160 BOOL SYSTRAY_RegisterClass(void)
161 {
162   WNDCLASSA  wc;
163
164   wc.style         = CS_SAVEBITS|CS_DBLCLKS;
165   wc.lpfnWndProc   = (WNDPROC)SYSTRAY_WndProc;
166   wc.cbClsExtra    = 0;
167   wc.cbWndExtra    = 0;
168   wc.hInstance     = 0;
169   wc.hIcon         = 0;
170   wc.hCursor       = LoadCursorA(0, (LPSTR)IDC_ARROW);
171   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
172   wc.lpszMenuName  = NULL;
173   wc.lpszClassName = "WineSystray";
174
175   if (!RegisterClassA(&wc)) {
176     ERR("RegisterClass(WineSystray) failed\n");
177     return FALSE;
178   }
179   return TRUE;
180 }
181
182
183 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
184 {
185   RECT rect;
186
187   /* Register the class if this is our first tray item. */
188   if ( firstSystray ) {
189     firstSystray = FALSE;
190     if ( !SYSTRAY_RegisterClass() ) {
191       ERR( "RegisterClass(WineSystray) failed\n" );
192       return FALSE;
193     }
194   }
195
196   /* Initialize the window size. */
197   rect.left   = 0;
198   rect.top    = 0;
199   rect.right  = ICON_SIZE+2*ICON_BORDER;
200   rect.bottom = ICON_SIZE+2*ICON_BORDER;
201
202   ZeroMemory( ptrayItem, sizeof(SystrayItem) );
203   /* Create tray window for icon. */
204   ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
205                                 "WineSystray", "Wine-Systray",
206                                 WS_VISIBLE,
207                                 CW_USEDEFAULT, CW_USEDEFAULT,
208                                 rect.right-rect.left, rect.bottom-rect.top,
209                                 0, 0, 0, 0 );
210   if ( !ptrayItem->hWnd ) {
211     ERR( "CreateWindow(WineSystray) failed\n" );
212     return FALSE;
213   }
214
215   /* Create tooltip for icon. */
216   ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
217                                      CW_USEDEFAULT, CW_USEDEFAULT,
218                                      CW_USEDEFAULT, CW_USEDEFAULT,
219                                      ptrayItem->hWnd, 0, 0, 0 );
220   if ( !ptrayItem->hWndToolTip ) {
221     ERR( "CreateWindow(TOOLTIP) failed\n" );
222     return FALSE;
223   }
224   return TRUE;
225 }
226
227
228 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
229 {
230   if(ptrayItem->notifyIcon.hIcon)
231      DestroyIcon(ptrayItem->notifyIcon.hIcon);
232   if(ptrayItem->hWndToolTip)
233       DestroyWindow(ptrayItem->hWndToolTip);
234   if(ptrayItem->hWnd)
235     DestroyWindow(ptrayItem->hWnd);
236   return;
237 }
238
239
240 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
241 {
242   ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
243 }
244
245
246 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
247 {
248   if(ptrayItem->notifyIcon.hIcon)
249     DestroyIcon(ptrayItem->notifyIcon.hIcon);
250   ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
251   InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
252 }
253
254
255 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
256 {
257   TTTOOLINFOA ti;
258
259   strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
260   ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
261
262   ti.cbSize = sizeof(TTTOOLINFOA);
263   ti.uFlags = 0;
264   ti.hwnd = ptrayItem->hWnd;
265   ti.hinst = 0;
266   ti.uId = 0;
267   ti.lpszText = ptrayItem->notifyIcon.szTip;
268   ti.rect.left   = 0;
269   ti.rect.top    = 0;
270   ti.rect.right  = ICON_SIZE+2*ICON_BORDER;
271   ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
272
273   if(modify)
274     SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
275   else
276     SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
277 }
278
279
280 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
281 {
282   SystrayItem **ptrayItem = &systray;
283
284   /* Find last element. */
285   while( *ptrayItem ) {
286     if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
287       return FALSE;
288     ptrayItem = &((*ptrayItem)->nextTrayItem);
289   }
290   /* Allocate SystrayItem for element and add to end of list. */
291   (*ptrayItem) = HeapAlloc(GetProcessHeap(),0,sizeof(SystrayItem));
292
293   /* Initialize and set data for the tray element. */
294   SYSTRAY_ItemInit( (*ptrayItem) );
295   (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
296   (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
297   SYSTRAY_ItemSetIcon   (*ptrayItem, (pnid->uFlags&NIF_ICON)   ?pnid->hIcon           :0);
298   SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
299   SYSTRAY_ItemSetTip    (*ptrayItem, (pnid->uFlags&NIF_TIP)    ?pnid->szTip           :"", FALSE);
300
301   TRACE("%p: %p %s\n",  (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
302                                           (*ptrayItem)->notifyIcon.szTip);
303   return TRUE;
304 }
305
306
307 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
308 {
309   SystrayItem *ptrayItem = systray;
310
311   while ( ptrayItem ) {
312     if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
313       if (pnid->uFlags & NIF_ICON)
314         SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
315       if (pnid->uFlags & NIF_MESSAGE)
316         SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
317       if (pnid->uFlags & NIF_TIP)
318         SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
319
320       TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
321       return TRUE;
322     }
323     ptrayItem = ptrayItem->nextTrayItem;
324   }
325   return FALSE; /* not found */
326 }
327
328
329 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
330 {
331   SystrayItem **ptrayItem = &systray;
332
333   while (*ptrayItem) {
334     if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
335       SystrayItem *next = (*ptrayItem)->nextTrayItem;
336       TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
337       SYSTRAY_ItemTerm(*ptrayItem);
338
339       HeapFree(GetProcessHeap(),0,*ptrayItem);
340       *ptrayItem = next;
341
342       return TRUE;
343     }
344     ptrayItem = &((*ptrayItem)->nextTrayItem);
345   }
346
347   return FALSE; /* not found */
348 }
349
350 /*************************************************************************
351  *
352  */
353 BOOL SYSTRAY_Init(void)
354 {
355   return TRUE;
356 }
357
358 /*************************************************************************
359  * Shell_NotifyIcon                     [SHELL32.296]
360  * Shell_NotifyIconA                    [SHELL32.297]
361  */
362 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
363 {
364   BOOL flag=FALSE;
365   TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
366   switch(dwMessage) {
367   case NIM_ADD:
368     flag = SYSTRAY_Add(pnid);
369     break;
370   case NIM_MODIFY:
371     flag = SYSTRAY_Modify(pnid);
372     break;
373   case NIM_DELETE:
374     flag = SYSTRAY_Delete(pnid);
375     break;
376   }
377   TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
378   return flag;
379 }
380
381 /*************************************************************************
382  * Shell_NotifyIconW                    [SHELL32.298]
383  */
384 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
385 {
386         BOOL ret;
387
388         PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
389         memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
390         WideCharToMultiByte( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip), NULL, NULL );
391         p->szTip[sizeof(p->szTip)-1] = 0;
392
393         ret = Shell_NotifyIconA(dwMessage, p );
394
395         HeapFree(GetProcessHeap(),0,p);
396         return ret;
397 }