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