2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: win32ss/user/user32/controls/appswitch.c
5 * PURPOSE: app switching functionality
6 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald@reactos.org)
7 * David Quintana (gigaherz@gmail.com)
8 * Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
14 // Add registry support.
20 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
22 #define DIALOG_MARGIN 8 // margin of dialog contents
24 #define CX_ICON 32 // width of icon
25 #define CY_ICON 32 // height of icon
26 #define ICON_MARGIN 4 // margin width around an icon
28 #define CX_ITEM (CX_ICON + 2 * ICON_MARGIN)
29 #define CY_ITEM (CY_ICON + 2 * ICON_MARGIN)
30 #define ITEM_MARGIN 4 // margin width around an item
32 #define CX_ITEM_SPACE (CX_ITEM + 2 * ITEM_MARGIN)
33 #define CY_ITEM_SPACE (CY_ITEM + 2 * ITEM_MARGIN)
35 #define CY_TEXT_MARGIN 4 // margin height around text
37 // limit the number of windows shown in the alt-tab window
38 // 120 windows results in (12*40) by (10*40) pixels worth of icons.
39 #define MAX_WINDOWS 120
42 HWND switchdialog
= NULL
;
44 int selectedWindow
= 0;
49 WCHAR windowText
[1024];
51 HWND windowList
[MAX_WINDOWS
];
52 HICON iconList
[MAX_WINDOWS
];
55 int cxBorder
, cyBorder
;
56 int nItems
, nCols
, nRows
;
66 BOOL CoolSwitch
= TRUE
;
67 int CoolSwitchRows
= 3;
68 int CoolSwitchColumns
= 7;
71 const DWORD Style
= WS_POPUP
| WS_BORDER
| WS_DISABLED
;
72 const DWORD ExStyle
= WS_EX_TOPMOST
| WS_EX_DLGMODALFRAME
| WS_EX_TOOLWINDOW
;
74 DWORD
wtodw(const WCHAR
*psz
)
76 const WCHAR
*pch
= psz
;
78 while ('0' <= *pch
&& *pch
<= '9')
86 BOOL
LoadCoolSwitchSettings(void)
90 CoolSwitchColumns
= 7;
92 // FIXME: load the settings from registry
94 TRACE("CoolSwitch: %d\n", CoolSwitch
);
95 TRACE("CoolSwitchRows: %d\n", CoolSwitchRows
);
96 TRACE("CoolSwitchColumns: %d\n", CoolSwitchColumns
);
101 void ResizeAndCenter(HWND hwnd
, int width
, int height
)
106 int screenwidth
= GetSystemMetrics(SM_CXSCREEN
);
107 int screenheight
= GetSystemMetrics(SM_CYSCREEN
);
109 x
= (screenwidth
- width
) / 2;
110 y
= (screenheight
- height
) / 2;
112 SetRect(&Rect
, x
, y
, x
+ width
, y
+ height
);
113 AdjustWindowRectEx(&Rect
, Style
, FALSE
, ExStyle
);
117 width
= Rect
.right
- Rect
.left
;
118 height
= Rect
.bottom
- Rect
.top
;
119 MoveWindow(hwnd
, x
, y
, width
, height
, FALSE
);
125 void MakeWindowActive(HWND hwnd
)
128 PostMessageW(hwnd
, WM_SYSCOMMAND
, SC_RESTORE
, 0);
130 BringWindowToTop(hwnd
); // same as: SetWindowPos(hwnd,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); ?
131 SetForegroundWindow(hwnd
);
134 void CompleteSwitch(BOOL doSwitch
)
141 TRACE("[ATbot] CompleteSwitch Hiding Window.\n");
142 ShowWindow(switchdialog
, SW_HIDE
);
146 if(selectedWindow
>= windowCount
)
149 // FIXME: workaround because reactos fails to activate the previous window correctly.
150 //if(selectedWindow != 0)
152 HWND hwnd
= windowList
[selectedWindow
];
154 GetWindowTextW(hwnd
, windowText
, _countof(windowText
));
156 TRACE("[ATbot] CompleteSwitch Switching to 0x%08x (%ls)\n", hwnd
, windowText
);
158 MakeWindowActive(hwnd
);
165 BOOL CALLBACK
EnumerateCallback(HWND window
, LPARAM lParam
)
168 HWND hwndIcon
, hwndOwner
;
170 UNREFERENCED_PARAMETER(lParam
);
172 hwndOwner
= GetWindow(window
, GW_OWNER
);
173 hwndIcon
= (hwndOwner
? hwndOwner
: window
);
175 // First try to get the big icon assigned to the window
176 hIcon
= (HICON
)SendMessageW(hwndIcon
, WM_GETICON
, ICON_BIG
, 0);
179 // If no icon is assigned, try to get the icon assigned to the windows' class
180 hIcon
= (HICON
)GetClassLongPtrW(hwndIcon
, GCL_HICON
);
183 // If we still don't have an icon, see if we can do with the small icon,
184 // or a default application icon
185 hIcon
= (HICON
)SendMessageW(hwndIcon
, WM_GETICON
, ICON_SMALL2
, 0);
188 // using windows logo icon as default
189 hIcon
= gpsi
->hIconWindows
;
192 //if all attempts to get icon fails go to the next window
199 windowList
[windowCount
] = window
;
200 iconList
[windowCount
] = CopyIcon(hIcon
);
204 // If we got to the max number of windows,
205 // we won't be able to add any more
206 if(windowCount
>= MAX_WINDOWS
)
212 // Function mostly compatible with the normal EnumChildWindows,
213 // except it lists in Z-Order and it doesn't ensure consistency
214 // if a window is removed while enumerating
215 void EnumWindowsZOrder(WNDENUMPROC callback
, LPARAM lParam
)
217 HWND hwnd
, hwndOwner
;
221 for (hwnd
= GetTopWindow(NULL
); hwnd
; hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
))
223 if (!IsWindowVisible(hwnd
))
226 // check special windows
227 if (!GetClassNameW(hwnd
, szClass
, _countof(szClass
)) ||
228 wcscmp(szClass
, L
"Shell_TrayWnd") == 0 ||
229 wcscmp(szClass
, L
"Progman") == 0)
234 ExStyle
= GetWindowLongPtrW(hwnd
, GWL_EXSTYLE
);
235 if (ExStyle
& WS_EX_TOOLWINDOW
)
238 hwndOwner
= GetWindow(hwnd
, GW_OWNER
);
239 if ((ExStyle
& WS_EX_APPWINDOW
) || !IsWindowVisible(hwndOwner
))
241 if (!callback(hwnd
, lParam
))
249 void ProcessMouseMessage(UINT message
, LPARAM lParam
)
251 int xPos
= LOWORD(lParam
);
252 int yPos
= HIWORD(lParam
);
254 int xIndex
= (xPos
- DIALOG_MARGIN
) / CX_ITEM_SPACE
;
255 int yIndex
= (yPos
- DIALOG_MARGIN
) / CY_ITEM_SPACE
;
257 if (xIndex
< 0 || nCols
<= xIndex
||
258 yIndex
< 0 || nRows
<= yIndex
)
263 selectedWindow
= (yIndex
*nCols
) + xIndex
;
264 if (message
== WM_MOUSEMOVE
)
266 InvalidateRect(switchdialog
, NULL
, TRUE
);
267 //RedrawWindow(switchdialog, NULL, NULL, 0);
271 selectedWindow
= (yIndex
*nCols
) + xIndex
;
272 CompleteSwitch(TRUE
);
276 void OnPaint(HWND hWnd
)
281 int i
, xPos
, yPos
, CharCount
;
288 if (nCols
== 0 || nItems
== 0)
292 dialogDC
= BeginPaint(hWnd
, &paint
);
293 if (dialogDC
== NULL
)
296 // fill the client area
297 GetClientRect(hWnd
, &cRC
);
298 FillRect(dialogDC
, &cRC
, (HBRUSH
)(COLOR_3DFACE
+ 1));
300 // if the selection index exceeded the display items, then
301 // do display item shifting
302 if (selectedWindow
>= nItems
)
303 nShift
= selectedWindow
- nItems
+ 1;
307 for (i
= 0; i
< nItems
; ++i
)
309 // get the icon to display
310 hIcon
= iconList
[i
+ nShift
];
312 // calculate the position where we start drawing
313 xPos
= DIALOG_MARGIN
+ CX_ITEM_SPACE
* (i
% nCols
) + ITEM_MARGIN
;
314 yPos
= DIALOG_MARGIN
+ CY_ITEM_SPACE
* (i
/ nCols
) + ITEM_MARGIN
;
317 if (nItems
< CoolSwitchColumns
)
319 xPos
+= (itemsW
- nItems
* CX_ITEM_SPACE
) / 2;
322 // if this position is selected,
323 if (selectedWindow
== i
+ nShift
)
325 // create a solid pen
326 Color
= GetSysColor(COLOR_HIGHLIGHT
);
327 hPen
= CreatePen(PS_SOLID
, 1, Color
);
329 // draw a rectangle with using the pen
330 SelectObject(dialogDC
, hPen
);
331 SelectObject(dialogDC
, GetStockObject(NULL_BRUSH
));
332 Rectangle(dialogDC
, xPos
, yPos
, xPos
+ CX_ITEM
, yPos
+ CY_ITEM
);
333 Rectangle(dialogDC
, xPos
+ 1, yPos
+ 1,
334 xPos
+ CX_ITEM
- 1, yPos
+ CY_ITEM
- 1);
341 DrawIconEx(dialogDC
, xPos
+ ICON_MARGIN
, yPos
+ ICON_MARGIN
,
342 hIcon
, CX_ICON
, CY_ICON
, 0, NULL
, DI_NORMAL
);
345 // set the text rectangle
346 SetRect(&textRC
, DIALOG_MARGIN
, DIALOG_MARGIN
+ itemsH
,
347 totalW
- DIALOG_MARGIN
, totalH
- DIALOG_MARGIN
);
349 // draw the sunken button around text
350 DrawFrameControl(dialogDC
, &textRC
, DFC_BUTTON
,
351 DFCS_BUTTONPUSH
| DFCS_PUSHED
);
354 CharCount
= GetWindowTextW(windowList
[selectedWindow
], windowText
,
355 _countof(windowText
));
358 dcFont
= SelectObject(dialogDC
, dialogFont
);
359 SetTextColor(dialogDC
, GetSysColor(COLOR_BTNTEXT
));
360 SetBkMode(dialogDC
, TRANSPARENT
);
361 DrawTextW(dialogDC
, windowText
, CharCount
, &textRC
,
362 DT_CENTER
| DT_VCENTER
| DT_END_ELLIPSIS
| DT_SINGLELINE
);
363 SelectObject(dialogDC
, dcFont
);
366 EndPaint(hWnd
, &paint
);
369 DWORD
CreateSwitcherWindow(HINSTANCE hInstance
)
371 switchdialog
= CreateWindowExW( WS_EX_TOPMOST
|WS_EX_DLGMODALFRAME
|WS_EX_TOOLWINDOW
,
374 WS_POPUP
|WS_BORDER
|WS_DISABLED
,
382 TRACE("[ATbot] Task Switcher Window failed to create.\n");
390 DWORD
GetDialogFont(VOID
)
395 dialogFont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
398 GetTextMetrics(tDC
, &tm
);
399 fontHeight
= tm
.tmHeight
;
405 void PrepareWindow(VOID
)
407 nItems
= windowCount
;
409 nCols
= CoolSwitchColumns
;
410 nRows
= (nItems
+ CoolSwitchColumns
- 1) / CoolSwitchColumns
;
411 if (nRows
> CoolSwitchRows
)
413 nRows
= CoolSwitchRows
;
414 nItems
= nRows
* nCols
;
417 itemsW
= nCols
* CX_ITEM_SPACE
;
418 itemsH
= nRows
* CY_ITEM_SPACE
;
420 totalW
= itemsW
+ 2 * DIALOG_MARGIN
;
421 totalH
= itemsH
+ 2 * DIALOG_MARGIN
;
422 totalH
+= fontHeight
+ 2 * CY_TEXT_MARGIN
;
424 ResizeAndCenter(switchdialog
, totalW
, totalH
);
427 BOOL
ProcessHotKey(VOID
)
432 EnumWindowsZOrder(EnumerateCallback
, 0);
434 if (windowCount
== 0)
437 if (windowCount
== 1)
440 CompleteSwitch(TRUE
);
446 TRACE("[ATbot] HotKey Received. Opening window.\n");
447 ShowWindow(switchdialog
, SW_SHOWNORMAL
);
448 MakeWindowActive(switchdialog
);
453 TRACE("[ATbot] HotKey Received Rotating.\n");
454 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
455 InvalidateRect(switchdialog
, NULL
, TRUE
);
460 void RotateTasks(BOOL bShift
)
462 HWND hwndFirst
, hwndLast
;
465 if (windowCount
< 2 || !Esc
)
468 hwndFirst
= windowList
[0];
469 hwndLast
= windowList
[windowCount
- 1];
473 SetWindowPos(hwndLast
, HWND_TOP
, 0, 0, 0, 0,
474 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
475 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
477 MakeWindowActive(hwndLast
);
479 Size
= (windowCount
- 1) * sizeof(HWND
);
480 MoveMemory(&windowList
[1], &windowList
[0], Size
);
481 windowList
[0] = hwndLast
;
485 SetWindowPos(hwndFirst
, hwndLast
, 0, 0, 0, 0,
486 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
487 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
489 MakeWindowActive(windowList
[1]);
491 Size
= (windowCount
- 1) * sizeof(HWND
);
492 MoveMemory(&windowList
[0], &windowList
[1], Size
);
493 windowList
[windowCount
- 1] = hwndFirst
;
497 static void MoveLeft(void)
499 selectedWindow
= selectedWindow
- 1;
500 if (selectedWindow
< 0)
501 selectedWindow
= windowCount
- 1;
502 InvalidateRect(switchdialog
, NULL
, TRUE
);
505 static void MoveRight(void)
507 selectedWindow
= (selectedWindow
+ 1) % windowCount
;
508 InvalidateRect(switchdialog
, NULL
, TRUE
);
511 static void MoveUp(void)
513 INT iRow
= selectedWindow
/ nCols
;
514 INT iCol
= selectedWindow
% nCols
;
520 selectedWindow
= iRow
* nCols
+ iCol
;
521 if (selectedWindow
>= windowCount
)
522 selectedWindow
= windowCount
- 1;
523 InvalidateRect(switchdialog
, NULL
, TRUE
);
526 static void MoveDown(void)
528 INT iRow
= selectedWindow
/ nCols
;
529 INT iCol
= selectedWindow
% nCols
;
535 selectedWindow
= iRow
* nCols
+ iCol
;
536 if (selectedWindow
>= windowCount
)
537 selectedWindow
= windowCount
- 1;
538 InvalidateRect(switchdialog
, NULL
, TRUE
);
542 DestroyAppWindows(VOID
)
544 // for every item of the icon list:
546 for (i
= 0; i
< windowCount
; ++i
)
549 DestroyIcon(iconList
[i
]);
554 LRESULT WINAPI
DoAppSwitch( WPARAM wParam
, LPARAM lParam
)
559 // FIXME: Is loading timing OK?
560 LoadCoolSwitchSettings();
565 // Already in the loop.
566 if (switchdialog
|| Esc
) return 0;
568 hwndActive
= GetActiveWindow();
569 // Nothing is active so exit.
570 if (!hwndActive
) return 0;
572 if (lParam
== VK_ESCAPE
)
577 EnumWindowsZOrder(EnumerateCallback
, 0);
582 RotateTasks(GetAsyncKeyState(VK_SHIFT
) < 0);
584 hwndActive
= GetActiveWindow();
586 if (hwndActive
== NULL
)
593 // Capture current active window.
594 SetCapture( hwndActive
);
599 if( !CreateSwitcherWindow(User32Instance
) ) goto Exit
;
600 if( !GetDialogFont() ) goto Exit
;
601 if( !ProcessHotKey() ) goto Exit
;
610 // Main message loop:
615 if (PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
))
617 if (!CallMsgFilterW( &msg
, MSGF_NEXTWINDOW
)) break;
618 /* remove the message from the queue */
619 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
629 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
630 if (msg
.wParam
== VK_MENU
)
632 CompleteSwitch(TRUE
);
634 else if (msg
.wParam
== VK_RETURN
)
636 CompleteSwitch(TRUE
);
638 else if (msg
.wParam
== VK_ESCAPE
)
640 TRACE("DoAppSwitch VK_ESCAPE 2\n");
641 CompleteSwitch(FALSE
);
648 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
649 if (HIWORD(msg
.lParam
) & KF_ALTDOWN
)
651 if ( msg
.wParam
== VK_TAB
)
654 if (GetKeyState(VK_SHIFT
) < 0)
663 else if ( msg
.wParam
== VK_ESCAPE
)
666 RotateTasks(GetKeyState(VK_SHIFT
) < 0);
668 else if ( msg
.wParam
== VK_LEFT
)
672 else if ( msg
.wParam
== VK_RIGHT
)
676 else if ( msg
.wParam
== VK_UP
)
680 else if ( msg
.wParam
== VK_DOWN
)
689 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
690 ProcessMouseMessage(msg
.message
, msg
.lParam
);
694 if (PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
))
696 TranslateMessage(&msg
);
697 DispatchMessageW(&msg
);
704 if (switchdialog
) DestroyWindow(switchdialog
);
705 if (Esc
) DestroyAppWindows();
714 // Switch System Class Window Proc.
716 LRESULT WINAPI
SwitchWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
720 pWnd
= ValidateHwnd(hWnd
);
725 NtUserSetWindowFNID(hWnd
, FNID_SWITCH
);
732 if (!(ati
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ati
))))
734 SetWindowLongPtrW( hWnd
, 0, (LONG_PTR
)ati
);
741 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
742 ati
->cbSize
= sizeof(ALTTABINFO
);
743 ati
->cItems
= nItems
;
744 ati
->cColumns
= nCols
;
748 ati
->iColFocus
= (selectedWindow
- nShift
) % nCols
;
749 ati
->iRowFocus
= (selectedWindow
- nShift
) / nCols
;
756 ati
->cxItem
= CX_ITEM_SPACE
;
757 ati
->cyItem
= CY_ITEM_SPACE
;
758 ati
->ptStart
= ptStart
;
763 ProcessMouseMessage(uMsg
, lParam
);
767 if (wParam
== WA_INACTIVE
)
769 CompleteSwitch(FALSE
);
779 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
780 HeapFree( GetProcessHeap(), 0, ati
);
781 SetWindowLongPtrW( hWnd
, 0, 0 );
783 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
786 return DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
789 LRESULT WINAPI
SwitchWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
791 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
794 LRESULT WINAPI
SwitchWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
796 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);