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