xref: /reactos/base/applications/taskmgr/column.c (revision 4561998a)
1 /*
2  *  ReactOS Task Manager
3  *
4  *  column.c
5  *
6  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7  *                2005         Klemens Friedl <frik85@reactos.at>
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 Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23 
24 #include "precomp.h"
25 
26 UINT    ColumnDataHints[COLUMN_NMAX];
27 
28 #define DECLARE_COLUMN_PRESET(_name, _size, _state) \
29     { IDS_TAB_##_name, IDC_##_name, _size, _state },
30 
31 const PresetColumnEntry ColumnPresets[COLUMN_NMAX] = {
32     DECLARE_COLUMN_PRESET(IMAGENAME,        105,  TRUE)
33     DECLARE_COLUMN_PRESET(PID,               50,  TRUE)
34     DECLARE_COLUMN_PRESET(USERNAME,         107, FALSE)
35     DECLARE_COLUMN_PRESET(SESSIONID,         70, FALSE)
36     DECLARE_COLUMN_PRESET(CPUUSAGE,          35,  TRUE)
37     DECLARE_COLUMN_PRESET(CPUTIME,           70,  TRUE)
38     DECLARE_COLUMN_PRESET(MEMORYUSAGE,       70,  TRUE)
39     DECLARE_COLUMN_PRESET(PEAKMEMORYUSAGE,  100, FALSE)
40     DECLARE_COLUMN_PRESET(MEMORYUSAGEDELTA,  70, FALSE)
41     DECLARE_COLUMN_PRESET(PAGEFAULTS,        70, FALSE)
42     DECLARE_COLUMN_PRESET(PAGEFAULTSDELTA,   70, FALSE)
43     DECLARE_COLUMN_PRESET(VIRTUALMEMORYSIZE, 70, FALSE)
44     DECLARE_COLUMN_PRESET(PAGEDPOOL,         70, FALSE)
45     DECLARE_COLUMN_PRESET(NONPAGEDPOOL,      70, FALSE)
46     DECLARE_COLUMN_PRESET(BASEPRIORITY,      60, FALSE)
47     DECLARE_COLUMN_PRESET(HANDLECOUNT,       60, FALSE)
48     DECLARE_COLUMN_PRESET(THREADCOUNT,       60, FALSE)
49     DECLARE_COLUMN_PRESET(USEROBJECTS,       60, FALSE)
50     DECLARE_COLUMN_PRESET(GDIOBJECTS,        60, FALSE)
51     DECLARE_COLUMN_PRESET(IOREADS,           70, FALSE)
52     DECLARE_COLUMN_PRESET(IOWRITES,          70, FALSE)
53     DECLARE_COLUMN_PRESET(IOOTHER,           70, FALSE)
54     DECLARE_COLUMN_PRESET(IOREADBYTES,       70, FALSE)
55     DECLARE_COLUMN_PRESET(IOWRITEBYTES,      70, FALSE)
56     DECLARE_COLUMN_PRESET(IOOTHERBYTES,      70, FALSE)
57     DECLARE_COLUMN_PRESET(COMMANDLINE,      450, FALSE)
58 };
59 
60 static int          InsertColumn(int nCol, LPCWSTR lpszColumnHeading, int nFormat, int nWidth, int nSubItem);
61 INT_PTR CALLBACK    ColumnsDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
62 
63 void AddColumns(void)
64 {
65     LRESULT       size;
66     WCHAR         szTemp[256];
67     unsigned int  n;
68 
69     for (n=0; n<COLUMN_NMAX; n++) {
70         if (TaskManagerSettings.Columns[n]) {
71             LoadStringW(hInst, ColumnPresets[n].dwIdsName, szTemp, sizeof(szTemp)/sizeof(WCHAR));
72             InsertColumn(n, szTemp, LVCFMT_LEFT, TaskManagerSettings.ColumnSizeArray[n], -1);
73         }
74     }
75 
76     size = SendMessageW(hProcessPageHeaderCtrl, HDM_GETITEMCOUNT, 0, 0);
77     SendMessageW(hProcessPageHeaderCtrl, HDM_SETORDERARRAY, (WPARAM) size, (LPARAM) &TaskManagerSettings.ColumnOrderArray);
78 
79     UpdateColumnDataHints();
80 }
81 
82 static int InsertColumn(int nCol, LPCWSTR lpszColumnHeading, int nFormat, int nWidth, int nSubItem)
83 {
84     LVCOLUMN  column;
85 
86     column.mask = LVCF_TEXT|LVCF_FMT;
87     column.pszText = (LPWSTR)lpszColumnHeading;
88     column.fmt = nFormat;
89 
90     if (nWidth != -1)
91     {
92         column.mask |= LVCF_WIDTH;
93         column.cx = nWidth;
94     }
95 
96     if (nSubItem != -1)
97     {
98         column.mask |= LVCF_SUBITEM;
99         column.iSubItem = nSubItem;
100     }
101 
102     return ListView_InsertColumn(hProcessPageListCtrl, nCol, &column);
103 }
104 
105 void SaveColumnSettings(void)
106 {
107     HDITEM        hditem;
108     int           i, n;
109     WCHAR         text[260];
110     WCHAR         szTemp[256];
111     LRESULT       size;
112 
113     /* Reset column data */
114     for (i=0; i<COLUMN_NMAX; i++) {
115         TaskManagerSettings.ColumnOrderArray[i] = i;
116         TaskManagerSettings.Columns[i] = FALSE;
117         TaskManagerSettings.ColumnSizeArray[i] = ColumnPresets[i].size;
118     }
119 
120     /* Get header order */
121     size = SendMessageW(hProcessPageHeaderCtrl, HDM_GETITEMCOUNT, 0, 0);
122     SendMessageW(hProcessPageHeaderCtrl, HDM_GETORDERARRAY, (WPARAM) size, (LPARAM) &TaskManagerSettings.ColumnOrderArray);
123 
124     /* Get visible columns */
125     for (i = 0; i < SendMessageW(hProcessPageHeaderCtrl, HDM_GETITEMCOUNT, 0, 0); i++) {
126         memset(&hditem, 0, sizeof(HDITEM));
127 
128         hditem.mask = HDI_TEXT|HDI_WIDTH;
129         hditem.pszText = text;
130         hditem.cchTextMax = 260;
131 
132         SendMessageW(hProcessPageHeaderCtrl, HDM_GETITEM, i, (LPARAM) &hditem);
133 
134         for (n = 0; n < COLUMN_NMAX; n++) {
135             LoadStringW(hInst, ColumnPresets[n].dwIdsName, szTemp, sizeof(szTemp)/sizeof(WCHAR));
136             if (_wcsicmp(text, szTemp) == 0)
137             {
138                 TaskManagerSettings.Columns[n] = TRUE;
139                 TaskManagerSettings.ColumnSizeArray[n] = hditem.cxy;
140             }
141         }
142     }
143 }
144 
145 void ProcessPage_OnViewSelectColumns(void)
146 {
147     int  i;
148 
149     if (DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_COLUMNS_DIALOG), hMainWnd, ColumnsDialogWndProc) == IDOK)
150     {
151         for (i=Header_GetItemCount(hProcessPageHeaderCtrl)-1; i>=0; i--)
152         {
153             (void)ListView_DeleteColumn(hProcessPageListCtrl, i);
154         }
155 
156         for (i=0; i<COLUMN_NMAX; i++) {
157             TaskManagerSettings.ColumnOrderArray[i] = i;
158             TaskManagerSettings.ColumnSizeArray[i] = ColumnPresets[i].size;
159         }
160 
161         AddColumns();
162     }
163 }
164 
165 INT_PTR CALLBACK
166 ColumnsDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
167 {
168     unsigned int  i;
169 
170     switch (message)
171     {
172     case WM_INITDIALOG:
173 
174         for (i=0; i<COLUMN_NMAX; i++) {
175             if (TaskManagerSettings.Columns[i])
176                 CheckDlgButton(hDlg, ColumnPresets[i].dwIdcCtrl, BST_CHECKED);
177         }
178         return TRUE;
179 
180     case WM_COMMAND:
181 
182         if (LOWORD(wParam) == IDCANCEL)
183         {
184             EndDialog(hDlg, LOWORD(wParam));
185             return TRUE;
186         }
187 
188         if (LOWORD(wParam) == IDOK)
189         {
190             for (i=0; i<COLUMN_NMAX; i++)
191                 TaskManagerSettings.Columns[i] = (BOOL)IsDlgButtonChecked(hDlg, ColumnPresets[i].dwIdcCtrl);
192 
193             EndDialog(hDlg, LOWORD(wParam));
194             return TRUE;
195         }
196 
197         break;
198     }
199 
200     return 0;
201 }
202 
203 void UpdateColumnDataHints(void)
204 {
205     HDITEM        hditem;
206     WCHAR         text[260];
207     ULONG         Index;
208     ULONG         uItems;
209     WCHAR         szTemp[256];
210     UINT          i;
211 
212     uItems = min(SendMessageW(hProcessPageHeaderCtrl, HDM_GETITEMCOUNT, 0, 0), COLUMN_NMAX);
213 
214     for (Index=0; Index<uItems; Index++)
215     {
216         memset(&hditem, 0, sizeof(HDITEM));
217 
218         hditem.mask = HDI_TEXT;
219         hditem.pszText = text;
220         hditem.cchTextMax = 260;
221 
222         SendMessageW(hProcessPageHeaderCtrl, HDM_GETITEM, Index, (LPARAM) &hditem);
223 
224         for (i=0; i<COLUMN_NMAX; i++) {
225             LoadStringW(hInst, ColumnPresets[i].dwIdsName, szTemp, sizeof(szTemp)/sizeof(WCHAR));
226             if (_wcsicmp(text, szTemp) == 0)
227                 ColumnDataHints[Index] = i;
228         }
229     }
230 }
231