1 |
/* wptListView.cpp - Dynamic list view control |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
3 |
* Copyright (C) 2004 Andreas Jobs |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or |
8 |
* modify it under the terms of the GNU General Public License |
9 |
* as published by the Free Software Foundation; either version 2 |
10 |
* of the License, or (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
* General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <stdio.h> |
27 |
#include <windows.h> |
28 |
#include <commctrl.h> |
29 |
|
30 |
#include "wptCommonCtl.h" |
31 |
#include "wptW32API.h" |
32 |
#include "wptVersion.h" |
33 |
#include "wptErrors.h" |
34 |
#include "wptTypes.h" |
35 |
#include "wptGPG.h" |
36 |
#include "wptKeylist.h" |
37 |
#include "../resource.h" |
38 |
|
39 |
|
40 |
int |
41 |
listview_new( listview_ctrl_t *ctx ) |
42 |
{ |
43 |
struct listview_ctrl_s *c; |
44 |
|
45 |
c = new struct listview_ctrl_s; |
46 |
if( !c ) |
47 |
BUG( NULL ); |
48 |
c->cols = 0; |
49 |
c->items = 0; |
50 |
c->ctrl = NULL; |
51 |
*ctx = c; |
52 |
return 0; |
53 |
} /* listview_new */ |
54 |
|
55 |
|
56 |
void |
57 |
listview_release( listview_ctrl_t ctx ) |
58 |
{ |
59 |
if( ctx ) { |
60 |
ctx->cols = 0; |
61 |
ctx->ctrl = NULL; |
62 |
ctx->items = 0; |
63 |
delete ctx; |
64 |
ctx = NULL; |
65 |
} |
66 |
} /* listview_release */ |
67 |
|
68 |
|
69 |
int |
70 |
listview_add_column (listview_ctrl_t ctx, listview_column_t col) |
71 |
{ |
72 |
int rc = 0; |
73 |
LV_COLUMN lvc; |
74 |
|
75 |
memset( &lvc, 0, sizeof lvc ); |
76 |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
77 |
lvc.pszText = col->fieldname; |
78 |
lvc.cx = col->width; |
79 |
lvc.fmt = LVCFMT_LEFT; |
80 |
lvc.iSubItem = col->pos; |
81 |
if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1) |
82 |
rc = 1; |
83 |
ctx->cols++; |
84 |
return rc; |
85 |
} /* listview_add_column */ |
86 |
|
87 |
|
88 |
int |
89 |
listview_add_item_pos (listview_ctrl_t ctx, int pos) |
90 |
{ |
91 |
int rc = 0; |
92 |
LV_ITEM lvi; |
93 |
|
94 |
memset (&lvi, 0, sizeof lvi); |
95 |
lvi.iItem = pos; |
96 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
97 |
if (rc == -1) |
98 |
rc = 1; |
99 |
ctx->items++; |
100 |
return rc; |
101 |
} |
102 |
|
103 |
|
104 |
int |
105 |
listview_add_item( listview_ctrl_t ctx, const char *text ) |
106 |
{ |
107 |
int rc = 0; |
108 |
LV_ITEM lvi; |
109 |
|
110 |
memset( &lvi, 0, sizeof lvi ); |
111 |
lvi.mask = LVIF_TEXT; |
112 |
lvi.pszText = (char *)text; |
113 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
114 |
if( rc == -1 ) |
115 |
rc = 1; |
116 |
ctx->items++; |
117 |
return rc; |
118 |
} /* listview_add_item */ |
119 |
|
120 |
|
121 |
int |
122 |
listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic) |
123 |
{ |
124 |
int rc = 0; |
125 |
LV_ITEM lvi; |
126 |
|
127 |
memset( &lvi, 0, sizeof lvi ); |
128 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
129 |
lvi.pszText = (char *)text; |
130 |
lvi.lParam = (LPARAM)magic; |
131 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
132 |
if( rc == -1 ) |
133 |
rc = 1; |
134 |
ctx->items++; |
135 |
return rc; |
136 |
} /* listview_add_item2 */ |
137 |
|
138 |
|
139 |
void* |
140 |
listview_get_item2 (listview_ctrl_t ctx, int pos) |
141 |
{ |
142 |
LVITEM lvi; |
143 |
|
144 |
memset (&lvi, 0, sizeof lvi); |
145 |
lvi.mask = LVIF_PARAM; |
146 |
lvi.iItem = pos; |
147 |
ListView_GetItem (ctx->ctrl, &lvi); |
148 |
return (void*)lvi.lParam; |
149 |
} |
150 |
|
151 |
int |
152 |
listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic) |
153 |
{ |
154 |
LVITEM lvi; |
155 |
|
156 |
memset (&lvi, 0, sizeof (lvi)); |
157 |
lvi.mask = LVIF_PARAM; |
158 |
lvi.iItem = pos; |
159 |
lvi.lParam = (LPARAM)magic; |
160 |
if (ListView_SetItem (ctx->ctrl, &lvi) == -1) |
161 |
return 1; |
162 |
return 0; |
163 |
} |
164 |
|
165 |
|
166 |
void |
167 |
listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text ) |
168 |
{ |
169 |
ListView_SetItemText( ctx->ctrl, pos, col, (char *)text ); |
170 |
} /* listview_add_sub_item */ |
171 |
|
172 |
|
173 |
int |
174 |
listview_count_items( listview_ctrl_t ctx, int curr_sel ) |
175 |
{ |
176 |
int n; |
177 |
|
178 |
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
179 |
ListView_GetItemCount( ctx->ctrl ); |
180 |
return n; |
181 |
} /* listview_count_items */ |
182 |
|
183 |
|
184 |
int |
185 |
listview_del_item( listview_ctrl_t ctx, int pos ) |
186 |
{ |
187 |
int rc = 0; |
188 |
|
189 |
if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 ) |
190 |
rc = 1; |
191 |
return rc; |
192 |
} /* listview_del_item */ |
193 |
|
194 |
|
195 |
int |
196 |
listview_del_items( listview_ctrl_t ctx ) |
197 |
{ |
198 |
int i, n; |
199 |
|
200 |
/* delete all selected entries */ |
201 |
n = listview_count_items (ctx, 0); |
202 |
for( i = n; i > 0; i-- ) { |
203 |
if ( listview_get_item_state( ctx, i ) ) |
204 |
listview_del_item( ctx, i ); |
205 |
} |
206 |
return 0; |
207 |
} /* listview_del_items */ |
208 |
|
209 |
|
210 |
int |
211 |
listview_del_all( listview_ctrl_t ctx ) |
212 |
{ |
213 |
int rc = 0; |
214 |
|
215 |
if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE ) |
216 |
rc = 1; |
217 |
return rc; |
218 |
} /* listview_del_all */ |
219 |
|
220 |
|
221 |
int |
222 |
listview_get_item_state( listview_ctrl_t ctx, int pos ) |
223 |
{ |
224 |
return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED ); |
225 |
} /* listview_get_item_state */ |
226 |
|
227 |
|
228 |
int |
229 |
listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb ) |
230 |
{ |
231 |
HWND hheader; |
232 |
HDITEM hdi; |
233 |
int idx; |
234 |
|
235 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
236 |
hheader = ListView_GetHeader (ctx->ctrl); |
237 |
if (!hheader) |
238 |
return 0; |
239 |
|
240 |
/* Remove image from all header fields */ |
241 |
memset (&hdi, 0, sizeof(hdi)); |
242 |
|
243 |
for (int i=0; i < 7; i++) { |
244 |
hdi.mask = HDI_FORMAT; |
245 |
Header_GetItem (hheader, i, &hdi); |
246 |
hdi.fmt &= ~HDF_IMAGE; |
247 |
Header_SetItem (hheader, i, &hdi); |
248 |
} |
249 |
|
250 |
switch (sortby & ~KEYLIST_SORT_DESC) { |
251 |
case KEY_SORT_USERID: idx = 0; break; |
252 |
case KEY_SORT_KEYID: idx = 1; break; |
253 |
case KEY_SORT_IS_SECRET: idx = 2; break; |
254 |
case KEY_SORT_LEN: idx = 3; break; |
255 |
case KEY_SORT_VALIDITY: idx = 5; break; |
256 |
case KEY_SORT_OTRUST: idx = 6; break; |
257 |
case KEY_SORT_CREATED: idx = 7; break; |
258 |
case KEY_SORT_ALGO: idx = 8; break; |
259 |
default: idx = 0; break; |
260 |
} |
261 |
|
262 |
/* Set image to currently sorted field */ |
263 |
memset (&hdi, 0, sizeof(hdi)); |
264 |
hdi.mask = HDI_IMAGE | HDI_FORMAT; |
265 |
Header_GetItem (hheader, idx, &hdi); |
266 |
hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT; |
267 |
hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ? |
268 |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
269 |
Header_SetItem (hheader, idx, &hdi); |
270 |
return 0; |
271 |
} /* listview_sort_items */ |
272 |
|
273 |
|
274 |
int |
275 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
276 |
{ |
277 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
278 |
} /* listview_get_curr_pos */ |
279 |
|
280 |
|
281 |
int |
282 |
listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text, |
283 |
int maxbytes) |
284 |
{ |
285 |
if (pos == -1) { |
286 |
pos = listview_get_curr_pos (ctx); |
287 |
if (pos == -1) |
288 |
return -1; |
289 |
} |
290 |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
291 |
return 0; |
292 |
} /* listview_get_item_text */ |
293 |
|
294 |
|
295 |
void |
296 |
listview_set_ext_style( listview_ctrl_t ctx ) |
297 |
{ |
298 |
ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT ); |
299 |
} /* listview_set_ext_style */ |
300 |
|
301 |
|
302 |
int |
303 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
304 |
{ |
305 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
306 |
} /* listview_set_column_order */ |
307 |
|
308 |
|
309 |
void |
310 |
listview_select_all (listview_ctrl_t ctx) |
311 |
{ |
312 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
313 |
} /* listview_select_all */ |
314 |
|
315 |
|
316 |
void |
317 |
listview_select_one (listview_ctrl_t ctx, int pos) |
318 |
{ |
319 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED); |
320 |
} |
321 |
|
322 |
|
323 |
void |
324 |
listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos) |
325 |
{ |
326 |
const int size=14; |
327 |
|
328 |
if (oldpos == -1) |
329 |
oldpos = 0; |
330 |
//log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos); |
331 |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
332 |
} |
333 |
|
334 |
|
335 |
int |
336 |
listview_find (listview_ctrl_t ctx, const char * str) |
337 |
{ |
338 |
LVFINDINFO inf; |
339 |
int pos; |
340 |
|
341 |
memset (&inf, 0, sizeof (inf)); |
342 |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
343 |
inf.psz = str; |
344 |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
345 |
return pos; |
346 |
} |
347 |
|
348 |
/** Some functions to make the handling with TreeView Controls easier **/ |
349 |
int |
350 |
treeview_add_item( HWND tree, HTREEITEM parent, const char *text ) |
351 |
{ |
352 |
TVINSERTSTRUCT tvis; |
353 |
|
354 |
memset( &tvis, 0, sizeof tvis ); |
355 |
tvis.hParent = parent; |
356 |
tvis.hInsertAfter = TVI_LAST; |
357 |
tvis.item.mask = TVIF_TEXT; |
358 |
tvis.item.pszText = (char *)text; |
359 |
TreeView_InsertItem( tree, &tvis ); |
360 |
|
361 |
return 0; |
362 |
} /* treeview_add_item */ |