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