]> cygwin.com Git - cygwin-apps/setup.git/blame - PickView.cc
Use solver to check for problems and produce a list of package transactions
[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"
97647369
RC
24#include "dialog.h"
25#include "resource.h"
693916f8
RC
26/* For 'source' */
27#include "state.h"
28#include "LogSingleton.h"
27820085 29#include "Exception.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 &&
4209699d
JT
178 (pkg.picked () || // install bin
179 pkg.srcpicked ())))) // src
82306ac2
BD
180
181 // "Up to date" : installed packages that will not be changed
182 || (view_mode == PickView::views::PackageKeeps &&
4209699d
JT
183 (pkg.installed && pkg.desired && !pkg.picked ()
184 && !pkg.srcpicked ()))
82306ac2
BD
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
359ef440 494PickView::PickView (Category &cat) : deftrust (TRUST_CURR),
82306ac2 495contents (*this, cat, 0, false, true), showObsolete (false),
325bb73a 496packageFilterString (), hasWindowRect (false), total_delta_x (0)
97647369 497{
693916f8 498}
97647369 499
693916f8
RC
500void
501PickView::init(views _mode)
502{
503 HDC dc = GetDC (GetHWND());
97647369
RC
504 sysfont = GetStockObject (DEFAULT_GUI_FONT);
505 SelectObject (dc, sysfont);
506 GetTextMetrics (dc, &tm);
507
508 bitmap_dc = CreateCompatibleDC (dc);
82306ac2
BD
509#define LI(x) LoadImage (hinstance, MAKEINTRESOURCE (x), IMAGE_BITMAP, 0, 0, 0);
510 bm_spin = LI (IDB_SPIN);
511 bm_checkyes = LI (IDB_CHECK_YES);
512 bm_checkno = LI (IDB_CHECK_NO);
513 bm_checkna = LI (IDB_CHECK_NA);
514 bm_treeplus = LI (IDB_TREE_PLUS);
515 bm_treeminus = LI (IDB_TREE_MINUS);
516#undef LI
3d4c5ebb
IP
517 icon_dc = CreateCompatibleDC (dc);
518 bm_icon = CreateCompatibleBitmap (dc, 11, 11);
519 SelectObject (icon_dc, bm_icon);
520 rect_icon = CreateRectRgn (0, 0, 11, 11);
97647369
RC
521
522 row_height = (tm.tmHeight + tm.tmExternalLeading + ROW_MARGIN);
82306ac2 523 int irh = tm.tmExternalLeading + tm.tmDescent + 11 + ROW_MARGIN;
97647369
RC
524 if (row_height < irh)
525 row_height = irh;
526
97647369
RC
527 HDLAYOUT hdl;
528 WINDOWPOS wp;
529
530 // Ensure that the common control DLL is loaded, and then create
531 // the header control.
82306ac2
BD
532 INITCOMMONCONTROLSEX controlinfo = { sizeof (INITCOMMONCONTROLSEX),
533 ICC_LISTVIEW_CLASSES };
97647369
RC
534 InitCommonControlsEx (&controlinfo);
535
536 if ((listheader = CreateWindowEx (0, WC_HEADER, (LPCTSTR) NULL,
537 WS_CHILD | WS_BORDER | CCS_NORESIZE |
538 // | HDS_BUTTONS
693916f8 539 HDS_HORZ, 0, 0, 0, 0, GetHWND(),
97647369
RC
540 (HMENU) IDC_CHOOSE_LISTHEADER, hinstance,
541 (LPVOID) NULL)) == NULL)
27820085
KB
542 throw new Exception (TOSTRING(__LINE__) " " __FILE__,
543 "Unable to create list header window",
544 APPERR_WINDOW_ERROR);
97647369
RC
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))
27820085
KB
554 throw new Exception (TOSTRING(__LINE__) " " __FILE__,
555 "Unable to get size and position of rectangle",
556 APPERR_WINDOW_ERROR);
97647369 557
09130e58
RC
558 // Set the font of the listheader, but don't redraw, because its not shown
559 // yet.This message does not return a value, so we are not checking it as we
560 // do above.
561 SendMessage (listheader, WM_SETFONT, (WPARAM) sysfont, FALSE);
97647369
RC
562
563 // Set the size, position, and visibility of the header control.
564 SetWindowPos (listheader, wp.hwndInsertAfter, wp.x, wp.y,
565 wp.cx, wp.cy, wp.flags | SWP_SHOWWINDOW);
566
567 header_height = wp.cy;
82306ac2 568 ReleaseDC (GetHWND (), dc);
97647369
RC
569
570 view_mode = _mode;
82306ac2 571 refresh ();
97647369 572}
49cf3899
RC
573
574PickView::~PickView()
575{
3d4c5ebb
IP
576 DeleteDC (bitmap_dc);
577 DeleteObject (bm_spin);
578 DeleteObject (bm_checkyes);
579 DeleteObject (bm_checkno);
580 DeleteObject (bm_checkna);
581 DeleteObject (bm_treeplus);
582 DeleteObject (bm_treeminus);
583 DeleteObject (rect_icon);
584 DeleteObject (bm_icon);
585 DeleteDC (icon_dc);
49cf3899 586}
693916f8
RC
587
588bool PickView::registerWindowClass ()
589{
590 if (WindowClassAtom != 0)
591 return true;
592
593 // We're not registered yet
594 WNDCLASSEX wc;
595
596 wc.cbSize = sizeof (wc);
597 // Some sensible style defaults
598 wc.style = CS_HREDRAW | CS_VREDRAW;
599 // Our default window procedure. This replaces itself
600 // on the first call with the simpler Window::WindowProcReflector().
601 wc.lpfnWndProc = Window::FirstWindowProcReflector;
602 // No class bytes
603 wc.cbClsExtra = 0;
604 // One pointer to REFLECTION_INFO in the extra window instance bytes
605 wc.cbWndExtra = 4;
606 // The app instance
607 wc.hInstance = hinstance; //GetInstance ();
608 // Use a bunch of system defaults for the GUI elements
609 wc.hIcon = LoadIcon (0, IDI_APPLICATION);
610 wc.hIconSm = NULL;
611 wc.hCursor = LoadCursor (0, IDC_ARROW);
82306ac2 612 wc.hbrBackground = NULL;
693916f8
RC
613 // No menu
614 wc.lpszMenuName = NULL;
615 // We'll get a little crazy here with the class name
616 wc.lpszClassName = "listview";
617
618 // All set, try to register
619 WindowClassAtom = RegisterClassEx (&wc);
620 if (WindowClassAtom == 0)
157dc2b8 621 Log (LOG_BABBLE) << "Failed to register listview " << GetLastError () << endLog;
693916f8
RC
622 return WindowClassAtom != 0;
623}
624
625LRESULT CALLBACK
626PickView::list_vscroll (HWND hwnd, HWND hctl, UINT code, int pos)
627{
d2be933d 628 scroll (hwnd, SB_VERT, &scroll_ulc_y, code);
693916f8
RC
629 return 0;
630}
631
632LRESULT CALLBACK
633PickView::list_hscroll (HWND hwnd, HWND hctl, UINT code, int pos)
634{
d2be933d 635 scroll (hwnd, SB_HORZ, &scroll_ulc_x, code);
693916f8
RC
636 return 0;
637}
638
325bb73a
DK
639void
640PickView::set_vscroll_info (const RECT &r)
641{
642 SCROLLINFO si;
643 memset (&si, 0, sizeof (si));
644 si.cbSize = sizeof (si);
645 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL; /* SIF_RANGE was giving strange behaviour */
646 si.nMin = 0;
647
648 si.nMax = contents.itemcount () * row_height;
649 si.nPage = r.bottom - header_height;
650
651 /* if we are under the minimum display count ,
652 * set the offset to 0
653 */
654 if ((unsigned int) si.nMax <= si.nPage)
655 scroll_ulc_y = 0;
656 si.nPos = scroll_ulc_y;
657
658 SetScrollInfo (GetHWND(), SB_VERT, &si, TRUE);
659}
660
693916f8
RC
661LRESULT CALLBACK
662PickView::list_click (HWND hwnd, BOOL dblclk, int x, int y, UINT hitCode)
663{
c6bf266d 664 int row, refresh __attribute__ ((unused));
693916f8 665
d2be933d 666 if (contents.itemcount () == 0)
693916f8
RC
667 return 0;
668
d2be933d 669 if (y < header_height)
693916f8 670 return 0;
d2be933d
RC
671 x += scroll_ulc_x;
672 y += scroll_ulc_y - header_height;
693916f8 673
d2be933d 674 row = (y + ROW_MARGIN / 2) / row_height;
693916f8 675
d2be933d 676 if (row < 0 || row >= contents.itemcount ())
693916f8
RC
677 return 0;
678
d2be933d 679 refresh = click (row, x);
693916f8 680
325bb73a 681 // XXX we need a method to query the database to see if more
693916f8
RC
682 // than just one package has changed! Until then...
683#if 0
684 if (refresh)
685 {
686#endif
bb8e2353 687 RECT r = GetClientRect ();
325bb73a 688 set_vscroll_info (r);
9c9cfce7 689 InvalidateRect (GetHWND(), &r, TRUE);
693916f8
RC
690#if 0
691 }
692 else
693 {
694 RECT rect;
695 rect.left =
d2be933d 696 headers[new_col].x - scroll_ulc_x;
693916f8 697 rect.right =
d2be933d 698 headers[src_col + 1].x - scroll_ulc_x;
693916f8 699 rect.top =
d2be933d
RC
700 header_height + row * row_height -
701 scroll_ulc_y;
702 rect.bottom = rect.top + row_height;
693916f8
RC
703 InvalidateRect (hwnd, &rect, TRUE);
704 }
705#endif
706 return 0;
707}
708
709/*
710 * LRESULT CALLBACK
711 * PickView::listview_proc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
712 */
713LRESULT
714PickView::WindowProc (UINT message, WPARAM wParam, LPARAM lParam)
715{
82306ac2
BD
716 int wheel_notches;
717 UINT wheel_lines;
718
693916f8
RC
719 switch (message)
720 {
721 case WM_HSCROLL:
4875ac88
MB
722 list_hscroll (GetHWND(), (HWND)lParam, LOWORD(wParam), HIWORD(wParam));
723 return 0;
693916f8 724 case WM_VSCROLL:
4875ac88
MB
725 list_vscroll (GetHWND(), (HWND)lParam, LOWORD(wParam), HIWORD(wParam));
726 return 0;
82306ac2
BD
727 case WM_MOUSEWHEEL:
728 // this is how many 'notches' the wheel scrolled, forward/up = positive
729 wheel_notches = GET_WHEEL_DELTA_WPARAM(wParam) / 120;
730
731 // determine how many lines the user has configred for a mouse scroll
732 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &wheel_lines, 0);
733
734 if (wheel_lines == 0) // do no scrolling
735 return 0;
736 else if (wheel_lines == WHEEL_PAGESCROLL)
737 scroll (GetHWND (), SB_VERT, &scroll_ulc_y, (wheel_notches > 0) ?
738 SB_PAGEUP : SB_PAGEDOWN);
739 else
740 scroll (GetHWND (), SB_VERT, &scroll_ulc_y, (wheel_notches > 0) ?
741 SB_LINEUP : SB_LINEDOWN, wheel_lines * abs (wheel_notches));
742 return 0; // handled
693916f8 743 case WM_LBUTTONDOWN:
4875ac88
MB
744 list_click (GetHWND(), FALSE, LOWORD(lParam), HIWORD(lParam), wParam);
745 return 0;
693916f8
RC
746 case WM_PAINT:
747 paint (GetHWND());
748 return 0;
749 case WM_NOTIFY:
750 {
82306ac2
BD
751 // pnmh = (LPNMHDR) lParam
752 LPNMHEADER phdr = (LPNMHEADER) lParam;
753 switch (phdr->hdr.code)
754 {
755 case HDN_ITEMCHANGED:
756 if (phdr->hdr.hwndFrom == ListHeader ())
757 {
758 if (phdr->pitem && phdr->pitem->mask & HDI_WIDTH)
759 headers[phdr->iItem].width = phdr->pitem->cxy;
760
761 for (int i = 1; i <= last_col; i++)
762 headers[i].x = headers[i - 1].x + headers[i - 1].width;
763
764 RECT r = GetClientRect ();
765 SCROLLINFO si;
766 si.cbSize = sizeof (si);
767 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
768 GetScrollInfo (GetHWND(), SB_HORZ, &si);
769
770 int oldMax = si.nMax;
771 si.nMax = headers[last_col].x + headers[last_col].width;
772 if (si.nTrackPos && oldMax > si.nMax)
773 si.nTrackPos += si.nMax - oldMax;
774
775 si.nPage = r.right;
776 SetScrollInfo (GetHWND(), SB_HORZ, &si, TRUE);
777 InvalidateRect (GetHWND(), &r, TRUE);
778 if (si.nTrackPos && oldMax > si.nMax)
779 scroll (GetHWND(), SB_HORZ, &scroll_ulc_x, SB_THUMBTRACK);
780 }
781 break;
782 }
783 }
784 break;
ee91d9be
RC
785 case WM_SIZE:
786 {
787 // Note: WM_SIZE msgs only appear when 'just' scrolling the window
325bb73a 788 RECT windowRect = GetWindowRect ();
325bb73a 789 if (hasWindowRect)
82306ac2
BD
790 {
791 int dx;
325bb73a
DK
792 if ((dx = windowRect.right - windowRect.left -
793 lastWindowRect.width ()) != 0)
82306ac2 794 {
0baffe9a 795 cat_headers[set_header_column_order (views::Category)].width += dx;
fa8967ca 796 pkg_headers[set_header_column_order (views::PackagePending)].width += dx;
0baffe9a 797 set_header_column_order (view_mode);
82306ac2
BD
798 set_headers ();
799 ::MoveWindow (listheader, -scroll_ulc_x, 0,
800 headers[last_col].x +
801 headers[last_col].width, header_height, TRUE);
0baffe9a 802 total_delta_x += dx;
82306ac2 803 }
325bb73a
DK
804 if (windowRect.bottom - windowRect.top - lastWindowRect.height ())
805 set_vscroll_info (GetClientRect ());
82306ac2
BD
806 }
807 else
325bb73a 808 hasWindowRect = true;
82306ac2 809
325bb73a 810 lastWindowRect = windowRect;
82306ac2 811 return 0;
ee91d9be 812 }
fdad758c
JT
813 case WM_MOUSEACTIVATE:
814 SetFocus(GetHWND());
815 return MA_ACTIVATE;
693916f8 816 }
82306ac2
BD
817
818 // default: can't handle this message
819 return DefWindowProc (GetHWND(), message, wParam, lParam);
693916f8
RC
820}
821
3d4c5ebb
IP
822////
823// Turn black into foreground color and white into background color by
824// 1) Filling a square with ~(FG^BG)
825// 2) Blitting the bitmap on it with NOTSRCERASE (white->black; black->FG^BG)
826// 3) Blitting the result on BG with SRCINVERT (white->BG; black->FG)
827void
828PickView::DrawIcon (HDC hdc, int x, int y, HANDLE hIcon)
829{
830 SelectObject (bitmap_dc, hIcon);
831 FillRgn (icon_dc, rect_icon, bg_fg_brush);
832 BitBlt (icon_dc, 0, 0, 11, 11, bitmap_dc, 0, 0, NOTSRCERASE);
833 BitBlt (hdc, x, y, 11, 11, icon_dc, 0, 0, SRCINVERT);
834///////////// On WinNT-based systems, we could've done the below instead
835///////////// See http://support.microsoft.com/default.aspx?scid=kb;en-us;79212
836// SelectObject (hdc, GetSysColorBrush (COLOR_WINDOWTEXT));
837// HBITMAP bm_icon = CreateBitmap (11, 11, 1, 1, NULL);
838// SelectObject (icon_dc, bm_icon);
839// BitBlt (icon_dc, 0, 0, 11, 11, bitmap_dc, 0, 0, SRCCOPY);
840// MaskBlt (hdc, x2, by, 11, 11, bitmap_dc, 0, 0, bm_icon, 0, 0, MAKEROP4 (SRCAND, PATCOPY));
841// DeleteObject (bm_icon);
842}
843
693916f8
RC
844void
845PickView::paint (HWND hwnd)
846{
82306ac2
BD
847 // we want to retrieve the update region before calling BeginPaint,
848 // because after we do that the update region is validated and we can
849 // no longer retrieve it
850 HRGN hUpdRgn = CreateRectRgn (0, 0, 0, 0);
693916f8 851
82306ac2
BD
852 if (GetUpdateRgn (hwnd, hUpdRgn, FALSE) == 0)
853 {
854 // error?
855 return;
856 }
693916f8 857
82306ac2
BD
858 // tell the system that we're going to begin painting our window
859 // it will prevent further WM_PAINT messages from arriving until we're
860 // done, and if any part of our window was invalidated while we are
861 // painting, it will retrigger us so that we can fix it
862 PAINTSTRUCT ps;
863 HDC hdc = BeginPaint (hwnd, &ps);
864
d2be933d 865 SelectObject (hdc, sysfont);
693916f8 866 SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT));
3d4c5ebb 867 SetBkColor (hdc, GetSysColor (COLOR_WINDOW));
82306ac2 868 FillRgn (hdc, hUpdRgn, GetSysColorBrush(COLOR_WINDOW));
693916f8 869
3d4c5ebb
IP
870 COLORREF clr = ~GetSysColor (COLOR_WINDOW) ^ GetSysColor (COLOR_WINDOWTEXT);
871 clr = RGB (GetRValue (clr), GetGValue (clr), GetBValue (clr)); // reconvert
872 bg_fg_brush = CreateSolidBrush (clr);
873
693916f8
RC
874 RECT cr;
875 ::GetClientRect (hwnd, &cr);
876
82306ac2
BD
877 int x = cr.left - scroll_ulc_x;
878 int y = cr.top - scroll_ulc_y + header_height;
693916f8 879
82306ac2
BD
880 contents.paint (hdc, hUpdRgn, x, y, 0, (view_mode ==
881 PickView::views::Category) ? 0 : 1);
693916f8 882
d2be933d 883 if (contents.itemcount () == 0)
693916f8
RC
884 {
885 static const char *msg = "Nothing to Install/Update";
886 if (source == IDC_SOURCE_DOWNLOAD)
82306ac2 887 msg = "Nothing to Download";
ee91d9be 888 TextOut (hdc, x + HMARGIN, y, msg, strlen (msg));
693916f8
RC
889 }
890
82306ac2 891 DeleteObject (hUpdRgn);
3d4c5ebb 892 DeleteObject (bg_fg_brush);
693916f8
RC
893 EndPaint (hwnd, &ps);
894}
895
896
897bool
898PickView::Create (Window * parent, DWORD Style, RECT *r)
899{
900
901 // First register the window class, if we haven't already
902 if (!registerWindowClass ())
903 {
904 // Registration failed
905 return false;
906 }
907
82306ac2 908 // Save our parent, we'll probably need it eventually.
693916f8
RC
909 setParent(parent);
910
911 // Create the window instance
82306ac2
BD
912 CreateWindowEx (// Extended Style
913 WS_EX_CLIENTEDGE,
914 // window class atom (name)
915 "listview", //MAKEINTATOM(WindowClassAtom),
916 "listviewwindow", // no title-bar string yet
917 // Style bits
693916f8 918 Style,
82306ac2
BD
919 r ? r->left : CW_USEDEFAULT,
920 r ? r->top : CW_USEDEFAULT,
921 r ? r->right - r->left + 1 : CW_USEDEFAULT,
922 r ? r->bottom - r->top + 1 : CW_USEDEFAULT,
923 // Parent Window
924 parent == NULL ? (HWND)NULL : parent->GetHWND (),
925 // use class menu
926 (HMENU) MAKEINTRESOURCE (IDC_CHOOSE_LIST),
927 // The application instance
928 GetInstance (),
929 // The this ptr, which we'll use to set up
930 // the WindowProc reflection.
693916f8 931 reinterpret_cast<void *>((Window *)this));
693916f8
RC
932 if (GetHWND() == NULL)
933 {
157dc2b8 934 Log (LOG_BABBLE) << "Failed to create PickView " << GetLastError () << endLog;
693916f8
RC
935 return false;
936 }
937
938 return true;
939}
22120c90
RC
940
941void
942PickView::defaultTrust (trusts trust)
943{
944 this->deftrust = trust;
db9818d7 945
22120c90 946 packagedb db;
db9818d7
JT
947 db.defaultTrust(trust);
948
949 // force the picker to redraw
bb8e2353 950 RECT r = GetClientRect ();
22120c90 951 InvalidateRect (this->GetHWND(), &r, TRUE);
22120c90 952}
ec2dbbf0 953
82306ac2 954/* This recalculates all column widths and resets the view */
ec2dbbf0
RC
955void
956PickView::refresh()
957{
82306ac2
BD
958 HDC dc = GetDC (GetHWND ());
959
960 // we must set the font of the DC here, otherwise the width calculations
961 // will be off because the system will use the wrong font metrics
962 sysfont = GetStockObject (DEFAULT_GUI_FONT);
963 SelectObject (dc, sysfont);
964
965 // init headers for the current mode
966 set_headers ();
967 init_headers (dc);
968
969 // save the current mode
970 views cur_view_mode = view_mode;
971
972 // switch to the other type and do those headers
973 view_mode = (view_mode == PickView::views::Category) ?
974 PickView::views::PackageFull : PickView::views::Category;
975 set_headers ();
976 init_headers (dc);
977 ReleaseDC (GetHWND (), dc);
978
979 view_mode = cur_view_mode;
980 setViewMode (view_mode);
ec2dbbf0 981}
This page took 0.18942 seconds and 5 git commands to generate.