source: trunk/src/shell32/systray.c@ 6496

Last change on this file since 6496 was 5618, checked in by sandervl, 24 years ago

resync with latest wine

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