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