4 * taskmgr.c : Defines the entry point for the application.
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * 2005 Klemens Friedl <frik85@reactos.at>
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.
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.
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
26 #define STATUS_WINDOW 2001
28 /* Global Variables: */
29 HINSTANCE hInst
; /* current instance */
31 HWND hMainWnd
; /* Main Window */
32 HWND hStatusWnd
; /* Status Bar Window */
33 HWND hTabWnd
; /* Tab Control Window */
35 int nMinimumWidth
; /* Minimum width of the dialog (OnSize()'s cx) */
36 int nMinimumHeight
; /* Minimum height of the dialog (OnSize()'s cy) */
38 int nOldWidth
; /* Holds the previous client area width */
39 int nOldHeight
; /* Holds the previous client area height */
41 BOOL bInMenuLoop
= FALSE
; /* Tells us if we are in the menu loop */
43 TASKMANAGER_SETTINGS TaskManagerSettings
;
45 ////////////////////////////////////////////////////////////////////////////////
46 // Taken from WinSpy++ 1.7
47 // http://www.catch22.net/software/winspy
48 // Copyright (c) 2002 by J Brown
52 // Copied from uxtheme.h
53 // If you have this new header, then delete these and
54 // #include <uxtheme.h> instead!
56 #define ETDT_DISABLE 0x00000001
57 #define ETDT_ENABLE 0x00000002
58 #define ETDT_USETABTEXTURE 0x00000004
59 #define ETDT_ENABLETAB (ETDT_ENABLE | ETDT_USETABTEXTURE)
62 typedef HRESULT (WINAPI
* ETDTProc
) (HWND
, DWORD
);
65 // Try to call EnableThemeDialogTexture, if uxtheme.dll is present
67 BOOL
EnableDialogTheme(HWND hwnd
)
70 ETDTProc fnEnableThemeDialogTexture
;
72 hUXTheme
= LoadLibraryA("uxtheme.dll");
76 fnEnableThemeDialogTexture
=
77 (ETDTProc
)GetProcAddress(hUXTheme
, "EnableThemeDialogTexture");
79 if(fnEnableThemeDialogTexture
)
81 fnEnableThemeDialogTexture(hwnd
, ETDT_ENABLETAB
);
83 FreeLibrary(hUXTheme
);
88 // Failed to locate API!
89 FreeLibrary(hUXTheme
);
95 // Not running under XP? Just fail gracefully
100 int APIENTRY
wWinMain(HINSTANCE hInstance
,
101 HINSTANCE hPrevInstance
,
107 TOKEN_PRIVILEGES tkp
;
110 /* check wether we're already running or not */
111 hMutex
= CreateMutexW(NULL
, TRUE
, L
"taskmgrros");
112 if (hMutex
&& GetLastError() == ERROR_ALREADY_EXISTS
)
114 /* Restore existing taskmanager and bring window to front */
115 /* Relies on the fact that the application title string and window title are the same */
117 TCHAR szTaskmgr
[128];
119 LoadString(hInst
, IDS_APP_TITLE
, szTaskmgr
, sizeof(szTaskmgr
)/sizeof(TCHAR
));
120 hTaskMgr
= FindWindow(NULL
, szTaskmgr
);
122 if (hTaskMgr
!= NULL
)
124 SendMessage(hTaskMgr
, WM_SYSCOMMAND
, SC_RESTORE
, 0);
125 SetForegroundWindow(hTaskMgr
);
134 /* Initialize global variables */
137 /* Change our priority class to HIGH */
138 hProcess
= OpenProcess(PROCESS_ALL_ACCESS
, FALSE
, GetCurrentProcessId());
139 SetPriorityClass(hProcess
, HIGH_PRIORITY_CLASS
);
140 CloseHandle(hProcess
);
142 /* Now lets get the SE_DEBUG_NAME privilege
143 * so that we can debug processes
146 /* Get a token for this process. */
147 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
)) {
148 /* Get the LUID for the debug privilege. */
149 LookupPrivilegeValueW(NULL
, SE_DEBUG_NAME
, &tkp
.Privileges
[0].Luid
);
151 tkp
.PrivilegeCount
= 1; /* one privilege to set */
152 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
154 /* Get the debug privilege for this process. */
155 AdjustTokenPrivileges(hToken
, FALSE
, &tkp
, 0, (PTOKEN_PRIVILEGES
)NULL
, 0);
159 /* Load our settings from the registry */
162 /* Initialize perf data */
163 if (!PerfDataInitialize()) {
167 DialogBoxW(hInst
, (LPCWSTR
)IDD_TASKMGR_DIALOG
, NULL
, TaskManagerWndProc
);
169 /* Save our settings to the registry */
171 PerfDataUninitialize();
175 /* Message handler for dialog box. */
177 TaskManagerWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
191 return OnCreate(hDlg
);
194 if (LOWORD(wParam
) == IDOK
|| LOWORD(wParam
) == IDCANCEL
) {
195 EndDialog(hDlg
, LOWORD(wParam
));
198 /* Process menu commands */
199 switch (LOWORD(wParam
))
202 TaskManager_OnFileNew();
204 case ID_OPTIONS_ALWAYSONTOP
:
205 TaskManager_OnOptionsAlwaysOnTop();
207 case ID_OPTIONS_MINIMIZEONUSE
:
208 TaskManager_OnOptionsMinimizeOnUse();
210 case ID_OPTIONS_HIDEWHENMINIMIZED
:
211 TaskManager_OnOptionsHideWhenMinimized();
213 case ID_OPTIONS_SHOW16BITTASKS
:
214 TaskManager_OnOptionsShow16BitTasks();
217 TaskManager_OnRestoreMainWindow();
221 case ID_VIEW_DETAILS
:
222 ApplicationPage_OnView(LOWORD(wParam
));
224 case ID_VIEW_SHOWKERNELTIMES
:
225 PerformancePage_OnViewShowKernelTimes();
227 case ID_VIEW_CPUHISTORY_ONEGRAPHALL
:
228 PerformancePage_OnViewCPUHistoryOneGraphAll();
230 case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
:
231 PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
233 case ID_VIEW_UPDATESPEED_HIGH
:
234 case ID_VIEW_UPDATESPEED_NORMAL
:
235 case ID_VIEW_UPDATESPEED_LOW
:
236 case ID_VIEW_UPDATESPEED_PAUSED
:
237 TaskManager_OnViewUpdateSpeed(LOWORD(wParam
));
239 case ID_VIEW_SELECTCOLUMNS
:
240 ProcessPage_OnViewSelectColumns();
242 case ID_VIEW_REFRESH
:
243 PostMessageW(hDlg
, WM_TIMER
, 0, 0);
245 case ID_WINDOWS_TILEHORIZONTALLY
:
246 ApplicationPage_OnWindowsTile(MDITILE_HORIZONTAL
);
248 case ID_WINDOWS_TILEVERTICALLY
:
249 ApplicationPage_OnWindowsTile(MDITILE_VERTICAL
);
251 case ID_WINDOWS_MINIMIZE
:
252 ApplicationPage_OnWindowsMinimize();
254 case ID_WINDOWS_MAXIMIZE
:
255 ApplicationPage_OnWindowsMaximize();
257 case ID_WINDOWS_CASCADE
:
258 ApplicationPage_OnWindowsCascade();
260 case ID_WINDOWS_BRINGTOFRONT
:
261 ApplicationPage_OnWindowsBringToFront();
263 case ID_APPLICATION_PAGE_SWITCHTO
:
264 ApplicationPage_OnSwitchTo();
266 case ID_APPLICATION_PAGE_ENDTASK
:
267 ApplicationPage_OnEndTask();
269 case ID_APPLICATION_PAGE_GOTOPROCESS
:
270 ApplicationPage_OnGotoProcess();
272 case ID_PROCESS_PAGE_ENDPROCESS
:
273 ProcessPage_OnEndProcess();
275 case ID_PROCESS_PAGE_ENDPROCESSTREE
:
276 ProcessPage_OnEndProcessTree();
278 case ID_PROCESS_PAGE_DEBUG
:
279 ProcessPage_OnDebug();
281 case ID_PROCESS_PAGE_SETAFFINITY
:
282 ProcessPage_OnSetAffinity();
284 case ID_PROCESS_PAGE_SETPRIORITY_REALTIME
:
285 DoSetPriority(REALTIME_PRIORITY_CLASS
);
287 case ID_PROCESS_PAGE_SETPRIORITY_HIGH
:
288 DoSetPriority(HIGH_PRIORITY_CLASS
);
290 case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL
:
291 DoSetPriority(ABOVE_NORMAL_PRIORITY_CLASS
);
293 case ID_PROCESS_PAGE_SETPRIORITY_NORMAL
:
294 DoSetPriority(NORMAL_PRIORITY_CLASS
);
296 case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL
:
297 DoSetPriority(BELOW_NORMAL_PRIORITY_CLASS
);
299 case ID_PROCESS_PAGE_SETPRIORITY_LOW
:
300 DoSetPriority(IDLE_PRIORITY_CLASS
);
302 case ID_PROCESS_PAGE_DEBUGCHANNELS
:
303 ProcessPage_OnDebugChannels();
309 EndDialog(hDlg
, IDOK
);
321 HMENU hMenu
, hPopupMenu
;
325 OnTop
= ((GetWindowLongPtrW(hMainWnd
, GWL_EXSTYLE
) & WS_EX_TOPMOST
) != 0);
327 hMenu
= LoadMenuW(hInst
, MAKEINTRESOURCEW(IDR_TRAY_POPUP
));
328 hPopupMenu
= GetSubMenu(hMenu
, 0);
330 if(IsWindowVisible(hMainWnd
))
332 DeleteMenu(hPopupMenu
, ID_RESTORE
, MF_BYCOMMAND
);
336 SetMenuDefaultItem(hPopupMenu
, ID_RESTORE
, FALSE
);
341 CheckMenuItem(hPopupMenu
, ID_OPTIONS_ALWAYSONTOP
, MF_BYCOMMAND
| MF_CHECKED
);
344 CheckMenuItem(hPopupMenu
, ID_OPTIONS_ALWAYSONTOP
, MF_BYCOMMAND
| MF_UNCHECKED
);
347 SetForegroundWindow(hMainWnd
);
348 TrackPopupMenuEx(hPopupMenu
, 0, pt
.x
, pt
.y
, hMainWnd
, NULL
);
353 case WM_LBUTTONDBLCLK
:
354 TaskManager_OnRestoreMainWindow();
360 pnmh
= (LPNMHDR
)lParam
;
361 if ((pnmh
->hwndFrom
== hTabWnd
) &&
362 (pnmh
->idFrom
== IDC_TAB
) &&
363 (pnmh
->code
== TCN_SELCHANGE
))
365 TaskManager_OnTabWndSelChange();
371 GetClientRect(hDlg
, &rc
);
372 Draw3dRect(hdc
, rc
.left
, rc
.top
, rc
.right
, rc
.top
+ 2, GetSysColor(COLOR_3DSHADOW
), GetSysColor(COLOR_3DHILIGHT
));
373 ReleaseDC(hDlg
, hdc
);
377 hdc
= BeginPaint(hDlg
, &ps
);
378 GetClientRect(hDlg
, &rc
);
379 Draw3dRect(hdc
, rc
.left
, rc
.top
, rc
.right
, rc
.top
+ 2, GetSysColor(COLOR_3DSHADOW
), GetSysColor(COLOR_3DHILIGHT
));
384 /* Make sure the user is sizing the dialog */
385 /* in an acceptable range */
386 pRC
= (LPRECT
)lParam
;
387 if ((wParam
== WMSZ_LEFT
) || (wParam
== WMSZ_TOPLEFT
) || (wParam
== WMSZ_BOTTOMLEFT
)) {
388 /* If the width is too small enlarge it to the minimum */
389 if (nMinimumWidth
> (pRC
->right
- pRC
->left
))
390 pRC
->left
= pRC
->right
- nMinimumWidth
;
392 /* If the width is too small enlarge it to the minimum */
393 if (nMinimumWidth
> (pRC
->right
- pRC
->left
))
394 pRC
->right
= pRC
->left
+ nMinimumWidth
;
396 if ((wParam
== WMSZ_TOP
) || (wParam
== WMSZ_TOPLEFT
) || (wParam
== WMSZ_TOPRIGHT
)) {
397 /* If the height is too small enlarge it to the minimum */
398 if (nMinimumHeight
> (pRC
->bottom
- pRC
->top
))
399 pRC
->top
= pRC
->bottom
- nMinimumHeight
;
401 /* If the height is too small enlarge it to the minimum */
402 if (nMinimumHeight
> (pRC
->bottom
- pRC
->top
))
403 pRC
->bottom
= pRC
->top
+ nMinimumHeight
;
409 /* Handle the window sizing in it's own function */
410 OnSize(wParam
, LOWORD(lParam
), HIWORD(lParam
));
414 /* Handle the window moving in it's own function */
415 OnMove(wParam
, LOWORD(lParam
), HIWORD(lParam
));
419 ShowWindow(hDlg
, SW_HIDE
);
420 TrayIcon_ShellRemoveTrayIcon();
421 wp
.length
= sizeof(WINDOWPLACEMENT
);
422 GetWindowPlacement(hDlg
, &wp
);
423 TaskManagerSettings
.Left
= wp
.rcNormalPosition
.left
;
424 TaskManagerSettings
.Top
= wp
.rcNormalPosition
.top
;
425 TaskManagerSettings
.Right
= wp
.rcNormalPosition
.right
;
426 TaskManagerSettings
.Bottom
= wp
.rcNormalPosition
.bottom
;
427 if (IsZoomed(hDlg
) || (wp
.flags
& WPF_RESTORETOMAXIMIZED
))
428 TaskManagerSettings
.Maximized
= TRUE
;
430 TaskManagerSettings
.Maximized
= FALSE
;
431 return DefWindowProcW(hDlg
, message
, wParam
, lParam
);
434 /* Refresh the performance data */
436 RefreshApplicationPage();
437 RefreshProcessPage();
438 RefreshPerformancePage();
439 TrayIcon_ShellUpdateTrayIcon();
442 case WM_ENTERMENULOOP
:
443 TaskManager_OnEnterMenuLoop(hDlg
);
445 case WM_EXITMENULOOP
:
446 TaskManager_OnExitMenuLoop(hDlg
);
449 TaskManager_OnMenuSelect(hDlg
, LOWORD(wParam
), HIWORD(wParam
), (HMENU
)lParam
);
451 case WM_SYSCOLORCHANGE
:
452 /* Forward WM_SYSCOLORCHANGE to common controls */
453 SendMessage(hApplicationPageListCtrl
, WM_SYSCOLORCHANGE
, 0, 0);
454 SendMessage(hProcessPageListCtrl
, WM_SYSCOLORCHANGE
, 0, 0);
455 SendMessage(hProcessPageHeaderCtrl
, WM_SYSCOLORCHANGE
, 0, 0);
462 void FillSolidRect(HDC hDC
, LPCRECT lpRect
, COLORREF clr
)
464 SetBkColor(hDC
, clr
);
465 ExtTextOutW(hDC
, 0, 0, ETO_OPAQUE
, lpRect
, NULL
, 0, NULL
);
468 void FillSolidRect2(HDC hDC
, int x
, int y
, int cx
, int cy
, COLORREF clr
)
472 SetBkColor(hDC
, clr
);
476 rect
.bottom
= y
+ cy
;
477 ExtTextOutW(hDC
, 0, 0, ETO_OPAQUE
, &rect
, NULL
, 0, NULL
);
480 void Draw3dRect(HDC hDC
, int x
, int y
, int cx
, int cy
, COLORREF clrTopLeft
, COLORREF clrBottomRight
)
482 FillSolidRect2(hDC
, x
, y
, cx
- 1, 1, clrTopLeft
);
483 FillSolidRect2(hDC
, x
, y
, 1, cy
- 1, clrTopLeft
);
484 FillSolidRect2(hDC
, x
+ cx
, y
, -1, cy
, clrBottomRight
);
485 FillSolidRect2(hDC
, x
, y
+ cy
, cx
, -1, clrBottomRight
);
488 void Draw3dRect2(HDC hDC
, LPRECT lpRect
, COLORREF clrTopLeft
, COLORREF clrBottomRight
)
490 Draw3dRect(hDC
, lpRect
->left
, lpRect
->top
, lpRect
->right
- lpRect
->left
,
491 lpRect
->bottom
- lpRect
->top
, clrTopLeft
, clrBottomRight
);
494 static void SetUpdateSpeed(HWND hWnd
)
496 /* Setup update speed (pause=fall down) */
497 switch (TaskManagerSettings
.UpdateSpeed
) {
498 case ID_VIEW_UPDATESPEED_HIGH
:
499 SetTimer(hWnd
, 1, 1000, NULL
);
501 case ID_VIEW_UPDATESPEED_NORMAL
:
502 SetTimer(hWnd
, 1, 2000, NULL
);
504 case ID_VIEW_UPDATESPEED_LOW
:
505 SetTimer(hWnd
, 1, 4000, NULL
);
510 BOOL
OnCreate(HWND hWnd
)
515 HMENU hUpdateSpeedMenu
;
516 HMENU hCPUHistoryMenu
;
523 SendMessageW(hMainWnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)LoadIconW(hInst
, MAKEINTRESOURCEW(IDI_TASKMANAGER
)));
525 /* Initialize the Windows Common Controls DLL */
526 InitCommonControls();
528 /* Get the minimum window sizes */
529 GetWindowRect(hWnd
, &rc
);
530 nMinimumWidth
= (rc
.right
- rc
.left
);
531 nMinimumHeight
= (rc
.bottom
- rc
.top
);
533 /* Create the status bar */
534 hStatusWnd
= CreateStatusWindow(WS_VISIBLE
|WS_CHILD
|WS_CLIPSIBLINGS
|SBT_NOBORDERS
, L
"", hWnd
, STATUS_WINDOW
);
538 /* Create the status bar panes */
539 nParts
[0] = STATUS_SIZE1
;
540 nParts
[1] = STATUS_SIZE2
;
541 nParts
[2] = STATUS_SIZE3
;
542 SendMessageW(hStatusWnd
, SB_SETPARTS
, 3, (LPARAM
) (LPINT
) nParts
);
544 /* Create tab pages */
545 hTabWnd
= GetDlgItem(hWnd
, IDC_TAB
);
547 hApplicationPage
= CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE
), hWnd
, ApplicationPageWndProc
); EnableDialogTheme(hApplicationPage
);
548 hProcessPage
= CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_PROCESS_PAGE
), hWnd
, ProcessPageWndProc
); EnableDialogTheme(hProcessPage
);
549 hPerformancePage
= CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE
), hWnd
, PerformancePageWndProc
); EnableDialogTheme(hPerformancePage
);
551 hApplicationPage
= CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE
), hTabWnd
, ApplicationPageWndProc
); EnableDialogTheme(hApplicationPage
);
552 hProcessPage
= CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_PROCESS_PAGE
), hTabWnd
, ProcessPageWndProc
); EnableDialogTheme(hProcessPage
);
553 hPerformancePage
= CreateDialogW(hInst
, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE
), hTabWnd
, PerformancePageWndProc
); EnableDialogTheme(hPerformancePage
);
557 LoadStringW(hInst
, IDS_TAB_APPS
, szTemp
, 256);
558 memset(&item
, 0, sizeof(TCITEM
));
559 item
.mask
= TCIF_TEXT
;
560 item
.pszText
= szTemp
;
561 (void)TabCtrl_InsertItem(hTabWnd
, 0, &item
);
562 LoadStringW(hInst
, IDS_TAB_PROCESSES
, szTemp
, 256);
563 memset(&item
, 0, sizeof(TCITEM
));
564 item
.mask
= TCIF_TEXT
;
565 item
.pszText
= szTemp
;
566 (void)TabCtrl_InsertItem(hTabWnd
, 1, &item
);
567 LoadStringW(hInst
, IDS_TAB_PERFORMANCE
, szTemp
, 256);
568 memset(&item
, 0, sizeof(TCITEM
));
569 item
.mask
= TCIF_TEXT
;
570 item
.pszText
= szTemp
;
571 (void)TabCtrl_InsertItem(hTabWnd
, 2, &item
);
573 /* Size everything correctly */
574 GetClientRect(hWnd
, &rc
);
575 nOldWidth
= rc
.right
;
576 nOldHeight
= rc
.bottom
;
577 /* nOldStartX = rc.left; */
578 /*nOldStartY = rc.top; */
580 #define PAGE_OFFSET_LEFT 17
581 #define PAGE_OFFSET_TOP 72
582 #define PAGE_OFFSET_WIDTH (PAGE_OFFSET_LEFT*2)
583 #define PAGE_OFFSET_HEIGHT (PAGE_OFFSET_TOP+32)
585 if ((TaskManagerSettings
.Left
!= 0) ||
586 (TaskManagerSettings
.Top
!= 0) ||
587 (TaskManagerSettings
.Right
!= 0) ||
588 (TaskManagerSettings
.Bottom
!= 0))
590 MoveWindow(hWnd
, TaskManagerSettings
.Left
, TaskManagerSettings
.Top
, TaskManagerSettings
.Right
- TaskManagerSettings
.Left
, TaskManagerSettings
.Bottom
- TaskManagerSettings
.Top
, TRUE
);
591 #ifdef __GNUC__TEST__
592 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
);
593 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
);
594 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
);
597 if (TaskManagerSettings
.Maximized
)
598 ShowWindow(hWnd
, SW_MAXIMIZE
);
600 /* Set the always on top style */
601 hMenu
= GetMenu(hWnd
);
602 hEditMenu
= GetSubMenu(hMenu
, 1);
603 hViewMenu
= GetSubMenu(hMenu
, 2);
604 hUpdateSpeedMenu
= GetSubMenu(hViewMenu
, 1);
605 hCPUHistoryMenu
= GetSubMenu(hViewMenu
, 7);
607 /* Check or uncheck the always on top menu item */
608 if (TaskManagerSettings
.AlwaysOnTop
) {
609 CheckMenuItem(hEditMenu
, ID_OPTIONS_ALWAYSONTOP
, MF_BYCOMMAND
|MF_CHECKED
);
610 SetWindowPos(hWnd
, HWND_TOPMOST
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
612 CheckMenuItem(hEditMenu
, ID_OPTIONS_ALWAYSONTOP
, MF_BYCOMMAND
|MF_UNCHECKED
);
613 SetWindowPos(hWnd
, HWND_NOTOPMOST
, 0, 0, 0, 0, SWP_NOMOVE
|SWP_NOSIZE
);
616 /* Check or uncheck the minimize on use menu item */
617 if (TaskManagerSettings
.MinimizeOnUse
)
618 CheckMenuItem(hEditMenu
, ID_OPTIONS_MINIMIZEONUSE
, MF_BYCOMMAND
|MF_CHECKED
);
620 CheckMenuItem(hEditMenu
, ID_OPTIONS_MINIMIZEONUSE
, MF_BYCOMMAND
|MF_UNCHECKED
);
622 /* Check or uncheck the hide when minimized menu item */
623 if (TaskManagerSettings
.HideWhenMinimized
)
624 CheckMenuItem(hEditMenu
, ID_OPTIONS_HIDEWHENMINIMIZED
, MF_BYCOMMAND
|MF_CHECKED
);
626 CheckMenuItem(hEditMenu
, ID_OPTIONS_HIDEWHENMINIMIZED
, MF_BYCOMMAND
|MF_UNCHECKED
);
628 /* Check or uncheck the show 16-bit tasks menu item */
629 if (TaskManagerSettings
.Show16BitTasks
)
630 CheckMenuItem(hEditMenu
, ID_OPTIONS_SHOW16BITTASKS
, MF_BYCOMMAND
|MF_CHECKED
);
632 CheckMenuItem(hEditMenu
, ID_OPTIONS_SHOW16BITTASKS
, MF_BYCOMMAND
|MF_UNCHECKED
);
634 /* Set the view mode */
635 CheckMenuRadioItem(hViewMenu
, ID_VIEW_LARGE
, ID_VIEW_DETAILS
, TaskManagerSettings
.ViewMode
, MF_BYCOMMAND
);
637 if (TaskManagerSettings
.ShowKernelTimes
)
638 CheckMenuItem(hViewMenu
, ID_VIEW_SHOWKERNELTIMES
, MF_BYCOMMAND
|MF_CHECKED
);
640 CheckMenuItem(hViewMenu
, ID_VIEW_SHOWKERNELTIMES
, MF_BYCOMMAND
|MF_UNCHECKED
);
642 CheckMenuRadioItem(hUpdateSpeedMenu
, ID_VIEW_UPDATESPEED_HIGH
, ID_VIEW_UPDATESPEED_PAUSED
, TaskManagerSettings
.UpdateSpeed
, MF_BYCOMMAND
);
644 if (TaskManagerSettings
.CPUHistory_OneGraphPerCPU
)
645 CheckMenuRadioItem(hCPUHistoryMenu
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, MF_BYCOMMAND
);
647 CheckMenuRadioItem(hCPUHistoryMenu
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, MF_BYCOMMAND
);
649 nActivePage
= TaskManagerSettings
.ActiveTabPage
;
650 TabCtrl_SetCurFocus
/*Sel*/(hTabWnd
, 0);
651 TabCtrl_SetCurFocus
/*Sel*/(hTabWnd
, 1);
652 TabCtrl_SetCurFocus
/*Sel*/(hTabWnd
, 2);
653 TabCtrl_SetCurFocus
/*Sel*/(hTabWnd
, nActivePage
);
655 /* Setup update speed */
656 SetUpdateSpeed(hWnd
);
659 * Refresh the performance data
660 * Sample it twice so we can establish
661 * the delta values & cpu usage
666 RefreshApplicationPage();
667 RefreshProcessPage();
668 RefreshPerformancePage();
670 TrayIcon_ShellAddTrayIcon();
676 * This function handles all the moving events for the application
677 * It moves every child window that needs moving
679 void OnMove( WPARAM nType
, int cx
, int cy
)
681 #ifdef __GNUC__TEST__
682 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
);
683 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
);
684 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
);
689 * This function handles all the sizing events for the application
690 * It re-sizes every window, and child window that needs re-sizing
692 void OnSize( WPARAM nType
, int cx
, int cy
)
699 if (nType
== SIZE_MINIMIZED
)
701 if(TaskManagerSettings
.HideWhenMinimized
)
703 ShowWindow(hMainWnd
, SW_HIDE
);
708 nXDifference
= cx
- nOldWidth
;
709 nYDifference
= cy
- nOldHeight
;
713 /* Update the status bar size */
714 GetWindowRect(hStatusWnd
, &rc
);
715 SendMessageW(hStatusWnd
, WM_SIZE
, nType
, MAKELPARAM(cx
,rc
.bottom
- rc
.top
));
717 /* Update the status bar pane sizes */
718 nParts
[0] = bInMenuLoop
? -1 : STATUS_SIZE1
;
719 nParts
[1] = STATUS_SIZE2
;
721 SendMessageW(hStatusWnd
, SB_SETPARTS
, bInMenuLoop
? 1 : 3, (LPARAM
) (LPINT
) nParts
);
723 /* Resize the tab control */
724 GetWindowRect(hTabWnd
, &rc
);
725 cx
= (rc
.right
- rc
.left
) + nXDifference
;
726 cy
= (rc
.bottom
- rc
.top
) + nYDifference
;
727 SetWindowPos(hTabWnd
, NULL
, 0, 0, cx
, cy
, SWP_NOACTIVATE
|SWP_NOOWNERZORDER
|SWP_NOMOVE
|SWP_NOZORDER
);
729 /* Resize the application page */
730 GetWindowRect(hApplicationPage
, &rc
);
731 cx
= (rc
.right
- rc
.left
) + nXDifference
;
732 cy
= (rc
.bottom
- rc
.top
) + nYDifference
;
733 SetWindowPos(hApplicationPage
, NULL
, 0, 0, cx
, cy
, SWP_NOACTIVATE
|SWP_NOOWNERZORDER
|SWP_NOMOVE
|SWP_NOZORDER
);
735 /* Resize the process page */
736 GetWindowRect(hProcessPage
, &rc
);
737 cx
= (rc
.right
- rc
.left
) + nXDifference
;
738 cy
= (rc
.bottom
- rc
.top
) + nYDifference
;
739 SetWindowPos(hProcessPage
, NULL
, 0, 0, cx
, cy
, SWP_NOACTIVATE
|SWP_NOOWNERZORDER
|SWP_NOMOVE
|SWP_NOZORDER
);
741 /* Resize the performance page */
742 GetWindowRect(hPerformancePage
, &rc
);
743 cx
= (rc
.right
- rc
.left
) + nXDifference
;
744 cy
= (rc
.bottom
- rc
.top
) + nYDifference
;
745 SetWindowPos(hPerformancePage
, NULL
, 0, 0, cx
, cy
, SWP_NOACTIVATE
|SWP_NOOWNERZORDER
|SWP_NOMOVE
|SWP_NOZORDER
);
748 void LoadSettings(void)
751 WCHAR szSubKey
[] = L
"Software\\ReactOS\\TaskManager";
755 /* Window size & position settings */
756 TaskManagerSettings
.Maximized
= FALSE
;
757 TaskManagerSettings
.Left
= 0;
758 TaskManagerSettings
.Top
= 0;
759 TaskManagerSettings
.Right
= 0;
760 TaskManagerSettings
.Bottom
= 0;
763 TaskManagerSettings
.ActiveTabPage
= 0;
765 /* Options menu settings */
766 TaskManagerSettings
.AlwaysOnTop
= FALSE
;
767 TaskManagerSettings
.MinimizeOnUse
= TRUE
;
768 TaskManagerSettings
.HideWhenMinimized
= TRUE
;
769 TaskManagerSettings
.Show16BitTasks
= TRUE
;
771 /* Update speed settings */
772 TaskManagerSettings
.UpdateSpeed
= ID_VIEW_UPDATESPEED_NORMAL
;
774 /* Applications page settings */
775 TaskManagerSettings
.ViewMode
= ID_VIEW_DETAILS
;
777 /* Processes page settings */
778 TaskManagerSettings
.ShowProcessesFromAllUsers
= FALSE
; /* Server-only? */
780 for (i
= 0; i
< COLUMN_NMAX
; i
++) {
781 TaskManagerSettings
.Columns
[i
] = ColumnPresets
[i
].bDefaults
;
782 TaskManagerSettings
.ColumnOrderArray
[i
] = i
;
783 TaskManagerSettings
.ColumnSizeArray
[i
] = ColumnPresets
[i
].size
;
786 TaskManagerSettings
.SortColumn
= COLUMN_IMAGENAME
;
787 TaskManagerSettings
.SortAscending
= TRUE
;
789 /* Performance page settings */
790 TaskManagerSettings
.CPUHistory_OneGraphPerCPU
= TRUE
;
791 TaskManagerSettings
.ShowKernelTimes
= FALSE
;
794 if (RegOpenKeyExW(HKEY_CURRENT_USER
, szSubKey
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
796 /* Read the settings */
797 dwSize
= sizeof(TASKMANAGER_SETTINGS
);
798 RegQueryValueExW(hKey
, L
"Preferences", NULL
, NULL
, (LPBYTE
)&TaskManagerSettings
, &dwSize
);
801 * ATM, the 'ImageName' column is always visible
802 * (and grayed in configuration dialog)
803 * This will avoid troubles if the registry gets corrupted.
805 TaskManagerSettings
.Column_ImageName
= TRUE
;
811 void SaveSettings(void)
814 WCHAR szSubKey
[] = L
"Software\\ReactOS\\TaskManager";
816 /* Open (or create) the key */
817 if (RegCreateKeyExW(HKEY_CURRENT_USER
, szSubKey
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, NULL
) != ERROR_SUCCESS
)
819 /* Save the settings */
820 RegSetValueExW(hKey
, L
"Preferences", 0, REG_BINARY
, (LPBYTE
)&TaskManagerSettings
, sizeof(TASKMANAGER_SETTINGS
));
825 void TaskManager_OnRestoreMainWindow(void)
827 //HMENU hMenu, hOptionsMenu;
830 //hMenu = GetMenu(hMainWnd);
831 //hOptionsMenu = GetSubMenu(hMenu, OPTIONS_MENU_INDEX);
832 OnTop
= ((GetWindowLongPtrW(hMainWnd
, GWL_EXSTYLE
) & WS_EX_TOPMOST
) != 0);
835 SetForegroundWindow(hMainWnd
);
836 SetWindowPos(hMainWnd
, (OnTop
? HWND_TOPMOST
: HWND_TOP
), 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
| SWP_SHOWWINDOW
);
839 void TaskManager_OnEnterMenuLoop(HWND hWnd
)
843 /* Update the status bar pane sizes */
845 SendMessageW(hStatusWnd
, SB_SETPARTS
, 1, (LPARAM
) (LPINT
)&nParts
);
847 SendMessageW(hStatusWnd
, SB_SETTEXT
, (WPARAM
)0, (LPARAM
)L
"");
850 void TaskManager_OnExitMenuLoop(HWND hWnd
)
855 WCHAR szCpuUsage
[256], szProcesses
[256];
857 LoadStringW(hInst
, IDS_STATUS_CPUUSAGE
, szCpuUsage
, 256);
858 LoadStringW(hInst
, IDS_STATUS_PROCESSES
, szProcesses
, 256);
861 /* Update the status bar pane sizes */
862 GetClientRect(hWnd
, &rc
);
863 nParts
[0] = STATUS_SIZE1
;
864 nParts
[1] = STATUS_SIZE2
;
865 nParts
[2] = rc
.right
;
866 SendMessageW(hStatusWnd
, SB_SETPARTS
, 3, (LPARAM
) (LPINT
) nParts
);
867 SendMessageW(hStatusWnd
, SB_SETTEXT
, 0, (LPARAM
)L
"");
868 wsprintfW(text
, szCpuUsage
, PerfDataGetProcessorUsage());
869 SendMessageW(hStatusWnd
, SB_SETTEXT
, 1, (LPARAM
)text
);
870 wsprintfW(text
, szProcesses
, PerfDataGetProcessCount());
871 SendMessageW(hStatusWnd
, SB_SETTEXT
, 0, (LPARAM
)text
);
874 void TaskManager_OnMenuSelect(HWND hWnd
, UINT nItemID
, UINT nFlags
, HMENU hSysMenu
)
879 if (LoadStringW(hInst
, nItemID
, str
, 100)) {
880 /* load appropriate string */
882 /* first newline terminates actual string */
883 lpsz
= wcschr(lpsz
, '\n');
887 SendMessageW(hStatusWnd
, SB_SETTEXT
, 0, (LPARAM
)str
);
890 void TaskManager_OnViewUpdateSpeed(DWORD dwSpeed
)
894 HMENU hUpdateSpeedMenu
;
896 hMenu
= GetMenu(hMainWnd
);
897 hViewMenu
= GetSubMenu(hMenu
, 2);
898 hUpdateSpeedMenu
= GetSubMenu(hViewMenu
, 1);
900 TaskManagerSettings
.UpdateSpeed
= dwSpeed
;
901 CheckMenuRadioItem(hUpdateSpeedMenu
, ID_VIEW_UPDATESPEED_HIGH
, ID_VIEW_UPDATESPEED_PAUSED
, dwSpeed
, MF_BYCOMMAND
);
903 KillTimer(hMainWnd
, 1);
905 SetUpdateSpeed(hMainWnd
);
908 void TaskManager_OnTabWndSelChange(void)
918 hMenu
= GetMenu(hMainWnd
);
919 hViewMenu
= GetSubMenu(hMenu
, 2);
920 hOptionsMenu
= GetSubMenu(hMenu
, 1);
921 TaskManagerSettings
.ActiveTabPage
= TabCtrl_GetCurSel(hTabWnd
);
922 for (i
= GetMenuItemCount(hViewMenu
) - 1; i
> 2; i
--) {
923 hSubMenu
= GetSubMenu(hViewMenu
, i
);
925 DestroyMenu(hSubMenu
);
926 RemoveMenu(hViewMenu
, i
, MF_BYPOSITION
);
928 RemoveMenu(hOptionsMenu
, 3, MF_BYPOSITION
);
929 switch (TaskManagerSettings
.ActiveTabPage
) {
931 ShowWindow(hApplicationPage
, SW_SHOW
);
932 ShowWindow(hProcessPage
, SW_HIDE
);
933 ShowWindow(hPerformancePage
, SW_HIDE
);
934 BringWindowToTop(hApplicationPage
);
936 LoadStringW(hInst
, IDS_MENU_LARGEICONS
, szTemp
, 256);
937 AppendMenuW(hViewMenu
, MF_STRING
, ID_VIEW_LARGE
, szTemp
);
939 LoadStringW(hInst
, IDS_MENU_SMALLICONS
, szTemp
, 256);
940 AppendMenuW(hViewMenu
, MF_STRING
, ID_VIEW_SMALL
, szTemp
);
942 LoadStringW(hInst
, IDS_MENU_DETAILS
, szTemp
, 256);
943 AppendMenuW(hViewMenu
, MF_STRING
, ID_VIEW_DETAILS
, szTemp
);
945 if (GetMenuItemCount(hMenu
) <= 4) {
946 hSubMenu
= LoadMenuW(hInst
, MAKEINTRESOURCEW(IDR_WINDOWSMENU
));
948 LoadStringW(hInst
, IDS_MENU_WINDOWS
, szTemp
, 256);
949 InsertMenuW(hMenu
, 3, MF_BYPOSITION
|MF_POPUP
, (UINT_PTR
) hSubMenu
, szTemp
);
951 DrawMenuBar(hMainWnd
);
953 CheckMenuRadioItem(hViewMenu
, ID_VIEW_LARGE
, ID_VIEW_DETAILS
, TaskManagerSettings
.ViewMode
, MF_BYCOMMAND
);
956 * Give the application list control focus
958 SetFocus(hApplicationPageListCtrl
);
962 ShowWindow(hApplicationPage
, SW_HIDE
);
963 ShowWindow(hProcessPage
, SW_SHOW
);
964 ShowWindow(hPerformancePage
, SW_HIDE
);
965 BringWindowToTop(hProcessPage
);
967 LoadStringW(hInst
, IDS_MENU_SELECTCOLUMNS
, szTemp
, 256);
968 AppendMenuW(hViewMenu
, MF_STRING
, ID_VIEW_SELECTCOLUMNS
, szTemp
);
970 LoadStringW(hInst
, IDS_MENU_16BITTASK
, szTemp
, 256);
971 AppendMenuW(hOptionsMenu
, MF_STRING
, ID_OPTIONS_SHOW16BITTASKS
, szTemp
);
973 if (TaskManagerSettings
.Show16BitTasks
)
974 CheckMenuItem(hOptionsMenu
, ID_OPTIONS_SHOW16BITTASKS
, MF_BYCOMMAND
|MF_CHECKED
);
975 if (GetMenuItemCount(hMenu
) > 4)
977 DeleteMenu(hMenu
, 3, MF_BYPOSITION
);
978 DrawMenuBar(hMainWnd
);
981 * Give the process list control focus
983 SetFocus(hProcessPageListCtrl
);
987 ShowWindow(hApplicationPage
, SW_HIDE
);
988 ShowWindow(hProcessPage
, SW_HIDE
);
989 ShowWindow(hPerformancePage
, SW_SHOW
);
990 BringWindowToTop(hPerformancePage
);
991 if (GetMenuItemCount(hMenu
) > 4) {
992 DeleteMenu(hMenu
, 3, MF_BYPOSITION
);
993 DrawMenuBar(hMainWnd
);
996 GetSystemInfo(&sysInfo
);
998 /* Hide CPU graph options on single CPU systems */
999 if (sysInfo
.dwNumberOfProcessors
> 1)
1001 hSubMenu
= CreatePopupMenu();
1003 LoadStringW(hInst
, IDS_MENU_ONEGRAPHALLCPUS
, szTemp
, 256);
1004 AppendMenuW(hSubMenu
, MF_STRING
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, szTemp
);
1006 LoadStringW(hInst
, IDS_MENU_ONEGRAPHPERCPU
, szTemp
, 256);
1007 AppendMenuW(hSubMenu
, MF_STRING
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, szTemp
);
1009 LoadStringW(hInst
, IDS_MENU_CPUHISTORY
, szTemp
, 256);
1010 AppendMenuW(hViewMenu
, MF_STRING
|MF_POPUP
, (UINT_PTR
) hSubMenu
, szTemp
);
1012 if (TaskManagerSettings
.CPUHistory_OneGraphPerCPU
)
1013 CheckMenuRadioItem(hSubMenu
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, MF_BYCOMMAND
);
1015 CheckMenuRadioItem(hSubMenu
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU
, ID_VIEW_CPUHISTORY_ONEGRAPHALL
, MF_BYCOMMAND
);
1018 LoadStringW(hInst
, IDS_MENU_SHOWKERNELTIMES
, szTemp
, 256);
1019 AppendMenuW(hViewMenu
, MF_STRING
, ID_VIEW_SHOWKERNELTIMES
, szTemp
);
1021 if (TaskManagerSettings
.ShowKernelTimes
)
1022 CheckMenuItem(hViewMenu
, ID_VIEW_SHOWKERNELTIMES
, MF_BYCOMMAND
|MF_CHECKED
);
1024 CheckMenuItem(hViewMenu
, ID_VIEW_SHOWKERNELTIMES
, MF_BYCOMMAND
|MF_UNCHECKED
);
1027 * Give the tab control focus
1034 LPWSTR
GetLastErrorText(LPWSTR lpszBuf
, DWORD dwSize
)
1037 LPWSTR lpszTemp
= NULL
;
1039 dwRet
= FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1047 /* supplied buffer is not long enough */
1048 if (!dwRet
|| ( (long)dwSize
< (long)dwRet
+14)) {
1051 lpszTemp
[lstrlenW(lpszTemp
)-2] = L
'\0'; /*remove cr and newline character */
1052 wsprintfW(lpszBuf
, L
"%s (0x%x)", lpszTemp
, (int)GetLastError());
1055 LocalFree((HLOCAL
)lpszTemp
);
1060 DWORD
EndLocalThread(HANDLE
*hThread
, DWORD dwThread
)
1062 DWORD dwExitCodeThread
= 0;
1064 if (*hThread
!= NULL
) {
1065 PostThreadMessage(dwThread
,WM_QUIT
,0,0);
1069 if (WAIT_OBJECT_0
== WaitForSingleObject(*hThread
, 500))
1071 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
1072 TranslateMessage(&msg
);
1073 DispatchMessage(&msg
);
1076 GetExitCodeThread(*hThread
, &dwExitCodeThread
);
1077 CloseHandle(*hThread
);
1080 return dwExitCodeThread
;