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 <windows.h> |
27 |
#include <commctrl.h> |
28 |
|
29 |
#include "wptCommonCtl.h" |
30 |
#include "wptW32API.h" |
31 |
#include "wptVersion.h" |
32 |
#include "wptErrors.h" |
33 |
#include "wptTypes.h" |
34 |
#include "wptGPG.h" |
35 |
#include "wptKeylist.h" |
36 |
#include "resource.h" |
37 |
|
38 |
|
39 |
int |
40 |
listview_new( listview_ctrl_t *ctx ) |
41 |
{ |
42 |
struct listview_ctrl_s *c; |
43 |
|
44 |
c = new struct listview_ctrl_s; |
45 |
if( !c ) |
46 |
BUG( NULL ); |
47 |
c->cols = 0; |
48 |
c->items = 0; |
49 |
c->ctrl = NULL; |
50 |
*ctx = c; |
51 |
return 0; |
52 |
} /* listview_new */ |
53 |
|
54 |
|
55 |
void |
56 |
listview_release( listview_ctrl_t ctx ) |
57 |
{ |
58 |
if( ctx ) { |
59 |
ctx->cols = 0; |
60 |
ctx->ctrl = NULL; |
61 |
ctx->items = 0; |
62 |
delete ctx; |
63 |
ctx = NULL; |
64 |
} |
65 |
} /* listview_release */ |
66 |
|
67 |
|
68 |
int |
69 |
listview_add_column (listview_ctrl_t ctx, listview_column_t col) |
70 |
{ |
71 |
int rc = 0; |
72 |
LV_COLUMN lvc; |
73 |
|
74 |
memset( &lvc, 0, sizeof lvc ); |
75 |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
76 |
lvc.pszText = col->fieldname; |
77 |
lvc.cx = col->width; |
78 |
lvc.fmt = LVCFMT_LEFT; |
79 |
lvc.iSubItem = col->pos; |
80 |
if( ListView_InsertColumn (ctx->ctrl, col->pos, &lvc) == -1) |
81 |
rc = 1; |
82 |
ctx->cols++; |
83 |
return rc; |
84 |
} /* listview_add_column */ |
85 |
|
86 |
|
87 |
int |
88 |
listview_add_item_pos (listview_ctrl_t ctx, int pos) |
89 |
{ |
90 |
int rc = 0; |
91 |
LV_ITEM lvi; |
92 |
|
93 |
memset (&lvi, 0, sizeof lvi); |
94 |
lvi.iItem = pos; |
95 |
rc = ListView_InsertItem (ctx->ctrl, &lvi); |
96 |
if (rc == -1) |
97 |
rc = 1; |
98 |
ctx->items++; |
99 |
return rc; |
100 |
} |
101 |
|
102 |
|
103 |
int |
104 |
listview_add_item( listview_ctrl_t ctx, const char *text ) |
105 |
{ |
106 |
int rc = 0; |
107 |
LV_ITEM lvi; |
108 |
|
109 |
memset( &lvi, 0, sizeof lvi ); |
110 |
lvi.mask = LVIF_TEXT; |
111 |
lvi.pszText = (char *)text; |
112 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
113 |
if( rc == -1 ) |
114 |
rc = 1; |
115 |
ctx->items++; |
116 |
return rc; |
117 |
} /* listview_add_item */ |
118 |
|
119 |
|
120 |
int |
121 |
listview_add_item2 (listview_ctrl_t ctx, const char *text, void *magic) |
122 |
{ |
123 |
int rc = 0; |
124 |
LV_ITEM lvi; |
125 |
|
126 |
memset( &lvi, 0, sizeof lvi ); |
127 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
128 |
lvi.pszText = (char *)text; |
129 |
lvi.lParam = (LPARAM)magic; |
130 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
131 |
if( rc == -1 ) |
132 |
rc = 1; |
133 |
ctx->items++; |
134 |
return rc; |
135 |
} /* listview_add_item2 */ |
136 |
|
137 |
|
138 |
void* |
139 |
listview_get_item2 (listview_ctrl_t ctx, int pos) |
140 |
{ |
141 |
LVITEM lvi; |
142 |
|
143 |
memset (&lvi, 0, sizeof lvi); |
144 |
lvi.mask = LVIF_PARAM; |
145 |
lvi.iItem = pos; |
146 |
ListView_GetItem (ctx->ctrl, &lvi); |
147 |
return (void*)lvi.lParam; |
148 |
} |
149 |
|
150 |
int |
151 |
listview_set_item2 (listview_ctrl_t ctx, int pos, void *magic) |
152 |
{ |
153 |
LVITEM lvi; |
154 |
|
155 |
memset (&lvi, 0, sizeof (lvi)); |
156 |
lvi.mask = LVIF_PARAM; |
157 |
lvi.iItem = pos; |
158 |
lvi.lParam = (LPARAM)magic; |
159 |
if (ListView_SetItem (ctx->ctrl, &lvi) == -1) |
160 |
return 1; |
161 |
return 0; |
162 |
} |
163 |
|
164 |
|
165 |
void |
166 |
listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text ) |
167 |
{ |
168 |
ListView_SetItemText( ctx->ctrl, pos, col, (char *)text ); |
169 |
} /* listview_add_sub_item */ |
170 |
|
171 |
|
172 |
int |
173 |
listview_count_items( listview_ctrl_t ctx, int curr_sel ) |
174 |
{ |
175 |
int n; |
176 |
|
177 |
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
178 |
ListView_GetItemCount( ctx->ctrl ); |
179 |
return n; |
180 |
} /* listview_count_items */ |
181 |
|
182 |
|
183 |
int |
184 |
listview_del_item( listview_ctrl_t ctx, int pos ) |
185 |
{ |
186 |
int rc = 0; |
187 |
|
188 |
if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 ) |
189 |
rc = 1; |
190 |
return rc; |
191 |
} /* listview_del_item */ |
192 |
|
193 |
|
194 |
int |
195 |
listview_del_items( listview_ctrl_t ctx ) |
196 |
{ |
197 |
int i, n; |
198 |
|
199 |
/* delete all selected entries */ |
200 |
n = listview_count_items (ctx, 0); |
201 |
for( i = n; i > 0; i-- ) { |
202 |
if ( listview_get_item_state( ctx, i ) ) |
203 |
listview_del_item( ctx, i ); |
204 |
} |
205 |
return 0; |
206 |
} /* listview_del_items */ |
207 |
|
208 |
|
209 |
int |
210 |
listview_del_all( listview_ctrl_t ctx ) |
211 |
{ |
212 |
int rc = 0; |
213 |
|
214 |
if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE ) |
215 |
rc = 1; |
216 |
return rc; |
217 |
} /* listview_del_all */ |
218 |
|
219 |
|
220 |
int |
221 |
listview_get_item_state( listview_ctrl_t ctx, int pos ) |
222 |
{ |
223 |
return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED ); |
224 |
} /* listview_get_item_state */ |
225 |
|
226 |
|
227 |
int |
228 |
listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb ) |
229 |
{ |
230 |
HWND hheader; |
231 |
HDITEM hdi; |
232 |
int idx; |
233 |
|
234 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
235 |
hheader = ListView_GetHeader (ctx->ctrl); |
236 |
if (!hheader) |
237 |
return 0; |
238 |
|
239 |
/* Remove image from all header fields */ |
240 |
memset (&hdi, 0, sizeof(hdi)); |
241 |
|
242 |
for (int i=0; i < 7; i++) { |
243 |
hdi.mask = HDI_FORMAT; |
244 |
Header_GetItem (hheader, i, &hdi); |
245 |
hdi.fmt &= ~HDF_IMAGE; |
246 |
Header_SetItem (hheader, i, &hdi); |
247 |
} |
248 |
|
249 |
switch (sortby & ~KEYLIST_SORT_DESC) { |
250 |
case KEY_SORT_USERID: idx = 0; break; |
251 |
case KEY_SORT_KEYID: idx = 1; break; |
252 |
case KEY_SORT_IS_SECRET: idx = 2; break; |
253 |
case KEY_SORT_LEN: idx = 3; break; |
254 |
case KEY_SORT_VALIDITY: idx = 5; break; |
255 |
case KEY_SORT_OTRUST: idx = 6; break; |
256 |
case KEY_SORT_CREATED: idx = 7; break; |
257 |
case KEY_SORT_ALGO: idx = 8; break; |
258 |
default: idx = 0; break; |
259 |
} |
260 |
|
261 |
/* Set image to currently sorted field */ |
262 |
memset (&hdi, 0, sizeof(hdi)); |
263 |
hdi.mask = HDI_IMAGE | HDI_FORMAT; |
264 |
Header_GetItem (hheader, idx, &hdi); |
265 |
hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT; |
266 |
hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ? |
267 |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
268 |
Header_SetItem (hheader, idx, &hdi); |
269 |
return 0; |
270 |
} /* listview_sort_items */ |
271 |
|
272 |
|
273 |
int |
274 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
275 |
{ |
276 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
277 |
} /* listview_get_curr_pos */ |
278 |
|
279 |
|
280 |
int |
281 |
listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text, |
282 |
int maxbytes) |
283 |
{ |
284 |
if (pos == -1) { |
285 |
pos = listview_get_curr_pos (ctx); |
286 |
if (pos == -1) |
287 |
return -1; |
288 |
} |
289 |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
290 |
return 0; |
291 |
} /* listview_get_item_text */ |
292 |
|
293 |
|
294 |
void |
295 |
listview_set_ext_style( listview_ctrl_t ctx ) |
296 |
{ |
297 |
ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT ); |
298 |
} /* listview_set_ext_style */ |
299 |
|
300 |
|
301 |
int |
302 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
303 |
{ |
304 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
305 |
} /* listview_set_column_order */ |
306 |
|
307 |
|
308 |
void |
309 |
listview_select_all (listview_ctrl_t ctx) |
310 |
{ |
311 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
312 |
} /* listview_select_all */ |
313 |
|
314 |
|
315 |
void |
316 |
listview_deselect_all (listview_ctrl_t ctx) |
317 |
{ |
318 |
ListView_SetItemState (ctx->ctrl, -1, ~LVNI_SELECTED, LVNI_SELECTED); |
319 |
} |
320 |
|
321 |
|
322 |
void |
323 |
listview_select_one (listview_ctrl_t ctx, int pos) |
324 |
{ |
325 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED); |
326 |
} |
327 |
|
328 |
|
329 |
void |
330 |
listview_scroll (listview_ctrl_t ctx, int oldpos, int newpos) |
331 |
{ |
332 |
const int size=14; |
333 |
|
334 |
if (oldpos == -1) |
335 |
oldpos = 0; |
336 |
//log_box ("debug", 0, "oldpos=%d newpos=%d diff=%d", oldpos, newpos, newpos-oldpos); |
337 |
ListView_Scroll (ctx->ctrl, 0, (newpos-oldpos)*size); |
338 |
} |
339 |
|
340 |
|
341 |
int |
342 |
listview_find (listview_ctrl_t ctx, const char * str) |
343 |
{ |
344 |
LVFINDINFO inf; |
345 |
int pos; |
346 |
|
347 |
memset (&inf, 0, sizeof (inf)); |
348 |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
349 |
inf.psz = str; |
350 |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
351 |
return pos; |
352 |
} |
353 |
|