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