systray.c 10.9 KB
Newer Older
1 2 3 4 5 6
/*
 *	Systray
 *
 *	Copyright 1999 Kai Morich	<kai.morich@bigfoot.de>
 *
 *  Manage the systray window. That it actually appears in the docking
7 8
 *  area of KDE is handled in dlls/x11drv/window.c,
 *  X11DRV_set_wm_hints using KWM_DOCKWINDOW.
9
 *
10 11 12 13 14 15 16 17 18 19 20 21 22
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 24
 */

25 26
#include "config.h"

27 28 29
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
30
#include <stdarg.h>
31 32
#include <string.h>

33
#include "windef.h"
34
#include "winbase.h"
35
#include "winnls.h"
36 37
#include "wingdi.h"
#include "winuser.h"
38
#include "shlobj.h"
39 40 41
#include "shellapi.h"
#include "shell32_main.h"
#include "commctrl.h"
42
#include "wine/debug.h"
43

44
WINE_DEFAULT_DEBUG_CHANNEL(shell);
45

46
typedef struct SystrayItem {
47 48 49
  HWND                  hWnd;
  HWND                  hWndToolTip;
  NOTIFYICONDATAA       notifyIcon;
50 51
  struct SystrayItem    *nextTrayItem;
} SystrayItem;
52

53 54
static SystrayItem *systray=NULL;
static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
55 56 57 58

static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);


59 60 61 62
#define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
/* space around icon (forces icon to center of KDE systray area) */
#define ICON_BORDER  4

63 64


65 66 67 68 69 70
static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
{
  if (pnid1->hWnd != pnid2->hWnd) return FALSE;
  if (pnid1->uID  != pnid2->uID)  return FALSE;
  return TRUE;
}
71 72 73 74 75 76 77 78 79 80

static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  HDC hdc;
  PAINTSTRUCT ps;

  switch (message) {
  case WM_PAINT:
  {
    RECT rc;
81 82 83 84 85 86 87 88 89
    SystrayItem  *ptrayItem = systray;

    while (ptrayItem) {
      if (ptrayItem->hWnd==hWnd) {
	if (ptrayItem->notifyIcon.hIcon) {
	  hdc = BeginPaint(hWnd, &ps);
	  GetClientRect(hWnd, &rc);
	  if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
			  ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
90
	    ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
91 92 93 94
	    SYSTRAY_Delete(&ptrayItem->notifyIcon);
	  }
	}
	break;
95 96
      }
      ptrayItem = ptrayItem->nextTrayItem;
97 98
    }
    EndPaint(hWnd, &ps);
99 100 101
  }
  break;

102 103 104 105 106 107 108 109 110
  case WM_MOUSEMOVE:
  case WM_LBUTTONDOWN:
  case WM_LBUTTONUP:
  case WM_RBUTTONDOWN:
  case WM_RBUTTONUP:
  case WM_MBUTTONDOWN:
  case WM_MBUTTONUP:
  {
    MSG msg;
111
    SystrayItem *ptrayItem = systray;
112

113 114
    while ( ptrayItem ) {
      if (ptrayItem->hWnd == hWnd) {
115 116 117 118 119 120 121 122 123 124 125 126
        msg.hwnd=hWnd;
        msg.message=message;
        msg.wParam=wParam;
        msg.lParam=lParam;
        msg.time = GetMessageTime ();
        msg.pt.x = LOWORD(GetMessagePos ());
        msg.pt.y = HIWORD(GetMessagePos ());

        SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
      }
      ptrayItem = ptrayItem->nextTrayItem;
    }
127
  }
128
  /* fall through */
129

130 131 132 133
  case WM_LBUTTONDBLCLK:
  case WM_RBUTTONDBLCLK:
  case WM_MBUTTONDBLCLK:
  {
134 135 136 137 138
    SystrayItem *ptrayItem = systray;

    while (ptrayItem) {
      if (ptrayItem->hWnd == hWnd) {
	if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
139
          if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
140
                            (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
141
	      ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
142 143
	      SYSTRAY_Delete(&ptrayItem->notifyIcon);
	    }
144
        }
145
	break;
146
      }
147
      ptrayItem = ptrayItem->nextTrayItem;
148
    }
149 150 151
  }
  break;

152 153
  default:
    return (DefWindowProcA(hWnd, message, wParam, lParam));
154 155 156
  }
  return (0);

157 158
}

159

Mike McCormack's avatar
Mike McCormack committed
160
static BOOL SYSTRAY_RegisterClass(void)
161 162
{
  WNDCLASSA  wc;
163

164
  wc.style         = CS_SAVEBITS|CS_DBLCLKS;
165
  wc.lpfnWndProc   = SYSTRAY_WndProc;
166 167 168
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = 0;
169
  wc.hIcon         = 0;
170
  wc.hCursor       = LoadCursorA(0, (LPSTR)IDC_ARROW);
171
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
172 173
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = "WineSystray";
174

175 176 177 178
  if (!RegisterClassA(&wc)) {
    ERR("RegisterClass(WineSystray) failed\n");
    return FALSE;
  }
179 180 181 182
  return TRUE;
}


Mike McCormack's avatar
Mike McCormack committed
183
static BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
184 185 186 187
{
  RECT rect;

  /* Register the class if this is our first tray item. */
188 189 190
  if ( firstSystray ) {
    firstSystray = FALSE;
    if ( !SYSTRAY_RegisterClass() ) {
191 192 193 194 195 196
      ERR( "RegisterClass(WineSystray) failed\n" );
      return FALSE;
    }
  }

  /* Initialize the window size. */
197 198
  rect.left   = 0;
  rect.top    = 0;
199 200
  rect.right  = ICON_SIZE+2*ICON_BORDER;
  rect.bottom = ICON_SIZE+2*ICON_BORDER;
201

202
  ZeroMemory( ptrayItem, sizeof(SystrayItem) );
203 204 205 206 207 208 209
  /* Create tray window for icon. */
  ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
                                "WineSystray", "Wine-Systray",
                                WS_VISIBLE,
                                CW_USEDEFAULT, CW_USEDEFAULT,
                                rect.right-rect.left, rect.bottom-rect.top,
                                0, 0, 0, 0 );
210
  if ( !ptrayItem->hWnd ) {
211
    ERR( "CreateWindow(WineSystray) failed\n" );
212 213
    return FALSE;
  }
214 215 216 217 218 219

  /* Create tooltip for icon. */
  ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
                                     CW_USEDEFAULT, CW_USEDEFAULT,
                                     CW_USEDEFAULT, CW_USEDEFAULT,
                                     ptrayItem->hWnd, 0, 0, 0 );
220
  if ( !ptrayItem->hWndToolTip ) {
221
    ERR( "CreateWindow(TOOLTIP) failed\n" );
222 223 224 225 226
    return FALSE;
  }
  return TRUE;
}

227

228
static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
229
{
230
  if(ptrayItem->notifyIcon.hIcon)
231
     DestroyIcon(ptrayItem->notifyIcon.hIcon);
232 233 234 235 236
  if(ptrayItem->hWndToolTip)
      DestroyWindow(ptrayItem->hWndToolTip);
  if(ptrayItem->hWnd)
    DestroyWindow(ptrayItem->hWnd);
  return;
237 238
}

239

Mike McCormack's avatar
Mike McCormack committed
240
static void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
241
{
242
  ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
243 244
}

245

Mike McCormack's avatar
Mike McCormack committed
246
static void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
247
{
Ove Kaaven's avatar
Ove Kaaven committed
248 249
  if(ptrayItem->notifyIcon.hIcon)
    DestroyIcon(ptrayItem->notifyIcon.hIcon);
250
  ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
251
  InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
252 253
}

254

Mike McCormack's avatar
Mike McCormack committed
255
static void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
256
{
257 258
  TTTOOLINFOA ti;

259
  lstrcpynA(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
260 261

  ti.cbSize = sizeof(TTTOOLINFOA);
262 263 264
  ti.uFlags = 0;
  ti.hwnd = ptrayItem->hWnd;
  ti.hinst = 0;
265
  ti.uId = 0;
266 267 268
  ti.lpszText = ptrayItem->notifyIcon.szTip;
  ti.rect.left   = 0;
  ti.rect.top    = 0;
269 270
  ti.rect.right  = ICON_SIZE+2*ICON_BORDER;
  ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
271

272 273 274 275
  if(modify)
    SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
  else
    SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
276 277
}

278

279 280
static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
{
281
  SystrayItem **ptrayItem = &systray;
282

283 284 285
  /* Find last element. */
  while( *ptrayItem ) {
    if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
286
      return FALSE;
287
    ptrayItem = &((*ptrayItem)->nextTrayItem);
288
  }
289
  /* Allocate SystrayItem for element and add to end of list. */
290
  (*ptrayItem) = HeapAlloc(GetProcessHeap(),0,sizeof(SystrayItem));
291 292

  /* Initialize and set data for the tray element. */
293 294 295 296 297 298 299
  SYSTRAY_ItemInit( (*ptrayItem) );
  (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
  (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
  SYSTRAY_ItemSetIcon   (*ptrayItem, (pnid->uFlags&NIF_ICON)   ?pnid->hIcon           :0);
  SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
  SYSTRAY_ItemSetTip    (*ptrayItem, (pnid->uFlags&NIF_TIP)    ?pnid->szTip           :"", FALSE);

300
  TRACE("%p: %p %s\n",  (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
301
                                          (*ptrayItem)->notifyIcon.szTip);
302 303 304
  return TRUE;
}

305

306 307
static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
{
308
  SystrayItem *ptrayItem = systray;
309

310 311
  while ( ptrayItem ) {
    if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
312
      if (pnid->uFlags & NIF_ICON)
313
        SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
314
      if (pnid->uFlags & NIF_MESSAGE)
315
        SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
316
      if (pnid->uFlags & NIF_TIP)
317
        SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
318

319
      TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
320 321
      return TRUE;
    }
322
    ptrayItem = ptrayItem->nextTrayItem;
323 324 325 326
  }
  return FALSE; /* not found */
}

327

328 329
static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
{
330
  SystrayItem **ptrayItem = &systray;
331

332 333 334
  while (*ptrayItem) {
    if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
      SystrayItem *next = (*ptrayItem)->nextTrayItem;
335
      TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
336
      SYSTRAY_ItemTerm(*ptrayItem);
337

338
      HeapFree(GetProcessHeap(),0,*ptrayItem);
339 340
      *ptrayItem = next;

341 342
      return TRUE;
    }
343
    ptrayItem = &((*ptrayItem)->nextTrayItem);
344 345 346 347 348 349 350 351 352 353 354 355 356 357
  }

  return FALSE; /* not found */
}

/*************************************************************************
 *
 */
BOOL SYSTRAY_Init(void)
{
  return TRUE;
}

/*************************************************************************
358 359
 * Shell_NotifyIcon			[SHELL32.296]
 * Shell_NotifyIconA			[SHELL32.297]
360 361 362 363
 */
BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
{
  BOOL flag=FALSE;
364
  TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
365 366 367 368 369 370 371 372 373 374 375
  switch(dwMessage) {
  case NIM_ADD:
    flag = SYSTRAY_Add(pnid);
    break;
  case NIM_MODIFY:
    flag = SYSTRAY_Modify(pnid);
    break;
  case NIM_DELETE:
    flag = SYSTRAY_Delete(pnid);
    break;
  }
376
  TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
377 378 379
  return flag;
}

Juergen Schmied's avatar
Juergen Schmied committed
380
/*************************************************************************
381
 * Shell_NotifyIconW			[SHELL32.298]
Juergen Schmied's avatar
Juergen Schmied committed
382 383 384 385 386 387 388
 */
BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
{
	BOOL ret;

	PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
	memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
389 390
        WideCharToMultiByte( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip), NULL, NULL );
        p->szTip[sizeof(p->szTip)-1] = 0;
Juergen Schmied's avatar
Juergen Schmied committed
391 392 393 394 395 396

	ret = Shell_NotifyIconA(dwMessage, p );

	HeapFree(GetProcessHeap(),0,p);
	return ret;
}