[SERVMAN]
[reactos.git] / reactos / base / applications / mscutils / servman / listview.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/listview.c
5 * PURPOSE: service listview manipulation functions
6 * COPYRIGHT: Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "precomp.h"
11
12 typedef struct _COLUMN_LIST
13 {
14 int iSubItem;
15 int cx;
16 UINT idsText;
17 } COLUMN_LIST;
18
19 static const COLUMN_LIST Columns[] =
20 {
21 /* name */
22 { LVNAME, 150, IDS_FIRSTCOLUMN },
23 /* description */
24 { LVDESC, 240, IDS_SECONDCOLUMN },
25 /* status */
26 { LVSTATUS, 55, IDS_THIRDCOLUMN },
27 /* startup type */
28 { LVSTARTUP, 80, IDS_FOURTHCOLUMN },
29 /* logon as */
30 { LVLOGONAS, 100, IDS_FITHCOLUMN },
31 };
32
33 VOID
34 SetListViewStyle(HWND hListView,
35 DWORD View)
36 {
37 DWORD Style = GetWindowLongPtr(hListView, GWL_STYLE);
38
39 if ((Style & LVS_TYPEMASK) != View)
40 {
41 SetWindowLongPtr(hListView,
42 GWL_STYLE,
43 (Style & ~LVS_TYPEMASK) | View);
44 }
45 }
46
47 VOID
48 ListViewSelectionChanged(PMAIN_WND_INFO Info,
49 LPNMLISTVIEW pnmv)
50 {
51 HMENU hMainMenu;
52
53 /* get handle to menu */
54 hMainMenu = GetMenu(Info->hMainWnd);
55
56 /* activate properties menu item, if not already */
57 if (GetMenuState(hMainMenu,
58 ID_PROP,
59 MF_BYCOMMAND) != MF_ENABLED)
60 {
61 EnableMenuItem(hMainMenu,
62 ID_PROP,
63 MF_ENABLED);
64 EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0),
65 ID_PROP,
66 MF_ENABLED);
67 SetMenuDefaultItem(GetSubMenu(Info->hShortcutMenu, 0),
68 ID_PROP,
69 MF_BYCOMMAND);
70 }
71
72 /* activate delete menu item, if not already */
73 if (GetMenuState(hMainMenu,
74 ID_DELETE,
75 MF_BYCOMMAND) != MF_ENABLED)
76 {
77 EnableMenuItem(hMainMenu,
78 ID_DELETE,
79 MF_ENABLED);
80 EnableMenuItem(GetSubMenu(Info->hShortcutMenu, 0),
81 ID_DELETE,
82 MF_ENABLED);
83 }
84
85 /* set selected service */
86 Info->SelectedItem = pnmv->iItem;
87
88 /* get pointer to selected service */
89 Info->pCurrentService = GetSelectedService(Info);
90
91 /* set current selected service in the status bar */
92 SendMessage(Info->hStatus,
93 SB_SETTEXT,
94 1,
95 (LPARAM)Info->pCurrentService->lpDisplayName);
96
97 /* show the properties button */
98 SendMessage(Info->hTool,
99 TB_SETSTATE,
100 ID_PROP,
101 (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
102 }
103
104 VOID
105 ChangeListViewText(PMAIN_WND_INFO Info,
106 ENUM_SERVICE_STATUS_PROCESS* pService,
107 UINT Column)
108 {
109 LVFINDINFO lvfi;
110 LVITEM lvItem;
111 INT index;
112
113 lvfi.flags = LVFI_PARAM;
114 lvfi.lParam = (LPARAM)pService;
115 index = ListView_FindItem(Info->hListView,
116 -1,
117 &lvfi);
118 if (index != -1)
119 {
120 lvItem.iItem = index;
121 lvItem.iSubItem = Column;
122
123 switch (Column)
124 {
125 case LVNAME:
126 {
127 LPQUERY_SERVICE_CONFIG lpServiceConfig;
128
129 lpServiceConfig = GetServiceConfig(pService->lpServiceName);
130 if (lpServiceConfig)
131 {
132 lvItem.pszText = lpServiceConfig->lpDisplayName;
133 SendMessage(Info->hListView,
134 LVM_SETITEMTEXT,
135 lvItem.iItem,
136 (LPARAM)&lvItem);
137
138 HeapFree(ProcessHeap,
139 0,
140 lpServiceConfig);
141 }
142 }
143 break;
144
145 case LVDESC:
146 {
147 LPWSTR lpDescription;
148
149 lpDescription = GetServiceDescription(pService->lpServiceName);
150
151 lvItem.pszText = lpDescription;
152 SendMessage(Info->hListView,
153 LVM_SETITEMTEXTW,
154 lvItem.iItem,
155 (LPARAM) &lvItem);
156
157 HeapFree(ProcessHeap,
158 0,
159 lpDescription);
160 }
161 break;
162
163 case LVSTATUS:
164 {
165 WCHAR szStatus[64];
166
167 if (pService->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING)
168 {
169 LoadStringW(hInstance,
170 IDS_SERVICES_STARTED,
171 szStatus,
172 sizeof(szStatus) / sizeof(WCHAR));
173 }
174 else
175 {
176 szStatus[0] = 0;
177 }
178
179 lvItem.pszText = szStatus;
180 SendMessageW(Info->hListView,
181 LVM_SETITEMTEXT,
182 lvItem.iItem,
183 (LPARAM) &lvItem);
184 }
185 break;
186
187 case LVSTARTUP:
188 {
189 LPQUERY_SERVICE_CONFIGW lpServiceConfig;
190 LPWSTR lpStartup = NULL;
191 DWORD StringId = 0;
192
193 lpServiceConfig = GetServiceConfig(pService->lpServiceName);
194
195 if (lpServiceConfig)
196 {
197 switch (lpServiceConfig->dwStartType)
198 {
199 case 2: StringId = IDS_SERVICES_AUTO; break;
200 case 3: StringId = IDS_SERVICES_MAN; break;
201 case 4: StringId = IDS_SERVICES_DIS; break;
202 }
203 }
204
205 if (StringId)
206 AllocAndLoadString(&lpStartup,
207 hInstance,
208 StringId);
209
210 lvItem.pszText = lpStartup;
211 SendMessageW(Info->hListView,
212 LVM_SETITEMTEXTW,
213 lvItem.iItem,
214 (LPARAM)&lvItem);
215
216 LocalFree(lpStartup);
217 HeapFree(ProcessHeap,
218 0,
219 lpServiceConfig);
220 }
221 break;
222
223 case LVLOGONAS:
224 {
225 LPQUERY_SERVICE_CONFIG lpServiceConfig;
226
227 lpServiceConfig = GetServiceConfig(pService->lpServiceName);
228 if (lpServiceConfig)
229 {
230 lvItem.pszText = lpServiceConfig->lpServiceStartName;
231 SendMessageW(Info->hListView,
232 LVM_SETITEMTEXT,
233 lvItem.iItem,
234 (LPARAM)&lvItem);
235
236 HeapFree(ProcessHeap,
237 0,
238 lpServiceConfig);
239 }
240 }
241 break;
242 }
243 }
244 }
245
246 BOOL
247 RefreshServiceList(PMAIN_WND_INFO Info)
248 {
249 ENUM_SERVICE_STATUS_PROCESS *pService;
250 LVITEMW lvItem;
251 DWORD NumServices;
252 DWORD Index;
253
254 SendMessage (Info->hListView,
255 WM_SETREDRAW,
256 FALSE,
257 0);
258
259 (void)ListView_DeleteAllItems(Info->hListView);
260
261 if (GetServiceList(Info, &NumServices))
262 {
263 for (Index = 0; Index < NumServices; Index++)
264 {
265 INT i;
266
267 pService = &Info->pAllServices[Index];
268
269 /* set the display name */
270 ZeroMemory(&lvItem, sizeof(LVITEMW));
271 lvItem.mask = LVIF_TEXT | LVIF_PARAM;
272 lvItem.pszText = pService->lpDisplayName;
273
274 /* Add the service pointer */
275 lvItem.lParam = (LPARAM)pService;
276
277 /* add it to the listview */
278 lvItem.iItem = ListView_InsertItem(Info->hListView, &lvItem);
279
280 /* fill out all the column data */
281 for (i = LVDESC; i <= LVLOGONAS; i++)
282 {
283 ChangeListViewText(Info, pService, i);
284 }
285 }
286
287 UpdateServiceCount(Info);
288 }
289
290 /* turn redraw flag on. */
291 SendMessageW(Info->hListView,
292 WM_SETREDRAW,
293 TRUE,
294 0);
295
296 return TRUE;
297 }
298
299 static VOID
300 InitListViewImage(PMAIN_WND_INFO Info)
301 {
302 HICON hSmIconItem, hLgIconItem;
303 HIMAGELIST hSmall, hLarge;
304
305 hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
306 GetSystemMetrics(SM_CYSMICON),
307 ILC_MASK | ILC_COLOR32,
308 1,
309 1);
310 if (hSmall)
311 {
312 hSmIconItem = LoadImageW(hInstance,
313 MAKEINTRESOURCE(IDI_SM_ICON),
314 IMAGE_ICON,
315 16,
316 16,
317 0);
318 if (hSmIconItem)
319 {
320 ImageList_AddIcon(hSmall,
321 hSmIconItem);
322 (void)ListView_SetImageList(Info->hListView,
323 hSmall,
324 LVSIL_SMALL);
325
326 DestroyIcon(hSmIconItem);
327 }
328 }
329
330 hLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
331 GetSystemMetrics(SM_CYICON),
332 ILC_MASK | ILC_COLOR32,
333 1,
334 1);
335 if (hLarge)
336 {
337 hLgIconItem = LoadImageW(hInstance,
338 MAKEINTRESOURCE(IDI_SM_ICON),
339 IMAGE_ICON,
340 32,
341 32,
342 0);
343 if (hLgIconItem)
344 {
345 ImageList_AddIcon(hLarge,
346 hLgIconItem);
347 (void)ListView_SetImageList(Info->hListView,
348 hLarge,
349 LVSIL_NORMAL);
350 DestroyIcon(hLgIconItem);
351 }
352 }
353 }
354
355 BOOL
356 CreateListView(PMAIN_WND_INFO Info)
357 {
358 LVCOLUMNW lvc = { 0 };
359 WCHAR szTemp[256];
360 HDITEM hdi;
361 int i, n;
362
363 Info->hListView = CreateWindowExW(WS_EX_CLIENTEDGE,
364 WC_LISTVIEWW,
365 NULL,
366 WS_CHILD | WS_VISIBLE | LVS_REPORT | WS_BORDER |
367 LBS_NOTIFY | LVS_SORTASCENDING | LBS_NOREDRAW,
368 0, 0, 0, 0,
369 Info->hMainWnd,
370 (HMENU) IDC_SERVLIST,
371 hInstance,
372 NULL);
373 if (Info->hListView == NULL)
374 {
375 MessageBoxW(Info->hMainWnd,
376 L"Could not create List View.",
377 L"Error",
378 MB_OK | MB_ICONERROR);
379 return FALSE;
380 }
381
382 Info->hHeader = ListView_GetHeader(Info->hListView);
383
384 (void)ListView_SetExtendedListViewStyle(Info->hListView,
385 LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);/*LVS_EX_GRIDLINES |*/
386
387 lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;
388 lvc.fmt = LVCFMT_LEFT;
389 lvc.pszText = szTemp;
390
391 /* Add columns to the list-view */
392 for (n = 0; n < sizeof(Columns) / sizeof(Columns[0]); n++)
393 {
394 lvc.iSubItem = Columns[n].iSubItem;
395 lvc.cx = Columns[n].cx;
396
397 LoadStringW(hInstance,
398 Columns[n].idsText,
399 szTemp,
400 sizeof(szTemp) / sizeof(szTemp[0]));
401
402 i = ListView_InsertColumn(Info->hListView, Columns[n].iSubItem, &lvc);
403
404 hdi.mask = HDI_LPARAM;
405 hdi.lParam = ORD_ASCENDING;
406 (void)Header_SetItem(Info->hHeader, i, &hdi);
407 }
408
409 InitListViewImage(Info);
410
411 /* check the details view menu item */
412 CheckMenuRadioItem(GetMenu(Info->hMainWnd),
413 ID_VIEW_LARGE,
414 ID_VIEW_DETAILS,
415 ID_VIEW_DETAILS,
416 MF_BYCOMMAND);
417
418 return TRUE;
419 }