Sync with trunk r63743.
[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 LocalFree(lpStartup);
196 HeapFree(ProcessHeap,
197 0,
198 lpServiceConfig);
199 }
200 break;
201
202 case LVLOGONAS:
203 {
204 LPQUERY_SERVICE_CONFIG lpServiceConfig;
205
206 lpServiceConfig = GetServiceConfig(pService->lpServiceName);
207 if (lpServiceConfig)
208 {
209 lvItem.pszText = lpServiceConfig->lpServiceStartName;
210 SendMessage(Info->hListView,
211 LVM_SETITEMTEXT,
212 lvItem.iItem,
213 (LPARAM)&lvItem);
214
215 HeapFree(ProcessHeap,
216 0,
217 lpServiceConfig);
218 }
219 }
220 break;
221 }
222 }
223 }
224
225 BOOL
226 RefreshServiceList(PMAIN_WND_INFO Info)
227 {
228 ENUM_SERVICE_STATUS_PROCESS *pService;
229 LVITEM lvItem;
230 DWORD NumServices;
231 DWORD Index;
232
233 SendMessage (Info->hListView,
234 WM_SETREDRAW,
235 FALSE,
236 0);
237
238 (void)ListView_DeleteAllItems(Info->hListView);
239
240 if (GetServiceList(Info, &NumServices))
241 {
242 for (Index = 0; Index < NumServices; Index++)
243 {
244 INT i;
245
246 pService = &Info->pAllServices[Index];
247
248 /* set the display name */
249 ZeroMemory(&lvItem, sizeof(LVITEM));
250 lvItem.mask = LVIF_TEXT | LVIF_PARAM;
251 lvItem.pszText = pService->lpDisplayName;
252
253 /* Add the service pointer */
254 lvItem.lParam = (LPARAM)pService;
255
256 /* add it to the listview */
257 lvItem.iItem = ListView_InsertItem(Info->hListView, &lvItem);
258
259 /* fill out all the column data */
260 for (i = LVDESC; i <= LVLOGONAS; i++)
261 {
262 ChangeListViewText(Info, pService, i);
263 }
264 }
265
266 UpdateServiceCount(Info);
267 }
268
269 /* turn redraw flag on. */
270 SendMessage (Info->hListView,
271 WM_SETREDRAW,
272 TRUE,
273 0);
274
275 return TRUE;
276 }
277
278 static VOID
279 InitListViewImage(PMAIN_WND_INFO Info)
280 {
281 HICON hSmIconItem, hLgIconItem;
282 HIMAGELIST hSmall, hLarge;
283
284 hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
285 GetSystemMetrics(SM_CYSMICON),
286 ILC_MASK | ILC_COLOR32,
287 1,
288 1);
289 if (hSmall)
290 {
291 hSmIconItem = LoadImage(hInstance,
292 MAKEINTRESOURCE(IDI_SM_ICON),
293 IMAGE_ICON,
294 16,
295 16,
296 0);
297 if (hSmIconItem)
298 {
299 ImageList_AddIcon(hSmall,
300 hSmIconItem);
301 (void)ListView_SetImageList(Info->hListView,
302 hSmall,
303 LVSIL_SMALL);
304
305 DestroyIcon(hSmIconItem);
306 }
307 }
308
309 hLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
310 GetSystemMetrics(SM_CYICON),
311 ILC_MASK | ILC_COLOR32,
312 1,
313 1);
314 if (hLarge)
315 {
316 hLgIconItem = LoadImage(hInstance,
317 MAKEINTRESOURCE(IDI_SM_ICON),
318 IMAGE_ICON,
319 32,
320 32,
321 0);
322 if (hLgIconItem)
323 {
324 ImageList_AddIcon(hLarge,
325 hLgIconItem);
326 (void)ListView_SetImageList(Info->hListView,
327 hLarge,
328 LVSIL_NORMAL);
329 DestroyIcon(hLgIconItem);
330 }
331 }
332 }
333
334 BOOL
335 CreateListView(PMAIN_WND_INFO Info)
336 {
337 LVCOLUMN lvc = { 0 };
338 TCHAR szTemp[256];
339
340 Info->hListView = CreateWindowEx(WS_EX_CLIENTEDGE,
341 WC_LISTVIEW,
342 NULL,
343 WS_CHILD | WS_VISIBLE | LVS_REPORT | WS_BORDER |
344 LBS_NOTIFY | LVS_SORTASCENDING | LBS_NOREDRAW,
345 0, 0, 0, 0,
346 Info->hMainWnd,
347 (HMENU) IDC_SERVLIST,
348 hInstance,
349 NULL);
350 if (Info->hListView == NULL)
351 {
352 MessageBox(Info->hMainWnd,
353 _T("Could not create List View."),
354 _T("Error"),
355 MB_OK | MB_ICONERROR);
356 return FALSE;
357 }
358
359 (void)ListView_SetExtendedListViewStyle(Info->hListView,
360 LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);/*LVS_EX_GRIDLINES |*/
361
362 lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;
363 lvc.fmt = LVCFMT_LEFT;
364
365 /* Add columns to the list-view */
366 /* name */
367 lvc.iSubItem = LVNAME;
368 lvc.cx = 150;
369 LoadString(hInstance,
370 IDS_FIRSTCOLUMN,
371 szTemp,
372 sizeof(szTemp) / sizeof(TCHAR));
373 lvc.pszText = szTemp;
374 (void)ListView_InsertColumn(Info->hListView,
375 0,
376 &lvc);
377
378 /* description */
379 lvc.iSubItem = LVDESC;
380 lvc.cx = 240;
381 LoadString(hInstance,
382 IDS_SECONDCOLUMN,
383 szTemp,
384 sizeof(szTemp) / sizeof(TCHAR));
385 lvc.pszText = szTemp;
386 (void)ListView_InsertColumn(Info->hListView,
387 1,
388 &lvc);
389
390 /* status */
391 lvc.iSubItem = LVSTATUS;
392 lvc.cx = 55;
393 LoadString(hInstance,
394 IDS_THIRDCOLUMN,
395 szTemp,
396 sizeof(szTemp) / sizeof(TCHAR));
397 lvc.pszText = szTemp;
398 (void)ListView_InsertColumn(Info->hListView,
399 2,
400 &lvc);
401
402 /* startup type */
403 lvc.iSubItem = LVSTARTUP;
404 lvc.cx = 80;
405 LoadString(hInstance,
406 IDS_FOURTHCOLUMN,
407 szTemp,
408 sizeof(szTemp) / sizeof(TCHAR));
409 lvc.pszText = szTemp;
410 (void)ListView_InsertColumn(Info->hListView,
411 3,
412 &lvc);
413
414 /* logon as */
415 lvc.iSubItem = LVLOGONAS;
416 lvc.cx = 100;
417 LoadString(hInstance,
418 IDS_FITHCOLUMN,
419 szTemp,
420 sizeof(szTemp) / sizeof(TCHAR));
421 lvc.pszText = szTemp;
422 (void)ListView_InsertColumn(Info->hListView,
423 4,
424 &lvc);
425
426 InitListViewImage(Info);
427
428 /* check the details view menu item */
429 CheckMenuRadioItem(GetMenu(Info->hMainWnd),
430 ID_VIEW_LARGE,
431 ID_VIEW_DETAILS,
432 ID_VIEW_DETAILS,
433 MF_BYCOMMAND);
434
435 return TRUE;
436 }