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
);
438 // Explicitly request running applications to re-register their systray icons
439 SendNotifyMessage(HWND_BROADCAST
,
440 RegisterWindowMessage(TEXT("TaskbarCreated")),
447 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
449 PCOPYDATASTRUCT cpData
= (PCOPYDATASTRUCT
) lParam
;
450 if (cpData
->dwData
== 1)
452 SYS_PAGER_COPY_DATA
* data
;
453 NOTIFYICONDATA
*iconData
;
457 parentHWND
= GetParent();
458 parentHWND
= ::GetParent(parentHWND
);
459 ::GetClientRect(parentHWND
, &windowRect
);
461 data
= (PSYS_PAGER_COPY_DATA
) cpData
->lpData
;
462 iconData
= &data
->nicon_data
;
464 TRACE("NotifyIconCmd received. Code=%d\n", data
->notify_code
);
465 switch (data
->notify_code
)
468 ret
= Toolbar
.AddButton(iconData
);
471 ret
= Toolbar
.UpdateButton(iconData
);
474 ret
= Toolbar
.RemoveButton(iconData
);
477 TRACE("NotifyIconCmd received with unknown code %d.\n", data
->notify_code
);
481 SendMessage(parentHWND
,
484 MAKELONG(windowRect
.right
- windowRect
.left
,
485 windowRect
.bottom
- windowRect
.top
));
493 void GetSize(IN WPARAM wParam
, IN PSIZE size
)
496 int VisibleButtonCount
= Toolbar
.GetVisibleButtonCount();
498 if (wParam
) /* horizontal */
500 rows
= size
->cy
/ 24;
503 size
->cx
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
507 rows
= size
->cx
/ 24;
510 size
->cy
= (VisibleButtonCount
+ rows
- 1) / rows
* 24;
514 LRESULT
OnGetInfoTip(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
516 NMTBGETINFOTIPW
* nmtip
= (NMTBGETINFOTIPW
*) hdr
;
517 Toolbar
.GetTooltipText(nmtip
->iItem
, nmtip
->pszText
, nmtip
->cchTextMax
);
521 LRESULT
OnCustomDraw(INT uCode
, LPNMHDR hdr
, BOOL
& bHandled
)
523 NMCUSTOMDRAW
* cdraw
= (NMCUSTOMDRAW
*) hdr
;
524 switch (cdraw
->dwDrawStage
)
527 return CDRF_NOTIFYITEMDRAW
;
529 case CDDS_ITEMPREPAINT
:
530 return TBCDRF_NOBACKGROUND
| TBCDRF_NOEDGES
| TBCDRF_NOOFFSET
| TBCDRF_NOMARK
| TBCDRF_NOETCHEDEFFECT
;
535 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
539 szClient
.cx
= LOWORD(lParam
);
540 szClient
.cy
= HIWORD(lParam
);
542 Ret
= DefWindowProc(uMsg
, wParam
, lParam
);
547 tbm
.cbSize
= sizeof(tbm
);
548 tbm
.dwMask
= TBMF_BARPAD
| TBMF_BUTTONSPACING
;
549 tbm
.cxBarPad
= tbm
.cyBarPad
= 0;
550 tbm
.cxButtonSpacing
= 0;
551 tbm
.cyButtonSpacing
= 0;
553 Toolbar
.SetMetrics(&tbm
);
555 Toolbar
.SetWindowPos(NULL
, 0, 0, szClient
.cx
, szClient
.cy
, SWP_NOZORDER
);
559 Toolbar
.GetClientRect(&rc
);
561 SIZE szBar
= { rc
.right
- rc
.left
, rc
.bottom
- rc
.top
};
563 INT xOff
= (szClient
.cx
- szBar
.cx
) / 2;
564 INT yOff
= (szClient
.cy
- szBar
.cy
) / 2;
566 Toolbar
.SetWindowPos(NULL
, xOff
, yOff
, szBar
.cx
, szBar
.cy
, SWP_NOZORDER
);
571 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
577 DECLARE_WND_CLASS_EX(szSysPagerWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
579 BEGIN_MSG_MAP(CTaskSwitchWnd
)
580 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
581 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
582 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
583 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
)
584 NOTIFY_CODE_HANDLER(TBN_GETINFOTIPW
, OnGetInfoTip
)
585 NOTIFY_CODE_HANDLER(NM_CUSTOMDRAW
, OnCustomDraw
)
588 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
592 /* Create the window. The tray window is going to move it to the correct
593 position and resize it as needed. */
594 dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
596 dwStyle
|= WS_VISIBLE
;
598 Create(hWndParent
, 0, NULL
, dwStyle
);
605 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
615 static const WCHAR szTrayClockWndClass
[] = TEXT("TrayClockWClass");
617 #define ID_TRAYCLOCK_TIMER 0
618 #define ID_TRAYCLOCK_TIMER_INIT 1
625 } ClockWndFormats
[] = {
627 { FALSE
, 0, TEXT("dddd") },
628 { FALSE
, DATE_SHORTDATE
, NULL
}
631 #define CLOCKWND_FORMAT_COUNT (sizeof(ClockWndFormats) / sizeof(ClockWndFormats[0]))
633 #define TRAY_CLOCK_WND_SPACING_X 0
634 #define TRAY_CLOCK_WND_SPACING_Y 0
636 class CTrayClockWnd
:
637 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
638 public CWindowImpl
< CTrayClockWnd
, CWindow
, CControlWinTraits
>
644 SYSTEMTIME LocalTime
;
651 DWORD IsTimerEnabled
: 1;
652 DWORD IsInitTimerEnabled
: 1;
653 DWORD LinesMeasured
: 1;
654 DWORD IsHorizontal
: 1;
660 SIZE LineSizes
[CLOCKWND_FORMAT_COUNT
];
661 WCHAR szLines
[CLOCKWND_FORMAT_COUNT
][48];
671 ZeroMemory(&textColor
, sizeof(textColor
));
672 ZeroMemory(&rcText
, sizeof(rcText
));
673 ZeroMemory(&LocalTime
, sizeof(LocalTime
));
674 ZeroMemory(&CurrentSize
, sizeof(CurrentSize
));
675 ZeroMemory(LineSizes
, sizeof(LineSizes
));
676 ZeroMemory(szLines
, sizeof(LineSizes
));
678 virtual ~CTrayClockWnd() { }
680 LRESULT
OnThemeChanged()
686 clockTheme
= OpenThemeData(m_hWnd
, L
"Clock");
690 GetThemeFont(clockTheme
,
697 hFont
= CreateFontIndirectW(&clockFont
);
699 GetThemeColor(clockTheme
,
707 NONCLIENTMETRICS ncm
= { 0 };
708 ncm
.cbSize
= sizeof(ncm
);
709 SystemParametersInfo(SPI_GETNONCLIENTMETRICS
, sizeof(ncm
), &ncm
, FALSE
);
711 hFont
= CreateFontIndirectW(&ncm
.lfMessageFont
);
713 textColor
= RGB(0, 0, 0);
716 SetFont(hFont
, FALSE
);
718 CloseThemeData(clockTheme
);
723 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
725 return OnThemeChanged();
739 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
741 for (i
= 0; i
!= CLOCKWND_FORMAT_COUNT
&& bRet
; i
++)
743 if (szLines
[i
][0] != TEXT('\0') &&
744 !GetTextExtentPoint(hDC
, szLines
[i
], _tcslen(szLines
[i
]),
753 SelectObject(hDC
, hPrevFont
);
755 ReleaseDC(m_hWnd
, hDC
);
761 /* calculate the line spacing */
762 for (i
= 0, c
= 0; i
!= CLOCKWND_FORMAT_COUNT
; i
++)
764 if (LineSizes
[i
].cx
> 0)
766 LineSpacing
+= LineSizes
[i
].cy
;
773 /* We want a spaceing of 1/2 line */
774 LineSpacing
= (LineSpacing
/ c
) / 2;
784 WORD
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
786 WORD iLinesVisible
= 0;
788 SIZE szMax
= { 0, 0 };
791 LinesMeasured
= MeasureLines();
797 i
!= CLOCKWND_FORMAT_COUNT
;
800 if (LineSizes
[i
].cx
!= 0)
802 if (iLinesVisible
> 0)
806 if (szMax
.cy
+ LineSizes
[i
].cy
+ (LONG
) LineSpacing
>
807 pSize
->cy
- (2 * TRAY_CLOCK_WND_SPACING_Y
))
814 if (LineSizes
[i
].cx
> pSize
->cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
818 /* Add line spacing */
819 szMax
.cy
+= LineSpacing
;
824 /* Increase maximum rectangle */
825 szMax
.cy
+= LineSizes
[i
].cy
;
826 if (LineSizes
[i
].cx
> szMax
.cx
- (2 * TRAY_CLOCK_WND_SPACING_X
))
827 szMax
.cx
= LineSizes
[i
].cx
+ (2 * TRAY_CLOCK_WND_SPACING_X
);
831 szMax
.cx
+= 2 * TRAY_CLOCK_WND_SPACING_X
;
832 szMax
.cy
+= 2 * TRAY_CLOCK_WND_SPACING_Y
;
836 return iLinesVisible
;
843 INT BufSize
, iRet
, i
;
846 ZeroMemory(LineSizes
,
849 szPrevCurrent
= CurrentSize
;
852 i
!= CLOCKWND_FORMAT_COUNT
;
855 szLines
[i
][0] = TEXT('\0');
856 BufSize
= sizeof(szLines
[0]) / sizeof(szLines
[0][0]);
858 if (ClockWndFormats
[i
].IsTime
)
860 iRet
= GetTimeFormat(LOCALE_USER_DEFAULT
,
861 AdvancedSettings
.bShowSeconds
? ClockWndFormats
[i
].dwFormatFlags
: TIME_NOSECONDS
,
863 ClockWndFormats
[i
].lpFormat
,
869 iRet
= GetDateFormat(LOCALE_USER_DEFAULT
,
870 ClockWndFormats
[i
].dwFormatFlags
,
872 ClockWndFormats
[i
].lpFormat
,
877 if (iRet
!= 0 && i
== 0)
879 /* Set the window text to the time only */
880 SetWindowText(szLines
[i
]);
884 LinesMeasured
= MeasureLines();
887 GetClientRect(&rcClient
))
891 szWnd
.cx
= rcClient
.right
;
892 szWnd
.cy
= rcClient
.bottom
;
894 VisibleLines
= GetMinimumSize(IsHorizontal
, &szWnd
);
898 if (IsWindowVisible(m_hWnd
))
900 InvalidateRect(NULL
, TRUE
);
902 if (hWndNotify
!= NULL
&&
903 (szPrevCurrent
.cx
!= CurrentSize
.cx
||
904 szPrevCurrent
.cy
!= CurrentSize
.cy
))
908 nmh
.hwndFrom
= m_hWnd
;
909 nmh
.idFrom
= GetWindowLongPtr(m_hWnd
, GWLP_ID
);
910 nmh
.code
= NTNWM_REALIGN
;
912 SendMessage(hWndNotify
,
922 GetLocalTime(&LocalTime
);
926 UINT
CalculateDueTime()
930 /* Calculate the due time */
931 GetLocalTime(&LocalTime
);
932 uiDueTime
= 1000 - (UINT
) LocalTime
.wMilliseconds
;
933 if (AdvancedSettings
.bShowSeconds
)
934 uiDueTime
+= (UINT
) LocalTime
.wSecond
* 100;
936 uiDueTime
+= (59 - (UINT
) LocalTime
.wSecond
) * 1000;
938 if (uiDueTime
< USER_TIMER_MINIMUM
|| uiDueTime
> USER_TIMER_MAXIMUM
)
942 /* Add an artificial delay of 0.05 seconds to make sure the timer
943 doesn't fire too early*/
955 /* Disable all timers */
958 KillTimer(ID_TRAYCLOCK_TIMER
);
959 IsTimerEnabled
= FALSE
;
962 if (IsInitTimerEnabled
)
964 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
967 uiDueTime
= CalculateDueTime();
969 /* Set the new timer */
970 Ret
= SetTimer(ID_TRAYCLOCK_TIMER_INIT
, uiDueTime
, NULL
) != 0;
971 IsInitTimerEnabled
= Ret
;
973 /* Update the time */
979 VOID
CalibrateTimer()
983 UINT uiWait1
, uiWait2
;
985 /* Kill the initialization timer */
986 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
987 IsInitTimerEnabled
= FALSE
;
989 uiDueTime
= CalculateDueTime();
991 if (AdvancedSettings
.bShowSeconds
)
993 uiWait1
= 1000 - 200;
998 uiWait1
= 60 * 1000 - 200;
1002 if (uiDueTime
> uiWait1
)
1004 /* The update of the clock will be up to 200 ms late, but that's
1005 acceptable. We're going to setup a timer that fires depending
1007 Ret
= SetTimer(ID_TRAYCLOCK_TIMER
, uiWait2
, NULL
) != 0;
1008 IsTimerEnabled
= Ret
;
1010 /* Update the time */
1015 /* Recalibrate the timer and recalculate again when the current
1016 minute/second ends. */
1021 LRESULT
OnDestroy(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1023 /* Disable all timers */
1026 KillTimer(ID_TRAYCLOCK_TIMER
);
1029 if (IsInitTimerEnabled
)
1031 KillTimer(ID_TRAYCLOCK_TIMER_INIT
);
1037 LRESULT
OnPaint(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1041 int iPrevBkMode
, i
, line
;
1044 HDC hDC
= (HDC
) wParam
;
1048 hDC
= BeginPaint(&ps
);
1054 if (LinesMeasured
&&
1055 GetClientRect(&rcClient
))
1057 iPrevBkMode
= SetBkMode(hDC
, TRANSPARENT
);
1059 SetTextColor(hDC
, textColor
);
1061 hPrevFont
= (HFONT
) SelectObject(hDC
, hFont
);
1063 rcClient
.left
= (rcClient
.right
/ 2) - (CurrentSize
.cx
/ 2);
1064 rcClient
.top
= (rcClient
.bottom
/ 2) - (CurrentSize
.cy
/ 2);
1065 rcClient
.right
= rcClient
.left
+ CurrentSize
.cx
;
1066 rcClient
.bottom
= rcClient
.top
+ CurrentSize
.cy
;
1068 for (i
= 0, line
= 0;
1069 i
!= CLOCKWND_FORMAT_COUNT
&& line
< VisibleLines
;
1072 if (LineSizes
[i
].cx
!= 0)
1075 rcClient
.left
+ (CurrentSize
.cx
/ 2) - (LineSizes
[i
].cx
/ 2) +
1076 TRAY_CLOCK_WND_SPACING_X
,
1077 rcClient
.top
+ TRAY_CLOCK_WND_SPACING_Y
,
1079 _tcslen(szLines
[i
]));
1081 rcClient
.top
+= LineSizes
[i
].cy
+ LineSpacing
;
1086 SelectObject(hDC
, hPrevFont
);
1088 SetBkMode(hDC
, iPrevBkMode
);
1099 VOID
SetFont(IN HFONT hNewFont
, IN BOOL bRedraw
)
1102 LinesMeasured
= MeasureLines();
1105 InvalidateRect(NULL
, TRUE
);
1109 LRESULT
DrawBackground(HDC hdc
)
1113 GetClientRect(&rect
);
1114 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1119 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1121 HDC hdc
= (HDC
) wParam
;
1129 return DrawBackground(hdc
);
1132 LRESULT
OnTimer(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1136 case ID_TRAYCLOCK_TIMER
:
1140 case ID_TRAYCLOCK_TIMER_INIT
:
1147 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1149 IsHorizontal
= (BOOL
) wParam
;
1151 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
) != 0;
1154 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1156 return (LRESULT
) ResetTime();
1159 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1161 return HTTRANSPARENT
;
1164 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1166 SetFont((HFONT
) wParam
, (BOOL
) LOWORD(lParam
));
1170 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1176 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1180 szClient
.cx
= LOWORD(lParam
);
1181 szClient
.cy
= HIWORD(lParam
);
1183 VisibleLines
= GetMinimumSize(IsHorizontal
, &szClient
);
1184 CurrentSize
= szClient
;
1186 InvalidateRect(NULL
, TRUE
);
1190 DECLARE_WND_CLASS_EX(szTrayClockWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1192 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1193 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1194 MESSAGE_HANDLER(WM_DESTROY
, OnDestroy
)
1195 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1196 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1197 MESSAGE_HANDLER(WM_PAINT
, OnPaint
)
1198 MESSAGE_HANDLER(WM_PRINTCLIENT
, OnPaint
)
1199 MESSAGE_HANDLER(WM_TIMER
, OnTimer
)
1200 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1201 MESSAGE_HANDLER(TCWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1202 MESSAGE_HANDLER(TCWM_UPDATETIME
, OnUpdateTime
)
1206 HWND
_Init(IN HWND hWndParent
, IN BOOL bVisible
)
1208 IsHorizontal
= TRUE
;
1210 hWndNotify
= hWndParent
;
1212 /* Create the window. The tray window is going to move it to the correct
1213 position and resize it as needed. */
1214 DWORD dwStyle
= WS_CHILD
| WS_CLIPSIBLINGS
;
1216 dwStyle
|= WS_VISIBLE
;
1218 Create(hWndParent
, 0, NULL
, dwStyle
);
1222 SetWindowTheme(m_hWnd
, L
"TrayNotify", NULL
);
1235 static const WCHAR szTrayNotifyWndClass
[] = TEXT("TrayNotifyWnd");
1237 #define TRAY_NOTIFY_WND_SPACING_X 2
1238 #define TRAY_NOTIFY_WND_SPACING_Y 2
1240 class CTrayNotifyWnd
:
1241 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
1242 public CWindowImpl
< CTrayNotifyWnd
, CWindow
, CControlWinTraits
>
1246 CSysPagerWnd
* m_pager
;
1247 CTrayClockWnd
* m_clock
;
1249 CComPtr
<ITrayWindow
> TrayWindow
;
1252 SIZE szTrayClockMin
;
1254 MARGINS ContentMargin
;
1261 DWORD HideClock
: 1;
1262 DWORD IsHorizontal
: 1;
1275 ZeroMemory(&szTrayClockMin
, sizeof(szTrayClockMin
));
1276 ZeroMemory(&szTrayNotify
, sizeof(szTrayNotify
));
1277 ZeroMemory(&ContentMargin
, sizeof(ContentMargin
));
1279 virtual ~CTrayNotifyWnd() { }
1281 LRESULT
OnThemeChanged()
1284 CloseThemeData(TrayTheme
);
1286 if (IsThemeActive())
1287 TrayTheme
= OpenThemeData(m_hWnd
, L
"TrayNotify");
1293 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, 0);
1295 GetThemeMargins(TrayTheme
,
1305 SetWindowExStyle(m_hWnd
, WS_EX_STATICEDGE
, WS_EX_STATICEDGE
);
1307 ContentMargin
.cxLeftWidth
= 0;
1308 ContentMargin
.cxRightWidth
= 0;
1309 ContentMargin
.cyTopHeight
= 0;
1310 ContentMargin
.cyBottomHeight
= 0;
1316 LRESULT
OnThemeChanged(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1318 return OnThemeChanged();
1321 LRESULT
OnCreate(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1323 m_clock
= new CTrayClockWnd();
1324 m_clock
->_Init(m_hWnd
, !HideClock
);
1326 m_pager
= new CSysPagerWnd();
1327 m_pager
->_Init(m_hWnd
, !HideClock
);
1334 BOOL
GetMinimumSize(IN BOOL Horizontal
, IN OUT PSIZE pSize
)
1336 SIZE szClock
= { 0, 0 };
1337 SIZE szTray
= { 0, 0 };
1339 IsHorizontal
= Horizontal
;
1341 SetWindowTheme(m_hWnd
, L
"TrayNotifyHoriz", NULL
);
1343 SetWindowTheme(m_hWnd
, L
"TrayNotifyVert", NULL
);
1349 szClock
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1350 if (szClock
.cy
<= 0)
1355 szClock
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1356 if (szClock
.cx
<= 0)
1360 m_clock
->SendMessage(TCWM_GETMINIMUMSIZE
, (WPARAM
) Horizontal
, (LPARAM
) &szClock
);
1362 szTrayClockMin
= szClock
;
1366 szTrayClockMin
= szClock
;
1370 szTray
.cy
= pSize
->cy
- 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1374 szTray
.cx
= pSize
->cx
- 2 * TRAY_NOTIFY_WND_SPACING_X
;
1377 m_pager
->GetSize(Horizontal
, &szTray
);
1379 szTrayNotify
= szTray
;
1383 pSize
->cx
= 2 * TRAY_NOTIFY_WND_SPACING_X
;
1386 pSize
->cx
+= TRAY_NOTIFY_WND_SPACING_X
+ szTrayClockMin
.cx
;
1388 pSize
->cx
+= szTray
.cx
;
1392 pSize
->cy
= 2 * TRAY_NOTIFY_WND_SPACING_Y
;
1395 pSize
->cy
+= TRAY_NOTIFY_WND_SPACING_Y
+ szTrayClockMin
.cy
;
1397 pSize
->cy
+= szTray
.cy
;
1400 pSize
->cy
+= ContentMargin
.cyTopHeight
+ ContentMargin
.cyBottomHeight
;
1401 pSize
->cx
+= ContentMargin
.cxLeftWidth
+ ContentMargin
.cxRightWidth
;
1406 VOID
Size(IN
const SIZE
*pszClient
)
1415 ptClock
.x
= pszClient
->cx
- TRAY_NOTIFY_WND_SPACING_X
- szTrayClockMin
.cx
;
1416 ptClock
.y
= TRAY_NOTIFY_WND_SPACING_Y
;
1417 szClock
.cx
= szTrayClockMin
.cx
;
1418 szClock
.cy
= pszClient
->cy
- (2 * TRAY_NOTIFY_WND_SPACING_Y
);
1422 ptClock
.x
= TRAY_NOTIFY_WND_SPACING_X
;
1423 ptClock
.y
= pszClient
->cy
- TRAY_NOTIFY_WND_SPACING_Y
- szTrayClockMin
.cy
;
1424 szClock
.cx
= pszClient
->cx
- (2 * TRAY_NOTIFY_WND_SPACING_X
);
1425 szClock
.cy
= szTrayClockMin
.cy
;
1428 m_clock
->SetWindowPos(
1438 ptClock
.x
-= szTrayNotify
.cx
;
1442 ptClock
.y
-= szTrayNotify
.cy
;
1445 m_pager
->SetWindowPos(
1455 LRESULT
DrawBackground(HDC hdc
)
1460 GetClientRect(&rect
);
1464 if (IsThemeBackgroundPartiallyTransparent(TrayTheme
, TNP_BACKGROUND
, 0))
1466 DrawThemeParentBackground(m_hWnd
, hdc
, &rect
);
1469 res
= DrawThemeBackground(TrayTheme
, hdc
, TNP_BACKGROUND
, 0, &rect
, 0);
1475 LRESULT
OnEraseBackground(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1477 HDC hdc
= (HDC
) wParam
;
1485 return DrawBackground(hdc
);
1488 BOOL
NotifyIconCmd(WPARAM wParam
, LPARAM lParam
)
1492 return m_pager
->NotifyIconCmd(wParam
, lParam
);
1498 BOOL
GetClockRect(OUT PRECT rcClock
)
1500 if (!IsWindowVisible(m_clock
->m_hWnd
))
1503 return GetWindowRect(m_clock
->m_hWnd
, rcClock
);
1506 LRESULT
OnGetMinimumSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1508 return (LRESULT
) GetMinimumSize((BOOL
) wParam
, (PSIZE
) lParam
);
1511 LRESULT
OnUpdateTime(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1513 if (m_clock
!= NULL
)
1515 /* Forward the message to the tray clock window procedure */
1516 return m_clock
->OnUpdateTime(uMsg
, wParam
, lParam
, bHandled
);
1521 LRESULT
OnSize(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1525 szClient
.cx
= LOWORD(lParam
);
1526 szClient
.cy
= HIWORD(lParam
);
1533 LRESULT
OnNcHitTest(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1535 return HTTRANSPARENT
;
1538 LRESULT
OnShowClock(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1540 BOOL PrevHidden
= HideClock
;
1541 HideClock
= (wParam
== 0);
1543 if (m_clock
!= NULL
&& PrevHidden
!= HideClock
)
1545 m_clock
->ShowWindow(HideClock
? SW_HIDE
: SW_SHOW
);
1548 return (LRESULT
) (!PrevHidden
);
1551 LRESULT
OnNotify(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1553 const NMHDR
*nmh
= (const NMHDR
*) lParam
;
1555 if (nmh
->hwndFrom
== m_clock
->m_hWnd
)
1557 /* Pass down notifications */
1558 return m_clock
->SendMessage(WM_NOTIFY
, wParam
, lParam
);
1564 LRESULT
OnSetFont(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1566 if (m_clock
!= NULL
)
1568 m_clock
->SendMessageW(WM_SETFONT
, wParam
, lParam
);
1575 LRESULT
OnCtxMenu(UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL
& bHandled
)
1581 DECLARE_WND_CLASS_EX(szTrayNotifyWndClass
, CS_DBLCLKS
, COLOR_3DFACE
)
1583 BEGIN_MSG_MAP(CTaskSwitchWnd
)
1584 MESSAGE_HANDLER(WM_CREATE
, OnCreate
)
1585 MESSAGE_HANDLER(WM_THEMECHANGED
, OnThemeChanged
)
1586 MESSAGE_HANDLER(WM_ERASEBKGND
, OnEraseBackground
)
1587 MESSAGE_HANDLER(WM_SIZE
, OnSize
)
1588 MESSAGE_HANDLER(WM_NCHITTEST
, OnNcHitTest
)
1589 MESSAGE_HANDLER(WM_NOTIFY
, OnNotify
)
1590 MESSAGE_HANDLER(WM_SETFONT
, OnSetFont
)
1591 MESSAGE_HANDLER(WM_CONTEXTMENU
, OnCtxMenu
) // FIXME: This handler is not necessary in Windows
1592 MESSAGE_HANDLER(TNWM_GETMINIMUMSIZE
, OnGetMinimumSize
)
1593 MESSAGE_HANDLER(TNWM_UPDATETIME
, OnUpdateTime
)
1594 MESSAGE_HANDLER(TNWM_SHOWCLOCK
, OnShowClock
)
1597 HWND
_Init(IN OUT ITrayWindow
*TrayWindow
, IN BOOL bHideClock
)
1599 HWND hWndTrayWindow
;
1601 hWndTrayWindow
= TrayWindow
->GetHWND();
1602 if (hWndTrayWindow
== NULL
)
1605 this->TrayWindow
= TrayWindow
;
1606 this->HideClock
= bHideClock
;
1607 this->hWndNotify
= hWndTrayWindow
;
1609 DWORD dwStyle
= WS_CHILD
| WS_VISIBLE
| WS_CLIPSIBLINGS
| WS_CLIPCHILDREN
;
1610 return Create(hWndTrayWindow
, 0, NULL
, dwStyle
, WS_EX_STATICEDGE
);
1614 HWND
CreateTrayNotifyWnd(IN OUT ITrayWindow
*Tray
, BOOL bHideClock
, CTrayNotifyWnd
** ppinstance
)
1616 CTrayNotifyWnd
* pTrayNotify
= new CTrayNotifyWnd();
1617 // TODO: Destroy after the window is destroyed
1618 *ppinstance
= pTrayNotify
;
1620 return pTrayNotify
->_Init(Tray
, bHideClock
);
1624 TrayNotify_NotifyIconCmd(CTrayNotifyWnd
* pTrayNotify
, WPARAM wParam
, LPARAM lParam
)
1626 return pTrayNotify
->NotifyIconCmd(wParam
, lParam
);
1630 TrayNotify_GetClockRect(CTrayNotifyWnd
* pTrayNotify
, OUT PRECT rcClock
)
1632 return pTrayNotify
->GetClockRect(rcClock
);