4 * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 static const WCHAR szSysPagerWndClass
[] = L
"SysPager";
28 // Data comes from shell32/systray.cpp -> TrayNotifyCDS_Dummy
29 typedef struct _SYS_PAGER_COPY_DATA
33 NOTIFYICONDATA nicon_data
;
34 } SYS_PAGER_COPY_DATA
, *PSYS_PAGER_COPY_DATA
;
36 class CNotifyToolbar
:
37 public CWindowImplBaseT
< CToolbar
<NOTIFYICONDATA
>, CControlWinTraits
>
39 HIMAGELIST m_ImageList
;
40 int m_VisibleButtonCount
;
45 m_VisibleButtonCount(0)
53 int GetVisibleButtonCount()
55 return m_VisibleButtonCount
;
58 int FindItemByIconData(IN CONST NOTIFYICONDATA
*iconData
, NOTIFYICONDATA
** pdata
)
60 int count
= GetButtonCount();
62 for (int i
= 0; i
< count
; i
++)
64 NOTIFYICONDATA
* data
;
66 data
= GetItemData(i
);
68 if (data
->hWnd
== iconData
->hWnd
&&
69 data
->uID
== iconData
->uID
)
80 BOOL
AddButton(IN CONST NOTIFYICONDATA
*iconData
)
83 NOTIFYICONDATA
* notifyItem
;
86 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
89 return UpdateButton(iconData
);
92 notifyItem
= new NOTIFYICONDATA();
93 ZeroMemory(notifyItem
, sizeof(*notifyItem
));
95 notifyItem
->hWnd
= iconData
->hWnd
;
96 notifyItem
->uID
= iconData
->uID
;
98 tbBtn
.fsState
= TBSTATE_ENABLED
;
99 tbBtn
.fsStyle
= BTNS_NOPREFIX
;
100 tbBtn
.dwData
= (DWORD_PTR
)notifyItem
;
101 tbBtn
.iString
= (INT_PTR
) text
;
102 tbBtn
.idCommand
= GetButtonCount();
104 if (iconData
->uFlags
& NIF_MESSAGE
)
106 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
109 if (iconData
->uFlags
& NIF_ICON
)
111 notifyItem
->hIcon
= (HICON
)CopyImage(iconData
->hIcon
, IMAGE_ICON
, 0, 0, 0);
112 tbBtn
.iBitmap
= ImageList_AddIcon(m_ImageList
, iconData
->hIcon
);
115 if (iconData
->uFlags
& NIF_TIP
)
117 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
120 m_VisibleButtonCount
++;
121 if (iconData
->uFlags
& NIF_STATE
)
123 notifyItem
->dwState
&= ~iconData
->dwStateMask
;
124 notifyItem
->dwState
|= (iconData
->dwState
& iconData
->dwStateMask
);
125 if (notifyItem
->dwState
& NIS_HIDDEN
)
127 tbBtn
.fsState
|= TBSTATE_HIDDEN
;
128 m_VisibleButtonCount
--;
132 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
134 CToolbar::AddButton(&tbBtn
);
135 SetButtonSize(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
));
140 BOOL
UpdateButton(IN CONST NOTIFYICONDATA
*iconData
)
142 NOTIFYICONDATA
* notifyItem
;
143 TBBUTTONINFO tbbi
= { 0 };
145 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
148 return AddButton(iconData
);
151 tbbi
.cbSize
= sizeof(tbbi
);
152 tbbi
.dwMask
= TBIF_BYINDEX
| TBIF_COMMAND
;
153 tbbi
.idCommand
= index
;
155 if (iconData
->uFlags
& NIF_MESSAGE
)
157 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
160 if (iconData
->uFlags
& NIF_ICON
)
162 DestroyIcon(notifyItem
->hIcon
);
163 notifyItem
->hIcon
= (HICON
)CopyImage(iconData
->hIcon
, IMAGE_ICON
, 0, 0, 0);
164 tbbi
.dwMask
|= TBIF_IMAGE
;
165 tbbi
.iImage
= ImageList_ReplaceIcon(m_ImageList
, index
, iconData
->hIcon
);
168 if (iconData
->uFlags
& NIF_TIP
)
170 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
173 if (iconData
->uFlags
& NIF_STATE
)
175 if (iconData
->dwStateMask
& NIS_HIDDEN
&&
176 (notifyItem
->dwState
& NIS_HIDDEN
) != (iconData
->dwState
& NIS_HIDDEN
))
178 tbbi
.dwMask
|= TBIF_STATE
;
179 if (iconData
->dwState
& NIS_HIDDEN
)
181 tbbi
.fsState
|= TBSTATE_HIDDEN
;
182 m_VisibleButtonCount
--;
186 tbbi
.fsState
&= ~TBSTATE_HIDDEN
;
187 m_VisibleButtonCount
++;
191 notifyItem
->dwState
&= ~iconData
->dwStateMask
;
192 notifyItem
->dwState
|= (iconData
->dwState
& iconData
->dwStateMask
);
195 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
197 SetButtonInfo(index
, &tbbi
);
202 BOOL
RemoveButton(IN CONST NOTIFYICONDATA
*iconData
)
204 NOTIFYICONDATA
* notifyItem
;
206 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
210 if (!(notifyItem
->dwState
& NIS_HIDDEN
))
212 m_VisibleButtonCount
--;
215 DestroyIcon(notifyItem
->hIcon
);
219 ImageList_Remove(m_ImageList
, index
);
221 int count
= GetButtonCount();
223 /* shift all buttons one index to the left -- starting one index right
224 from item to delete -- to preserve their correct icon and tip */
225 for (int i
= index
; i
< count
- 1; i
++)
227 notifyItem
= GetItemData(i
+ 1);
228 SetItemData(i
, notifyItem
);
229 UpdateButton(notifyItem
);
232 /* Delete the right-most, now obsolete button */
233 DeleteButton(count
- 1);
238 VOID
GetTooltipText(int index
, LPTSTR szTip
, DWORD cchTip
)
240 NOTIFYICONDATA
* notifyItem
;
241 notifyItem
= GetItemData(index
);
245 StringCchCopy(szTip
, cchTip
, notifyItem
->szTip
);
249 VOID
ResizeImagelist()
254 if (!ImageList_GetIconSize(m_ImageList
, &cx
, &cy
))
257 if (cx
== GetSystemMetrics(SM_CXSMICON
) && cy
== GetSystemMetrics(SM_CYSMICON
))
260 iml
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_COLOR32
| ILC_MASK
, 0, 1000);
264 ImageList_Destroy(m_ImageList
);
266 SetImageList(m_ImageList
);
268 int count
= GetButtonCount();
269 for (int i
= 0; i
< count
; i
++)
271 NOTIFYICONDATA
* data
= GetItemData(i
);
272 INT iIcon
= ImageList_AddIcon(iml
, data
->hIcon
);
273 TBBUTTONINFO tbbi
= { sizeof(tbbi
), TBIF_BYINDEX
| TBIF_IMAGE
, 0, iIcon
};
274 SetButtonInfo(i
, &tbbi
);
277 SetButtonSize(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
));
282 VOID
SendMouseEvent(IN WORD wIndex
, IN UINT uMsg
, IN WPARAM wParam
)
284 static LPCWSTR eventNames
[] = {
301 NOTIFYICONDATA
* notifyItem
= GetItemData(wIndex
);
303 if (!::IsWindow(notifyItem
->hWnd
))
305 // We detect and destroy icons with invalid handles only on mouse move over systray, same as MS does.
306 // Alternatively we could search for them periodically (would waste more resources).
307 TRACE("destroying icon with invalid handle\n");
309 HWND parentHWND
= GetParent();
310 parentHWND
= ::GetParent(parentHWND
);
313 ::GetClientRect(parentHWND
, &windowRect
);
315 RemoveButton(notifyItem
);
317 SendMessage(parentHWND
,
320 MAKELONG(windowRect
.right
- windowRect
.left
,
321 windowRect
.bottom
- windowRect
.top
));
326 if (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
)
328 TRACE("Sending message %S from button %d to %p (msg=%x, w=%x, l=%x)...\n",
329 eventNames
[uMsg
- WM_MOUSEFIRST
], wIndex
,
330 notifyItem
->hWnd
, notifyItem
->uCallbackMessage
, notifyItem
->uID
, uMsg
);
334 GetWindowThreadProcessId(notifyItem
->hWnd
, &pid
);
336 if (pid
== GetCurrentProcessId() ||
337 (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
))
339 ::PostMessage(notifyItem
->hWnd
,
340 notifyItem
->uCallbackMessage
,
346 SendMessage(notifyItem
->hWnd
,
347 notifyItem
->uCallbackMessage
,
353 LRESULT
OnMouseEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
355 POINT pt
= { GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
) };
357 INT iBtn
= HitTest(&pt
);
361 SendMouseEvent(iBtn
, uMsg
, wParam
);
368 LRESULT
OnTooltipShow(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
371 ::GetWindowRect(hdr
->hwndFrom
, &rcTip
);
373 SIZE szTip
= { rcTip
.right
- rcTip
.left
, rcTip
.bottom
- rcTip
.top
};
375 INT iBtn
= GetHotItem();
379 MONITORINFO monInfo
= { 0 };
380 HMONITOR hMon
= MonitorFromWindow(m_hWnd
, MONITOR_DEFAULTTONEAREST
);
382 monInfo
.cbSize
= sizeof(monInfo
);
385 GetMonitorInfo(hMon
, &monInfo
);
387 ::GetWindowRect(GetDesktopWindow(), &monInfo
.rcMonitor
);
389 GetItemRect(iBtn
, &rcItem
);
391 POINT ptItem
= { rcItem
.left
, rcItem
.top
};
392 SIZE szItem
= { rcItem
.right
- rcItem
.left
, rcItem
.bottom
- rcItem
.top
};
393 ClientToScreen(&ptItem
);
395 ptItem
.x
+= szItem
.cx
/ 2;
396 ptItem
.y
-= szTip
.cy
;
398 if (ptItem
.x
+ szTip
.cx
> monInfo
.rcMonitor
.right
)
399 ptItem
.x
= monInfo
.rcMonitor
.right
- szTip
.cx
;
401 if (ptItem
.y
+ szTip
.cy
> monInfo
.rcMonitor
.bottom
)
402 ptItem
.y
= monInfo
.rcMonitor
.bottom
- szTip
.cy
;
404 if (ptItem
.x
< monInfo
.rcMonitor
.left
)
405 ptItem
.x
= monInfo
.rcMonitor
.left
;
407 if (ptItem
.y
< monInfo
.rcMonitor
.top
)
408 ptItem
.y
= monInfo
.rcMonitor
.top
;
410 TRACE("ptItem { %d, %d }\n", ptItem
.x
, ptItem
.y
);
412 ::SetWindowPos(hdr
->hwndFrom
, NULL
, ptItem
.x
, ptItem
.y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
423 BEGIN_MSG_MAP(CNotifyToolbar
)
424 MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST
, WM_MOUSELAST
, OnMouseEvent
)
425 NOTIFY_CODE_HANDLER(TTN_SHOW
, OnTooltipShow
)
428 void Initialize(HWND hWndParent
)
431 WS_CHILD
| WS_VISIBLE
| WS_CLIPCHILDREN
|
432 TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
| TBSTYLE_WRAPABLE
| TBSTYLE_TRANSPARENT
|
433 CCS_TOP
| CCS_NORESIZE
| CCS_NOPARENTALIGN
| CCS_NODIVIDER
;
435 SubclassWindow(CToolbar::Create(hWndParent
, styles
));
437 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
439 m_ImageList
= ImageList_Create(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), ILC_COLOR32
| ILC_MASK
, 0, 1000);
440 SetImageList(m_ImageList
);
442 TBMETRICS tbm
= {sizeof(tbm
)};
443 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
| TBMF_PAD
;
446 tbm
.cxButtonSpacing
= 1;
447 tbm
.cyButtonSpacing
= 1;
450 SetButtonSize(GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
));
455 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
456 public CWindowImpl
< CSysPagerWnd
, CWindow
, CControlWinTraits
>
458 CNotifyToolbar Toolbar
;
462 virtual ~CSysPagerWnd() {}
464 LRESULT
DrawBackground(HDC hdc
)
468 GetClientRect(&rect
);
469 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
474 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
476 HDC hdc
= (HDC
) wParam
;
484 return DrawBackground(hdc
);
487 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
489 Toolbar
.Initialize(m_hWnd
);
491 // Explicitly request running applications to re-register their systray icons
492 ::SendNotifyMessageW(HWND_BROADCAST
,
493 RegisterWindowMessageW(L
"TaskbarCreated"),
499 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
501 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
502 if (cpData
->dwData
== 1)
504 SYS_PAGER_COPY_DATA
* data
;
505 NOTIFYICONDATA
*iconData
;
509 parentHWND
= GetParent();
510 parentHWND
= ::GetParent(parentHWND
);
511 ::GetClientRect(parentHWND
, &windowRect
);
513 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
514 iconData
= &data
->nicon_data
;
516 TRACE("NotifyIconCmd received. Code=%d\n", data
->notify_code
);
517 switch (data
->notify_code
)
520 ret
= Toolbar
.AddButton(iconData
);
523 ret
= Toolbar
.UpdateButton(iconData
);
526 ret
= Toolbar
.RemoveButton(iconData
);
529 TRACE("NotifyIconCmd received with unknown code %d.\n", data
->notify_code
);
533 SendMessage(parentHWND
,
536 MAKELONG(windowRect
.right
- windowRect
.left
,
537 windowRect
.bottom
- windowRect
.top
));
545 void GetSize(IN BOOL IsHorizontal
, IN PSIZE size
)
547 /* Get the ideal height or width */
549 /* Unfortunately this doens't work correctly in ros */
550 Toolbar
.GetIdealSize(!IsHorizontal
, size
);
552 /* Make the reference dimension an exact multiple of the icon size */
554 size
->cy
-= size
->cy
% GetSystemMetrics(SM_CYSMICON
);
556 size
->cx
-= size
->cx
% GetSystemMetrics(SM_CXSMICON
);
561 INT cyButton
= GetSystemMetrics(SM_CYSMICON
) + 2;
562 INT cxButton
= GetSystemMetrics(SM_CXSMICON
) + 2;
563 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
567 rows
= max(size
->cy
/ cyButton
, 1);
568 columns
= max((VisibleButtonCount
+ rows
) / rows
, 1);
572 columns
= max(size
->cx
/ cxButton
, 1);
573 rows
= max((VisibleButtonCount
+ columns
) / columns
, 1);
575 size
->cx
= columns
* cxButton
;
576 size
->cy
= rows
* cyButton
;
580 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
582 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
583 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
587 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
589 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
590 switch (cdraw
->dwDrawStage
)
593 return CDRF_NOTIFYITEMDRAW
;
595 case CDDS_ITEMPREPAINT
:
596 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
601 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
605 szClient
.cx
= LOWORD(lParam
);
606 szClient
.cy
= HIWORD(lParam
);
608 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
612 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
616 Toolbar
.GetClientRect(&rc
);
618 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
620 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
621 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
623 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
628 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
634 void ResizeImagelist()
636 Toolbar
.ResizeImagelist();
639 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
641 BEGIN_MSG_MAP(CSysPagerWnd
)
642 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
643 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
644 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
645 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
646 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
647 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
650 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
654 /* Create the window. The tray window is going to move it to the correct
655 position and resize it as needed. */
656 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
658 dwStyle
|= WS_VISIBLE
;
660 Create(hWndParent
, 0, NULL
, dwStyle
);
667 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
677 static const WCHAR szTrayClockWndClass
[] = L
"TrayClockWClass";
679 #define ID_TRAYCLOCK_TIMER 0
680 #define ID_TRAYCLOCK_TIMER_INIT 1
687 } ClockWndFormats
[] = {
689 { FALSE
, 0, L
"dddd" },
690 { FALSE
, DATE_SHORTDATE
, NULL
}
693 #define CLOCKWND_FORMAT_COUNT (_ARRAYSIZE(ClockWndFormats))
695 #define TRAY_CLOCK_WND_SPACING_X 0
696 #define TRAY_CLOCK_WND_SPACING_Y 0
698 class CTrayClockWnd
:
699 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
700 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
706 SYSTEMTIME LocalTime
;
713 DWORD IsTimerEnabled
: 1;
714 DWORD IsInitTimerEnabled
: 1;
715 DWORD LinesMeasured
: 1;
716 DWORD IsHorizontal
: 1;
722 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
723 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
733 ZeroMemory(&textColor
, sizeof(textColor
));
734 ZeroMemory(&rcText
, sizeof(rcText
));
735 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
736 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
737 ZeroMemory(LineSizes
, sizeof(LineSizes
));
738 ZeroMemory(szLines
, sizeof(szLines
));
740 virtual ~CTrayClockWnd() { }
742 LRESULT
OnThemeChanged()
748 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
752 GetThemeFont(clockTheme
,
759 hFont
= CreateFontIndirectW(&clockFont
);
761 GetThemeColor(clockTheme
,
767 if (this->hFont
!= NULL
)
768 DeleteObject(this->hFont
);
770 SetFont(hFont
, FALSE
);
774 /* We don't need to set a font here, our parent will use
775 * WM_SETFONT to set the right one when themes are not enabled. */
776 textColor
= RGB(0, 0, 0);
779 CloseThemeData(clockTheme
);
784 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
786 return OnThemeChanged();
800 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
802 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
804 if (szLines
[i
][0] != L
'\0' &&
805 !GetTextExtentPointW(hDC
, szLines
[i
], wcslen(szLines
[i
]),
814 SelectObject(hDC
, hPrevFont
);
822 /* calculate the line spacing */
823 for (i
= 0, c
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
825 if (LineSizes
[i
].cx
> 0)
827 LineSpacing
+= LineSizes
[i
].cy
;
834 /* We want a spacing of 1/2 line */
835 LineSpacing
= (LineSpacing
/ c
) / 2;
845 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
847 WORD iLinesVisible
= 0;
849 SIZE szMax
= { 0, 0 };
852 LinesMeasured
= MeasureLines();
857 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
859 if (LineSizes
[i
].cx
!= 0)
861 if (iLinesVisible
> 0)
865 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
866 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
873 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
877 /* Add line spacing */
878 szMax
.cy
+= LineSpacing
;
883 /* Increase maximum rectangle */
884 szMax
.cy
+= LineSizes
[i
].cy
;
885 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
886 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
890 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
891 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
895 return iLinesVisible
;
906 ZeroMemory(LineSizes
, sizeof(LineSizes
));
908 szPrevCurrent
= CurrentSize
;
910 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
912 szLines
[i
][0] = L
'\0';
913 BufSize
= _countof(szLines
[0]);
915 if (ClockWndFormats
[i
].IsTime
)
917 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
918 TaskBarSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
920 ClockWndFormats
[i
].lpFormat
,
926 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
927 ClockWndFormats
[i
].dwFormatFlags
,
929 ClockWndFormats
[i
].lpFormat
,
934 if (iRet
!= 0 && i
== 0)
936 /* Set the window text to the time only */
937 SetWindowText(szLines
[i
]);
941 LinesMeasured
= MeasureLines();
944 GetClientRect(&rcClient
))
948 szWnd
.cx
= rcClient
.right
;
949 szWnd
.cy
= rcClient
.bottom
;
951 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
955 if (IsWindowVisible())
957 InvalidateRect(NULL
, TRUE
);
959 if (hWndNotify
!= NULL
&&
960 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
961 szPrevCurrent
.cy
!= CurrentSize
.cy
))
965 nmh
.hwndFrom
= m_hWnd
;
966 nmh
.idFrom
= GetWindowLongPtr(GWLP_ID
);
967 nmh
.code
= NTNWM_REALIGN
;
969 SendMessage(hWndNotify
,
979 GetLocalTime(&LocalTime
);
983 UINT
CalculateDueTime()
987 /* Calculate the due time */
988 GetLocalTime(&LocalTime
);
989 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
990 if (TaskBarSettings
.bShowSeconds
)
991 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
993 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
995 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
999 /* Add an artificial delay of 0.05 seconds to make sure the timer
1000 doesn't fire too early*/
1012 /* Disable all timers */
1015 KillTimer(ID_TRAYCLOCK_TIMER
);
1016 IsTimerEnabled
= FALSE
;
1019 if (IsInitTimerEnabled
)
1021 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1024 uiDueTime
= CalculateDueTime();
1026 /* Set the new timer */
1027 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
1028 IsInitTimerEnabled
= Ret
;
1030 /* Update the time */
1036 VOID
CalibrateTimer()
1040 UINT uiWait1
, uiWait2
;
1042 /* Kill the initialization timer */
1043 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1044 IsInitTimerEnabled
= FALSE
;
1046 uiDueTime
= CalculateDueTime();
1048 if (TaskBarSettings
.bShowSeconds
)
1050 uiWait1
= 1000 - 200;
1055 uiWait1
= 60 * 1000 - 200;
1056 uiWait2
= 60 * 1000;
1059 if (uiDueTime
> uiWait1
)
1061 /* The update of the clock will be up to 200 ms late, but that's
1062 acceptable. We're going to setup a timer that fires depending
1064 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
1065 IsTimerEnabled
= Ret
;
1067 /* Update the time */
1072 /* Recalibrate the timer and recalculate again when the current
1073 minute/second ends. */
1078 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1080 /* Disable all timers */
1083 KillTimer(ID_TRAYCLOCK_TIMER
);
1086 if (IsInitTimerEnabled
)
1088 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1094 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1102 HDC hDC
= (HDC
) wParam
;
1106 hDC
= BeginPaint(&ps
);
1112 if (LinesMeasured
&&
1113 GetClientRect(&rcClient
))
1115 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1117 SetTextColor(hDC
, textColor
);
1119 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1121 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1122 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1123 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1124 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1126 for (i
= 0, line
= 0;
1127 i
< CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1130 if (LineSizes
[i
].cx
!= 0)
1133 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1134 TRAY_CLOCK_WND_SPACING_X
,
1135 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1137 wcslen(szLines
[i
]));
1139 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1144 SelectObject(hDC
, hPrevFont
);
1146 SetBkMode(hDC
, iPrevBkMode
);
1157 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1160 LinesMeasured
= MeasureLines();
1163 InvalidateRect(NULL
, TRUE
);
1167 LRESULT
DrawBackground(HDC hdc
)
1171 GetClientRect(&rect
);
1172 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1177 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1179 HDC hdc
= (HDC
) wParam
;
1187 return DrawBackground(hdc
);
1190 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1194 case ID_TRAYCLOCK_TIMER
:
1198 case ID_TRAYCLOCK_TIMER_INIT
:
1205 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1207 IsHorizontal
= (BOOL
) wParam
;
1209 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1212 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1214 return (LRESULT
) ResetTime();
1217 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1219 return HTTRANSPARENT
;
1222 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1224 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1228 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1234 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1238 szClient
.cx
= LOWORD(lParam
);
1239 szClient
.cy
= HIWORD(lParam
);
1241 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1242 CurrentSize
= szClient
;
1244 InvalidateRect(NULL
, TRUE
);
1248 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1250 BEGIN_MSG_MAP(CTrayClockWnd
)
1251 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1252 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1253 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1254 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1255 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1256 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1257 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1258 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1259 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1260 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1261 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1262 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1266 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1268 IsHorizontal
= TRUE
;
1270 hWndNotify
= hWndParent
;
1272 /* Create the window. The tray window is going to move it to the correct
1273 position and resize it as needed. */
1274 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1276 dwStyle
|= WS_VISIBLE
;
1278 Create(hWndParent
, 0, NULL
, dwStyle
);
1281 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1292 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1294 #define TRAY_NOTIFY_WND_SPACING_X 1
1295 #define TRAY_NOTIFY_WND_SPACING_Y 1
1297 class CTrayNotifyWnd
:
1298 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1299 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1303 CSysPagerWnd
* m_pager
;
1304 CTrayClockWnd
* m_clock
;
1306 CComPtr
<ITrayWindow
> TrayWindow
;
1309 SIZE szTrayClockMin
;
1311 MARGINS ContentMargin
;
1318 DWORD HideClock
: 1;
1319 DWORD IsHorizontal
: 1;
1332 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1333 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1334 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1336 virtual ~CTrayNotifyWnd() { }
1338 LRESULT
OnThemeChanged()
1341 CloseThemeData(TrayTheme
);
1343 if (IsThemeActive())
1344 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1350 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1352 GetThemeMargins(TrayTheme
,
1362 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1364 ContentMargin
.cxLeftWidth
= 0;
1365 ContentMargin
.cxRightWidth
= 0;
1366 ContentMargin
.cyTopHeight
= 0;
1367 ContentMargin
.cyBottomHeight
= 0;
1373 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1375 return OnThemeChanged();
1378 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1380 m_clock
= new CTrayClockWnd();
1381 m_clock
->_Init(m_hWnd
, !HideClock
);
1383 m_pager
= new CSysPagerWnd();
1384 m_pager
->_Init(m_hWnd
, !HideClock
);
1389 BOOL
GetMinimumSize(IN OUT PSIZE pSize
)
1391 SIZE szClock
= { 0, 0 };
1392 SIZE szTray
= { 0, 0 };
1398 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1399 if (szClock
.cy
<= 0)
1404 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1405 if (szClock
.cx
<= 0)
1409 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) IsHorizontal
, (LPARAM
) &szClock
);
1411 szTrayClockMin
= szClock
;
1415 szTrayClockMin
= szClock
;
1419 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1423 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1426 m_pager
->GetSize(IsHorizontal
, &szTray
);
1428 szTrayNotify
= szTray
;
1432 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1435 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1437 pSize
->cx
+= szTray
.cx
;
1441 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1444 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1446 pSize
->cy
+= szTray
.cy
;
1449 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1450 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1455 VOID
Size(IN
const SIZE
*pszClient
)
1464 ptClock
.x
= pszClient
->cx
- TRAY_NOTIFY_WND_SPACING_X
- szTrayClockMin
.cx
;
1465 ptClock
.y
= TRAY_NOTIFY_WND_SPACING_Y
;
1466 szClock
.cx
= szTrayClockMin
.cx
;
1467 szClock
.cy
= pszClient
->cy
- (2 * TRAY_NOTIFY_WND_SPACING_Y
);
1471 ptClock
.x
= TRAY_NOTIFY_WND_SPACING_X
;
1472 ptClock
.y
= pszClient
->cy
- TRAY_NOTIFY_WND_SPACING_Y
- szTrayClockMin
.cy
;
1473 szClock
.cx
= pszClient
->cx
- (2 * TRAY_NOTIFY_WND_SPACING_X
);
1474 szClock
.cy
= szTrayClockMin
.cy
;
1477 m_clock
->SetWindowPos(
1489 ptPager
.x
= ptClock
.x
- szTrayNotify
.cx
;
1490 ptPager
.y
= (pszClient
->cy
- szTrayNotify
.cy
)/2;
1494 ptPager
.x
= (pszClient
->cx
- szTrayNotify
.cx
)/2;
1495 ptPager
.y
= ptClock
.y
- szTrayNotify
.cy
;
1498 m_pager
->SetWindowPos(
1508 LRESULT
DrawBackground(HDC hdc
)
1513 GetClientRect(&rect
);
1517 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1519 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1522 res
= DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1528 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1530 HDC hdc
= (HDC
) wParam
;
1538 return DrawBackground(hdc
);
1541 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
1545 return m_pager
->NotifyIconCmd(wParam
, lParam
);
1551 BOOL
GetClockRect(OUT PRECT rcClock
)
1553 if (!m_clock
->IsWindowVisible())
1556 return m_clock
->GetWindowRect(rcClock
);
1559 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1561 BOOL Horizontal
= (BOOL
) wParam
;
1563 if (Horizontal
!= IsHorizontal
)
1565 IsHorizontal
= Horizontal
;
1567 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1569 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1572 return (LRESULT
) GetMinimumSize((PSIZE
) lParam
);
1575 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1577 if (m_clock
!= NULL
)
1579 /* Forward the message to the tray clock window procedure */
1580 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1585 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1589 szClient
.cx
= LOWORD(lParam
);
1590 szClient
.cy
= HIWORD(lParam
);
1597 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1599 return HTTRANSPARENT
;
1602 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1604 BOOL PrevHidden
= HideClock
;
1605 HideClock
= (wParam
== 0);
1607 if (m_clock
!= NULL
&& PrevHidden
!= HideClock
)
1609 m_clock
->ShowWindow(HideClock
? SW_HIDE
: SW_SHOW
);
1612 return (LRESULT
) (!PrevHidden
);
1615 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1617 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1619 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1621 /* Pass down notifications */
1622 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1628 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1630 if (m_clock
!= NULL
)
1632 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1639 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1645 LRESULT
OnSettingChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1647 if (wParam
== SPI_SETNONCLIENTMETRICS
)
1649 m_pager
->ResizeImagelist();
1654 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1656 BEGIN_MSG_MAP(CTrayNotifyWnd
)
1657 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1658 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1659 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1660 MESSAGE_HANDLER(WM_SETTINGCHANGE
, OnSettingChanged
)
1661 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1662 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1663 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
1664 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1665 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
1666 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1667 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
1668 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
1671 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
, IN BOOL bHideClock
)
1673 HWND hWndTrayWindow
;
1675 hWndTrayWindow
= TrayWindow
->GetHWND();
1676 if (hWndTrayWindow
== NULL
)
1679 this->TrayWindow
= TrayWindow
;
1680 this->HideClock
= bHideClock
;
1681 this->hWndNotify
= hWndTrayWindow
;
1683 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
1684 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
1688 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, BOOL bHideClock
, CTrayNotifyWnd
** ppinstance
)
1690 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
1691 // TODO: Destroy after the window is destroyed
1692 *ppinstance
= pTrayNotify
;
1694 return pTrayNotify
->_Init(Tray
, bHideClock
);
1698 TrayNotify_NotifyIconCmd(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
1700 return pTrayNotify
->NotifyIconCmd(wParam
, lParam
);
1704 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
1706 return pTrayNotify
->GetClockRect(rcClock
);