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