systray.c 11.2 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
  HWND                  hWnd;
  HWND                  hWndToolTip;
Vincent Béron's avatar
Vincent Béron committed
49
  NOTIFYICONDATAW       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 59 60
#define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
/* space around icon (forces icon to center of KDE systray area) */
#define ICON_BORDER  4

61 62


Vincent Béron's avatar
Vincent Béron committed
63
static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAW pnid1, PNOTIFYICONDATAW pnid2)
64 65 66 67 68
{
  if (pnid1->hWnd != pnid2->hWnd) return FALSE;
  if (pnid1->uID  != pnid2->uID)  return FALSE;
  return TRUE;
}
69

70 71 72 73 74 75 76 77 78 79 80 81 82

static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
{
  if(ptrayItem->notifyIcon.hIcon)
     DestroyIcon(ptrayItem->notifyIcon.hIcon);
  if(ptrayItem->hWndToolTip)
      DestroyWindow(ptrayItem->hWndToolTip);
  if(ptrayItem->hWnd)
    DestroyWindow(ptrayItem->hWnd);
  return;
}


Vincent Béron's avatar
Vincent Béron committed
83
static BOOL SYSTRAY_Delete(PNOTIFYICONDATAW pnid)
84 85 86 87 88 89
{
  SystrayItem **ptrayItem = &systray;

  while (*ptrayItem) {
    if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
      SystrayItem *next = (*ptrayItem)->nextTrayItem;
Vincent Béron's avatar
Vincent Béron committed
90
      TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, debugstr_w((*ptrayItem)->notifyIcon.szTip));
91 92 93 94 95 96 97 98 99 100 101 102 103
      SYSTRAY_ItemTerm(*ptrayItem);

      HeapFree(GetProcessHeap(),0,*ptrayItem);
      *ptrayItem = next;

      return TRUE;
    }
    ptrayItem = &((*ptrayItem)->nextTrayItem);
  }

  return FALSE; /* not found */
}

104 105 106 107 108 109 110 111 112
static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  HDC hdc;
  PAINTSTRUCT ps;

  switch (message) {
  case WM_PAINT:
  {
    RECT rc;
113 114 115 116 117 118 119 120 121
    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)) {
122
	    ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
123 124 125 126
	    SYSTRAY_Delete(&ptrayItem->notifyIcon);
	  }
	}
	break;
127 128
      }
      ptrayItem = ptrayItem->nextTrayItem;
129 130
    }
    EndPaint(hWnd, &ps);
131 132 133
  }
  break;

134 135 136 137 138 139 140 141 142
  case WM_MOUSEMOVE:
  case WM_LBUTTONDOWN:
  case WM_LBUTTONUP:
  case WM_RBUTTONDOWN:
  case WM_RBUTTONUP:
  case WM_MBUTTONDOWN:
  case WM_MBUTTONUP:
  {
    MSG msg;
143
    SystrayItem *ptrayItem = systray;
144

145 146
    while ( ptrayItem ) {
      if (ptrayItem->hWnd == hWnd) {
147 148 149 150 151 152 153 154
        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 ());

Vincent Béron's avatar
Vincent Béron committed
155
        SendMessageW(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
156 157 158
      }
      ptrayItem = ptrayItem->nextTrayItem;
    }
159
  }
160
  /* fall through */
161

162 163 164 165
  case WM_LBUTTONDBLCLK:
  case WM_RBUTTONDBLCLK:
  case WM_MBUTTONDBLCLK:
  {
166 167 168 169 170
    SystrayItem *ptrayItem = systray;

    while (ptrayItem) {
      if (ptrayItem->hWnd == hWnd) {
	if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
Vincent Béron's avatar
Vincent Béron committed
171
          if (!PostMessageW(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
172
                            (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
173
	      ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
174 175
	      SYSTRAY_Delete(&ptrayItem->notifyIcon);
	    }
176
        }
177
	break;
178
      }
179
      ptrayItem = ptrayItem->nextTrayItem;
180
    }
181 182 183
  }
  break;

184
  default:
Vincent Béron's avatar
Vincent Béron committed
185
    return (DefWindowProcW(hWnd, message, wParam, lParam));
186 187 188
  }
  return (0);

189 190
}

191

Mike McCormack's avatar
Mike McCormack committed
192
static BOOL SYSTRAY_RegisterClass(void)
193
{
Vincent Béron's avatar
Vincent Béron committed
194 195
  WNDCLASSW  wc;
  static const WCHAR WineSystrayW[] = { 'W','i','n','e','S','y','s','t','r','a','y',0 };
196

197
  wc.style         = CS_SAVEBITS|CS_DBLCLKS;
198
  wc.lpfnWndProc   = SYSTRAY_WndProc;
199 200 201
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = 0;
202
  wc.hIcon         = 0;
Vincent Béron's avatar
Vincent Béron committed
203
  wc.hCursor       = LoadCursorW(0, (LPWSTR)IDC_ARROW);
204
  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
205
  wc.lpszMenuName  = NULL;
Vincent Béron's avatar
Vincent Béron committed
206
  wc.lpszClassName = WineSystrayW;
207

Vincent Béron's avatar
Vincent Béron committed
208
  if (!RegisterClassW(&wc)) {
209 210 211
    ERR("RegisterClass(WineSystray) failed\n");
    return FALSE;
  }
212 213 214 215
  return TRUE;
}


Mike McCormack's avatar
Mike McCormack committed
216
static BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
217 218
{
  RECT rect;
Vincent Béron's avatar
Vincent Béron committed
219 220
  static const WCHAR WineSystrayW[] = { 'W','i','n','e','S','y','s','t','r','a','y',0 };
  static const WCHAR Wine_SystrayW[] = { 'W','i','n','e','-','S','y','s','t','r','a','y',0 };
221 222

  /* Register the class if this is our first tray item. */
223 224 225
  if ( firstSystray ) {
    firstSystray = FALSE;
    if ( !SYSTRAY_RegisterClass() ) {
226 227 228 229 230 231
      ERR( "RegisterClass(WineSystray) failed\n" );
      return FALSE;
    }
  }

  /* Initialize the window size. */
232 233
  rect.left   = 0;
  rect.top    = 0;
234 235
  rect.right  = ICON_SIZE+2*ICON_BORDER;
  rect.bottom = ICON_SIZE+2*ICON_BORDER;
236

237
  ZeroMemory( ptrayItem, sizeof(SystrayItem) );
238
  /* Create tray window for icon. */
Vincent Béron's avatar
Vincent Béron committed
239 240
  ptrayItem->hWnd = CreateWindowExW( WS_EX_TRAYWINDOW,
                                WineSystrayW, Wine_SystrayW,
241 242 243 244
                                WS_VISIBLE,
                                CW_USEDEFAULT, CW_USEDEFAULT,
                                rect.right-rect.left, rect.bottom-rect.top,
                                0, 0, 0, 0 );
245
  if ( !ptrayItem->hWnd ) {
246
    ERR( "CreateWindow(WineSystray) failed\n" );
247 248
    return FALSE;
  }
249 250

  /* Create tooltip for icon. */
Vincent Béron's avatar
Vincent Béron committed
251
  ptrayItem->hWndToolTip = CreateWindowW( TOOLTIPS_CLASSW,NULL,TTS_ALWAYSTIP,
252 253 254
                                     CW_USEDEFAULT, CW_USEDEFAULT,
                                     CW_USEDEFAULT, CW_USEDEFAULT,
                                     ptrayItem->hWnd, 0, 0, 0 );
255
  if ( !ptrayItem->hWndToolTip ) {
256
    ERR( "CreateWindow(TOOLTIP) failed\n" );
257 258 259 260 261
    return FALSE;
  }
  return TRUE;
}

262

Mike McCormack's avatar
Mike McCormack committed
263
static void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
264
{
265
  ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
266 267
}

268

Mike McCormack's avatar
Mike McCormack committed
269
static void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
270
{
Ove Kaaven's avatar
Ove Kaaven committed
271 272
  if(ptrayItem->notifyIcon.hIcon)
    DestroyIcon(ptrayItem->notifyIcon.hIcon);
273
  ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
274
  InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
275 276
}

277

Vincent Béron's avatar
Vincent Béron committed
278
static void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, const WCHAR* szTip, int modify)
279
{
Vincent Béron's avatar
Vincent Béron committed
280
  TTTOOLINFOW ti;
281

Vincent Béron's avatar
Vincent Béron committed
282
  lstrcpynW(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip)/sizeof(WCHAR));
283

Vincent Béron's avatar
Vincent Béron committed
284
  ti.cbSize = sizeof(TTTOOLINFOW);
285 286 287
  ti.uFlags = 0;
  ti.hwnd = ptrayItem->hWnd;
  ti.hinst = 0;
288
  ti.uId = 0;
289 290 291
  ti.lpszText = ptrayItem->notifyIcon.szTip;
  ti.rect.left   = 0;
  ti.rect.top    = 0;
292 293
  ti.rect.right  = ICON_SIZE+2*ICON_BORDER;
  ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
294

295
  if(modify)
Vincent Béron's avatar
Vincent Béron committed
296
    SendMessageW(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
297
  else
Vincent Béron's avatar
Vincent Béron committed
298
    SendMessageW(ptrayItem->hWndToolTip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
299 300
}

301

Vincent Béron's avatar
Vincent Béron committed
302
static BOOL SYSTRAY_Add(PNOTIFYICONDATAW pnid)
303
{
304
  SystrayItem **ptrayItem = &systray;
Vincent Béron's avatar
Vincent Béron committed
305
  static const WCHAR emptyW[] = { 0 };
306

307 308 309
  /* Find last element. */
  while( *ptrayItem ) {
    if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
310
      return FALSE;
311
    ptrayItem = &((*ptrayItem)->nextTrayItem);
312
  }
313
  /* Allocate SystrayItem for element and add to end of list. */
314
  (*ptrayItem) = HeapAlloc(GetProcessHeap(),0,sizeof(SystrayItem));
315 316

  /* Initialize and set data for the tray element. */
317 318 319 320 321
  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);
Vincent Béron's avatar
Vincent Béron committed
322
  SYSTRAY_ItemSetTip    (*ptrayItem, (pnid->uFlags&NIF_TIP)    ?pnid->szTip           :emptyW, FALSE);
323

324
  TRACE("%p: %p %s\n",  (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
Vincent Béron's avatar
Vincent Béron committed
325
                                          debugstr_w((*ptrayItem)->notifyIcon.szTip));
326 327 328
  return TRUE;
}

329

Vincent Béron's avatar
Vincent Béron committed
330
static BOOL SYSTRAY_Modify(PNOTIFYICONDATAW pnid)
331
{
332
  SystrayItem *ptrayItem = systray;
333

334 335
  while ( ptrayItem ) {
    if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
336
      if (pnid->uFlags & NIF_ICON)
337
        SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
338
      if (pnid->uFlags & NIF_MESSAGE)
339
        SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
340
      if (pnid->uFlags & NIF_TIP)
341
        SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
342

Vincent Béron's avatar
Vincent Béron committed
343
      TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, debugstr_w(ptrayItem->notifyIcon.szTip));
344 345
      return TRUE;
    }
346
    ptrayItem = ptrayItem->nextTrayItem;
347 348 349 350
  }
  return FALSE; /* not found */
}

351

352 353 354 355 356 357 358 359 360
/*************************************************************************
 *
 */
BOOL SYSTRAY_Init(void)
{
  return TRUE;
}

/*************************************************************************
Vincent Béron's avatar
Vincent Béron committed
361
 * Shell_NotifyIconW			[SHELL32.298]
362
 */
Vincent Béron's avatar
Vincent Béron committed
363
BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW pnid )
364 365
{
  BOOL flag=FALSE;
366
  TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
367 368 369 370 371 372 373 374 375 376 377
  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;
  }
378
  TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
379 380 381
  return flag;
}

Juergen Schmied's avatar
Juergen Schmied committed
382
/*************************************************************************
Vincent Béron's avatar
Vincent Béron committed
383
 * Shell_NotifyIconA			[SHELL32.297]
384
 * Shell_NotifyIcon			[SHELL32.296]
Juergen Schmied's avatar
Juergen Schmied committed
385
 */
Vincent Béron's avatar
Vincent Béron committed
386
BOOL WINAPI Shell_NotifyIconA (DWORD dwMessage, PNOTIFYICONDATAA pnid )
Juergen Schmied's avatar
Juergen Schmied committed
387 388 389
{
	BOOL ret;

Vincent Béron's avatar
Vincent Béron committed
390 391 392 393
	PNOTIFYICONDATAW p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAW));
	memcpy(p, pnid, sizeof(NOTIFYICONDATAW));
        MultiByteToWideChar( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip)/sizeof(WCHAR) );
        p->szTip[sizeof(p->szTip)/sizeof(WCHAR)-1] = 0;
Juergen Schmied's avatar
Juergen Schmied committed
394

Vincent Béron's avatar
Vincent Béron committed
395
	ret = Shell_NotifyIconW(dwMessage, p );
Juergen Schmied's avatar
Juergen Schmied committed
396 397 398 399

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