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
[] = TEXT("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 CToolbar
<NOTIFYICONDATA
>
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 VOID
AddButton(IN CONST NOTIFYICONDATA
*iconData
)
88 NOTIFYICONDATA
* notifyItem
;
89 WCHAR text
[] = TEXT("");
91 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
94 UpdateButton(iconData
);
98 notifyItem
= new NOTIFYICONDATA();
99 ZeroMemory(notifyItem
, sizeof(*notifyItem
));
101 notifyItem
->hWnd
= iconData
->hWnd
;
102 notifyItem
->uID
= iconData
->uID
;
104 tbBtn
.fsState
= TBSTATE_ENABLED
;
105 tbBtn
.fsStyle
= BTNS_NOPREFIX
;
106 tbBtn
.dwData
= (DWORD_PTR
)notifyItem
;
107 tbBtn
.iString
= (INT_PTR
) text
;
108 tbBtn
.idCommand
= GetButtonCount();
110 if (iconData
->uFlags
& NIF_MESSAGE
)
112 notifyItem
->uCallbackMessage
= iconData
->uCallbackMessage
;
115 if (iconData
->uFlags
& NIF_ICON
)
117 tbBtn
.iBitmap
= ImageList_AddIcon(m_ImageList
, iconData
->hIcon
);
120 if (iconData
->uFlags
& NIF_TIP
)
122 StringCchCopy(notifyItem
->szTip
, _countof(notifyItem
->szTip
), iconData
->szTip
);
125 m_VisibleButtonCount
++;
126 if (iconData
->uFlags
& NIF_STATE
)
128 notifyItem
->dwState
&= ~iconData
->dwStateMask
;
129 notifyItem
->dwState
|= (iconData
->dwState
& iconData
->dwStateMask
);
130 if (notifyItem
->dwState
& NIS_HIDDEN
)
132 tbBtn
.fsState
|= TBSTATE_HIDDEN
;
133 m_VisibleButtonCount
--;
137 /* TODO: support NIF_INFO, NIF_GUID, NIF_REALTIME, NIF_SHOWTIP */
139 CToolbar::AddButton(&tbBtn
);
140 SetButtonSize(ICON_SIZE
, ICON_SIZE
);
143 VOID
UpdateButton(IN CONST NOTIFYICONDATA
*iconData
)
145 NOTIFYICONDATA
* notifyItem
;
146 TBBUTTONINFO tbbi
= { 0 };
148 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
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_AddIcon(m_ImageList
, 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
);
202 VOID
RemoveButton(IN CONST NOTIFYICONDATA
*iconData
)
204 NOTIFYICONDATA
* notifyItem
;
206 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
212 if (!(notifyItem
->dwState
& NIS_HIDDEN
))
214 m_VisibleButtonCount
--;
220 VOID
GetTooltipText(int index
, LPTSTR szTip
, DWORD cchTip
)
222 NOTIFYICONDATA
* notifyItem
;
223 notifyItem
= GetItemData(index
);
227 StringCchCopy(szTip
, cchTip
, notifyItem
->szTip
);
233 VOID
SendMouseEvent(IN WORD wIndex
, IN UINT uMsg
, IN WPARAM wParam
)
235 static LPCWSTR eventNames
[] = {
252 NOTIFYICONDATA
* notifyItem
= GetItemData(wIndex
);
254 if (!::IsWindow(notifyItem
->hWnd
))
257 if (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
)
259 TRACE("Sending message %S from button %d to %p (msg=%x, w=%x, l=%x)...\n",
260 eventNames
[uMsg
- WM_MOUSEFIRST
], wIndex
,
261 notifyItem
->hWnd
, notifyItem
->uCallbackMessage
, notifyItem
->uID
, uMsg
);
265 GetWindowThreadProcessId(notifyItem
->hWnd
, &pid
);
267 if (pid
== GetCurrentProcessId() ||
268 (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
))
270 PostMessage(notifyItem
->hWnd
,
271 notifyItem
->uCallbackMessage
,
277 SendMessage(notifyItem
->hWnd
,
278 notifyItem
->uCallbackMessage
,
284 LRESULT
OnMouseEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
286 POINT pt
= { GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
) };
288 INT iBtn
= HitTest(&pt
);
292 SendMouseEvent(iBtn
, uMsg
, wParam
);
299 LRESULT
OnTooltipShow(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
302 GetWindowRect(hdr
->hwndFrom
, &rcTip
);
304 SIZE szTip
= { rcTip
.right
- rcTip
.left
, rcTip
.bottom
- rcTip
.top
};
306 INT iBtn
= GetHotItem();
310 MONITORINFO monInfo
= { 0 };
311 HMONITOR hMon
= MonitorFromWindow(m_hWnd
, MONITOR_DEFAULTTONEAREST
);
313 monInfo
.cbSize
= sizeof(monInfo
);
316 GetMonitorInfo(hMon
, &monInfo
);
318 GetWindowRect(GetDesktopWindow(), &monInfo
.rcMonitor
);
320 GetItemRect(iBtn
, &rcItem
);
322 POINT ptItem
= { rcItem
.left
, rcItem
.top
};
323 SIZE szItem
= { rcItem
.right
- rcItem
.left
, rcItem
.bottom
- rcItem
.top
};
324 ClientToScreen(m_hWnd
, &ptItem
);
326 ptItem
.x
+= szItem
.cx
/ 2;
327 ptItem
.y
-= szTip
.cy
;
329 if (ptItem
.x
+ szTip
.cx
> monInfo
.rcMonitor
.right
)
330 ptItem
.x
= monInfo
.rcMonitor
.right
- szTip
.cx
;
332 if (ptItem
.y
+ szTip
.cy
> monInfo
.rcMonitor
.bottom
)
333 ptItem
.y
= monInfo
.rcMonitor
.bottom
- szTip
.cy
;
335 if (ptItem
.x
< monInfo
.rcMonitor
.left
)
336 ptItem
.x
= monInfo
.rcMonitor
.left
;
338 if (ptItem
.y
< monInfo
.rcMonitor
.top
)
339 ptItem
.y
= monInfo
.rcMonitor
.top
;
341 TRACE("ptItem { %d, %d }\n", ptItem
.x
, ptItem
.y
);
343 ::SetWindowPos(hdr
->hwndFrom
, NULL
, ptItem
.x
, ptItem
.y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
354 BEGIN_MSG_MAP(CNotifyToolbar
)
355 MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST
, WM_MOUSELAST
, OnMouseEvent
)
356 NOTIFY_CODE_HANDLER(TTN_SHOW
, OnTooltipShow
)
359 void Initialize(HWND hWndParent
)
362 WS_CHILD
| WS_VISIBLE
| WS_CLIPCHILDREN
|
363 TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
| TBSTYLE_WRAPABLE
| TBSTYLE_TRANSPARENT
|
364 CCS_TOP
| CCS_NORESIZE
| CCS_NOPARENTALIGN
| CCS_NODIVIDER
;
366 SubclassWindow(Create(hWndParent
, styles
));
368 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
370 m_ImageList
= ImageList_Create(16, 16, ILC_COLOR32
| ILC_MASK
, 0, 1000);
371 SetImageList(m_ImageList
);
373 SetButtonSize(ICON_SIZE
, ICON_SIZE
);
378 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
379 public CWindowImpl
< CSysPagerWnd
, CWindow
, CControlWinTraits
>
381 CNotifyToolbar Toolbar
;
385 virtual ~CSysPagerWnd() {}
387 LRESULT
DrawBackground(HDC hdc
)
391 GetClientRect(&rect
);
392 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
397 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
399 HDC hdc
= (HDC
) wParam
;
407 return DrawBackground(hdc
);
410 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
412 Toolbar
.Initialize(m_hWnd
);
416 LRESULT
NotifyMsg(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
418 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
419 if (cpData
->dwData
== 1)
421 SYS_PAGER_COPY_DATA
* data
;
422 NOTIFYICONDATA
*iconData
;
425 parentHWND
= GetParent();
426 parentHWND
= ::GetParent(parentHWND
);
427 ::GetClientRect(parentHWND
, &windowRect
);
429 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
430 iconData
= &data
->nicon_data
;
432 switch (data
->notify_code
)
436 Toolbar
.AddButton(iconData
);
441 Toolbar
.UpdateButton(iconData
);
446 Toolbar
.RemoveButton(iconData
);
450 TRACE("NotifyMessage received with unknown code %d.\n", data
->notify_code
);
453 SendMessage(parentHWND
,
456 MAKELONG(windowRect
.right
- windowRect
.left
,
457 windowRect
.bottom
- windowRect
.top
));
463 void GetSize(IN WPARAM wParam
, IN PSIZE size
)
466 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
468 if (wParam
) /* horizontal */
470 rows
= size
->cy
/ 24;
473 size
->cx
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
477 rows
= size
->cx
/ 24;
480 size
->cy
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
484 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
486 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
487 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
491 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
493 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
494 switch (cdraw
->dwDrawStage
)
497 return CDRF_NOTIFYITEMDRAW
;
499 case CDDS_ITEMPREPAINT
:
500 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
505 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
509 szClient
.cx
= LOWORD(lParam
);
510 szClient
.cy
= HIWORD(lParam
);
512 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
517 tbm
.cbSize
= sizeof(tbm
);
518 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
;
519 tbm
.cxBarPad
= tbm
.cyBarPad
= 0;
520 tbm
.cxButtonSpacing
= 0;
521 tbm
.cyButtonSpacing
= 0;
523 Toolbar
.SetMetrics(&tbm
);
525 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
529 Toolbar
.GetClientRect(&rc
);
531 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
533 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
534 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
536 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
541 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
547 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
549 BEGIN_MSG_MAP(CTaskSwitchWnd
)
550 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
551 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
552 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
553 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
554 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
555 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
558 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
562 /* Create the window. The tray window is going to move it to the correct
563 position and resize it as needed. */
564 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
566 dwStyle
|= WS_VISIBLE
;
568 Create(hWndParent
, 0, NULL
, dwStyle
);
575 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
585 static const WCHAR szTrayClockWndClass
[] = TEXT("TrayClockWClass");
587 #define ID_TRAYCLOCK_TIMER 0
588 #define ID_TRAYCLOCK_TIMER_INIT 1
595 } ClockWndFormats
[] = {
597 { FALSE
, 0, TEXT("dddd") },
598 { FALSE
, DATE_SHORTDATE
, NULL
}
601 #define CLOCKWND_FORMAT_COUNT (sizeof(ClockWndFormats) / sizeof(ClockWndFormats[0]))
603 #define TRAY_CLOCK_WND_SPACING_X 0
604 #define TRAY_CLOCK_WND_SPACING_Y 0
606 class CTrayClockWnd
:
607 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
608 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
614 SYSTEMTIME LocalTime
;
621 DWORD IsTimerEnabled
: 1;
622 DWORD IsInitTimerEnabled
: 1;
623 DWORD LinesMeasured
: 1;
624 DWORD IsHorizontal
: 1;
630 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
631 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
641 ZeroMemory(&textColor
, sizeof(textColor
));
642 ZeroMemory(&rcText
, sizeof(rcText
));
643 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
644 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
645 ZeroMemory(LineSizes
, sizeof(LineSizes
));
646 ZeroMemory(szLines
, sizeof(LineSizes
));
648 virtual ~CTrayClockWnd() { }
650 LRESULT
OnThemeChanged()
656 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
660 GetThemeFont(clockTheme
,
667 hFont
= CreateFontIndirectW(&clockFont
);
669 GetThemeColor(clockTheme
,
677 NONCLIENTMETRICS ncm
= { 0 };
678 ncm
.cbSize
= sizeof(ncm
);
679 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(ncm
), &ncm
, FALSE
);
681 hFont
= CreateFontIndirectW(&ncm
.lfMessageFont
);
683 textColor
= RGB(0, 0, 0);
686 SetFont(hFont
, FALSE
);
688 CloseThemeData(clockTheme
);
693 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
695 return OnThemeChanged();
709 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
711 for (i
= 0; i
!= CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
713 if (szLines
[i
][0] != TEXT('\0') &&
714 !GetTextExtentPoint(hDC
, szLines
[i
], _tcslen(szLines
[i
]),
723 SelectObject(hDC
, hPrevFont
);
725 ReleaseDC(m_hWnd
, hDC
);
731 /* calculate the line spacing */
732 for (i
= 0, c
= 0; i
!= CLOCKWND_FORMAT_COUNT
; i
++)
734 if (LineSizes
[i
].cx
> 0)
736 LineSpacing
+= LineSizes
[i
].cy
;
743 /* We want a spaceing of 1/2 line */
744 LineSpacing
= (LineSpacing
/ c
) / 2;
754 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
756 WORD iLinesVisible
= 0;
758 SIZE szMax
= { 0, 0 };
761 LinesMeasured
= MeasureLines();
767 i
!= CLOCKWND_FORMAT_COUNT
;
770 if (LineSizes
[i
].cx
!= 0)
772 if (iLinesVisible
> 0)
776 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
777 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
784 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
788 /* Add line spacing */
789 szMax
.cy
+= LineSpacing
;
794 /* Increase maximum rectangle */
795 szMax
.cy
+= LineSizes
[i
].cy
;
796 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
797 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
801 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
802 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
806 return iLinesVisible
;
813 INT BufSize
, iRet
, i
;
816 ZeroMemory(LineSizes
,
819 szPrevCurrent
= CurrentSize
;
822 i
!= CLOCKWND_FORMAT_COUNT
;
825 szLines
[i
][0] = TEXT('\0');
826 BufSize
= sizeof(szLines
[0]) / sizeof(szLines
[0][0]);
828 if (ClockWndFormats
[i
].IsTime
)
830 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
831 AdvancedSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
833 ClockWndFormats
[i
].lpFormat
,
839 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
840 ClockWndFormats
[i
].dwFormatFlags
,
842 ClockWndFormats
[i
].lpFormat
,
847 if (iRet
!= 0 && i
== 0)
849 /* Set the window text to the time only */
850 SetWindowText(szLines
[i
]);
854 LinesMeasured
= MeasureLines();
857 GetClientRect(&rcClient
))
861 szWnd
.cx
= rcClient
.right
;
862 szWnd
.cy
= rcClient
.bottom
;
864 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
868 if (IsWindowVisible(m_hWnd
))
870 InvalidateRect(NULL
, TRUE
);
872 if (hWndNotify
!= NULL
&&
873 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
874 szPrevCurrent
.cy
!= CurrentSize
.cy
))
878 nmh
.hwndFrom
= m_hWnd
;
879 nmh
.idFrom
= GetWindowLongPtr(m_hWnd
, GWLP_ID
);
880 nmh
.code
= NTNWM_REALIGN
;
882 SendMessage(hWndNotify
,
892 GetLocalTime(&LocalTime
);
896 UINT
CalculateDueTime()
900 /* Calculate the due time */
901 GetLocalTime(&LocalTime
);
902 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
903 if (AdvancedSettings
.bShowSeconds
)
904 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
906 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
908 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
912 /* Add an artificial delay of 0.05 seconds to make sure the timer
913 doesn't fire too early*/
925 /* Disable all timers */
928 KillTimer(ID_TRAYCLOCK_TIMER
);
929 IsTimerEnabled
= FALSE
;
932 if (IsInitTimerEnabled
)
934 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
937 uiDueTime
= CalculateDueTime();
939 /* Set the new timer */
940 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
941 IsInitTimerEnabled
= Ret
;
943 /* Update the time */
949 VOID
CalibrateTimer()
953 UINT uiWait1
, uiWait2
;
955 /* Kill the initialization timer */
956 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
957 IsInitTimerEnabled
= FALSE
;
959 uiDueTime
= CalculateDueTime();
961 if (AdvancedSettings
.bShowSeconds
)
963 uiWait1
= 1000 - 200;
968 uiWait1
= 60 * 1000 - 200;
972 if (uiDueTime
> uiWait1
)
974 /* The update of the clock will be up to 200 ms late, but that's
975 acceptable. We're going to setup a timer that fires depending
977 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
978 IsTimerEnabled
= Ret
;
980 /* Update the time */
985 /* Recalibrate the timer and recalculate again when the current
986 minute/second ends. */
991 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
993 /* Disable all timers */
996 KillTimer(ID_TRAYCLOCK_TIMER
);
999 if (IsInitTimerEnabled
)
1001 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1007 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1011 int iPrevBkMode
, i
, line
;
1014 HDC hDC
= (HDC
) wParam
;
1018 hDC
= BeginPaint(&ps
);
1024 if (LinesMeasured
&&
1025 GetClientRect(&rcClient
))
1027 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1029 SetTextColor(hDC
, textColor
);
1031 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1033 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1034 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1035 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1036 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1038 for (i
= 0, line
= 0;
1039 i
!= CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1042 if (LineSizes
[i
].cx
!= 0)
1045 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1046 TRAY_CLOCK_WND_SPACING_X
,
1047 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1049 _tcslen(szLines
[i
]));
1051 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1056 SelectObject(hDC
, hPrevFont
);
1058 SetBkMode(hDC
, iPrevBkMode
);
1069 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1072 LinesMeasured
= MeasureLines();
1075 InvalidateRect(NULL
, TRUE
);
1079 LRESULT
DrawBackground(HDC hdc
)
1083 GetClientRect(&rect
);
1084 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1089 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1091 HDC hdc
= (HDC
) wParam
;
1099 return DrawBackground(hdc
);
1102 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1106 case ID_TRAYCLOCK_TIMER
:
1110 case ID_TRAYCLOCK_TIMER_INIT
:
1117 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1119 IsHorizontal
= (BOOL
) wParam
;
1121 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1124 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1126 return (LRESULT
) ResetTime();
1129 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1131 return HTTRANSPARENT
;
1134 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1136 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1140 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1146 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1150 szClient
.cx
= LOWORD(lParam
);
1151 szClient
.cy
= HIWORD(lParam
);
1153 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1154 CurrentSize
= szClient
;
1156 InvalidateRect(NULL
, TRUE
);
1160 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1162 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1163 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1164 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1165 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1166 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1167 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1168 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1169 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1170 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1171 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1172 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1176 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1178 IsHorizontal
= TRUE
;
1180 hWndNotify
= hWndParent
;
1182 /* Create the window. The tray window is going to move it to the correct
1183 position and resize it as needed. */
1184 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1186 dwStyle
|= WS_VISIBLE
;
1188 Create(hWndParent
, 0, NULL
, dwStyle
);
1192 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1205 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1207 #define TRAY_NOTIFY_WND_SPACING_X 2
1208 #define TRAY_NOTIFY_WND_SPACING_Y 2
1210 class CTrayNotifyWnd
:
1211 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1212 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1216 CSysPagerWnd
* m_pager
;
1217 CTrayClockWnd
* m_clock
;
1219 CComPtr
<ITrayWindow
> TrayWindow
;
1222 SIZE szTrayClockMin
;
1224 MARGINS ContentMargin
;
1231 DWORD HideClock
: 1;
1232 DWORD IsHorizontal
: 1;
1245 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1246 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1247 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1249 virtual ~CTrayNotifyWnd() { }
1251 LRESULT
OnThemeChanged()
1254 CloseThemeData(TrayTheme
);
1256 if (IsThemeActive())
1257 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1263 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1265 GetThemeMargins(TrayTheme
,
1275 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1277 ContentMargin
.cxLeftWidth
= 0;
1278 ContentMargin
.cxRightWidth
= 0;
1279 ContentMargin
.cyTopHeight
= 0;
1280 ContentMargin
.cyBottomHeight
= 0;
1286 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1288 return OnThemeChanged();
1291 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1293 m_clock
= new CTrayClockWnd();
1294 m_clock
->_Init(m_hWnd
, !HideClock
);
1296 m_pager
= new CSysPagerWnd();
1297 m_pager
->_Init(m_hWnd
, !HideClock
);
1304 BOOL
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
1306 SIZE szClock
= { 0, 0 };
1307 SIZE szTray
= { 0, 0 };
1309 IsHorizontal
= Horizontal
;
1311 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1313 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1319 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1320 if (szClock
.cy
<= 0)
1325 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1326 if (szClock
.cx
<= 0)
1330 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) Horizontal
, (LPARAM
) &szClock
);
1332 szTrayClockMin
= szClock
;
1336 szTrayClockMin
= szClock
;
1340 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1344 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1347 m_pager
->GetSize(Horizontal
, &szTray
);
1349 szTrayNotify
= szTray
;
1353 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1356 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1358 pSize
->cx
+= szTray
.cx
;
1362 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1365 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1367 pSize
->cy
+= szTray
.cy
;
1370 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1371 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1376 VOID
Size(IN
const SIZE
*pszClient
)
1385 ptClock
.x
= pszClient
->cx
- TRAY_NOTIFY_WND_SPACING_X
- szTrayClockMin
.cx
;
1386 ptClock
.y
= TRAY_NOTIFY_WND_SPACING_Y
;
1387 szClock
.cx
= szTrayClockMin
.cx
;
1388 szClock
.cy
= pszClient
->cy
- (2 * TRAY_NOTIFY_WND_SPACING_Y
);
1392 ptClock
.x
= TRAY_NOTIFY_WND_SPACING_X
;
1393 ptClock
.y
= pszClient
->cy
- TRAY_NOTIFY_WND_SPACING_Y
- szTrayClockMin
.cy
;
1394 szClock
.cx
= pszClient
->cx
- (2 * TRAY_NOTIFY_WND_SPACING_X
);
1395 szClock
.cy
= szTrayClockMin
.cy
;
1398 m_clock
->SetWindowPos(
1408 ptClock
.x
-= szTrayNotify
.cx
;
1412 ptClock
.y
-= szTrayNotify
.cy
;
1415 m_pager
->SetWindowPos(
1425 LRESULT
DrawBackground(HDC hdc
)
1429 GetClientRect(&rect
);
1433 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1435 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1438 DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1444 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1446 HDC hdc
= (HDC
) wParam
;
1454 return DrawBackground(hdc
);
1457 LRESULT
NotifyMsg(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1461 m_pager
->NotifyMsg(uMsg
, wParam
, lParam
, bHandled
);
1467 BOOL
GetClockRect(OUT PRECT rcClock
)
1469 if (!IsWindowVisible(m_clock
->m_hWnd
))
1472 return GetWindowRect(m_clock
->m_hWnd
, rcClock
);
1475 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1477 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
);
1480 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1482 if (m_clock
!= NULL
)
1484 /* Forward the message to the tray clock window procedure */
1485 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1490 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1494 szClient
.cx
= LOWORD(lParam
);
1495 szClient
.cy
= HIWORD(lParam
);
1502 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1504 return HTTRANSPARENT
;
1507 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1509 BOOL PrevHidden
= HideClock
;
1510 HideClock
= (wParam
== 0);
1512 if (m_clock
!= NULL
&& PrevHidden
!= HideClock
)
1514 m_clock
->ShowWindow(HideClock
? SW_HIDE
: SW_SHOW
);
1517 return (LRESULT
) (!PrevHidden
);
1520 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1522 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1524 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1526 /* Pass down notifications */
1527 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1533 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1535 if (m_clock
!= NULL
)
1537 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1544 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1550 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1552 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1553 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1554 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1555 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1556 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1557 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1558 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
1559 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1560 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
1561 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1562 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
1563 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
1566 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
, IN BOOL bHideClock
)
1568 HWND hWndTrayWindow
;
1570 hWndTrayWindow
= TrayWindow
->GetHWND();
1571 if (hWndTrayWindow
== NULL
)
1574 this->TrayWindow
= TrayWindow
;
1575 this->HideClock
= bHideClock
;
1576 this->hWndNotify
= hWndTrayWindow
;
1578 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
1579 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
1583 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, BOOL bHideClock
, CTrayNotifyWnd
** ppinstance
)
1585 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
1586 // TODO: Destroy after the window is destroyed
1587 *ppinstance
= pTrayNotify
;
1589 return pTrayNotify
->_Init(Tray
, bHideClock
);
1593 TrayNotify_NotifyMsg(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
1596 pTrayNotify
->NotifyMsg(0, wParam
, lParam
, bDummy
);
1600 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
1602 return pTrayNotify
->GetClockRect(rcClock
);