PackageManager: Ported GUI to C
[reactos.git] / rosapps / packmgr / gui / main.c
1 ////////////////////////////////////////////////////////
2 //
3 // main.cpp
4 //
5 // Implementation of the Package Manager GUI
6 //
7 //
8 // Maarten Bosma, 09.01.2004
9 // maarten.paul@bosma.de
10 //
11 ////////////////////////////////////////////////////////////////////
12
13 #include "main.h"
14
15
16 // Application's Entry Point
17 int WINAPI WinMain (HINSTANCE hinst, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
18 {
19 HWND hwnd;
20 MSG msg;
21 WNDCLASSEX wc = {0};
22
23 // Window creation
24 wc.cbSize = sizeof(WNDCLASSEX);
25 wc.lpszClassName = L"pgkmgr";
26 wc.style = CS_HREDRAW | CS_VREDRAW;
27 wc.lpfnWndProc = (WNDPROC)WndProc;
28 wc.hInstance = hinst;
29 wc.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_MAIN));
30 wc.hbrBackground = (HBRUSH)(COLOR_SCROLLBAR);
31
32 RegisterClassEx(&wc);
33
34 hwnd = CreateWindow(L"pgkmgr",
35 L"ReactOS - Package Manager v0.3",
36 WS_OVERLAPPEDWINDOW,
37 CW_USEDEFAULT,
38 CW_USEDEFAULT,
39 500, 600,
40 NULL, NULL,
41 hinst,
42 NULL);
43
44
45 // Toolbar creation
46 InitCommonControls();
47
48 hTBar = CreateToolbarEx(hwnd, WS_CHILD|WS_VISIBLE|TBSTYLE_FLAT, 0, 8, hinst, IDB_TOOLBAR,
49 Buttons, sizeof(Buttons)/sizeof(TBBUTTON), TBSIZE, TBSIZE, TBSIZE, TBSIZE, sizeof(TBBUTTON));
50
51 // Show the windows
52 ShowWindow(hwnd, SW_SHOW);
53 UpdateWindow(hwnd);
54
55 // Load the tree
56 int error = PML_LoadTree(&tree, "tree.xml", AddItem);
57
58 if(error)
59 {
60 MessageBox(0,PML_TransError(error),0,0);
61 return 0;
62 }
63
64 // Read the help
65 Help();
66
67 // Start getting messages
68 while(GetMessage(&msg,NULL,0,0))
69 {
70 if(!TranslateAccelerator(hwnd, hHotKeys, &msg))
71 {
72 TranslateMessage(&msg);
73 DispatchMessage(&msg);
74 }
75 }
76
77 // Close our handle
78 PML_CloseTree (tree);
79
80 return 0;
81 }
82
83 // Add a item to our tree
84 int AddItem (int id, const char* name, int parent, int icon)
85 {
86 TV_INSERTSTRUCT tvins;
87
88 tvins.item.lParam = (UINT)id;
89 tvins.item.mask = TVIF_TEXT|TVIF_PARAM;
90 tvins.item.pszText = (WCHAR*)name; //that is ok
91 tvins.item.cchTextMax = strlen(name);
92 tvins.hInsertAfter = TVI_LAST;
93
94 if(icon)
95 {
96 tvins.item.iImage = icon;
97 tvins.item.iSelectedImage = icon;
98 tvins.item.mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
99 }
100
101 if (parent==0)
102 tvins.hParent = TVI_ROOT;
103 else
104 tvins.hParent = nodes[parent];
105
106 nodes[id] = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMA, 0, (LPARAM)&tvins);
107
108 return 0;
109 }
110
111 // Load the Help from file and display it
112 void Help (void)
113 {
114 int i;
115 char buffer [2000];
116 FILE* file = fopen ("help.txt", "r");
117
118 if(!file)
119 return;
120
121 for(i=0; i<2000; i++)
122 {
123 buffer[i] = getc(file);
124 if(!buffer[i]) break;
125 }
126
127 SetText(buffer);
128 }
129
130 // Create our Controls
131 void InitControls (HWND hwnd)
132 {
133
134 HINSTANCE hinst = GetModuleHandle(NULL);
135
136 // Create the controls
137 hTree = CreateWindowEx(0, WC_TREEVIEW, L"TreeView", WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS,
138 0, 0, 0, 0, hwnd, NULL, hinst, NULL);
139
140 hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"edit", PML_TransError(IDS_LOAD), WS_CHILD|WS_VISIBLE|ES_MULTILINE,
141 0, 0, 100, 100, hwnd, NULL, hinst, NULL);
142
143 hPopup = LoadMenu(hinst, MAKEINTRESOURCE(IDR_POPUP));
144
145 // Create Tree Icons
146 HIMAGELIST hIcon = ImageList_Create(16,16,ILC_COLOR32,1,1);
147 SendMessage(hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)(HIMAGELIST)hIcon);
148
149 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(1)));
150 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(11)));
151 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(12)));
152 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(13)));
153 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(14)));
154
155 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(2)));
156 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(3)));
157 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(4)));
158 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(5)));
159 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(6)));
160 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(7)));
161 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(8)));
162 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(9)));
163 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(10)));
164
165 // Setup Hotkeys
166 hHotKeys = LoadAccelerators (hinst, MAKEINTRESOURCE(IDR_HOTKEYS));
167 }
168
169 // Set the Icons
170 int SetIcon (int id, int icon)
171 {
172 TVITEMEX item;
173
174 item.hItem = nodes[id];
175 item.iImage = icon;
176 item.iSelectedImage = icon;
177 item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
178
179 TreeView_SetItem(hTree, &item);
180
181 return 1;
182 }
183
184 // Set the Icons
185 int Ask (const WCHAR* message)
186 {
187 int ans = MessageBox (0,message,0,MB_YESNO);
188
189 if(ans == IDYES)
190 return 1;
191
192 return 0;
193 }
194
195 // En- or Disable a Button inside of the toolbar and the Context Menu
196 int SetButton (DWORD id, BOOL state)
197 {
198 // Change the Toorbar Button
199 TBBUTTONINFO ti;
200
201 ti.cbSize = sizeof (ti);
202 ti.dwMask = TBIF_STATE;
203 /*
204 if(state&&id==1)
205 MessageBox(0,L"on",0,0);
206 else if(!state&&id==1)
207 MessageBox(0,L"off",0,0);
208 */
209 if(state)
210 ti.fsState = TBSTATE_ENABLED;
211 else
212 ti.fsState = TBSTATE_INDETERMINATE;
213
214 SendMessage (hTBar, TB_SETBUTTONINFO, id, (LPARAM)&ti);
215
216 // Change the Context Menu item
217 MENUITEMINFO mi;
218
219 mi.cbSize = sizeof (mi);
220 mi.fMask = MIIM_STATE;
221
222 if(state)
223 mi.fState = MFS_ENABLED;
224
225 else
226 mi.fState = MFS_GRAYED;
227
228 SetMenuItemInfo(hPopup, id, FALSE, &mi);
229
230 return 0;
231 }
232
233 // Set the text of the text box
234 int SetText (const char* text)
235 {
236 int i, j;
237 char buffer [2000];
238
239 if(!text)
240 return 1;
241
242 // the windows does not need "\n"
243 // for new lines but "\r\n"
244 for(i=0,j=0; text[i]; i++,j++)
245 {
246 buffer[j] = text[i];
247 if(buffer[j] == '\n')
248 buffer[++j] = '\r';
249 }
250 buffer[i] = 0;
251
252 SetWindowTextA(hEdit, buffer);
253
254 return 0;
255 }
256
257 // Windows Message Callback (this is where most things happen)
258 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
259 {
260 switch (message)
261 {
262 // at the very beginning ...
263 case WM_CREATE:
264 {
265 InitControls(hwnd);
266 }
267 break;
268
269 // calculate the size of the controls
270 case WM_SIZE:
271 {
272 RECT rcl;
273 SendMessage(hTBar, TB_AUTOSIZE, 0L, 0L);
274 GetWindowRect(hTBar, &rcl);
275
276 int win_top = rcl.bottom - rcl.top;
277 int win_hight = HIWORD(lParam) - win_top;
278
279 MoveWindow(hTree, 0, win_top, LOWORD(lParam), splitter_pos*win_hight/100, TRUE);
280 MoveWindow(hEdit, 0, (splitter_pos*win_hight/100)+win_top, LOWORD(lParam), win_hight, TRUE);
281 }
282 break;
283
284 // for the treeview
285 case WM_NOTIFY:
286 {
287 if(((LPNMHDR)lParam)->code == TVN_SELCHANGED)
288 {
289 selected = ((LPNMTREEVIEW)lParam)->itemNew.lParam;
290 PML_LoadPackage (tree, selected, SetButton);
291 SetText(PML_GetDescription (tree, selected));
292 }
293
294 else if ((int)(((LPNMHDR)lParam)->code) == NM_RCLICK) // <= aarrggg LISP
295 {
296 // which item has been click on
297 HTREEITEM item = TreeView_GetDropHilight(hTree);
298
299 if(item != NULL)
300 {
301 // mark the one as seleacted
302 SendMessage (hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)item);
303 TreeView_EnsureVisible (hTree, item);
304 }
305
306 // create the context menu
307 if(selected != 0)
308 {
309 POINT pt;
310 GetCursorPos (&pt);
311 TrackPopupMenu (GetSubMenu(hPopup, 0), 0, (UINT)pt.x, (UINT)pt.y, 0, hwnd, NULL);
312 }
313 }
314 }
315 break;
316
317 // for the toolbar
318 case WM_COMMAND:
319 {
320 // All Actions
321 if(LOWORD(wParam) <= 5 && LOWORD(wParam) >= 1)
322 {
323 if(selected)
324 if(PML_SetAction(tree, selected, LOWORD(wParam)-1, SetIcon, Ask) == ERR_OK)
325 break;
326
327 MessageBeep(MB_ICONHAND);
328 }
329
330 // DoIt
331 else if(LOWORD(wParam)==6)
332 {
333 if(PML_DoIt(tree, SetStatus, Ask) == ERR_OK)
334 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DOIT), hwnd, StatusProc);
335 else
336 MessageBeep(MB_ICONHAND);
337 }
338
339 // Help
340 else if(LOWORD(wParam)==7)
341 Help();
342
343 // Options
344 else if(LOWORD(wParam)==8)
345 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS), hwnd, OptionsProc);
346 }
347 break;
348
349 // prozess hotkeys
350 case WM_HOTKEY:
351 {
352 if(PML_SetAction(tree, selected, wParam, SetIcon, Ask) != ERR_OK)
353 MessageBeep(MB_ICONHAND);
354 }
355 break;
356
357 // ... at the very end
358 case WM_DESTROY:
359 {
360 PostQuitMessage(0);
361 return 0;
362 }
363 }
364
365 return DefWindowProc (hwnd, message, wParam, lParam);
366 }
367
368 // Warning: This function is called from another thread
369 int SetStatus (int status1, int status2, WCHAR* text)
370 {
371 // Set the Rage to 1000
372 SendMessage(GetDlgItem(hStatus, IDC_STATUS1), PBM_SETRANGE32, 0, 1000);
373 SendMessage(GetDlgItem(hStatus, IDC_STATUS2), PBM_SETRANGE32, 0, 1000);
374
375 // The prozessbars and the text filds
376 if(text)
377 SetDlgItemText(hStatus, IDC_TSTATUS, text);
378
379 if(status1!=-1)
380 SendMessage(GetDlgItem(hStatus, IDC_STATUS1), PBM_SETPOS, status1, 0);
381
382 if(status2!=-1)
383 SendMessage(GetDlgItem(hStatus, IDC_STATUS2), PBM_SETPOS, status2, 0);
384
385 // If the Status is 1000 very thing is done
386 if(status1==1000)
387 {
388 EndDialog(hStatus, TRUE);
389 MessageBox(0,PML_TransError(status2),0,0);
390 }
391
392 return 0;
393 }
394
395 // Callback for the Status Dialog
396 INT_PTR CALLBACK StatusProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
397 {
398 switch (msg)
399 {
400 case WM_INITDIALOG:
401 {
402 hStatus = hwnd;
403
404 } break;
405
406 case WM_COMMAND: // can only be the about button
407 case WM_CLOSE: // the close-window-[x]
408 {
409 PML_Abort();
410 EndDialog(hwnd, TRUE);
411 return 0;
412 }
413 }
414
415 return 0;
416 }
417
418 // Callback for the Options Dialog
419 INT_PTR CALLBACK OptionsProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
420 {
421 switch (msg)
422 {
423 case WM_CLOSE:
424 EndDialog(hwnd, TRUE);
425 return 0;
426 }
427
428 return 0;
429 }