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