wined3d: Unify color conversion code.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <tchar.h>
30 #include <stdio.h>
31 #include <winnt.h>
32
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, LPCTSTR lpszText, 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 = (HBITMAP)SelectObject(hFontDC, hFontBitmap);
100
101     for (i = 0; i < (int)_tcslen(lpszText); i++) {
102         if ((lpszText[i] >= '0') && (lpszText[i] <= '9')) {
103             BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, (lpszText[i] - '0') * 8, 0, SRCCOPY);
104         }
105         else if (lpszText[i] == 'K')
106         {
107             BitBlt(hDC, x + (i * 8), y, 8, 11, hFontDC, 80, 0, SRCCOPY);
108         }
109         else if (lpszText[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     TCHAR   szTemp[256];
130     TCITEM  item;
131
132     SendMessage(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(hInst, MAKEINTRESOURCE(IDI_TASKMANAGER)));
133
134     /* Initialize the Windows Common Controls DLL */
135     InitCommonControls();
136
137     /* Get the minimum window sizes */
138     GetWindowRect(hWnd, &rc);
139     nMinimumWidth = (rc.right - rc.left);
140     nMinimumHeight = (rc.bottom - rc.top);
141
142     /* Create the status bar */
143     hStatusWnd = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS, _T(""), hWnd, STATUS_WINDOW);
144     if(!hStatusWnd)
145         return FALSE;
146
147     /* Create the status bar panes */
148     nParts[0] = 100;
149     nParts[1] = 210;
150     nParts[2] = 400;
151     SendMessage(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
152
153     /* Create tab pages */
154     hTabWnd = GetDlgItem(hWnd, IDC_TAB);
155 #if 1
156     hApplicationPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_APPLICATION_PAGE), hWnd, ApplicationPageWndProc);
157     hProcessPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PROCESS_PAGE), hWnd, ProcessPageWndProc);
158     hPerformancePage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PERFORMANCE_PAGE), hWnd, PerformancePageWndProc);
159 #else
160     hApplicationPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_APPLICATION_PAGE), hTabWnd, ApplicationPageWndProc);
161     hProcessPage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PROCESS_PAGE), hTabWnd, ProcessPageWndProc);
162     hPerformancePage = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PERFORMANCE_PAGE), hTabWnd, PerformancePageWndProc);
163 #endif
164
165     /* Insert tabs */
166     _tcscpy(szTemp, _T("Applications"));
167     memset(&item, 0, sizeof(TCITEM));
168     item.mask = TCIF_TEXT;
169     item.pszText = szTemp;
170     SendMessage(hTabWnd, TCM_INSERTITEM, 0, (LPARAM)&item);
171     _tcscpy(szTemp, _T("Processes"));
172     memset(&item, 0, sizeof(TCITEM));
173     item.mask = TCIF_TEXT;
174     item.pszText = szTemp;
175     SendMessage(hTabWnd, TCM_INSERTITEM, 1, (LPARAM)&item);
176     _tcscpy(szTemp, _T("Performance"));
177     memset(&item, 0, sizeof(TCITEM));
178     item.mask = TCIF_TEXT;
179     item.pszText = szTemp;
180     SendMessage(hTabWnd, TCM_INSERTITEM, 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     SendMessage(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     SendMessage(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (long)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     TCHAR   szSubKey[] = _T("Software\\Wine\\TaskManager");
376     int     i;
377     DWORD   dwSize;
378
379     /* Window size & position settings */
380     TaskManagerSettings.Maximized = FALSE;
381     TaskManagerSettings.Left = 0;
382     TaskManagerSettings.Top = 0;
383     TaskManagerSettings.Right = 0;
384     TaskManagerSettings.Bottom = 0;
385
386     /* Tab settings */
387     TaskManagerSettings.ActiveTabPage = 0;
388
389     /* Options menu settings */
390     TaskManagerSettings.AlwaysOnTop = FALSE;
391     TaskManagerSettings.MinimizeOnUse = TRUE;
392     TaskManagerSettings.HideWhenMinimized = TRUE;
393     TaskManagerSettings.Show16BitTasks = TRUE;
394
395     /* Update speed settings */
396     TaskManagerSettings.UpdateSpeed = 2;
397
398     /* Applications page settings */
399     TaskManagerSettings.View_LargeIcons = FALSE;
400     TaskManagerSettings.View_SmallIcons = FALSE;
401     TaskManagerSettings.View_Details = TRUE;
402
403     /* Processes page settings */
404     TaskManagerSettings.ShowProcessesFromAllUsers = FALSE; /* Server-only? */
405     TaskManagerSettings.Column_ImageName = TRUE;
406     TaskManagerSettings.Column_PID = TRUE;
407     TaskManagerSettings.Column_CPUUsage = TRUE;
408     TaskManagerSettings.Column_CPUTime = TRUE;
409     TaskManagerSettings.Column_MemoryUsage = TRUE;
410     TaskManagerSettings.Column_MemoryUsageDelta = FALSE;
411     TaskManagerSettings.Column_PeakMemoryUsage = FALSE;
412     TaskManagerSettings.Column_PageFaults = FALSE;
413     TaskManagerSettings.Column_USERObjects = FALSE;
414     TaskManagerSettings.Column_IOReads = FALSE;
415     TaskManagerSettings.Column_IOReadBytes = FALSE;
416     TaskManagerSettings.Column_SessionID = FALSE; /* Server-only? */
417     TaskManagerSettings.Column_UserName = FALSE; /* Server-only? */
418     TaskManagerSettings.Column_PageFaultsDelta = FALSE;
419     TaskManagerSettings.Column_VirtualMemorySize = FALSE;
420     TaskManagerSettings.Column_PagedPool = FALSE;
421     TaskManagerSettings.Column_NonPagedPool = FALSE;
422     TaskManagerSettings.Column_BasePriority = FALSE;
423     TaskManagerSettings.Column_HandleCount = FALSE;
424     TaskManagerSettings.Column_ThreadCount = FALSE;
425     TaskManagerSettings.Column_GDIObjects = FALSE;
426     TaskManagerSettings.Column_IOWrites = FALSE;
427     TaskManagerSettings.Column_IOWriteBytes = FALSE;
428     TaskManagerSettings.Column_IOOther = FALSE;
429     TaskManagerSettings.Column_IOOtherBytes = FALSE;
430
431     for (i = 0; i < 25; i++) {
432         TaskManagerSettings.ColumnOrderArray[i] = i;
433     }
434     TaskManagerSettings.ColumnSizeArray[0] = 105;
435     TaskManagerSettings.ColumnSizeArray[1] = 50;
436     TaskManagerSettings.ColumnSizeArray[2] = 107;
437     TaskManagerSettings.ColumnSizeArray[3] = 70;
438     TaskManagerSettings.ColumnSizeArray[4] = 35;
439     TaskManagerSettings.ColumnSizeArray[5] = 70;
440     TaskManagerSettings.ColumnSizeArray[6] = 70;
441     TaskManagerSettings.ColumnSizeArray[7] = 100;
442     TaskManagerSettings.ColumnSizeArray[8] = 70;
443     TaskManagerSettings.ColumnSizeArray[9] = 70;
444     TaskManagerSettings.ColumnSizeArray[10] = 70;
445     TaskManagerSettings.ColumnSizeArray[11] = 70;
446     TaskManagerSettings.ColumnSizeArray[12] = 70;
447     TaskManagerSettings.ColumnSizeArray[13] = 70;
448     TaskManagerSettings.ColumnSizeArray[14] = 60;
449     TaskManagerSettings.ColumnSizeArray[15] = 60;
450     TaskManagerSettings.ColumnSizeArray[16] = 60;
451     TaskManagerSettings.ColumnSizeArray[17] = 60;
452     TaskManagerSettings.ColumnSizeArray[18] = 60;
453     TaskManagerSettings.ColumnSizeArray[19] = 70;
454     TaskManagerSettings.ColumnSizeArray[20] = 70;
455     TaskManagerSettings.ColumnSizeArray[21] = 70;
456     TaskManagerSettings.ColumnSizeArray[22] = 70;
457     TaskManagerSettings.ColumnSizeArray[23] = 70;
458     TaskManagerSettings.ColumnSizeArray[24] = 70;
459
460     TaskManagerSettings.SortColumn = 1;
461     TaskManagerSettings.SortAscending = TRUE;
462
463     /* Performance page settings */
464     TaskManagerSettings.CPUHistory_OneGraphPerCPU = TRUE;
465     TaskManagerSettings.ShowKernelTimes = FALSE;
466
467     /* Open the key */
468     /* @@ Wine registry key: HKCU\Software\Wine\TaskManager */
469     if (RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
470         return;
471     /* Read the settings */
472     dwSize = sizeof(TASKMANAGER_SETTINGS);
473     RegQueryValueEx(hKey, _T("Preferences"), NULL, NULL, (LPBYTE)&TaskManagerSettings, &dwSize);
474
475     /* Close the key */
476     RegCloseKey(hKey);
477 }
478
479 static void SaveSettings(void)
480 {
481     HKEY hKey;
482     TCHAR szSubKey3[] = _T("Software\\Wine\\TaskManager");
483
484     /* Open (or create) the key */
485
486     /* @@ Wine registry key: HKCU\Software\Wine\TaskManager */
487     if (RegCreateKeyEx(HKEY_CURRENT_USER, szSubKey3, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) != ERROR_SUCCESS)
488         return;
489     /* Save the settings */
490     RegSetValueEx(hKey, _T("Preferences"), 0, REG_BINARY, (LPBYTE)&TaskManagerSettings, sizeof(TASKMANAGER_SETTINGS));
491     /* Close the key */
492     RegCloseKey(hKey);
493 }
494
495 static void TaskManager_OnRestoreMainWindow(void)
496 {
497   HMENU hMenu, hOptionsMenu;
498   BOOL OnTop;
499
500   hMenu = GetMenu(hMainWnd);
501   hOptionsMenu = GetSubMenu(hMenu, OPTIONS_MENU_INDEX);
502   OnTop = ((GetWindowLong(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
503   
504   OpenIcon(hMainWnd);
505   SetForegroundWindow(hMainWnd);
506   SetWindowPos(hMainWnd, (OnTop ? HWND_TOPMOST : HWND_TOP), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
507 }
508
509 static void TaskManager_OnEnterMenuLoop(HWND hWnd)
510 {
511     int nParts;
512
513     /* Update the status bar pane sizes */
514     nParts = -1;
515     SendMessage(hStatusWnd, SB_SETPARTS, 1, (long)&nParts);
516     bInMenuLoop = TRUE;
517     SendMessage(hStatusWnd, SB_SETTEXT, (WPARAM)0, (LPARAM)_T(""));
518 }
519
520 static void TaskManager_OnExitMenuLoop(HWND hWnd)
521 {
522     RECT  rc;
523     int   nParts[3];
524     TCHAR text[260];
525
526     bInMenuLoop = FALSE;
527     /* Update the status bar pane sizes */
528     GetClientRect(hWnd, &rc);
529     nParts[0] = 100;
530     nParts[1] = 210;
531     nParts[2] = rc.right;
532     SendMessage(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
533     SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)_T(""));
534     wsprintf(text, _T("CPU Usage: %3d%%"), PerfDataGetProcessorUsage());
535     SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
536     wsprintf(text, _T("Processes: %d"), PerfDataGetProcessCount());
537     SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
538 }
539
540 static void TaskManager_OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
541 {
542     TCHAR str[100];
543
544     _tcscpy(str, TEXT(""));
545     if (LoadString(hInst, nItemID, str, 100)) {
546         /* load appropriate string */
547         LPTSTR lpsz = str;
548         /* first newline terminates actual string */
549         lpsz = _tcschr(lpsz, '\n');
550         if (lpsz != NULL)
551             *lpsz = '\0';
552     }
553     SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)str);
554 }
555
556 static void TaskManager_OnViewUpdateSpeedHigh(void)
557 {
558     HMENU hMenu;
559     HMENU hViewMenu;
560     HMENU hUpdateSpeedMenu;
561
562     hMenu = GetMenu(hMainWnd);
563     hViewMenu = GetSubMenu(hMenu, 2);
564     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
565
566     TaskManagerSettings.UpdateSpeed = 1;
567     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_HIGH, MF_BYCOMMAND);
568
569     KillTimer(hMainWnd, 1);
570     SetTimer(hMainWnd, 1, 1000, NULL);
571 }
572
573 static void TaskManager_OnViewUpdateSpeedNormal(void)
574 {
575     HMENU hMenu;
576     HMENU hViewMenu;
577     HMENU hUpdateSpeedMenu;
578
579     hMenu = GetMenu(hMainWnd);
580     hViewMenu = GetSubMenu(hMenu, 2);
581     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
582
583     TaskManagerSettings.UpdateSpeed = 2;
584     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_NORMAL, MF_BYCOMMAND);
585
586     KillTimer(hMainWnd, 1);
587     SetTimer(hMainWnd, 1, 2000, NULL);
588 }
589
590 static void TaskManager_OnViewUpdateSpeedLow(void)
591 {
592     HMENU hMenu;
593     HMENU hViewMenu;
594     HMENU hUpdateSpeedMenu;
595
596     hMenu = GetMenu(hMainWnd);
597     hViewMenu = GetSubMenu(hMenu, 2);
598     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
599
600     TaskManagerSettings.UpdateSpeed = 4;
601     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_LOW, MF_BYCOMMAND);
602
603     KillTimer(hMainWnd, 1);
604     SetTimer(hMainWnd, 1, 4000, NULL);
605 }
606
607 static void TaskManager_OnViewUpdateSpeedPaused(void)
608 {
609     HMENU hMenu;
610     HMENU hViewMenu;
611     HMENU hUpdateSpeedMenu;
612
613     hMenu = GetMenu(hMainWnd);
614     hViewMenu = GetSubMenu(hMenu, 2);
615     hUpdateSpeedMenu = GetSubMenu(hViewMenu, 1);
616     TaskManagerSettings.UpdateSpeed = 0;
617     CheckMenuRadioItem(hUpdateSpeedMenu, ID_VIEW_UPDATESPEED_HIGH, ID_VIEW_UPDATESPEED_PAUSED, ID_VIEW_UPDATESPEED_PAUSED, MF_BYCOMMAND);
618     KillTimer(hMainWnd, 1);
619 }
620
621 static void TaskManager_OnTabWndSelChange(void)
622 {
623     int   i;
624     HMENU hMenu;
625     HMENU hOptionsMenu;
626     HMENU hViewMenu;
627     HMENU hSubMenu;
628
629     hMenu = GetMenu(hMainWnd);
630     hViewMenu = GetSubMenu(hMenu, 2);
631     hOptionsMenu = GetSubMenu(hMenu, 1);
632     TaskManagerSettings.ActiveTabPage = TabCtrl_GetCurSel(hTabWnd);
633     for (i = GetMenuItemCount(hViewMenu) - 1; i > 2; i--) {
634         hSubMenu = GetSubMenu(hViewMenu, i);
635         if (hSubMenu)
636             DestroyMenu(hSubMenu);
637         RemoveMenu(hViewMenu, i, MF_BYPOSITION);
638     }
639     RemoveMenu(hOptionsMenu, 3, MF_BYPOSITION);
640     switch (TaskManagerSettings.ActiveTabPage) {
641     case 0:
642         ShowWindow(hApplicationPage, SW_SHOW);
643         ShowWindow(hProcessPage, SW_HIDE);
644         ShowWindow(hPerformancePage, SW_HIDE);
645         BringWindowToTop(hApplicationPage);
646         AppendMenu(hViewMenu, MF_STRING, ID_VIEW_LARGE, _T("Lar&ge Icons"));
647         AppendMenu(hViewMenu, MF_STRING, ID_VIEW_SMALL, _T("S&mall Icons"));
648         AppendMenu(hViewMenu, MF_STRING, ID_VIEW_DETAILS, _T("&Details"));
649
650         if (GetMenuItemCount(hMenu) <= 4) {
651             hSubMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_WINDOWSMENU));
652             InsertMenu(hMenu, 3, MF_BYPOSITION|MF_POPUP, (UINT_PTR)hSubMenu, _T("&Windows"));
653             DrawMenuBar(hMainWnd);
654         }
655         if (TaskManagerSettings.View_LargeIcons)
656             CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_LARGE, MF_BYCOMMAND);
657         else if (TaskManagerSettings.View_SmallIcons)
658             CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_SMALL, MF_BYCOMMAND);
659         else
660             CheckMenuRadioItem(hViewMenu, ID_VIEW_LARGE, ID_VIEW_DETAILS, ID_VIEW_DETAILS, MF_BYCOMMAND);
661         /*
662          * Give the application list control focus
663          */
664         SetFocus(hApplicationPageListCtrl);
665         break;
666
667     case 1:
668         ShowWindow(hApplicationPage, SW_HIDE);
669         ShowWindow(hProcessPage, SW_SHOW);
670         ShowWindow(hPerformancePage, SW_HIDE);
671         BringWindowToTop(hProcessPage);
672         AppendMenu(hViewMenu, MF_STRING, ID_VIEW_SELECTCOLUMNS, _T("&Select Columns..."));
673         AppendMenu(hOptionsMenu, MF_STRING, ID_OPTIONS_SHOW16BITTASKS, _T("&Show 16-bit tasks"));
674         if (TaskManagerSettings.Show16BitTasks)
675             CheckMenuItem(hOptionsMenu, ID_OPTIONS_SHOW16BITTASKS, MF_BYCOMMAND|MF_CHECKED);
676         if (GetMenuItemCount(hMenu) > 4)
677         {
678             RemoveMenu(hMenu, 3, MF_BYPOSITION);
679             DrawMenuBar(hMainWnd);
680         }
681         /*
682          * Give the process list control focus
683          */
684         SetFocus(hProcessPageListCtrl);
685         break;
686
687     case 2:
688         ShowWindow(hApplicationPage, SW_HIDE);
689         ShowWindow(hProcessPage, SW_HIDE);
690         ShowWindow(hPerformancePage, SW_SHOW);
691         BringWindowToTop(hPerformancePage);
692         if (GetMenuItemCount(hMenu) > 4) {
693             RemoveMenu(hMenu, 3, MF_BYPOSITION);
694             DrawMenuBar(hMainWnd);
695         }
696         hSubMenu = CreatePopupMenu();
697         AppendMenu(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHALL, _T("&One Graph, All CPUs"));
698         AppendMenu(hSubMenu, MF_STRING, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, _T("One Graph &Per CPU"));
699         AppendMenu(hViewMenu, MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, _T("&CPU History"));
700         AppendMenu(hViewMenu, MF_STRING, ID_VIEW_SHOWKERNELTIMES, _T("&Show Kernel Times"));
701         if (TaskManagerSettings.ShowKernelTimes)
702             CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_CHECKED);
703         else
704             CheckMenuItem(hViewMenu, ID_VIEW_SHOWKERNELTIMES, MF_BYCOMMAND|MF_UNCHECKED);
705         if (TaskManagerSettings.CPUHistory_OneGraphPerCPU)
706             CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, MF_BYCOMMAND);
707         else
708             CheckMenuRadioItem(hSubMenu, ID_VIEW_CPUHISTORY_ONEGRAPHALL, ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU, ID_VIEW_CPUHISTORY_ONEGRAPHALL, MF_BYCOMMAND);
709         /*
710          * Give the tab control focus
711          */
712         SetFocus(hTabWnd);
713         break;
714     }
715 }
716
717 LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize)
718 {
719     DWORD  dwRet;
720     LPTSTR lpszTemp = NULL;
721
722     dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
723                            NULL,
724                            GetLastError(),
725                            LANG_NEUTRAL,
726                            (LPTSTR)&lpszTemp,
727                            0,
728                            NULL );
729
730     /* supplied buffer is not long enough */
731     if (!dwRet || ( (long)dwSize < (long)dwRet+14)) {
732         lpszBuf[0] = TEXT('\0');
733     } else {
734         lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0');  /* remove cr and newline character */
735         _stprintf(lpszBuf, TEXT("%s (%u)"), lpszTemp, GetLastError());
736     }
737     if (lpszTemp) {
738         LocalFree((HLOCAL)lpszTemp);
739     }
740     return lpszBuf;
741 }
742
743 /* Message handler for dialog box. */
744 static INT_PTR CALLBACK
745 TaskManagerWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
746 {
747     HDC             hdc;
748     PAINTSTRUCT     ps;
749     LPRECT          pRC;
750     RECT            rc;
751     int             idctrl;
752     LPNMHDR         pnmh;
753     WINDOWPLACEMENT wp;
754
755     switch (message) {
756     case WM_INITDIALOG:
757         hMainWnd = hDlg;
758         return OnCreate(hDlg);
759
760     case WM_COMMAND:
761         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
762             EndDialog(hDlg, LOWORD(wParam));
763             return TRUE;
764         }
765         /* Process menu commands */
766         switch (LOWORD(wParam))
767         {
768         case ID_FILE_NEW:
769             TaskManager_OnFileNew();
770             break;
771         case ID_OPTIONS_ALWAYSONTOP:
772             TaskManager_OnOptionsAlwaysOnTop();
773             break;
774         case ID_OPTIONS_MINIMIZEONUSE:
775             TaskManager_OnOptionsMinimizeOnUse();
776             break;
777         case ID_OPTIONS_HIDEWHENMINIMIZED:
778             TaskManager_OnOptionsHideWhenMinimized();
779             break;
780         case ID_OPTIONS_SHOW16BITTASKS:
781             TaskManager_OnOptionsShow16BitTasks();
782             break;
783         case ID_RESTORE:
784             TaskManager_OnRestoreMainWindow();
785             break;
786         case ID_VIEW_LARGE:
787             ApplicationPage_OnViewLargeIcons();
788             break;
789         case ID_VIEW_SMALL:
790             ApplicationPage_OnViewSmallIcons();
791             break;
792         case ID_VIEW_DETAILS:
793             ApplicationPage_OnViewDetails();
794             break;
795         case ID_VIEW_SHOWKERNELTIMES:
796             PerformancePage_OnViewShowKernelTimes();
797             break;
798         case ID_VIEW_CPUHISTORY_ONEGRAPHALL:
799             PerformancePage_OnViewCPUHistoryOneGraphAll();
800             break;
801         case ID_VIEW_CPUHISTORY_ONEGRAPHPERCPU:
802             PerformancePage_OnViewCPUHistoryOneGraphPerCPU();
803             break;
804         case ID_VIEW_UPDATESPEED_HIGH:
805             TaskManager_OnViewUpdateSpeedHigh();
806             break;
807         case ID_VIEW_UPDATESPEED_NORMAL:
808             TaskManager_OnViewUpdateSpeedNormal();
809             break;
810         case ID_VIEW_UPDATESPEED_LOW:
811             TaskManager_OnViewUpdateSpeedLow();
812             break;
813         case ID_VIEW_UPDATESPEED_PAUSED:
814             TaskManager_OnViewUpdateSpeedPaused();
815             break;
816         case ID_VIEW_SELECTCOLUMNS:
817             ProcessPage_OnViewSelectColumns();
818             break;
819         case ID_VIEW_REFRESH:
820             PostMessage(hDlg, WM_TIMER, 0, 0);
821             break;
822         case ID_WINDOWS_TILEHORIZONTALLY:
823             ApplicationPage_OnWindowsTileHorizontally();
824             break;
825         case ID_WINDOWS_TILEVERTICALLY:
826             ApplicationPage_OnWindowsTileVertically();
827             break;
828         case ID_WINDOWS_MINIMIZE:
829             ApplicationPage_OnWindowsMinimize();
830             break;
831         case ID_WINDOWS_MAXIMIZE:
832             ApplicationPage_OnWindowsMaximize();
833             break;
834         case ID_WINDOWS_CASCADE:
835             ApplicationPage_OnWindowsCascade();
836             break;
837         case ID_WINDOWS_BRINGTOFRONT:
838             ApplicationPage_OnWindowsBringToFront();
839             break;
840         case ID_APPLICATION_PAGE_SWITCHTO:
841             ApplicationPage_OnSwitchTo();
842             break;
843         case ID_APPLICATION_PAGE_ENDTASK:
844             ApplicationPage_OnEndTask();
845             break;
846         case ID_APPLICATION_PAGE_GOTOPROCESS:
847             ApplicationPage_OnGotoProcess();
848             break;
849         case ID_PROCESS_PAGE_ENDPROCESS:
850             ProcessPage_OnEndProcess();
851             break;
852         case ID_PROCESS_PAGE_ENDPROCESSTREE:
853             ProcessPage_OnEndProcessTree();
854             break;
855         case ID_PROCESS_PAGE_DEBUG:
856             ProcessPage_OnDebug();
857             break;
858         case ID_PROCESS_PAGE_SETAFFINITY:
859             ProcessPage_OnSetAffinity();
860             break;
861         case ID_PROCESS_PAGE_SETPRIORITY_REALTIME:
862             ProcessPage_OnSetPriorityRealTime();
863             break;
864         case ID_PROCESS_PAGE_SETPRIORITY_HIGH:
865             ProcessPage_OnSetPriorityHigh();
866             break;
867         case ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL:
868             ProcessPage_OnSetPriorityAboveNormal();
869             break;
870         case ID_PROCESS_PAGE_SETPRIORITY_NORMAL:
871             ProcessPage_OnSetPriorityNormal();
872             break;
873         case ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL:
874             ProcessPage_OnSetPriorityBelowNormal();
875             break;
876         case ID_PROCESS_PAGE_SETPRIORITY_LOW:
877             ProcessPage_OnSetPriorityLow();
878             break;
879         case ID_PROCESS_PAGE_DEBUGCHANNELS:
880             ProcessPage_OnDebugChannels();
881             break;
882         case ID_HELP_ABOUT:
883             OnAbout();
884             break;
885         case ID_FILE_EXIT:
886             EndDialog(hDlg, IDOK);
887             break;
888         }     
889         break;
890
891     case WM_ONTRAYICON:
892         switch(lParam)
893         {
894         case WM_RBUTTONDOWN:
895             {
896             POINT pt;
897             BOOL OnTop;
898             HMENU hMenu, hPopupMenu;
899             
900             GetCursorPos(&pt);
901             
902             OnTop = ((GetWindowLong(hMainWnd, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0);
903             
904             hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAY_POPUP));
905             hPopupMenu = GetSubMenu(hMenu, 0);
906             
907             if(IsWindowVisible(hMainWnd))
908             {
909               DeleteMenu(hPopupMenu, ID_RESTORE, MF_BYCOMMAND);
910             }
911             else
912             {
913               SetMenuDefaultItem(hPopupMenu, ID_RESTORE, FALSE);
914             }
915             
916             if(OnTop)
917             {
918               CheckMenuItem(hPopupMenu, ID_OPTIONS_ALWAYSONTOP, MF_BYCOMMAND | MF_CHECKED);
919             }
920             
921             SetForegroundWindow(hMainWnd);
922             TrackPopupMenuEx(hPopupMenu, 0, pt.x, pt.y, hMainWnd, NULL);
923             
924             DestroyMenu(hMenu);
925             break;
926             }
927         case WM_LBUTTONDBLCLK:
928             TaskManager_OnRestoreMainWindow();
929             break;
930         }
931         break;
932
933     case WM_NOTIFY:
934         idctrl = (int)wParam;
935         pnmh = (LPNMHDR)lParam;
936         if ((pnmh->hwndFrom == hTabWnd) &&
937             (pnmh->idFrom == IDC_TAB) &&
938             (pnmh->code == TCN_SELCHANGE))
939         {
940             TaskManager_OnTabWndSelChange();
941         }
942         break;
943
944     case WM_NCPAINT:
945         hdc = GetDC(hDlg);
946         GetClientRect(hDlg, &rc);
947         Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
948         ReleaseDC(hDlg, hdc);
949         break;
950
951     case WM_PAINT:
952         hdc = BeginPaint(hDlg, &ps);
953         GetClientRect(hDlg, &rc);
954         Draw3dRect(hdc, rc.left, rc.top, rc.right, rc.top + 2, GetSysColor(COLOR_3DSHADOW), GetSysColor(COLOR_3DHILIGHT));
955         EndPaint(hDlg, &ps);
956         break;
957
958     case WM_SIZING:
959         /* Make sure the user is sizing the dialog */
960         /* in an acceptable range */
961         pRC = (LPRECT)lParam;
962         if ((wParam == WMSZ_LEFT) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_BOTTOMLEFT)) {
963             /* If the width is too small enlarge it to the minimum */
964             if (nMinimumWidth > (pRC->right - pRC->left))
965                 pRC->left = pRC->right - nMinimumWidth;
966         } else {
967             /* If the width is too small enlarge it to the minimum */
968             if (nMinimumWidth > (pRC->right - pRC->left))
969                 pRC->right = pRC->left + nMinimumWidth;
970         }
971         if ((wParam == WMSZ_TOP) || (wParam == WMSZ_TOPLEFT) || (wParam == WMSZ_TOPRIGHT)) {
972             /* If the height is too small enlarge it to the minimum */
973             if (nMinimumHeight > (pRC->bottom - pRC->top))
974                 pRC->top = pRC->bottom - nMinimumHeight;
975         } else {
976             /* If the height is too small enlarge it to the minimum */
977             if (nMinimumHeight > (pRC->bottom - pRC->top))
978                 pRC->bottom = pRC->top + nMinimumHeight;
979         }
980         return TRUE;
981
982     case WM_SIZE:
983         /* Handle the window sizing in it's own function */
984         OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
985         break;
986
987     case WM_MOVE:
988         /* Handle the window moving in it's own function */
989         OnMove(wParam, LOWORD(lParam), HIWORD(lParam));
990         break;
991
992     case WM_DESTROY:
993         ShowWindow(hDlg, SW_HIDE);
994         TrayIcon_ShellRemoveTrayIcon();
995         wp.length = sizeof(WINDOWPLACEMENT);
996         GetWindowPlacement(hDlg, &wp);
997         TaskManagerSettings.Left = wp.rcNormalPosition.left;
998         TaskManagerSettings.Top = wp.rcNormalPosition.top;
999         TaskManagerSettings.Right = wp.rcNormalPosition.right;
1000         TaskManagerSettings.Bottom = wp.rcNormalPosition.bottom;
1001         if (IsZoomed(hDlg) || (wp.flags & WPF_RESTORETOMAXIMIZED))
1002             TaskManagerSettings.Maximized = TRUE;
1003         else
1004             TaskManagerSettings.Maximized = FALSE;
1005         return DefWindowProc(hDlg, message, wParam, lParam);
1006         
1007     case WM_TIMER:
1008         /* Refresh the performance data */
1009         PerfDataRefresh();
1010         RefreshApplicationPage();
1011         RefreshProcessPage();
1012         RefreshPerformancePage();
1013         TrayIcon_ShellUpdateTrayIcon();
1014         break;
1015
1016     case WM_ENTERMENULOOP:
1017         TaskManager_OnEnterMenuLoop(hDlg);
1018         break;
1019     case WM_EXITMENULOOP:
1020         TaskManager_OnExitMenuLoop(hDlg);
1021         break;
1022     case WM_MENUSELECT:
1023         TaskManager_OnMenuSelect(hDlg, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
1024         break;
1025     }
1026
1027     return 0;
1028 }
1029
1030 int APIENTRY WinMain(HINSTANCE hInstance,
1031                      HINSTANCE hPrevInstance,
1032                      LPSTR     lpCmdLine,
1033                      int       nCmdShow)
1034 {
1035     HANDLE hProcess;
1036     HANDLE hToken; 
1037     TOKEN_PRIVILEGES tkp; 
1038
1039     /* Initialize global variables */
1040     hInst = hInstance;
1041
1042     /* Change our priority class to HIGH */
1043     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1044     SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
1045     CloseHandle(hProcess);
1046
1047     /* Now let's get the SE_DEBUG_NAME privilege
1048      * so that we can debug processes 
1049      */
1050
1051     /* Get a token for this process.  */
1052     if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
1053         /* Get the LUID for the debug privilege.  */
1054         LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid); 
1055
1056         tkp.PrivilegeCount = 1;  /* one privilege to set */
1057         tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
1058
1059         /* Get the debug privilege for this process. */
1060         AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); 
1061     }
1062
1063     /* Load our settings from the registry */
1064     LoadSettings();
1065
1066     /* Initialize perf data */
1067     if (!PerfDataInitialize()) {
1068         return -1;
1069     }
1070
1071     DialogBox(hInst, (LPCTSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc);
1072  
1073     /* Save our settings to the registry */
1074     SaveSettings();
1075     PerfDataUninitialize();
1076     return 0;
1077 }