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