systray.c 11 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

160
BOOL SYSTRAY_RegisterClass(void)
161 162
{
  WNDCLASSA  wc;
163

164
  wc.style         = CS_SAVEBITS|CS_DBLCLKS;
165 166 167 168
  wc.lpfnWndProc   = (WNDPROC)SYSTRAY_WndProc;
  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;
}


183
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

240
void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
241
{
242
  ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
243 244
}

245

246 247
void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
{
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 255

void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
256
{
257 258 259 260
  TTTOOLINFOA ti;

  strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
  ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
261 262

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

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

279

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

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

  /* Initialize and set data for the tray element. */
294 295 296 297 298 299 300
  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);

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

306

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

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

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

328

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

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

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

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

  return FALSE; /* not found */
}

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

/*************************************************************************
359 360
 * Shell_NotifyIcon			[SHELL32.296]
 * Shell_NotifyIconA			[SHELL32.297]
361 362 363 364
 */
BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
{
  BOOL flag=FALSE;
365
  TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
366 367 368 369 370 371 372 373 374 375 376
  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;
  }
377
  TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
378 379 380
  return flag;
}

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

	PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
	memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
390 391
        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
392 393 394 395 396 397

	ret = Shell_NotifyIconA(dwMessage, p );

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