wined3d: Allow to apply extension emulation wrappers independently.
[wine.git] / programs / taskmgr / taskmgr.c
blob80dc81ca6292246417dca26cda9c3a4a43e9cba7
1 /*
2 * ReactOS Task Manager
4 * taskmgr.c : Defines the entry point for the application.
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * Copyright (C) 2008 Vladimir Pankratov
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 St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <winnt.h>
31 #include "wine/unicode.h"
32 #include "resource.h"
33 #include "taskmgr.h"
34 #include "perfdata.h"
35 #include "column.h"
37 #define STATUS_WINDOW 2001
39 /* Global Variables: */
40 HINSTANCE hInst; /* current instance */
42 HWND hMainWnd; /* Main Window */
43 HWND hStatusWnd; /* Status Bar Window */
44 HWND hTabWnd; /* Tab Control Window */
46 int nMinimumWidth; /* Minimum width of the dialog (OnSize()'s cx) */
47 int nMinimumHeight; /* Minimum height of the dialog (OnSize()'s cy) */
49 int nOldWidth; /* Holds the previous client area width */
50 int nOldHeight; /* Holds the previous client area height */
52 BOOL bInMenuLoop = FALSE; /* Tells us if we are in the menu loop */
54 TASKMANAGER_SETTINGS TaskManagerSettings;
57 void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
59 SetBkColor(hDC, clr);
60 ExtTextOutW(hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
63 static void FillSolidRect2(HDC hDC, int x, int y, int cx, int cy, COLORREF clr)
65 RECT rect;
67 SetBkColor(hDC, clr);
68 rect.left = x;
69 rect.top = y;
70 rect.right = x + cx;
71 rect.bottom = y + cy;
72 ExtTextOutW(hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
75 static void Draw3dRect(HDC hDC, int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
77 FillSolidRect2(hDC, x, y, cx - 1, 1, clrTopLeft);
78 FillSolidRect2(hDC, x, y, 1, cy - 1, clrTopLeft);
79 FillSolidRect2(hDC, x + cx, y, -1, cy, clrBottomRight);
80 FillSolidRect2(hDC, x, y + cy, cx, -1, clrBottomRight);
83 void Font_DrawText(HDC hDC, LPWSTR lpwszText, int x, int y)
85 HDC hFontDC;
86 HBITMAP hFontBitmap;
87 HBITMAP hOldBitmap;
88 int i;
90 hFontDC = CreateCompatibleDC(hDC);
91 hFontBitmap = LoadBitmapW(hInst, MAKEINTRESOURCEW(IDB_FONT));
92 hOldBitmap = SelectObject(hFontDC, hFontBitmap);
94 for (i = 0; lpwszText[i]; i++) {
95 if ((lpwszText[i] >= '0') && (lpwszText[i] <= '9')) {
96 BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, (lpwszText[i] - '0') * 8, 0, SRCCOPY);
98 else if (lpwszText[i] == 'K')
100 BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 80, 0, SRCCOPY);
102 else if (lpwszText[i] == '%')
104 BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 88, 0, SRCCOPY);
107 SelectObject(hFontDC, hOldBitmap);
108 DeleteObject(hFontBitmap);
109 DeleteDC(hFontDC);
112 static BOOL OnCreate(HWND hWnd)
114 HMENU hMenu;
115 HMENU hEditMenu;
116 HMENU hViewMenu;
117 HMENU hUpdateSpeedMenu;
118 HMENU hCPUHistoryMenu;
119 int nActivePage;
120 int nParts[3];
121 RECT rc;
122 TCITEMW item;
124 static WCHAR wszApplications[255];
125 static WCHAR wszProcesses[255];
126 static WCHAR wszPerformance[255];
128 LoadStringW(hInst, IDS_APPLICATIONS, wszApplications, sizeof(wszApplications)/sizeof(WCHAR));
129 LoadStringW(hInst, IDS_PROCESSES, wszProcesses, sizeof(wszProcesses)/sizeof(WCHAR));
130 LoadStringW(hInst, IDS_PERFORMANCE, wszPerformance, sizeof(wszPerformance)/sizeof(WCHAR));
132 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIconW(hInst, MAKEINTRESOURCEW(IDI_TASKMANAGER)));
133 SendMessageW(hMainWnd, WM_SETICON, ICON_SMALL,
134 (LPARAM)LoadImageW(hInst, MAKEINTRESOURCEW(IDI_TASKMANAGER), IMAGE_ICON,
135 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
136 LR_SHARED));
138 /* Initialize the Windows Common Controls DLL */
139 InitCommonControls();
141 /* Get the minimum window sizes */
142 GetWindowRect(hWnd, &rc);
143 nMinimumWidth = (rc.right - rc.left);
144 nMinimumHeight = (rc.bottom - rc.top);
146 /* Create the status bar */
147 hStatusWnd = CreateStatusWindowW(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS, NULL, hWnd, STATUS_WINDOW);
148 if(!hStatusWnd)
149 return FALSE;
151 /* Create the status bar panes */
152 nParts[0] = 100;
153 nParts[1] = 210;
154 nParts[2] = 400;
155 SendMessageW(hStatusWnd, SB_SETPARTS, 3, (LPARAM)nParts);
157 /* Create tab pages */
158 hTabWnd = GetDlgItem(hWnd, IDC_TAB);
159 #if 1
160 hApplicationPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE), hWnd, ApplicationPageWndProc);
161 hProcessPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PROCESS_PAGE), hWnd, ProcessPageWndProc);
162 hPerformancePage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE), hWnd, PerformancePageWndProc);
163 #else
164 hApplicationPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_APPLICATION_PAGE), hTabWnd, ApplicationPageWndProc);
165 hProcessPage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PROCESS_PAGE), hTabWnd, ProcessPageWndProc);
166 hPerformancePage = CreateDialogW(hInst, MAKEINTRESOURCEW(IDD_PERFORMANCE_PAGE), hTabWnd, PerformancePageWndProc);
167 #endif
169 /* Insert tabs */
170 memset(&item, 0, sizeof(TCITEMW));
171 item.mask = TCIF_TEXT;
172 item.pszText = wszApplications;
173 SendMessageW(hTabWnd, TCM_INSERTITEMW, 0, (LPARAM)&item);
174 memset(&item, 0, sizeof(TCITEMW));
175 item.mask = TCIF_TEXT;
176 item.pszText = wszProcesses;
177 SendMessageW(hTabWnd, TCM_INSERTITEMW, 1, (LPARAM)&item);
178 memset(&item, 0, sizeof(TCITEMW));
179 item.mask = TCIF_TEXT;
180 item.pszText = wszPerformance;
181 SendMessageW(hTabWnd, TCM_INSERTITEMW, 2, (LPARAM)&item);
183 /* Size everything correctly */
184 GetClientRect(hWnd, &rc);
185 nOldWidth = rc.right;
186 nOldHeight = rc.bottom;
188 if ((TaskManagerSettings.Left != 0) ||
189 (TaskManagerSettings.Top != 0) ||
190 (TaskManagerSettings.Right != 0) ||
191 (TaskManagerSettings.Bottom != 0))
192 MoveWindow(hWnd, TaskManagerSettings.Left, TaskManagerSettings.Top, TaskManagerSettings.Right - TaskManagerSettings.Left, TaskManagerSettings.Bottom - TaskManagerSettings.Top, TRUE);
194 if (TaskManagerSettings.Maximized)
195 ShowWindow(hWnd, SW_MAXIMIZE);
197 /* Set the always on top style */
198 hMenu = GetMenu(hWnd);
199 hEditMenu = GetSubMenu(hMenu, 1);
200 hViewMenu = GetSubMenu(hMenu, 2);
201 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
202 hCPUHistoryMenu = GetSubMenu(hViewMenu, 7);
204 /* Check or uncheck the always on top menu item */
205 if (TaskManagerSettings.AlwaysOnTop) {
206 CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_CHECKED);
207 SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
208 } else {
209 CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_UNCHECKED);
210 SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
213 /* Check or uncheck the minimize on use menu item */
214 if (TaskManagerSettings.MinimizeOnUse)
215 CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_CHECKED);
216 else
217 CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_UNCHECKED);
219 /* Check or uncheck the hide when minimized menu item */
220 if (TaskManagerSettings.HideWhenMinimized)
221 CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_CHECKED);
222 else
223 CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_UNCHECKED);
225 /* Check or uncheck the show 16-bit tasks menu item */
226 if (TaskManagerSettings.Show16BitTasks)
227 CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
228 else
229 CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_UNCHECKED);
231 if (TaskManagerSettings.View_LargeIcons)
232 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
233 else if (TaskManagerSettings.View_SmallIcons)
234 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
235 else
236 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
238 if (TaskManagerSettings.ShowKernelTimes)
239 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
240 else
241 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
243 if (TaskManagerSettings.UpdateSpeed == 1)
244 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
245 else if (TaskManagerSettings.UpdateSpeed == 2)
246 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
247 else if (TaskManagerSettings.UpdateSpeed == 4)
248 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
249 else
250 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
252 if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
253 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
254 else
255 CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
257 nActivePage = TaskManagerSettings.ActiveTabPage;
258 SendMessageW(hTabWnd, TCM_SETCURFOCUS, 0, 0);
259 SendMessageW(hTabWnd, TCM_SETCURFOCUS, 1, 0);
260 SendMessageW(hTabWnd, TCM_SETCURFOCUS, 2, 0);
261 SendMessageW(hTabWnd, TCM_SETCURFOCUS, nActivePage, 0);
263 if (TaskManagerSettings.UpdateSpeed == 1)
264 SetTimer(hWnd, 1, 1000, NULL);
265 else if (TaskManagerSettings.UpdateSpeed == 2)
266 SetTimer(hWnd, 1, 2000, NULL);
267 else if (TaskManagerSettings.UpdateSpeed == 4)
268 SetTimer(hWnd, 1, 4000, NULL);
271 * Refresh the performance data
272 * Sample it twice so we can establish
273 * the delta values & cpu usage
275 PerfDataRefresh();
276 PerfDataRefresh();
278 RefreshApplicationPage();
279 RefreshProcessPage();
280 RefreshPerformancePage();
282 TrayIcon_ShellAddTrayIcon();
284 return TRUE;
287 /* OnSize()
288 * This function handles all the sizing events for the application
289 * It re-sizes every window, and child window that needs re-sizing
291 static void OnSize( UINT nType, int cx, int cy )
293 int nParts[3];
294 int nXDifference;
295 int nYDifference;
296 RECT rc;
298 if (nType == SIZE_MINIMIZED)
300 if(TaskManagerSettings.HideWhenMinimized)
302 ShowWindow(hMainWnd, SW_HIDE);
304 return;
307 nXDifference = cx - nOldWidth;
308 nYDifference = cy - nOldHeight;
309 nOldWidth = cx;
310 nOldHeight = cy;
312 /* Update the status bar size */
313 GetWindowRect(hStatusWnd, &rc);
314 SendMessageW(hStatusWnd, WM_SIZE, nType, MAKELPARAM(cx, cy + (rc.bottom - rc.top)));
316 /* Update the status bar pane sizes */
317 nParts[0] = bInMenuLoop ? -1 : 100;
318 nParts[1] = 210;
319 nParts[2] = cx;
320 SendMessageW(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (LPARAM)nParts);
322 /* Resize the tab control */
323 GetWindowRect(hTabWnd, &rc);
324 cx = (rc.right - rc.left) + nXDifference;
325 cy = (rc.bottom - rc.top) + nYDifference;
326 SetWindowPos(hTabWnd, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
328 /* Resize the application page */
329 GetWindowRect(hApplicationPage, &rc);
330 cx = (rc.right - rc.left) + nXDifference;
331 cy = (rc.bottom - rc.top) + nYDifference;
332 SetWindowPos(hApplicationPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
334 /* Resize the process page */
335 GetWindowRect(hProcessPage, &rc);
336 cx = (rc.right - rc.left) + nXDifference;
337 cy = (rc.bottom - rc.top) + nYDifference;
338 SetWindowPos(hProcessPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
340 /* Resize the performance page */
341 GetWindowRect(hPerformancePage, &rc);
342 cx = (rc.right - rc.left) + nXDifference;
343 cy = (rc.bottom - rc.top) + nYDifference;
344 SetWindowPos(hPerformancePage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
347 static void LoadSettings(void)
349 HKEY hKey;
350 int i;
351 DWORD dwSize;
353 static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
354 'W','i','n','e','\\','T','a','s','k','M','a','n','a','g','e','r',0};
355 static const WCHAR wszPreferences[] = {'P','r','e','f','e','r','e','n','c','e','s',0};
357 /* Window size & position settings */
358 TaskManagerSettings.Maximized = FALSE;
359 TaskManagerSettings.Left = 0;
360 TaskManagerSettings.Top = 0;
361 TaskManagerSettings.Right = 0;
362 TaskManagerSettings.Bottom = 0;
364 /* Tab settings */
365 TaskManagerSettings.ActiveTabPage = 0;
367 /* Options menu settings */
368 TaskManagerSettings.AlwaysOnTop = FALSE;
369 TaskManagerSettings.MinimizeOnUse = TRUE;
370 TaskManagerSettings.HideWhenMinimized = TRUE;
371 TaskManagerSettings.Show16BitTasks = TRUE;
373 /* Update speed settings */
374 TaskManagerSettings.UpdateSpeed = 2;
376 /* Applications page settings */
377 TaskManagerSettings.View_LargeIcons = FALSE;
378 TaskManagerSettings.View_SmallIcons = FALSE;
379 TaskManagerSettings.View_Details = TRUE;
381 /* Processes page settings */
382 TaskManagerSettings.ShowProcessesFromAllUsers = FALSE; /* Server-only? */
383 TaskManagerSettings.Column_ImageName = TRUE;
384 TaskManagerSettings.Column_PID = TRUE;
385 TaskManagerSettings.Column_CPUUsage = TRUE;
386 TaskManagerSettings.Column_CPUTime = TRUE;
387 TaskManagerSettings.Column_MemoryUsage = TRUE;
388 TaskManagerSettings.Column_MemoryUsageDelta = FALSE;
389 TaskManagerSettings.Column_PeakMemoryUsage = FALSE;
390 TaskManagerSettings.Column_PageFaults = FALSE;
391 TaskManagerSettings.Column_USERObjects = FALSE;
392 TaskManagerSettings.Column_IOReads = FALSE;
393 TaskManagerSettings.Column_IOReadBytes = FALSE;
394 TaskManagerSettings.Column_SessionID = FALSE; /* Server-only? */
395 TaskManagerSettings.Column_UserName = FALSE; /* Server-only? */
396 TaskManagerSettings.Column_PageFaultsDelta = FALSE;
397 TaskManagerSettings.Column_VirtualMemorySize = FALSE;
398 TaskManagerSettings.Column_PagedPool = FALSE;
399 TaskManagerSettings.Column_NonPagedPool = FALSE;
400 TaskManagerSettings.Column_BasePriority = FALSE;
401 TaskManagerSettings.Column_HandleCount = FALSE;
402 TaskManagerSettings.Column_ThreadCount = FALSE;
403 TaskManagerSettings.Column_GDIObjects = FALSE;
404 TaskManagerSettings.Column_IOWrites = FALSE;
405 TaskManagerSettings.Column_IOWriteBytes = FALSE;
406 TaskManagerSettings.Column_IOOther = FALSE;
407 TaskManagerSettings.Column_IOOtherBytes = FALSE;
409 for (i = 0; i < 25; i++) {
410 TaskManagerSettings.ColumnOrderArray[i] = i;
412 TaskManagerSettings.ColumnSizeArray[0] = 105;
413 TaskManagerSettings.ColumnSizeArray[1] = 50;
414 TaskManagerSettings.ColumnSizeArray[2] = 107;
415 TaskManagerSettings.ColumnSizeArray[3] = 70;
416 TaskManagerSettings.ColumnSizeArray[4] = 35;
417 TaskManagerSettings.ColumnSizeArray[5] = 70;
418 TaskManagerSettings.ColumnSizeArray[6] = 70;
419 TaskManagerSettings.ColumnSizeArray[7] = 100;
420 TaskManagerSettings.ColumnSizeArray[8] = 70;
421 TaskManagerSettings.ColumnSizeArray[9] = 70;
422 TaskManagerSettings.ColumnSizeArray[10] = 70;
423 TaskManagerSettings.ColumnSizeArray[11] = 70;
424 TaskManagerSettings.ColumnSizeArray[12] = 70;
425 TaskManagerSettings.ColumnSizeArray[13] = 70;
426 TaskManagerSettings.ColumnSizeArray[14] = 60;
427 TaskManagerSettings.ColumnSizeArray[15] = 60;
428 TaskManagerSettings.ColumnSizeArray[16] = 60;
429 TaskManagerSettings.ColumnSizeArray[17] = 60;
430 TaskManagerSettings.ColumnSizeArray[18] = 60;
431 TaskManagerSettings.ColumnSizeArray[19] = 70;
432 TaskManagerSettings.ColumnSizeArray[20] = 70;
433 TaskManagerSettings.ColumnSizeArray[21] = 70;
434 TaskManagerSettings.ColumnSizeArray[22] = 70;
435 TaskManagerSettings.ColumnSizeArray[23] = 70;
436 TaskManagerSettings.ColumnSizeArray[24] = 70;
438 TaskManagerSettings.SortColumn = 1;
439 TaskManagerSettings.SortAscending = TRUE;
441 /* Performance page settings */
442 TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
443 TaskManagerSettings.ShowKernelTimes = FALSE;
445 /* Open the key */
446 /* @@ Wine registry key: HKCU\Software\Wine\TaskManager */
447 if (RegOpenKeyExW(HKEY_CURRENT_USER, wszSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
448 return;
449 /* Read the settings */
450 dwSize = sizeof(TASKMANAGER_SETTINGS);
451 RegQueryValueExW(hKey, wszPreferences, NULL, NULL, (LPBYTE)&TaskManagerSettings, &dwSize);
453 /* Close the key */
454 RegCloseKey(hKey);
457 static void SaveSettings(void)
459 HKEY hKey;
461 static const WCHAR wszSubKey3[] = {'S','o','f','t','w','a','r','e','\\',
462 'W','i','n','e','\\','T','a','s','k','M','a','n','a','g','e','r',0};
463 static const WCHAR wszPreferences[] = {'P','r','e','f','e','r','e','n','c','e','s',0};
465 /* Open (or create) the key */
467 /* @@ Wine registry key: HKCU\Software\Wine\TaskManager */
468 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszSubKey3, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
469 return;
470 /* Save the settings */
471 RegSetValueExW(hKey, wszPreferences, 0, REG_BINARY, (LPBYTE)&TaskManagerSettings, sizeof(TASKMANAGER_SETTINGS));
472 /* Close the key */
473 RegCloseKey(hKey);
476 static void TaskManager_OnRestoreMainWindow(void)
478 BOOL OnTop;
480 OnTop = (GetWindowLongW(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
482 OpenIcon(hMainWnd);
483 SetForegroundWindow(hMainWnd);
484 SetWindowPos(hMainWnd, (OnTop ? HWND_TOPMOST : HWND_TOP), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
487 static void TaskManager_OnEnterMenuLoop(HWND hWnd)
489 int nParts;
491 /* Update the status bar pane sizes */
492 nParts = -1;
493 SendMessageW(hStatusWnd, SB_SETPARTS, 1, (LPARAM)&nParts);
494 bInMenuLoop = TRUE;
495 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, 0);
498 static void TaskManager_OnExitMenuLoop(HWND hWnd)
500 RECT rc;
501 int nParts[3];
502 WCHAR text[256];
504 WCHAR wszCPU_Usage[255];
505 WCHAR wszProcesses[255];
507 LoadStringW(hInst, IDS_STATUS_BAR_CPU_USAGE, wszCPU_Usage, sizeof(wszCPU_Usage)/sizeof(WCHAR));
508 LoadStringW(hInst, IDS_STATUS_BAR_PROCESSES, wszProcesses, sizeof(wszProcesses)/sizeof(WCHAR));
510 bInMenuLoop = FALSE;
511 /* Update the status bar pane sizes */
512 GetClientRect(hWnd, &rc);
513 nParts[0] = 100;
514 nParts[1] = 210;
515 nParts[2] = rc.right;
516 SendMessageW(hStatusWnd, SB_SETPARTS, 3, (LPARAM)nParts);
517 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, 0);
518 wsprintfW(text, wszCPU_Usage, PerfDataGetProcessorUsage());
519 SendMessageW(hStatusWnd, SB_SETTEXTW, 1, (LPARAM)text);
520 wsprintfW(text, wszProcesses, PerfDataGetProcessCount());
521 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)text);
524 static void TaskManager_OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
526 WCHAR wstr[256] = {0};
528 LoadStringW(hInst, nItemID, wstr, sizeof(wstr)/sizeof(WCHAR));
529 SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)wstr);
532 static void TaskManager_OnViewUpdateSpeedHigh(void)
534 HMENU hMenu;
535 HMENU hViewMenu;
536 HMENU hUpdateSpeedMenu;
538 hMenu = GetMenu(hMainWnd);
539 hViewMenu = GetSubMenu(hMenu, 2);
540 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
542 TaskManagerSettings.UpdateSpeed = 1;
543 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
545 KillTimer(hMainWnd, 1);
546 SetTimer(hMainWnd, 1, 1000, NULL);
549 static void TaskManager_OnViewUpdateSpeedNormal(void)
551 HMENU hMenu;
552 HMENU hViewMenu;
553 HMENU hUpdateSpeedMenu;
555 hMenu = GetMenu(hMainWnd);
556 hViewMenu = GetSubMenu(hMenu, 2);
557 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
559 TaskManagerSettings.UpdateSpeed = 2;
560 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
562 KillTimer(hMainWnd, 1);
563 SetTimer(hMainWnd, 1, 2000, NULL);
566 static void TaskManager_OnViewUpdateSpeedLow(void)
568 HMENU hMenu;
569 HMENU hViewMenu;
570 HMENU hUpdateSpeedMenu;
572 hMenu = GetMenu(hMainWnd);
573 hViewMenu = GetSubMenu(hMenu, 2);
574 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
576 TaskManagerSettings.UpdateSpeed = 4;
577 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
579 KillTimer(hMainWnd, 1);
580 SetTimer(hMainWnd, 1, 4000, NULL);
583 static void TaskManager_OnViewUpdateSpeedPaused(void)
585 HMENU hMenu;
586 HMENU hViewMenu;
587 HMENU hUpdateSpeedMenu;
589 hMenu = GetMenu(hMainWnd);
590 hViewMenu = GetSubMenu(hMenu, 2);
591 hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
592 TaskManagerSettings.UpdateSpeed = 0;
593 CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
594 KillTimer(hMainWnd, 1);
597 static void TaskManager_OnTabWndSelChange(void)
599 int i;
600 HMENU hMenu;
601 HMENU hOptionsMenu;
602 HMENU hViewMenu;
603 HMENU hSubMenu;
605 WCHAR wszLargeIcons[255];
606 WCHAR wszSmallIcons[255];
607 WCHAR wszDetails[255];
608 WCHAR wszWindows[255];
609 WCHAR wszSelectColumns[255];
610 WCHAR wszShow16bTasks[255];
611 WCHAR wszOneGraphAllCPU[255];
612 WCHAR wszOneGraphPerCPU[255];
613 WCHAR wszCPUHistory[255];
614 WCHAR wszShowKernelTimes[255];
616 LoadStringW(hInst, IDS_VIEW_LARGE, wszLargeIcons, sizeof(wszLargeIcons)/sizeof(WCHAR));
617 LoadStringW(hInst, IDS_VIEW_SMALL, wszSmallIcons, sizeof(wszSmallIcons)/sizeof(WCHAR));
618 LoadStringW(hInst, IDS_VIEW_DETAILS, wszDetails, sizeof(wszDetails)/sizeof(WCHAR));
619 LoadStringW(hInst, IDS_WINDOWS, wszWindows, sizeof(wszWindows)/sizeof(WCHAR));
620 LoadStringW(hInst, IDS_VIEW_SELECTCOLUMNS, wszSelectColumns, sizeof(wszSelectColumns)/sizeof(WCHAR));
621 LoadStringW(hInst, IDS_OPTIONS_SHOW16BITTASKS, wszShow16bTasks, sizeof(wszShow16bTasks)/sizeof(WCHAR));
622 LoadStringW(hInst, IDS_VIEW_CPUHISTORY_ONEGRAPHALL, wszOneGraphAllCPU, sizeof(wszOneGraphAllCPU)/sizeof(WCHAR));
623 LoadStringW(hInst, IDS_VIEW_CPUHISTORY_ONEGRAPHPERCPU, wszOneGraphPerCPU, sizeof(wszOneGraphPerCPU)/sizeof(WCHAR));
624 LoadStringW(hInst, IDS_VIEW_CPUHISTORY, wszCPUHistory, sizeof(wszCPUHistory)/sizeof(WCHAR));
625 LoadStringW(hInst, IDS_VIEW_SHOWKERNELTIMES, wszShowKernelTimes, sizeof(wszShowKernelTimes)/sizeof(WCHAR));
627 hMenu = GetMenu(hMainWnd);
628 hViewMenu = GetSubMenu(hMenu, 2);
629 hOptionsMenu = GetSubMenu(hMenu, 1);
630 TaskManagerSettings.ActiveTabPage = SendMessageW(hTabWnd, TCM_GETCURSEL, 0, 0);
631 for (i = GetMenuItemCount(hViewMenu) - 1; i > 2; i--) {
632 hSubMenu = GetSubMenu(hViewMenu, i);
633 if (hSubMenu)
634 DestroyMenu(hSubMenu);
635 RemoveMenu(hViewMenu, i, MF_BYPOSITION);
637 RemoveMenu(hOptionsMenu, 3, MF_BYPOSITION);
638 switch (TaskManagerSettings.ActiveTabPage) {
639 case 0:
640 ShowWindow(hApplicationPage, SW_SHOW);
641 ShowWindow(hProcessPage, SW_HIDE);
642 ShowWindow(hPerformancePage, SW_HIDE);
643 BringWindowToTop(hApplicationPage);
644 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_LARGE, wszLargeIcons);
645 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SMALL, wszSmallIcons);
646 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_DETAILS, wszDetails);
648 if (GetMenuItemCount(hMenu) <= 4) {
649 hSubMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_WINDOWSMENU));
650 InsertMenuW(hMenu, 3, MF_BYPOSITION|MF_POPUP, (UINT_PTR)hSubMenu, wszWindows);
651 DrawMenuBar(hMainWnd);
653 if (TaskManagerSettings.View_LargeIcons)
654 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
655 else if (TaskManagerSettings.View_SmallIcons)
656 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
657 else
658 CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
660 * Give the application list control focus
662 SetFocus(hApplicationPageListCtrl);
663 break;
665 case 1:
666 ShowWindow(hApplicationPage, SW_HIDE);
667 ShowWindow(hProcessPage, SW_SHOW);
668 ShowWindow(hPerformancePage, SW_HIDE);
669 BringWindowToTop(hProcessPage);
670 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SELECTCOLUMNS, wszSelectColumns);
671 AppendMenuW(hOptionsMenu, MF_STRING, ID_OPTIONS_SHOW16BITTASKS, wszShow16bTasks);
672 if (TaskManagerSettings.Show16BitTasks)
673 CheckMenuItem(hOptionsMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
674 if (GetMenuItemCount(hMenu) > 4)
676 RemoveMenu(hMenu, 3, MF_BYPOSITION);
677 DrawMenuBar(hMainWnd);
680 * Give the process list control focus
682 SetFocus(hProcessPageListCtrl);
683 break;
685 case 2:
686 ShowWindow(hApplicationPage, SW_HIDE);
687 ShowWindow(hProcessPage, SW_HIDE);
688 ShowWindow(hPerformancePage, SW_SHOW);
689 BringWindowToTop(hPerformancePage);
690 if (GetMenuItemCount(hMenu) > 4) {
691 RemoveMenu(hMenu, 3, MF_BYPOSITION);
692 DrawMenuBar(hMainWnd);
694 hSubMenu = CreatePopupMenu();
695 AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHALL, wszOneGraphAllCPU);
696 AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, wszOneGraphPerCPU);
697 AppendMenuW(hViewMenu, MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, wszCPUHistory);
698 AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SHOWKERNELTIMES, wszShowKernelTimes);
699 if (TaskManagerSettings.ShowKernelTimes)
700 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
701 else
702 CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
703 if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
704 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
705 else
706 CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
708 * Give the tab control focus
710 SetFocus(hTabWnd);
711 break;
715 LPWSTR GetLastErrorText(LPWSTR lpwszBuf, DWORD dwSize)
717 DWORD dwRet;
718 LPWSTR lpwszTemp = NULL;
719 static const WCHAR wszFormat[] = {'%','s',' ','(','%','u',')',0};
721 dwRet = FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
722 NULL,
723 GetLastError(),
724 LANG_NEUTRAL,
725 (LPWSTR)&lpwszTemp,
727 NULL );
729 /* supplied buffer is not long enough */
730 if (!dwRet || ( dwSize < dwRet+14)) {
731 lpwszBuf[0] = '\0';
732 } else {
733 lpwszTemp[strlenW(lpwszTemp)-2] = '\0'; /* remove cr and newline character */
734 sprintfW(lpwszBuf, wszFormat, lpwszTemp, GetLastError());
736 if (lpwszTemp) {
737 LocalFree(lpwszTemp);
739 return lpwszBuf;
742 /* Message handler for dialog box. */
743 static INT_PTR CALLBACK
744 TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
746 static const WCHAR wszTaskmgr[] = {'t','a','s','k','m','g','r',0};
747 HDC hdc;
748 PAINTSTRUCT ps;
749 LPRECT pRC;
750 RECT rc;
751 LPNMHDR pnmh;
752 WINDOWPLACEMENT wp;
754 switch (message) {
755 case WM_INITDIALOG:
756 hMainWnd = hDlg;
757 return OnCreate(hDlg);
759 case WM_COMMAND:
760 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
761 EndDialog(hDlg, LOWORD(wParam));
762 return TRUE;
764 /* Process menu commands */
765 switch (LOWORD(wParam))
767 case ID_FILE_NEW:
768 TaskManager_OnFileNew();
769 break;
770 case ID_OPTIONS_ALWAYSONTOP:
771 TaskManager_OnOptionsAlwaysOnTop();
772 break;
773 case ID_OPTIONS_MINIMIZEONUSE:
774 TaskManager_OnOptionsMinimizeOnUse();
775 break;
776 case ID_OPTIONS_HIDEWHENMINIMIZED:
777 TaskManager_OnOptionsHideWhenMinimized();
778 break;
779 case ID_OPTIONS_SHOW16BITTASKS:
780 TaskManager_OnOptionsShow16BitTasks();
781 break;
782 case ID_RESTORE:
783 TaskManager_OnRestoreMainWindow();
784 break;
785 case ID_VIEW_LARGE:
786 ApplicationPage_OnViewLargeIcons();
787 break;
788 case ID_VIEW_SMALL:
789 ApplicationPage_OnViewSmallIcons();
790 break;
791 case ID_VIEW_DETAILS:
792 ApplicationPage_OnViewDetails();
793 break;
794 case ID_VIEW_SHOWKERNELTIMES:
795 PerformancePage_OnViewShowKernelTimes();
796 break;
797 case ID_VIEW_CPUHISTORY_ONEGRAPHALL:
798 PerformancePage_OnViewCPUHistoryOneGraphAll();
799 break;
800 case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU:
801 PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
802 break;
803 case ID_VIEW_UPDATESPEED_HIGH:
804 TaskManager_OnViewUpdateSpeedHigh();
805 break;
806 case ID_VIEW_UPDATESPEED_NORMAL:
807 TaskManager_OnViewUpdateSpeedNormal();
808 break;
809 case ID_VIEW_UPDATESPEED_LOW:
810 TaskManager_OnViewUpdateSpeedLow();
811 break;
812 case ID_VIEW_UPDATESPEED_PAUSED:
813 TaskManager_OnViewUpdateSpeedPaused();
814 break;
815 case ID_VIEW_SELECTCOLUMNS:
816 ProcessPage_OnViewSelectColumns();
817 break;
818 case ID_VIEW_REFRESH:
819 PostMessageW(hDlg, WM_TIMER, 0, 0);
820 break;
821 case ID_WINDOWS_TILEHORIZONTALLY:
822 ApplicationPage_OnWindowsTileHorizontally();
823 break;
824 case ID_WINDOWS_TILEVERTICALLY:
825 ApplicationPage_OnWindowsTileVertically();
826 break;
827 case ID_WINDOWS_MINIMIZE:
828 ApplicationPage_OnWindowsMinimize();
829 break;
830 case ID_WINDOWS_MAXIMIZE:
831 ApplicationPage_OnWindowsMaximize();
832 break;
833 case ID_WINDOWS_CASCADE:
834 ApplicationPage_OnWindowsCascade();
835 break;
836 case ID_WINDOWS_BRINGTOFRONT:
837 ApplicationPage_OnWindowsBringToFront();
838 break;
839 case ID_APPLICATION_PAGE_SWITCHTO:
840 ApplicationPage_OnSwitchTo();
841 break;
842 case ID_APPLICATION_PAGE_ENDTASK:
843 ApplicationPage_OnEndTask();
844 break;
845 case ID_APPLICATION_PAGE_GOTOPROCESS:
846 ApplicationPage_OnGotoProcess();
847 break;
848 case ID_PROCESS_PAGE_ENDPROCESS:
849 ProcessPage_OnEndProcess();
850 break;
851 case ID_PROCESS_PAGE_ENDPROCESSTREE:
852 ProcessPage_OnEndProcessTree();
853 break;
854 case ID_PROCESS_PAGE_DEBUG:
855 ProcessPage_OnDebug();
856 break;
857 case ID_PROCESS_PAGE_SETAFFINITY:
858 ProcessPage_OnSetAffinity();
859 break;
860 case ID_PROCESS_PAGE_SETPRIORITY_REALTIME:
861 ProcessPage_OnSetPriorityRealTime();
862 break;
863 case ID_PROCESS_PAGE_SETPRIORITY_HIGH:
864 ProcessPage_OnSetPriorityHigh();
865 break;
866 case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL:
867 ProcessPage_OnSetPriorityAboveNormal();
868 break;
869 case ID_PROCESS_PAGE_SETPRIORITY_NORMAL:
870 ProcessPage_OnSetPriorityNormal();
871 break;
872 case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL:
873 ProcessPage_OnSetPriorityBelowNormal();
874 break;
875 case ID_PROCESS_PAGE_SETPRIORITY_LOW:
876 ProcessPage_OnSetPriorityLow();
877 break;
878 case ID_PROCESS_PAGE_DEBUGCHANNELS:
879 ProcessPage_OnDebugChannels();
880 break;
881 case ID_HELP_TOPICS:
882 WinHelpW(hDlg, wszTaskmgr, HELP_FINDER, 0);
883 break;
884 case ID_HELP_ABOUT:
885 OnAbout();
886 break;
887 case ID_FILE_EXIT:
888 EndDialog(hDlg, IDOK);
889 break;
891 break;
893 case WM_ONTRAYICON:
894 switch(lParam)
896 case WM_RBUTTONDOWN:
898 POINT pt;
899 BOOL OnTop;
900 HMENU hMenu, hPopupMenu;
902 GetCursorPos(&pt);
904 OnTop = (GetWindowLongW(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
906 hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_TRAY_POPUP));
907 hPopupMenu = GetSubMenu(hMenu, 0);
909 if(IsWindowVisible(hMainWnd))
911 DeleteMenu(hPopupMenu, ID_RESTORE, MF_BYCOMMAND);
913 else
915 SetMenuDefaultItem(hPopupMenu, ID_RESTORE, FALSE);
918 if(OnTop)
920 CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_CHECKED);
923 SetForegroundWindow(hMainWnd);
924 TrackPopupMenuEx(hPopupMenu, 0, pt.x, pt.y, hMainWnd, NULL);
926 DestroyMenu(hMenu);
927 break;
929 case WM_LBUTTONDBLCLK:
930 TaskManager_OnRestoreMainWindow();
931 break;
933 break;
935 case WM_NOTIFY:
936 pnmh = (LPNMHDR)lParam;
937 if ((pnmh->hwndFrom == hTabWnd) &&
938 (pnmh->idFrom == IDC_TAB) &&
939 (pnmh->code == TCN_SELCHANGE))
941 TaskManager_OnTabWndSelChange();
943 break;
945 case WM_NCPAINT:
946 hdc = GetDC(hDlg);
947 GetClientRect(hDlg, &rc);
948 Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
949 ReleaseDC(hDlg, hdc);
950 break;
952 case WM_PAINT:
953 hdc = BeginPaint(hDlg, &ps);
954 GetClientRect(hDlg, &rc);
955 Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
956 EndPaint(hDlg, &ps);
957 break;
959 case WM_SIZING:
960 /* Make sure the user is sizing the dialog */
961 /* in an acceptable range */
962 pRC = (LPRECT)lParam;
963 if ((wParam == WMSZ_LEFT) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_BOTTOMLEFT)) {
964 /* If the width is too small enlarge it to the minimum */
965 if (nMinimumWidth > (pRC->right - pRC->left))
966 pRC->left = pRC->right - nMinimumWidth;
967 } else {
968 /* If the width is too small enlarge it to the minimum */
969 if (nMinimumWidth > (pRC->right - pRC->left))
970 pRC->right = pRC->left + nMinimumWidth;
972 if ((wParam == WMSZ_TOP) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_TOPRIGHT)) {
973 /* If the height is too small enlarge it to the minimum */
974 if (nMinimumHeight > (pRC->bottom - pRC->top))
975 pRC->top = pRC->bottom - nMinimumHeight;
976 } else {
977 /* If the height is too small enlarge it to the minimum */
978 if (nMinimumHeight > (pRC->bottom - pRC->top))
979 pRC->bottom = pRC->top + nMinimumHeight;
981 return TRUE;
983 case WM_SIZE:
984 /* Handle the window sizing in its own function */
985 OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
986 break;
988 case WM_DESTROY:
989 ShowWindow(hDlg, SW_HIDE);
990 TrayIcon_ShellRemoveTrayIcon();
991 wp.length = sizeof(WINDOWPLACEMENT);
992 GetWindowPlacement(hDlg, &wp);
993 TaskManagerSettings.Left = wp.rcNormalPosition.left;
994 TaskManagerSettings.Top = wp.rcNormalPosition.top;
995 TaskManagerSettings.Right = wp.rcNormalPosition.right;
996 TaskManagerSettings.Bottom = wp.rcNormalPosition.bottom;
997 if (IsZoomed(hDlg) || (wp.flags & WPF_RESTORETOMAXIMIZED))
998 TaskManagerSettings.Maximized = TRUE;
999 else
1000 TaskManagerSettings.Maximized = FALSE;
1001 return DefWindowProcW(hDlg, message, wParam, lParam);
1003 case WM_TIMER:
1004 /* Refresh the performance data */
1005 PerfDataRefresh();
1006 RefreshApplicationPage();
1007 RefreshProcessPage();
1008 RefreshPerformancePage();
1009 TrayIcon_ShellUpdateTrayIcon();
1010 break;
1012 case WM_ENTERMENULOOP:
1013 TaskManager_OnEnterMenuLoop(hDlg);
1014 break;
1015 case WM_EXITMENULOOP:
1016 TaskManager_OnExitMenuLoop(hDlg);
1017 break;
1018 case WM_MENUSELECT:
1019 TaskManager_OnMenuSelect(hDlg, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
1020 break;
1023 return 0;
1026 int APIENTRY WinMain(HINSTANCE hInstance,
1027 HINSTANCE hPrevInstance,
1028 LPSTR lpCmdLine,
1029 int nCmdShow)
1031 HANDLE hProcess;
1032 HANDLE hToken;
1033 TOKEN_PRIVILEGES tkp;
1035 /* Initialize global variables */
1036 hInst = hInstance;
1038 /* Change our priority class to HIGH */
1039 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1040 SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
1041 CloseHandle(hProcess);
1043 /* Now let's get the SE_DEBUG_NAME privilege
1044 * so that we can debug processes
1047 /* Get a token for this process. */
1048 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1049 static const WCHAR SeDebugPrivilegeW[] = {'S','e','D','e','b','u','g','P','r','i','v','i','l','e','g','e',0};
1051 /* Get the LUID for the debug privilege. */
1052 LookupPrivilegeValueW(NULL, SeDebugPrivilegeW, &tkp.Privileges[0].Luid);
1054 tkp.PrivilegeCount = 1; /* one privilege to set */
1055 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1057 /* Get the debug privilege for this process. */
1058 AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);
1061 /* Load our settings from the registry */
1062 LoadSettings();
1064 /* Initialize perf data */
1065 if (!PerfDataInitialize()) {
1066 return -1;
1069 DialogBoxW(hInst, (LPWSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc);
1071 /* Save our settings to the registry */
1072 SaveSettings();
1073 return 0;