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 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
;
89 WCHAR text
[] = TEXT("");
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 ::SendNotifyMessage(HWND_BROADCAST
,
454 RegisterWindowMessage(TEXT("TaskbarCreated")),
461 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
463 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
464 if (cpData
->dwData
== 1)
466 SYS_PAGER_COPY_DATA
* data
;
467 NOTIFYICONDATA
*iconData
;
471 parentHWND
= GetParent();
472 parentHWND
= ::GetParent(parentHWND
);
473 ::GetClientRect(parentHWND
, &windowRect
);
475 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
476 iconData
= &data
->nicon_data
;
478 TRACE("NotifyIconCmd received. Code=%d\n", data
->notify_code
);
479 switch (data
->notify_code
)
482 ret
= Toolbar
.AddButton(iconData
);
485 ret
= Toolbar
.UpdateButton(iconData
);
488 ret
= Toolbar
.RemoveButton(iconData
);
491 TRACE("NotifyIconCmd received with unknown code %d.\n", data
->notify_code
);
495 SendMessage(parentHWND
,
498 MAKELONG(windowRect
.right
- windowRect
.left
,
499 windowRect
.bottom
- windowRect
.top
));
507 void GetSize(IN WPARAM wParam
, IN PSIZE size
)
510 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
512 if (wParam
) /* horizontal */
514 rows
= size
->cy
/ 24;
517 size
->cx
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
521 rows
= size
->cx
/ 24;
524 size
->cy
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
528 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
530 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
531 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
535 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
537 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
538 switch (cdraw
->dwDrawStage
)
541 return CDRF_NOTIFYITEMDRAW
;
543 case CDDS_ITEMPREPAINT
:
544 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
549 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
553 szClient
.cx
= LOWORD(lParam
);
554 szClient
.cy
= HIWORD(lParam
);
556 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
561 tbm
.cbSize
= sizeof(tbm
);
562 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
;
563 tbm
.cxBarPad
= tbm
.cyBarPad
= 0;
564 tbm
.cxButtonSpacing
= 0;
565 tbm
.cyButtonSpacing
= 0;
567 Toolbar
.SetMetrics(&tbm
);
569 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
573 Toolbar
.GetClientRect(&rc
);
575 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
577 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
578 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
580 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
585 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
591 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
593 BEGIN_MSG_MAP(CTaskSwitchWnd
)
594 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
595 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
596 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
597 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
598 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
599 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
602 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
606 /* Create the window. The tray window is going to move it to the correct
607 position and resize it as needed. */
608 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
610 dwStyle
|= WS_VISIBLE
;
612 Create(hWndParent
, 0, NULL
, dwStyle
);
619 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
629 static const WCHAR szTrayClockWndClass
[] = TEXT("TrayClockWClass");
631 #define ID_TRAYCLOCK_TIMER 0
632 #define ID_TRAYCLOCK_TIMER_INIT 1
639 } ClockWndFormats
[] = {
641 { FALSE
, 0, TEXT("dddd") },
642 { FALSE
, DATE_SHORTDATE
, NULL
}
645 #define CLOCKWND_FORMAT_COUNT (sizeof(ClockWndFormats) / sizeof(ClockWndFormats[0]))
647 #define TRAY_CLOCK_WND_SPACING_X 0
648 #define TRAY_CLOCK_WND_SPACING_Y 0
650 class CTrayClockWnd
:
651 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
652 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
658 SYSTEMTIME LocalTime
;
665 DWORD IsTimerEnabled
: 1;
666 DWORD IsInitTimerEnabled
: 1;
667 DWORD LinesMeasured
: 1;
668 DWORD IsHorizontal
: 1;
674 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
675 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
685 ZeroMemory(&textColor
, sizeof(textColor
));
686 ZeroMemory(&rcText
, sizeof(rcText
));
687 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
688 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
689 ZeroMemory(LineSizes
, sizeof(LineSizes
));
690 ZeroMemory(szLines
, sizeof(LineSizes
));
692 virtual ~CTrayClockWnd() { }
694 LRESULT
OnThemeChanged()
700 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
704 GetThemeFont(clockTheme
,
711 hFont
= CreateFontIndirectW(&clockFont
);
713 GetThemeColor(clockTheme
,
721 NONCLIENTMETRICS ncm
= { 0 };
722 ncm
.cbSize
= sizeof(ncm
);
723 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(ncm
), &ncm
, FALSE
);
725 hFont
= CreateFontIndirectW(&ncm
.lfMessageFont
);
727 textColor
= RGB(0, 0, 0);
730 SetFont(hFont
, FALSE
);
732 CloseThemeData(clockTheme
);
737 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
739 return OnThemeChanged();
753 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
755 for (i
= 0; i
!= CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
757 if (szLines
[i
][0] != TEXT('\0') &&
758 !GetTextExtentPoint(hDC
, szLines
[i
], _tcslen(szLines
[i
]),
767 SelectObject(hDC
, hPrevFont
);
775 /* calculate the line spacing */
776 for (i
= 0, c
= 0; i
!= CLOCKWND_FORMAT_COUNT
; i
++)
778 if (LineSizes
[i
].cx
> 0)
780 LineSpacing
+= LineSizes
[i
].cy
;
787 /* We want a spaceing of 1/2 line */
788 LineSpacing
= (LineSpacing
/ c
) / 2;
798 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
800 WORD iLinesVisible
= 0;
802 SIZE szMax
= { 0, 0 };
805 LinesMeasured
= MeasureLines();
811 i
!= CLOCKWND_FORMAT_COUNT
;
814 if (LineSizes
[i
].cx
!= 0)
816 if (iLinesVisible
> 0)
820 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
821 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
828 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
832 /* Add line spacing */
833 szMax
.cy
+= LineSpacing
;
838 /* Increase maximum rectangle */
839 szMax
.cy
+= LineSizes
[i
].cy
;
840 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
841 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
845 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
846 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
850 return iLinesVisible
;
857 INT BufSize
, iRet
, i
;
860 ZeroMemory(LineSizes
,
863 szPrevCurrent
= CurrentSize
;
866 i
!= CLOCKWND_FORMAT_COUNT
;
869 szLines
[i
][0] = TEXT('\0');
870 BufSize
= sizeof(szLines
[0]) / sizeof(szLines
[0][0]);
872 if (ClockWndFormats
[i
].IsTime
)
874 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
875 AdvancedSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
877 ClockWndFormats
[i
].lpFormat
,
883 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
884 ClockWndFormats
[i
].dwFormatFlags
,
886 ClockWndFormats
[i
].lpFormat
,
891 if (iRet
!= 0 && i
== 0)
893 /* Set the window text to the time only */
894 SetWindowText(szLines
[i
]);
898 LinesMeasured
= MeasureLines();
901 GetClientRect(&rcClient
))
905 szWnd
.cx
= rcClient
.right
;
906 szWnd
.cy
= rcClient
.bottom
;
908 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
912 if (IsWindowVisible())
914 InvalidateRect(NULL
, TRUE
);
916 if (hWndNotify
!= NULL
&&
917 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
918 szPrevCurrent
.cy
!= CurrentSize
.cy
))
922 nmh
.hwndFrom
= m_hWnd
;
923 nmh
.idFrom
= GetWindowLongPtr(GWLP_ID
);
924 nmh
.code
= NTNWM_REALIGN
;
926 SendMessage(hWndNotify
,
936 GetLocalTime(&LocalTime
);
940 UINT
CalculateDueTime()
944 /* Calculate the due time */
945 GetLocalTime(&LocalTime
);
946 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
947 if (AdvancedSettings
.bShowSeconds
)
948 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
950 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
952 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
956 /* Add an artificial delay of 0.05 seconds to make sure the timer
957 doesn't fire too early*/
969 /* Disable all timers */
972 KillTimer(ID_TRAYCLOCK_TIMER
);
973 IsTimerEnabled
= FALSE
;
976 if (IsInitTimerEnabled
)
978 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
981 uiDueTime
= CalculateDueTime();
983 /* Set the new timer */
984 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
985 IsInitTimerEnabled
= Ret
;
987 /* Update the time */
993 VOID
CalibrateTimer()
997 UINT uiWait1
, uiWait2
;
999 /* Kill the initialization timer */
1000 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1001 IsInitTimerEnabled
= FALSE
;
1003 uiDueTime
= CalculateDueTime();
1005 if (AdvancedSettings
.bShowSeconds
)
1007 uiWait1
= 1000 - 200;
1012 uiWait1
= 60 * 1000 - 200;
1013 uiWait2
= 60 * 1000;
1016 if (uiDueTime
> uiWait1
)
1018 /* The update of the clock will be up to 200 ms late, but that's
1019 acceptable. We're going to setup a timer that fires depending
1021 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
1022 IsTimerEnabled
= Ret
;
1024 /* Update the time */
1029 /* Recalibrate the timer and recalculate again when the current
1030 minute/second ends. */
1035 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1037 /* Disable all timers */
1040 KillTimer(ID_TRAYCLOCK_TIMER
);
1043 if (IsInitTimerEnabled
)
1045 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1051 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1055 int iPrevBkMode
, i
, line
;
1058 HDC hDC
= (HDC
) wParam
;
1062 hDC
= BeginPaint(&ps
);
1068 if (LinesMeasured
&&
1069 GetClientRect(&rcClient
))
1071 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1073 SetTextColor(hDC
, textColor
);
1075 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1077 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1078 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1079 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1080 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1082 for (i
= 0, line
= 0;
1083 i
!= CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1086 if (LineSizes
[i
].cx
!= 0)
1089 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1090 TRAY_CLOCK_WND_SPACING_X
,
1091 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1093 _tcslen(szLines
[i
]));
1095 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1100 SelectObject(hDC
, hPrevFont
);
1102 SetBkMode(hDC
, iPrevBkMode
);
1113 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1116 LinesMeasured
= MeasureLines();
1119 InvalidateRect(NULL
, TRUE
);
1123 LRESULT
DrawBackground(HDC hdc
)
1127 GetClientRect(&rect
);
1128 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1133 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1135 HDC hdc
= (HDC
) wParam
;
1143 return DrawBackground(hdc
);
1146 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1150 case ID_TRAYCLOCK_TIMER
:
1154 case ID_TRAYCLOCK_TIMER_INIT
:
1161 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1163 IsHorizontal
= (BOOL
) wParam
;
1165 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1168 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1170 return (LRESULT
) ResetTime();
1173 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1175 return HTTRANSPARENT
;
1178 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1180 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1184 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1190 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1194 szClient
.cx
= LOWORD(lParam
);
1195 szClient
.cy
= HIWORD(lParam
);
1197 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1198 CurrentSize
= szClient
;
1200 InvalidateRect(NULL
, TRUE
);
1204 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1206 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1207 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1208 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1209 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1210 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1211 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1212 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1213 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1214 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1215 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1216 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1220 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1222 IsHorizontal
= TRUE
;
1224 hWndNotify
= hWndParent
;
1226 /* Create the window. The tray window is going to move it to the correct
1227 position and resize it as needed. */
1228 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1230 dwStyle
|= WS_VISIBLE
;
1232 Create(hWndParent
, 0, NULL
, dwStyle
);
1236 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1249 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1251 #define TRAY_NOTIFY_WND_SPACING_X 2
1252 #define TRAY_NOTIFY_WND_SPACING_Y 2
1254 class CTrayNotifyWnd
:
1255 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1256 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1260 CSysPagerWnd
* m_pager
;
1261 CTrayClockWnd
* m_clock
;
1263 CComPtr
<ITrayWindow
> TrayWindow
;
1266 SIZE szTrayClockMin
;
1268 MARGINS ContentMargin
;
1275 DWORD HideClock
: 1;
1276 DWORD IsHorizontal
: 1;
1289 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1290 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1291 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1293 virtual ~CTrayNotifyWnd() { }
1295 LRESULT
OnThemeChanged()
1298 CloseThemeData(TrayTheme
);
1300 if (IsThemeActive())
1301 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1307 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1309 GetThemeMargins(TrayTheme
,
1319 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1321 ContentMargin
.cxLeftWidth
= 0;
1322 ContentMargin
.cxRightWidth
= 0;
1323 ContentMargin
.cyTopHeight
= 0;
1324 ContentMargin
.cyBottomHeight
= 0;
1330 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1332 return OnThemeChanged();
1335 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1337 m_clock
= new CTrayClockWnd();
1338 m_clock
->_Init(m_hWnd
, !HideClock
);
1340 m_pager
= new CSysPagerWnd();
1341 m_pager
->_Init(m_hWnd
, !HideClock
);
1348 BOOL
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
1350 SIZE szClock
= { 0, 0 };
1351 SIZE szTray
= { 0, 0 };
1353 IsHorizontal
= Horizontal
;
1355 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1357 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1363 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1364 if (szClock
.cy
<= 0)
1369 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1370 if (szClock
.cx
<= 0)
1374 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) Horizontal
, (LPARAM
) &szClock
);
1376 szTrayClockMin
= szClock
;
1380 szTrayClockMin
= szClock
;
1384 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1388 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1391 m_pager
->GetSize(Horizontal
, &szTray
);
1393 szTrayNotify
= szTray
;
1397 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1400 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1402 pSize
->cx
+= szTray
.cx
;
1406 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1409 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1411 pSize
->cy
+= szTray
.cy
;
1414 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1415 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1420 VOID
Size(IN
const SIZE
*pszClient
)
1429 ptClock
.x
= pszClient
->cx
- TRAY_NOTIFY_WND_SPACING_X
- szTrayClockMin
.cx
;
1430 ptClock
.y
= TRAY_NOTIFY_WND_SPACING_Y
;
1431 szClock
.cx
= szTrayClockMin
.cx
;
1432 szClock
.cy
= pszClient
->cy
- (2 * TRAY_NOTIFY_WND_SPACING_Y
);
1436 ptClock
.x
= TRAY_NOTIFY_WND_SPACING_X
;
1437 ptClock
.y
= pszClient
->cy
- TRAY_NOTIFY_WND_SPACING_Y
- szTrayClockMin
.cy
;
1438 szClock
.cx
= pszClient
->cx
- (2 * TRAY_NOTIFY_WND_SPACING_X
);
1439 szClock
.cy
= szTrayClockMin
.cy
;
1442 m_clock
->SetWindowPos(
1452 ptClock
.x
-= szTrayNotify
.cx
;
1456 ptClock
.y
-= szTrayNotify
.cy
;
1459 m_pager
->SetWindowPos(
1469 LRESULT
DrawBackground(HDC hdc
)
1474 GetClientRect(&rect
);
1478 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1480 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1483 res
= DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1489 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1491 HDC hdc
= (HDC
) wParam
;
1499 return DrawBackground(hdc
);
1502 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
1506 return m_pager
->NotifyIconCmd(wParam
, lParam
);
1512 BOOL
GetClockRect(OUT PRECT rcClock
)
1514 if (!m_clock
->IsWindowVisible())
1517 return m_clock
->GetWindowRect(rcClock
);
1520 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1522 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
);
1525 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1527 if (m_clock
!= NULL
)
1529 /* Forward the message to the tray clock window procedure */
1530 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1535 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1539 szClient
.cx
= LOWORD(lParam
);
1540 szClient
.cy
= HIWORD(lParam
);
1547 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1549 return HTTRANSPARENT
;
1552 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1554 BOOL PrevHidden
= HideClock
;
1555 HideClock
= (wParam
== 0);
1557 if (m_clock
!= NULL
&& PrevHidden
!= HideClock
)
1559 m_clock
->ShowWindow(HideClock
? SW_HIDE
: SW_SHOW
);
1562 return (LRESULT
) (!PrevHidden
);
1565 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1567 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1569 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1571 /* Pass down notifications */
1572 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1578 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1580 if (m_clock
!= NULL
)
1582 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1589 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1595 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1597 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1598 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1599 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1600 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1601 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1602 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1603 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
1604 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1605 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
1606 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1607 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
1608 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
1611 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
, IN BOOL bHideClock
)
1613 HWND hWndTrayWindow
;
1615 hWndTrayWindow
= TrayWindow
->GetHWND();
1616 if (hWndTrayWindow
== NULL
)
1619 this->TrayWindow
= TrayWindow
;
1620 this->HideClock
= bHideClock
;
1621 this->hWndNotify
= hWndTrayWindow
;
1623 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
1624 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
1628 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, BOOL bHideClock
, CTrayNotifyWnd
** ppinstance
)
1630 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
1631 // TODO: Destroy after the window is destroyed
1632 *ppinstance
= pTrayNotify
;
1634 return pTrayNotify
->_Init(Tray
, bHideClock
);
1638 TrayNotify_NotifyIconCmd(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
1640 return pTrayNotify
->NotifyIconCmd(wParam
, lParam
);
1644 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
1646 return pTrayNotify
->GetClockRect(rcClock
);