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