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