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( listview_ctrl_t ctx, const char *text ) |
86 |
{ |
87 |
int rc = 0; |
88 |
LV_ITEM lvi; |
89 |
|
90 |
memset( &lvi, 0, sizeof lvi ); |
91 |
lvi.mask = LVIF_TEXT; |
92 |
lvi.pszText = (char *)text; |
93 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
94 |
if( rc == -1 ) |
95 |
rc = 1; |
96 |
ctx->items++; |
97 |
return rc; |
98 |
} /* listview_add_item */ |
99 |
|
100 |
|
101 |
int |
102 |
listview_add_item2( listview_ctrl_t ctx, const char * text, void * magic ) |
103 |
{ |
104 |
int rc = 0; |
105 |
LV_ITEM lvi; |
106 |
|
107 |
memset( &lvi, 0, sizeof lvi ); |
108 |
lvi.mask = LVIF_TEXT | LVIF_PARAM; |
109 |
lvi.pszText = (char *)text; |
110 |
lvi.lParam = (LPARAM)magic; |
111 |
rc = ListView_InsertItem( ctx->ctrl, &lvi ); |
112 |
if( rc == -1 ) |
113 |
rc = 1; |
114 |
ctx->items++; |
115 |
return rc; |
116 |
} /* listview_add_item2 */ |
117 |
|
118 |
|
119 |
void |
120 |
listview_add_sub_item( listview_ctrl_t ctx, int pos, int col, const char *text ) |
121 |
{ |
122 |
ListView_SetItemText( ctx->ctrl, pos, col, (char *)text ); |
123 |
} /* listview_add_sub_item */ |
124 |
|
125 |
|
126 |
int |
127 |
listview_count_items( listview_ctrl_t ctx, int curr_sel ) |
128 |
{ |
129 |
int n; |
130 |
|
131 |
n = curr_sel? ListView_GetSelectedCount( ctx->ctrl ) : |
132 |
ListView_GetItemCount( ctx->ctrl ); |
133 |
return n; |
134 |
} /* listview_count_items */ |
135 |
|
136 |
|
137 |
int |
138 |
listview_del_item( listview_ctrl_t ctx, int pos ) |
139 |
{ |
140 |
int rc = 0; |
141 |
|
142 |
if( ListView_DeleteItem( ctx->ctrl, pos ) == -1 ) |
143 |
rc = 1; |
144 |
return rc; |
145 |
} /* listview_del_item */ |
146 |
|
147 |
|
148 |
int |
149 |
listview_del_items( listview_ctrl_t ctx ) |
150 |
{ |
151 |
int n; |
152 |
|
153 |
/* delete all selected entries */ |
154 |
for( n = 0; n < listview_count_items( ctx, 0 ); n++ ) { |
155 |
if ( listview_get_item_state( ctx, n ) ) |
156 |
listview_del_item( ctx, n ); |
157 |
} |
158 |
return 0; |
159 |
} /* listview_del_items */ |
160 |
|
161 |
|
162 |
int |
163 |
listview_del_all( listview_ctrl_t ctx ) |
164 |
{ |
165 |
int rc = 0; |
166 |
|
167 |
if( ListView_DeleteAllItems( ctx->ctrl ) == FALSE ) |
168 |
rc = 1; |
169 |
return rc; |
170 |
} /* listview_del_all */ |
171 |
|
172 |
|
173 |
int |
174 |
listview_get_item_state( listview_ctrl_t ctx, int pos ) |
175 |
{ |
176 |
return ListView_GetItemState( ctx->ctrl, pos, LVIS_SELECTED ); |
177 |
} /* listview_get_item_state */ |
178 |
|
179 |
|
180 |
int |
181 |
listview_sort_items( listview_ctrl_t ctx, int sortby, listview_cmp sort_cb ) |
182 |
{ |
183 |
HWND hheader; |
184 |
HDITEM hdi; |
185 |
int idx; |
186 |
|
187 |
ListView_SortItems (ctx->ctrl, sort_cb, sortby); |
188 |
hheader = ListView_GetHeader (ctx->ctrl); |
189 |
if (!hheader) |
190 |
return 0; |
191 |
|
192 |
/* Remove image from all header fields */ |
193 |
memset (&hdi, 0, sizeof(hdi)); |
194 |
|
195 |
for (int i=0; i < 7; i++) { |
196 |
hdi.mask = HDI_FORMAT; |
197 |
Header_GetItem (hheader, i, &hdi); |
198 |
hdi.fmt &= ~HDF_IMAGE; |
199 |
Header_SetItem (hheader, i, &hdi); |
200 |
} |
201 |
|
202 |
switch (sortby & ~KEYLIST_SORT_DESC) { |
203 |
case GPGME_ATTR_USERID: idx = 0; break; |
204 |
case GPGME_ATTR_KEYID: idx = 1; break; |
205 |
case GPGME_ATTR_IS_SECRET: idx = 2; break; |
206 |
case GPGME_ATTR_LEN: idx = 3; break; |
207 |
case GPGME_ATTR_VALIDITY: idx = 5; break; |
208 |
case GPGME_ATTR_OTRUST: idx = 6; break; |
209 |
case GPGME_ATTR_CREATED: idx = 7; break; |
210 |
case GPGME_ATTR_ALGO: idx = 8; break; |
211 |
default: idx = 0; break; |
212 |
} |
213 |
|
214 |
/* Set image to currently sorted field */ |
215 |
memset (&hdi, 0, sizeof(hdi)); |
216 |
hdi.mask = HDI_IMAGE | HDI_FORMAT; |
217 |
Header_GetItem (hheader, idx, &hdi); |
218 |
hdi.fmt |= HDF_IMAGE | HDF_BITMAP_ON_RIGHT; |
219 |
hdi.iImage = imagelist_getindex((sortby & KEYLIST_SORT_DESC) ? |
220 |
IMI_SORT_DOWNARROW : IMI_SORT_UPARROW); |
221 |
Header_SetItem (hheader, idx, &hdi); |
222 |
return 0; |
223 |
} /* listview_sort_items */ |
224 |
|
225 |
|
226 |
int |
227 |
listview_get_curr_pos( listview_ctrl_t ctx ) |
228 |
{ |
229 |
return ListView_GetNextItem( ctx->ctrl, -1, LVNI_SELECTED ); |
230 |
} /* listview_get_curr_pos */ |
231 |
|
232 |
|
233 |
int |
234 |
listview_get_item_text (listview_ctrl_t ctx, int pos, int col, char *text, |
235 |
int maxbytes) |
236 |
{ |
237 |
if (pos == -1) { |
238 |
pos = listview_get_curr_pos (ctx); |
239 |
if (pos == -1) |
240 |
return -1; |
241 |
} |
242 |
ListView_GetItemText (ctx->ctrl, pos, col, text, maxbytes); |
243 |
return 0; |
244 |
} /* listview_get_item_text */ |
245 |
|
246 |
|
247 |
void |
248 |
listview_set_ext_style( listview_ctrl_t ctx ) |
249 |
{ |
250 |
ListView_SetExtendedListViewStyle( ctx->ctrl, LVS_EX_FULLROWSELECT ); |
251 |
} /* listview_set_ext_style */ |
252 |
|
253 |
|
254 |
int |
255 |
listview_set_column_order( listview_ctrl_t ctx, int *array ) |
256 |
{ |
257 |
return ListView_SetColumnOrderArray( ctx->ctrl, ctx->cols, array ); |
258 |
} /* listview_set_column_order */ |
259 |
|
260 |
|
261 |
void |
262 |
listview_select_all (listview_ctrl_t ctx) |
263 |
{ |
264 |
ListView_SetItemState( ctx->ctrl, -1, LVIS_SELECTED, LVIS_SELECTED ); |
265 |
} /* listview_select_all */ |
266 |
|
267 |
|
268 |
void |
269 |
listview_select_one (listview_ctrl_t ctx, int pos) |
270 |
{ |
271 |
ListView_SetItemState (ctx->ctrl, pos, LVIS_SELECTED|LVIS_FOCUSED, LVIS_FOCUSED|LVIS_SELECTED); |
272 |
} |
273 |
|
274 |
|
275 |
int |
276 |
listview_find (listview_ctrl_t ctx, const char * str) |
277 |
{ |
278 |
LVFINDINFO inf; |
279 |
int pos; |
280 |
|
281 |
memset (&inf, 0, sizeof inf); |
282 |
inf.flags = LVFI_STRING|LVFI_PARTIAL; |
283 |
inf.psz = str; |
284 |
pos = ListView_FindItem (ctx->ctrl, -1, &inf); |
285 |
return pos; |
286 |
} |
287 |
|
288 |
/** Some functions to make the handling with TreeView Controls easier **/ |
289 |
int |
290 |
treeview_add_item( HWND tree, HTREEITEM parent, const char *text ) |
291 |
{ |
292 |
TVINSERTSTRUCT tvis; |
293 |
|
294 |
memset( &tvis, 0, sizeof tvis ); |
295 |
tvis.hParent = parent; |
296 |
tvis.hInsertAfter = TVI_LAST; |
297 |
tvis.item.mask = TVIF_TEXT; |
298 |
tvis.item.pszText = (char *)text; |
299 |
TreeView_InsertItem( tree, &tvis ); |
300 |
|
301 |
return 0; |
302 |
} /* treeview_add_item */ |