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_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
);
204 BOOL
RemoveButton(IN CONST NOTIFYICONDATA
*iconData
)
206 NOTIFYICONDATA
* notifyItem
;
208 int index
= FindItemByIconData(iconData
, ¬ifyItem
);
214 if (!(notifyItem
->dwState
& NIS_HIDDEN
))
216 m_VisibleButtonCount
--;
224 VOID
GetTooltipText(int index
, LPTSTR szTip
, DWORD cchTip
)
226 NOTIFYICONDATA
* notifyItem
;
227 notifyItem
= GetItemData(index
);
231 StringCchCopy(szTip
, cchTip
, notifyItem
->szTip
);
237 VOID
SendMouseEvent(IN WORD wIndex
, IN UINT uMsg
, IN WPARAM wParam
)
239 static LPCWSTR eventNames
[] = {
256 NOTIFYICONDATA
* notifyItem
= GetItemData(wIndex
);
258 if (!::IsWindow(notifyItem
->hWnd
))
260 // We detect and destroy icons with invalid handles only on mouse move over systray, same as MS does.
261 // Alternatively we could search for them periodically (would waste more resources).
262 TRACE("destroying icon with invalid handle\n");
264 HWND parentHWND
= GetParent();
265 parentHWND
= ::GetParent(parentHWND
);
268 ::GetClientRect(parentHWND
, &windowRect
);
270 RemoveButton(notifyItem
);
272 SendMessage(parentHWND
,
275 MAKELONG(windowRect
.right
- windowRect
.left
,
276 windowRect
.bottom
- windowRect
.top
));
281 if (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
)
283 TRACE("Sending message %S from button %d to %p (msg=%x, w=%x, l=%x)...\n",
284 eventNames
[uMsg
- WM_MOUSEFIRST
], wIndex
,
285 notifyItem
->hWnd
, notifyItem
->uCallbackMessage
, notifyItem
->uID
, uMsg
);
289 GetWindowThreadProcessId(notifyItem
->hWnd
, &pid
);
291 if (pid
== GetCurrentProcessId() ||
292 (uMsg
>= WM_MOUSEFIRST
&& uMsg
<= WM_MOUSELAST
))
294 PostMessage(notifyItem
->hWnd
,
295 notifyItem
->uCallbackMessage
,
301 SendMessage(notifyItem
->hWnd
,
302 notifyItem
->uCallbackMessage
,
308 LRESULT
OnMouseEvent(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
310 POINT pt
= { GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
) };
312 INT iBtn
= HitTest(&pt
);
316 SendMouseEvent(iBtn
, uMsg
, wParam
);
323 LRESULT
OnTooltipShow(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
326 GetWindowRect(hdr
->hwndFrom
, &rcTip
);
328 SIZE szTip
= { rcTip
.right
- rcTip
.left
, rcTip
.bottom
- rcTip
.top
};
330 INT iBtn
= GetHotItem();
334 MONITORINFO monInfo
= { 0 };
335 HMONITOR hMon
= MonitorFromWindow(m_hWnd
, MONITOR_DEFAULTTONEAREST
);
337 monInfo
.cbSize
= sizeof(monInfo
);
340 GetMonitorInfo(hMon
, &monInfo
);
342 GetWindowRect(GetDesktopWindow(), &monInfo
.rcMonitor
);
344 GetItemRect(iBtn
, &rcItem
);
346 POINT ptItem
= { rcItem
.left
, rcItem
.top
};
347 SIZE szItem
= { rcItem
.right
- rcItem
.left
, rcItem
.bottom
- rcItem
.top
};
348 ClientToScreen(m_hWnd
, &ptItem
);
350 ptItem
.x
+= szItem
.cx
/ 2;
351 ptItem
.y
-= szTip
.cy
;
353 if (ptItem
.x
+ szTip
.cx
> monInfo
.rcMonitor
.right
)
354 ptItem
.x
= monInfo
.rcMonitor
.right
- szTip
.cx
;
356 if (ptItem
.y
+ szTip
.cy
> monInfo
.rcMonitor
.bottom
)
357 ptItem
.y
= monInfo
.rcMonitor
.bottom
- szTip
.cy
;
359 if (ptItem
.x
< monInfo
.rcMonitor
.left
)
360 ptItem
.x
= monInfo
.rcMonitor
.left
;
362 if (ptItem
.y
< monInfo
.rcMonitor
.top
)
363 ptItem
.y
= monInfo
.rcMonitor
.top
;
365 TRACE("ptItem { %d, %d }\n", ptItem
.x
, ptItem
.y
);
367 ::SetWindowPos(hdr
->hwndFrom
, NULL
, ptItem
.x
, ptItem
.y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
378 BEGIN_MSG_MAP(CNotifyToolbar
)
379 MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST
, WM_MOUSELAST
, OnMouseEvent
)
380 NOTIFY_CODE_HANDLER(TTN_SHOW
, OnTooltipShow
)
383 void Initialize(HWND hWndParent
)
386 WS_CHILD
| WS_VISIBLE
| WS_CLIPCHILDREN
|
387 TBSTYLE_FLAT
| TBSTYLE_TOOLTIPS
| TBSTYLE_WRAPABLE
| TBSTYLE_TRANSPARENT
|
388 CCS_TOP
| CCS_NORESIZE
| CCS_NOPARENTALIGN
| CCS_NODIVIDER
;
390 SubclassWindow(CToolbar::Create(hWndParent
, styles
));
392 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
394 m_ImageList
= ImageList_Create(16, 16, ILC_COLOR32
| ILC_MASK
, 0, 1000);
395 SetImageList(m_ImageList
);
397 SetButtonSize(ICON_SIZE
, ICON_SIZE
);
402 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
403 public CWindowImpl
< CSysPagerWnd
, CWindow
, CControlWinTraits
>
405 CNotifyToolbar Toolbar
;
409 virtual ~CSysPagerWnd() {}
411 LRESULT
DrawBackground(HDC hdc
)
415 GetClientRect(&rect
);
416 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
421 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
423 HDC hdc
= (HDC
) wParam
;
431 return DrawBackground(hdc
);
434 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
436 Toolbar
.Initialize(m_hWnd
);
440 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
442 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
443 if (cpData
->dwData
== 1)
445 SYS_PAGER_COPY_DATA
* data
;
446 NOTIFYICONDATA
*iconData
;
450 parentHWND
= GetParent();
451 parentHWND
= ::GetParent(parentHWND
);
452 ::GetClientRect(parentHWND
, &windowRect
);
454 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
455 iconData
= &data
->nicon_data
;
457 TRACE("NotifyIconCmd received. Code=%d\n", data
->notify_code
);
458 switch (data
->notify_code
)
461 ret
= Toolbar
.AddButton(iconData
);
464 ret
= Toolbar
.UpdateButton(iconData
);
467 ret
= Toolbar
.RemoveButton(iconData
);
470 TRACE("NotifyIconCmd received with unknown code %d.\n", data
->notify_code
);
474 SendMessage(parentHWND
,
477 MAKELONG(windowRect
.right
- windowRect
.left
,
478 windowRect
.bottom
- windowRect
.top
));
486 void GetSize(IN WPARAM wParam
, IN PSIZE size
)
489 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
491 if (wParam
) /* horizontal */
493 rows
= size
->cy
/ 24;
496 size
->cx
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
500 rows
= size
->cx
/ 24;
503 size
->cy
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
507 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
509 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
510 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
514 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
516 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
517 switch (cdraw
->dwDrawStage
)
520 return CDRF_NOTIFYITEMDRAW
;
522 case CDDS_ITEMPREPAINT
:
523 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
528 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
532 szClient
.cx
= LOWORD(lParam
);
533 szClient
.cy
= HIWORD(lParam
);
535 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
540 tbm
.cbSize
= sizeof(tbm
);
541 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
;
542 tbm
.cxBarPad
= tbm
.cyBarPad
= 0;
543 tbm
.cxButtonSpacing
= 0;
544 tbm
.cyButtonSpacing
= 0;
546 Toolbar
.SetMetrics(&tbm
);
548 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
552 Toolbar
.GetClientRect(&rc
);
554 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
556 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
557 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
559 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
564 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
570 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
572 BEGIN_MSG_MAP(CTaskSwitchWnd
)
573 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
574 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
575 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
576 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
577 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
578 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
581 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
585 /* Create the window. The tray window is going to move it to the correct
586 position and resize it as needed. */
587 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
589 dwStyle
|= WS_VISIBLE
;
591 Create(hWndParent
, 0, NULL
, dwStyle
);
598 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
608 static const WCHAR szTrayClockWndClass
[] = TEXT("TrayClockWClass");
610 #define ID_TRAYCLOCK_TIMER 0
611 #define ID_TRAYCLOCK_TIMER_INIT 1
618 } ClockWndFormats
[] = {
620 { FALSE
, 0, TEXT("dddd") },
621 { FALSE
, DATE_SHORTDATE
, NULL
}
624 #define CLOCKWND_FORMAT_COUNT (sizeof(ClockWndFormats) / sizeof(ClockWndFormats[0]))
626 #define TRAY_CLOCK_WND_SPACING_X 0
627 #define TRAY_CLOCK_WND_SPACING_Y 0
629 class CTrayClockWnd
:
630 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
631 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
637 SYSTEMTIME LocalTime
;
644 DWORD IsTimerEnabled
: 1;
645 DWORD IsInitTimerEnabled
: 1;
646 DWORD LinesMeasured
: 1;
647 DWORD IsHorizontal
: 1;
653 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
654 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
664 ZeroMemory(&textColor
, sizeof(textColor
));
665 ZeroMemory(&rcText
, sizeof(rcText
));
666 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
667 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
668 ZeroMemory(LineSizes
, sizeof(LineSizes
));
669 ZeroMemory(szLines
, sizeof(LineSizes
));
671 virtual ~CTrayClockWnd() { }
673 LRESULT
OnThemeChanged()
679 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
683 GetThemeFont(clockTheme
,
690 hFont
= CreateFontIndirectW(&clockFont
);
692 GetThemeColor(clockTheme
,
700 NONCLIENTMETRICS ncm
= { 0 };
701 ncm
.cbSize
= sizeof(ncm
);
702 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(ncm
), &ncm
, FALSE
);
704 hFont
= CreateFontIndirectW(&ncm
.lfMessageFont
);
706 textColor
= RGB(0, 0, 0);
709 SetFont(hFont
, FALSE
);
711 CloseThemeData(clockTheme
);
716 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
718 return OnThemeChanged();
732 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
734 for (i
= 0; i
!= CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
736 if (szLines
[i
][0] != TEXT('\0') &&
737 !GetTextExtentPoint(hDC
, szLines
[i
], _tcslen(szLines
[i
]),
746 SelectObject(hDC
, hPrevFont
);
748 ReleaseDC(m_hWnd
, hDC
);
754 /* calculate the line spacing */
755 for (i
= 0, c
= 0; i
!= CLOCKWND_FORMAT_COUNT
; i
++)
757 if (LineSizes
[i
].cx
> 0)
759 LineSpacing
+= LineSizes
[i
].cy
;
766 /* We want a spaceing of 1/2 line */
767 LineSpacing
= (LineSpacing
/ c
) / 2;
777 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
779 WORD iLinesVisible
= 0;
781 SIZE szMax
= { 0, 0 };
784 LinesMeasured
= MeasureLines();
790 i
!= CLOCKWND_FORMAT_COUNT
;
793 if (LineSizes
[i
].cx
!= 0)
795 if (iLinesVisible
> 0)
799 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
800 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
807 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
811 /* Add line spacing */
812 szMax
.cy
+= LineSpacing
;
817 /* Increase maximum rectangle */
818 szMax
.cy
+= LineSizes
[i
].cy
;
819 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
820 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
824 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
825 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
829 return iLinesVisible
;
836 INT BufSize
, iRet
, i
;
839 ZeroMemory(LineSizes
,
842 szPrevCurrent
= CurrentSize
;
845 i
!= CLOCKWND_FORMAT_COUNT
;
848 szLines
[i
][0] = TEXT('\0');
849 BufSize
= sizeof(szLines
[0]) / sizeof(szLines
[0][0]);
851 if (ClockWndFormats
[i
].IsTime
)
853 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
854 AdvancedSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
856 ClockWndFormats
[i
].lpFormat
,
862 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
863 ClockWndFormats
[i
].dwFormatFlags
,
865 ClockWndFormats
[i
].lpFormat
,
870 if (iRet
!= 0 && i
== 0)
872 /* Set the window text to the time only */
873 SetWindowText(szLines
[i
]);
877 LinesMeasured
= MeasureLines();
880 GetClientRect(&rcClient
))
884 szWnd
.cx
= rcClient
.right
;
885 szWnd
.cy
= rcClient
.bottom
;
887 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
891 if (IsWindowVisible(m_hWnd
))
893 InvalidateRect(NULL
, TRUE
);
895 if (hWndNotify
!= NULL
&&
896 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
897 szPrevCurrent
.cy
!= CurrentSize
.cy
))
901 nmh
.hwndFrom
= m_hWnd
;
902 nmh
.idFrom
= GetWindowLongPtr(m_hWnd
, GWLP_ID
);
903 nmh
.code
= NTNWM_REALIGN
;
905 SendMessage(hWndNotify
,
915 GetLocalTime(&LocalTime
);
919 UINT
CalculateDueTime()
923 /* Calculate the due time */
924 GetLocalTime(&LocalTime
);
925 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
926 if (AdvancedSettings
.bShowSeconds
)
927 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
929 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
931 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
935 /* Add an artificial delay of 0.05 seconds to make sure the timer
936 doesn't fire too early*/
948 /* Disable all timers */
951 KillTimer(ID_TRAYCLOCK_TIMER
);
952 IsTimerEnabled
= FALSE
;
955 if (IsInitTimerEnabled
)
957 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
960 uiDueTime
= CalculateDueTime();
962 /* Set the new timer */
963 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
964 IsInitTimerEnabled
= Ret
;
966 /* Update the time */
972 VOID
CalibrateTimer()
976 UINT uiWait1
, uiWait2
;
978 /* Kill the initialization timer */
979 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
980 IsInitTimerEnabled
= FALSE
;
982 uiDueTime
= CalculateDueTime();
984 if (AdvancedSettings
.bShowSeconds
)
986 uiWait1
= 1000 - 200;
991 uiWait1
= 60 * 1000 - 200;
995 if (uiDueTime
> uiWait1
)
997 /* The update of the clock will be up to 200 ms late, but that's
998 acceptable. We're going to setup a timer that fires depending
1000 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
1001 IsTimerEnabled
= Ret
;
1003 /* Update the time */
1008 /* Recalibrate the timer and recalculate again when the current
1009 minute/second ends. */
1014 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1016 /* Disable all timers */
1019 KillTimer(ID_TRAYCLOCK_TIMER
);
1022 if (IsInitTimerEnabled
)
1024 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1030 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1034 int iPrevBkMode
, i
, line
;
1037 HDC hDC
= (HDC
) wParam
;
1041 hDC
= BeginPaint(&ps
);
1047 if (LinesMeasured
&&
1048 GetClientRect(&rcClient
))
1050 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1052 SetTextColor(hDC
, textColor
);
1054 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1056 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1057 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1058 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1059 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1061 for (i
= 0, line
= 0;
1062 i
!= CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1065 if (LineSizes
[i
].cx
!= 0)
1068 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1069 TRAY_CLOCK_WND_SPACING_X
,
1070 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1072 _tcslen(szLines
[i
]));
1074 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1079 SelectObject(hDC
, hPrevFont
);
1081 SetBkMode(hDC
, iPrevBkMode
);
1092 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1095 LinesMeasured
= MeasureLines();
1098 InvalidateRect(NULL
, TRUE
);
1102 LRESULT
DrawBackground(HDC hdc
)
1106 GetClientRect(&rect
);
1107 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1112 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1114 HDC hdc
= (HDC
) wParam
;
1122 return DrawBackground(hdc
);
1125 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1129 case ID_TRAYCLOCK_TIMER
:
1133 case ID_TRAYCLOCK_TIMER_INIT
:
1140 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1142 IsHorizontal
= (BOOL
) wParam
;
1144 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1147 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1149 return (LRESULT
) ResetTime();
1152 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1154 return HTTRANSPARENT
;
1157 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1159 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1163 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1169 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1173 szClient
.cx
= LOWORD(lParam
);
1174 szClient
.cy
= HIWORD(lParam
);
1176 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1177 CurrentSize
= szClient
;
1179 InvalidateRect(NULL
, TRUE
);
1183 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1185 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1186 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1187 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1188 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1189 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1190 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1191 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1192 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1193 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1194 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1195 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1199 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1201 IsHorizontal
= TRUE
;
1203 hWndNotify
= hWndParent
;
1205 /* Create the window. The tray window is going to move it to the correct
1206 position and resize it as needed. */
1207 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1209 dwStyle
|= WS_VISIBLE
;
1211 Create(hWndParent
, 0, NULL
, dwStyle
);
1215 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1228 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1230 #define TRAY_NOTIFY_WND_SPACING_X 2
1231 #define TRAY_NOTIFY_WND_SPACING_Y 2
1233 class CTrayNotifyWnd
:
1234 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1235 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1239 CSysPagerWnd
* m_pager
;
1240 CTrayClockWnd
* m_clock
;
1242 CComPtr
<ITrayWindow
> TrayWindow
;
1245 SIZE szTrayClockMin
;
1247 MARGINS ContentMargin
;
1254 DWORD HideClock
: 1;
1255 DWORD IsHorizontal
: 1;
1268 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1269 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1270 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1272 virtual ~CTrayNotifyWnd() { }
1274 LRESULT
OnThemeChanged()
1277 CloseThemeData(TrayTheme
);
1279 if (IsThemeActive())
1280 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1286 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1288 GetThemeMargins(TrayTheme
,
1298 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1300 ContentMargin
.cxLeftWidth
= 0;
1301 ContentMargin
.cxRightWidth
= 0;
1302 ContentMargin
.cyTopHeight
= 0;
1303 ContentMargin
.cyBottomHeight
= 0;
1309 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1311 return OnThemeChanged();
1314 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1316 m_clock
= new CTrayClockWnd();
1317 m_clock
->_Init(m_hWnd
, !HideClock
);
1319 m_pager
= new CSysPagerWnd();
1320 m_pager
->_Init(m_hWnd
, !HideClock
);
1327 BOOL
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
1329 SIZE szClock
= { 0, 0 };
1330 SIZE szTray
= { 0, 0 };
1332 IsHorizontal
= Horizontal
;
1334 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1336 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1342 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1343 if (szClock
.cy
<= 0)
1348 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1349 if (szClock
.cx
<= 0)
1353 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) Horizontal
, (LPARAM
) &szClock
);
1355 szTrayClockMin
= szClock
;
1359 szTrayClockMin
= szClock
;
1363 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1367 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1370 m_pager
->GetSize(Horizontal
, &szTray
);
1372 szTrayNotify
= szTray
;
1376 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1379 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1381 pSize
->cx
+= szTray
.cx
;
1385 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1388 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1390 pSize
->cy
+= szTray
.cy
;
1393 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1394 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1399 VOID
Size(IN
const SIZE
*pszClient
)
1408 ptClock
.x
= pszClient
->cx
- TRAY_NOTIFY_WND_SPACING_X
- szTrayClockMin
.cx
;
1409 ptClock
.y
= TRAY_NOTIFY_WND_SPACING_Y
;
1410 szClock
.cx
= szTrayClockMin
.cx
;
1411 szClock
.cy
= pszClient
->cy
- (2 * TRAY_NOTIFY_WND_SPACING_Y
);
1415 ptClock
.x
= TRAY_NOTIFY_WND_SPACING_X
;
1416 ptClock
.y
= pszClient
->cy
- TRAY_NOTIFY_WND_SPACING_Y
- szTrayClockMin
.cy
;
1417 szClock
.cx
= pszClient
->cx
- (2 * TRAY_NOTIFY_WND_SPACING_X
);
1418 szClock
.cy
= szTrayClockMin
.cy
;
1421 m_clock
->SetWindowPos(
1431 ptClock
.x
-= szTrayNotify
.cx
;
1435 ptClock
.y
-= szTrayNotify
.cy
;
1438 m_pager
->SetWindowPos(
1448 LRESULT
DrawBackground(HDC hdc
)
1453 GetClientRect(&rect
);
1457 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1459 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1462 res
= DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1468 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1470 HDC hdc
= (HDC
) wParam
;
1478 return DrawBackground(hdc
);
1481 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
1485 return m_pager
->NotifyIconCmd(wParam
, lParam
);
1491 BOOL
GetClockRect(OUT PRECT rcClock
)
1493 if (!IsWindowVisible(m_clock
->m_hWnd
))
1496 return GetWindowRect(m_clock
->m_hWnd
, rcClock
);
1499 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1501 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
);
1504 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1506 if (m_clock
!= NULL
)
1508 /* Forward the message to the tray clock window procedure */
1509 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1514 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1518 szClient
.cx
= LOWORD(lParam
);
1519 szClient
.cy
= HIWORD(lParam
);
1526 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1528 return HTTRANSPARENT
;
1531 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1533 BOOL PrevHidden
= HideClock
;
1534 HideClock
= (wParam
== 0);
1536 if (m_clock
!= NULL
&& PrevHidden
!= HideClock
)
1538 m_clock
->ShowWindow(HideClock
? SW_HIDE
: SW_SHOW
);
1541 return (LRESULT
) (!PrevHidden
);
1544 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1546 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1548 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1550 /* Pass down notifications */
1551 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1557 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1559 if (m_clock
!= NULL
)
1561 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1568 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1574 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1576 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1577 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1578 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1579 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1580 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1581 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1582 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
1583 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1584 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
1585 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1586 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
1587 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
1590 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
, IN BOOL bHideClock
)
1592 HWND hWndTrayWindow
;
1594 hWndTrayWindow
= TrayWindow
->GetHWND();
1595 if (hWndTrayWindow
== NULL
)
1598 this->TrayWindow
= TrayWindow
;
1599 this->HideClock
= bHideClock
;
1600 this->hWndNotify
= hWndTrayWindow
;
1602 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
1603 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
1607 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, BOOL bHideClock
, CTrayNotifyWnd
** ppinstance
)
1609 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
1610 // TODO: Destroy after the window is destroyed
1611 *ppinstance
= pTrayNotify
;
1613 return pTrayNotify
->_Init(Tray
, bHideClock
);
1617 TrayNotify_NotifyIconCmd(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
1619 return pTrayNotify
->NotifyIconCmd(wParam
, lParam
);
1623 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
1625 return pTrayNotify
->GetClockRect(rcClock
);