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