]>
Commit | Line | Data |
---|---|---|
97647369 RC |
1 | /* |
2 | * Copyright (c) 2002 Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <robertc@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | #include "PickView.h" | |
17 | #include <commctrl.h> | |
18 | #include "PickPackageLine.h" | |
19 | #include "PickCategoryLine.h" | |
20 | #include "package_db.h" | |
21 | #include "package_version.h" | |
22 | #include "dialog.h" | |
23 | #include "resource.h" | |
24 | ||
25 | ||
26 | static PickView::Header pkg_headers[] = { | |
27 | {"Current", 7, 0, 0}, | |
28 | {"New", 3, 0, 0}, | |
29 | {"Src?", 4, 0, 0}, | |
30 | {"Category", 8, 0, 0}, | |
31 | {"Package", 7, 0, 0}, | |
32 | {0, 0, 0, 0} | |
33 | }; | |
34 | ||
35 | static PickView::Header cat_headers[] = { | |
36 | {"Category", 8, 0, 0}, | |
37 | {"Current", 7, 0, 0}, | |
38 | {"New", 3, 0, 0}, | |
39 | {"Src?", 4, 0, 0}, | |
40 | {"Package", 7, 0, 0}, | |
41 | {0, 0, 0, 0} | |
42 | }; | |
43 | ||
44 | // PickView:: views | |
45 | const PickView::views PickView::views::Unknown (0); | |
46 | const PickView::views PickView::views::PackageFull (1); | |
47 | const PickView::views PickView::views::Package = PickView::views (2); | |
48 | const PickView::views PickView::views::Category (3); | |
49 | ||
50 | // DoInsertItem - inserts an item into a header control. | |
51 | // Returns the index of the new item. | |
52 | // hwndHeader - handle to the header control. | |
53 | // iInsertAfter - index of the previous item. | |
54 | // nWidth - width of the new item. | |
55 | // lpsz - address of the item string. | |
56 | static int | |
57 | DoInsertItem (HWND hwndHeader, int iInsertAfter, int nWidth, LPSTR lpsz) | |
58 | { | |
59 | HDITEM hdi; | |
60 | int index; | |
61 | ||
62 | hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH; | |
63 | hdi.pszText = lpsz; | |
64 | hdi.cxy = nWidth; | |
65 | hdi.cchTextMax = lstrlen (hdi.pszText); | |
66 | hdi.fmt = HDF_LEFT | HDF_STRING; | |
67 | ||
68 | index = SendMessage (hwndHeader, HDM_INSERTITEM, | |
69 | (WPARAM) iInsertAfter, (LPARAM) & hdi); | |
70 | ||
71 | return index; | |
72 | } | |
73 | ||
74 | ||
75 | ||
76 | void | |
77 | PickView::set_headers () | |
78 | { | |
79 | if (view_mode == views::Unknown) | |
80 | return; | |
81 | if (view_mode == views::PackageFull || | |
82 | view_mode == views::Package) | |
83 | { | |
84 | headers = pkg_headers; | |
85 | current_col = 0; | |
86 | new_col = 1; | |
87 | src_col = 2; | |
88 | cat_col = 3; | |
89 | pkg_col = 4; | |
90 | last_col = 4; | |
91 | } | |
92 | else if (view_mode == views::Category) | |
93 | { | |
94 | headers = cat_headers; | |
95 | current_col = 1; | |
96 | new_col = 2; | |
97 | src_col = 3; | |
98 | cat_col = 0; | |
99 | pkg_col = 4; | |
100 | last_col = 4; | |
101 | } | |
102 | else | |
103 | return; | |
104 | while (int n = SendMessage (listheader, HDM_GETITEMCOUNT, 0, 0)) | |
105 | { | |
106 | SendMessage (listheader, HDM_DELETEITEM, n - 1, 0); | |
107 | } | |
108 | int i; | |
109 | for (i = 0; i <= last_col; i++) | |
110 | DoInsertItem (listheader, i, headers[i].width, (char *) headers[i].text); | |
111 | } | |
112 | ||
113 | void | |
114 | PickView::note_width (PickView::Header *hdrs, HDC dc, const char *string, int addend, | |
115 | int column) | |
116 | { | |
117 | if (!string) | |
118 | { | |
119 | if (hdrs[column].width < addend) | |
120 | hdrs[column].width = addend; | |
121 | return; | |
122 | } | |
123 | SIZE s; | |
124 | GetTextExtentPoint32 (dc, string, strlen (string), &s); | |
125 | if (hdrs[column].width < s.cx + addend) | |
126 | hdrs[column].width = s.cx + addend; | |
127 | } | |
128 | ||
129 | void | |
130 | PickView::set_view_mode (PickView::views _mode) | |
131 | { | |
132 | view_mode = _mode; | |
133 | set_headers (); | |
134 | } | |
135 | ||
136 | const char * | |
137 | PickView::mode_caption () | |
138 | { | |
139 | return view_mode.caption (); | |
140 | } | |
141 | ||
142 | const char * | |
143 | PickView::views::caption () | |
144 | { | |
145 | switch (_value) | |
146 | { | |
147 | case 1: | |
148 | return "Full"; | |
149 | case 2: | |
150 | return "Partial"; | |
151 | case 3: | |
152 | return "Category"; | |
153 | default: | |
154 | return ""; | |
155 | } | |
156 | } | |
157 | ||
158 | void | |
159 | PickView::insert_pkg (packagemeta & pkg) | |
160 | { | |
161 | if (view_mode != views::Category) | |
162 | { | |
163 | PickPackageLine & line = *new PickPackageLine (*this, pkg); | |
164 | contents.insert (line); | |
165 | } | |
166 | else | |
167 | { | |
168 | for (size_t x = 1; x <= pkg.Categories.number (); x++) | |
169 | { | |
170 | Category & cat = pkg.Categories[x]->key; | |
171 | // Special case - yuck | |
172 | if (cat == Category ("All")) | |
173 | continue; | |
174 | PickCategoryLine & catline = *new PickCategoryLine (*this, cat, 1); | |
175 | PickPackageLine & line = *new PickPackageLine (*this, pkg); | |
176 | catline.insert (line); | |
177 | contents.insert (catline); | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
182 | void | |
183 | PickView::insert_category (Category * cat, bool collapsed) | |
184 | { | |
185 | if (*cat == Category ("All")) | |
186 | return; | |
187 | PickCategoryLine & catline = *new PickCategoryLine (*this, *cat, 1, collapsed); | |
188 | for (CategoryPackage * catpkg = cat->packages; catpkg; | |
189 | catpkg = catpkg->next) | |
190 | { | |
191 | ||
192 | PickPackageLine & line = *new PickPackageLine (*this, *catpkg->pkg); | |
193 | catline.insert (line); | |
194 | } | |
195 | contents.insert (catline); | |
196 | } | |
197 | ||
198 | void | |
199 | PickView::clear_view (void) | |
200 | { | |
201 | contents.empty (); | |
202 | if (view_mode == views::Unknown) | |
203 | return; | |
204 | if (view_mode == views::PackageFull || | |
205 | view_mode == views::Package) | |
206 | contents.ShowLabel (false); | |
207 | else if (view_mode == views::Category) | |
208 | contents.ShowLabel (); | |
209 | } | |
210 | ||
211 | PickView::views& | |
212 | PickView::views::operator++ () | |
213 | { | |
214 | ++_value; | |
215 | if (_value > Category._value) | |
216 | _value = 1; | |
217 | return *this; | |
218 | } | |
219 | ||
220 | int | |
221 | PickView::click (int row, int x) | |
222 | { | |
223 | return contents.click (0, row, x); | |
224 | } | |
225 | ||
226 | ||
227 | void | |
228 | PickView::scroll (HWND hwnd, int which, int *var, int code) | |
229 | { | |
230 | SCROLLINFO si; | |
231 | si.cbSize = sizeof (si); | |
232 | si.fMask = SIF_ALL; | |
233 | GetScrollInfo (hwnd, which, &si); | |
234 | ||
235 | switch (code) | |
236 | { | |
237 | case SB_THUMBTRACK: | |
238 | si.nPos = si.nTrackPos; | |
239 | break; | |
240 | case SB_THUMBPOSITION: | |
241 | break; | |
242 | case SB_BOTTOM: | |
243 | si.nPos = si.nMax; | |
244 | break; | |
245 | case SB_TOP: | |
246 | si.nPos = 0; | |
247 | break; | |
248 | case SB_LINEDOWN: | |
249 | si.nPos += row_height; | |
250 | break; | |
251 | case SB_LINEUP: | |
252 | si.nPos -= row_height; | |
253 | break; | |
254 | case SB_PAGEDOWN: | |
255 | si.nPos += si.nPage * 9 / 10; | |
256 | break; | |
257 | case SB_PAGEUP: | |
258 | si.nPos -= si.nPage * 9 / 10; | |
259 | break; | |
260 | } | |
261 | ||
262 | if ((int) si.nPos < 0) | |
263 | si.nPos = 0; | |
264 | if (si.nPos + si.nPage > (unsigned int) si.nMax) | |
265 | si.nPos = si.nMax - si.nPage; | |
266 | ||
267 | si.fMask = SIF_POS; | |
268 | SetScrollInfo (hwnd, which, &si, TRUE); | |
269 | ||
270 | int ox = scroll_ulc_x; | |
271 | int oy = scroll_ulc_y; | |
272 | *var = si.nPos; | |
273 | ||
274 | RECT cr, sr; | |
275 | GetClientRect (hwnd, &cr); | |
276 | sr = cr; | |
277 | sr.top += header_height; | |
278 | UpdateWindow (hwnd); | |
279 | ScrollWindow (hwnd, ox - scroll_ulc_x, oy - scroll_ulc_y, &sr, &sr); | |
280 | /* | |
281 | sr.bottom = sr.top; | |
282 | sr.top = cr.top; | |
283 | ScrollWindow (hwnd, ox - scroll_ulc_x, 0, &sr, &sr); | |
284 | */ | |
285 | if (ox - scroll_ulc_x) | |
286 | { | |
287 | GetClientRect (listheader, &cr); | |
288 | sr = cr; | |
289 | // UpdateWindow (htmp); | |
290 | MoveWindow (listheader, -scroll_ulc_x, 0, | |
291 | headers[last_col].x + | |
292 | headers[last_col].width, header_height, TRUE); | |
293 | } | |
294 | UpdateWindow (hwnd); | |
295 | } | |
296 | ||
297 | void | |
298 | PickView::init_headers (HDC dc) | |
299 | { | |
300 | int i; | |
301 | ||
302 | for (i = 0; headers[i].text; i++) | |
303 | { | |
304 | headers[i].width = 0; | |
305 | headers[i].x = 0; | |
306 | } | |
307 | ||
308 | for (i = 0; headers[i].text; i++) | |
309 | note_width (headers, dc, headers[i].text, HMARGIN, i); | |
310 | /* src checkbox */ | |
311 | note_width (headers, dc, 0, HMARGIN + 11, src_col); | |
312 | packagedb db; | |
313 | for (size_t n = 1; n <= db.packages.number (); n++) | |
314 | { | |
315 | packagemeta & pkg = *db.packages[n]; | |
316 | if (pkg.installed) | |
317 | note_width (headers, dc, pkg.installed->Canonical_version (), | |
318 | HMARGIN, current_col); | |
319 | for (size_t n = 1; n <= pkg.versions.number (); n++) | |
320 | if (pkg.versions[n] != pkg.installed) | |
321 | note_width (headers, dc, | |
322 | pkg.versions[n]->Canonical_version (), | |
323 | NEW_COL_SIZE_SLOP + HMARGIN, new_col); | |
324 | for (size_t n = 1; n <= db.categories.number (); n++) | |
325 | note_width (headers, dc, db.categories[n]->name, HMARGIN, cat_col); | |
326 | if (!pkg.SDesc ()) | |
327 | note_width (headers, dc, pkg.name, HMARGIN, pkg_col); | |
328 | else | |
329 | { | |
330 | static char buf[512]; | |
331 | strcpy (buf, pkg.name); | |
332 | strcat (buf, ": "); | |
333 | strcat (buf, pkg.SDesc ()); | |
334 | note_width (headers, dc, buf, HMARGIN, pkg_col); | |
335 | } | |
336 | } | |
337 | note_width (headers, dc, "keep", NEW_COL_SIZE_SLOP + HMARGIN, new_col); | |
338 | note_width (headers, dc, "uninstall", NEW_COL_SIZE_SLOP + HMARGIN, new_col); | |
339 | ||
340 | headers[0].x = 0; | |
341 | for (i = 1; i <= last_col; i++) | |
342 | headers[i].x = headers[i - 1].x + headers[i - 1].width; | |
343 | } | |
344 | ||
345 | ||
346 | PickView::PickView (views _mode, HWND lv, Category &cat) : deftrust (TRUST_UNKNOWN), | |
347 | contents (*this, cat, 0, false, true), listview (lv) | |
348 | { | |
349 | ||
350 | HDC dc = GetDC (listview); | |
351 | sysfont = GetStockObject (DEFAULT_GUI_FONT); | |
352 | SelectObject (dc, sysfont); | |
353 | GetTextMetrics (dc, &tm); | |
354 | ||
355 | bitmap_dc = CreateCompatibleDC (dc); | |
356 | bm_spin = LoadImage (hinstance, MAKEINTRESOURCE (IDB_SPIN), IMAGE_BITMAP, 0, 0, 0); | |
357 | bm_rtarrow = LoadImage (hinstance, MAKEINTRESOURCE (IDB_RTARROW), IMAGE_BITMAP, | |
358 | 0, 0, 0); | |
359 | ||
360 | bm_checkyes = LoadImage (hinstance, MAKEINTRESOURCE (IDB_CHECK_YES), IMAGE_BITMAP, | |
361 | 0, 0, 0); | |
362 | bm_checkno = LoadImage (hinstance, MAKEINTRESOURCE (IDB_CHECK_NO), IMAGE_BITMAP, | |
363 | 0, 0, 0); | |
364 | bm_checkna = LoadImage (hinstance, MAKEINTRESOURCE (IDB_CHECK_NA), IMAGE_BITMAP, | |
365 | 0, 0, 0); | |
366 | ||
367 | row_height = (tm.tmHeight + tm.tmExternalLeading + ROW_MARGIN); | |
368 | int | |
369 | irh = | |
370 | tm. | |
371 | tmExternalLeading + | |
372 | tm. | |
373 | tmDescent + | |
374 | 11 + | |
375 | ROW_MARGIN; | |
376 | if (row_height < irh) | |
377 | row_height = irh; | |
378 | ||
379 | RECT rcParent; | |
380 | HDLAYOUT hdl; | |
381 | WINDOWPOS wp; | |
382 | ||
383 | // Ensure that the common control DLL is loaded, and then create | |
384 | // the header control. | |
385 | INITCOMMONCONTROLSEX controlinfo = | |
386 | { | |
387 | sizeof (INITCOMMONCONTROLSEX), ICC_LISTVIEW_CLASSES}; | |
388 | InitCommonControlsEx (&controlinfo); | |
389 | ||
390 | if ((listheader = CreateWindowEx (0, WC_HEADER, (LPCTSTR) NULL, | |
391 | WS_CHILD | WS_BORDER | CCS_NORESIZE | | |
392 | // | HDS_BUTTONS | |
393 | HDS_HORZ, 0, 0, 0, 0, listview, | |
394 | (HMENU) IDC_CHOOSE_LISTHEADER, hinstance, | |
395 | (LPVOID) NULL)) == NULL) | |
396 | // FIXME: throw an exception | |
397 | exit (10); | |
398 | ||
399 | // Retrieve the bounding rectangle of the parent window's | |
400 | // client area, and then request size and position values | |
401 | // from the header control. | |
402 | GetClientRect (listview, &rcParent); | |
403 | ||
404 | hdl.prc = &rcParent; | |
405 | hdl.pwpos = ℘ | |
406 | if (!SendMessage (listheader, HDM_LAYOUT, 0, (LPARAM) & hdl)) | |
407 | // FIXME: throw an exception | |
408 | exit (11); | |
409 | ||
410 | ||
411 | // Set the size, position, and visibility of the header control. | |
412 | SetWindowPos (listheader, wp.hwndInsertAfter, wp.x, wp.y, | |
413 | wp.cx, wp.cy, wp.flags | SWP_SHOWWINDOW); | |
414 | ||
415 | header_height = wp.cy; | |
416 | ||
417 | view_mode = PickView::views::Package; | |
418 | set_headers (); | |
419 | init_headers (dc); | |
420 | view_mode = PickView::views::Category; | |
421 | set_headers (); | |
422 | init_headers (dc); | |
423 | ||
424 | view_mode = _mode; | |
425 | set_headers (); | |
426 | ||
427 | ReleaseDC (lv, dc); | |
428 | } |