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