fix include file case
[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 WCHAR errbuf[2000];
44
45 // Window creation
46 wc.cbSize = sizeof(WNDCLASSEX);
47 wc.lpszClassName = L"pgkmgr";
48 wc.style = CS_HREDRAW | CS_VREDRAW;
49 wc.lpfnWndProc = (WNDPROC)WndProc;
50 wc.hInstance = hinst;
51 wc.hIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_MAIN));
52 wc.hbrBackground = (HBRUSH)(COLOR_SCROLLBAR);
53
54 RegisterClassEx(&wc);
55
56 hwnd = CreateWindow(L"pgkmgr",
57 L"ReactOS - Package Manager v0.3",
58 WS_OVERLAPPEDWINDOW,
59 CW_USEDEFAULT,
60 CW_USEDEFAULT,
61 500, 600,
62 NULL, NULL,
63 hinst,
64 NULL);
65
66
67 // Toolbar creation
68 InitCommonControls();
69
70 hTBar = CreateToolbarEx(hwnd, WS_CHILD|WS_VISIBLE|TBSTYLE_FLAT, 0, 8, hinst, IDB_TOOLBAR,
71 Buttons, sizeof(Buttons)/sizeof(TBBUTTON), TBSIZE, TBSIZE, TBSIZE, TBSIZE, sizeof(TBBUTTON));
72
73 // Show the windows
74 ShowWindow(hwnd, SW_SHOW);
75 UpdateWindow(hwnd);
76
77 // Load the tree
78 int error = PML_LoadTree(&tree, "tree_bare.xml", AddItem);
79
80 if(error)
81 {
82 MessageBox(0,PML_TransError(error, errbuf, sizeof(errbuf)/sizeof(WCHAR)),0,0);
83 return 0;
84 }
85
86 // Read the help
87 Help();
88
89 // Start getting messages
90 while(GetMessage(&msg,NULL,0,0))
91 {
92 if(!TranslateAccelerator(hwnd, hHotKeys, &msg))
93 {
94 TranslateMessage(&msg);
95 DispatchMessage(&msg);
96 }
97 }
98
99 // Close our handle
100 PML_CloseTree (tree);
101
102 return 0;
103 }
104
105 // Add a item to our tree
106 int AddItem (int id, const char* name, int parent, int icon)
107 {
108 TV_INSERTSTRUCT tvins;
109
110 tvins.item.lParam = (UINT)id;
111 tvins.item.mask = TVIF_TEXT|TVIF_PARAM;
112 tvins.item.pszText = (WCHAR*)name; //that is ok
113 tvins.item.cchTextMax = strlen(name);
114 tvins.hInsertAfter = TVI_LAST;
115
116 if(icon)
117 {
118 tvins.item.iImage = icon;
119 tvins.item.iSelectedImage = icon;
120 tvins.item.mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
121 }
122
123 if (parent==0)
124 tvins.hParent = TVI_ROOT;
125 else
126 tvins.hParent = nodes[parent];
127
128 nodes[id] = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMA, 0, (LPARAM)&tvins);
129
130 return 0;
131 }
132
133 // Load the Help from file and display it
134 void Help (void)
135 {
136 int i;
137 char buffer [2000];
138 FILE* file = fopen ("help.txt", "r");
139
140 if(!file)
141 return;
142
143 for(i=0; i<2000; i++)
144 {
145 buffer[i] = getc(file);
146 if(buffer[i]==EOF) break;
147 }
148 buffer[i] = 0;
149
150 SetText(buffer);
151 }
152
153 // Create our Controls
154 void InitControls (HWND hwnd)
155 {
156
157 HINSTANCE hinst = GetModuleHandle(NULL);
158 WCHAR errbuf[2000];
159
160 // Create the controls
161 hTree = CreateWindowEx(0, WC_TREEVIEW, L"TreeView", WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS,
162 0, 0, 0, 0, hwnd, NULL, hinst, NULL);
163
164 hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"edit", PML_TransError(IDS_LOAD, errbuf, sizeof(errbuf)/sizeof(WCHAR)), WS_CHILD|WS_VISIBLE|ES_MULTILINE,
165 0, 0, 100, 100, hwnd, NULL, hinst, NULL);
166
167 hPopup = LoadMenu(hinst, MAKEINTRESOURCE(IDR_POPUP));
168
169 // Create Tree Icons
170 HIMAGELIST hIcon = ImageList_Create(16, 16, ILC_MASK|ILC_COLOR32, 1, 1);
171 SendMessage(hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)(HIMAGELIST)hIcon);
172
173 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(1)));
174 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(11)));
175 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(12)));
176 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(13)));
177 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(14)));
178
179 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(2)));
180 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(3)));
181 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(4)));
182 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(5)));
183 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(6)));
184 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(7)));
185 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(8)));
186 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(9)));
187 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(10)));
188
189 // Setup Hotkeys
190 hHotKeys = LoadAccelerators (hinst, MAKEINTRESOURCE(IDR_HOTKEYS));
191 }
192
193 // Set the Icons
194 int SetIcon (int id, int icon)
195 {
196 TVITEMEX item;
197
198 item.hItem = nodes[id];
199 item.iImage = icon;
200 item.iSelectedImage = icon;
201 item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
202
203 return (int)TreeView_SetItem(hTree, &item);
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 selected
322 SendMessage (hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)item);
323 (void)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 WCHAR errbuf[2000];
392
393 // Set the Rage to 1000
394 SendMessage(GetDlgItem(hStatus, IDC_STATUS1), PBM_SETRANGE32, 0, 1000);
395 SendMessage(GetDlgItem(hStatus, IDC_STATUS2), PBM_SETRANGE32, 0, 1000);
396
397 // The prozessbars and the text filds
398 if(text)
399 SetDlgItemText(hStatus, IDC_TSTATUS, text);
400
401 if(status1!=-1)
402 SendMessage(GetDlgItem(hStatus, IDC_STATUS1), PBM_SETPOS, status1, 0);
403
404 if(status2!=-1)
405 SendMessage(GetDlgItem(hStatus, IDC_STATUS2), PBM_SETPOS, status2, 0);
406
407 // If the Status is 1000 everything is done
408 if(status1==1000)
409 {
410 EndDialog(hStatus, TRUE);
411 MessageBox(0,PML_TransError(status2, errbuf, sizeof(errbuf)/sizeof(WCHAR)),0,0);
412 }
413
414 return 0;
415 }
416
417 // Callback for the Status Dialog
418 INT_PTR CALLBACK StatusProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
419 {
420 switch (msg)
421 {
422 case WM_INITDIALOG:
423 {
424 hStatus = hwnd;
425
426 } break;
427
428 case WM_COMMAND: // can only be the about button
429 case WM_CLOSE: // the close-window-[x]
430 {
431 PML_Abort();
432 EndDialog(hwnd, TRUE);
433 return 0;
434 }
435 }
436
437 return 0;
438 }
439
440 // Callback for the Options Dialog
441 INT_PTR CALLBACK OptionsProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
442 {
443 switch (msg)
444 {
445 case WM_CLOSE:
446 EndDialog(hwnd, TRUE);
447 return 0;
448 }
449
450 return 0;
451 }