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 if (!IsWindowVisible(window
))
174 if (GetWindow(window
, GW_OWNER
) != NULL
)
177 GetClassNameW(window
, windowText
, _countof(windowText
));
178 if ((wcscmp(L
"Shell_TrayWnd", windowText
)==0) ||
179 (wcscmp(L
"Progman", windowText
)==0) )
182 // First try to get the big icon assigned to the window
183 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_BIG
, 0);
186 // If no icon is assigned, try to get the icon assigned to the windows' class
187 hIcon
= (HICON
)GetClassLongPtrW(window
, GCL_HICON
);
190 // If we still don't have an icon, see if we can do with the small icon,
191 // or a default application icon
192 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_SMALL2
, 0);
195 // using windows logo icon as default
196 hIcon
= gpsi
->hIconWindows
;
199 //if all attempts to get icon fails go to the next window
206 windowList
[windowCount
] = window
;
207 iconList
[windowCount
] = CopyIcon(hIcon
);
211 // If we got to the max number of windows,
212 // we won't be able to add any more
213 if(windowCount
>= MAX_WINDOWS
)
219 // Function mostly compatible with the normal EnumWindows,
220 // except it lists in Z-Order and it doesn't ensure consistency
221 // if a window is removed while enumerating
222 void EnumWindowsZOrder(WNDENUMPROC callback
, LPARAM lParam
)
224 HWND next
= GetTopWindow(NULL
);
227 if(!callback(next
, lParam
))
229 next
= GetWindow(next
, GW_HWNDNEXT
);
233 void ProcessMouseMessage(UINT message
, LPARAM lParam
)
235 int xPos
= LOWORD(lParam
);
236 int yPos
= HIWORD(lParam
);
238 int xIndex
= (xPos
- DIALOG_MARGIN
) / CX_ITEM_SPACE
;
239 int yIndex
= (yPos
- DIALOG_MARGIN
) / CY_ITEM_SPACE
;
241 if (xIndex
< 0 || nCols
<= xIndex
||
242 yIndex
< 0 || nRows
<= yIndex
)
247 selectedWindow
= (yIndex
*nCols
) + xIndex
;
248 if (message
== WM_MOUSEMOVE
)
250 InvalidateRect(switchdialog
, NULL
, TRUE
);
251 //RedrawWindow(switchdialog, NULL, NULL, 0);
255 selectedWindow
= (yIndex
*nCols
) + xIndex
;
256 CompleteSwitch(TRUE
);
260 void OnPaint(HWND hWnd
)
265 int i
, xPos
, yPos
, CharCount
;
272 if (nCols
== 0 || nItems
== 0)
276 dialogDC
= BeginPaint(hWnd
, &paint
);
277 if (dialogDC
== NULL
)
280 // fill the client area
281 GetClientRect(hWnd
, &cRC
);
282 FillRect(dialogDC
, &cRC
, (HBRUSH
)(COLOR_3DFACE
+ 1));
284 // if the selection index exceeded the display items, then
285 // do display item shifting
286 if (selectedWindow
>= nItems
)
287 nShift
= selectedWindow
- nItems
+ 1;
291 for (i
= 0; i
< nItems
; ++i
)
293 // get the icon to display
294 hIcon
= iconList
[i
+ nShift
];
296 // calculate the position where we start drawing
297 xPos
= DIALOG_MARGIN
+ CX_ITEM_SPACE
* (i
% nCols
) + ITEM_MARGIN
;
298 yPos
= DIALOG_MARGIN
+ CY_ITEM_SPACE
* (i
/ nCols
) + ITEM_MARGIN
;
301 if (nItems
< CoolSwitchColumns
)
303 xPos
+= (itemsW
- nItems
* CX_ITEM_SPACE
) / 2;
306 // if this position is selected,
307 if (selectedWindow
== i
+ nShift
)
309 // create a solid pen
310 Color
= GetSysColor(COLOR_HIGHLIGHT
);
311 hPen
= CreatePen(PS_SOLID
, 1, Color
);
313 // draw a rectangle with using the pen
314 SelectObject(dialogDC
, hPen
);
315 SelectObject(dialogDC
, GetStockObject(NULL_BRUSH
));
316 Rectangle(dialogDC
, xPos
, yPos
, xPos
+ CX_ITEM
, yPos
+ CY_ITEM
);
317 Rectangle(dialogDC
, xPos
+ 1, yPos
+ 1,
318 xPos
+ CX_ITEM
- 1, yPos
+ CY_ITEM
- 1);
325 DrawIconEx(dialogDC
, xPos
+ ICON_MARGIN
, yPos
+ ICON_MARGIN
,
326 hIcon
, CX_ICON
, CY_ICON
, 0, NULL
, DI_NORMAL
);
329 // set the text rectangle
330 SetRect(&textRC
, DIALOG_MARGIN
, DIALOG_MARGIN
+ itemsH
,
331 totalW
- DIALOG_MARGIN
, totalH
- DIALOG_MARGIN
);
333 // draw the sunken button around text
334 DrawFrameControl(dialogDC
, &textRC
, DFC_BUTTON
,
335 DFCS_BUTTONPUSH
| DFCS_PUSHED
);
338 CharCount
= GetWindowTextW(windowList
[selectedWindow
], windowText
,
339 _countof(windowText
));
342 dcFont
= SelectObject(dialogDC
, dialogFont
);
343 SetTextColor(dialogDC
, GetSysColor(COLOR_BTNTEXT
));
344 SetBkMode(dialogDC
, TRANSPARENT
);
345 DrawTextW(dialogDC
, windowText
, CharCount
, &textRC
,
346 DT_CENTER
| DT_VCENTER
| DT_END_ELLIPSIS
| DT_SINGLELINE
);
347 SelectObject(dialogDC
, dcFont
);
350 EndPaint(hWnd
, &paint
);
353 DWORD
CreateSwitcherWindow(HINSTANCE hInstance
)
355 switchdialog
= CreateWindowExW( WS_EX_TOPMOST
|WS_EX_DLGMODALFRAME
|WS_EX_TOOLWINDOW
,
358 WS_POPUP
|WS_BORDER
|WS_DISABLED
,
366 TRACE("[ATbot] Task Switcher Window failed to create.\n");
374 DWORD
GetDialogFont(VOID
)
379 dialogFont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
382 GetTextMetrics(tDC
, &tm
);
383 fontHeight
= tm
.tmHeight
;
389 void PrepareWindow(VOID
)
391 nItems
= windowCount
;
393 nCols
= CoolSwitchColumns
;
394 nRows
= (nItems
+ CoolSwitchColumns
- 1) / CoolSwitchColumns
;
395 if (nRows
> CoolSwitchRows
)
397 nRows
= CoolSwitchRows
;
398 nItems
= nRows
* nCols
;
401 itemsW
= nCols
* CX_ITEM_SPACE
;
402 itemsH
= nRows
* CY_ITEM_SPACE
;
404 totalW
= itemsW
+ 2 * DIALOG_MARGIN
;
405 totalH
= itemsH
+ 2 * DIALOG_MARGIN
;
406 totalH
+= fontHeight
+ 2 * CY_TEXT_MARGIN
;
408 ResizeAndCenter(switchdialog
, totalW
, totalH
);
411 BOOL
ProcessHotKey(VOID
)
416 EnumWindowsZOrder(EnumerateCallback
, 0);
423 TRACE("[ATbot] HotKey Received. Opening window.\n");
424 ShowWindow(switchdialog
, SW_SHOWNORMAL
);
425 MakeWindowActive(switchdialog
);
430 TRACE("[ATbot] HotKey Received Rotating.\n");
431 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
432 InvalidateRect(switchdialog
, NULL
, TRUE
);
437 void RotateTasks(BOOL bShift
)
439 HWND hwndFirst
, hwndLast
;
442 if (windowCount
< 2 || !Esc
)
445 hwndFirst
= windowList
[0];
446 hwndLast
= windowList
[windowCount
- 1];
450 SetWindowPos(hwndLast
, HWND_TOP
, 0, 0, 0, 0,
451 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
452 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
454 MakeWindowActive(hwndLast
);
456 Size
= (windowCount
- 1) * sizeof(HWND
);
457 MoveMemory(&windowList
[1], &windowList
[0], Size
);
458 windowList
[0] = hwndLast
;
462 SetWindowPos(hwndFirst
, hwndLast
, 0, 0, 0, 0,
463 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOACTIVATE
|
464 SWP_NOOWNERZORDER
| SWP_NOREPOSITION
);
466 MakeWindowActive(windowList
[1]);
468 Size
= (windowCount
- 1) * sizeof(HWND
);
469 MoveMemory(&windowList
[0], &windowList
[1], Size
);
470 windowList
[windowCount
- 1] = hwndFirst
;
475 DestroyAppWindows(VOID
)
477 // for every item of the icon list:
479 for (i
= 0; i
< windowCount
; ++i
)
482 DestroyIcon(iconList
[i
]);
487 LRESULT WINAPI
DoAppSwitch( WPARAM wParam
, LPARAM lParam
)
492 // FIXME: Is loading timing OK?
493 LoadCoolSwitchSettings();
498 // Already in the loop.
499 if (switchdialog
|| Esc
) return 0;
501 hwndActive
= GetActiveWindow();
502 // Nothing is active so exit.
503 if (!hwndActive
) return 0;
505 if (lParam
== VK_ESCAPE
)
510 EnumWindowsZOrder(EnumerateCallback
, 0);
515 RotateTasks(GetAsyncKeyState(VK_SHIFT
) < 0);
517 hwndActive
= GetActiveWindow();
519 if (hwndActive
== NULL
)
526 // Capture current active window.
527 SetCapture( hwndActive
);
532 if( !CreateSwitcherWindow(User32Instance
) ) goto Exit
;
533 if( !GetDialogFont() ) goto Exit
;
534 if( !ProcessHotKey() ) goto Exit
;
543 // Main message loop:
548 if (PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
))
550 if (!CallMsgFilterW( &msg
, MSGF_NEXTWINDOW
)) break;
551 /* remove the message from the queue */
552 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
562 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
563 if (msg
.wParam
== VK_MENU
)
565 CompleteSwitch(TRUE
);
567 else if (msg
.wParam
== VK_RETURN
)
569 CompleteSwitch(TRUE
);
571 else if (msg
.wParam
== VK_ESCAPE
)
573 TRACE("DoAppSwitch VK_ESCAPE 2\n");
574 CompleteSwitch(FALSE
);
581 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
582 if (HIWORD(msg
.lParam
) & KF_ALTDOWN
)
584 if ( msg
.wParam
== VK_TAB
)
587 if (GetKeyState(VK_SHIFT
) < 0)
589 selectedWindow
= selectedWindow
- 1;
590 if (selectedWindow
< 0)
591 selectedWindow
= windowCount
- 1;
595 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
597 InvalidateRect(switchdialog
, NULL
, TRUE
);
599 else if ( msg
.wParam
== VK_ESCAPE
)
602 RotateTasks(GetKeyState(VK_SHIFT
) < 0);
609 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
610 ProcessMouseMessage(msg
.message
, msg
.lParam
);
614 if (PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
))
616 TranslateMessage(&msg
);
617 DispatchMessageW(&msg
);
624 if (switchdialog
) DestroyWindow(switchdialog
);
625 if (Esc
) DestroyAppWindows();
634 // Switch System Class Window Proc.
636 LRESULT WINAPI
SwitchWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
640 pWnd
= ValidateHwnd(hWnd
);
645 NtUserSetWindowFNID(hWnd
, FNID_SWITCH
);
652 if (!(ati
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ati
))))
654 SetWindowLongPtrW( hWnd
, 0, (LONG_PTR
)ati
);
661 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
662 ati
->cbSize
= sizeof(ALTTABINFO
);
663 ati
->cItems
= nItems
;
664 ati
->cColumns
= nCols
;
668 ati
->iColFocus
= (selectedWindow
- nShift
) % nCols
;
669 ati
->iRowFocus
= (selectedWindow
- nShift
) / nCols
;
676 ati
->cxItem
= CX_ITEM_SPACE
;
677 ati
->cyItem
= CY_ITEM_SPACE
;
678 ati
->ptStart
= ptStart
;
683 ProcessMouseMessage(uMsg
, lParam
);
687 if (wParam
== WA_INACTIVE
)
689 CompleteSwitch(FALSE
);
699 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
700 HeapFree( GetProcessHeap(), 0, ati
);
701 SetWindowLongPtrW( hWnd
, 0, 0 );
703 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
706 return DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
709 LRESULT WINAPI
SwitchWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
711 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
714 LRESULT WINAPI
SwitchWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
716 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);