Unregister 16-bit dlls on module unload.
[wine/multimedia.git] / dlls / shell32 / systray.c
blob84ea9254bda5670db5b2284a32f2006db05446ab
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 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 typedef struct SystrayItem {
41 HWND hWnd;
42 HWND hWndToolTip;
43 NOTIFYICONDATAA notifyIcon;
44 struct SystrayItem *nextTrayItem;
45 } SystrayItem;
47 static SystrayItem *systray=NULL;
48 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
50 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
53 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
54 /* space around icon (forces icon to center of KDE systray area) */
55 #define ICON_BORDER 4
59 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
61 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
62 if (pnid1->uID != pnid2->uID) return FALSE;
63 return TRUE;
66 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
68 HDC hdc;
69 PAINTSTRUCT ps;
71 switch (message) {
72 case WM_PAINT:
74 RECT rc;
75 SystrayItem *ptrayItem = systray;
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);
88 break;
90 ptrayItem = ptrayItem->nextTrayItem;
92 EndPaint(hWnd, &ps);
94 break;
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:
104 MSG msg;
105 SystrayItem *ptrayItem = systray;
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 ());
117 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
119 ptrayItem = ptrayItem->nextTrayItem;
122 /* fall through */
124 case WM_LBUTTONDBLCLK:
125 case WM_RBUTTONDBLCLK:
126 case WM_MBUTTONDBLCLK:
128 SystrayItem *ptrayItem = systray;
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);
139 break;
141 ptrayItem = ptrayItem->nextTrayItem;
144 break;
146 default:
147 return (DefWindowProcA(hWnd, message, wParam, lParam));
149 return (0);
154 BOOL SYSTRAY_RegisterClass(void)
156 WNDCLASSA wc;
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";
169 if (!RegisterClassA(&wc)) {
170 ERR("RegisterClass(WineSystray) failed\n");
171 return FALSE;
173 return TRUE;
177 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
179 RECT rect;
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;
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;
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;
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;
218 return TRUE;
222 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
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;
234 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
236 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
240 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
242 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
243 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
247 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
249 TTTOOLINFOA ti;
251 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
252 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
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;
265 if(modify)
266 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
267 else
268 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
272 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
274 SystrayItem **ptrayItem = &systray;
276 /* Find last element. */
277 while( *ptrayItem ) {
278 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
279 return FALSE;
280 ptrayItem = &((*ptrayItem)->nextTrayItem);
282 /* Allocate SystrayItem for element and add to end of list. */
283 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
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);
293 TRACE("%p: 0x%08x %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
294 (*ptrayItem)->notifyIcon.szTip);
295 return TRUE;
299 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
301 SystrayItem *ptrayItem = systray;
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);
312 TRACE("%p: 0x%08x %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
313 return TRUE;
315 ptrayItem = ptrayItem->nextTrayItem;
317 return FALSE; /* not found */
321 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
323 SystrayItem **ptrayItem = &systray;
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);
331 free(*ptrayItem);
332 *ptrayItem = next;
334 return TRUE;
336 ptrayItem = &((*ptrayItem)->nextTrayItem);
339 return FALSE; /* not found */
342 /*************************************************************************
345 BOOL SYSTRAY_Init(void)
347 return TRUE;
350 /*************************************************************************
351 * Shell_NotifyIcon [SHELL32.296]
352 * Shell_NotifyIconA [SHELL32.297]
354 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
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;
369 TRACE("leave %d %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
370 return flag;
373 /*************************************************************************
374 * Shell_NotifyIconW [SHELL32.298]
376 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
378 BOOL ret;
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;
385 ret = Shell_NotifyIconA(dwMessage, p );
387 HeapFree(GetProcessHeap(),0,p);
388 return ret;