]>
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" | |
2e0aaec9 BD |
17 | #include <algorithm> |
18 | #include <limits.h> | |
97647369 | 19 | #include <commctrl.h> |
1789ef54 | 20 | #include <shlwapi.h> |
97647369 RC |
21 | #include "PickPackageLine.h" |
22 | #include "PickCategoryLine.h" | |
23 | #include "package_db.h" | |
24 | #include "package_version.h" | |
25 | #include "dialog.h" | |
26 | #include "resource.h" | |
693916f8 RC |
27 | /* For 'source' */ |
28 | #include "state.h" | |
29 | #include "LogSingleton.h" | |
97647369 | 30 | |
6625e635 | 31 | using namespace std; |
97647369 RC |
32 | |
33 | static PickView::Header pkg_headers[] = { | |
82306ac2 BD |
34 | {"Current", 0, 0, true}, |
35 | {"New", 0, 0, true}, | |
36 | {"Bin?", 0, 0, false}, | |
37 | {"Src?", 0, 0, false}, | |
38 | {"Categories", 0, 0, true}, | |
0ac305ec | 39 | {"Size", 0, 0, true}, |
82306ac2 BD |
40 | {"Package", 0, 0, true}, |
41 | {0, 0, 0, false} | |
97647369 RC |
42 | }; |
43 | ||
44 | static PickView::Header cat_headers[] = { | |
82306ac2 BD |
45 | {"Category", 0, 0, true}, |
46 | {"Current", 0, 0, true}, | |
47 | {"New", 0, 0, true}, | |
48 | {"Bin?", 0, 0, false}, | |
49 | {"Src?", 0, 0, false}, | |
0ac305ec | 50 | {"Size", 0, 0, true}, |
82306ac2 BD |
51 | {"Package", 0, 0, true}, |
52 | {0, 0, 0, false} | |
97647369 RC |
53 | }; |
54 | ||
55 | // PickView:: views | |
56 | const PickView::views PickView::views::Unknown (0); | |
57 | const PickView::views PickView::views::PackageFull (1); | |
e0aec95e MB |
58 | const PickView::views PickView::views::Package (2); |
59 | const PickView::views PickView::views::PackageKeeps (3); | |
60 | const PickView::views PickView::views::PackageSkips = PickView::views (4); | |
61 | const PickView::views PickView::views::Category (5); | |
97647369 | 62 | |
693916f8 RC |
63 | ATOM PickView::WindowClassAtom = 0; |
64 | ||
97647369 RC |
65 | // DoInsertItem - inserts an item into a header control. |
66 | // Returns the index of the new item. | |
67 | // hwndHeader - handle to the header control. | |
68 | // iInsertAfter - index of the previous item. | |
69 | // nWidth - width of the new item. | |
70 | // lpsz - address of the item string. | |
71 | static int | |
72 | DoInsertItem (HWND hwndHeader, int iInsertAfter, int nWidth, LPSTR lpsz) | |
73 | { | |
74 | HDITEM hdi; | |
75 | int index; | |
76 | ||
77 | hdi.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH; | |
78 | hdi.pszText = lpsz; | |
79 | hdi.cxy = nWidth; | |
80 | hdi.cchTextMax = lstrlen (hdi.pszText); | |
81 | hdi.fmt = HDF_LEFT | HDF_STRING; | |
82 | ||
83 | index = SendMessage (hwndHeader, HDM_INSERTITEM, | |
84 | (WPARAM) iInsertAfter, (LPARAM) & hdi); | |
85 | ||
86 | return index; | |
87 | } | |
88 | ||
0baffe9a DK |
89 | int |
90 | PickView::set_header_column_order (views vm) | |
97647369 | 91 | { |
0baffe9a DK |
92 | if (vm == views::Unknown) |
93 | return -1; | |
94 | else if (vm == views::PackageFull || vm == views::Package | |
95 | || vm == views::PackageKeeps || vm == views::PackageSkips) | |
97647369 RC |
96 | { |
97 | headers = pkg_headers; | |
98 | current_col = 0; | |
99 | new_col = 1; | |
2fa7c5a4 RC |
100 | bintick_col = new_col + 1; |
101 | srctick_col = bintick_col + 1; | |
102 | cat_col = srctick_col + 1; | |
0ac305ec BD |
103 | size_col = cat_col + 1; |
104 | pkg_col = size_col + 1; | |
931f2755 | 105 | last_col = pkg_col; |
97647369 | 106 | } |
0baffe9a | 107 | else if (vm == views::Category) |
97647369 RC |
108 | { |
109 | headers = cat_headers; | |
0baffe9a | 110 | cat_col = 0; |
97647369 | 111 | current_col = 1; |
2fa7c5a4 RC |
112 | new_col = current_col + 1; |
113 | bintick_col = new_col + 1; | |
114 | srctick_col = bintick_col + 1; | |
0ac305ec BD |
115 | size_col = srctick_col + 1; |
116 | pkg_col = size_col + 1; | |
2fa7c5a4 | 117 | last_col = pkg_col; |
97647369 RC |
118 | } |
119 | else | |
0baffe9a DK |
120 | return -1; |
121 | return last_col; | |
122 | } | |
123 | ||
124 | void | |
125 | PickView::set_headers () | |
126 | { | |
127 | if (set_header_column_order (view_mode) == -1) | |
97647369 RC |
128 | return; |
129 | while (int n = SendMessage (listheader, HDM_GETITEMCOUNT, 0, 0)) | |
130 | { | |
131 | SendMessage (listheader, HDM_DELETEITEM, n - 1, 0); | |
132 | } | |
133 | int i; | |
134 | for (i = 0; i <= last_col; i++) | |
135 | DoInsertItem (listheader, i, headers[i].width, (char *) headers[i].text); | |
136 | } | |
137 | ||
138 | void | |
d19f12fd MB |
139 | PickView::note_width (PickView::Header *hdrs, HDC dc, |
140 | const std::string& string, int addend, int column) | |
97647369 | 141 | { |
82306ac2 BD |
142 | SIZE s = { 0, 0 }; |
143 | ||
144 | if (string.size()) | |
145 | GetTextExtentPoint32 (dc, string.c_str(), string.size(), &s); | |
97647369 RC |
146 | if (hdrs[column].width < s.cx + addend) |
147 | hdrs[column].width = s.cx + addend; | |
148 | } | |
149 | ||
150 | void | |
82306ac2 | 151 | PickView::cycleViewMode () |
97647369 | 152 | { |
82306ac2 | 153 | setViewMode (++view_mode); |
97647369 RC |
154 | } |
155 | ||
525531ca | 156 | void |
82306ac2 | 157 | PickView::setViewMode (views mode) |
525531ca | 158 | { |
82306ac2 BD |
159 | view_mode = mode; |
160 | set_headers (); | |
525531ca | 161 | packagedb db; |
82306ac2 BD |
162 | |
163 | contents.empty (); | |
164 | if (view_mode == PickView::views::Category) | |
525531ca | 165 | { |
82306ac2 BD |
166 | contents.ShowLabel (true); |
167 | /* start collapsed. TODO: make this a chooser flag */ | |
168 | for (packagedb::categoriesType::iterator n = | |
169 | packagedb::categories.begin(); n != packagedb::categories.end(); | |
170 | ++n) | |
7c593196 IP |
171 | insert_category (&*n, (*n).first.c_str()[0] == '.' |
172 | ? CATEGORY_EXPANDED : CATEGORY_COLLAPSED); | |
525531ca | 173 | } |
82306ac2 | 174 | else |
525531ca | 175 | { |
82306ac2 BD |
176 | contents.ShowLabel (false); |
177 | // iterate through every package | |
263157cb | 178 | for (packagedb::packagecollection::iterator i = db.packages.begin (); |
82306ac2 BD |
179 | i != db.packages.end (); ++i) |
180 | { | |
263157cb | 181 | packagemeta & pkg = *(i->second); |
82306ac2 BD |
182 | |
183 | if ( // "Full" : everything | |
184 | (view_mode == PickView::views::PackageFull) | |
185 | ||
5a2eaf00 | 186 | // "Pending" : packages that are being added/removed/upgraded |
82306ac2 BD |
187 | || (view_mode == PickView::views::Package && |
188 | ((!pkg.desired && pkg.installed) || // uninstall | |
189 | (pkg.desired && | |
190 | (pkg.desired.picked () || // install bin | |
191 | pkg.desired.sourcePackage ().picked ())))) // src | |
192 | ||
193 | // "Up to date" : installed packages that will not be changed | |
194 | || (view_mode == PickView::views::PackageKeeps && | |
195 | (pkg.installed && pkg.desired && !pkg.desired.picked () | |
196 | && !pkg.desired.sourcePackage ().picked ())) | |
197 | ||
198 | // "Not installed" | |
199 | || (view_mode == PickView::views::PackageSkips && | |
200 | (!pkg.desired && !pkg.installed))) | |
059906b3 DK |
201 | { |
202 | // Filter by package name | |
203 | if (packageFilterString.empty () | |
1789ef54 | 204 | || StrStrI (pkg.name.c_str (), packageFilterString.c_str ())) |
059906b3 DK |
205 | insert_pkg (pkg); |
206 | } | |
82306ac2 | 207 | } |
525531ca RC |
208 | } |
209 | ||
bb8e2353 | 210 | RECT r = GetClientRect (); |
525531ca RC |
211 | SCROLLINFO si; |
212 | memset (&si, 0, sizeof (si)); | |
213 | si.cbSize = sizeof (si); | |
214 | si.fMask = SIF_ALL | SIF_DISABLENOSCROLL; | |
215 | si.nMin = 0; | |
216 | si.nMax = headers[last_col].x + headers[last_col].width; // + HMARGIN; | |
217 | si.nPage = r.right; | |
218 | SetScrollInfo (GetHWND(), SB_HORZ, &si, TRUE); | |
219 | ||
220 | si.nMax = contents.itemcount () * row_height; | |
221 | si.nPage = r.bottom - header_height; | |
222 | SetScrollInfo (GetHWND(), SB_VERT, &si, TRUE); | |
223 | ||
224 | scroll_ulc_x = scroll_ulc_y = 0; | |
225 | ||
226 | InvalidateRect (GetHWND(), &r, TRUE); | |
227 | } | |
228 | ||
97647369 RC |
229 | const char * |
230 | PickView::mode_caption () | |
231 | { | |
232 | return view_mode.caption (); | |
233 | } | |
234 | ||
235 | const char * | |
236 | PickView::views::caption () | |
237 | { | |
238 | switch (_value) | |
239 | { | |
240 | case 1: | |
241 | return "Full"; | |
242 | case 2: | |
5a2eaf00 | 243 | return "Pending"; |
97647369 | 244 | case 3: |
e0aec95e MB |
245 | return "Up To Date"; |
246 | case 4: | |
247 | return "Not Installed"; | |
248 | case 5: | |
97647369 RC |
249 | return "Category"; |
250 | default: | |
251 | return ""; | |
252 | } | |
253 | } | |
254 | ||
82306ac2 BD |
255 | /* meant to be called on packagemeta::categories */ |
256 | bool | |
d19f12fd | 257 | isObsolete (set <std::string, casecompare_lt_op> &categories) |
82306ac2 | 258 | { |
d19f12fd | 259 | set <std::string, casecompare_lt_op>::const_iterator i; |
82306ac2 BD |
260 | |
261 | for (i = categories.begin (); i != categories.end (); ++i) | |
262 | if (isObsolete (*i)) | |
263 | return true; | |
264 | return false; | |
265 | } | |
266 | ||
267 | bool | |
d19f12fd | 268 | isObsolete (const std::string& catname) |
82306ac2 | 269 | { |
afa76033 MB |
270 | if (casecompare(catname, "ZZZRemovedPackages") == 0 |
271 | || casecompare(catname, "_", 1) == 0) | |
82306ac2 BD |
272 | return true; |
273 | return false; | |
274 | } | |
275 | ||
276 | /* Sets the mode for showing/hiding obsolete junk packages. */ | |
277 | void | |
278 | PickView::setObsolete (bool doit) | |
279 | { | |
280 | showObsolete = doit; | |
281 | refresh (); | |
282 | } | |
283 | ||
284 | ||
97647369 RC |
285 | void |
286 | PickView::insert_pkg (packagemeta & pkg) | |
287 | { | |
82306ac2 BD |
288 | if (!showObsolete && isObsolete (pkg.categories)) |
289 | return; | |
290 | ||
97647369 RC |
291 | if (view_mode != views::Category) |
292 | { | |
49cf3899 | 293 | PickLine & line = *new PickPackageLine (*this, pkg); |
97647369 RC |
294 | contents.insert (line); |
295 | } | |
296 | else | |
297 | { | |
d19f12fd | 298 | for (set <std::string, casecompare_lt_op>::const_iterator x |
405d7186 | 299 | = pkg.categories.begin (); x != pkg.categories.end (); ++x) |
97647369 | 300 | { |
405d7186 | 301 | // Special case - yuck |
afa76033 | 302 | if (casecompare(*x, "All") == 0) |
405d7186 RC |
303 | continue; |
304 | ||
82306ac2 | 305 | packagedb db; |
405d7186 | 306 | PickCategoryLine & catline = |
82306ac2 | 307 | *new PickCategoryLine (*this, *db.categories.find (*x), 1); |
405d7186 RC |
308 | PickLine & line = *new PickPackageLine(*this, pkg); |
309 | catline.insert (line); | |
310 | contents.insert (catline); | |
97647369 RC |
311 | } |
312 | } | |
313 | } | |
314 | ||
315 | void | |
82306ac2 | 316 | PickView::insert_category (Category *cat, bool collapsed) |
97647369 | 317 | { |
0cf68afd | 318 | // Urk, special case |
afa76033 | 319 | if (casecompare(cat->first, "All") == 0 || |
82306ac2 | 320 | (!showObsolete && isObsolete (cat->first))) |
97647369 RC |
321 | return; |
322 | PickCategoryLine & catline = *new PickCategoryLine (*this, *cat, 1, collapsed); | |
059906b3 | 323 | int packageCount = 0; |
0cf68afd RC |
324 | for (vector <packagemeta *>::iterator i = cat->second.begin (); |
325 | i != cat->second.end () ; ++i) | |
97647369 | 326 | { |
059906b3 | 327 | if (packageFilterString.empty () \ |
1789ef54 CV |
328 | || (*i |
329 | && StrStrI ((*i)->name.c_str (), packageFilterString.c_str ()))) | |
059906b3 DK |
330 | { |
331 | PickLine & line = *new PickPackageLine (*this, **i); | |
332 | catline.insert (line); | |
333 | packageCount++; | |
334 | } | |
97647369 | 335 | } |
059906b3 DK |
336 | |
337 | if (packageFilterString.empty () || packageCount) | |
338 | contents.insert (catline); | |
339 | else | |
340 | delete &catline; | |
97647369 RC |
341 | } |
342 | ||
97647369 RC |
343 | PickView::views& |
344 | PickView::views::operator++ () | |
345 | { | |
346 | ++_value; | |
347 | if (_value > Category._value) | |
348 | _value = 1; | |
349 | return *this; | |
350 | } | |
351 | ||
352 | int | |
353 | PickView::click (int row, int x) | |
354 | { | |
355 | return contents.click (0, row, x); | |
356 | } | |
357 | ||
358 | ||
359 | void | |
82306ac2 | 360 | PickView::scroll (HWND hwnd, int which, int *var, int code, int howmany = 1) |
97647369 RC |
361 | { |
362 | SCROLLINFO si; | |
363 | si.cbSize = sizeof (si); | |
e0aec95e | 364 | si.fMask = SIF_ALL | SIF_DISABLENOSCROLL; |
97647369 RC |
365 | GetScrollInfo (hwnd, which, &si); |
366 | ||
367 | switch (code) | |
368 | { | |
369 | case SB_THUMBTRACK: | |
370 | si.nPos = si.nTrackPos; | |
371 | break; | |
372 | case SB_THUMBPOSITION: | |
373 | break; | |
374 | case SB_BOTTOM: | |
375 | si.nPos = si.nMax; | |
376 | break; | |
377 | case SB_TOP: | |
378 | si.nPos = 0; | |
379 | break; | |
380 | case SB_LINEDOWN: | |
82306ac2 | 381 | si.nPos += (row_height * howmany); |
97647369 RC |
382 | break; |
383 | case SB_LINEUP: | |
82306ac2 | 384 | si.nPos -= (row_height * howmany); |
97647369 RC |
385 | break; |
386 | case SB_PAGEDOWN: | |
387 | si.nPos += si.nPage * 9 / 10; | |
388 | break; | |
389 | case SB_PAGEUP: | |
390 | si.nPos -= si.nPage * 9 / 10; | |
391 | break; | |
392 | } | |
393 | ||
394 | if ((int) si.nPos < 0) | |
395 | si.nPos = 0; | |
396 | if (si.nPos + si.nPage > (unsigned int) si.nMax) | |
397 | si.nPos = si.nMax - si.nPage; | |
398 | ||
399 | si.fMask = SIF_POS; | |
400 | SetScrollInfo (hwnd, which, &si, TRUE); | |
401 | ||
402 | int ox = scroll_ulc_x; | |
403 | int oy = scroll_ulc_y; | |
404 | *var = si.nPos; | |
405 | ||
406 | RECT cr, sr; | |
693916f8 | 407 | ::GetClientRect (hwnd, &cr); |
97647369 RC |
408 | sr = cr; |
409 | sr.top += header_height; | |
410 | UpdateWindow (hwnd); | |
411 | ScrollWindow (hwnd, ox - scroll_ulc_x, oy - scroll_ulc_y, &sr, &sr); | |
412 | /* | |
413 | sr.bottom = sr.top; | |
414 | sr.top = cr.top; | |
415 | ScrollWindow (hwnd, ox - scroll_ulc_x, 0, &sr, &sr); | |
416 | */ | |
417 | if (ox - scroll_ulc_x) | |
418 | { | |
693916f8 | 419 | ::GetClientRect (listheader, &cr); |
97647369 RC |
420 | sr = cr; |
421 | // UpdateWindow (htmp); | |
693916f8 | 422 | ::MoveWindow (listheader, -scroll_ulc_x, 0, |
97647369 RC |
423 | headers[last_col].x + |
424 | headers[last_col].width, header_height, TRUE); | |
425 | } | |
426 | UpdateWindow (hwnd); | |
427 | } | |
428 | ||
82306ac2 BD |
429 | /* this means to make the 'category' column wide enough to fit the first 'n' |
430 | categories for each package. */ | |
431 | #define NUM_CATEGORY_COL_WIDTH 2 | |
432 | ||
97647369 RC |
433 | void |
434 | PickView::init_headers (HDC dc) | |
435 | { | |
436 | int i; | |
437 | ||
438 | for (i = 0; headers[i].text; i++) | |
439 | { | |
440 | headers[i].width = 0; | |
441 | headers[i].x = 0; | |
442 | } | |
443 | ||
23761198 JT |
444 | // A margin of 3*GetSystemMetrics(SM_CXEDGE) is used at each side of the |
445 | // header text. (Probably should use that rather than hard-coding HMARGIN | |
446 | // everywhere) | |
447 | int addend = 2*3*GetSystemMetrics(SM_CXEDGE); | |
448 | ||
449 | // accommodate widths of the 'bin' and 'src' checkbox columns | |
450 | note_width (headers, dc, headers[bintick_col].text, addend, bintick_col); | |
451 | note_width (headers, dc, headers[srctick_col].text, addend, srctick_col); | |
452 | ||
82306ac2 | 453 | // accomodate the width of each category name |
97647369 | 454 | packagedb db; |
0cf68afd RC |
455 | for (packagedb::categoriesType::iterator n = packagedb::categories.begin(); |
456 | n != packagedb::categories.end(); ++n) | |
82306ac2 BD |
457 | { |
458 | if (!showObsolete && isObsolete (n->first)) | |
459 | continue; | |
460 | note_width (headers, dc, n->first, HMARGIN, cat_col); | |
461 | } | |
462 | ||
463 | /* For each package, accomodate the width of the installed version in the | |
464 | current_col, the widths of all other versions in the new_col, and the | |
465 | width of the sdesc for the pkg_col. Also, if this is not a Category | |
466 | view, adjust the 'category' column so that the first NUM_CATEGORY_COL_WIDTH | |
467 | categories from each package fits. */ | |
263157cb | 468 | for (packagedb::packagecollection::iterator n = db.packages.begin (); |
cfae3b8d | 469 | n != db.packages.end (); ++n) |
97647369 | 470 | { |
263157cb | 471 | packagemeta & pkg = *(n->second); |
82306ac2 BD |
472 | if (!showObsolete && isObsolete (pkg.categories)) |
473 | continue; | |
97647369 | 474 | if (pkg.installed) |
3c196821 | 475 | note_width (headers, dc, pkg.installed.Canonical_version (), |
97647369 | 476 | HMARGIN, current_col); |
82306ac2 BD |
477 | for (set<packageversion>::iterator i = pkg.versions.begin (); |
478 | i != pkg.versions.end (); ++i) | |
0ac305ec BD |
479 | { |
480 | if (*i != pkg.installed) | |
481 | note_width (headers, dc, i->Canonical_version (), | |
482 | HMARGIN + SPIN_WIDTH, new_col); | |
d19f12fd | 483 | std::string z = format_1000s(packageversion(*i).source ()->size); |
0ac305ec BD |
484 | note_width (headers, dc, z, HMARGIN, size_col); |
485 | z = format_1000s(packageversion(i->sourcePackage ()).source ()->size); | |
486 | note_width (headers, dc, z, HMARGIN, size_col); | |
487 | } | |
d19f12fd | 488 | std::string s = pkg.name; |
3c054baf | 489 | if (pkg.SDesc ().size()) |
d19f12fd | 490 | s += std::string (": ") + std::string(pkg.SDesc ()); |
3c054baf | 491 | note_width (headers, dc, s, HMARGIN, pkg_col); |
82306ac2 BD |
492 | |
493 | if (view_mode != PickView::views::Category && pkg.categories.size () > 2) | |
494 | { | |
d19f12fd MB |
495 | std::string compound_cat(""); |
496 | std::set<std::string, casecompare_lt_op>::const_iterator cat; | |
82306ac2 BD |
497 | size_t cnt; |
498 | ||
499 | for (cnt = 0, cat = pkg.categories.begin (); | |
500 | cnt < NUM_CATEGORY_COL_WIDTH && cat != pkg.categories.end (); | |
501 | ++cat) | |
502 | { | |
afa76033 | 503 | if (casecompare(*cat, "All") == 0) |
82306ac2 BD |
504 | continue; |
505 | if (compound_cat.size ()) | |
506 | compound_cat += ", "; | |
507 | compound_cat += *cat; | |
508 | cnt++; | |
509 | } | |
510 | note_width (headers, dc, compound_cat, HMARGIN, cat_col); | |
511 | } | |
97647369 | 512 | } |
82306ac2 BD |
513 | |
514 | // ensure that the new_col is wide enough for all the labels | |
515 | const char *captions[] = { "Uninstall", "Skip", "Reinstall", "Retrieve", | |
516 | "Source", "Keep", NULL }; | |
517 | for (int i = 0; captions[i]; i++) | |
518 | note_width (headers, dc, captions[i], HMARGIN + SPIN_WIDTH, new_col); | |
519 | ||
520 | // finally, compute the actual x values based on widths | |
97647369 RC |
521 | headers[0].x = 0; |
522 | for (i = 1; i <= last_col; i++) | |
523 | headers[i].x = headers[i - 1].x + headers[i - 1].width; | |
0baffe9a DK |
524 | // and allow for resizing to ensure the last column reaches |
525 | // all the way to the end of the chooser box. | |
526 | headers[last_col].width += total_delta_x; | |
97647369 RC |
527 | } |
528 | ||
529 | ||
693916f8 | 530 | PickView::PickView (Category &cat) : deftrust (TRUST_UNKNOWN), |
82306ac2 | 531 | contents (*this, cat, 0, false, true), showObsolete (false), |
325bb73a | 532 | packageFilterString (), hasWindowRect (false), total_delta_x (0) |
97647369 | 533 | { |
693916f8 | 534 | } |
97647369 | 535 | |
693916f8 RC |
536 | void |
537 | PickView::init(views _mode) | |
538 | { | |
539 | HDC dc = GetDC (GetHWND()); | |
97647369 RC |
540 | sysfont = GetStockObject (DEFAULT_GUI_FONT); |
541 | SelectObject (dc, sysfont); | |
542 | GetTextMetrics (dc, &tm); | |
543 | ||
544 | bitmap_dc = CreateCompatibleDC (dc); | |
82306ac2 BD |
545 | #define LI(x) LoadImage (hinstance, MAKEINTRESOURCE (x), IMAGE_BITMAP, 0, 0, 0); |
546 | bm_spin = LI (IDB_SPIN); | |
547 | bm_checkyes = LI (IDB_CHECK_YES); | |
548 | bm_checkno = LI (IDB_CHECK_NO); | |
549 | bm_checkna = LI (IDB_CHECK_NA); | |
550 | bm_treeplus = LI (IDB_TREE_PLUS); | |
551 | bm_treeminus = LI (IDB_TREE_MINUS); | |
552 | #undef LI | |
3d4c5ebb IP |
553 | icon_dc = CreateCompatibleDC (dc); |
554 | bm_icon = CreateCompatibleBitmap (dc, 11, 11); | |
555 | SelectObject (icon_dc, bm_icon); | |
556 | rect_icon = CreateRectRgn (0, 0, 11, 11); | |
97647369 RC |
557 | |
558 | row_height = (tm.tmHeight + tm.tmExternalLeading + ROW_MARGIN); | |
82306ac2 | 559 | int irh = tm.tmExternalLeading + tm.tmDescent + 11 + ROW_MARGIN; |
97647369 RC |
560 | if (row_height < irh) |
561 | row_height = irh; | |
562 | ||
97647369 RC |
563 | HDLAYOUT hdl; |
564 | WINDOWPOS wp; | |
565 | ||
566 | // Ensure that the common control DLL is loaded, and then create | |
567 | // the header control. | |
82306ac2 BD |
568 | INITCOMMONCONTROLSEX controlinfo = { sizeof (INITCOMMONCONTROLSEX), |
569 | ICC_LISTVIEW_CLASSES }; | |
97647369 RC |
570 | InitCommonControlsEx (&controlinfo); |
571 | ||
572 | if ((listheader = CreateWindowEx (0, WC_HEADER, (LPCTSTR) NULL, | |
573 | WS_CHILD | WS_BORDER | CCS_NORESIZE | | |
574 | // | HDS_BUTTONS | |
693916f8 | 575 | HDS_HORZ, 0, 0, 0, 0, GetHWND(), |
97647369 RC |
576 | (HMENU) IDC_CHOOSE_LISTHEADER, hinstance, |
577 | (LPVOID) NULL)) == NULL) | |
578 | // FIXME: throw an exception | |
579 | exit (10); | |
580 | ||
581 | // Retrieve the bounding rectangle of the parent window's | |
582 | // client area, and then request size and position values | |
583 | // from the header control. | |
bb8e2353 | 584 | RECT rcParent = GetClientRect (); |
97647369 RC |
585 | |
586 | hdl.prc = &rcParent; | |
587 | hdl.pwpos = ℘ | |
588 | if (!SendMessage (listheader, HDM_LAYOUT, 0, (LPARAM) & hdl)) | |
589 | // FIXME: throw an exception | |
590 | exit (11); | |
591 | ||
09130e58 RC |
592 | // Set the font of the listheader, but don't redraw, because its not shown |
593 | // yet.This message does not return a value, so we are not checking it as we | |
594 | // do above. | |
595 | SendMessage (listheader, WM_SETFONT, (WPARAM) sysfont, FALSE); | |
97647369 RC |
596 | |
597 | // Set the size, position, and visibility of the header control. | |
598 | SetWindowPos (listheader, wp.hwndInsertAfter, wp.x, wp.y, | |
599 | wp.cx, wp.cy, wp.flags | SWP_SHOWWINDOW); | |
600 | ||
601 | header_height = wp.cy; | |
82306ac2 | 602 | ReleaseDC (GetHWND (), dc); |
97647369 RC |
603 | |
604 | view_mode = _mode; | |
82306ac2 | 605 | refresh (); |
97647369 | 606 | } |
49cf3899 RC |
607 | |
608 | PickView::~PickView() | |
609 | { | |
3d4c5ebb IP |
610 | DeleteDC (bitmap_dc); |
611 | DeleteObject (bm_spin); | |
612 | DeleteObject (bm_checkyes); | |
613 | DeleteObject (bm_checkno); | |
614 | DeleteObject (bm_checkna); | |
615 | DeleteObject (bm_treeplus); | |
616 | DeleteObject (bm_treeminus); | |
617 | DeleteObject (rect_icon); | |
618 | DeleteObject (bm_icon); | |
619 | DeleteDC (icon_dc); | |
49cf3899 | 620 | } |
693916f8 RC |
621 | |
622 | bool PickView::registerWindowClass () | |
623 | { | |
624 | if (WindowClassAtom != 0) | |
625 | return true; | |
626 | ||
627 | // We're not registered yet | |
628 | WNDCLASSEX wc; | |
629 | ||
630 | wc.cbSize = sizeof (wc); | |
631 | // Some sensible style defaults | |
632 | wc.style = CS_HREDRAW | CS_VREDRAW; | |
633 | // Our default window procedure. This replaces itself | |
634 | // on the first call with the simpler Window::WindowProcReflector(). | |
635 | wc.lpfnWndProc = Window::FirstWindowProcReflector; | |
636 | // No class bytes | |
637 | wc.cbClsExtra = 0; | |
638 | // One pointer to REFLECTION_INFO in the extra window instance bytes | |
639 | wc.cbWndExtra = 4; | |
640 | // The app instance | |
641 | wc.hInstance = hinstance; //GetInstance (); | |
642 | // Use a bunch of system defaults for the GUI elements | |
643 | wc.hIcon = LoadIcon (0, IDI_APPLICATION); | |
644 | wc.hIconSm = NULL; | |
645 | wc.hCursor = LoadCursor (0, IDC_ARROW); | |
82306ac2 | 646 | wc.hbrBackground = NULL; |
693916f8 RC |
647 | // No menu |
648 | wc.lpszMenuName = NULL; | |
649 | // We'll get a little crazy here with the class name | |
650 | wc.lpszClassName = "listview"; | |
651 | ||
652 | // All set, try to register | |
653 | WindowClassAtom = RegisterClassEx (&wc); | |
654 | if (WindowClassAtom == 0) | |
157dc2b8 | 655 | Log (LOG_BABBLE) << "Failed to register listview " << GetLastError () << endLog; |
693916f8 RC |
656 | return WindowClassAtom != 0; |
657 | } | |
658 | ||
659 | LRESULT CALLBACK | |
660 | PickView::list_vscroll (HWND hwnd, HWND hctl, UINT code, int pos) | |
661 | { | |
d2be933d | 662 | scroll (hwnd, SB_VERT, &scroll_ulc_y, code); |
693916f8 RC |
663 | return 0; |
664 | } | |
665 | ||
666 | LRESULT CALLBACK | |
667 | PickView::list_hscroll (HWND hwnd, HWND hctl, UINT code, int pos) | |
668 | { | |
d2be933d | 669 | scroll (hwnd, SB_HORZ, &scroll_ulc_x, code); |
693916f8 RC |
670 | return 0; |
671 | } | |
672 | ||
325bb73a DK |
673 | void |
674 | PickView::set_vscroll_info (const RECT &r) | |
675 | { | |
676 | SCROLLINFO si; | |
677 | memset (&si, 0, sizeof (si)); | |
678 | si.cbSize = sizeof (si); | |
679 | si.fMask = SIF_ALL | SIF_DISABLENOSCROLL; /* SIF_RANGE was giving strange behaviour */ | |
680 | si.nMin = 0; | |
681 | ||
682 | si.nMax = contents.itemcount () * row_height; | |
683 | si.nPage = r.bottom - header_height; | |
684 | ||
685 | /* if we are under the minimum display count , | |
686 | * set the offset to 0 | |
687 | */ | |
688 | if ((unsigned int) si.nMax <= si.nPage) | |
689 | scroll_ulc_y = 0; | |
690 | si.nPos = scroll_ulc_y; | |
691 | ||
692 | SetScrollInfo (GetHWND(), SB_VERT, &si, TRUE); | |
693 | } | |
694 | ||
693916f8 RC |
695 | LRESULT CALLBACK |
696 | PickView::list_click (HWND hwnd, BOOL dblclk, int x, int y, UINT hitCode) | |
697 | { | |
c6bf266d | 698 | int row, refresh __attribute__ ((unused)); |
693916f8 | 699 | |
d2be933d | 700 | if (contents.itemcount () == 0) |
693916f8 RC |
701 | return 0; |
702 | ||
d2be933d | 703 | if (y < header_height) |
693916f8 | 704 | return 0; |
d2be933d RC |
705 | x += scroll_ulc_x; |
706 | y += scroll_ulc_y - header_height; | |
693916f8 | 707 | |
d2be933d | 708 | row = (y + ROW_MARGIN / 2) / row_height; |
693916f8 | 709 | |
d2be933d | 710 | if (row < 0 || row >= contents.itemcount ()) |
693916f8 RC |
711 | return 0; |
712 | ||
d2be933d | 713 | refresh = click (row, x); |
693916f8 | 714 | |
325bb73a | 715 | // XXX we need a method to query the database to see if more |
693916f8 RC |
716 | // than just one package has changed! Until then... |
717 | #if 0 | |
718 | if (refresh) | |
719 | { | |
720 | #endif | |
bb8e2353 | 721 | RECT r = GetClientRect (); |
325bb73a | 722 | set_vscroll_info (r); |
9c9cfce7 | 723 | InvalidateRect (GetHWND(), &r, TRUE); |
693916f8 RC |
724 | #if 0 |
725 | } | |
726 | else | |
727 | { | |
728 | RECT rect; | |
729 | rect.left = | |
d2be933d | 730 | headers[new_col].x - scroll_ulc_x; |
693916f8 | 731 | rect.right = |
d2be933d | 732 | headers[src_col + 1].x - scroll_ulc_x; |
693916f8 | 733 | rect.top = |
d2be933d RC |
734 | header_height + row * row_height - |
735 | scroll_ulc_y; | |
736 | rect.bottom = rect.top + row_height; | |
693916f8 RC |
737 | InvalidateRect (hwnd, &rect, TRUE); |
738 | } | |
739 | #endif | |
740 | return 0; | |
741 | } | |
742 | ||
743 | /* | |
744 | * LRESULT CALLBACK | |
745 | * PickView::listview_proc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) | |
746 | */ | |
747 | LRESULT | |
748 | PickView::WindowProc (UINT message, WPARAM wParam, LPARAM lParam) | |
749 | { | |
82306ac2 BD |
750 | int wheel_notches; |
751 | UINT wheel_lines; | |
752 | ||
693916f8 RC |
753 | switch (message) |
754 | { | |
755 | case WM_HSCROLL: | |
4875ac88 MB |
756 | list_hscroll (GetHWND(), (HWND)lParam, LOWORD(wParam), HIWORD(wParam)); |
757 | return 0; | |
693916f8 | 758 | case WM_VSCROLL: |
4875ac88 MB |
759 | list_vscroll (GetHWND(), (HWND)lParam, LOWORD(wParam), HIWORD(wParam)); |
760 | return 0; | |
82306ac2 BD |
761 | case WM_MOUSEWHEEL: |
762 | // this is how many 'notches' the wheel scrolled, forward/up = positive | |
763 | wheel_notches = GET_WHEEL_DELTA_WPARAM(wParam) / 120; | |
764 | ||
765 | // determine how many lines the user has configred for a mouse scroll | |
766 | SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &wheel_lines, 0); | |
767 | ||
768 | if (wheel_lines == 0) // do no scrolling | |
769 | return 0; | |
770 | else if (wheel_lines == WHEEL_PAGESCROLL) | |
771 | scroll (GetHWND (), SB_VERT, &scroll_ulc_y, (wheel_notches > 0) ? | |
772 | SB_PAGEUP : SB_PAGEDOWN); | |
773 | else | |
774 | scroll (GetHWND (), SB_VERT, &scroll_ulc_y, (wheel_notches > 0) ? | |
775 | SB_LINEUP : SB_LINEDOWN, wheel_lines * abs (wheel_notches)); | |
776 | return 0; // handled | |
693916f8 | 777 | case WM_LBUTTONDOWN: |
4875ac88 MB |
778 | list_click (GetHWND(), FALSE, LOWORD(lParam), HIWORD(lParam), wParam); |
779 | return 0; | |
693916f8 RC |
780 | case WM_PAINT: |
781 | paint (GetHWND()); | |
782 | return 0; | |
783 | case WM_NOTIFY: | |
784 | { | |
82306ac2 BD |
785 | // pnmh = (LPNMHDR) lParam |
786 | LPNMHEADER phdr = (LPNMHEADER) lParam; | |
787 | switch (phdr->hdr.code) | |
788 | { | |
789 | case HDN_ITEMCHANGED: | |
790 | if (phdr->hdr.hwndFrom == ListHeader ()) | |
791 | { | |
792 | if (phdr->pitem && phdr->pitem->mask & HDI_WIDTH) | |
793 | headers[phdr->iItem].width = phdr->pitem->cxy; | |
794 | ||
795 | for (int i = 1; i <= last_col; i++) | |
796 | headers[i].x = headers[i - 1].x + headers[i - 1].width; | |
797 | ||
798 | RECT r = GetClientRect (); | |
799 | SCROLLINFO si; | |
800 | si.cbSize = sizeof (si); | |
801 | si.fMask = SIF_ALL | SIF_DISABLENOSCROLL; | |
802 | GetScrollInfo (GetHWND(), SB_HORZ, &si); | |
803 | ||
804 | int oldMax = si.nMax; | |
805 | si.nMax = headers[last_col].x + headers[last_col].width; | |
806 | if (si.nTrackPos && oldMax > si.nMax) | |
807 | si.nTrackPos += si.nMax - oldMax; | |
808 | ||
809 | si.nPage = r.right; | |
810 | SetScrollInfo (GetHWND(), SB_HORZ, &si, TRUE); | |
811 | InvalidateRect (GetHWND(), &r, TRUE); | |
812 | if (si.nTrackPos && oldMax > si.nMax) | |
813 | scroll (GetHWND(), SB_HORZ, &scroll_ulc_x, SB_THUMBTRACK); | |
814 | } | |
815 | break; | |
816 | } | |
817 | } | |
818 | break; | |
ee91d9be RC |
819 | case WM_SIZE: |
820 | { | |
821 | // Note: WM_SIZE msgs only appear when 'just' scrolling the window | |
325bb73a | 822 | RECT windowRect = GetWindowRect (); |
325bb73a | 823 | if (hasWindowRect) |
82306ac2 BD |
824 | { |
825 | int dx; | |
325bb73a DK |
826 | if ((dx = windowRect.right - windowRect.left - |
827 | lastWindowRect.width ()) != 0) | |
82306ac2 | 828 | { |
0baffe9a DK |
829 | cat_headers[set_header_column_order (views::Category)].width += dx; |
830 | pkg_headers[set_header_column_order (views::Package)].width += dx; | |
831 | set_header_column_order (view_mode); | |
82306ac2 BD |
832 | set_headers (); |
833 | ::MoveWindow (listheader, -scroll_ulc_x, 0, | |
834 | headers[last_col].x + | |
835 | headers[last_col].width, header_height, TRUE); | |
0baffe9a | 836 | total_delta_x += dx; |
82306ac2 | 837 | } |
325bb73a DK |
838 | if (windowRect.bottom - windowRect.top - lastWindowRect.height ()) |
839 | set_vscroll_info (GetClientRect ()); | |
82306ac2 BD |
840 | } |
841 | else | |
325bb73a | 842 | hasWindowRect = true; |
82306ac2 | 843 | |
325bb73a | 844 | lastWindowRect = windowRect; |
82306ac2 | 845 | return 0; |
ee91d9be | 846 | } |
693916f8 | 847 | } |
82306ac2 BD |
848 | |
849 | // default: can't handle this message | |
850 | return DefWindowProc (GetHWND(), message, wParam, lParam); | |
693916f8 RC |
851 | } |
852 | ||
3d4c5ebb IP |
853 | //// |
854 | // Turn black into foreground color and white into background color by | |
855 | // 1) Filling a square with ~(FG^BG) | |
856 | // 2) Blitting the bitmap on it with NOTSRCERASE (white->black; black->FG^BG) | |
857 | // 3) Blitting the result on BG with SRCINVERT (white->BG; black->FG) | |
858 | void | |
859 | PickView::DrawIcon (HDC hdc, int x, int y, HANDLE hIcon) | |
860 | { | |
861 | SelectObject (bitmap_dc, hIcon); | |
862 | FillRgn (icon_dc, rect_icon, bg_fg_brush); | |
863 | BitBlt (icon_dc, 0, 0, 11, 11, bitmap_dc, 0, 0, NOTSRCERASE); | |
864 | BitBlt (hdc, x, y, 11, 11, icon_dc, 0, 0, SRCINVERT); | |
865 | ///////////// On WinNT-based systems, we could've done the below instead | |
866 | ///////////// See http://support.microsoft.com/default.aspx?scid=kb;en-us;79212 | |
867 | // SelectObject (hdc, GetSysColorBrush (COLOR_WINDOWTEXT)); | |
868 | // HBITMAP bm_icon = CreateBitmap (11, 11, 1, 1, NULL); | |
869 | // SelectObject (icon_dc, bm_icon); | |
870 | // BitBlt (icon_dc, 0, 0, 11, 11, bitmap_dc, 0, 0, SRCCOPY); | |
871 | // MaskBlt (hdc, x2, by, 11, 11, bitmap_dc, 0, 0, bm_icon, 0, 0, MAKEROP4 (SRCAND, PATCOPY)); | |
872 | // DeleteObject (bm_icon); | |
873 | } | |
874 | ||
693916f8 RC |
875 | void |
876 | PickView::paint (HWND hwnd) | |
877 | { | |
82306ac2 BD |
878 | // we want to retrieve the update region before calling BeginPaint, |
879 | // because after we do that the update region is validated and we can | |
880 | // no longer retrieve it | |
881 | HRGN hUpdRgn = CreateRectRgn (0, 0, 0, 0); | |
693916f8 | 882 | |
82306ac2 BD |
883 | if (GetUpdateRgn (hwnd, hUpdRgn, FALSE) == 0) |
884 | { | |
885 | // error? | |
886 | return; | |
887 | } | |
693916f8 | 888 | |
82306ac2 BD |
889 | // tell the system that we're going to begin painting our window |
890 | // it will prevent further WM_PAINT messages from arriving until we're | |
891 | // done, and if any part of our window was invalidated while we are | |
892 | // painting, it will retrigger us so that we can fix it | |
893 | PAINTSTRUCT ps; | |
894 | HDC hdc = BeginPaint (hwnd, &ps); | |
895 | ||
d2be933d | 896 | SelectObject (hdc, sysfont); |
693916f8 | 897 | SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)); |
3d4c5ebb | 898 | SetBkColor (hdc, GetSysColor (COLOR_WINDOW)); |
82306ac2 | 899 | FillRgn (hdc, hUpdRgn, GetSysColorBrush(COLOR_WINDOW)); |
693916f8 | 900 | |
3d4c5ebb IP |
901 | COLORREF clr = ~GetSysColor (COLOR_WINDOW) ^ GetSysColor (COLOR_WINDOWTEXT); |
902 | clr = RGB (GetRValue (clr), GetGValue (clr), GetBValue (clr)); // reconvert | |
903 | bg_fg_brush = CreateSolidBrush (clr); | |
904 | ||
693916f8 RC |
905 | RECT cr; |
906 | ::GetClientRect (hwnd, &cr); | |
907 | ||
82306ac2 BD |
908 | int x = cr.left - scroll_ulc_x; |
909 | int y = cr.top - scroll_ulc_y + header_height; | |
693916f8 | 910 | |
82306ac2 BD |
911 | contents.paint (hdc, hUpdRgn, x, y, 0, (view_mode == |
912 | PickView::views::Category) ? 0 : 1); | |
693916f8 | 913 | |
d2be933d | 914 | if (contents.itemcount () == 0) |
693916f8 RC |
915 | { |
916 | static const char *msg = "Nothing to Install/Update"; | |
917 | if (source == IDC_SOURCE_DOWNLOAD) | |
82306ac2 | 918 | msg = "Nothing to Download"; |
ee91d9be | 919 | TextOut (hdc, x + HMARGIN, y, msg, strlen (msg)); |
693916f8 RC |
920 | } |
921 | ||
82306ac2 | 922 | DeleteObject (hUpdRgn); |
3d4c5ebb | 923 | DeleteObject (bg_fg_brush); |
693916f8 RC |
924 | EndPaint (hwnd, &ps); |
925 | } | |
926 | ||
927 | ||
928 | bool | |
929 | PickView::Create (Window * parent, DWORD Style, RECT *r) | |
930 | { | |
931 | ||
932 | // First register the window class, if we haven't already | |
933 | if (!registerWindowClass ()) | |
934 | { | |
935 | // Registration failed | |
936 | return false; | |
937 | } | |
938 | ||
82306ac2 | 939 | // Save our parent, we'll probably need it eventually. |
693916f8 RC |
940 | setParent(parent); |
941 | ||
942 | // Create the window instance | |
82306ac2 BD |
943 | CreateWindowEx (// Extended Style |
944 | WS_EX_CLIENTEDGE, | |
945 | // window class atom (name) | |
946 | "listview", //MAKEINTATOM(WindowClassAtom), | |
947 | "listviewwindow", // no title-bar string yet | |
948 | // Style bits | |
693916f8 | 949 | Style, |
82306ac2 BD |
950 | r ? r->left : CW_USEDEFAULT, |
951 | r ? r->top : CW_USEDEFAULT, | |
952 | r ? r->right - r->left + 1 : CW_USEDEFAULT, | |
953 | r ? r->bottom - r->top + 1 : CW_USEDEFAULT, | |
954 | // Parent Window | |
955 | parent == NULL ? (HWND)NULL : parent->GetHWND (), | |
956 | // use class menu | |
957 | (HMENU) MAKEINTRESOURCE (IDC_CHOOSE_LIST), | |
958 | // The application instance | |
959 | GetInstance (), | |
960 | // The this ptr, which we'll use to set up | |
961 | // the WindowProc reflection. | |
693916f8 | 962 | reinterpret_cast<void *>((Window *)this)); |
693916f8 RC |
963 | if (GetHWND() == NULL) |
964 | { | |
157dc2b8 | 965 | Log (LOG_BABBLE) << "Failed to create PickView " << GetLastError () << endLog; |
693916f8 RC |
966 | return false; |
967 | } | |
968 | ||
969 | return true; | |
970 | } | |
22120c90 RC |
971 | |
972 | void | |
973 | PickView::defaultTrust (trusts trust) | |
974 | { | |
975 | this->deftrust = trust; | |
db9818d7 | 976 | |
22120c90 | 977 | packagedb db; |
db9818d7 JT |
978 | db.defaultTrust(trust); |
979 | ||
980 | // force the picker to redraw | |
bb8e2353 | 981 | RECT r = GetClientRect (); |
22120c90 | 982 | InvalidateRect (this->GetHWND(), &r, TRUE); |
22120c90 | 983 | } |
ec2dbbf0 | 984 | |
82306ac2 | 985 | /* This recalculates all column widths and resets the view */ |
ec2dbbf0 RC |
986 | void |
987 | PickView::refresh() | |
988 | { | |
82306ac2 BD |
989 | HDC dc = GetDC (GetHWND ()); |
990 | ||
991 | // we must set the font of the DC here, otherwise the width calculations | |
992 | // will be off because the system will use the wrong font metrics | |
993 | sysfont = GetStockObject (DEFAULT_GUI_FONT); | |
994 | SelectObject (dc, sysfont); | |
995 | ||
996 | // init headers for the current mode | |
997 | set_headers (); | |
998 | init_headers (dc); | |
999 | ||
1000 | // save the current mode | |
1001 | views cur_view_mode = view_mode; | |
1002 | ||
1003 | // switch to the other type and do those headers | |
1004 | view_mode = (view_mode == PickView::views::Category) ? | |
1005 | PickView::views::PackageFull : PickView::views::Category; | |
1006 | set_headers (); | |
1007 | init_headers (dc); | |
1008 | ReleaseDC (GetHWND (), dc); | |
1009 | ||
1010 | view_mode = cur_view_mode; | |
1011 | setViewMode (view_mode); | |
ec2dbbf0 | 1012 | } |