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
23 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
24 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
29 static const WCHAR szSysPagerWndClass
[] = L
"SysPager";
31 // Data comes from shell32/systray.cpp -> TrayNotifyCDS_Dummy
32 typedef struct _SYS_PAGER_COPY_DATA
36 NOTIFYICONDATA nicon_data
;
37 } SYS_PAGER_COPY_DATA
, *PSYS_PAGER_COPY_DATA
;
39 class CNotifyToolbar
:
40 public CWindowImplBaseT
< CToolbar
<NOTIFYICONDATA
>, CControlWinTraits
>
42 static const int ICON_SIZE
= 16;
44 HIMAGELIST m_ImageList
;
45 int m_VisibleButtonCount
;
50 m_VisibleButtonCount(0)
58 int GetVisibleButtonCount()
60 return m_VisibleButtonCount
;
63 int FindItemByIconData(IN CONST NOTIFYICONDATA
*iconData
, NOTIFYICONDATA
** pdata
)
65 int count
= GetButtonCount();
67 for (int i
= 0; i
< count
; i
++)
69 NOTIFYICONDATA
* data
;
71 data
= GetItemData(i
);
73 if (data
->hWnd
== iconData
->hWnd
&&
74 data
->uID
== iconData
->uID
)
85 BOOL
AddButton(IN CONST NOTIFYICONDATA
*iconData
)
88 NOTIFYICONDATA
* notifyItem
;
91 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
94 return UpdateButton(iconData
);
97 notifyItem
= new NOTIFYICONDATA();
98 ZeroMemory(notifyItem
, sizeof(*notifyItem
));
100 notifyItem
->hWnd
= iconData
->hWnd
;
101 notifyItem
->uID
= iconData
->uID
;
103 tbBtn
.fsState
= TBSTATE_ENABLED
;
104 tbBtn
.fsStyle
= BTNS_NOPREFIX
;
105 tbBtn
.dwData
= (DWORD_PTR
)notifyItem
;
106 tbBtn
.iString
= (INT_PTR
) text
;
107 tbBtn
.idCommand
= GetButtonCount();
109 if (iconData
->uFlags
& NIF_MESSAGE
)
111 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
114 if (iconData
->uFlags
& NIF_ICON
)
116 tbBtn
.iBitmap
= ImageList_AddIcon(m_ImageList
, iconData
->hIcon
);
119 if (iconData
->uFlags
& NIF_TIP
)
121 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
124 m_VisibleButtonCount
++;
125 if (iconData
->uFlags
& NIF_STATE
)
127 notifyItem
->dwState
&= ~iconData
->dwStateMask
;
128 notifyItem
->dwState
|= (iconData
->dwState
& iconData
->dwStateMask
);
129 if (notifyItem
->dwState
& NIS_HIDDEN
)
131 tbBtn
.fsState
|= TBSTATE_HIDDEN
;
132 m_VisibleButtonCount
--;
136 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
138 CToolbar::AddButton(&tbBtn
);
139 SetButtonSize(ICON_SIZE
, ICON_SIZE
);
144 BOOL
UpdateButton(IN CONST NOTIFYICONDATA
*iconData
)
146 NOTIFYICONDATA
* notifyItem
;
147 TBBUTTONINFO tbbi
= { 0 };
149 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
152 return AddButton(iconData
);
155 tbbi
.cbSize
= sizeof(tbbi
);
156 tbbi
.dwMask
= TBIF_BYINDEX
| TBIF_COMMAND
;
157 tbbi
.idCommand
= index
;
159 if (iconData
->uFlags
& NIF_MESSAGE
)
161 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
164 if (iconData
->uFlags
& NIF_ICON
)
166 tbbi
.dwMask
|= TBIF_IMAGE
;
167 tbbi
.iImage
= ImageList_ReplaceIcon(m_ImageList
, index
, iconData
->hIcon
);
170 if (iconData
->uFlags
& NIF_TIP
)
172 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
175 if (iconData
->uFlags
& NIF_STATE
)
177 if (iconData
->dwStateMask
& NIS_HIDDEN
&&
178 (notifyItem
->dwState
& NIS_HIDDEN
) != (iconData
->dwState
& NIS_HIDDEN
))
180 tbbi
.dwMask
|= TBIF_STATE
;
181 if (iconData
->dwState
& NIS_HIDDEN
)
183 tbbi
.fsState
|= TBSTATE_HIDDEN
;
184 m_VisibleButtonCount
--;
188 tbbi
.fsState
&= ~TBSTATE_HIDDEN
;
189 m_VisibleButtonCount
++;
193 notifyItem
->dwState
&= ~iconData
->dwStateMask
;
194 notifyItem
->dwState
|= (iconData
->dwState
& iconData
->dwStateMask
);
197 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
199 SetButtonInfo(index
, &tbbi
);
204 BOOL
RemoveButton(IN CONST NOTIFYICONDATA
*iconData
)
206 NOTIFYICONDATA
* notifyItem
;
208 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
212 if (!(notifyItem
->dwState
& NIS_HIDDEN
))
214 m_VisibleButtonCount
--;
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
);
251 VOID
SendMouseEvent(IN WORD wIndex
, IN UINT uMsg
, IN WPARAM wParam
)
253 static LPCWSTR eventNames
[] = {
270 NOTIFYICONDATA
* notifyItem
= GetItemData(wIndex
);
272 if (!::IsWindow(notifyItem
->hWnd
))
274 // We detect and destroy icons with invalid handles only on mouse move over systray, same as MS does.
275 // Alternatively we could search for them periodically (would waste more resources).
276 TRACE("destroying icon with invalid handle\n");
278 HWND parentHWND
= GetParent();
279 parentHWND
= ::GetParent(parentHWND
);
282 ::GetClientRect(parentHWND
, &windowRect
);
284 RemoveButton(notifyItem
);
286 SendMessage(parentHWND
,
289 MAKELONG(windowRect
.right
- windowRect
.left
,
290 windowRect
.bottom
- windowRect
.top
));
295 if (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
)
297 TRACE("Sending message %S from button %d to %p (msg=%x, w=%x, l=%x)...\n",
298 eventNames
[uMsg
- WM_MOUSEFIRST
], wIndex
,
299 notifyItem
->hWnd
, notifyItem
->uCallbackMessage
, notifyItem
->uID
, uMsg
);
303 GetWindowThreadProcessId(notifyItem
->hWnd
, &pid
);
305 if (pid
== GetCurrentProcessId() ||
306 (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
))
308 ::PostMessage(notifyItem
->hWnd
,
309 notifyItem
->uCallbackMessage
,
315 SendMessage(notifyItem
->hWnd
,
316 notifyItem
->uCallbackMessage
,
322 LRESULT
OnMouseEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
324 POINT pt
= { GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
) };
326 INT iBtn
= HitTest(&pt
);
330 SendMouseEvent(iBtn
, uMsg
, wParam
);
337 LRESULT
OnTooltipShow(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
340 ::GetWindowRect(hdr
->hwndFrom
, &rcTip
);
342 SIZE szTip
= { rcTip
.right
- rcTip
.left
, rcTip
.bottom
- rcTip
.top
};
344 INT iBtn
= GetHotItem();
348 MONITORINFO monInfo
= { 0 };
349 HMONITOR hMon
= MonitorFromWindow(m_hWnd
, MONITOR_DEFAULTTONEAREST
);
351 monInfo
.cbSize
= sizeof(monInfo
);
354 GetMonitorInfo(hMon
, &monInfo
);
356 ::GetWindowRect(GetDesktopWindow(), &monInfo
.rcMonitor
);
358 GetItemRect(iBtn
, &rcItem
);
360 POINT ptItem
= { rcItem
.left
, rcItem
.top
};
361 SIZE szItem
= { rcItem
.right
- rcItem
.left
, rcItem
.bottom
- rcItem
.top
};
362 ClientToScreen(&ptItem
);
364 ptItem
.x
+= szItem
.cx
/ 2;
365 ptItem
.y
-= szTip
.cy
;
367 if (ptItem
.x
+ szTip
.cx
> monInfo
.rcMonitor
.right
)
368 ptItem
.x
= monInfo
.rcMonitor
.right
- szTip
.cx
;
370 if (ptItem
.y
+ szTip
.cy
> monInfo
.rcMonitor
.bottom
)
371 ptItem
.y
= monInfo
.rcMonitor
.bottom
- szTip
.cy
;
373 if (ptItem
.x
< monInfo
.rcMonitor
.left
)
374 ptItem
.x
= monInfo
.rcMonitor
.left
;
376 if (ptItem
.y
< monInfo
.rcMonitor
.top
)
377 ptItem
.y
= monInfo
.rcMonitor
.top
;
379 TRACE("ptItem { %d, %d }\n", ptItem
.x
, ptItem
.y
);
381 ::SetWindowPos(hdr
->hwndFrom
, NULL
, ptItem
.x
, ptItem
.y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
392 BEGIN_MSG_MAP(CNotifyToolbar
)
393 MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST
, WM_MOUSELAST
, OnMouseEvent
)
394 NOTIFY_CODE_HANDLER(TTN_SHOW
, OnTooltipShow
)
397 void Initialize(HWND hWndParent
)
400 WS_CHILD
| WS_VISIBLE
| WS_CLIPCHILDREN
|
401 TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
| TBSTYLE_WRAPABLE
| TBSTYLE_TRANSPARENT
|
402 CCS_TOP
| CCS_NORESIZE
| CCS_NOPARENTALIGN
| CCS_NODIVIDER
;
404 SubclassWindow(CToolbar::Create(hWndParent
, styles
));
406 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
408 m_ImageList
= ImageList_Create(16, 16, ILC_COLOR32
| ILC_MASK
, 0, 1000);
409 SetImageList(m_ImageList
);
411 SetButtonSize(ICON_SIZE
, ICON_SIZE
);
416 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
417 public CWindowImpl
< CSysPagerWnd
, CWindow
, CControlWinTraits
>
419 CNotifyToolbar Toolbar
;
423 virtual ~CSysPagerWnd() {}
425 LRESULT
DrawBackground(HDC hdc
)
429 GetClientRect(&rect
);
430 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
435 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
437 HDC hdc
= (HDC
) wParam
;
445 return DrawBackground(hdc
);
448 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
450 Toolbar
.Initialize(m_hWnd
);
452 // Explicitly request running applications to re-register their systray icons
453 ::SendNotifyMessageW(HWND_BROADCAST
,
454 RegisterWindowMessageW(L
"TaskbarCreated"),
460 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
462 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
463 if (cpData
->dwData
== 1)
465 SYS_PAGER_COPY_DATA
* data
;
466 NOTIFYICONDATA
*iconData
;
470 parentHWND
= GetParent();
471 parentHWND
= ::GetParent(parentHWND
);
472 ::GetClientRect(parentHWND
, &windowRect
);
474 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
475 iconData
= &data
->nicon_data
;
477 TRACE("NotifyIconCmd received. Code=%d\n", data
->notify_code
);
478 switch (data
->notify_code
)
481 ret
= Toolbar
.AddButton(iconData
);
484 ret
= Toolbar
.UpdateButton(iconData
);
487 ret
= Toolbar
.RemoveButton(iconData
);
490 TRACE("NotifyIconCmd received with unknown code %d.\n", data
->notify_code
);
494 SendMessage(parentHWND
,
497 MAKELONG(windowRect
.right
- windowRect
.left
,
498 windowRect
.bottom
- windowRect
.top
));
506 void GetSize(IN WPARAM wParam
, IN PSIZE size
)
509 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
511 if (wParam
) /* horizontal */
513 rows
= size
->cy
/ 24;
516 size
->cx
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
520 rows
= size
->cx
/ 24;
523 size
->cy
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
527 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
529 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
530 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
534 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
536 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
537 switch (cdraw
->dwDrawStage
)
540 return CDRF_NOTIFYITEMDRAW
;
542 case CDDS_ITEMPREPAINT
:
543 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
548 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
552 szClient
.cx
= LOWORD(lParam
);
553 szClient
.cy
= HIWORD(lParam
);
555 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
560 tbm
.cbSize
= sizeof(tbm
);
561 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
;
562 tbm
.cxBarPad
= tbm
.cyBarPad
= 0;
563 tbm
.cxButtonSpacing
= 0;
564 tbm
.cyButtonSpacing
= 0;
566 Toolbar
.SetMetrics(&tbm
);
568 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
572 Toolbar
.GetClientRect(&rc
);
574 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
576 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
577 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
579 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
584 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
590 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
592 BEGIN_MSG_MAP(CTaskSwitchWnd
)
593 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
594 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
595 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
596 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
597 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
598 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
601 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
605 /* Create the window. The tray window is going to move it to the correct
606 position and resize it as needed. */
607 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
609 dwStyle
|= WS_VISIBLE
;
611 Create(hWndParent
, 0, NULL
, dwStyle
);
618 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
628 static const WCHAR szTrayClockWndClass
[] = L
"TrayClockWClass";
630 #define ID_TRAYCLOCK_TIMER 0
631 #define ID_TRAYCLOCK_TIMER_INIT 1
638 } ClockWndFormats
[] = {
640 { FALSE
, 0, L
"dddd" },
641 { FALSE
, DATE_SHORTDATE
, NULL
}
644 #define CLOCKWND_FORMAT_COUNT (_ARRAYSIZE(ClockWndFormats))
646 #define TRAY_CLOCK_WND_SPACING_X 0
647 #define TRAY_CLOCK_WND_SPACING_Y 0
649 class CTrayClockWnd
:
650 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
651 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
657 SYSTEMTIME LocalTime
;
664 DWORD IsTimerEnabled
: 1;
665 DWORD IsInitTimerEnabled
: 1;
666 DWORD LinesMeasured
: 1;
667 DWORD IsHorizontal
: 1;
673 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
674 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
684 ZeroMemory(&textColor
, sizeof(textColor
));
685 ZeroMemory(&rcText
, sizeof(rcText
));
686 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
687 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
688 ZeroMemory(LineSizes
, sizeof(LineSizes
));
689 ZeroMemory(szLines
, sizeof(szLines
));
691 virtual ~CTrayClockWnd() { }
693 LRESULT
OnThemeChanged()
699 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
703 GetThemeFont(clockTheme
,
710 hFont
= CreateFontIndirectW(&clockFont
);
712 GetThemeColor(clockTheme
,
718 if (this->hFont
!= NULL
)
719 DeleteObject(this->hFont
);
721 SetFont(hFont
, FALSE
);
725 /* We don't need to set a font here, our parent will use
726 * WM_SETFONT to set the right one when themes are not enabled. */
727 textColor
= RGB(0, 0, 0);
730 CloseThemeData(clockTheme
);
735 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
737 return OnThemeChanged();
751 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
753 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
755 if (szLines
[i
][0] != L
'\0' &&
756 !GetTextExtentPointW(hDC
, szLines
[i
], wcslen(szLines
[i
]),
765 SelectObject(hDC
, hPrevFont
);
773 /* calculate the line spacing */
774 for (i
= 0, c
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
776 if (LineSizes
[i
].cx
> 0)
778 LineSpacing
+= LineSizes
[i
].cy
;
785 /* We want a spacing of 1/2 line */
786 LineSpacing
= (LineSpacing
/ c
) / 2;
796 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
798 WORD iLinesVisible
= 0;
800 SIZE szMax
= { 0, 0 };
803 LinesMeasured
= MeasureLines();
808 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
810 if (LineSizes
[i
].cx
!= 0)
812 if (iLinesVisible
> 0)
816 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
817 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
824 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
828 /* Add line spacing */
829 szMax
.cy
+= LineSpacing
;
834 /* Increase maximum rectangle */
835 szMax
.cy
+= LineSizes
[i
].cy
;
836 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
837 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
841 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
842 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
846 return iLinesVisible
;
857 ZeroMemory(LineSizes
, sizeof(LineSizes
));
859 szPrevCurrent
= CurrentSize
;
861 for (i
= 0; i
< CLOCKWND_FORMAT_COUNT
; i
++)
863 szLines
[i
][0] = L
'\0';
864 BufSize
= _countof(szLines
[0]);
866 if (ClockWndFormats
[i
].IsTime
)
868 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
869 TaskBarSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
871 ClockWndFormats
[i
].lpFormat
,
877 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
878 ClockWndFormats
[i
].dwFormatFlags
,
880 ClockWndFormats
[i
].lpFormat
,
885 if (iRet
!= 0 && i
== 0)
887 /* Set the window text to the time only */
888 SetWindowText(szLines
[i
]);
892 LinesMeasured
= MeasureLines();
895 GetClientRect(&rcClient
))
899 szWnd
.cx
= rcClient
.right
;
900 szWnd
.cy
= rcClient
.bottom
;
902 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
906 if (IsWindowVisible())
908 InvalidateRect(NULL
, TRUE
);
910 if (hWndNotify
!= NULL
&&
911 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
912 szPrevCurrent
.cy
!= CurrentSize
.cy
))
916 nmh
.hwndFrom
= m_hWnd
;
917 nmh
.idFrom
= GetWindowLongPtr(GWLP_ID
);
918 nmh
.code
= NTNWM_REALIGN
;
920 SendMessage(hWndNotify
,
930 GetLocalTime(&LocalTime
);
934 UINT
CalculateDueTime()
938 /* Calculate the due time */
939 GetLocalTime(&LocalTime
);
940 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
941 if (TaskBarSettings
.bShowSeconds
)
942 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
944 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
946 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
950 /* Add an artificial delay of 0.05 seconds to make sure the timer
951 doesn't fire too early*/
963 /* Disable all timers */
966 KillTimer(ID_TRAYCLOCK_TIMER
);
967 IsTimerEnabled
= FALSE
;
970 if (IsInitTimerEnabled
)
972 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
975 uiDueTime
= CalculateDueTime();
977 /* Set the new timer */
978 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
979 IsInitTimerEnabled
= Ret
;
981 /* Update the time */
987 VOID
CalibrateTimer()
991 UINT uiWait1
, uiWait2
;
993 /* Kill the initialization timer */
994 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
995 IsInitTimerEnabled
= FALSE
;
997 uiDueTime
= CalculateDueTime();
999 if (TaskBarSettings
.bShowSeconds
)
1001 uiWait1
= 1000 - 200;
1006 uiWait1
= 60 * 1000 - 200;
1007 uiWait2
= 60 * 1000;
1010 if (uiDueTime
> uiWait1
)
1012 /* The update of the clock will be up to 200 ms late, but that's
1013 acceptable. We're going to setup a timer that fires depending
1015 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
1016 IsTimerEnabled
= Ret
;
1018 /* Update the time */
1023 /* Recalibrate the timer and recalculate again when the current
1024 minute/second ends. */
1029 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1031 /* Disable all timers */
1034 KillTimer(ID_TRAYCLOCK_TIMER
);
1037 if (IsInitTimerEnabled
)
1039 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1045 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1053 HDC hDC
= (HDC
) wParam
;
1057 hDC
= BeginPaint(&ps
);
1063 if (LinesMeasured
&&
1064 GetClientRect(&rcClient
))
1066 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1068 SetTextColor(hDC
, textColor
);
1070 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1072 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1073 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1074 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1075 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1077 for (i
= 0, line
= 0;
1078 i
< CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1081 if (LineSizes
[i
].cx
!= 0)
1084 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1085 TRAY_CLOCK_WND_SPACING_X
,
1086 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1088 wcslen(szLines
[i
]));
1090 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1095 SelectObject(hDC
, hPrevFont
);
1097 SetBkMode(hDC
, iPrevBkMode
);
1108 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1111 LinesMeasured
= MeasureLines();
1114 InvalidateRect(NULL
, TRUE
);
1118 LRESULT
DrawBackground(HDC hdc
)
1122 GetClientRect(&rect
);
1123 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1128 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1130 HDC hdc
= (HDC
) wParam
;
1138 return DrawBackground(hdc
);
1141 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1145 case ID_TRAYCLOCK_TIMER
:
1149 case ID_TRAYCLOCK_TIMER_INIT
:
1156 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1158 IsHorizontal
= (BOOL
) wParam
;
1160 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1163 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1165 return (LRESULT
) ResetTime();
1168 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1170 return HTTRANSPARENT
;
1173 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1175 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1179 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1185 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1189 szClient
.cx
= LOWORD(lParam
);
1190 szClient
.cy
= HIWORD(lParam
);
1192 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1193 CurrentSize
= szClient
;
1195 InvalidateRect(NULL
, TRUE
);
1199 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1201 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1202 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1203 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1204 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1205 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1206 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1207 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1208 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1209 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1210 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1211 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1212 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1213 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1217 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1219 IsHorizontal
= TRUE
;
1221 hWndNotify
= hWndParent
;
1223 /* Create the window. The tray window is going to move it to the correct
1224 position and resize it as needed. */
1225 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1227 dwStyle
|= WS_VISIBLE
;
1229 Create(hWndParent
, 0, NULL
, dwStyle
);
1232 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1243 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1245 #define TRAY_NOTIFY_WND_SPACING_X 2
1246 #define TRAY_NOTIFY_WND_SPACING_Y 2
1248 class CTrayNotifyWnd
:
1249 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1250 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1254 CSysPagerWnd
* m_pager
;
1255 CTrayClockWnd
* m_clock
;
1257 CComPtr
<ITrayWindow
> TrayWindow
;
1260 SIZE szTrayClockMin
;
1262 MARGINS ContentMargin
;
1269 DWORD HideClock
: 1;
1270 DWORD IsHorizontal
: 1;
1283 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1284 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1285 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1287 virtual ~CTrayNotifyWnd() { }
1289 LRESULT
OnThemeChanged()
1292 CloseThemeData(TrayTheme
);
1294 if (IsThemeActive())
1295 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1301 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1303 GetThemeMargins(TrayTheme
,
1313 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1315 ContentMargin
.cxLeftWidth
= 0;
1316 ContentMargin
.cxRightWidth
= 0;
1317 ContentMargin
.cyTopHeight
= 0;
1318 ContentMargin
.cyBottomHeight
= 0;
1324 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1326 return OnThemeChanged();
1329 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1331 m_clock
= new CTrayClockWnd();
1332 m_clock
->_Init(m_hWnd
, !HideClock
);
1334 m_pager
= new CSysPagerWnd();
1335 m_pager
->_Init(m_hWnd
, !HideClock
);
1342 BOOL
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
1344 SIZE szClock
= { 0, 0 };
1345 SIZE szTray
= { 0, 0 };
1347 IsHorizontal
= Horizontal
;
1349 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1351 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1357 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1358 if (szClock
.cy
<= 0)
1363 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1364 if (szClock
.cx
<= 0)
1368 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) Horizontal
, (LPARAM
) &szClock
);
1370 szTrayClockMin
= szClock
;
1374 szTrayClockMin
= szClock
;
1378 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1382 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1385 m_pager
->GetSize(Horizontal
, &szTray
);
1387 szTrayNotify
= szTray
;
1391 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1394 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1396 pSize
->cx
+= szTray
.cx
;
1400 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1403 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1405 pSize
->cy
+= szTray
.cy
;
1408 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1409 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1414 VOID
Size(IN
const SIZE
*pszClient
)
1423 ptClock
.x
= pszClient
->cx
- TRAY_NOTIFY_WND_SPACING_X
- szTrayClockMin
.cx
;
1424 ptClock
.y
= TRAY_NOTIFY_WND_SPACING_Y
;
1425 szClock
.cx
= szTrayClockMin
.cx
;
1426 szClock
.cy
= pszClient
->cy
- (2 * TRAY_NOTIFY_WND_SPACING_Y
);
1430 ptClock
.x
= TRAY_NOTIFY_WND_SPACING_X
;
1431 ptClock
.y
= pszClient
->cy
- TRAY_NOTIFY_WND_SPACING_Y
- szTrayClockMin
.cy
;
1432 szClock
.cx
= pszClient
->cx
- (2 * TRAY_NOTIFY_WND_SPACING_X
);
1433 szClock
.cy
= szTrayClockMin
.cy
;
1436 m_clock
->SetWindowPos(
1446 ptClock
.x
-= szTrayNotify
.cx
;
1450 ptClock
.y
-= szTrayNotify
.cy
;
1453 m_pager
->SetWindowPos(
1463 LRESULT
DrawBackground(HDC hdc
)
1468 GetClientRect(&rect
);
1472 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1474 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1477 res
= DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1483 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1485 HDC hdc
= (HDC
) wParam
;
1493 return DrawBackground(hdc
);
1496 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
1500 return m_pager
->NotifyIconCmd(wParam
, lParam
);
1506 BOOL
GetClockRect(OUT PRECT rcClock
)
1508 if (!m_clock
->IsWindowVisible())
1511 return m_clock
->GetWindowRect(rcClock
);
1514 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1516 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
);
1519 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1521 if (m_clock
!= NULL
)
1523 /* Forward the message to the tray clock window procedure */
1524 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1529 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1533 szClient
.cx
= LOWORD(lParam
);
1534 szClient
.cy
= HIWORD(lParam
);
1541 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1543 return HTTRANSPARENT
;
1546 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1548 BOOL PrevHidden
= HideClock
;
1549 HideClock
= (wParam
== 0);
1551 if (m_clock
!= NULL
&& PrevHidden
!= HideClock
)
1553 m_clock
->ShowWindow(HideClock
? SW_HIDE
: SW_SHOW
);
1556 return (LRESULT
) (!PrevHidden
);
1559 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1561 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1563 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1565 /* Pass down notifications */
1566 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1572 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1574 if (m_clock
!= NULL
)
1576 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1583 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1589 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1591 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1592 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1593 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1594 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1595 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1596 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1597 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
1598 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1599 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
1600 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1601 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
1602 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
1605 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
, IN BOOL bHideClock
)
1607 HWND hWndTrayWindow
;
1609 hWndTrayWindow
= TrayWindow
->GetHWND();
1610 if (hWndTrayWindow
== NULL
)
1613 this->TrayWindow
= TrayWindow
;
1614 this->HideClock
= bHideClock
;
1615 this->hWndNotify
= hWndTrayWindow
;
1617 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
1618 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
1622 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, BOOL bHideClock
, CTrayNotifyWnd
** ppinstance
)
1624 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
1625 // TODO: Destroy after the window is destroyed
1626 *ppinstance
= pTrayNotify
;
1628 return pTrayNotify
->_Init(Tray
, bHideClock
);
1632 TrayNotify_NotifyIconCmd(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
1634 return pTrayNotify
->NotifyIconCmd(wParam
, lParam
);
1638 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
1640 return pTrayNotify
->GetClockRect(rcClock
);