taskmgr: Converted some functions and variables to Unicode.
[wine] / programs / 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  *  Copyright (C) 2008  Vladimir Pankratov
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 St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <stdlib.h>
28 #include <malloc.h>
29 #include <memory.h>
30 #include <stdio.h>
31 #include <winnt.h>
32
33 #include "wine/unicode.h"
34 #include "resource.h"
35 #include "taskmgr.h"
36 #include "perfdata.h"
37 #include "column.h"
38
39 #define STATUS_WINDOW   2001
40
41 /* Global Variables: */
42 HINSTANCE hInst;                 /* current instance */
43
44 HWND hMainWnd;                   /* Main Window */
45 HWND hStatusWnd;                 /* Status Bar Window */
46 HWND hTabWnd;                    /* Tab Control Window */
47
48 int  nMinimumWidth;              /* Minimum width of the dialog (OnSize()'s cx) */
49 int  nMinimumHeight;             /* Minimum height of the dialog (OnSize()'s cy) */
50
51 int  nOldWidth;                  /* Holds the previous client area width */
52 int  nOldHeight;                 /* Holds the previous client area height */
53
54 BOOL bInMenuLoop = FALSE;        /* Tells us if we are in the menu loop */
55
56 TASKMANAGER_SETTINGS TaskManagerSettings;
57
58
59 void FillSolidRect(HDC hDC, LPCRECT lpRect, COLORREF clr)
60 {
61     SetBkColor(hDC, clr);
62     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, lpRect, NULL, 0, NULL);
63 }
64
65 void FillSolidRect2(HDC hDC, int x, int y, int cx, int cy, COLORREF clr)
66 {
67     RECT rect;
68
69     SetBkColor(hDC, clr);
70     rect.left = x;
71     rect.top = y;
72     rect.right = x + cx;
73     rect.bottom = y + cy;
74     ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
75 }
76
77 void Draw3dRect(HDC hDC, int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
78 {
79     FillSolidRect2(hDC, x, y, cx - 1, 1, clrTopLeft);
80     FillSolidRect2(hDC, x, y, 1, cy - 1, clrTopLeft);
81     FillSolidRect2(hDC, x + cx, y, -1, cy, clrBottomRight);
82     FillSolidRect2(hDC, x, y + cy, cx, -1, clrBottomRight);
83 }
84
85 void Draw3dRect2(HDC hDC, LPRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight)
86 {
87     Draw3dRect(hDC, lpRect->left, lpRect->top, lpRect->right - lpRect->left,
88         lpRect->bottom - lpRect->top, clrTopLeft, clrBottomRight);
89 }
90
91 void Font_DrawText(HDC hDC, LPWSTR lpwszText, int x, int y)
92 {
93     HDC        hFontDC;
94     HBITMAP    hFontBitmap;
95     HBITMAP    hOldBitmap;
96     int        i;
97
98     hFontDC = CreateCompatibleDC(hDC);
99     hFontBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_FONT));
100     hOldBitmap = (HBITMAP)SelectObject(hFontDC, hFontBitmap);
101
102     for (i = 0; lpwszText[i]; i++) {
103         if ((lpwszText[i] >= '0') && (lpwszText[i] <= '9')) {
104             BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, (lpwszText[i] - '0') * 8, 0, SRCCOPY);
105         }
106         else if (lpwszText[i] == 'K')
107         {
108             BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 80, 0, SRCCOPY);
109         }
110         else if (lpwszText[i] == '%')
111         {
112             BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 88, 0, SRCCOPY);
113         }
114     }
115     SelectObject(hFontDC, hOldBitmap);
116     DeleteObject(hFontBitmap);
117     DeleteDC(hFontDC);
118 }
119
120 static BOOL OnCreate(HWND hWnd)
121 {
122     HMENU   hMenu;
123     HMENU   hEditMenu;
124     HMENU   hViewMenu;
125     HMENU   hUpdateSpeedMenu;
126     HMENU   hCPUHistoryMenu;
127     int     nActivePage;
128     int     nParts[3];
129     RECT    rc;
130     TCITEMW item;
131
132     static WCHAR wszApplications[] = {'A','p','p','l','i','c','a','t','i','o','n','s',0};
133     static WCHAR wszProcesses[] = {'P','r','o','c','e','s','s','e','s',0};
134     static WCHAR wszPerformance[] = {'P','e','r','f','o','r','m','a','n','c','e',0};
135
136     SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(IDI_TASKMANAGER)));
137
138     /* Initialize the Windows Common Controls DLL */
139     InitCommonControls();
140
141     /* Get the minimum window sizes */
142     GetWindowRect(hWnd, &rc);
143     nMinimumWidth = (rc.right - rc.left);
144     nMinimumHeight = (rc.bottom - rc.top);
145
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;
150
151     /* Create the status bar panes */
152     nParts[0] = 100;
153     nParts[1] = 210;
154     nParts[2] = 400;
155     SendMessageW(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
156
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
168
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);
182
183     /* Size everything correctly */
184     GetClientRect(hWnd, &rc);
185     nOldWidth = rc.right;
186     nOldHeight = rc.bottom;
187     /* nOldStartX = rc.left; */
188     /*nOldStartY = rc.top;  */
189
190 #define PAGE_OFFSET_LEFT    17
191 #define PAGE_OFFSET_TOP     72
192 #define PAGE_OFFSET_WIDTH   (PAGE_OFFSET_LEFT*2)
193 #define PAGE_OFFSET_HEIGHT  (PAGE_OFFSET_TOP+32)
194
195     if ((TaskManagerSettings.Left != 0) ||
196         (TaskManagerSettings.Top != 0) ||
197         (TaskManagerSettings.Right != 0) ||
198         (TaskManagerSettings.Bottom != 0))
199     {
200         MoveWindow(hWnd, TaskManagerSettings.Left, TaskManagerSettings.Top, TaskManagerSettings.Right - TaskManagerSettings.Left, TaskManagerSettings.Bottom - TaskManagerSettings.Top, TRUE);
201 #ifdef __GNUC__TEST__
202         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);
203         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);
204         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);
205 #endif
206     }
207     if (TaskManagerSettings.Maximized)
208         ShowWindow(hWnd, SW_MAXIMIZE);
209
210     /* Set the always on top style */
211     hMenu = GetMenu(hWnd);
212     hEditMenu = GetSubMenu(hMenu, 1);
213     hViewMenu = GetSubMenu(hMenu, 2);
214     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
215     hCPUHistoryMenu = GetSubMenu(hViewMenu, 7);
216
217     /* Check or uncheck the always on top menu item */
218     if (TaskManagerSettings.AlwaysOnTop) {
219         CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_CHECKED);
220         SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
221     } else {
222         CheckMenuItem(hEditMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND|MF_UNCHECKED);
223         SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
224     }
225
226     /* Check or uncheck the minimize on use menu item */
227     if (TaskManagerSettings.MinimizeOnUse)
228         CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_CHECKED);
229     else
230         CheckMenuItem(hEditMenu, ID_OPTIONS_MINIMIZEONUSE, MF_BYCOMMAND|MF_UNCHECKED);
231
232     /* Check or uncheck the hide when minimized menu item */
233     if (TaskManagerSettings.HideWhenMinimized)
234         CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_CHECKED);
235     else
236         CheckMenuItem(hEditMenu, ID_OPTIONS_HIDEWHENMINIMIZED, MF_BYCOMMAND|MF_UNCHECKED);
237
238     /* Check or uncheck the show 16-bit tasks menu item */
239     if (TaskManagerSettings.Show16BitTasks)
240         CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
241     else
242         CheckMenuItem(hEditMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_UNCHECKED);
243
244     if (TaskManagerSettings.View_LargeIcons)
245         CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
246     else if (TaskManagerSettings.View_SmallIcons)
247         CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
248     else
249         CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
250
251     if (TaskManagerSettings.ShowKernelTimes)
252         CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
253     else
254         CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
255
256     if (TaskManagerSettings.UpdateSpeed == 1)
257         CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
258     else if (TaskManagerSettings.UpdateSpeed == 2)
259         CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
260     else if (TaskManagerSettings.UpdateSpeed == 4)
261         CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
262     else
263         CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
264
265     if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
266         CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
267     else
268         CheckMenuRadioItem(hCPUHistoryMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
269
270     nActivePage = TaskManagerSettings.ActiveTabPage;
271     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 0);
272     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 1);
273     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, 2);
274     TabCtrl_SetCurFocus/*Sel*/(hTabWnd, nActivePage);
275
276     if (TaskManagerSettings.UpdateSpeed == 1)
277         SetTimer(hWnd, 1, 1000, NULL);
278     else if (TaskManagerSettings.UpdateSpeed == 2)
279         SetTimer(hWnd, 1, 2000, NULL);
280     else if (TaskManagerSettings.UpdateSpeed == 4)
281         SetTimer(hWnd, 1, 4000, NULL);
282
283     /*
284      * Refresh the performance data
285      * Sample it twice so we can establish
286      * the delta values & cpu usage
287      */
288     PerfDataRefresh();
289     PerfDataRefresh();
290
291     RefreshApplicationPage();
292     RefreshProcessPage();
293     RefreshPerformancePage();
294
295     TrayIcon_ShellAddTrayIcon();
296
297     return TRUE;
298 }
299
300 /* OnMove()
301  * This function handles all the moving events for the application
302  * It moves every child window that needs moving
303  */
304 static void OnMove( UINT nType, int cx, int cy )
305 {
306 #ifdef __GNUC__TEST__
307     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);
308     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);
309     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);
310 #endif
311 }
312
313 /* OnSize()
314  * This function handles all the sizing events for the application
315  * It re-sizes every window, and child window that needs re-sizing
316  */
317 static void OnSize( UINT nType, int cx, int cy )
318 {
319     int     nParts[3];
320     int     nXDifference;
321     int     nYDifference;
322     RECT    rc;
323
324     if (nType == SIZE_MINIMIZED)
325     {
326         if(TaskManagerSettings.HideWhenMinimized)
327         {
328           ShowWindow(hMainWnd, SW_HIDE);
329         }
330         return;
331     }
332
333     nXDifference = cx - nOldWidth;
334     nYDifference = cy - nOldHeight;
335     nOldWidth = cx;
336     nOldHeight = cy;
337
338     /* Update the status bar size */
339     GetWindowRect(hStatusWnd, &rc);
340     SendMessageW(hStatusWnd, WM_SIZE, nType, MAKELPARAM(cx, cy + (rc.bottom - rc.top)));
341
342     /* Update the status bar pane sizes */
343     nParts[0] = bInMenuLoop ? -1 : 100;
344     nParts[1] = 210;
345     nParts[2] = cx;
346     SendMessageW(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (LPARAM)nParts);
347
348     /* Resize the tab control */
349     GetWindowRect(hTabWnd, &rc);
350     cx = (rc.right - rc.left) + nXDifference;
351     cy = (rc.bottom - rc.top) + nYDifference;
352     SetWindowPos(hTabWnd, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
353
354     /* Resize the application page */
355     GetWindowRect(hApplicationPage, &rc);
356     cx = (rc.right - rc.left) + nXDifference;
357     cy = (rc.bottom - rc.top) + nYDifference;
358     SetWindowPos(hApplicationPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
359     
360     /* Resize the process page */
361     GetWindowRect(hProcessPage, &rc);
362     cx = (rc.right - rc.left) + nXDifference;
363     cy = (rc.bottom - rc.top) + nYDifference;
364     SetWindowPos(hProcessPage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
365     
366     /* Resize the performance page */
367     GetWindowRect(hPerformancePage, &rc);
368     cx = (rc.right - rc.left) + nXDifference;
369     cy = (rc.bottom - rc.top) + nYDifference;
370     SetWindowPos(hPerformancePage, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
371 }
372
373 static void LoadSettings(void)
374 {
375     HKEY    hKey;
376     int     i;
377     DWORD   dwSize;
378
379    static const WCHAR    wszSubKey[] = {'S','o','f','t','w','a','r','e','\\',
380                                         'W','i','n','e','\\','T','a','s','k','M','a','n','a','g','e','r',0};
381    static const WCHAR    wszPreferences[] = {'P','r','e','f','e','r','e','n','c','e','s',0};
382
383     /* Window size & position settings */
384     TaskManagerSettings.Maximized = FALSE;
385     TaskManagerSettings.Left = 0;
386     TaskManagerSettings.Top = 0;
387     TaskManagerSettings.Right = 0;
388     TaskManagerSettings.Bottom = 0;
389
390     /* Tab settings */
391     TaskManagerSettings.ActiveTabPage = 0;
392
393     /* Options menu settings */
394     TaskManagerSettings.AlwaysOnTop = FALSE;
395     TaskManagerSettings.MinimizeOnUse = TRUE;
396     TaskManagerSettings.HideWhenMinimized = TRUE;
397     TaskManagerSettings.Show16BitTasks = TRUE;
398
399     /* Update speed settings */
400     TaskManagerSettings.UpdateSpeed = 2;
401
402     /* Applications page settings */
403     TaskManagerSettings.View_LargeIcons = FALSE;
404     TaskManagerSettings.View_SmallIcons = FALSE;
405     TaskManagerSettings.View_Details = TRUE;
406
407     /* Processes page settings */
408     TaskManagerSettings.ShowProcessesFromAllUsers = FALSE; /* Server-only? */
409     TaskManagerSettings.Column_ImageName = TRUE;
410     TaskManagerSettings.Column_PID = TRUE;
411     TaskManagerSettings.Column_CPUUsage = TRUE;
412     TaskManagerSettings.Column_CPUTime = TRUE;
413     TaskManagerSettings.Column_MemoryUsage = TRUE;
414     TaskManagerSettings.Column_MemoryUsageDelta = FALSE;
415     TaskManagerSettings.Column_PeakMemoryUsage = FALSE;
416     TaskManagerSettings.Column_PageFaults = FALSE;
417     TaskManagerSettings.Column_USERObjects = FALSE;
418     TaskManagerSettings.Column_IOReads = FALSE;
419     TaskManagerSettings.Column_IOReadBytes = FALSE;
420     TaskManagerSettings.Column_SessionID = FALSE; /* Server-only? */
421     TaskManagerSettings.Column_UserName = FALSE; /* Server-only? */
422     TaskManagerSettings.Column_PageFaultsDelta = FALSE;
423     TaskManagerSettings.Column_VirtualMemorySize = FALSE;
424     TaskManagerSettings.Column_PagedPool = FALSE;
425     TaskManagerSettings.Column_NonPagedPool = FALSE;
426     TaskManagerSettings.Column_BasePriority = FALSE;
427     TaskManagerSettings.Column_HandleCount = FALSE;
428     TaskManagerSettings.Column_ThreadCount = FALSE;
429     TaskManagerSettings.Column_GDIObjects = FALSE;
430     TaskManagerSettings.Column_IOWrites = FALSE;
431     TaskManagerSettings.Column_IOWriteBytes = FALSE;
432     TaskManagerSettings.Column_IOOther = FALSE;
433     TaskManagerSettings.Column_IOOtherBytes = FALSE;
434
435     for (i = 0; i < 25; i++) {
436         TaskManagerSettings.ColumnOrderArray[i] = i;
437     }
438     TaskManagerSettings.ColumnSizeArray[0] = 105;
439     TaskManagerSettings.ColumnSizeArray[1] = 50;
440     TaskManagerSettings.ColumnSizeArray[2] = 107;
441     TaskManagerSettings.ColumnSizeArray[3] = 70;
442     TaskManagerSettings.ColumnSizeArray[4] = 35;
443     TaskManagerSettings.ColumnSizeArray[5] = 70;
444     TaskManagerSettings.ColumnSizeArray[6] = 70;
445     TaskManagerSettings.ColumnSizeArray[7] = 100;
446     TaskManagerSettings.ColumnSizeArray[8] = 70;
447     TaskManagerSettings.ColumnSizeArray[9] = 70;
448     TaskManagerSettings.ColumnSizeArray[10] = 70;
449     TaskManagerSettings.ColumnSizeArray[11] = 70;
450     TaskManagerSettings.ColumnSizeArray[12] = 70;
451     TaskManagerSettings.ColumnSizeArray[13] = 70;
452     TaskManagerSettings.ColumnSizeArray[14] = 60;
453     TaskManagerSettings.ColumnSizeArray[15] = 60;
454     TaskManagerSettings.ColumnSizeArray[16] = 60;
455     TaskManagerSettings.ColumnSizeArray[17] = 60;
456     TaskManagerSettings.ColumnSizeArray[18] = 60;
457     TaskManagerSettings.ColumnSizeArray[19] = 70;
458     TaskManagerSettings.ColumnSizeArray[20] = 70;
459     TaskManagerSettings.ColumnSizeArray[21] = 70;
460     TaskManagerSettings.ColumnSizeArray[22] = 70;
461     TaskManagerSettings.ColumnSizeArray[23] = 70;
462     TaskManagerSettings.ColumnSizeArray[24] = 70;
463
464     TaskManagerSettings.SortColumn = 1;
465     TaskManagerSettings.SortAscending = TRUE;
466
467     /* Performance page settings */
468     TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
469     TaskManagerSettings.ShowKernelTimes = FALSE;
470
471     /* Open the key */
472     /* @@ Wine registry key: HKCU\Software\Wine\TaskManager */
473     if (RegOpenKeyExW(HKEY_CURRENT_USER, wszSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
474         return;
475     /* Read the settings */
476     dwSize = sizeof(TASKMANAGER_SETTINGS);
477     RegQueryValueExW(hKey, wszPreferences, NULL, NULL, (LPBYTE)&TaskManagerSettings, &dwSize);
478
479     /* Close the key */
480     RegCloseKey(hKey);
481 }
482
483 static void SaveSettings(void)
484 {
485     HKEY hKey;
486
487     static const WCHAR wszSubKey3[] = {'S','o','f','t','w','a','r','e','\\',
488                                        'W','i','n','e','\\','T','a','s','k','M','a','n','a','g','e','r',0};
489     static const WCHAR wszPreferences[] = {'P','r','e','f','e','r','e','n','c','e','s',0};
490
491     /* Open (or create) the key */
492
493     /* @@ Wine registry key: HKCU\Software\Wine\TaskManager */
494     if (RegCreateKeyExW(HKEY_CURRENT_USER, wszSubKey3, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
495         return;
496     /* Save the settings */
497     RegSetValueExW(hKey, wszPreferences, 0, REG_BINARY, (LPBYTE)&TaskManagerSettings, sizeof(TASKMANAGER_SETTINGS));
498     /* Close the key */
499     RegCloseKey(hKey);
500 }
501
502 static void TaskManager_OnRestoreMainWindow(void)
503 {
504   HMENU hMenu, hOptionsMenu;
505   BOOL OnTop;
506
507   hMenu = GetMenu(hMainWnd);
508   hOptionsMenu = GetSubMenu(hMenu, OPTIONS_MENU_INDEX);
509   OnTop = ((GetWindowLong(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
510   
511   OpenIcon(hMainWnd);
512   SetForegroundWindow(hMainWnd);
513   SetWindowPos(hMainWnd, (OnTop ? HWND_TOPMOST : HWND_TOP), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
514 }
515
516 static void TaskManager_OnEnterMenuLoop(HWND hWnd)
517 {
518     int nParts;
519
520     /* Update the status bar pane sizes */
521     nParts = -1;
522     SendMessageW(hStatusWnd, SB_SETPARTS, 1, (long)&nParts);
523     bInMenuLoop = TRUE;
524     SendMessageW(hStatusWnd, SB_SETTEXTW, 0, 0);
525 }
526
527 static void TaskManager_OnExitMenuLoop(HWND hWnd)
528 {
529     RECT  rc;
530     int   nParts[3];
531     WCHAR text[256];
532
533     static const WCHAR wszCPU_Usage[] = {'C','P','U',' ','U','s','a','g','e',':',' ','%','3','d','%','%',0};
534     static const WCHAR wszProcesses[] = {'P','r','o','c','e','s','s','e','s',':',' ','%','d',0};
535
536     bInMenuLoop = FALSE;
537     /* Update the status bar pane sizes */
538     GetClientRect(hWnd, &rc);
539     nParts[0] = 100;
540     nParts[1] = 210;
541     nParts[2] = rc.right;
542     SendMessageW(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
543     SendMessageW(hStatusWnd, SB_SETTEXT, 0, 0);
544     wsprintfW(text, wszCPU_Usage, PerfDataGetProcessorUsage());
545     SendMessageW(hStatusWnd, SB_SETTEXTW, 1, (LPARAM)text);
546     wsprintfW(text, wszProcesses, PerfDataGetProcessCount());
547     SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)text);
548 }
549
550 static void TaskManager_OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
551 {
552     WCHAR wstr[256] = {0};
553
554     LoadStringW(hInst, nItemID, wstr, sizeof(wstr)/sizeof(WCHAR));
555     SendMessageW(hStatusWnd, SB_SETTEXTW, 0, (LPARAM)wstr);
556 }
557
558 static void TaskManager_OnViewUpdateSpeedHigh(void)
559 {
560     HMENU hMenu;
561     HMENU hViewMenu;
562     HMENU hUpdateSpeedMenu;
563
564     hMenu = GetMenu(hMainWnd);
565     hViewMenu = GetSubMenu(hMenu, 2);
566     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
567
568     TaskManagerSettings.UpdateSpeed = 1;
569     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
570
571     KillTimer(hMainWnd, 1);
572     SetTimer(hMainWnd, 1, 1000, NULL);
573 }
574
575 static void TaskManager_OnViewUpdateSpeedNormal(void)
576 {
577     HMENU hMenu;
578     HMENU hViewMenu;
579     HMENU hUpdateSpeedMenu;
580
581     hMenu = GetMenu(hMainWnd);
582     hViewMenu = GetSubMenu(hMenu, 2);
583     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
584
585     TaskManagerSettings.UpdateSpeed = 2;
586     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
587
588     KillTimer(hMainWnd, 1);
589     SetTimer(hMainWnd, 1, 2000, NULL);
590 }
591
592 static void TaskManager_OnViewUpdateSpeedLow(void)
593 {
594     HMENU hMenu;
595     HMENU hViewMenu;
596     HMENU hUpdateSpeedMenu;
597
598     hMenu = GetMenu(hMainWnd);
599     hViewMenu = GetSubMenu(hMenu, 2);
600     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
601
602     TaskManagerSettings.UpdateSpeed = 4;
603     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
604
605     KillTimer(hMainWnd, 1);
606     SetTimer(hMainWnd, 1, 4000, NULL);
607 }
608
609 static void TaskManager_OnViewUpdateSpeedPaused(void)
610 {
611     HMENU hMenu;
612     HMENU hViewMenu;
613     HMENU hUpdateSpeedMenu;
614
615     hMenu = GetMenu(hMainWnd);
616     hViewMenu = GetSubMenu(hMenu, 2);
617     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
618     TaskManagerSettings.UpdateSpeed = 0;
619     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
620     KillTimer(hMainWnd, 1);
621 }
622
623 static void TaskManager_OnTabWndSelChange(void)
624 {
625     int   i;
626     HMENU hMenu;
627     HMENU hOptionsMenu;
628     HMENU hViewMenu;
629     HMENU hSubMenu;
630
631     static const WCHAR    wszLargeIcons[] = {'L','a','r','&','g','e',' ','I','c','o','n','s',0};
632     static const WCHAR    wszSmallIcons[] = {'S','&','m','a','l','l',' ','I','c','o','n','s',0};
633     static const WCHAR    wszDetails[] = {'&','D','e','t','a','i','l','s',0};
634     static const WCHAR    wszWindows[] = {'&','W','i','n','d','o','w','s',0};
635     static const WCHAR    wszSelectColumns[] = {'&','S','e','l','e','c','t',' ',
636                                                 'C','o','l','u','m','n','s','.','.','.',0};
637     static const WCHAR    wszShow16bTasks[] = {'&','S','h','o','w',' ','1','6','-','b','i','t',' ',
638                                                't','a','s','k','s',0};
639     static const WCHAR    wszOneGraphAllCPU[] = {'&','O','n','e',' ','G','r','a','p','h',',',' ',
640                                                  'A','l','l',' ','C','P','U','s',0};
641     static const WCHAR    wszOneGraphPerCPU[] = {'O','n','e',' ','G','r','a','p','h',' ',
642                                                  '&','P','e','r',' ','C','P','U',0};
643     static const WCHAR    wszCPUHistory[] = {'&','C','P','U',' ','H','i','s','t','o','r','y',0};
644     static const WCHAR    wszShowKernelTimes[] = {'&','S','h','o','w',' ','K','e','r','n','e','l',' ',
645                                                   'T','i','m','e','s',0};
646
647     hMenu = GetMenu(hMainWnd);
648     hViewMenu = GetSubMenu(hMenu, 2);
649     hOptionsMenu = GetSubMenu(hMenu, 1);
650     TaskManagerSettings.ActiveTabPage = TabCtrl_GetCurSel(hTabWnd);
651     for (i = GetMenuItemCount(hViewMenu) - 1; i > 2; i--) {
652         hSubMenu = GetSubMenu(hViewMenu, i);
653         if (hSubMenu)
654             DestroyMenu(hSubMenu);
655         RemoveMenu(hViewMenu, i, MF_BYPOSITION);
656     }
657     RemoveMenu(hOptionsMenu, 3, MF_BYPOSITION);
658     switch (TaskManagerSettings.ActiveTabPage) {
659     case 0:
660         ShowWindow(hApplicationPage, SW_SHOW);
661         ShowWindow(hProcessPage, SW_HIDE);
662         ShowWindow(hPerformancePage, SW_HIDE);
663         BringWindowToTop(hApplicationPage);
664         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_LARGE, wszLargeIcons);
665         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SMALL, wszSmallIcons);
666         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_DETAILS, wszDetails);
667
668         if (GetMenuItemCount(hMenu) <= 4) {
669             hSubMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_WINDOWSMENU));
670             InsertMenuW(hMenu, 3, MF_BYPOSITION|MF_POPUP, (UINT_PTR)hSubMenu, wszWindows);
671             DrawMenuBar(hMainWnd);
672         }
673         if (TaskManagerSettings.View_LargeIcons)
674             CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
675         else if (TaskManagerSettings.View_SmallIcons)
676             CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
677         else
678             CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
679         /*
680          * Give the application list control focus
681          */
682         SetFocus(hApplicationPageListCtrl);
683         break;
684
685     case 1:
686         ShowWindow(hApplicationPage, SW_HIDE);
687         ShowWindow(hProcessPage, SW_SHOW);
688         ShowWindow(hPerformancePage, SW_HIDE);
689         BringWindowToTop(hProcessPage);
690         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SELECTCOLUMNS, wszSelectColumns);
691         AppendMenuW(hOptionsMenu, MF_STRING, ID_OPTIONS_SHOW16BITTASKS, wszShow16bTasks);
692         if (TaskManagerSettings.Show16BitTasks)
693             CheckMenuItem(hOptionsMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
694         if (GetMenuItemCount(hMenu) > 4)
695         {
696             RemoveMenu(hMenu, 3, MF_BYPOSITION);
697             DrawMenuBar(hMainWnd);
698         }
699         /*
700          * Give the process list control focus
701          */
702         SetFocus(hProcessPageListCtrl);
703         break;
704
705     case 2:
706         ShowWindow(hApplicationPage, SW_HIDE);
707         ShowWindow(hProcessPage, SW_HIDE);
708         ShowWindow(hPerformancePage, SW_SHOW);
709         BringWindowToTop(hPerformancePage);
710         if (GetMenuItemCount(hMenu) > 4) {
711             RemoveMenu(hMenu, 3, MF_BYPOSITION);
712             DrawMenuBar(hMainWnd);
713         }
714         hSubMenu = CreatePopupMenu();
715         AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHALL, wszOneGraphAllCPU);
716         AppendMenuW(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, wszOneGraphPerCPU);
717         AppendMenuW(hViewMenu, MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, wszCPUHistory);
718         AppendMenuW(hViewMenu, MF_STRING, ID_VIEW_SHOWKERNELTIMES, wszShowKernelTimes);
719         if (TaskManagerSettings.ShowKernelTimes)
720             CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
721         else
722             CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
723         if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
724             CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
725         else
726             CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
727         /*
728          * Give the tab control focus
729          */
730         SetFocus(hTabWnd);
731         break;
732     }
733 }
734
735 LPWSTR GetLastErrorText(LPWSTR lpwszBuf, DWORD dwSize)
736 {
737     DWORD  dwRet;
738     LPWSTR lpwszTemp = NULL;
739     static const WCHAR    wszFormat[] = {'%','s',' ','(','%','u',')',0};
740
741     dwRet = FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
742                            NULL,
743                            GetLastError(),
744                            LANG_NEUTRAL,
745                            (LPWSTR)&lpwszTemp,
746                            0,
747                            NULL );
748
749     /* supplied buffer is not long enough */
750     if (!dwRet || ( (long)dwSize < (long)dwRet+14)) {
751         lpwszBuf[0] = '\0';
752     } else {
753         lpwszTemp[strlenW(lpwszTemp)-2] = '\0';  /* remove cr and newline character */
754         sprintfW(lpwszBuf, wszFormat, lpwszTemp, GetLastError());
755     }
756     if (lpwszTemp) {
757         LocalFree((HLOCAL)lpwszTemp);
758     }
759     return lpwszBuf;
760 }
761
762 /* Message handler for dialog box. */
763 static INT_PTR CALLBACK
764 TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
765 {
766     HDC             hdc;
767     PAINTSTRUCT     ps;
768     LPRECT          pRC;
769     RECT            rc;
770     int             idctrl;
771     LPNMHDR         pnmh;
772     WINDOWPLACEMENT wp;
773
774     switch (message) {
775     case WM_INITDIALOG:
776         hMainWnd = hDlg;
777         return OnCreate(hDlg);
778
779     case WM_COMMAND:
780         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
781             EndDialog(hDlg, LOWORD(wParam));
782             return TRUE;
783         }
784         /* Process menu commands */
785         switch (LOWORD(wParam))
786         {
787         case ID_FILE_NEW:
788             TaskManager_OnFileNew();
789             break;
790         case ID_OPTIONS_ALWAYSONTOP:
791             TaskManager_OnOptionsAlwaysOnTop();
792             break;
793         case ID_OPTIONS_MINIMIZEONUSE:
794             TaskManager_OnOptionsMinimizeOnUse();
795             break;
796         case ID_OPTIONS_HIDEWHENMINIMIZED:
797             TaskManager_OnOptionsHideWhenMinimized();
798             break;
799         case ID_OPTIONS_SHOW16BITTASKS:
800             TaskManager_OnOptionsShow16BitTasks();
801             break;
802         case ID_RESTORE:
803             TaskManager_OnRestoreMainWindow();
804             break;
805         case ID_VIEW_LARGE:
806             ApplicationPage_OnViewLargeIcons();
807             break;
808         case ID_VIEW_SMALL:
809             ApplicationPage_OnViewSmallIcons();
810             break;
811         case ID_VIEW_DETAILS:
812             ApplicationPage_OnViewDetails();
813             break;
814         case ID_VIEW_SHOWKERNELTIMES:
815             PerformancePage_OnViewShowKernelTimes();
816             break;
817         case ID_VIEW_CPUHISTORY_ONEGRAPHALL:
818             PerformancePage_OnViewCPUHistoryOneGraphAll();
819             break;
820         case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU:
821             PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
822             break;
823         case ID_VIEW_UPDATESPEED_HIGH:
824             TaskManager_OnViewUpdateSpeedHigh();
825             break;
826         case ID_VIEW_UPDATESPEED_NORMAL:
827             TaskManager_OnViewUpdateSpeedNormal();
828             break;
829         case ID_VIEW_UPDATESPEED_LOW:
830             TaskManager_OnViewUpdateSpeedLow();
831             break;
832         case ID_VIEW_UPDATESPEED_PAUSED:
833             TaskManager_OnViewUpdateSpeedPaused();
834             break;
835         case ID_VIEW_SELECTCOLUMNS:
836             ProcessPage_OnViewSelectColumns();
837             break;
838         case ID_VIEW_REFRESH:
839             PostMessage(hDlg, WM_TIMER, 0, 0);
840             break;
841         case ID_WINDOWS_TILEHORIZONTALLY:
842             ApplicationPage_OnWindowsTileHorizontally();
843             break;
844         case ID_WINDOWS_TILEVERTICALLY:
845             ApplicationPage_OnWindowsTileVertically();
846             break;
847         case ID_WINDOWS_MINIMIZE:
848             ApplicationPage_OnWindowsMinimize();
849             break;
850         case ID_WINDOWS_MAXIMIZE:
851             ApplicationPage_OnWindowsMaximize();
852             break;
853         case ID_WINDOWS_CASCADE:
854             ApplicationPage_OnWindowsCascade();
855             break;
856         case ID_WINDOWS_BRINGTOFRONT:
857             ApplicationPage_OnWindowsBringToFront();
858             break;
859         case ID_APPLICATION_PAGE_SWITCHTO:
860             ApplicationPage_OnSwitchTo();
861             break;
862         case ID_APPLICATION_PAGE_ENDTASK:
863             ApplicationPage_OnEndTask();
864             break;
865         case ID_APPLICATION_PAGE_GOTOPROCESS:
866             ApplicationPage_OnGotoProcess();
867             break;
868         case ID_PROCESS_PAGE_ENDPROCESS:
869             ProcessPage_OnEndProcess();
870             break;
871         case ID_PROCESS_PAGE_ENDPROCESSTREE:
872             ProcessPage_OnEndProcessTree();
873             break;
874         case ID_PROCESS_PAGE_DEBUG:
875             ProcessPage_OnDebug();
876             break;
877         case ID_PROCESS_PAGE_SETAFFINITY:
878             ProcessPage_OnSetAffinity();
879             break;
880         case ID_PROCESS_PAGE_SETPRIORITY_REALTIME:
881             ProcessPage_OnSetPriorityRealTime();
882             break;
883         case ID_PROCESS_PAGE_SETPRIORITY_HIGH:
884             ProcessPage_OnSetPriorityHigh();
885             break;
886         case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL:
887             ProcessPage_OnSetPriorityAboveNormal();
888             break;
889         case ID_PROCESS_PAGE_SETPRIORITY_NORMAL:
890             ProcessPage_OnSetPriorityNormal();
891             break;
892         case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL:
893             ProcessPage_OnSetPriorityBelowNormal();
894             break;
895         case ID_PROCESS_PAGE_SETPRIORITY_LOW:
896             ProcessPage_OnSetPriorityLow();
897             break;
898         case ID_PROCESS_PAGE_DEBUGCHANNELS:
899             ProcessPage_OnDebugChannels();
900             break;
901         case ID_HELP_ABOUT:
902             OnAbout();
903             break;
904         case ID_FILE_EXIT:
905             EndDialog(hDlg, IDOK);
906             break;
907         }     
908         break;
909
910     case WM_ONTRAYICON:
911         switch(lParam)
912         {
913         case WM_RBUTTONDOWN:
914             {
915             POINT pt;
916             BOOL OnTop;
917             HMENU hMenu, hPopupMenu;
918             
919             GetCursorPos(&pt);
920             
921             OnTop = ((GetWindowLong(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
922             
923             hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_TRAY_POPUP));
924             hPopupMenu = GetSubMenu(hMenu, 0);
925             
926             if(IsWindowVisible(hMainWnd))
927             {
928               DeleteMenu(hPopupMenu, ID_RESTORE, MF_BYCOMMAND);
929             }
930             else
931             {
932               SetMenuDefaultItem(hPopupMenu, ID_RESTORE, FALSE);
933             }
934             
935             if(OnTop)
936             {
937               CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_CHECKED);
938             }
939             
940             SetForegroundWindow(hMainWnd);
941             TrackPopupMenuEx(hPopupMenu, 0, pt.x, pt.y, hMainWnd, NULL);
942             
943             DestroyMenu(hMenu);
944             break;
945             }
946         case WM_LBUTTONDBLCLK:
947             TaskManager_OnRestoreMainWindow();
948             break;
949         }
950         break;
951
952     case WM_NOTIFY:
953         idctrl = (int)wParam;
954         pnmh = (LPNMHDR)lParam;
955         if ((pnmh->hwndFrom == hTabWnd) &&
956             (pnmh->idFrom == IDC_TAB) &&
957             (pnmh->code == TCN_SELCHANGE))
958         {
959             TaskManager_OnTabWndSelChange();
960         }
961         break;
962
963     case WM_NCPAINT:
964         hdc = GetDC(hDlg);
965         GetClientRect(hDlg, &rc);
966         Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
967         ReleaseDC(hDlg, hdc);
968         break;
969
970     case WM_PAINT:
971         hdc = BeginPaint(hDlg, &ps);
972         GetClientRect(hDlg, &rc);
973         Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
974         EndPaint(hDlg, &ps);
975         break;
976
977     case WM_SIZING:
978         /* Make sure the user is sizing the dialog */
979         /* in an acceptable range */
980         pRC = (LPRECT)lParam;
981         if ((wParam == WMSZ_LEFT) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_BOTTOMLEFT)) {
982             /* If the width is too small enlarge it to the minimum */
983             if (nMinimumWidth > (pRC->right - pRC->left))
984                 pRC->left = pRC->right - nMinimumWidth;
985         } else {
986             /* If the width is too small enlarge it to the minimum */
987             if (nMinimumWidth > (pRC->right - pRC->left))
988                 pRC->right = pRC->left + nMinimumWidth;
989         }
990         if ((wParam == WMSZ_TOP) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_TOPRIGHT)) {
991             /* If the height is too small enlarge it to the minimum */
992             if (nMinimumHeight > (pRC->bottom - pRC->top))
993                 pRC->top = pRC->bottom - nMinimumHeight;
994         } else {
995             /* If the height is too small enlarge it to the minimum */
996             if (nMinimumHeight > (pRC->bottom - pRC->top))
997                 pRC->bottom = pRC->top + nMinimumHeight;
998         }
999         return TRUE;
1000
1001     case WM_SIZE:
1002         /* Handle the window sizing in it's own function */
1003         OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
1004         break;
1005
1006     case WM_MOVE:
1007         /* Handle the window moving in it's own function */
1008         OnMove(wParam, LOWORD(lParam), HIWORD(lParam));
1009         break;
1010
1011     case WM_DESTROY:
1012         ShowWindow(hDlg, SW_HIDE);
1013         TrayIcon_ShellRemoveTrayIcon();
1014         wp.length = sizeof(WINDOWPLACEMENT);
1015         GetWindowPlacement(hDlg, &wp);
1016         TaskManagerSettings.Left = wp.rcNormalPosition.left;
1017         TaskManagerSettings.Top = wp.rcNormalPosition.top;
1018         TaskManagerSettings.Right = wp.rcNormalPosition.right;
1019         TaskManagerSettings.Bottom = wp.rcNormalPosition.bottom;
1020         if (IsZoomed(hDlg) || (wp.flags & WPF_RESTORETOMAXIMIZED))
1021             TaskManagerSettings.Maximized = TRUE;
1022         else
1023             TaskManagerSettings.Maximized = FALSE;
1024         return DefWindowProc(hDlg, message, wParam, lParam);
1025         
1026     case WM_TIMER:
1027         /* Refresh the performance data */
1028         PerfDataRefresh();
1029         RefreshApplicationPage();
1030         RefreshProcessPage();
1031         RefreshPerformancePage();
1032         TrayIcon_ShellUpdateTrayIcon();
1033         break;
1034
1035     case WM_ENTERMENULOOP:
1036         TaskManager_OnEnterMenuLoop(hDlg);
1037         break;
1038     case WM_EXITMENULOOP:
1039         TaskManager_OnExitMenuLoop(hDlg);
1040         break;
1041     case WM_MENUSELECT:
1042         TaskManager_OnMenuSelect(hDlg, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
1043         break;
1044     }
1045
1046     return 0;
1047 }
1048
1049 int APIENTRY WinMain(HINSTANCE hInstance,
1050                      HINSTANCE hPrevInstance,
1051                      LPSTR     lpCmdLine,
1052                      int       nCmdShow)
1053 {
1054     HANDLE hProcess;
1055     HANDLE hToken; 
1056     TOKEN_PRIVILEGES tkp; 
1057
1058     /* Initialize global variables */
1059     hInst = hInstance;
1060
1061     /* Change our priority class to HIGH */
1062     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1063     SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
1064     CloseHandle(hProcess);
1065
1066     /* Now let's get the SE_DEBUG_NAME privilege
1067      * so that we can debug processes 
1068      */
1069
1070     /* Get a token for this process.  */
1071     if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1072         /* Get the LUID for the debug privilege.  */
1073         LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid); 
1074
1075         tkp.PrivilegeCount = 1;  /* one privilege to set */
1076         tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
1077
1078         /* Get the debug privilege for this process. */
1079         AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); 
1080     }
1081
1082     /* Load our settings from the registry */
1083     LoadSettings();
1084
1085     /* Initialize perf data */
1086     if (!PerfDataInitialize()) {
1087         return -1;
1088     }
1089
1090     DialogBoxW(hInst, (LPWSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc);
1091  
1092     /* Save our settings to the registry */
1093     SaveSettings();
1094     PerfDataUninitialize();
1095     return 0;
1096 }