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