2eb384b6af9c4b13a5098adaac771e1fc7209e11
[reactos.git] / reactos / base / applications / taskmgr / taskmgr.c
1 /*
2 * ReactOS Task Manager
3 *
4 * taskmgr.c : Defines the entry point for the application.
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * 2005 Klemens Friedl <frik85@reactos.at>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "precomp.h"
25
26 #include "perfpage.h"
27 #include "about.h"
28 #include "affinity.h"
29 #include "debug.h"
30 #include "priority.h"
31
32 #define STATUS_WINDOW 2001
33
34 /* Global Variables: */
35 HINSTANCE hInst; /* current instance */
36
37 HWND hMainWnd; /* Main Window */
38 HWND hStatusWnd; /* Status Bar Window */
39 HWND hTabWnd; /* Tab Control Window */
40
41 HMENU hWindowMenu = NULL;
42
43 int nMinimumWidth; /* Minimum width of the dialog (OnSize()'s cx) */
44 int nMinimumHeight; /* Minimum height of the dialog (OnSize()'s cy) */
45
46 int nOldWidth; /* Holds the previous client area width */
47 int nOldHeight; /* Holds the previous client area height */
48
49 BOOL bInMenuLoop = FALSE; /* Tells us if we are in the menu loop */
50
51 TASKMANAGER_SETTINGS TaskManagerSettings;
52
53 ////////////////////////////////////////////////////////////////////////////////
54 // Taken from WinSpy++ 1.7
55 // http://www.catch22.net/software/winspy
56 // Copyright (c) 2002 by J Brown
57 //
58
59 //
60 // Copied from uxtheme.h
61 // If you have this new header, then delete these and
62 // #include <uxtheme.h> instead!
63 //
64 #define ETDT_DISABLE 0x00000001
65 #define ETDT_ENABLE 0x00000002
66 #define ETDT_USETABTEXTURE 0x00000004
67 #define ETDT_ENABLETAB (ETDT_ENABLE | ETDT_USETABTEXTURE)
68
69 //
70 typedef HRESULT (WINAPI * ETDTProc) (HWND, DWORD);
71
72 //
73 // Try to call EnableThemeDialogTexture, if uxtheme.dll is present
74 //
75 BOOL EnableDialogTheme(HWND hwnd)
76 {
77 HMODULE hUXTheme;
78 ETDTProc fnEnableThemeDialogTexture;
79
80 hUXTheme = LoadLibraryA("uxtheme.dll");
81
82 if(hUXTheme)
83 {
84 fnEnableThemeDialogTexture =
85 (ETDTProc)GetProcAddress(hUXTheme, "EnableThemeDialogTexture");
86
87 if(fnEnableThemeDialogTexture)
88 {
89 fnEnableThemeDialogTexture(hwnd, ETDT_ENABLETAB);
90
91 FreeLibrary(hUXTheme);
92 return TRUE;
93 }
94 else
95 {
96 // Failed to locate API!
97 FreeLibrary(hUXTheme);
98 return FALSE;
99 }
100 }
101 else
102 {
103 // Not running under XP? Just fail gracefully
104 return FALSE;
105 }
106 }
107
108 int APIENTRY wWinMain(HINSTANCE hInstance,
109 HINSTANCE hPrevInstance,
110 LPWSTR lpCmdLine,
111 int nCmdShow)
112 {
113 HANDLE hProcess;
114 HANDLE hToken;
115 TOKEN_PRIVILEGES tkp;
116 HANDLE hMutex;
117
118 /* check wether we're already running or not */
119 hMutex = CreateMutexW(NULL, TRUE, L"taskmgrros");
120 if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
121 {
122 /* Restore existing taskmanager and bring window to front */
123 /* Relies on the fact that the application title string and window title are the same */
124 HWND hTaskMgr;
125 TCHAR szTaskmgr[128];
126
127 LoadString(hInst, IDS_APP_TITLE, szTaskmgr, sizeof(szTaskmgr)/sizeof(TCHAR));
128 hTaskMgr = FindWindow(NULL, szTaskmgr);
129
130 if (hTaskMgr != NULL)
131 {
132 SendMessage(hTaskMgr, WM_SYSCOMMAND, SC_RESTORE, 0);
133 SetForegroundWindow(hTaskMgr);
134 }
135
136 CloseHandle(hMutex);
137 return 0;
138 }
139 else if (!hMutex)
140 {
141 return 1;
142 }
143
144 /* Initialize global variables */
145 hInst = hInstance;
146
147 /* Change our priority class to HIGH */
148 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
149 SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
150 CloseHandle(hProcess);
151
152 /* Now lets get the SE_DEBUG_NAME privilege
153 * so that we can debug processes
154 */
155
156 /* Get a token for this process. */
157 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
158 {
159 /* Get the LUID for the debug privilege. */
160 if (LookupPrivilegeValueW(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid))
161 {
162 tkp.PrivilegeCount = 1; /* one privilege to set */
163 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
164
165 /* Get the debug privilege for this process. */
166 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
167 }
168 CloseHandle(hToken);
169 }
170
171 /* Load our settings from the registry */
172 LoadSettings();
173
174 /* Initialize perf data */
175 if (!PerfDataInitialize())
176 {
177 return -1;
178 }
179
180 /*
181 * Set our shutdown parameters: we want to shutdown the very last,
182 * without displaying any end task dialog if needed.
183 */
184 SetProcessShutdownParameters(1, SHUTDOWN_NORETRY);
185
186 DialogBoxW(hInst, (LPCWSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc);
187
188 /* Save our settings to the registry */
189 SaveSettings();
190 PerfDataUninitialize();
191 CloseHandle(hMutex);
192 if (hWindowMenu)
193 DestroyMenu(hWindowMenu);
194 return 0;
195 }
196
197 /* Message handler for dialog box. */
198 INT_PTR CALLBACK
199 TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
200 {
201 #if 0
202 HDC hdc;
203 PAINTSTRUCT ps;
204 RECT rc;
205 #endif
206 LPRECT pRC;
207 LPNMHDR pnmh;
208 WINDOWPLACEMENT wp;
209
210 switch (message) {
211 case WM_INITDIALOG:
212 hMainWnd = hDlg;
213 return OnCreate(hDlg);
214
215 case WM_COMMAND:
216 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
217 EndDialog(hDlg, LOWORD(wParam));
218 return TRUE;
219 }
220 /* Process menu commands */
221 switch (LOWORD(wParam))
222 {
223 case ID_FILE_NEW:
224 TaskManager_OnFileNew();
225 break;
226 case ID_OPTIONS_ALWAYSONTOP:
227 TaskManager_OnOptionsAlwaysOnTop();
228 break;
229 case ID_OPTIONS_MINIMIZEONUSE:
230 TaskManager_OnOptionsMinimizeOnUse();
231 break;
232 case ID_OPTIONS_HIDEWHENMINIMIZED:
233 TaskManager_OnOptionsHideWhenMinimized();
234 break;
235 case ID_OPTIONS_SHOW16BITTASKS:
236 TaskManager_OnOptionsShow16BitTasks();
237 break;
238 case ID_RESTORE:
239 TaskManager_OnRestoreMainWindow();
240 break;
241 case ID_VIEW_LARGE:
242 case ID_VIEW_SMALL:
243 case ID_VIEW_DETAILS:
244 ApplicationPage_OnView(LOWORD(wParam));
245 break;
246 case ID_VIEW_SHOWKERNELTIMES:
247 PerformancePage_OnViewShowKernelTimes();
248 break;
249 case ID_VIEW_CPUHISTORY_ONEGRAPHALL:
250 PerformancePage_OnViewCPUHistoryOneGraphAll();
251 break;
252 case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU:
253 PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
254 break;
255 case ID_VIEW_UPDATESPEED_HIGH:
256 case ID_VIEW_UPDATESPEED_NORMAL:
257 case ID_VIEW_UPDATESPEED_LOW:
258 case ID_VIEW_UPDATESPEED_PAUSED:
259 TaskManager_OnViewUpdateSpeed(LOWORD(wParam));
260 break;
261 case ID_VIEW_SELECTCOLUMNS:
262 ProcessPage_OnViewSelectColumns();
263 break;
264 case ID_VIEW_REFRESH:
265 PostMessageW(hDlg, WM_TIMER, 0, 0);
266 break;
267 case ID_WINDOWS_TILEHORIZONTALLY:
268 ApplicationPage_OnWindowsTile(MDITILE_HORIZONTAL);
269 break;
270 case ID_WINDOWS_TILEVERTICALLY:
271 ApplicationPage_OnWindowsTile(MDITILE_VERTICAL);
272 break;
273 case ID_WINDOWS_MINIMIZE:
274 ApplicationPage_OnWindowsMinimize();
275 break;
276 case ID_WINDOWS_MAXIMIZE:
277 ApplicationPage_OnWindowsMaximize();
278 break;
279 case ID_WINDOWS_CASCADE:
280 ApplicationPage_OnWindowsCascade();
281 break;
282 case ID_WINDOWS_BRINGTOFRONT:
283 ApplicationPage_OnWindowsBringToFront();
284 break;
285 case ID_APPLICATION_PAGE_SWITCHTO:
286 ApplicationPage_OnSwitchTo();
287 break;
288 case ID_APPLICATION_PAGE_ENDTASK:
289 ApplicationPage_OnEndTask();
290 break;
291 case ID_APPLICATION_PAGE_GOTOPROCESS:
292 ApplicationPage_OnGotoProcess();
293 break;
294 case ID_PROCESS_PAGE_ENDPROCESS:
295 ProcessPage_OnEndProcess();
296 break;
297 case ID_PROCESS_PAGE_ENDPROCESSTREE:
298 ProcessPage_OnEndProcessTree();
299 break;
300 case ID_PROCESS_PAGE_DEBUG:
301 ProcessPage_OnDebug();
302 break;
303 case ID_PROCESS_PAGE_SETAFFINITY:
304 ProcessPage_OnSetAffinity();
305 break;
306 case ID_PROCESS_PAGE_SETPRIORITY_REALTIME:
307 DoSetPriority(REALTIME_PRIORITY_CLASS);
308 break;
309 case ID_PROCESS_PAGE_SETPRIORITY_HIGH:
310 DoSetPriority(HIGH_PRIORITY_CLASS);
311 break;
312 case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL:
313 DoSetPriority(ABOVE_NORMAL_PRIORITY_CLASS);
314 break;
315 case ID_PROCESS_PAGE_SETPRIORITY_NORMAL:
316 DoSetPriority(NORMAL_PRIORITY_CLASS);
317 break;
318 case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL:
319 DoSetPriority(BELOW_NORMAL_PRIORITY_CLASS);
320 break;
321 case ID_PROCESS_PAGE_SETPRIORITY_LOW:
322 DoSetPriority(IDLE_PRIORITY_CLASS);
323 break;
324 case ID_PROCESS_PAGE_DEBUGCHANNELS:
325 ProcessPage_OnDebugChannels();
326 break;
327
328 /* ShutDown items */
329 case ID_SHUTDOWN_STANDBY:
330 ShutDown_StandBy();
331 break;
332 case ID_SHUTDOWN_HIBERNATE:
333 ShutDown_Hibernate();
334 break;
335 case ID_SHUTDOWN_POWEROFF:
336 ShutDown_PowerOff();
337 break;
338 case ID_SHUTDOWN_REBOOT:
339 ShutDown_Reboot();
340 break;
341 case ID_SHUTDOWN_LOGOFF:
342 ShutDown_LogOffUser();
343 break;
344 case ID_SHUTDOWN_SWITCHUSER:
345 ShutDown_SwitchUser();
346 break;
347 case ID_SHUTDOWN_LOCKCOMPUTER:
348 ShutDown_LockComputer();
349 break;
350 case ID_SHUTDOWN_DISCONNECT:
351 ShutDown_Disconnect();
352 break;
353 case ID_SHUTDOWN_EJECT_COMPUTER:
354 ShutDown_EjectComputer();
355 break;
356
357 case ID_HELP_ABOUT:
358 OnAbout();
359 break;
360 case ID_FILE_EXIT:
361 EndDialog(hDlg, IDOK);
362 break;
363 }
364 break;
365
366 case WM_ONTRAYICON:
367 switch(lParam)
368 {
369 case WM_RBUTTONDOWN:
370 {
371 POINT pt;
372 BOOL OnTop;
373 HMENU hMenu, hPopupMenu;
374
375 GetCursorPos(&pt);
376
377 OnTop = ((GetWindowLongPtrW(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
378
379 hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_TRAY_POPUP));
380 hPopupMenu = GetSubMenu(hMenu, 0);
381
382 if(IsWindowVisible(hMainWnd))
383 {
384 DeleteMenu(hPopupMenu, ID_RESTORE, MF_BYCOMMAND);
385 }
386 else
387 {
388 SetMenuDefaultItem(hPopupMenu, ID_RESTORE, FALSE);
389 }
390
391 if(OnTop)
392 {
393 CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_CHECKED);
394 } else
395 {
396 CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_UNCHECKED);
397 }
398
399 SetForegroundWindow(hMainWnd);
400 TrackPopupMenuEx(hPopupMenu, 0, pt.x, pt.y, hMainWnd, NULL);
401
402 DestroyMenu(hMenu);
403 break;
404 }
405 case WM_LBUTTONDBLCLK:
406 TaskManager_OnRestoreMainWindow();
407 break;
408 }
409 break;
410
411 case WM_NOTIFY:
412 pnmh = (LPNMHDR)lParam;
413 if ((pnmh->hwndFrom == hTabWnd) &&
414 (pnmh->idFrom == IDC_TAB) &&
415 (pnmh->code == TCN_SELCHANGE))
416 {
417 TaskManager_OnTabWndSelChange();
418 }
419 break;
420 #if 0
421 case WM_NCPAINT:
422 hdc = GetDC(hDlg);
423 GetClientRect(hDlg, &rc);
424 Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
425 ReleaseDC(hDlg, hdc);
426 break;
427
428 case WM_PAINT:
429 hdc = BeginPaint(hDlg, &ps);
430 GetClientRect(hDlg, &rc);
431 Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
432 EndPaint(hDlg, &ps);
433 break;
434 #endif
435 case WM_SIZING:
436 /* Make sure the user is sizing the dialog */
437 /* in an acceptable range */
438 pRC = (LPRECT)lParam;
439 if ((wParam == WMSZ_LEFT) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_BOTTOMLEFT)) {
440 /* If the width is too small enlarge it to the minimum */
441 if (nMinimumWidth > (pRC->right - pRC->left))
442 pRC->left = pRC->right - nMinimumWidth;
443 } else {
444 /* If the width is too small enlarge it to the minimum */
445 if (nMinimumWidth > (pRC->right - pRC->left))
446 pRC->right = pRC->left + nMinimumWidth;
447 }
448 if ((wParam == WMSZ_TOP) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_TOPRIGHT)) {
449 /* If the height is too small enlarge it to the minimum */
450 if (nMinimumHeight > (pRC->bottom - pRC->top))
451 pRC->top = pRC->bottom - nMinimumHeight;
452 } else {
453 /* If the height is too small enlarge it to the minimum */
454 if (nMinimumHeight > (pRC->bottom - pRC->top))
455 pRC->bottom = pRC->top + nMinimumHeight;
456 }
457 return TRUE;
458 break;
459
460 case WM_SIZE:
461 /* Handle the window sizing in it's own function */
462 OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
463 break;
464
465 case WM_MOVE:
466 /* Handle the window moving in it's own function */
467 OnMove(wParam, LOWORD(lParam), HIWORD(lParam));
468 break;
469
470 case WM_DESTROY:
471 ShowWindow(hDlg, SW_HIDE);
472 TrayIcon_ShellRemoveTrayIcon();
473 wp.length = sizeof(WINDOWPLACEMENT);
474 GetWindowPlacement(hDlg, &wp);
475 TaskManagerSettings.Left = wp.rcNormalPosition.left;
476 TaskManagerSettings.Top = wp.rcNormalPosition.top;
477 TaskManagerSettings.Right = wp.rcNormalPosition.right;
478 TaskManagerSettings.Bottom = wp.rcNormalPosition.bottom;
479 if (IsZoomed(hDlg) || (wp.flags & WPF_RESTORETOMAXIMIZED))
480 TaskManagerSettings.Maximized = TRUE;
481 else
482 TaskManagerSettings.Maximized = FALSE;
483 return DefWindowProcW(hDlg, message, wParam, lParam);
484
485 case WM_TIMER:
486 /* Refresh the performance data */
487 PerfDataRefresh();
488 RefreshApplicationPage();
489 RefreshProcessPage();
490 RefreshPerformancePage();
491 TrayIcon_ShellUpdateTrayIcon();
492 break;
493
494 case WM_ENTERMENULOOP:
495 TaskManager_OnEnterMenuLoop(hDlg);
496 break;
497 case WM_EXITMENULOOP:
498 TaskManager_OnExitMenuLoop(hDlg);
499 break;
500 case WM_MENUSELECT:
501 TaskManager_OnMenuSelect(hDlg, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
502 break;
503 case WM_SYSCOLORCHANGE:
504 /* Forward WM_SYSCOLORCHANGE to common controls */
505 SendMessage(hApplicationPageListCtrl, WM_SYSCOLORCHANGE, 0, 0);
506 SendMessage(hProcessPageListCtrl, WM_SYSCOLORCHANGE, 0, 0);
507 SendMessage(hProcessPageHeaderCtrl, WM_SYSCOLORCHANGE, 0, 0);
508 break;
509 }
510
511 return 0;
512 }
513
514 void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
515 {
516 SetBkColor(hDC, clr);
517 ExtTextOutW(hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
518 }
519
520 void FillSolidRect2(HDC hDC, int x, int y, int cx, int cy, COLORREF clr)
521 {
522 RECT rect;
523
524 SetBkColor(hDC, clr);
525 rect.left = x;
526 rect.top = y;
527 rect.right = x + cx;
528 rect.bottom = y + cy;
529 ExtTextOutW(hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
530 }
531
532 void Draw3dRect(HDC hDC, int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
533 {
534 FillSolidRect2(hDC, x, y, cx - 1, 1, clrTopLeft);
535 FillSolidRect2(hDC, x, y, 1, cy - 1, clrTopLeft);
536 FillSolidRect2(hDC, x + cx, y, -1, cy, clrBottomRight);
537 FillSolidRect2(hDC, x, y + cy, cx, -1, clrBottomRight);
538 }
539
540 void Draw3dRect2(HDC hDC, LPRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight)
541 {
542 Draw3dRect(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
543 lpRect->bottom - lpRect->top, clrTopLeft, clrBottomRight);
544 }
545
546 static void SetUpdateSpeed(HWND hWnd)
547 {
548 /* Setup update speed (pause=fall down) */
549 switch (TaskManagerSettings.UpdateSpeed) {
550 case ID_VIEW_UPDATESPEED_HIGH:
551 SetTimer(hWnd, 1, 1000, NULL);
552 break;
553 case ID_VIEW_UPDATESPEED_NORMAL:
554 SetTimer(hWnd, 1, 2000, NULL);
555 break;
556 case ID_VIEW_UPDATESPEED_LOW:
557 SetTimer(hWnd, 1, 4000, NULL);
558 break;
559 }
560 }
561
562 BOOL OnCreate(HWND hWnd)
563 {
564 HMENU hMenu;
565 HMENU hEditMenu;
566 HMENU hViewMenu;
567 HMENU hShutMenu;
568 HMENU hUpdateSpeedMenu;
569 HMENU hCPUHistoryMenu;
570 int nActivePage;
571 int nParts[3];
572 RECT rc;
573 WCHAR szTemp[256];
574 WCHAR szLogOffItem[MAX_PATH];
575 LPWSTR lpUserName;
576 TCITEM item;
577 DWORD len = 0;
578
579 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW(hInst, MAKEINTRESOURCEW(IDI_TASKMANAGER)));
580
581 /* Initialize the Windows Common Controls DLL */
582 InitCommonControls();
583
584 /* Get the minimum window sizes */
585 GetWindowRect(hWnd, &rc);
586 nMinimumWidth = (rc.right - rc.left);
587 nMinimumHeight = (rc.bottom - rc.top);
588
589 /* Create the status bar */
590 hStatusWnd = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS, L"", hWnd, STATUS_WINDOW);
591 if(!hStatusWnd)
592 return FALSE;
593
594 /* Create the status bar panes */
595 nParts[0] = STATUS_SIZE1;
596 nParts[1] = STATUS_SIZE2;
597 nParts[2] = STATUS_SIZE3;
598 SendMessageW(hStatusWnd, SB_SETPARTS, 3, (LPARAM) (LPINT) nParts);
599
600 /* Create tab pages */
601 hTabWnd = GetDlgItem(hWnd, IDC_TAB);
602 #if 1
603 hApplicationPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE), hWnd, ApplicationPageWndProc); EnableDialogTheme(hApplicationPage);
604 hProcessPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PROCESS_PAGE), hWnd, ProcessPageWndProc); EnableDialogTheme(hProcessPage);
605 hPerformancePage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE), hWnd, PerformancePageWndProc); EnableDialogTheme(hPerformancePage);
606 #else
607 hApplicationPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE), hTabWnd, ApplicationPageWndProc); EnableDialogTheme(hApplicationPage);
608 hProcessPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PROCESS_PAGE), hTabWnd, ProcessPageWndProc); EnableDialogTheme(hProcessPage);
609 hPerformancePage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE), hTabWnd, PerformancePageWndProc); EnableDialogTheme(hPerformancePage);
610 #endif
611
612 /* Insert tabs */
613 LoadStringW(hInst, IDS_TAB_APPS, szTemp, 256);
614 memset(&item, 0, sizeof(TCITEM));
615 item.mask = TCIF_TEXT;
616 item.pszText = szTemp;
617 (void)TabCtrl_InsertItem(hTabWnd, 0, &item);
618 LoadStringW(hInst, IDS_TAB_PROCESSES, szTemp, 256);
619 memset(&item, 0, sizeof(TCITEM));
620 item.mask = TCIF_TEXT;
621 item.pszText = szTemp;
622 (void)TabCtrl_InsertItem(hTabWnd, 1, &item);
623 LoadStringW(hInst, IDS_TAB_PERFORMANCE, szTemp, 256);
624 memset(&item, 0, sizeof(TCITEM));
625 item.mask = TCIF_TEXT;
626 item.pszText = szTemp;
627 (void)TabCtrl_InsertItem(hTabWnd, 2, &item);
628
629 /* Size everything correctly */
630 GetClientRect(hWnd, &rc);
631 nOldWidth = rc.right;
632 nOldHeight = rc.bottom;
633 /* nOldStartX = rc.left; */
634 /*nOldStartY = rc.top; */
635
636 #define PAGE_OFFSET_LEFT 17
637 #define PAGE_OFFSET_TOP 72
638 #define PAGE_OFFSET_WIDTH (PAGE_OFFSET_LEFT*2)
639 #define PAGE_OFFSET_HEIGHT (PAGE_OFFSET_TOP+32)
640
641 if ((TaskManagerSettings.Left != 0) ||
642 (TaskManagerSettings.Top != 0) ||
643 (TaskManagerSettings.Right != 0) ||
644 (TaskManagerSettings.Bottom != 0))
645 {
646 MoveWindow(hWnd, TaskManagerSettings.Left, TaskManagerSettings.Top, TaskManagerSettings.Right - TaskManagerSettings.Left, TaskManagerSettings.Bottom - TaskManagerSettings.Top, TRUE);
647 #ifdef __GNUC__TEST__
648 MoveWindow(hApplicationPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
649 MoveWindow(hProcessPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
650 MoveWindow(hPerformancePage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
651 #endif
652 }
653 if (TaskManagerSettings.Maximized)
654 ShowWindow(hWnd, SW_MAXIMIZE);
655
656 /* Set the always on top style */
657 hMenu = GetMenu(hWnd);
658 hEditMenu = GetSubMenu(hMenu, 1);
659 hViewMenu = GetSubMenu(hMenu, 2);
660 hShutMenu = GetSubMenu(hMenu, 4);
661 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
662 hCPUHistoryMenu = GetSubMenu(hViewMenu, 7);
663
664 /* Check or uncheck the always on top menu item */
665 if (TaskManagerSettings.AlwaysOnTop) {
666 CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_CHECKED);
667 SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
668 } else {
669 CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_UNCHECKED);
670 SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
671 }
672
673 /* Check or uncheck the minimize on use menu item */
674 if (TaskManagerSettings.MinimizeOnUse)
675 CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_CHECKED);
676 else
677 CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_UNCHECKED);
678
679 /* Check or uncheck the hide when minimized menu item */
680 if (TaskManagerSettings.HideWhenMinimized)
681 CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_CHECKED);
682 else
683 CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_UNCHECKED);
684
685 /* Check or uncheck the show 16-bit tasks menu item */
686 if (TaskManagerSettings.Show16BitTasks)
687 CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
688 else
689 CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_UNCHECKED);
690
691 /* Set the view mode */
692 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, TaskManagerSettings.ViewMode, MF_BYCOMMAND);
693
694 if (TaskManagerSettings.ShowKernelTimes)
695 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
696 else
697 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
698
699 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, TaskManagerSettings.UpdateSpeed, MF_BYCOMMAND);
700
701 if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
702 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
703 else
704 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
705
706 nActivePage = TaskManagerSettings.ActiveTabPage;
707 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 0);
708 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 1);
709 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 2);
710 TabCtrl_SetCurFocus/*Sel*/(hTabWnd, nActivePage);
711
712 /* Set the username in the "Log Off %s" item of the Shutdown menu */
713
714 /* 1- Get the menu item text and store it temporarily */
715 GetMenuStringW(hShutMenu, ID_SHUTDOWN_LOGOFF, szTemp, 256, MF_BYCOMMAND);
716
717 /* 2- Retrieve the username length first, then allocate a buffer for it and call it again */
718 if (!GetUserNameW(NULL, &len) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
719 {
720 lpUserName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
721 if (lpUserName && GetUserNameW(lpUserName, &len))
722 {
723 _snwprintf(szLogOffItem, sizeof(szLogOffItem)/sizeof(szLogOffItem[0]), szTemp, lpUserName);
724 szLogOffItem[sizeof(szLogOffItem)/sizeof(szLogOffItem[0]) - 1] = UNICODE_NULL;
725 }
726 else
727 {
728 _snwprintf(szLogOffItem, sizeof(szLogOffItem)/sizeof(szLogOffItem[0]), szTemp, L"n/a");
729 }
730
731 if (lpUserName) HeapFree(GetProcessHeap(), 0, lpUserName);
732 }
733 else
734 {
735 _snwprintf(szLogOffItem, sizeof(szLogOffItem)/sizeof(szLogOffItem[0]), szTemp, L"n/a");
736 }
737
738 /* 3- Set the menu item text to its formatted counterpart */
739 ModifyMenuW(hShutMenu, ID_SHUTDOWN_LOGOFF, MF_BYCOMMAND | MF_STRING, ID_SHUTDOWN_LOGOFF, szLogOffItem);
740
741 /* Setup update speed */
742 SetUpdateSpeed(hWnd);
743
744 /*
745 * Refresh the performance data
746 * Sample it twice so we can establish
747 * the delta values & cpu usage
748 */
749 PerfDataRefresh();
750 PerfDataRefresh();
751
752 RefreshApplicationPage();
753 RefreshProcessPage();
754 RefreshPerformancePage();
755
756 TrayIcon_ShellAddTrayIcon();
757
758 return TRUE;
759 }
760
761 /* OnMove()
762 * This function handles all the moving events for the application
763 * It moves every child window that needs moving
764 */
765 void OnMove( WPARAM nType, int cx, int cy )
766 {
767 #ifdef __GNUC__TEST__
768 MoveWindow(hApplicationPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
769 MoveWindow(hProcessPage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
770 MoveWindow(hPerformancePage, TaskManagerSettings.Left + PAGE_OFFSET_LEFT, TaskManagerSettings.Top + PAGE_OFFSET_TOP, TaskManagerSettings.Right - TaskManagerSettings.Left - PAGE_OFFSET_WIDTH, TaskManagerSettings.Bottom - TaskManagerSettings.Top - PAGE_OFFSET_HEIGHT, FALSE);
771 #endif
772 }
773
774 /* OnSize()
775 * This function handles all the sizing events for the application
776 * It re-sizes every window, and child window that needs re-sizing
777 */
778 void OnSize( WPARAM nType, int cx, int cy )
779 {
780 int nParts[3];
781 int nXDifference;
782 int nYDifference;
783 RECT rc;
784
785 if (nType == SIZE_MINIMIZED)
786 {
787 if(TaskManagerSettings.HideWhenMinimized)
788 {
789 ShowWindow(hMainWnd, SW_HIDE);
790 }
791 return;
792 }
793
794 nXDifference = cx - nOldWidth;
795 nYDifference = cy - nOldHeight;
796 nOldWidth = cx;
797 nOldHeight = cy;
798
799 /* Update the status bar size */
800 GetWindowRect(hStatusWnd, &rc);
801 SendMessageW(hStatusWnd, WM_SIZE, nType, MAKELPARAM(cx,rc.bottom - rc.top));
802
803 /* Update the status bar pane sizes */
804 nParts[0] = bInMenuLoop ? -1 : STATUS_SIZE1;
805 nParts[1] = STATUS_SIZE2;
806 nParts[2] = cx;
807 SendMessageW(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (LPARAM) (LPINT) nParts);
808
809 /* Resize the tab control */
810 GetWindowRect(hTabWnd, &rc);
811 cx = (rc.right - rc.left) + nXDifference;
812 cy = (rc.bottom - rc.top) + nYDifference;
813 SetWindowPos(hTabWnd, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
814
815 /* Resize the application page */
816 GetWindowRect(hApplicationPage, &rc);
817 cx = (rc.right - rc.left) + nXDifference;
818 cy = (rc.bottom - rc.top) + nYDifference;
819 SetWindowPos(hApplicationPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
820
821 /* Resize the process page */
822 GetWindowRect(hProcessPage, &rc);
823 cx = (rc.right - rc.left) + nXDifference;
824 cy = (rc.bottom - rc.top) + nYDifference;
825 SetWindowPos(hProcessPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
826
827 /* Resize the performance page */
828 GetWindowRect(hPerformancePage, &rc);
829 cx = (rc.right - rc.left) + nXDifference;
830 cy = (rc.bottom - rc.top) + nYDifference;
831 SetWindowPos(hPerformancePage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
832 }
833
834 void LoadSettings(void)
835 {
836 HKEY hKey;
837 WCHAR szSubKey[] = L"Software\\ReactOS\\TaskManager";
838 int i;
839 DWORD dwSize;
840
841 /* Window size & position settings */
842 TaskManagerSettings.Maximized = FALSE;
843 TaskManagerSettings.Left = 0;
844 TaskManagerSettings.Top = 0;
845 TaskManagerSettings.Right = 0;
846 TaskManagerSettings.Bottom = 0;
847
848 /* Tab settings */
849 TaskManagerSettings.ActiveTabPage = 0;
850
851 /* Options menu settings */
852 TaskManagerSettings.AlwaysOnTop = FALSE;
853 TaskManagerSettings.MinimizeOnUse = TRUE;
854 TaskManagerSettings.HideWhenMinimized = TRUE;
855 TaskManagerSettings.Show16BitTasks = TRUE;
856
857 /* Update speed settings */
858 TaskManagerSettings.UpdateSpeed = ID_VIEW_UPDATESPEED_NORMAL;
859
860 /* Applications page settings */
861 TaskManagerSettings.ViewMode = ID_VIEW_DETAILS;
862
863 /* Processes page settings */
864 TaskManagerSettings.ShowProcessesFromAllUsers = FALSE; /* Server-only? */
865
866 for (i = 0; i < COLUMN_NMAX; i++) {
867 TaskManagerSettings.Columns[i] = ColumnPresets[i].bDefaults;
868 TaskManagerSettings.ColumnOrderArray[i] = i;
869 TaskManagerSettings.ColumnSizeArray[i] = ColumnPresets[i].size;
870 }
871
872 TaskManagerSettings.SortColumn = COLUMN_IMAGENAME;
873 TaskManagerSettings.SortAscending = TRUE;
874
875 /* Performance page settings */
876 TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
877 TaskManagerSettings.ShowKernelTimes = FALSE;
878
879 /* Open the key */
880 if (RegOpenKeyExW(HKEY_CURRENT_USER, szSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
881 return;
882 /* Read the settings */
883 dwSize = sizeof(TASKMANAGER_SETTINGS);
884 RegQueryValueExW(hKey, L"Preferences", NULL, NULL, (LPBYTE)&TaskManagerSettings, &dwSize);
885
886 /*
887 * ATM, the 'ImageName' column is always visible
888 * (and grayed in configuration dialog)
889 * This will avoid troubles if the registry gets corrupted.
890 */
891 TaskManagerSettings.Column_ImageName = TRUE;
892
893 /* Close the key */
894 RegCloseKey(hKey);
895 }
896
897 void SaveSettings(void)
898 {
899 HKEY hKey;
900 WCHAR szSubKey[] = L"Software\\ReactOS\\TaskManager";
901
902 /* Open (or create) the key */
903 if (RegCreateKeyExW(HKEY_CURRENT_USER, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
904 return;
905 /* Save the settings */
906 RegSetValueExW(hKey, L"Preferences", 0, REG_BINARY, (LPBYTE)&TaskManagerSettings, sizeof(TASKMANAGER_SETTINGS));
907 /* Close the key */
908 RegCloseKey(hKey);
909 }
910
911 void TaskManager_OnRestoreMainWindow(void)
912 {
913 //HMENU hMenu, hOptionsMenu;
914 BOOL OnTop;
915
916 //hMenu = GetMenu(hMainWnd);
917 //hOptionsMenu = GetSubMenu(hMenu, OPTIONS_MENU_INDEX);
918 OnTop = ((GetWindowLongPtrW(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
919
920 OpenIcon(hMainWnd);
921 SetForegroundWindow(hMainWnd);
922 SetWindowPos(hMainWnd, (OnTop ? HWND_TOPMOST : HWND_TOP), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
923 }
924
925 void TaskManager_OnEnterMenuLoop(HWND hWnd)
926 {
927 int nParts;
928
929 /* Update the status bar pane sizes */
930 nParts = -1;
931 SendMessageW(hStatusWnd, SB_SETPARTS, 1, (LPARAM) (LPINT)&nParts);
932 bInMenuLoop = TRUE;
933 SendMessageW(hStatusWnd, SB_SETTEXT, (WPARAM)0, (LPARAM)L"");
934 }
935
936 void TaskManager_OnExitMenuLoop(HWND hWnd)
937 {
938 RECT rc;
939 int nParts[3];
940 WCHAR text[260];
941 WCHAR szCpuUsage[256], szProcesses[256];
942
943 LoadStringW(hInst, IDS_STATUS_CPUUSAGE, szCpuUsage, 256);
944 LoadStringW(hInst, IDS_STATUS_PROCESSES, szProcesses, 256);
945
946 bInMenuLoop = FALSE;
947 /* Update the status bar pane sizes */
948 GetClientRect(hWnd, &rc);
949 nParts[0] = STATUS_SIZE1;
950 nParts[1] = STATUS_SIZE2;
951 nParts[2] = rc.right;
952 SendMessageW(hStatusWnd, SB_SETPARTS, 3, (LPARAM) (LPINT) nParts);
953 SendMessageW(hStatusWnd, SB_SETTEXT, 0, (LPARAM)L"");
954 wsprintfW(text, szCpuUsage, PerfDataGetProcessorUsage());
955 SendMessageW(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
956 wsprintfW(text, szProcesses, PerfDataGetProcessCount());
957 SendMessageW(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
958 }
959
960 void TaskManager_OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
961 {
962 WCHAR str[100];
963
964 wcscpy(str, L"");
965 if (LoadStringW(hInst, nItemID, str, 100)) {
966 /* load appropriate string */
967 LPWSTR lpsz = str;
968 /* first newline terminates actual string */
969 lpsz = wcschr(lpsz, '\n');
970 if (lpsz != NULL)
971 *lpsz = '\0';
972 }
973 SendMessageW(hStatusWnd, SB_SETTEXT, 0, (LPARAM)str);
974 }
975
976 void TaskManager_OnViewUpdateSpeed(DWORD dwSpeed)
977 {
978 HMENU hMenu;
979 HMENU hViewMenu;
980 HMENU hUpdateSpeedMenu;
981
982 hMenu = GetMenu(hMainWnd);
983 hViewMenu = GetSubMenu(hMenu, 2);
984 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
985
986 TaskManagerSettings.UpdateSpeed = dwSpeed;
987 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, dwSpeed, MF_BYCOMMAND);
988
989 KillTimer(hMainWnd, 1);
990
991 SetUpdateSpeed(hMainWnd);
992 }
993
994 void TaskManager_OnTabWndSelChange(void)
995 {
996 int i;
997 HMENU hMenu;
998 HMENU hOptionsMenu;
999 HMENU hViewMenu;
1000 HMENU hSubMenu;
1001 WCHAR szTemp[256];
1002 SYSTEM_INFO sysInfo;
1003
1004 hMenu = GetMenu(hMainWnd);
1005 hViewMenu = GetSubMenu(hMenu, 2);
1006 hOptionsMenu = GetSubMenu(hMenu, 1);
1007 TaskManagerSettings.ActiveTabPage = TabCtrl_GetCurSel(hTabWnd);
1008 for (i = GetMenuItemCount(hViewMenu) - 1; i > 2; i--) {
1009 hSubMenu = GetSubMenu(hViewMenu, i);
1010 if (hSubMenu)
1011 DestroyMenu(hSubMenu);
1012 RemoveMenu(hViewMenu, i, MF_BYPOSITION);
1013 }
1014 RemoveMenu(hOptionsMenu, 3, MF_BYPOSITION);
1015 if (hWindowMenu)
1016 DestroyMenu(hWindowMenu);
1017 switch (TaskManagerSettings.ActiveTabPage) {
1018 case 0:
1019 ShowWindow(hApplicationPage, SW_SHOW);
1020 ShowWindow(hProcessPage, SW_HIDE);
1021 ShowWindow(hPerformancePage, SW_HIDE);
1022 BringWindowToTop(hApplicationPage);
1023
1024 LoadStringW(hInst, IDS_MENU_LARGEICONS, szTemp, 256);
1025 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_LARGE, szTemp);
1026
1027 LoadStringW(hInst, IDS_MENU_SMALLICONS, szTemp, 256);
1028 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SMALL, szTemp);
1029
1030 LoadStringW(hInst, IDS_MENU_DETAILS, szTemp, 256);
1031 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_DETAILS, szTemp);
1032
1033 if (GetMenuItemCount(hMenu) <= 5) {
1034 hWindowMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_WINDOWSMENU));
1035
1036 LoadStringW(hInst, IDS_MENU_WINDOWS, szTemp, 256);
1037 InsertMenuW(hMenu, 3, MF_BYPOSITION|MF_POPUP, (UINT_PTR) hWindowMenu, szTemp);
1038
1039 DrawMenuBar(hMainWnd);
1040 }
1041 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, TaskManagerSettings.ViewMode, MF_BYCOMMAND);
1042
1043 /*
1044 * Give the application list control focus
1045 */
1046 SetFocus(hApplicationPageListCtrl);
1047 break;
1048
1049 case 1:
1050 ShowWindow(hApplicationPage, SW_HIDE);
1051 ShowWindow(hProcessPage, SW_SHOW);
1052 ShowWindow(hPerformancePage, SW_HIDE);
1053 BringWindowToTop(hProcessPage);
1054
1055 LoadStringW(hInst, IDS_MENU_SELECTCOLUMNS, szTemp, 256);
1056 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SELECTCOLUMNS, szTemp);
1057
1058 LoadStringW(hInst, IDS_MENU_16BITTASK, szTemp, 256);
1059 AppendMenuW(hOptionsMenu, MF_STRING, ID_OPTIONS_SHOW16BITTASKS, szTemp);
1060
1061 if (TaskManagerSettings.Show16BitTasks)
1062 CheckMenuItem(hOptionsMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
1063 if (GetMenuItemCount(hMenu) > 5)
1064 {
1065 DeleteMenu(hMenu, 3, MF_BYPOSITION);
1066 DrawMenuBar(hMainWnd);
1067 }
1068 /*
1069 * Give the process list control focus
1070 */
1071 SetFocus(hProcessPageListCtrl);
1072 break;
1073
1074 case 2:
1075 ShowWindow(hApplicationPage, SW_HIDE);
1076 ShowWindow(hProcessPage, SW_HIDE);
1077 ShowWindow(hPerformancePage, SW_SHOW);
1078 BringWindowToTop(hPerformancePage);
1079 if (GetMenuItemCount(hMenu) > 5) {
1080 DeleteMenu(hMenu, 3, MF_BYPOSITION);
1081 DrawMenuBar(hMainWnd);
1082 }
1083
1084 GetSystemInfo(&sysInfo);
1085
1086 /* Hide CPU graph options on single CPU systems */
1087 if (sysInfo.dwNumberOfProcessors > 1)
1088 {
1089 hSubMenu = CreatePopupMenu();
1090
1091 LoadStringW(hInst, IDS_MENU_ONEGRAPHALLCPUS, szTemp, 256);
1092 AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHALL, szTemp);
1093
1094 LoadStringW(hInst, IDS_MENU_ONEGRAPHPERCPU, szTemp, 256);
1095 AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, szTemp);
1096
1097 LoadStringW(hInst, IDS_MENU_CPUHISTORY, szTemp, 256);
1098 AppendMenuW(hViewMenu, MF_STRING|MF_POPUP, (UINT_PTR) hSubMenu, szTemp);
1099
1100 if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
1101 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
1102 else
1103 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
1104 }
1105
1106 LoadStringW(hInst, IDS_MENU_SHOWKERNELTIMES, szTemp, 256);
1107 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SHOWKERNELTIMES, szTemp);
1108
1109 if (TaskManagerSettings.ShowKernelTimes)
1110 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
1111 else
1112 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
1113
1114 /*
1115 * Give the tab control focus
1116 */
1117 SetFocus(hTabWnd);
1118 break;
1119 }
1120 }
1121
1122 VOID ShowWin32Error(DWORD dwError)
1123 {
1124 LPWSTR lpMessageBuffer;
1125
1126 if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1127 NULL,
1128 dwError,
1129 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1130 (LPWSTR)&lpMessageBuffer,
1131 0, NULL) != 0)
1132 {
1133 MessageBoxW(hMainWnd, lpMessageBuffer, NULL, MB_OK | MB_ICONERROR);
1134 if (lpMessageBuffer) LocalFree(lpMessageBuffer);
1135 }
1136 }
1137
1138 LPWSTR GetLastErrorText(LPWSTR lpszBuf, DWORD dwSize)
1139 {
1140 DWORD dwRet;
1141 LPWSTR lpszTemp = NULL;
1142
1143 dwRet = FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
1144 NULL,
1145 GetLastError(),
1146 LANG_NEUTRAL,
1147 (LPWSTR)&lpszTemp,
1148 0,
1149 NULL );
1150
1151 /* supplied buffer is not long enough */
1152 if (!dwRet || ( (long)dwSize < (long)dwRet+14)) {
1153 lpszBuf[0] = L'\0';
1154 } else {
1155 lpszTemp[lstrlenW(lpszTemp)-2] = L'\0'; /*remove cr and newline character */
1156 wsprintfW(lpszBuf, L"%s (0x%x)", lpszTemp, (int)GetLastError());
1157 }
1158 if (lpszTemp) {
1159 LocalFree((HLOCAL)lpszTemp);
1160 }
1161 return lpszBuf;
1162 }
1163
1164 DWORD EndLocalThread(HANDLE *hThread, DWORD dwThread)
1165 {
1166 DWORD dwExitCodeThread = 0;
1167
1168 if (*hThread != NULL) {
1169 PostThreadMessage(dwThread,WM_QUIT,0,0);
1170 for (;;) {
1171 MSG msg;
1172
1173 if (WAIT_OBJECT_0 == WaitForSingleObject(*hThread, 500))
1174 break;
1175 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1176 TranslateMessage(&msg);
1177 DispatchMessage(&msg);
1178 }
1179 }
1180 GetExitCodeThread(*hThread, &dwExitCodeThread);
1181 CloseHandle(*hThread);
1182 *hThread = NULL;
1183 }
1184 return dwExitCodeThread;
1185 }
1186