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
)
170 UNREFERENCED_PARAMETER(lParam
);
172 if (!IsWindowVisible(window
))
175 hwndOwner
= GetWindow(window
, GW_OWNER
);
176 if (hwndOwner
&& IsWindowVisible(hwndOwner
))
179 GetClassNameW(window
, windowText
, _countof(windowText
));
180 if ((wcscmp(L
"Shell_TrayWnd", windowText
)==0) ||
181 (wcscmp(L
"Progman", windowText
)==0) )
184 // First try to get the big icon assigned to the window
185 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_BIG
, 0);
188 // If no icon is assigned, try to get the icon assigned to the windows' class
189 hIcon
= (HICON
)GetClassLongPtrW(window
, GCL_HICON
);
192 // If we still don't have an icon, see if we can do with the small icon,
193 // or a default application icon
194 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_SMALL2
, 0);
197 // using windows logo icon as default
198 hIcon
= gpsi
->hIconWindows
;
201 //if all attempts to get icon fails go to the next window
208 windowList
[windowCount
] = window
;
209 iconList
[windowCount
] = CopyIcon(hIcon
);
213 // If we got to the max number of windows,
214 // we won't be able to add any more
215 if(windowCount
>= MAX_WINDOWS
)
221 // Function mostly compatible with the normal EnumChildWindows,
222 // except it lists in Z-Order and it doesn't ensure consistency
223 // if a window is removed while enumerating
224 void EnumChildWindowsZOrder(HWND hwnd
, WNDENUMPROC callback
, LPARAM lParam
)
226 HWND next
= GetTopWindow(hwnd
);
229 if (!hwnd
&& !IsWindowVisible(next
))
231 // UPDATE: Seek also the owned windows of the hidden top-level window.
232 EnumChildWindowsZOrder(next
, callback
, lParam
);
235 if (!callback(next
, lParam
))
238 next
= GetWindow(next
, GW_HWNDNEXT
);
242 void ProcessMouseMessage(UINT message
, LPARAM lParam
)
244 int xPos
= LOWORD(lParam
);
245 int yPos
= HIWORD(lParam
);
247 int xIndex
= (xPos
- DIALOG_MARGIN
) / CX_ITEM_SPACE
;
248 int yIndex
= (yPos
- DIALOG_MARGIN
) / CY_ITEM_SPACE
;
250 if (xIndex
< 0 || nCols
<= xIndex
||
251 yIndex
< 0 || nRows
<= yIndex
)
256 selectedWindow
= (yIndex
*nCols
) + xIndex
;
257 if (message
== WM_MOUSEMOVE
)
259 InvalidateRect(switchdialog
, NULL
, TRUE
);
260 //RedrawWindow(switchdialog, NULL, NULL, 0);
264 selectedWindow
= (yIndex
*nCols
) + xIndex
;
265 CompleteSwitch(TRUE
);
269 void OnPaint(HWND hWnd
)
274 int i
, xPos
, yPos
, CharCount
;
281 if (nCols
== 0 || nItems
== 0)
285 dialogDC
= BeginPaint(hWnd
, &paint
);
286 if (dialogDC
== NULL
)
289 // fill the client area
290 GetClientRect(hWnd
, &cRC
);
291 FillRect(dialogDC
, &cRC
, (HBRUSH
)(COLOR_3DFACE
+ 1));
293 // if the selection index exceeded the display items, then
294 // do display item shifting
295 if (selectedWindow
>= nItems
)
296 nShift
= selectedWindow
- nItems
+ 1;
300 for (i
= 0; i
< nItems
; ++i
)
302 // get the icon to display
303 hIcon
= iconList
[i
+ nShift
];
305 // calculate the position where we start drawing
306 xPos
= DIALOG_MARGIN
+ CX_ITEM_SPACE
* (i
% nCols
) + ITEM_MARGIN
;
307 yPos
= DIALOG_MARGIN
+ CY_ITEM_SPACE
* (i
/ nCols
) + ITEM_MARGIN
;
310 if (nItems
< CoolSwitchColumns
)
312 xPos
+= (itemsW
- nItems
* CX_ITEM_SPACE
) / 2;
315 // if this position is selected,
316 if (selectedWindow
== i
+ nShift
)
318 // create a solid pen
319 Color
= GetSysColor(COLOR_HIGHLIGHT
);
320 hPen
= CreatePen(PS_SOLID
, 1, Color
);
322 // draw a rectangle with using the pen
323 SelectObject(dialogDC
, hPen
);
324 SelectObject(dialogDC
, GetStockObject(NULL_BRUSH
));
325 Rectangle(dialogDC
, xPos
, yPos
, xPos
+ CX_ITEM
, yPos
+ CY_ITEM
);
326 Rectangle(dialogDC
, xPos
+ 1, yPos
+ 1,
327 xPos
+ CX_ITEM
- 1, yPos
+ CY_ITEM
- 1);
334 DrawIconEx(dialogDC
, xPos
+ ICON_MARGIN
, yPos
+ ICON_MARGIN
,
335 hIcon
, CX_ICON
, CY_ICON
, 0, NULL
, DI_NORMAL
);
338 // set the text rectangle
339 SetRect(&textRC
, DIALOG_MARGIN
, DIALOG_MARGIN
+ itemsH
,
340 totalW
- DIALOG_MARGIN
, totalH
- DIALOG_MARGIN
);
342 // draw the sunken button around text
343 DrawFrameControl(dialogDC
, &textRC
, DFC_BUTTON
,
344 DFCS_BUTTONPUSH
| DFCS_PUSHED
);
347 CharCount
= GetWindowTextW(windowList
[selectedWindow
], windowText
,
348 _countof(windowText
));
351 dcFont
= SelectObject(dialogDC
, dialogFont
);
352 SetTextColor(dialogDC
, GetSysColor(COLOR_BTNTEXT
));
353 SetBkMode(dialogDC
, TRANSPARENT
);
354 DrawTextW(dialogDC
, windowText
, CharCount
, &textRC
,
355 DT_CENTER
| DT_VCENTER
| DT_END_ELLIPSIS
| DT_SINGLELINE
);
356 SelectObject(dialogDC
, dcFont
);
359 EndPaint(hWnd
, &paint
);
362 DWORD
CreateSwitcherWindow(HINSTANCE hInstance
)
364 switchdialog
= CreateWindowExW( WS_EX_TOPMOST
|WS_EX_DLGMODALFRAME
|WS_EX_TOOLWINDOW
,
367 WS_POPUP
|WS_BORDER
|WS_DISABLED
,
375 TRACE("[ATbot] Task Switcher Window failed to create.\n");
383 DWORD
GetDialogFont(VOID
)
388 dialogFont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
391 GetTextMetrics(tDC
, &tm
);
392 fontHeight
= tm
.tmHeight
;
398 void PrepareWindow(VOID
)
400 nItems
= windowCount
;
402 nCols
= CoolSwitchColumns
;
403 nRows
= (nItems
+ CoolSwitchColumns
- 1) / CoolSwitchColumns
;
404 if (nRows
> CoolSwitchRows
)
406 nRows
= CoolSwitchRows
;
407 nItems
= nRows
* nCols
;
410 itemsW
= nCols
* CX_ITEM_SPACE
;
411 itemsH
= nRows
* CY_ITEM_SPACE
;
413 totalW
= itemsW
+ 2 * DIALOG_MARGIN
;
414 totalH
= itemsH
+ 2 * DIALOG_MARGIN
;
415 totalH
+= fontHeight
+ 2 * CY_TEXT_MARGIN
;
417 ResizeAndCenter(switchdialog
, totalW
, totalH
);
420 BOOL
ProcessHotKey(VOID
)
425 EnumChildWindowsZOrder(NULL
, EnumerateCallback
, 0);
432 TRACE("[ATbot] HotKey Received. Opening window.\n");
433 ShowWindow(switchdialog
, SW_SHOWNORMAL
);
434 MakeWindowActive(switchdialog
);
439 TRACE("[ATbot] HotKey Received Rotating.\n");
440 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
441 InvalidateRect(switchdialog
, NULL
, TRUE
);
446 void RotateTasks(BOOL bShift
)
448 HWND hwndFirst
, hwndLast
;
451 if (windowCount
< 2 || !Esc
)
454 hwndFirst
= windowList
[0];
455 hwndLast
= windowList
[windowCount
- 1];
459 SetWindowPos(hwndLast
, HWND_TOP
, 0, 0, 0, 0,
460 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
461 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
463 MakeWindowActive(hwndLast
);
465 Size
= (windowCount
- 1) * sizeof(HWND
);
466 MoveMemory(&windowList
[1], &windowList
[0], Size
);
467 windowList
[0] = hwndLast
;
471 SetWindowPos(hwndFirst
, hwndLast
, 0, 0, 0, 0,
472 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
473 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
475 MakeWindowActive(windowList
[1]);
477 Size
= (windowCount
- 1) * sizeof(HWND
);
478 MoveMemory(&windowList
[0], &windowList
[1], Size
);
479 windowList
[windowCount
- 1] = hwndFirst
;
483 static void MoveLeft(void)
485 selectedWindow
= selectedWindow
- 1;
486 if (selectedWindow
< 0)
487 selectedWindow
= windowCount
- 1;
488 InvalidateRect(switchdialog
, NULL
, TRUE
);
491 static void MoveRight(void)
493 selectedWindow
= (selectedWindow
+ 1) % windowCount
;
494 InvalidateRect(switchdialog
, NULL
, TRUE
);
497 static void MoveUp(void)
499 INT iRow
= selectedWindow
/ nCols
;
500 INT iCol
= selectedWindow
% nCols
;
506 selectedWindow
= iRow
* nCols
+ iCol
;
507 if (selectedWindow
>= windowCount
)
508 selectedWindow
= windowCount
- 1;
509 InvalidateRect(switchdialog
, NULL
, TRUE
);
512 static void MoveDown(void)
514 INT iRow
= selectedWindow
/ nCols
;
515 INT iCol
= selectedWindow
% nCols
;
521 selectedWindow
= iRow
* nCols
+ iCol
;
522 if (selectedWindow
>= windowCount
)
523 selectedWindow
= windowCount
- 1;
524 InvalidateRect(switchdialog
, NULL
, TRUE
);
528 DestroyAppWindows(VOID
)
530 // for every item of the icon list:
532 for (i
= 0; i
< windowCount
; ++i
)
535 DestroyIcon(iconList
[i
]);
540 LRESULT WINAPI
DoAppSwitch( WPARAM wParam
, LPARAM lParam
)
545 // FIXME: Is loading timing OK?
546 LoadCoolSwitchSettings();
551 // Already in the loop.
552 if (switchdialog
|| Esc
) return 0;
554 hwndActive
= GetActiveWindow();
555 // Nothing is active so exit.
556 if (!hwndActive
) return 0;
558 if (lParam
== VK_ESCAPE
)
563 EnumChildWindowsZOrder(NULL
, EnumerateCallback
, 0);
568 RotateTasks(GetAsyncKeyState(VK_SHIFT
) < 0);
570 hwndActive
= GetActiveWindow();
572 if (hwndActive
== NULL
)
579 // Capture current active window.
580 SetCapture( hwndActive
);
585 if( !CreateSwitcherWindow(User32Instance
) ) goto Exit
;
586 if( !GetDialogFont() ) goto Exit
;
587 if( !ProcessHotKey() ) goto Exit
;
596 // Main message loop:
601 if (PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
))
603 if (!CallMsgFilterW( &msg
, MSGF_NEXTWINDOW
)) break;
604 /* remove the message from the queue */
605 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
615 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
616 if (msg
.wParam
== VK_MENU
)
618 CompleteSwitch(TRUE
);
620 else if (msg
.wParam
== VK_RETURN
)
622 CompleteSwitch(TRUE
);
624 else if (msg
.wParam
== VK_ESCAPE
)
626 TRACE("DoAppSwitch VK_ESCAPE 2\n");
627 CompleteSwitch(FALSE
);
634 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
635 if (HIWORD(msg
.lParam
) & KF_ALTDOWN
)
637 if ( msg
.wParam
== VK_TAB
)
640 if (GetKeyState(VK_SHIFT
) < 0)
649 else if ( msg
.wParam
== VK_ESCAPE
)
652 RotateTasks(GetKeyState(VK_SHIFT
) < 0);
654 else if ( msg
.wParam
== VK_LEFT
)
658 else if ( msg
.wParam
== VK_RIGHT
)
662 else if ( msg
.wParam
== VK_UP
)
666 else if ( msg
.wParam
== VK_DOWN
)
675 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
676 ProcessMouseMessage(msg
.message
, msg
.lParam
);
680 if (PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
))
682 TranslateMessage(&msg
);
683 DispatchMessageW(&msg
);
690 if (switchdialog
) DestroyWindow(switchdialog
);
691 if (Esc
) DestroyAppWindows();
700 // Switch System Class Window Proc.
702 LRESULT WINAPI
SwitchWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
706 pWnd
= ValidateHwnd(hWnd
);
711 NtUserSetWindowFNID(hWnd
, FNID_SWITCH
);
718 if (!(ati
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ati
))))
720 SetWindowLongPtrW( hWnd
, 0, (LONG_PTR
)ati
);
727 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
728 ati
->cbSize
= sizeof(ALTTABINFO
);
729 ati
->cItems
= nItems
;
730 ati
->cColumns
= nCols
;
734 ati
->iColFocus
= (selectedWindow
- nShift
) % nCols
;
735 ati
->iRowFocus
= (selectedWindow
- nShift
) / nCols
;
742 ati
->cxItem
= CX_ITEM_SPACE
;
743 ati
->cyItem
= CY_ITEM_SPACE
;
744 ati
->ptStart
= ptStart
;
749 ProcessMouseMessage(uMsg
, lParam
);
753 if (wParam
== WA_INACTIVE
)
755 CompleteSwitch(FALSE
);
765 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
766 HeapFree( GetProcessHeap(), 0, ati
);
767 SetWindowLongPtrW( hWnd
, 0, 0 );
769 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
772 return DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
775 LRESULT WINAPI
SwitchWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
777 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
780 LRESULT WINAPI
SwitchWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
782 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);