4 * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
5 * Copyright 2018 Ged Murphy <gedmurphy@reactos.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 static const WCHAR szSysPagerWndClass
[] = L
"SysPager";
29 // Data comes from shell32/systray.cpp -> TrayNotifyCDS_Dummy
30 typedef struct _SYS_PAGER_COPY_DATA
34 NOTIFYICONDATA nicon_data
;
35 } SYS_PAGER_COPY_DATA
, *PSYS_PAGER_COPY_DATA
;
38 struct IconWatcherData
42 NOTIFYICONDATA IconData
;
44 IconWatcherData(NOTIFYICONDATA
*iconData
) :
45 hProcess(NULL
), ProcessId(0)
47 IconData
.cbSize
= sizeof(NOTIFYICONDATA
);
48 IconData
.hWnd
= iconData
->hWnd
;
49 IconData
.uID
= iconData
->uID
;
50 IconData
.guidItem
= iconData
->guidItem
;
57 CloseHandle(hProcess
);
64 CAtlList
<IconWatcherData
*> m_WatcherList
;
65 CRITICAL_SECTION m_ListLock
;
66 HANDLE m_hWatcherThread
;
73 m_hWatcherThread(NULL
),
80 virtual ~CIconWatcher()
84 CloseHandle(m_WakeUpEvent
);
86 CloseHandle(m_hWatcherThread
);
89 bool Initialize(_In_ HWND hWndParent
)
91 m_hwndSysTray
= hWndParent
;
93 InitializeCriticalSection(&m_ListLock
);
94 m_WakeUpEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
95 if (m_WakeUpEvent
== NULL
)
98 m_hWatcherThread
= (HANDLE
)_beginthreadex(NULL
,
104 if (m_hWatcherThread
== NULL
)
113 SetEvent(m_WakeUpEvent
);
115 EnterCriticalSection(&m_ListLock
);
118 for (size_t i
= 0; i
< m_WatcherList
.GetCount(); i
++)
120 Pos
= m_WatcherList
.FindIndex(i
);
123 IconWatcherData
*Icon
;
124 Icon
= m_WatcherList
.GetAt(Pos
);
125 CloseHandle(Icon
->hProcess
);
128 m_WatcherList
.RemoveAll();
130 LeaveCriticalSection(&m_ListLock
);
133 bool AddIconToWatcher(_In_ NOTIFYICONDATA
*iconData
)
135 IconWatcherData
*Icon
= new IconWatcherData(iconData
);
137 (void)GetWindowThreadProcessId(iconData
->hWnd
, &Icon
->ProcessId
);
139 Icon
->hProcess
= OpenProcess(SYNCHRONIZE
, FALSE
, Icon
->ProcessId
);
140 if (Icon
->hProcess
== NULL
)
143 EnterCriticalSection(&m_ListLock
);
145 m_WatcherList
.AddTail(Icon
);
146 SetEvent(m_WakeUpEvent
);
148 LeaveCriticalSection(&m_ListLock
);
153 bool RemoveIconFromWatcher(_In_ NOTIFYICONDATA
*iconData
)
155 EnterCriticalSection(&m_ListLock
);
157 IconWatcherData
*Icon
;
158 Icon
= GetListEntry(iconData
, NULL
, true);
160 SetEvent(m_WakeUpEvent
);
161 LeaveCriticalSection(&m_ListLock
);
167 IconWatcherData
* GetListEntry(_In_opt_ NOTIFYICONDATA
*iconData
, _In_opt_ HANDLE hProcess
, _In_
bool Remove
)
169 IconWatcherData
*Entry
= NULL
;
170 POSITION NextPosition
= m_WatcherList
.GetHeadPosition();
174 Position
= NextPosition
;
176 Entry
= m_WatcherList
.GetNext(NextPosition
);
179 if ((iconData
&& ((Entry
->IconData
.hWnd
== iconData
->hWnd
) && (Entry
->IconData
.uID
== iconData
->uID
))) ||
180 (hProcess
&& (Entry
->hProcess
== hProcess
)))
183 m_WatcherList
.RemoveAt(Position
);
189 } while (NextPosition
!= NULL
);
196 static UINT WINAPI
WatcherThread(_In_opt_ LPVOID lpParam
)
198 CIconWatcher
* This
= reinterpret_cast<CIconWatcher
*>(lpParam
);
206 EnterCriticalSection(&This
->m_ListLock
);
208 Size
= This
->m_WatcherList
.GetCount() + 1;
209 WatchList
= new HANDLE
[Size
];
210 WatchList
[0] = This
->m_WakeUpEvent
;
213 for (size_t i
= 0; i
< This
->m_WatcherList
.GetCount(); i
++)
215 Pos
= This
->m_WatcherList
.FindIndex(i
);
218 IconWatcherData
*Icon
;
219 Icon
= This
->m_WatcherList
.GetAt(Pos
);
220 WatchList
[i
+ 1] = Icon
->hProcess
;
224 LeaveCriticalSection(&This
->m_ListLock
);
227 Status
= WaitForMultipleObjects(Size
,
231 if (Status
== WAIT_OBJECT_0
)
233 // We've been kicked, we have updates to our list (or we're exiting the thread)
235 TRACE("Updating watched icon list");
237 else if ((Status
>= WAIT_OBJECT_0
+ 1) && (Status
< Size
))
239 IconWatcherData
*Icon
;
240 Icon
= This
->GetListEntry(NULL
, WatchList
[Status
], false);
242 TRACE("Pid %lu owns a notification icon and has stopped without deleting it. We'll cleanup on its behalf", Icon
->ProcessId
);
244 int len
= FIELD_OFFSET(SYS_PAGER_COPY_DATA
, nicon_data
) + Icon
->IconData
.cbSize
;
245 PSYS_PAGER_COPY_DATA pnotify_data
= (PSYS_PAGER_COPY_DATA
)new BYTE
[len
];
246 pnotify_data
->cookie
= 1;
247 pnotify_data
->notify_code
= NIM_DELETE
;
248 memcpy(&pnotify_data
->nicon_data
, &Icon
->IconData
, Icon
->IconData
.cbSize
);
253 data
.lpData
= pnotify_data
;
255 BOOL Success
= FALSE
;
256 HWND parentHWND
= ::GetParent(GetParent(This
->m_hwndSysTray
));
258 Success
= ::SendMessage(parentHWND
, WM_COPYDATA
, (WPARAM
)&Icon
->IconData
, (LPARAM
)&data
);
264 // If we failed to handle the delete message, forcibly remove it
265 This
->RemoveIconFromWatcher(&Icon
->IconData
);
270 if (Status
== WAIT_FAILED
)
272 Status
= GetLastError();
274 ERR("Failed to wait on process handles : %lu\n", Status
);
275 This
->m_Loop
= false;
284 class CNotifyToolbar
:
285 public CWindowImplBaseT
< CToolbar
<NOTIFYICONDATA
>, CControlWinTraits
>
287 HIMAGELIST m_ImageList
;
288 int m_VisibleButtonCount
;
293 m_VisibleButtonCount(0)
301 int GetVisibleButtonCount()
303 return m_VisibleButtonCount
;
306 int FindItem(IN HWND hWnd
, IN UINT uID
, NOTIFYICONDATA
** pdata
)
308 int count
= GetButtonCount();
310 for (int i
= 0; i
< count
; i
++)
312 NOTIFYICONDATA
* data
;
314 data
= GetItemData(i
);
316 if (data
->hWnd
== hWnd
&&
328 int FindExistingSharedIcon(HICON handle
)
330 int count
= GetButtonCount();
331 for (int i
= 0; i
< count
; i
++)
333 NOTIFYICONDATA
* data
= GetItemData(i
);
334 if (data
->hIcon
== handle
)
345 BOOL
AddButton(IN CONST NOTIFYICONDATA
*iconData
)
348 NOTIFYICONDATA
* notifyItem
;
351 TRACE("Adding icon %d from hWnd %08x flags%s%s state%s%s",
352 iconData
->uID
, iconData
->hWnd
,
353 (iconData
->uFlags
& NIF_ICON
) ? " ICON" : "",
354 (iconData
->uFlags
& NIF_STATE
) ? " STATE" : "",
355 (iconData
->dwState
& NIS_HIDDEN
) ? " HIDDEN" : "",
356 (iconData
->dwState
& NIS_SHAREDICON
) ? " SHARED" : "");
358 int index
= FindItem(iconData
->hWnd
, iconData
->uID
, ¬ifyItem
);
361 TRACE("Icon %d from hWnd %08x ALREADY EXISTS!", iconData
->uID
, iconData
->hWnd
);
365 notifyItem
= new NOTIFYICONDATA();
366 ZeroMemory(notifyItem
, sizeof(*notifyItem
));
368 notifyItem
->hWnd
= iconData
->hWnd
;
369 notifyItem
->uID
= iconData
->uID
;
371 tbBtn
.fsState
= TBSTATE_ENABLED
;
372 tbBtn
.fsStyle
= BTNS_NOPREFIX
;
373 tbBtn
.dwData
= (DWORD_PTR
)notifyItem
;
374 tbBtn
.iString
= (INT_PTR
) text
;
375 tbBtn
.idCommand
= GetButtonCount();
377 if (iconData
->uFlags
& NIF_STATE
)
379 notifyItem
->dwState
= iconData
->dwState
& iconData
->dwStateMask
;
382 if (iconData
->uFlags
& NIF_MESSAGE
)
384 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
387 if (iconData
->uFlags
& NIF_ICON
)
389 notifyItem
->hIcon
= iconData
->hIcon
;
390 BOOL hasSharedIcon
= notifyItem
->dwState
& NIS_SHAREDICON
;
393 INT iIcon
= FindExistingSharedIcon(notifyItem
->hIcon
);
396 notifyItem
->hIcon
= NULL
;
397 TRACE("Shared icon requested, but HICON not found!!!");
399 tbBtn
.iBitmap
= iIcon
;
403 tbBtn
.iBitmap
= ImageList_AddIcon(m_ImageList
, notifyItem
->hIcon
);
407 if (iconData
->uFlags
& NIF_TIP
)
409 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
412 m_VisibleButtonCount
++;
413 if (notifyItem
->dwState
& NIS_HIDDEN
)
415 tbBtn
.fsState
|= TBSTATE_HIDDEN
;
416 m_VisibleButtonCount
--;
419 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
421 CToolbar::AddButton(&tbBtn
);
422 SetButtonSize(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
));
427 BOOL
UpdateButton(IN CONST NOTIFYICONDATA
*iconData
)
429 NOTIFYICONDATA
* notifyItem
;
430 TBBUTTONINFO tbbi
= { 0 };
432 TRACE("Updating icon %d from hWnd %08x flags%s%s state%s%s",
433 iconData
->uID
, iconData
->hWnd
,
434 (iconData
->uFlags
& NIF_ICON
) ? " ICON" : "",
435 (iconData
->uFlags
& NIF_STATE
) ? " STATE" : "",
436 (iconData
->dwState
& NIS_HIDDEN
) ? " HIDDEN" : "",
437 (iconData
->dwState
& NIS_SHAREDICON
) ? " SHARED" : "");
439 int index
= FindItem(iconData
->hWnd
, iconData
->uID
, ¬ifyItem
);
442 WARN("Icon %d from hWnd %08x DOES NOT EXIST!", iconData
->uID
, iconData
->hWnd
);
443 return AddButton(iconData
);
447 GetButton(index
, &btn
);
448 int oldIconIndex
= btn
.iBitmap
;
450 tbbi
.cbSize
= sizeof(tbbi
);
451 tbbi
.dwMask
= TBIF_BYINDEX
| TBIF_COMMAND
;
452 tbbi
.idCommand
= index
;
454 if (iconData
->uFlags
& NIF_STATE
)
456 if (iconData
->dwStateMask
& NIS_HIDDEN
&&
457 (notifyItem
->dwState
& NIS_HIDDEN
) != (iconData
->dwState
& NIS_HIDDEN
))
459 tbbi
.dwMask
|= TBIF_STATE
;
460 if (iconData
->dwState
& NIS_HIDDEN
)
462 tbbi
.fsState
|= TBSTATE_HIDDEN
;
463 m_VisibleButtonCount
--;
467 tbbi
.fsState
&= ~TBSTATE_HIDDEN
;
468 m_VisibleButtonCount
++;
472 notifyItem
->dwState
&= ~iconData
->dwStateMask
;
473 notifyItem
->dwState
|= (iconData
->dwState
& iconData
->dwStateMask
);
476 if (iconData
->uFlags
& NIF_MESSAGE
)
478 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
481 if (iconData
->uFlags
& NIF_ICON
)
483 BOOL hasSharedIcon
= notifyItem
->dwState
& NIS_SHAREDICON
;
486 INT iIcon
= FindExistingSharedIcon(iconData
->hIcon
);
489 notifyItem
->hIcon
= iconData
->hIcon
;
490 tbbi
.dwMask
|= TBIF_IMAGE
;
495 TRACE("Shared icon requested, but HICON not found!!! IGNORING!");
500 notifyItem
->hIcon
= iconData
->hIcon
;
501 tbbi
.dwMask
|= TBIF_IMAGE
;
502 tbbi
.iImage
= ImageList_ReplaceIcon(m_ImageList
, oldIconIndex
, notifyItem
->hIcon
);
506 if (iconData
->uFlags
& NIF_TIP
)
508 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
511 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
513 SetButtonInfo(index
, &tbbi
);
518 BOOL
RemoveButton(IN CONST NOTIFYICONDATA
*iconData
)
520 NOTIFYICONDATA
* notifyItem
;
522 TRACE("Removing icon %d from hWnd %08x", iconData
->uID
, iconData
->hWnd
);
524 int index
= FindItem(iconData
->hWnd
, iconData
->uID
, ¬ifyItem
);
527 TRACE("Icon %d from hWnd %08x ALREADY MISSING!", iconData
->uID
, iconData
->hWnd
);
532 if (!(notifyItem
->dwState
& NIS_HIDDEN
))
534 m_VisibleButtonCount
--;
537 if (!(notifyItem
->dwState
& NIS_SHAREDICON
))
540 GetButton(index
, &btn
);
541 int oldIconIndex
= btn
.iBitmap
;
542 ImageList_Remove(m_ImageList
, oldIconIndex
);
544 // Update other icons!
545 int count
= GetButtonCount();
546 for (int i
= 0; i
< count
; i
++)
551 if (btn
.iBitmap
> oldIconIndex
)
553 TBBUTTONINFO tbbi2
= { 0 };
554 tbbi2
.cbSize
= sizeof(tbbi2
);
555 tbbi2
.dwMask
= TBIF_BYINDEX
| TBIF_IMAGE
;
556 tbbi2
.iImage
= btn
.iBitmap
-1;
557 SetButtonInfo(i
, &tbbi2
);
568 VOID
GetTooltipText(int index
, LPTSTR szTip
, DWORD cchTip
)
570 NOTIFYICONDATA
* notifyItem
;
571 notifyItem
= GetItemData(index
);
575 StringCchCopy(szTip
, cchTip
, notifyItem
->szTip
);
579 VOID
ResizeImagelist()
584 if (!ImageList_GetIconSize(m_ImageList
, &cx
, &cy
))
587 if (cx
== GetSystemMetrics(SM_CXSMICON
) && cy
== GetSystemMetrics(SM_CYSMICON
))
590 iml
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_COLOR32
| ILC_MASK
, 0, 1000);
594 ImageList_Destroy(m_ImageList
);
596 SetImageList(m_ImageList
);
598 int count
= GetButtonCount();
599 for (int i
= 0; i
< count
; i
++)
601 NOTIFYICONDATA
* data
= GetItemData(i
);
602 BOOL hasSharedIcon
= data
->dwState
& NIS_SHAREDICON
;
603 INT iIcon
= hasSharedIcon
? FindExistingSharedIcon(data
->hIcon
) : -1;
605 iIcon
= ImageList_AddIcon(iml
, data
->hIcon
);
606 TBBUTTONINFO tbbi
= { sizeof(tbbi
), TBIF_BYINDEX
| TBIF_IMAGE
, 0, iIcon
};
607 SetButtonInfo(i
, &tbbi
);
610 SetButtonSize(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
));
615 VOID
SendMouseEvent(IN WORD wIndex
, IN UINT uMsg
, IN WPARAM wParam
)
617 static LPCWSTR eventNames
[] = {
634 NOTIFYICONDATA
* notifyItem
= GetItemData(wIndex
);
636 if (!::IsWindow(notifyItem
->hWnd
))
638 // We detect and destroy icons with invalid handles only on mouse move over systray, same as MS does.
639 // Alternatively we could search for them periodically (would waste more resources).
640 TRACE("Destroying icon %d with invalid handle hWnd=%08x\n", notifyItem
->uID
, notifyItem
->hWnd
);
642 RemoveButton(notifyItem
);
644 HWND parentHWND
= ::GetParent(::GetParent(GetParent()));
645 ::SendMessage(parentHWND
, WM_SIZE
, 0, 0);
650 if (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
)
652 TRACE("Sending message %S from button %d to %p (msg=%x, w=%x, l=%x)...\n",
653 eventNames
[uMsg
- WM_MOUSEFIRST
], wIndex
,
654 notifyItem
->hWnd
, notifyItem
->uCallbackMessage
, notifyItem
->uID
, uMsg
);
658 GetWindowThreadProcessId(notifyItem
->hWnd
, &pid
);
660 if (pid
== GetCurrentProcessId() ||
661 (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
))
663 ::PostMessage(notifyItem
->hWnd
,
664 notifyItem
->uCallbackMessage
,
670 SendMessage(notifyItem
->hWnd
,
671 notifyItem
->uCallbackMessage
,
677 LRESULT
OnMouseEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
679 POINT pt
= { GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
) };
681 INT iBtn
= HitTest(&pt
);
685 SendMouseEvent(iBtn
, uMsg
, wParam
);
692 LRESULT
OnTooltipShow(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
695 ::GetWindowRect(hdr
->hwndFrom
, &rcTip
);
697 SIZE szTip
= { rcTip
.right
- rcTip
.left
, rcTip
.bottom
- rcTip
.top
};
699 INT iBtn
= GetHotItem();
703 MONITORINFO monInfo
= { 0 };
704 HMONITOR hMon
= MonitorFromWindow(m_hWnd
, MONITOR_DEFAULTTONEAREST
);
706 monInfo
.cbSize
= sizeof(monInfo
);
709 GetMonitorInfo(hMon
, &monInfo
);
711 ::GetWindowRect(GetDesktopWindow(), &monInfo
.rcMonitor
);
713 GetItemRect(iBtn
, &rcItem
);
715 POINT ptItem
= { rcItem
.left
, rcItem
.top
};
716 SIZE szItem
= { rcItem
.right
- rcItem
.left
, rcItem
.bottom
- rcItem
.top
};
717 ClientToScreen(&ptItem
);
719 ptItem
.x
+= szItem
.cx
/ 2;
720 ptItem
.y
-= szTip
.cy
;
722 if (ptItem
.x
+ szTip
.cx
> monInfo
.rcMonitor
.right
)
723 ptItem
.x
= monInfo
.rcMonitor
.right
- szTip
.cx
;
725 if (ptItem
.y
+ szTip
.cy
> monInfo
.rcMonitor
.bottom
)
726 ptItem
.y
= monInfo
.rcMonitor
.bottom
- szTip
.cy
;
728 if (ptItem
.x
< monInfo
.rcMonitor
.left
)
729 ptItem
.x
= monInfo
.rcMonitor
.left
;
731 if (ptItem
.y
< monInfo
.rcMonitor
.top
)
732 ptItem
.y
= monInfo
.rcMonitor
.top
;
734 TRACE("ptItem { %d, %d }\n", ptItem
.x
, ptItem
.y
);
736 ::SetWindowPos(hdr
->hwndFrom
, NULL
, ptItem
.x
, ptItem
.y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
747 BEGIN_MSG_MAP(CNotifyToolbar
)
748 MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST
, WM_MOUSELAST
, OnMouseEvent
)
749 NOTIFY_CODE_HANDLER(TTN_SHOW
, OnTooltipShow
)
752 void Initialize(HWND hWndParent
)
755 WS_CHILD
| WS_VISIBLE
| WS_CLIPCHILDREN
|
756 TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
| TBSTYLE_WRAPABLE
| TBSTYLE_TRANSPARENT
|
757 CCS_TOP
| CCS_NORESIZE
| CCS_NOPARENTALIGN
| CCS_NODIVIDER
;
759 SubclassWindow(CToolbar::Create(hWndParent
, styles
));
761 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
763 m_ImageList
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_COLOR32
| ILC_MASK
, 0, 1000);
764 SetImageList(m_ImageList
);
766 TBMETRICS tbm
= {sizeof(tbm
)};
767 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
| TBMF_PAD
;
770 tbm
.cxButtonSpacing
= 1;
771 tbm
.cyButtonSpacing
= 1;
774 SetButtonSize(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
));
779 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
780 public CWindowImpl
< CSysPagerWnd
, CWindow
, CControlWinTraits
>,
783 CNotifyToolbar Toolbar
;
787 virtual ~CSysPagerWnd() {}
789 LRESULT
DrawBackground(HDC hdc
)
793 GetClientRect(&rect
);
794 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
799 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
801 HDC hdc
= (HDC
) wParam
;
809 return DrawBackground(hdc
);
812 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
814 Toolbar
.Initialize(m_hWnd
);
815 CIconWatcher::Initialize(m_hWnd
);
817 // Explicitly request running applications to re-register their systray icons
818 ::SendNotifyMessageW(HWND_BROADCAST
,
819 RegisterWindowMessageW(L
"TaskbarCreated"),
825 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
827 CIconWatcher::Uninitialize();
831 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
833 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
834 if (cpData
->dwData
== 1)
836 SYS_PAGER_COPY_DATA
* data
;
837 NOTIFYICONDATA
*iconData
;
840 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
842 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
843 iconData
= &data
->nicon_data
;
845 TRACE("NotifyIconCmd received. Code=%d\n", data
->notify_code
);
846 switch (data
->notify_code
)
849 ret
= Toolbar
.AddButton(iconData
);
852 AddIconToWatcher(iconData
);
856 ret
= Toolbar
.UpdateButton(iconData
);
859 ret
= Toolbar
.RemoveButton(iconData
);
862 RemoveIconFromWatcher(iconData
);
866 TRACE("NotifyIconCmd received with unknown code %d.\n", data
->notify_code
);
870 if (VisibleButtonCount
!= Toolbar
.GetVisibleButtonCount())
872 HWND parentHWND
= ::GetParent(GetParent());
873 ::SendMessage(parentHWND
, WM_SIZE
, 0, 0);
882 void GetSize(IN BOOL IsHorizontal
, IN PSIZE size
)
884 /* Get the ideal height or width */
886 /* Unfortunately this doens't work correctly in ros */
887 Toolbar
.GetIdealSize(!IsHorizontal
, size
);
889 /* Make the reference dimension an exact multiple of the icon size */
891 size
->cy
-= size
->cy
% GetSystemMetrics(SM_CYSMICON
);
893 size
->cx
-= size
->cx
% GetSystemMetrics(SM_CXSMICON
);
898 INT cyButton
= GetSystemMetrics(SM_CYSMICON
) + 2;
899 INT cxButton
= GetSystemMetrics(SM_CXSMICON
) + 2;
900 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
904 rows
= max(size
->cy
/ cyButton
, 1);
905 columns
= (VisibleButtonCount
+ rows
- 1) / rows
;
909 columns
= max(size
->cx
/ cxButton
, 1);
910 rows
= (VisibleButtonCount
+ columns
- 1) / columns
;
912 size
->cx
= columns
* cxButton
;
913 size
->cy
= rows
* cyButton
;
917 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
919 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
920 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
924 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
926 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
927 switch (cdraw
->dwDrawStage
)
930 return CDRF_NOTIFYITEMDRAW
;
932 case CDDS_ITEMPREPAINT
:
933 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
938 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
942 szClient
.cx
= LOWORD(lParam
);
943 szClient
.cy
= HIWORD(lParam
);
945 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
949 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
953 Toolbar
.GetClientRect(&rc
);
955 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
957 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
958 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
960 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
965 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
971 void ResizeImagelist()
973 Toolbar
.ResizeImagelist();
976 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
978 BEGIN_MSG_MAP(CSysPagerWnd
)
979 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
980 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
981 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
982 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
983 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
984 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
985 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
988 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
992 /* Create the window. The tray window is going to move it to the correct
993 position and resize it as needed. */
994 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
996 dwStyle
|= WS_VISIBLE
;
998 Create(hWndParent
, 0, NULL
, dwStyle
);
1005 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1015 static const WCHAR szTrayClockWndClass
[] = L
"TrayClockWClass";
1017 #define ID_TRAYCLOCK_TIMER 0
1018 #define ID_TRAYCLOCK_TIMER_INIT 1
1023 DWORD dwFormatFlags
;
1025 } ClockWndFormats
[] = {
1027 { FALSE
, 0, L
"dddd" },
1028 { FALSE
, DATE_SHORTDATE
, NULL
}
1031 #define CLOCKWND_FORMAT_COUNT (_ARRAYSIZE(ClockWndFormats))
1033 #define TRAY_CLOCK_WND_SPACING_X 0
1034 #define TRAY_CLOCK_WND_SPACING_Y 0
1036 class CTrayClockWnd
:
1037 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1038 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
1044 SYSTEMTIME LocalTime
;
1051 DWORD IsTimerEnabled
: 1;
1052 DWORD IsInitTimerEnabled
: 1;
1053 DWORD LinesMeasured
: 1;
1054 DWORD IsHorizontal
: 1;
1060 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
1061 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
1071 ZeroMemory(&textColor
, sizeof(textColor
));
1072 ZeroMemory(&rcText
, sizeof(rcText
));
1073 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
1074 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
1075 ZeroMemory(LineSizes
, sizeof(LineSizes
));
1076 ZeroMemory(szLines
, sizeof(szLines
));
1078 virtual ~CTrayClockWnd() { }
1080 LRESULT
OnThemeChanged()
1086 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
1090 GetThemeFont(clockTheme
,
1097 hFont
= CreateFontIndirectW(&clockFont
);
1099 GetThemeColor(clockTheme
,
1105 if (this->hFont
!= NULL
)
1106 DeleteObject(this->hFont
);
1108 SetFont(hFont
, FALSE
);
1112 /* We don't need to set a font here, our parent will use
1113 * WM_SETFONT to set the right one when themes are not enabled. */
1114 textColor
= RGB(0, 0, 0);
1117 CloseThemeData(clockTheme
);
1122 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1124 return OnThemeChanged();
1138 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1140 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
1142 if (szLines
[i
][0] != L
'\0' &&
1143 !GetTextExtentPointW(hDC
, szLines
[i
], wcslen(szLines
[i
]),
1152 SelectObject(hDC
, hPrevFont
);
1160 /* calculate the line spacing */
1161 for (i
= 0, c
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
1163 if (LineSizes
[i
].cx
> 0)
1165 LineSpacing
+= LineSizes
[i
].cy
;
1172 /* We want a spacing of 1/2 line */
1173 LineSpacing
= (LineSpacing
/ c
) / 2;
1183 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
1185 WORD iLinesVisible
= 0;
1187 SIZE szMax
= { 0, 0 };
1190 LinesMeasured
= MeasureLines();
1195 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
1197 if (LineSizes
[i
].cx
!= 0)
1199 if (iLinesVisible
> 0)
1203 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
1204 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
1211 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
1215 /* Add line spacing */
1216 szMax
.cy
+= LineSpacing
;
1221 /* Increase maximum rectangle */
1222 szMax
.cy
+= LineSizes
[i
].cy
;
1223 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
1224 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
1228 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
1229 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
1233 return iLinesVisible
;
1244 ZeroMemory(LineSizes
, sizeof(LineSizes
));
1246 szPrevCurrent
= CurrentSize
;
1248 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
1250 szLines
[i
][0] = L
'\0';
1251 BufSize
= _countof(szLines
[0]);
1253 if (ClockWndFormats
[i
].IsTime
)
1255 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
1256 g_TaskbarSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
1258 ClockWndFormats
[i
].lpFormat
,
1264 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
1265 ClockWndFormats
[i
].dwFormatFlags
,
1267 ClockWndFormats
[i
].lpFormat
,
1272 if (iRet
!= 0 && i
== 0)
1274 /* Set the window text to the time only */
1275 SetWindowText(szLines
[i
]);
1279 LinesMeasured
= MeasureLines();
1281 if (LinesMeasured
&&
1282 GetClientRect(&rcClient
))
1286 szWnd
.cx
= rcClient
.right
;
1287 szWnd
.cy
= rcClient
.bottom
;
1289 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
1290 CurrentSize
= szWnd
;
1293 if (IsWindowVisible())
1295 InvalidateRect(NULL
, TRUE
);
1297 if (hWndNotify
!= NULL
&&
1298 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
1299 szPrevCurrent
.cy
!= CurrentSize
.cy
))
1303 nmh
.hwndFrom
= m_hWnd
;
1304 nmh
.idFrom
= GetWindowLongPtr(GWLP_ID
);
1305 nmh
.code
= NTNWM_REALIGN
;
1307 SendMessage(hWndNotify
,
1309 (WPARAM
) nmh
.idFrom
,
1317 GetLocalTime(&LocalTime
);
1321 UINT
CalculateDueTime()
1325 /* Calculate the due time */
1326 GetLocalTime(&LocalTime
);
1327 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
1328 if (g_TaskbarSettings
.bShowSeconds
)
1329 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
1331 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
1333 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
1337 /* Add an artificial delay of 0.05 seconds to make sure the timer
1338 doesn't fire too early*/
1350 /* Disable all timers */
1353 KillTimer(ID_TRAYCLOCK_TIMER
);
1354 IsTimerEnabled
= FALSE
;
1357 if (IsInitTimerEnabled
)
1359 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1362 uiDueTime
= CalculateDueTime();
1364 /* Set the new timer */
1365 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
1366 IsInitTimerEnabled
= Ret
;
1368 /* Update the time */
1374 VOID
CalibrateTimer()
1378 UINT uiWait1
, uiWait2
;
1380 /* Kill the initialization timer */
1381 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1382 IsInitTimerEnabled
= FALSE
;
1384 uiDueTime
= CalculateDueTime();
1386 if (g_TaskbarSettings
.bShowSeconds
)
1388 uiWait1
= 1000 - 200;
1393 uiWait1
= 60 * 1000 - 200;
1394 uiWait2
= 60 * 1000;
1397 if (uiDueTime
> uiWait1
)
1399 /* The update of the clock will be up to 200 ms late, but that's
1400 acceptable. We're going to setup a timer that fires depending
1402 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
1403 IsTimerEnabled
= Ret
;
1405 /* Update the time */
1410 /* Recalibrate the timer and recalculate again when the current
1411 minute/second ends. */
1416 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1418 /* Disable all timers */
1421 KillTimer(ID_TRAYCLOCK_TIMER
);
1424 if (IsInitTimerEnabled
)
1426 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1432 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1440 HDC hDC
= (HDC
) wParam
;
1444 hDC
= BeginPaint(&ps
);
1450 if (LinesMeasured
&&
1451 GetClientRect(&rcClient
))
1453 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1455 SetTextColor(hDC
, textColor
);
1457 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1459 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1460 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1461 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1462 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1464 for (i
= 0, line
= 0;
1465 i
< CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1468 if (LineSizes
[i
].cx
!= 0)
1471 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1472 TRAY_CLOCK_WND_SPACING_X
,
1473 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1475 wcslen(szLines
[i
]));
1477 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1482 SelectObject(hDC
, hPrevFont
);
1484 SetBkMode(hDC
, iPrevBkMode
);
1495 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1498 LinesMeasured
= MeasureLines();
1501 InvalidateRect(NULL
, TRUE
);
1505 LRESULT
DrawBackground(HDC hdc
)
1509 GetClientRect(&rect
);
1510 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1515 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1517 HDC hdc
= (HDC
) wParam
;
1525 return DrawBackground(hdc
);
1528 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1532 case ID_TRAYCLOCK_TIMER
:
1536 case ID_TRAYCLOCK_TIMER_INIT
:
1543 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1545 IsHorizontal
= (BOOL
) wParam
;
1547 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1550 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1552 return (LRESULT
) ResetTime();
1555 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1557 return HTTRANSPARENT
;
1560 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1562 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1566 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1572 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1576 szClient
.cx
= LOWORD(lParam
);
1577 szClient
.cy
= HIWORD(lParam
);
1579 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1580 CurrentSize
= szClient
;
1582 InvalidateRect(NULL
, TRUE
);
1586 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1588 BEGIN_MSG_MAP(CTrayClockWnd
)
1589 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1590 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1591 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1592 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1593 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1594 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1595 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1596 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1597 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1598 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1599 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1600 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1604 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1606 IsHorizontal
= TRUE
;
1608 hWndNotify
= hWndParent
;
1610 /* Create the window. The tray window is going to move it to the correct
1611 position and resize it as needed. */
1612 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1614 dwStyle
|= WS_VISIBLE
;
1616 Create(hWndParent
, 0, NULL
, dwStyle
);
1619 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1630 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1632 #define TRAY_NOTIFY_WND_SPACING_X 1
1633 #define TRAY_NOTIFY_WND_SPACING_Y 1
1635 class CTrayNotifyWnd
:
1636 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1637 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1641 CSysPagerWnd
* m_pager
;
1642 CTrayClockWnd
* m_clock
;
1644 CComPtr
<ITrayWindow
> TrayWindow
;
1647 SIZE szTrayClockMin
;
1649 MARGINS ContentMargin
;
1660 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1661 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1662 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1664 virtual ~CTrayNotifyWnd() { }
1666 LRESULT
OnThemeChanged()
1669 CloseThemeData(TrayTheme
);
1671 if (IsThemeActive())
1672 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1678 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1680 GetThemeMargins(TrayTheme
,
1690 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1692 ContentMargin
.cxLeftWidth
= 2;
1693 ContentMargin
.cxRightWidth
= 2;
1694 ContentMargin
.cyTopHeight
= 2;
1695 ContentMargin
.cyBottomHeight
= 2;
1701 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1703 return OnThemeChanged();
1706 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1708 m_clock
= new CTrayClockWnd();
1709 m_clock
->_Init(m_hWnd
, !g_TaskbarSettings
.sr
.HideClock
);
1711 m_pager
= new CSysPagerWnd();
1712 m_pager
->_Init(m_hWnd
, !g_TaskbarSettings
.sr
.HideClock
);
1717 BOOL
GetMinimumSize(IN OUT PSIZE pSize
)
1719 SIZE szClock
= { 0, 0 };
1720 SIZE szTray
= { 0, 0 };
1722 if (!g_TaskbarSettings
.sr
.HideClock
)
1726 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1727 if (szClock
.cy
<= 0)
1732 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1733 if (szClock
.cx
<= 0)
1737 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) IsHorizontal
, (LPARAM
) &szClock
);
1739 szTrayClockMin
= szClock
;
1743 szTrayClockMin
= szClock
;
1747 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1751 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1754 m_pager
->GetSize(IsHorizontal
, &szTray
);
1756 szTrayNotify
= szTray
;
1760 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1762 if (!g_TaskbarSettings
.sr
.HideClock
)
1763 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1765 pSize
->cx
+= szTray
.cx
;
1769 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1771 if (!g_TaskbarSettings
.sr
.HideClock
)
1772 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1774 pSize
->cy
+= szTray
.cy
;
1777 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1778 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1783 VOID
Size(IN
const SIZE
*pszClient
)
1785 if (!g_TaskbarSettings
.sr
.HideClock
)
1792 ptClock
.x
= pszClient
->cx
- szTrayClockMin
.cx
- ContentMargin
.cxRightWidth
;
1793 ptClock
.y
= ContentMargin
.cyTopHeight
;
1794 szClock
.cx
= szTrayClockMin
.cx
;
1795 szClock
.cy
= pszClient
->cy
- ContentMargin
.cyTopHeight
- ContentMargin
.cyBottomHeight
;
1799 ptClock
.x
= ContentMargin
.cxLeftWidth
;
1800 ptClock
.y
= pszClient
->cy
- szTrayClockMin
.cy
;
1801 szClock
.cx
= pszClient
->cx
- ContentMargin
.cxLeftWidth
- ContentMargin
.cxRightWidth
;
1802 szClock
.cy
= szTrayClockMin
.cy
;
1805 m_clock
->SetWindowPos(
1817 ptPager
.x
= ContentMargin
.cxLeftWidth
;
1818 ptPager
.y
= (pszClient
->cy
- szTrayNotify
.cy
)/2;
1822 ptPager
.x
= (pszClient
->cx
- szTrayNotify
.cx
)/2;
1823 ptPager
.y
= ContentMargin
.cyTopHeight
;
1826 m_pager
->SetWindowPos(
1836 LRESULT
DrawBackground(HDC hdc
)
1841 GetClientRect(&rect
);
1845 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1847 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1850 res
= DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1856 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1858 HDC hdc
= (HDC
) wParam
;
1866 return DrawBackground(hdc
);
1869 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
1873 return m_pager
->NotifyIconCmd(wParam
, lParam
);
1879 BOOL
GetClockRect(OUT PRECT rcClock
)
1881 if (!m_clock
->IsWindowVisible())
1884 return m_clock
->GetWindowRect(rcClock
);
1887 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1889 BOOL Horizontal
= (BOOL
) wParam
;
1891 if (Horizontal
!= IsHorizontal
)
1893 IsHorizontal
= Horizontal
;
1895 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1897 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1900 return (LRESULT
) GetMinimumSize((PSIZE
) lParam
);
1903 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1905 if (m_clock
!= NULL
)
1907 /* Forward the message to the tray clock window procedure */
1908 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1913 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1917 szClient
.cx
= LOWORD(lParam
);
1918 szClient
.cy
= HIWORD(lParam
);
1925 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1927 return HTTRANSPARENT
;
1930 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1932 BOOL PrevHidden
= g_TaskbarSettings
.sr
.HideClock
;
1933 g_TaskbarSettings
.sr
.HideClock
= (wParam
== 0);
1935 if (m_clock
!= NULL
&& PrevHidden
!= g_TaskbarSettings
.sr
.HideClock
)
1937 m_clock
->ShowWindow(g_TaskbarSettings
.sr
.HideClock
? SW_HIDE
: SW_SHOW
);
1940 return (LRESULT
) (!PrevHidden
);
1943 LRESULT
OnTaskbarSettingsChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1945 TaskbarSettings
* newSettings
= (TaskbarSettings
*)lParam
;
1946 if (newSettings
->bShowSeconds
!= g_TaskbarSettings
.bShowSeconds
)
1948 g_TaskbarSettings
.bShowSeconds
= newSettings
->bShowSeconds
;
1949 /* TODO: Toggle showing seconds */
1952 if (newSettings
->sr
.HideClock
!= g_TaskbarSettings
.sr
.HideClock
)
1954 g_TaskbarSettings
.sr
.HideClock
= newSettings
->sr
.HideClock
;
1955 /* TODO: Toggle hiding the clock */
1961 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1963 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1965 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1967 /* Pass down notifications */
1968 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1974 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1976 if (m_clock
!= NULL
)
1978 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1985 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1991 LRESULT
OnSettingChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1993 if (wParam
== SPI_SETNONCLIENTMETRICS
)
1995 m_pager
->ResizeImagelist();
2000 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
2002 BEGIN_MSG_MAP(CTrayNotifyWnd
)
2003 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
2004 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
2005 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
2006 MESSAGE_HANDLER(WM_SETTINGCHANGE
, OnSettingChanged
)
2007 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
2008 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
2009 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
2010 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
2011 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
2012 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
2013 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
2014 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
2015 MESSAGE_HANDLER(TWM_SETTINGSCHANGED
, OnTaskbarSettingsChanged
)
2018 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
)
2020 HWND hWndTrayWindow
;
2022 hWndTrayWindow
= TrayWindow
->GetHWND();
2023 if (hWndTrayWindow
== NULL
)
2026 this->TrayWindow
= TrayWindow
;
2027 this->hWndNotify
= hWndTrayWindow
;
2029 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
2030 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
2034 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, CTrayNotifyWnd
** ppinstance
)
2036 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
2037 // TODO: Destroy after the window is destroyed
2038 *ppinstance
= pTrayNotify
;
2040 return pTrayNotify
->_Init(Tray
);
2044 TrayNotify_NotifyIconCmd(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
2046 return pTrayNotify
->NotifyIconCmd(wParam
, lParam
);
2050 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
2052 return pTrayNotify
->GetClockRect(rcClock
);