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