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

Last change on this file since 6650 was 6650, checked in by bird, 24 years ago

Added $Id:$ keyword.

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