2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/user32/controls/appswitch.c
5 * PURPOSE: app switching functionality
6 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald@reactos.org)
7 * David Quintana (gigaherz@gmail.com)
12 #include <wine/debug.h>
13 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
15 // limit the number of windows shown in the alt-tab window
16 // 120 windows results in (12*40) by (10*40) pixels worth of icons.
17 #define MAX_WINDOWS 120
22 int selectedWindow
= 0;
27 WCHAR windowText
[1024];
29 HWND windowList
[MAX_WINDOWS
];
30 HICON iconList
[MAX_WINDOWS
];
33 int cxBorder
, cyBorder
;
34 int nItems
, nCols
, nRows
;
40 void ResizeAndCenter(HWND hwnd
, int width
, int height
)
42 int screenwidth
= GetSystemMetrics(SM_CXSCREEN
);
43 int screenheight
= GetSystemMetrics(SM_CYSCREEN
);
45 pt
.x
= (screenwidth
- width
) / 2;
46 pt
.y
= (screenheight
- height
) / 2;
48 MoveWindow(hwnd
, pt
.x
, pt
.y
, width
, height
, FALSE
);
51 void MakeWindowActive(HWND hwnd
)
55 wpl
.length
= sizeof(WINDOWPLACEMENT
);
56 GetWindowPlacement(hwnd
, &wpl
);
58 TRACE("GetWindowPlacement wpl.showCmd %d\n",wpl
.showCmd
);
59 if (wpl
.showCmd
== SW_SHOWMINIMIZED
)
60 ShowWindowAsync(hwnd
, SW_RESTORE
);
62 BringWindowToTop(hwnd
); // same as: SetWindowPos(hwnd,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); ?
63 SetForegroundWindow(hwnd
);
66 void CompleteSwitch(BOOL doSwitch
)
73 TRACE("[ATbot] CompleteSwitch Hiding Window.\n");
74 ShowWindow(switchdialog
, SW_HIDE
);
78 if(selectedWindow
>= windowCount
)
81 // FIXME: workaround because reactos fails to activate the previous window correctly.
82 //if(selectedWindow != 0)
84 HWND hwnd
= windowList
[selectedWindow
];
86 GetWindowTextW(hwnd
, windowText
, 1023);
88 TRACE("[ATbot] CompleteSwitch Switching to 0x%08x (%ls)\n", hwnd
, windowText
);
90 MakeWindowActive(hwnd
);
97 BOOL CALLBACK
EnumerateCallback(HWND window
, LPARAM lParam
)
101 UNREFERENCED_PARAMETER(lParam
);
103 if (!IsWindowVisible(window
))
106 GetClassNameW(window
,windowText
,4095);
107 if ((wcscmp(L
"Shell_TrayWnd",windowText
)==0) ||
108 (wcscmp(L
"Progman",windowText
)==0) )
111 // First try to get the big icon assigned to the window
112 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_BIG
, 0);
115 // If no icon is assigned, try to get the icon assigned to the windows' class
116 hIcon
= (HICON
)GetClassLongPtrW(window
, GCL_HICON
);
119 // If we still don't have an icon, see if we can do with the small icon,
120 // or a default application icon
121 hIcon
= (HICON
)SendMessageW(window
, WM_GETICON
, ICON_SMALL2
, 0);
124 // If all fails, give up and continue with the next window
130 windowList
[windowCount
] = window
;
131 iconList
[windowCount
] = CopyIcon(hIcon
);
135 // If we got to the max number of windows,
136 // we won't be able to add any more
137 if(windowCount
== MAX_WINDOWS
)
143 // Function mostly compatible with the normal EnumWindows,
144 // except it lists in Z-Order and it doesn't ensure consistency
145 // if a window is removed while enumerating
146 void EnumWindowsZOrder(WNDENUMPROC callback
, LPARAM lParam
)
148 HWND next
= GetTopWindow(NULL
);
151 if(!callback(next
, lParam
))
153 next
= GetWindow(next
, GW_HWNDNEXT
);
157 void ProcessMouseMessage(UINT message
, LPARAM lParam
)
159 int xPos
= LOWORD(lParam
);
160 int yPos
= HIWORD(lParam
);
162 int xIndex
= (xPos
- xOffset
)/40;
163 int xOff
= (xPos
- xOffset
)%40;
165 int yIndex
= (yPos
- yOffset
)/40;
166 int yOff
= (yPos
- yOffset
)%40;
168 if(xOff
> 32 || xIndex
> nItems
)
171 if(yOff
> 32 || yIndex
> nRows
)
174 selectedWindow
= (yIndex
*nCols
) + xIndex
;
175 if (message
== WM_MOUSEMOVE
)
177 InvalidateRect(switchdialog
, NULL
, TRUE
);
178 //RedrawWindow(switchdialog, NULL, NULL, 0);
182 selectedWindow
= (yIndex
*nCols
) + xIndex
;
183 CompleteSwitch(TRUE
);
187 void OnPaint(HWND hWnd
)
197 int nch
= GetWindowTextW(windowList
[selectedWindow
], windowText
, 1023);
199 dialogDC
= BeginPaint(hWnd
, &paint
);
201 GetClientRect(hWnd
, &cRC
);
202 FillRect(dialogDC
, &cRC
, GetSysColorBrush(COLOR_MENU
));
204 for(i
=0; i
< windowCount
; i
++)
206 HICON hIcon
= iconList
[i
];
208 int xpos
= xOffset
+ 40 * (i
% nCols
);
209 int ypos
= yOffset
+ 40 * (i
/ nCols
);
211 if (selectedWindow
== i
)
213 hBrush
= GetSysColorBrush(COLOR_HIGHLIGHT
);
217 hBrush
= GetSysColorBrush(COLOR_MENU
);
220 cr
= GetSysColor(COLOR_BTNTEXT
); // doesn't look right! >_<
221 hPen
= CreatePen(PS_DOT
, 1, cr
);
222 SelectObject(dialogDC
, hPen
);
223 SelectObject(dialogDC
, hBrush
);
224 Rectangle(dialogDC
, xpos
-2, ypos
-2, xpos
+32+2, ypos
+32+2);
226 // Must NOT destroy the system brush!
228 RECT rc
= { xpos
-2, ypos
-2, xpos
+32+2, ypos
+32+2 };
229 FillRect(dialogDC
, &rc
, hBrush
);
231 DrawIcon(dialogDC
, xpos
, ypos
, hIcon
);
234 dcFont
= SelectObject(dialogDC
, dialogFont
);
235 SetTextColor(dialogDC
, GetSysColor(COLOR_BTNTEXT
));
236 SetBkColor(dialogDC
, GetSysColor(COLOR_BTNFACE
));
240 textRC
.right
= totalW
- 8;
241 textRC
.bottom
= totalH
- 8;
242 DrawTextW(dialogDC
, windowText
, nch
, &textRC
, DT_CENTER
|DT_END_ELLIPSIS
);
243 SelectObject(dialogDC
, dcFont
);
245 EndPaint(hWnd
, &paint
);
248 DWORD
CreateSwitcherWindow(HINSTANCE hInstance
)
250 switchdialog
= CreateWindowExW( WS_EX_TOPMOST
|WS_EX_DLGMODALFRAME
|WS_EX_TOOLWINDOW
,
253 WS_POPUP
|WS_BORDER
|WS_DISABLED
,
261 TRACE("[ATbot] Task Switcher Window failed to create.\n");
269 DWORD
GetDialogFont()
274 dialogFont
= (HFONT
)GetStockObject(DEFAULT_GUI_FONT
);
277 GetTextMetrics(tDC
, &tm
);
278 fontHeight
= tm
.tmHeight
;
286 cxBorder
= GetSystemMetrics(SM_CXBORDER
);
287 cyBorder
= GetSystemMetrics(SM_CYBORDER
);
289 nItems
= windowCount
;
290 nCols
= min(max(nItems
,8),12);
291 nRows
= (nItems
+nCols
-1)/nCols
;
293 itemsW
= nCols
*32 + (nCols
+1)*8;
294 itemsH
= nRows
*32 + (nRows
+1)*8;
296 totalW
= itemsW
+ 2*cxBorder
+ 4;
297 totalH
= itemsH
+ 2*cyBorder
+ fontHeight
+ 8; // give extra pixels for the window title
304 int w2
= nItems
*32 + (nItems
-1)*8;
305 xOffset
= (itemsW
-w2
)/2;
307 ResizeAndCenter(switchdialog
, totalW
, totalH
);
315 EnumWindowsZOrder(EnumerateCallback
, 0);
322 TRACE("[ATbot] HotKey Received. Opening window.\n");
323 ShowWindow(switchdialog
, SW_SHOWNORMAL
);
324 MakeWindowActive(switchdialog
);
329 TRACE("[ATbot] HotKey Received Rotating.\n");
330 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
331 InvalidateRect(switchdialog
, NULL
, TRUE
);
335 LRESULT WINAPI
DoAppSwitch( WPARAM wParam
, LPARAM lParam
)
348 if( !CreateSwitcherWindow(User32Instance
) ) return 0;
349 if( !GetDialogFont() ) return 0;
356 EnumWindowsZOrder(EnumerateCallback
, 0);
357 if (windowCount
< 2) return 0;
358 if (wParam
== SC_NEXTWINDOW
)
362 if (windowCount
== 2)
365 Count
= windowCount
- 1;
367 TRACE("DoAppSwitch VK_ESCAPE 1 Count %d windowCount %d\n",Count
,windowCount
);
368 hwnd
= windowList
[Count
];
369 GetWindowTextW(hwnd
, Text
, 1023);
370 TRACE("[ATbot] Switching to 0x%08x (%ls)\n", hwnd
, Text
);
371 MakeWindowActive(hwnd
);
378 // Main message loop:
383 if (PeekMessageW( &msg
, 0, 0, 0, PM_NOREMOVE
))
385 if (!CallMsgFilterW( &msg
, MSGF_NEXTWINDOW
)) break;
386 /* remove the message from the queue */
387 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
397 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
398 if (msg
.wParam
== VK_MENU
)
400 CompleteSwitch(TRUE
);
402 else if (msg
.wParam
== VK_RETURN
)
404 CompleteSwitch(TRUE
);
406 else if (msg
.wParam
== VK_ESCAPE
)
408 TRACE("DoAppSwitch VK_ESCAPE 2\n");
409 CompleteSwitch(FALSE
);
416 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
417 if (HIWORD(msg
.lParam
) & KF_ALTDOWN
)
420 if ( msg
.wParam
== VK_TAB
)
423 Shift
= GetKeyState(VK_SHIFT
) & 0x8000 ? SC_PREVWINDOW
: SC_NEXTWINDOW
;
424 if (Shift
== SC_NEXTWINDOW
)
426 selectedWindow
= (selectedWindow
+ 1)%windowCount
;
430 selectedWindow
= selectedWindow
- 1;
431 if (selectedWindow
< 0)
432 selectedWindow
= windowCount
- 1;
434 InvalidateRect(switchdialog
, NULL
, TRUE
);
436 else if ( msg
.wParam
== VK_ESCAPE
)
441 if (wParam
== SC_NEXTWINDOW
)
443 Count
= (Count
+ 1)%windowCount
;
449 Count
= windowCount
- 1;
451 hwnd
= windowList
[Count
];
452 GetWindowTextW(hwnd
, Text
, 1023);
453 MakeWindowActive(hwnd
);
460 PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
);
461 ProcessMouseMessage(msg
.message
, msg
.lParam
);
465 if (PeekMessageW( &msg
, 0, msg
.message
, msg
.message
, PM_REMOVE
))
467 TranslateMessage(&msg
);
468 DispatchMessageW(&msg
);
474 if (switchdialog
) DestroyWindow(switchdialog
);
485 for (i
=0; i
< windowCount
; i
++)
487 HICON hIcon
= iconList
[i
];
493 // Switch System Class Window Proc.
495 LRESULT WINAPI
SwitchWndProc_common(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, BOOL unicode
)
499 pWnd
= ValidateHwnd(hWnd
);
504 NtUserSetWindowFNID(hWnd
, FNID_SWITCH
);
511 if (!(ati
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ati
))))
513 SetWindowLongPtrW( hWnd
, 0, (LONG_PTR
)ati
);
520 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
521 ati
->cItems
= nItems
;
522 ati
->cxItem
= ati
->cyItem
= 43;
524 ati
->cColumns
= nCols
;
529 ProcessMouseMessage(uMsg
, lParam
);
533 if (wParam
== WA_INACTIVE
)
535 CompleteSwitch(FALSE
);
545 ati
= (PALTTABINFO
)GetWindowLongPtrW(hWnd
, 0);
546 HeapFree( GetProcessHeap(), 0, ati
);
547 SetWindowLongPtrW( hWnd
, 0, 0 );
549 NtUserSetWindowFNID(hWnd
, FNID_DESTROY
);
552 return DefWindowProcW(hWnd
, uMsg
, wParam
, lParam
);
555 LRESULT WINAPI
SwitchWndProcA(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
557 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, FALSE
);
560 LRESULT WINAPI
SwitchWndProcW(HWND hWnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
562 return SwitchWndProc_common(hWnd
, uMsg
, wParam
, lParam
, TRUE
);