[USER32] Fix Task Switcher (#1086)
[reactos.git] / win32ss / user / user32 / controls / appswitch.c
1 /*
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)
9 */
10
11 //
12 // TODO:
13 // Move to Win32k.
14 // Add registry support.
15 //
16 //
17
18 #include <user32.h>
19
20 WINE_DEFAULT_DEBUG_CHANNEL(user32);
21
22 #define DIALOG_MARGIN 8 // margin of dialog contents
23
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
27
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
31
32 #define CX_ITEM_SPACE (CX_ITEM + 2 * ITEM_MARGIN)
33 #define CY_ITEM_SPACE (CY_ITEM + 2 * ITEM_MARGIN)
34
35 #define CY_TEXT_MARGIN 4 // margin height around text
36
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
40
41 // Global variables
42 HWND switchdialog = NULL;
43 HFONT dialogFont;
44 int selectedWindow = 0;
45 BOOL isOpen = FALSE;
46
47 int fontHeight=0;
48
49 WCHAR windowText[1024];
50
51 HWND windowList[MAX_WINDOWS];
52 HICON iconList[MAX_WINDOWS];
53 int windowCount = 0;
54
55 int cxBorder, cyBorder;
56 int nItems, nCols, nRows;
57 int itemsW, itemsH;
58 int totalW, totalH;
59 int xOffset, yOffset;
60 POINT ptStart;
61
62 int nShift = 0;
63
64 BOOL Esc = FALSE;
65
66 BOOL CoolSwitch = TRUE;
67 int CoolSwitchRows = 3;
68 int CoolSwitchColumns = 7;
69
70 // window style
71 const DWORD Style = WS_POPUP | WS_BORDER | WS_DISABLED;
72 const DWORD ExStyle = WS_EX_TOPMOST | WS_EX_DLGMODALFRAME | WS_EX_TOOLWINDOW;
73
74 DWORD wtodw(const WCHAR *psz)
75 {
76 const WCHAR *pch = psz;
77 DWORD Value = 0;
78 while ('0' <= *pch && *pch <= '9')
79 {
80 Value *= 10;
81 Value += *pch - L'0';
82 }
83 return Value;
84 }
85
86 BOOL LoadCoolSwitchSettings(void)
87 {
88 CoolSwitch = TRUE;
89 CoolSwitchRows = 3;
90 CoolSwitchColumns = 7;
91
92 // FIXME: load the settings from registry
93
94 TRACE("CoolSwitch: %d\n", CoolSwitch);
95 TRACE("CoolSwitchRows: %d\n", CoolSwitchRows);
96 TRACE("CoolSwitchColumns: %d\n", CoolSwitchColumns);
97
98 return TRUE;
99 }
100
101 void ResizeAndCenter(HWND hwnd, int width, int height)
102 {
103 int x, y;
104 RECT Rect;
105
106 int screenwidth = GetSystemMetrics(SM_CXSCREEN);
107 int screenheight = GetSystemMetrics(SM_CYSCREEN);
108
109 x = (screenwidth - width) / 2;
110 y = (screenheight - height) / 2;
111
112 SetRect(&Rect, x, y, x + width, y + height);
113 AdjustWindowRectEx(&Rect, Style, FALSE, ExStyle);
114
115 x = Rect.left;
116 y = Rect.top;
117 width = Rect.right - Rect.left;
118 height = Rect.bottom - Rect.top;
119 MoveWindow(hwnd, x, y, width, height, FALSE);
120
121 ptStart.x = x;
122 ptStart.y = y;
123 }
124
125 void MakeWindowActive(HWND hwnd)
126 {
127 if (IsIconic(hwnd))
128 PostMessageW(hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
129
130 BringWindowToTop(hwnd); // same as: SetWindowPos(hwnd,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); ?
131 SetForegroundWindow(hwnd);
132 }
133
134 void CompleteSwitch(BOOL doSwitch)
135 {
136 if (!isOpen)
137 return;
138
139 isOpen = FALSE;
140
141 TRACE("[ATbot] CompleteSwitch Hiding Window.\n");
142 ShowWindow(switchdialog, SW_HIDE);
143
144 if(doSwitch)
145 {
146 if(selectedWindow >= windowCount)
147 return;
148
149 // FIXME: workaround because reactos fails to activate the previous window correctly.
150 //if(selectedWindow != 0)
151 {
152 HWND hwnd = windowList[selectedWindow];
153
154 GetWindowTextW(hwnd, windowText, _countof(windowText));
155
156 TRACE("[ATbot] CompleteSwitch Switching to 0x%08x (%ls)\n", hwnd, windowText);
157
158 MakeWindowActive(hwnd);
159 }
160 }
161
162 windowCount = 0;
163 }
164
165 BOOL CALLBACK EnumerateCallback(HWND window, LPARAM lParam)
166 {
167 HICON hIcon;
168 HWND hwndOwner;
169
170 UNREFERENCED_PARAMETER(lParam);
171
172 if (!IsWindowVisible(window))
173 return TRUE;
174
175 hwndOwner = GetWindow(window, GW_OWNER);
176 if (hwndOwner && IsWindowVisible(hwndOwner))
177 return TRUE;
178
179 GetClassNameW(window, windowText, _countof(windowText));
180 if ((wcscmp(L"Shell_TrayWnd", windowText)==0) ||
181 (wcscmp(L"Progman", windowText)==0) )
182 return TRUE;
183
184 // First try to get the big icon assigned to the window
185 hIcon = (HICON)SendMessageW(window, WM_GETICON, ICON_BIG, 0);
186 if (!hIcon)
187 {
188 // If no icon is assigned, try to get the icon assigned to the windows' class
189 hIcon = (HICON)GetClassLongPtrW(window, GCL_HICON);
190 if (!hIcon)
191 {
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);
195 if (!hIcon)
196 {
197 // using windows logo icon as default
198 hIcon = gpsi->hIconWindows;
199 if (!hIcon)
200 {
201 //if all attempts to get icon fails go to the next window
202 return TRUE;
203 }
204 }
205 }
206 }
207
208 windowList[windowCount] = window;
209 iconList[windowCount] = CopyIcon(hIcon);
210
211 windowCount++;
212
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)
216 return FALSE;
217
218 return TRUE;
219 }
220
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)
225 {
226 HWND next = GetTopWindow(hwnd);
227 while (next != NULL)
228 {
229 if (!hwnd && !IsWindowVisible(next))
230 {
231 // UPDATE: Seek also the owned windows of the hidden top-level window.
232 EnumChildWindowsZOrder(next, callback, lParam);
233 }
234
235 if (!callback(next, lParam))
236 break;
237
238 next = GetWindow(next, GW_HWNDNEXT);
239 }
240 }
241
242 void ProcessMouseMessage(UINT message, LPARAM lParam)
243 {
244 int xPos = LOWORD(lParam);
245 int yPos = HIWORD(lParam);
246
247 int xIndex = (xPos - DIALOG_MARGIN) / CX_ITEM_SPACE;
248 int yIndex = (yPos - DIALOG_MARGIN) / CY_ITEM_SPACE;
249
250 if (xIndex < 0 || nCols <= xIndex ||
251 yIndex < 0 || nRows <= yIndex)
252 {
253 return;
254 }
255
256 selectedWindow = (yIndex*nCols) + xIndex;
257 if (message == WM_MOUSEMOVE)
258 {
259 InvalidateRect(switchdialog, NULL, TRUE);
260 //RedrawWindow(switchdialog, NULL, NULL, 0);
261 }
262 else
263 {
264 selectedWindow = (yIndex*nCols) + xIndex;
265 CompleteSwitch(TRUE);
266 }
267 }
268
269 void OnPaint(HWND hWnd)
270 {
271 HDC dialogDC;
272 PAINTSTRUCT paint;
273 RECT cRC, textRC;
274 int i, xPos, yPos, CharCount;
275 HFONT dcFont;
276 HICON hIcon;
277 HPEN hPen;
278 COLORREF Color;
279
280 // check
281 if (nCols == 0 || nItems == 0)
282 return;
283
284 // begin painting
285 dialogDC = BeginPaint(hWnd, &paint);
286 if (dialogDC == NULL)
287 return;
288
289 // fill the client area
290 GetClientRect(hWnd, &cRC);
291 FillRect(dialogDC, &cRC, (HBRUSH)(COLOR_3DFACE + 1));
292
293 // if the selection index exceeded the display items, then
294 // do display item shifting
295 if (selectedWindow >= nItems)
296 nShift = selectedWindow - nItems + 1;
297 else
298 nShift = 0;
299
300 for (i = 0; i < nItems; ++i)
301 {
302 // get the icon to display
303 hIcon = iconList[i + nShift];
304
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;
308
309 // centering
310 if (nItems < CoolSwitchColumns)
311 {
312 xPos += (itemsW - nItems * CX_ITEM_SPACE) / 2;
313 }
314
315 // if this position is selected,
316 if (selectedWindow == i + nShift)
317 {
318 // create a solid pen
319 Color = GetSysColor(COLOR_HIGHLIGHT);
320 hPen = CreatePen(PS_SOLID, 1, Color);
321
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);
328
329 // delete the pen
330 DeleteObject(hPen);
331 }
332
333 // draw icon
334 DrawIconEx(dialogDC, xPos + ICON_MARGIN, yPos + ICON_MARGIN,
335 hIcon, CX_ICON, CY_ICON, 0, NULL, DI_NORMAL);
336 }
337
338 // set the text rectangle
339 SetRect(&textRC, DIALOG_MARGIN, DIALOG_MARGIN + itemsH,
340 totalW - DIALOG_MARGIN, totalH - DIALOG_MARGIN);
341
342 // draw the sunken button around text
343 DrawFrameControl(dialogDC, &textRC, DFC_BUTTON,
344 DFCS_BUTTONPUSH | DFCS_PUSHED);
345
346 // get text
347 CharCount = GetWindowTextW(windowList[selectedWindow], windowText,
348 _countof(windowText));
349
350 // draw text
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);
357
358 // end painting
359 EndPaint(hWnd, &paint);
360 }
361
362 DWORD CreateSwitcherWindow(HINSTANCE hInstance)
363 {
364 switchdialog = CreateWindowExW( WS_EX_TOPMOST|WS_EX_DLGMODALFRAME|WS_EX_TOOLWINDOW,
365 WC_SWITCH,
366 L"",
367 WS_POPUP|WS_BORDER|WS_DISABLED,
368 CW_USEDEFAULT,
369 CW_USEDEFAULT,
370 400, 150,
371 NULL, NULL,
372 hInstance, NULL);
373 if (!switchdialog)
374 {
375 TRACE("[ATbot] Task Switcher Window failed to create.\n");
376 return 0;
377 }
378
379 isOpen = FALSE;
380 return 1;
381 }
382
383 DWORD GetDialogFont(VOID)
384 {
385 HDC tDC;
386 TEXTMETRIC tm;
387
388 dialogFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
389
390 tDC = GetDC(0);
391 GetTextMetrics(tDC, &tm);
392 fontHeight = tm.tmHeight;
393 ReleaseDC(0, tDC);
394
395 return 1;
396 }
397
398 void PrepareWindow(VOID)
399 {
400 nItems = windowCount;
401
402 nCols = CoolSwitchColumns;
403 nRows = (nItems + CoolSwitchColumns - 1) / CoolSwitchColumns;
404 if (nRows > CoolSwitchRows)
405 {
406 nRows = CoolSwitchRows;
407 nItems = nRows * nCols;
408 }
409
410 itemsW = nCols * CX_ITEM_SPACE;
411 itemsH = nRows * CY_ITEM_SPACE;
412
413 totalW = itemsW + 2 * DIALOG_MARGIN;
414 totalH = itemsH + 2 * DIALOG_MARGIN;
415 totalH += fontHeight + 2 * CY_TEXT_MARGIN;
416
417 ResizeAndCenter(switchdialog, totalW, totalH);
418 }
419
420 BOOL ProcessHotKey(VOID)
421 {
422 if (!isOpen)
423 {
424 windowCount=0;
425 EnumChildWindowsZOrder(NULL, EnumerateCallback, 0);
426
427 if (windowCount < 2)
428 return FALSE;
429
430 selectedWindow = 1;
431
432 TRACE("[ATbot] HotKey Received. Opening window.\n");
433 ShowWindow(switchdialog, SW_SHOWNORMAL);
434 MakeWindowActive(switchdialog);
435 isOpen = TRUE;
436 }
437 else
438 {
439 TRACE("[ATbot] HotKey Received Rotating.\n");
440 selectedWindow = (selectedWindow + 1)%windowCount;
441 InvalidateRect(switchdialog, NULL, TRUE);
442 }
443 return TRUE;
444 }
445
446 void RotateTasks(BOOL bShift)
447 {
448 HWND hwndFirst, hwndLast;
449 DWORD Size;
450
451 if (windowCount < 2 || !Esc)
452 return;
453
454 hwndFirst = windowList[0];
455 hwndLast = windowList[windowCount - 1];
456
457 if (bShift)
458 {
459 SetWindowPos(hwndLast, HWND_TOP, 0, 0, 0, 0,
460 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
461 SWP_NOOWNERZORDER | SWP_NOREPOSITION);
462
463 MakeWindowActive(hwndLast);
464
465 Size = (windowCount - 1) * sizeof(HWND);
466 MoveMemory(&windowList[1], &windowList[0], Size);
467 windowList[0] = hwndLast;
468 }
469 else
470 {
471 SetWindowPos(hwndFirst, hwndLast, 0, 0, 0, 0,
472 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
473 SWP_NOOWNERZORDER | SWP_NOREPOSITION);
474
475 MakeWindowActive(windowList[1]);
476
477 Size = (windowCount - 1) * sizeof(HWND);
478 MoveMemory(&windowList[0], &windowList[1], Size);
479 windowList[windowCount - 1] = hwndFirst;
480 }
481 }
482
483 VOID
484 DestroyAppWindows(VOID)
485 {
486 // for every item of the icon list:
487 INT i;
488 for (i = 0; i < windowCount; ++i)
489 {
490 // destroy the icon
491 DestroyIcon(iconList[i]);
492 iconList[i] = NULL;
493 }
494 }
495
496 LRESULT WINAPI DoAppSwitch( WPARAM wParam, LPARAM lParam )
497 {
498 HWND hwndActive;
499 MSG msg;
500
501 // FIXME: Is loading timing OK?
502 LoadCoolSwitchSettings();
503
504 if (!CoolSwitch)
505 return 0;
506
507 // Already in the loop.
508 if (switchdialog || Esc) return 0;
509
510 hwndActive = GetActiveWindow();
511 // Nothing is active so exit.
512 if (!hwndActive) return 0;
513
514 if (lParam == VK_ESCAPE)
515 {
516 Esc = TRUE;
517
518 windowCount = 0;
519 EnumChildWindowsZOrder(NULL, EnumerateCallback, 0);
520
521 if (windowCount < 2)
522 return 0;
523
524 RotateTasks(GetAsyncKeyState(VK_SHIFT) < 0);
525
526 hwndActive = GetActiveWindow();
527
528 if (hwndActive == NULL)
529 {
530 Esc = FALSE;
531 return 0;
532 }
533 }
534
535 // Capture current active window.
536 SetCapture( hwndActive );
537
538 switch (lParam)
539 {
540 case VK_TAB:
541 if( !CreateSwitcherWindow(User32Instance) ) goto Exit;
542 if( !GetDialogFont() ) goto Exit;
543 if( !ProcessHotKey() ) goto Exit;
544 break;
545
546 case VK_ESCAPE:
547 break;
548
549 default:
550 goto Exit;
551 }
552 // Main message loop:
553 while (1)
554 {
555 for (;;)
556 {
557 if (PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ))
558 {
559 if (!CallMsgFilterW( &msg, MSGF_NEXTWINDOW )) break;
560 /* remove the message from the queue */
561 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
562 }
563 else
564 WaitMessage();
565 }
566
567 switch (msg.message)
568 {
569 case WM_KEYUP:
570 {
571 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
572 if (msg.wParam == VK_MENU)
573 {
574 CompleteSwitch(TRUE);
575 }
576 else if (msg.wParam == VK_RETURN)
577 {
578 CompleteSwitch(TRUE);
579 }
580 else if (msg.wParam == VK_ESCAPE)
581 {
582 TRACE("DoAppSwitch VK_ESCAPE 2\n");
583 CompleteSwitch(FALSE);
584 }
585 goto Exit; //break;
586 }
587
588 case WM_SYSKEYDOWN:
589 {
590 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
591 if (HIWORD(msg.lParam) & KF_ALTDOWN)
592 {
593 if ( msg.wParam == VK_TAB )
594 {
595 if (Esc) break;
596 if (GetKeyState(VK_SHIFT) < 0)
597 {
598 selectedWindow = selectedWindow - 1;
599 if (selectedWindow < 0)
600 selectedWindow = windowCount - 1;
601 }
602 else
603 {
604 selectedWindow = (selectedWindow + 1)%windowCount;
605 }
606 InvalidateRect(switchdialog, NULL, TRUE);
607 }
608 else if ( msg.wParam == VK_ESCAPE )
609 {
610 if (!Esc) break;
611 RotateTasks(GetKeyState(VK_SHIFT) < 0);
612 }
613 }
614 break;
615 }
616
617 case WM_LBUTTONUP:
618 PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE );
619 ProcessMouseMessage(msg.message, msg.lParam);
620 goto Exit;
621
622 default:
623 if (PeekMessageW( &msg, 0, msg.message, msg.message, PM_REMOVE ))
624 {
625 TranslateMessage(&msg);
626 DispatchMessageW(&msg);
627 }
628 break;
629 }
630 }
631 Exit:
632 ReleaseCapture();
633 if (switchdialog) DestroyWindow(switchdialog);
634 if (Esc) DestroyAppWindows();
635 switchdialog = NULL;
636 selectedWindow = 0;
637 windowCount = 0;
638 Esc = FALSE;
639 return 0;
640 }
641
642 //
643 // Switch System Class Window Proc.
644 //
645 LRESULT WINAPI SwitchWndProc_common(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL unicode )
646 {
647 PWND pWnd;
648 PALTTABINFO ati;
649 pWnd = ValidateHwnd(hWnd);
650 if (pWnd)
651 {
652 if (!pWnd->fnid)
653 {
654 NtUserSetWindowFNID(hWnd, FNID_SWITCH);
655 }
656 }
657
658 switch (uMsg)
659 {
660 case WM_NCCREATE:
661 if (!(ati = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ati))))
662 return 0;
663 SetWindowLongPtrW( hWnd, 0, (LONG_PTR)ati );
664 return TRUE;
665
666 case WM_SHOWWINDOW:
667 if (wParam)
668 {
669 PrepareWindow();
670 ati = (PALTTABINFO)GetWindowLongPtrW(hWnd, 0);
671 ati->cbSize = sizeof(ALTTABINFO);
672 ati->cItems = nItems;
673 ati->cColumns = nCols;
674 ati->cRows = nRows;
675 if (nCols)
676 {
677 ati->iColFocus = (selectedWindow - nShift) % nCols;
678 ati->iRowFocus = (selectedWindow - nShift) / nCols;
679 }
680 else
681 {
682 ati->iColFocus = 0;
683 ati->iRowFocus = 0;
684 }
685 ati->cxItem = CX_ITEM_SPACE;
686 ati->cyItem = CY_ITEM_SPACE;
687 ati->ptStart = ptStart;
688 }
689 return 0;
690
691 case WM_MOUSEMOVE:
692 ProcessMouseMessage(uMsg, lParam);
693 return 0;
694
695 case WM_ACTIVATE:
696 if (wParam == WA_INACTIVE)
697 {
698 CompleteSwitch(FALSE);
699 }
700 return 0;
701
702 case WM_PAINT:
703 OnPaint(hWnd);
704 return 0;
705
706 case WM_DESTROY:
707 isOpen = FALSE;
708 ati = (PALTTABINFO)GetWindowLongPtrW(hWnd, 0);
709 HeapFree( GetProcessHeap(), 0, ati );
710 SetWindowLongPtrW( hWnd, 0, 0 );
711 DestroyAppWindows();
712 NtUserSetWindowFNID(hWnd, FNID_DESTROY);
713 return 0;
714 }
715 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
716 }
717
718 LRESULT WINAPI SwitchWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
719 {
720 return SwitchWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
721 }
722
723 LRESULT WINAPI SwitchWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
724 {
725 return SwitchWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
726 }