1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/** \file liststore.c
* Handling of the list store used for tree views and drop boxes
*/
/*
*
* Copyright 2016 Martin Fischer <m_fischer@users.sf.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define GTK_DISABLE_SINGLE_INCLUDES
#define GDK_DISABLE_DEPRECATED
#define GTK_DISABLE_DEPRECATED
#define GSEAL_ENABLE
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "gtkint.h"
/**
* Get data of one row in a list
* \param b IN list widget
* \param inx IN row to retrieve
* \param childR IN list for data (?)
* \returns
*/
wListItem_p wlibListItemGet(
GtkListStore *ls,
wIndex_t inx,
GList ** childR)
{
wListItem_p id_p;
assert(ls != NULL);
if (childR) {
*childR = NULL;
}
if (inx < 0) {
return NULL;
}
id_p = wlibListStoreGetContext(ls, inx);
return id_p;
}
/**
* Get the context (user data) for a row in the list store
*
* \param ls IN list store
* \param inx IN row
* \returns pointer to data
*/
void *
wlibListStoreGetContext(GtkListStore *ls, int inx)
{
GtkTreeIter iter;
gchar *string = NULL;
gboolean result;
gint childs;
childs = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(ls),
NULL);
if (inx < childs) {
result = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(ls),
&iter,
NULL,
inx);
if (result) {
gtk_tree_model_get(GTK_TREE_MODEL(ls),
&iter,
LISTCOL_DATA,
&string,
-1);
} else {
printf("Invalid index %d for list!\n", inx);
}
}
return (string);
}
/**
* Clear the list store. All data in the list store will be automatically
* free'd when the list store is cleared.
*
* \param listStore IN
*/
void
wlibListStoreClear(GtkListStore *listStore)
{
wListItem_p id_p;
int i = 0;
assert(listStore != NULL);
id_p = wlibListStoreGetContext(listStore, i++);
while (id_p) {
if (id_p->label) {
g_free(id_p->label);
}
g_free(id_p);
id_p = wlibListStoreGetContext(listStore, i++);
}
gtk_list_store_clear(listStore);
}
/**
* Create a list store. The list store will have one column for user
* data that will not be displayed, a column for a bitmap and <cnt>
* colors for data.
*
* \param colCnt IN number of additional columns
* \returns the list store
*/
GtkListStore *
wlibNewListStore(int colCnt)
{
GtkListStore *ls;
GType *colTypes;
int i;
/* create the list store, using strings for all columns */
colTypes = g_malloc(sizeof(GType) * (colCnt + LISTCOL_TEXT));
colTypes[ LISTCOL_BITMAP ] = GDK_TYPE_PIXBUF;
colTypes[ LISTCOL_DATA ] = G_TYPE_POINTER;
for (i = 0; i < colCnt; i++) {
colTypes[ LISTCOL_TEXT + i ] = G_TYPE_STRING;
}
ls = gtk_list_store_newv(colCnt + LISTCOL_TEXT, colTypes);
g_free(colTypes);
return (ls);
}
/**
* Update the list store at the iter's position
*
* \param ls IN list store
* \param iter IN iterator into list store
* \param labels IN tab separated label string
* \returns number of updated columns
*/
static int
wlibListStoreUpdateIter(GtkListStore *ls, GtkTreeIter *iter, char *labels)
{
char *convertedLabels;
char *text;
char *start;
int current = 0;
convertedLabels = strdup(wlibConvertInput(labels));
start = convertedLabels;
while ((text = strchr(start, '\t')) != NULL) {
*text = '\0';
gtk_list_store_set(ls, iter, LISTCOL_TEXT + current, start, -1);
start = text + 1;
current++;
}
/* add the last piece of the string */
gtk_list_store_set(ls, iter, LISTCOL_TEXT + current, start, -1);
free(convertedLabels);
return (current+1);
}
/**
* Add a pixbuf to the list store. So pixbuf is unref'ed so it will be freed
* with the list store.
*
* \param ls IN list store
* \param iter IN position
* \param pixbuf IN pixbuf to add
*/
void
wlibListStoreSetPixbuf(GtkListStore *ls, GtkTreeIter *iter, GdkPixbuf *pixbuf)
{
gtk_list_store_set(ls, iter, LISTCOL_BITMAP, pixbuf, -1);
g_object_ref_sink(pixbuf);
g_object_unref(pixbuf);
}
/**
* Add a row to the list store
*
* \param ls IN the list store
* \param cols IN columns in list store
* \param id IN id
* \returns number columns added
*/
int
wlibListStoreAddData(GtkListStore *ls, GdkPixbuf *pixbuf, int cols,
wListItem_p id)
{
GtkTreeIter iter;
int count;
gtk_list_store_append(ls, &iter);
gtk_list_store_set(ls, &iter, LISTCOL_DATA, id, -1);
if (pixbuf) {
wlibListStoreSetPixbuf(ls, &iter, pixbuf);
}
count = wlibListStoreUpdateIter(ls, &iter, (char *)id->label);
return (count);
}
/**
* Change a row in the list store. The passed strings are placed
* in the first cols text columns of the list store.
*
* \param ls IN list store
* \param row IN row in list store
* \param cols IN number of columns to set
* \param labels IN tab separated list of texts
* \param bm IN bitmap
* \return count of updated text fields
*/
int
wlibListStoreUpdateValues(GtkListStore *ls, int row, int cols, char *labels,
wIcon_p bm)
{
GtkTreeIter iter;
int count;
gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(ls),
&iter,
NULL,
row);
count = wlibListStoreUpdateIter(ls, &iter, labels);
if (bm) {
GdkPixbuf *pixbuf;
pixbuf = wlibMakePixbuf(bm);
wlibListStoreSetPixbuf(ls, &iter, pixbuf);
}
return (count);
}
|