7f97d0dfacb86188c9f8328db12943c8fda26981
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
)
169 UNREFERENCED_PARAMETER(lParam
);
171 // First try to get the big icon assigned to the window
172 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_BIG
, 0);
175 // If no icon is assigned, try to get the icon assigned to the windows' class
176 hIcon
= (HICON
)GetClassLongPtrW(window
, GCL_HICON
);
179 // If we still don't have an icon, see if we can do with the small icon,
180 // or a default application icon
181 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_SMALL2
, 0);
184 // using windows logo icon as default
185 hIcon
= gpsi
->hIconWindows
;
188 //if all attempts to get icon fails go to the next window
195 windowList
[windowCount
] = window
;
196 iconList
[windowCount
] = CopyIcon(hIcon
);
200 // If we got to the max number of windows,
201 // we won't be able to add any more
202 if(windowCount
>= MAX_WINDOWS
)
208 // Function mostly compatible with the normal EnumChildWindows,
209 // except it lists in Z-Order and it doesn't ensure consistency
210 // if a window is removed while enumerating
211 void EnumWindowsZOrder(WNDENUMPROC callback
, LPARAM lParam
)
213 HWND hwnd
, hwndOwner
;
217 for (hwnd
= GetTopWindow(NULL
); hwnd
; hwnd
= GetWindow(hwnd
, GW_HWNDNEXT
))
219 if (!IsWindowVisible(hwnd
))
222 // check special windows
223 if (!GetClassNameW(hwnd
, szClass
, _countof(szClass
)) ||
224 wcscmp(szClass
, L
"Shell_TrayWnd") == 0 ||
225 wcscmp(szClass
, L
"Progman") == 0)
230 ExStyle
= GetWindowLongPtrW(hwnd
, GWL_EXSTYLE
);
231 if (ExStyle
& WS_EX_TOOLWINDOW
)
234 hwndOwner
= GetWindow(hwnd
, GW_OWNER
);
235 if ((ExStyle
& WS_EX_APPWINDOW
) || !IsWindowVisible(hwndOwner
))
237 if (!callback(hwnd
, lParam
))
245 void ProcessMouseMessage(UINT message
, LPARAM lParam
)
247 int xPos
= LOWORD(lParam
);
248 int yPos
= HIWORD(lParam
);
250 int xIndex
= (xPos
- DIALOG_MARGIN
) / CX_ITEM_SPACE
;
251 int yIndex
= (yPos
- DIALOG_MARGIN
) / CY_ITEM_SPACE
;
253 if (xIndex
< 0 || nCols
<= xIndex
||
254 yIndex
< 0 || nRows
<= yIndex
)
259 selectedWindow
= (yIndex
*nCols
) + xIndex
;
260 if (message
== WM_MOUSEMOVE
)
262 InvalidateRect(switchdialog
, NULL
, TRUE
);
263 //RedrawWindow(switchdialog, NULL, NULL, 0);
267 selectedWindow
= (yIndex
*nCols
) + xIndex
;
268 CompleteSwitch(TRUE
);
272 void OnPaint(HWND hWnd
)
277 int i
, xPos
, yPos
, CharCount
;
284 if (nCols
== 0 || nItems
== 0)
288 dialogDC
= BeginPaint(hWnd
, &paint
);
289 if (dialogDC
== NULL
)
292 // fill the client area
293 GetClientRect(hWnd
, &cRC
);
294 FillRect(dialogDC
, &cRC
, (HBRUSH
)(COLOR_3DFACE
+ 1));
296 // if the selection index exceeded the display items, then
297 // do display item shifting
298 if (selectedWindow
>= nItems
)
299 nShift
= selectedWindow
- nItems
+ 1;
303 for (i
= 0; i
< nItems
; ++i
)
305 // get the icon to display
306 hIcon
= iconList
[i
+ nShift
];
308 // calculate the position where we start drawing
309 xPos
= DIALOG_MARGIN
+ CX_ITEM_SPACE
* (i
% nCols
) + ITEM_MARGIN
;
310 yPos
= DIALOG_MARGIN
+ CY_ITEM_SPACE
* (i
/ nCols
) + ITEM_MARGIN
;
313 if (nItems
< CoolSwitchColumns
)
315 xPos
+= (itemsW
- nItems
* CX_ITEM_SPACE
) / 2;
318 // if this position is selected,
319 if (selectedWindow
== i
+ nShift
)
321 // create a solid pen
322 Color
= GetSysColor(COLOR_HIGHLIGHT
);
323 hPen
= CreatePen(PS_SOLID
, 1, Color
);
325 // draw a rectangle with using the pen
326 SelectObject(dialogDC
, hPen
);
327 SelectObject(dialogDC
, GetStockObject(NULL_BRUSH
));
328 Rectangle(dialogDC
, xPos
, yPos
, xPos
+ CX_ITEM
, yPos
+ CY_ITEM
);
329 Rectangle(dialogDC
, xPos
+ 1, yPos
+ 1,
330 xPos
+ CX_ITEM
- 1, yPos
+ CY_ITEM
- 1);
337 DrawIconEx(dialogDC
, xPos
+ ICON_MARGIN
, yPos
+ ICON_MARGIN
,
338 hIcon
, CX_ICON
, CY_ICON
, 0, NULL
, DI_NORMAL
);
341 // set the text rectangle
342 SetRect(&textRC
, DIALOG_MARGIN
, DIALOG_MARGIN
+ itemsH
,
343 totalW
- DIALOG_MARGIN
, totalH
- DIALOG_MARGIN
);
345 // draw the sunken button around text
346 DrawFrameControl(dialogDC
, &textRC
, DFC_BUTTON
,
347 DFCS_BUTTONPUSH
| DFCS_PUSHED
);
350 CharCount
= GetWindowTextW(windowList
[selectedWindow
], windowText
,
351 _countof(windowText
));
354 dcFont
= SelectObject(dialogDC
, dialogFont
);
355 SetTextColor(dialogDC
, GetSysColor(COLOR_BTNTEXT
));
356 SetBkMode(dialogDC
, TRANSPARENT
);
357 DrawTextW(dialogDC
, windowText
, CharCount
, &textRC
,
358 DT_CENTER
| DT_VCENTER
| DT_END_ELLIPSIS
| DT_SINGLELINE
);
359 SelectObject(dialogDC
, dcFont
);
362 EndPaint(hWnd
, &paint
);
365 DWORD
CreateSwitcherWindow(HINSTANCE hInstance
)
367 switchdialog
= CreateWindowExW( WS_EX_TOPMOST
|WS_EX_DLGMODALFRAME
|WS_EX_TOOLWINDOW
,
370 WS_POPUP
|WS_BORDER
|WS_DISABLED
,
378 TRACE("[ATbot] Task Switcher Window failed to create.\n");
386 DWORD
GetDialogFont(VOID
)
391 dialogFont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
394 GetTextMetrics(tDC
, &tm
);
395 fontHeight
= tm
.tmHeight
;
401 void PrepareWindow(VOID
)
403 nItems
= windowCount
;
405 nCols
= CoolSwitchColumns
;
406 nRows
= (nItems
+ CoolSwitchColumns
- 1) / CoolSwitchColumns
;
407 if (nRows
> CoolSwitchRows
)
409 nRows
= CoolSwitchRows
;
410 nItems
= nRows
* nCols
;
413 itemsW
= nCols
* CX_ITEM_SPACE
;
414 itemsH
= nRows
* CY_ITEM_SPACE
;
416 totalW
= itemsW
+ 2 * DIALOG_MARGIN
;
417 totalH
= itemsH
+ 2 * DIALOG_MARGIN
;
418 totalH
+= fontHeight
+ 2 * CY_TEXT_MARGIN
;
420 ResizeAndCenter(switchdialog
, totalW
, totalH
);
423 BOOL
ProcessHotKey(VOID
)
428 EnumWindowsZOrder(EnumerateCallback
, 0);
430 if (windowCount
== 0)
433 if (windowCount
== 1)
436 CompleteSwitch(TRUE
);
442 TRACE("[ATbot] HotKey Received. Opening window.\n");
443 ShowWindow(switchdialog
, SW_SHOWNORMAL
);
444 MakeWindowActive(switchdialog
);
449 TRACE("[ATbot] HotKey Received Rotating.\n");
450 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
451 InvalidateRect(switchdialog
, NULL
, TRUE
);
456 void RotateTasks(BOOL bShift
)
458 HWND hwndFirst
, hwndLast
;
461 if (windowCount
< 2 || !Esc
)
464 hwndFirst
= windowList
[0];
465 hwndLast
= windowList
[windowCount
- 1];
469 SetWindowPos(hwndLast
, HWND_TOP
, 0, 0, 0, 0,
470 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
471 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
473 MakeWindowActive(hwndLast
);
475 Size
= (windowCount
- 1) * sizeof(HWND
);
476 MoveMemory(&windowList
[1], &windowList
[0], Size
);
477 windowList
[0] = hwndLast
;
481 SetWindowPos(hwndFirst
, hwndLast
, 0, 0, 0, 0,
482 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
483 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
485 MakeWindowActive(windowList
[1]);
487 Size
= (windowCount
- 1) * sizeof(HWND
);
488 MoveMemory(&windowList
[0], &windowList
[1], Size
);
489 windowList
[windowCount
- 1] = hwndFirst
;
493 static void MoveLeft(void)
495 selectedWindow
= selectedWindow
- 1;
496 if (selectedWindow
< 0)
497 selectedWindow
= windowCount
- 1;
498 InvalidateRect(switchdialog
, NULL
, TRUE
);
501 static void MoveRight(void)
503 selectedWindow
= (selectedWindow
+ 1) % windowCount
;
504 InvalidateRect(switchdialog
, NULL
, TRUE
);
507 static void MoveUp(void)
509 INT iRow
= selectedWindow
/ nCols
;
510 INT iCol
= selectedWindow
% nCols
;
516 selectedWindow
= iRow
* nCols
+ iCol
;
517 if (selectedWindow
>= windowCount
)
518 selectedWindow
= windowCount
- 1;
519 InvalidateRect(switchdialog
, NULL
, TRUE
);
522 static void MoveDown(void)
524 INT iRow
= selectedWindow
/ nCols
;
525 INT iCol
= selectedWindow
% nCols
;
531 selectedWindow
= iRow
* nCols
+ iCol
;
532 if (selectedWindow
>= windowCount
)
533 selectedWindow
= windowCount
- 1;
534 InvalidateRect(switchdialog
, NULL
, TRUE
);
538 DestroyAppWindows(VOID
)
540 // for every item of the icon list:
542 for (i
= 0; i
< windowCount
; ++i
)
545 DestroyIcon(iconList
[i
]);
550 LRESULT WINAPI
DoAppSwitch( WPARAM wParam
, LPARAM lParam
)
555 // FIXME: Is loading timing OK?
556 LoadCoolSwitchSettings();
561 // Already in the loop.
562 if (switchdialog
|| Esc
) return 0;
564 hwndActive
= GetActiveWindow();
565 // Nothing is active so exit.
566 if (!hwndActive
) return 0;
568 if (lParam
== VK_ESCAPE
)
573 EnumWindowsZOrder(EnumerateCallback
, 0);
578 RotateTasks(GetAsyncKeyState(VK_SHIFT
) < 0);
580 hwndActive
= GetActiveWindow();
582 if (hwndActive
== NULL
)
589 // Capture current active window.
590 SetCapture( hwndActive
);
595 if( !CreateSwitcherWindow(User32Instance
) ) goto Exit
;
596 if( !GetDialogFont() ) goto Exit
;
597 if( !ProcessHotKey() ) goto Exit
;
606 // Main message loop:
611 if (PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
))
613 if (!CallMsgFilterW( &msg
, MSGF_NEXTWINDOW
)) break;
614 /* remove the message from the queue */
615 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
625 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
626 if (msg
.wParam
== VK_MENU
)
628 CompleteSwitch(TRUE
);
630 else if (msg
.wParam
== VK_RETURN
)
632 CompleteSwitch(TRUE
);
634 else if (msg
.wParam
== VK_ESCAPE
)
636 TRACE("DoAppSwitch VK_ESCAPE 2\n");
637 CompleteSwitch(FALSE
);
644 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
645 if (HIWORD(msg
.lParam
) & KF_ALTDOWN
)
647 if ( msg
.wParam
== VK_TAB
)
650 if (GetKeyState(VK_SHIFT
) < 0)
659 else if ( msg
.wParam
== VK_ESCAPE
)
662 RotateTasks(GetKeyState(VK_SHIFT
) < 0);
664 else if ( msg
.wParam
== VK_LEFT
)
668 else if ( msg
.wParam
== VK_RIGHT
)
672 else if ( msg
.wParam
== VK_UP
)
676 else if ( msg
.wParam
== VK_DOWN
)
685 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
686 ProcessMouseMessage(msg
.message
, msg
.lParam
);
690 if (PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
))
692 TranslateMessage(&msg
);
693 DispatchMessageW(&msg
);
700 if (switchdialog
) DestroyWindow(switchdialog
);
701 if (Esc
) DestroyAppWindows();
710 // Switch System Class Window Proc.
712 LRESULT WINAPI
SwitchWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
716 pWnd
= ValidateHwnd(hWnd
);
721 NtUserSetWindowFNID(hWnd
, FNID_SWITCH
);
728 if (!(ati
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ati
))))
730 SetWindowLongPtrW( hWnd
, 0, (LONG_PTR
)ati
);
737 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
738 ati
->cbSize
= sizeof(ALTTABINFO
);
739 ati
->cItems
= nItems
;
740 ati
->cColumns
= nCols
;
744 ati
->iColFocus
= (selectedWindow
- nShift
) % nCols
;
745 ati
->iRowFocus
= (selectedWindow
- nShift
) / nCols
;
752 ati
->cxItem
= CX_ITEM_SPACE
;
753 ati
->cyItem
= CY_ITEM_SPACE
;
754 ati
->ptStart
= ptStart
;
759 ProcessMouseMessage(uMsg
, lParam
);
763 if (wParam
== WA_INACTIVE
)
765 CompleteSwitch(FALSE
);
775 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
776 HeapFree( GetProcessHeap(), 0, ati
);
777 SetWindowLongPtrW( hWnd
, 0, 0 );
779 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
782 return DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
785 LRESULT WINAPI
SwitchWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
787 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
790 LRESULT WINAPI
SwitchWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
792 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);