893cb7a026a92598459b498478ec7ff3cf1be102
[reactos.git] / rosapps / packmgr / gui / main.cpp
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 TranslateMessage(&msg);
71 DispatchMessage(&msg);
72 }
73
74 // Close our handle
75 PML_CloseTree (tree);
76
77 return 0;
78 }
79
80 // Add a item to our tree
81 int AddItem (int id, const char* name, int parent, int icon)
82 {
83 TV_INSERTSTRUCT tvins;
84
85 tvins.item.lParam = (UINT)id;
86 tvins.item.mask = TVIF_TEXT|TVIF_PARAM;
87 tvins.item.pszText = (WCHAR*)name; //that is ok
88 tvins.item.cchTextMax = strlen(name);
89 tvins.hInsertAfter = TVI_LAST;
90
91 if(icon)
92 {
93 tvins.item.iImage = icon;
94 tvins.item.iSelectedImage = icon;
95 tvins.item.mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
96 }
97
98 if (parent==0)
99 tvins.hParent = TVI_ROOT;
100 else
101 tvins.hParent = nodes[parent];
102
103 nodes[id] = (HTREEITEM)SendMessage(hTree, TVM_INSERTITEMA, 0, (LPARAM)&tvins);
104
105 return 0;
106 }
107
108 // Load the Help from file and display it
109 void Help (void)
110 {
111 string source;
112
113 ifstream file("help.txt", ios_base::in);
114 getline(file, source, '\0');
115
116 SetText(source.c_str());
117 }
118
119 // Create our Controls
120 void InitControls (HWND hwnd)
121 {
122
123 HINSTANCE hinst = GetModuleHandle(NULL);
124
125 // Create the controls
126 hTree = CreateWindowEx(0, WC_TREEVIEW, L"TreeView", WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS,
127 0, 0, 0, 0, hwnd, NULL, hinst, NULL);
128
129 hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"edit", PML_TransError(IDS_LOAD), WS_CHILD|WS_VISIBLE|ES_MULTILINE,
130 0, 0, 100, 100, hwnd, NULL, hinst, NULL);
131
132 hPopup = LoadMenu(hinst, MAKEINTRESOURCE(IDR_POPUP));
133
134 // Create Tree Icons
135 HIMAGELIST hIcon = ImageList_Create(16,16,ILC_COLOR32,1,1);
136 SendMessage(hTree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)(HIMAGELIST)hIcon);
137
138 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(1)));
139 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(11)));
140 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(12)));
141 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(13)));
142 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(14)));
143
144 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(2)));
145 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(3)));
146 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(4)));
147 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(5)));
148 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(6)));
149 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(7)));
150 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(8)));
151 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(9)));
152 ImageList_AddIcon(hIcon, LoadIcon(hinst, MAKEINTRESOURCE(10)));
153
154 // Setup Hotkeys
155 RegisterHotKey(hwnd, 1, MOD_CONTROL, VK_1);
156 RegisterHotKey(hwnd, 2, MOD_CONTROL, VK_2);
157 RegisterHotKey(hwnd, 3, MOD_CONTROL, VK_3);
158 RegisterHotKey(hwnd, 4, MOD_CONTROL, VK_4);
159 RegisterHotKey(hwnd, 0, MOD_CONTROL, VK_0);
160 }
161
162 // Set the Icons
163 int SetIcon (int id, int icon)
164 {
165 TVITEMEX item;
166
167 item.hItem = nodes[id];
168 item.iImage = icon;
169 item.iSelectedImage = icon;
170 item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
171
172 TreeView_SetItem(hTree, &item);
173
174 return 1;
175 }
176
177 // En- or Disable a Button inside of the toolbar and the Context Menu
178 int SetButton (DWORD id, BOOL state)
179 {
180 // Change the Toorbar Button
181 TBBUTTONINFO ti;
182
183 ti.cbSize = sizeof (ti);
184 ti.dwMask = TBIF_STATE;
185
186 if(state)
187 ti.fsState = TBSTATE_ENABLED;
188 else
189 ti.fsState = TBSTATE_INDETERMINATE;
190
191 SendMessage (hTBar, TB_SETBUTTONINFO, id, (LPARAM)&ti);
192
193 // Change the Context Menu item
194 MENUITEMINFO mi;
195
196 mi.cbSize = sizeof (mi);
197 mi.fMask = MIIM_STATE;
198
199 if(state)
200 mi.fState = MFS_ENABLED;
201
202 else
203 mi.fState = MFS_GRAYED;
204
205 SetMenuItemInfo(hPopup, id, FALSE, &mi);
206
207 return 1;
208 }
209
210 // Set the text of the text box
211 int SetText (const char* text)
212 {
213 int i = 0;
214 string source = text;
215
216 // the windows does not need "\n"
217 // for new lines but "\r\n"
218 for(i=0; source[i]; i++)
219 if(source[i]=='\n' && source[i]!='\r')
220 source.insert (i++, "\r");
221
222 SetWindowTextA(hEdit, source.c_str());
223
224 return 0;
225 }
226
227 // Windows Message Callback (this is where most things happen)
228 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
229 {
230 switch (message)
231 {
232 // at the very beginning ...
233 case WM_CREATE:
234 {
235 InitControls(hwnd);
236 }
237 break;
238
239 // calculate the size of the controls
240 case WM_SIZE:
241 {
242 RECT rcl;
243 SendMessage(hTBar, TB_AUTOSIZE, 0L, 0L);
244 GetWindowRect(hTBar, &rcl);
245
246 int win_top = rcl.bottom - rcl.top;
247 int win_hight = HIWORD(lParam) - win_top;
248
249 MoveWindow(hTree, 0, win_top, LOWORD(lParam), splitter_pos*win_hight/100, TRUE);
250 MoveWindow(hEdit, 0, (splitter_pos*win_hight/100)+win_top, LOWORD(lParam), win_hight, TRUE);
251 }
252 break;
253
254 // for the treeview
255 case WM_NOTIFY:
256 {
257 if(((LPNMHDR)lParam)->code == TVN_SELCHANGED)
258 {
259 selected = ((LPNMTREEVIEW)lParam)->itemNew.lParam;
260 PML_LoadPackage (tree, selected, SetButton, SetText);
261 }
262
263 else if ((int)(((LPNMHDR)lParam)->code) == NM_RCLICK) // <= ahhh LISP
264 {
265 // which item has been click on
266 HTREEITEM item = TreeView_GetDropHilight(hTree);
267
268 if(item != NULL)
269 {
270 // mark the one as seleacted
271 SendMessage (hTree, TVM_SELECTITEM, TVGN_CARET, (LPARAM)item);
272 TreeView_EnsureVisible (hTree, item);
273 }
274
275 // create the context menu
276 if(selected != 0)
277 {
278 POINT pt;
279 GetCursorPos (&pt);
280 TrackPopupMenu (GetSubMenu(hPopup, 0), 0, (UINT)pt.x, (UINT)pt.y, 0, hwnd, NULL);
281 }
282 }
283 }
284 break;
285
286 // for the toolbar
287 case WM_COMMAND:
288 {
289 // All Actions
290 if(LOWORD(wParam) <= 5 && LOWORD(wParam) >= 1)
291 {
292 if(selected)
293 if(PML_SetAction(tree, selected, LOWORD(wParam)-1, SetIcon) == ERR_OK)
294 break;
295
296 MessageBeep(MB_ICONHAND);
297 }
298
299 // DoIt
300 else if(LOWORD(wParam)==6)
301 {
302 if(PML_DoIt(tree, SetStatus) == ERR_OK)
303 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DOIT), hwnd, StatusProc);
304 else
305 MessageBeep(MB_ICONHAND);
306 }
307
308 // Help
309 else if(LOWORD(wParam)==7)
310 Help();
311
312 // Options
313 else if(LOWORD(wParam)==8)
314 DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS), hwnd, OptionsProc);
315
316 else
317 MessageBox(0,0,0,0);
318 }
319 break;
320
321 // prozess hotkeys
322 case WM_HOTKEY:
323 {
324 if(PML_SetAction(tree, selected, wParam, SetIcon) != ERR_OK)
325 MessageBeep(MB_ICONHAND);
326 }
327 break;
328
329 // ... at the very end
330 case WM_DESTROY:
331 {
332 PostQuitMessage(0);
333 return 0;
334 }
335 }
336
337 return DefWindowProc (hwnd, message, wParam, lParam);
338 }
339
340 // Warning: This function is called from another thread
341 int SetStatus (int status1, int status2, WCHAR* text)
342 {
343 // Set the Rage to 1000
344 SendMessage(GetDlgItem(hStatus, IDC_STATUS1), PBM_SETRANGE32, 0, 1000);
345 SendMessage(GetDlgItem(hStatus, IDC_STATUS2), PBM_SETRANGE32, 0, 1000);
346
347 // The prozessbars and the text filds
348 if(text)
349 SetDlgItemText(hStatus, IDC_TSTATUS, text);
350
351 if(status1!=-1)
352 SendMessage(GetDlgItem(hStatus, IDC_STATUS1), PBM_SETPOS, status1, 0);
353
354 if(status2!=-1)
355 SendMessage(GetDlgItem(hStatus, IDC_STATUS2), PBM_SETPOS, status2, 0);
356
357 // If the Status is 1000 very thing is done
358 if(status1==1000)
359 {
360 EndDialog(hStatus, TRUE);
361 MessageBox(0,PML_TransError(status2),0,0);
362 }
363
364 return 0;
365 }
366
367 // Callback for the Status Dialog
368 INT_PTR CALLBACK StatusProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
369 {
370 switch (msg)
371 {
372 case WM_INITDIALOG:
373 {
374 hStatus = hwnd;
375
376 } break;
377
378 case WM_COMMAND: // can only be the about button
379 case WM_CLOSE: // the close-window-[x]
380 {
381 PML_Abort();
382 EndDialog(hwnd, TRUE);
383 return 0;
384 }
385 }
386
387 return 0;
388 }
389
390 // Callback for the Options Dialog
391 INT_PTR CALLBACK OptionsProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
392 {
393 switch (msg)
394 {
395 case WM_CLOSE:
396 EndDialog(hwnd, TRUE);
397 return 0;
398 }
399
400 return 0;
401 }