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