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

Last change on this file since 5087 was 4121, checked in by sandervl, 25 years ago

complete merge with shell32 from wine 20000801

File size: 10.4 KB
Line 
1/* $Id: systray.c,v 1.1 2000-08-30 13:53:01 sandervl 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#ifdef __WIN32OS2__
13#define ICOM_CINTERFACE 1
14#include <odin.h>
15#else
16#include <unistd.h>
17#endif
18#include <stdlib.h>
19#include <string.h>
20
21#ifndef __WIN32OS2__
22#include "heap.h"
23#endif
24#include "shellapi.h"
25#include "shell32_main.h"
26#include "windows.h"
27#include "commctrl.h"
28#include "debugtools.h"
29#include "config.h"
30
31DEFAULT_DEBUG_CHANNEL(shell)
32
33typedef struct SystrayItem {
34 HWND hWnd;
35 HWND hWndToolTip;
36 NOTIFYICONDATAA notifyIcon;
37 struct SystrayItem *nextTrayItem;
38} SystrayItem;
39
40static SystrayItem *systray=NULL;
41static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
42
43static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
44
45
46#define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
47/* space around icon (forces icon to center of KDE systray area) */
48#define ICON_BORDER 4
49
50
51
52static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
53{
54 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
55 if (pnid1->uID != pnid2->uID) return FALSE;
56 return TRUE;
57}
58
59static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
60{
61 HDC hdc;
62 PAINTSTRUCT ps;
63
64 switch (message) {
65 case WM_PAINT:
66 {
67 RECT rc;
68 SystrayItem *ptrayItem = systray;
69
70 while (ptrayItem) {
71 if (ptrayItem->hWnd==hWnd) {
72 if (ptrayItem->notifyIcon.hIcon) {
73 hdc = BeginPaint(hWnd, &ps);
74 GetClientRect(hWnd, &rc);
75 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
76 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
77 ERR("Paint(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
78 SYSTRAY_Delete(&ptrayItem->notifyIcon);
79 }
80 }
81 break;
82 }
83 ptrayItem = ptrayItem->nextTrayItem;
84 }
85 EndPaint(hWnd, &ps);
86 }
87 break;
88
89 case WM_MOUSEMOVE:
90 case WM_LBUTTONDOWN:
91 case WM_LBUTTONUP:
92 case WM_RBUTTONDOWN:
93 case WM_RBUTTONUP:
94 case WM_MBUTTONDOWN:
95 case WM_MBUTTONUP:
96 {
97 MSG msg;
98 SystrayItem *ptrayItem = systray;
99
100 while ( ptrayItem ) {
101 if (ptrayItem->hWnd == hWnd) {
102 msg.hwnd=hWnd;
103 msg.message=message;
104 msg.wParam=wParam;
105 msg.lParam=lParam;
106 msg.time = GetMessageTime ();
107 msg.pt.x = LOWORD(GetMessagePos ());
108 msg.pt.y = HIWORD(GetMessagePos ());
109
110 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
111 }
112 ptrayItem = ptrayItem->nextTrayItem;
113 }
114 }
115 /* fallthru */
116
117 case WM_LBUTTONDBLCLK:
118 case WM_RBUTTONDBLCLK:
119 case WM_MBUTTONDBLCLK:
120 {
121 SystrayItem *ptrayItem = systray;
122
123 while (ptrayItem) {
124 if (ptrayItem->hWnd == hWnd) {
125 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
126 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
127 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
128 ERR("PostMessage(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
129 SYSTRAY_Delete(&ptrayItem->notifyIcon);
130 }
131 }
132 break;
133 }
134 ptrayItem = ptrayItem->nextTrayItem;
135 }
136 }
137 break;
138
139 default:
140 return (DefWindowProcA(hWnd, message, wParam, lParam));
141 }
142 return (0);
143
144}
145
146
147BOOL SYSTRAY_RegisterClass(void)
148{
149 WNDCLASSA wc;
150
151 wc.style = CS_SAVEBITS;
152 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
153 wc.cbClsExtra = 0;
154 wc.cbWndExtra = 0;
155 wc.hInstance = 0;
156 wc.hIcon = 0;
157 wc.hCursor = LoadCursorA(0, IDC_ARROWA);
158 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
159 wc.lpszMenuName = NULL;
160 wc.lpszClassName = "WineSystray";
161
162 if (!RegisterClassA(&wc)) {
163 ERR("RegisterClass(WineSystray) failed\n");
164 return FALSE;
165 }
166 return TRUE;
167}
168
169
170BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
171{
172 RECT rect;
173
174 /* Register the class if this is our first tray item. */
175 if ( firstSystray ) {
176 firstSystray = FALSE;
177 if ( !SYSTRAY_RegisterClass() ) {
178 ERR( "RegisterClass(WineSystray) failed\n" );
179 return FALSE;
180 }
181 }
182#ifdef __WIN32OS2__
183 //SvL: Disabled system tray for now
184 // Integrate with OS/2 WPS (WarpCenter tray?)
185 dprintf(("WARNING: SHELL32: SYSTRAY_ItemInit: ignored"));
186 return FALSE;
187#else
188 /* Initialize the window size. */
189 rect.left = 0;
190 rect.top = 0;
191 rect.right = ICON_SIZE+2*ICON_BORDER;
192 rect.bottom = ICON_SIZE+2*ICON_BORDER;
193
194 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
195 /* Create tray window for icon. */
196 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
197 "WineSystray", "Wine-Systray",
198 WS_VISIBLE,
199 CW_USEDEFAULT, CW_USEDEFAULT,
200 rect.right-rect.left, rect.bottom-rect.top,
201 0, 0, 0, 0 );
202 if ( !ptrayItem->hWnd ) {
203 ERR( "CreateWindow(WineSystray) failed\n" );
204 return FALSE;
205 }
206
207 /* Create tooltip for icon. */
208 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
209 CW_USEDEFAULT, CW_USEDEFAULT,
210 CW_USEDEFAULT, CW_USEDEFAULT,
211 ptrayItem->hWnd, 0, 0, 0 );
212 if ( !ptrayItem->hWndToolTip ) {
213 ERR( "CreateWindow(TOOLTIP) failed\n" );
214 return FALSE;
215 }
216 return TRUE;
217#endif //__WIN32OS2__
218}
219
220
221static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
222{
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 = hIcon; /* do we need a copy? */
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]
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.297]
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 if (*(pnid->szTip))
379 lstrcpynWtoA (p->szTip, pnid->szTip, 64 );
380
381 ret = Shell_NotifyIconA(dwMessage, p );
382
383 HeapFree(GetProcessHeap(),0,p);
384 return ret;
385}
386
387/*************************************************************************
388 * Shell_NotifyIcon [SHELL32.296]
389 */
390BOOL WINAPI Shell_NotifyIcon(DWORD dwMessage, PNOTIFYICONDATAA pnid)
391{
392 return Shell_NotifyIconA(dwMessage, pnid);
393}
Note: See TracBrowser for help on using the repository browser.