Release 20030408.
[wine/gsoc-2012-control.git] / dlls / shell32 / systray.c
blob908eebae796268f6fd02e652fec8acea1cd11791
1 /*
2 * Systray
4 * Copyright 1999 Kai Morich <kai.morich@bigfoot.de>
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.
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.
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.
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
25 #include "config.h"
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <stdlib.h>
31 #include <string.h>
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"
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42 typedef struct SystrayItem {
43 HWND hWnd;
44 HWND hWndToolTip;
45 NOTIFYICONDATAA notifyIcon;
46 struct SystrayItem *nextTrayItem;
47 } SystrayItem;
49 static SystrayItem *systray=NULL;
50 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
52 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
55 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
56 /* space around icon (forces icon to center of KDE systray area) */
57 #define ICON_BORDER 4
61 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
63 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
64 if (pnid1->uID != pnid2->uID) return FALSE;
65 return TRUE;
68 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
70 HDC hdc;
71 PAINTSTRUCT ps;
73 switch (message) {
74 case WM_PAINT:
76 RECT rc;
77 SystrayItem *ptrayItem = systray;
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 %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
87 SYSTRAY_Delete(&ptrayItem->notifyIcon);
90 break;
92 ptrayItem = ptrayItem->nextTrayItem;
94 EndPaint(hWnd, &ps);
96 break;
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:
106 MSG msg;
107 SystrayItem *ptrayItem = systray;
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 ());
119 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
121 ptrayItem = ptrayItem->nextTrayItem;
124 /* fall through */
126 case WM_LBUTTONDBLCLK:
127 case WM_RBUTTONDBLCLK:
128 case WM_MBUTTONDBLCLK:
130 SystrayItem *ptrayItem = systray;
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 %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
138 SYSTRAY_Delete(&ptrayItem->notifyIcon);
141 break;
143 ptrayItem = ptrayItem->nextTrayItem;
146 break;
148 default:
149 return (DefWindowProcA(hWnd, message, wParam, lParam));
151 return (0);
156 BOOL SYSTRAY_RegisterClass(void)
158 WNDCLASSA wc;
160 wc.style = CS_SAVEBITS|CS_DBLCLKS;
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";
171 if (!RegisterClassA(&wc)) {
172 ERR("RegisterClass(WineSystray) failed\n");
173 return FALSE;
175 return TRUE;
179 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
181 RECT rect;
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;
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;
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;
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;
220 return TRUE;
224 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
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;
236 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
238 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
242 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
244 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
245 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
249 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
251 TTTOOLINFOA ti;
253 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
254 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
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;
267 if(modify)
268 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
269 else
270 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
274 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
276 SystrayItem **ptrayItem = &systray;
278 /* Find last element. */
279 while( *ptrayItem ) {
280 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
281 return FALSE;
282 ptrayItem = &((*ptrayItem)->nextTrayItem);
284 /* Allocate SystrayItem for element and add to end of list. */
285 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
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);
295 TRACE("%p: %p %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
296 (*ptrayItem)->notifyIcon.szTip);
297 return TRUE;
301 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
303 SystrayItem *ptrayItem = systray;
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);
314 TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
315 return TRUE;
317 ptrayItem = ptrayItem->nextTrayItem;
319 return FALSE; /* not found */
323 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
325 SystrayItem **ptrayItem = &systray;
327 while (*ptrayItem) {
328 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
329 SystrayItem *next = (*ptrayItem)->nextTrayItem;
330 TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
331 SYSTRAY_ItemTerm(*ptrayItem);
333 free(*ptrayItem);
334 *ptrayItem = next;
336 return TRUE;
338 ptrayItem = &((*ptrayItem)->nextTrayItem);
341 return FALSE; /* not found */
344 /*************************************************************************
347 BOOL SYSTRAY_Init(void)
349 return TRUE;
352 /*************************************************************************
353 * Shell_NotifyIcon [SHELL32.296]
354 * Shell_NotifyIconA [SHELL32.297]
356 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
358 BOOL flag=FALSE;
359 TRACE("enter %p %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;
371 TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
372 return flag;
375 /*************************************************************************
376 * Shell_NotifyIconW [SHELL32.298]
378 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
380 BOOL ret;
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;
387 ret = Shell_NotifyIconA(dwMessage, p );
389 HeapFree(GetProcessHeap(),0,p);
390 return ret;