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