55aa961d6c8e2eb9734864e1b41090dbeec93a79
[reactos.git] / reactos / subsys / system / servman / servman.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsys/system/servman/servman.c
5 * PURPOSE: Main window message handler
6 * COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "servman.h"
11
12 const TCHAR ClassName[] = _T("ServiceManager");
13
14 HINSTANCE hInstance;
15 HWND hMainWnd;
16 HWND hListView;
17 HWND hStatus;
18 HWND hTool;
19 HMENU hShortcutMenu;
20 INT SelectedItem;
21
22
23 INT GetSelectedItem(VOID)
24 {
25 return SelectedItem;
26 }
27
28
29 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
30 {
31 switch(msg)
32 {
33 case WM_CREATE:
34 {
35 TBADDBITMAP tbab;
36 INT iImageOffset;
37 INT statwidths[] = {110, -1}; /* widths of status bar */
38 TCHAR szTemp[256];
39 LVCOLUMN lvc = { 0 };
40
41 /* Toolbar buttons */
42 TBBUTTON tbb [NUM_BUTTONS] =
43 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
44 {TBICON_PROP, ID_PROP, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0}, /* properties */
45 {TBICON_REFRESH, ID_REFRESH, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0}, /* refresh */
46 {TBICON_EXPORT, ID_EXPORT, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0}, /* export */
47
48 /* Note: First item for a seperator is its width in pixels */
49 {15, 0, TBSTATE_ENABLED, BTNS_SEP, {0}, 0, 0}, /* separator */
50
51 {TBICON_NEW, ID_NEW, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* create */
52
53 {15, 0, TBSTATE_ENABLED, BTNS_SEP, {0}, 0, 0}, /* separator */
54
55 {TBICON_START, ID_START, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* start */
56 {TBICON_STOP, ID_STOP, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* stop */
57 {TBICON_PAUSE, ID_PAUSE, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* pause */
58 {TBICON_RESTART, ID_RESTART, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* restart */
59
60 {15, 0, TBSTATE_ENABLED, BTNS_SEP, {0}, 0, 0}, /* separator */
61
62 {TBICON_HELP, ID_HELP, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* help */
63 {TBICON_EXIT, ID_EXIT, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }, /* exit */
64
65 };
66
67 /* ======================== Create Toolbar ============================== */
68
69 /* Create Toolbar */
70 hTool = CreateWindowEx(0,
71 TOOLBARCLASSNAME,
72 NULL,
73 WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS,
74 0, 0, 0, 0,
75 hwnd,
76 (HMENU)IDC_TOOLBAR,
77 hInstance,
78 NULL);
79 if(hTool == NULL)
80 MessageBox(hwnd, _T("Could not create tool bar."), _T("Error"), MB_OK | MB_ICONERROR);
81
82 /* Send the TB_BUTTONSTRUCTSIZE message, which is required for backward compatibility */
83 SendMessage(hTool, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
84
85 /* Add custom images */
86 tbab.hInst = hInstance;
87 tbab.nID = IDB_BUTTONS;
88 iImageOffset = (INT)SendMessage(hTool, TB_ADDBITMAP, NUM_BUTTONS, (LPARAM)&tbab);
89 tbb[0].iBitmap += iImageOffset; /* properties */
90 tbb[1].iBitmap += iImageOffset; /* refresh */
91 tbb[2].iBitmap += iImageOffset; /* export */
92 tbb[4].iBitmap += iImageOffset; /* new */
93 tbb[6].iBitmap += iImageOffset; /* start */
94 tbb[7].iBitmap += iImageOffset; /* stop */
95 tbb[8].iBitmap += iImageOffset; /* pause */
96 tbb[9].iBitmap += iImageOffset; /* restart */
97 tbb[11].iBitmap += iImageOffset; /* help */
98 tbb[12].iBitmap += iImageOffset; /* exit */
99
100 /* Add buttons to toolbar */
101 SendMessage(hTool, TB_ADDBUTTONS, NUM_BUTTONS, (LPARAM) &tbb);
102
103 /* Show toolbar */
104 ShowWindow(hTool, SW_SHOWNORMAL);
105
106
107
108 /* ======================== Create List View ============================== */
109
110 hListView = CreateWindowEx(0,
111 WC_LISTVIEW,
112 NULL,
113 WS_CHILD | WS_VISIBLE | LVS_REPORT | WS_BORDER |
114 LBS_NOTIFY | LVS_SORTASCENDING | LBS_NOREDRAW,
115 0, 0, 0, 0, /* sized via WM_SIZE */
116 hwnd,
117 (HMENU) IDC_SERVLIST,
118 hInstance,
119 NULL);
120 if (hListView == NULL)
121 MessageBox(hwnd, _T("Could not create List View."), _T("Error"), MB_OK | MB_ICONERROR);
122
123 ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT |
124 LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP);
125
126 lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH | LVCF_FMT;
127 lvc.fmt = LVCFMT_LEFT;
128
129 /* Add columns to the list-view */
130
131 /* name */
132 lvc.iSubItem = 0;
133 lvc.cx = 150;
134 LoadString(hInstance, IDS_FIRSTCOLUMN, szTemp, 256);
135 lvc.pszText = szTemp;
136 ListView_InsertColumn(hListView, 0, &lvc);
137
138 /* description */
139 lvc.iSubItem = 1;
140 lvc.cx = 240;
141 LoadString(hInstance, IDS_SECONDCOLUMN, szTemp, 256);
142 lvc.pszText = szTemp;
143 ListView_InsertColumn(hListView, 1, &lvc);
144
145 /* status */
146 lvc.iSubItem = 2;
147 lvc.cx = 55;
148 LoadString(hInstance, IDS_THIRDCOLUMN, szTemp, 256);
149 lvc.pszText = szTemp;
150 ListView_InsertColumn(hListView, 2, &lvc);
151
152 /* startup type */
153 lvc.iSubItem = 3;
154 lvc.cx = 80;
155 LoadString(hInstance, IDS_FOURTHCOLUMN, szTemp, 256);
156 lvc.pszText = szTemp;
157 ListView_InsertColumn(hListView, 3, &lvc);
158
159 /* logon as */
160 lvc.iSubItem = 4;
161 lvc.cx = 100;
162 LoadString(hInstance, IDS_FITHCOLUMN, szTemp, 256);
163 lvc.pszText = szTemp;
164 ListView_InsertColumn(hListView, 4, &lvc);
165
166
167
168 /* ======================== Create Status Bar ============================== */
169
170 hStatus = CreateWindowEx(0,
171 STATUSCLASSNAME,
172 NULL,
173 WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
174 0, 0, 0, 0,
175 hwnd,
176 (HMENU)IDC_STATUSBAR,
177 hInstance,
178 NULL);
179 if(hStatus == NULL)
180 MessageBox(hwnd, _T("Could not create status bar."),
181 _T("Error!"), MB_OK | MB_ICONERROR);
182
183 SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
184
185
186 /* ======================== Create Popup Menu ============================== */
187
188 hShortcutMenu = LoadMenu(hInstance, MAKEINTRESOURCE (IDR_POPUP));
189 hShortcutMenu = GetSubMenu(hShortcutMenu, 0);
190
191
192
193
194 /* ================= populate the list view with all services =================== */
195
196 RefreshServiceList();
197
198 }
199 break;
200
201 case WM_SIZE:
202 {
203 RECT rcTool;
204 int iToolHeight;
205
206 RECT rcStatus;
207 int iStatusHeight;
208
209 int lvHeight;
210 RECT rcClient;
211
212 /* Size toolbar and get height */
213 hTool = GetDlgItem(hwnd, IDC_TOOLBAR);
214 SendMessage(hTool, TB_AUTOSIZE, 0, 0);
215
216 GetWindowRect(hTool, &rcTool);
217 iToolHeight = rcTool.bottom - rcTool.top;
218
219 /* Size status bar and get height */
220 hStatus = GetDlgItem(hwnd, IDC_STATUSBAR);
221 SendMessage(hStatus, WM_SIZE, 0, 0);
222
223 GetWindowRect(hStatus, &rcStatus);
224 iStatusHeight = rcStatus.bottom - rcStatus.top;
225
226 /* Calculate remaining height and size list view */
227 GetClientRect(hwnd, &rcClient);
228
229 lvHeight = rcClient.bottom - iToolHeight - iStatusHeight;
230
231 hListView = GetDlgItem(hwnd, IDC_SERVLIST);
232 SetWindowPos(hListView, NULL, 0, iToolHeight, rcClient.right, lvHeight, SWP_NOZORDER);
233 }
234 break;
235
236 case WM_NOTIFY:
237 {
238 NMHDR* nm = (NMHDR*) lParam;
239
240 switch (nm->code)
241 {
242 case NM_DBLCLK:
243 OpenPropSheet(hwnd);
244 break;
245
246 case LVN_ITEMCHANGED:
247 {
248 LPNMLISTVIEW pnmv = (LPNMLISTVIEW) lParam;
249
250 SelectedItem = pnmv->iItem;
251
252 }
253 break;
254
255 case TTN_GETDISPINFO:
256 {
257 LPTOOLTIPTEXT lpttt;
258 UINT idButton;
259
260 lpttt = (LPTOOLTIPTEXT) lParam;
261
262 /* Specify the resource identifier of the descriptive
263 * text for the given button. */
264 idButton = (UINT)lpttt->hdr.idFrom;
265 switch (idButton)
266 {
267 case ID_PROP:
268 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_PROP);
269 break;
270
271 case ID_REFRESH:
272 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_REFRESH);
273 break;
274
275 case ID_EXPORT:
276 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_EXPORT);
277 break;
278
279 case ID_START:
280 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_START);
281 break;
282
283 case ID_STOP:
284 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_STOP);
285 break;
286
287 case ID_PAUSE:
288 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_PAUSE);
289 break;
290
291 case ID_RESTART:
292 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_RESTART);
293 break;
294
295 case ID_NEW:
296 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_NEW);
297 break;
298
299 case ID_HELP:
300 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_HELP);
301 break;
302
303 case ID_EXIT:
304 lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_EXIT);
305 break;
306
307 }
308 }
309 break;
310
311 default:
312 break;
313 }
314 }
315 break;
316
317 case WM_CONTEXTMENU:
318 {
319 int xPos, yPos;
320
321 xPos = GET_X_LPARAM(lParam);
322 yPos = GET_Y_LPARAM(lParam);
323
324 TrackPopupMenuEx(hShortcutMenu, TPM_RIGHTBUTTON,
325 xPos, yPos, hwnd, NULL);
326 }
327 break;
328
329 case WM_COMMAND:
330
331 switch(LOWORD(wParam))
332 {
333 case ID_PROP:
334 OpenPropSheet(hwnd);
335 break;
336
337 case ID_REFRESH:
338 RefreshServiceList();
339 break;
340
341 case ID_EXPORT:
342 break;
343
344 case ID_START:
345 DoStartService();
346 RefreshServiceList();
347 break;
348
349 case ID_STOP:
350 Control(SERVICE_CONTROL_STOP);
351 RefreshServiceList();
352 break;
353
354 case ID_PAUSE:
355 Control(SERVICE_CONTROL_PAUSE);
356 RefreshServiceList();
357 break;
358
359 case ID_RESUME:
360 Control(SERVICE_CONTROL_CONTINUE );
361 RefreshServiceList();
362 break;
363
364 case ID_RESTART:
365 Control(SERVICE_CONTROL_STOP);
366 DoStartService();
367 RefreshServiceList();
368 break;
369
370 case ID_NEW:
371 break;
372
373 case ID_HELP:
374 MessageBox(NULL, _T("Help is not yet implemented\n"),
375 _T("Note!"), MB_OK | MB_ICONINFORMATION);
376 break;
377
378 case ID_EXIT:
379 PostMessage(hwnd, WM_CLOSE, 0, 0);
380 break;
381
382 case ID_VIEW_CUSTOMIZE:
383 break;
384
385 case ID_ABOUT:
386 DialogBox(hInstance,
387 MAKEINTRESOURCE(IDD_ABOUTBOX),
388 hwnd,
389 AboutDialogProc);
390 break;
391
392 }
393 break;
394
395 case WM_CLOSE:
396 FreeMemory(); /* free the service array */
397 DestroyMenu(hShortcutMenu);
398 DestroyWindow(hwnd);
399 break;
400
401 case WM_DESTROY:
402 PostQuitMessage(0);
403 break;
404
405 default:
406 return DefWindowProc(hwnd, msg, wParam, lParam);
407 }
408 return 0;
409 }
410
411 #ifdef _MSC_VER
412 #pragma warning(disable : 4100)
413 #endif
414 int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
415 LPSTR lpCmdLine, int nCmdShow)
416 {
417 WNDCLASSEX wc;
418 MSG Msg;
419 BOOL bRet;
420
421 hInstance = hThisInstance;
422
423 InitCommonControls();
424
425 wc.cbSize = sizeof(WNDCLASSEX);
426 wc.style = 0;
427 wc.lpfnWndProc = WndProc;
428 wc.cbClsExtra = 0;
429 wc.cbWndExtra = 0;
430 wc.hInstance = hInstance;
431 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SM_ICON));
432 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
433 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
434 wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
435 wc.lpszClassName = ClassName;
436 wc.hIconSm = (HICON)LoadImage(hInstance,
437 MAKEINTRESOURCE(IDI_SM_ICON), IMAGE_ICON, 16, 16, 0);
438
439 if(!RegisterClassEx(&wc))
440 {
441 MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"),
442 MB_ICONEXCLAMATION | MB_OK);
443 return 0;
444 }
445
446 hMainWnd = CreateWindowEx(
447 0,
448 ClassName,
449 _T("ReactOS Service Manager"),
450 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
451 CW_USEDEFAULT, CW_USEDEFAULT, 650, 450,
452 NULL, NULL, hInstance, NULL);
453
454 if(hMainWnd == NULL)
455 {
456 MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"),
457 MB_ICONEXCLAMATION | MB_OK);
458 return 0;
459 }
460
461 ShowWindow(hMainWnd, nCmdShow);
462 UpdateWindow(hMainWnd);
463
464 while( (bRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
465 {
466 if (bRet == -1)
467 {
468 /* handle the error and possibly exit */
469 }
470 else
471 {
472 TranslateMessage(&Msg);
473 DispatchMessage(&Msg);
474 }
475 }
476 return (int)Msg.wParam;
477 }
478
479
480