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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* e-table-without.c
* Copyright 2000, 2001, Ximian, Inc.
*
* Authors:
* Chris Lahey <clahey@ximian.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License, version 2, as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtksignal.h>
#include "gal/util/e-util.h"
#include "e-table-without.h"
#define ETW_CLASS(e) ((ETableWithoutClass *)((GtkObject *)e)->klass)
#define PARENT_TYPE E_TABLE_SUBSET_TYPE
#define INCREMENT_AMOUNT 10
static ETableSubsetClass *parent_class;
struct _ETableWithoutPrivate {
GHashTable *hash;
GHashFunc hash_func;
GCompareFunc compare_func;
ETableWithoutGetKeyFunc get_key_func;
ETableWithoutDuplicateKeyFunc duplicate_key_func;
ETableWithoutFreeKeyFunc free_gotten_key_func;
ETableWithoutFreeKeyFunc free_duplicated_key_func;
void *closure;
};
static gboolean
check (ETableWithout *etw, int model_row)
{
gboolean ret_val;
void *key;
ETableSubset *etss = E_TABLE_SUBSET (etw);
if (etw->priv->get_key_func)
key = etw->priv->get_key_func (etss->source, model_row, etw->priv->closure);
else
key = GINT_TO_POINTER (model_row);
ret_val = (g_hash_table_lookup (etw->priv->hash, key) != NULL);
if (etw->priv->free_gotten_key_func)
etw->priv->free_gotten_key_func (key, etw->priv->closure);
return ret_val;
}
static gboolean
check_with_key (ETableWithout *etw, void *key, int model_row)
{
gboolean ret_val;
void *key2;
ETableSubset *etss = E_TABLE_SUBSET (etw);
if (etw->priv->get_key_func)
key2 = etw->priv->get_key_func (etss->source, model_row, etw->priv->closure);
else
key2 = GINT_TO_POINTER (model_row);
if (etw->priv->compare_func)
ret_val = (etw->priv->compare_func (key, key2));
else
ret_val = (key == key2);
if (etw->priv->free_gotten_key_func)
etw->priv->free_gotten_key_func (key2, etw->priv->closure);
return ret_val;
}
static gint
etw_view_to_model_row (ETableWithout *etw, int view_row)
{
ETableSubset *etss = E_TABLE_SUBSET (etw);
return etss->map_table[view_row];
}
static void
add_row (ETableWithout *etw, int model_row)
{
ETableSubset *etss = E_TABLE_SUBSET (etw);
e_table_model_pre_change (E_TABLE_MODEL (etw));
etss->map_table = g_renew (int, etss->map_table, etss->n_map + 1);
etss->map_table[etss->n_map++] = model_row;
e_table_model_row_inserted (E_TABLE_MODEL (etw), etss->n_map - 1);
}
static void
remove_row (ETableWithout *etw, int view_row)
{
ETableSubset *etss = E_TABLE_SUBSET (etw);
e_table_model_pre_change (E_TABLE_MODEL (etw));
memmove (etss->map_table + view_row, etss->map_table + view_row + 1, (etss->n_map - view_row - 1) * sizeof (int));
etss->n_map --;
e_table_model_row_deleted (E_TABLE_MODEL (etw), view_row);
}
static void
delete_hash_element (gpointer key,
gpointer value,
gpointer closure)
{
ETableWithout *etw = closure;
if (etw->priv->free_duplicated_key_func)
etw->priv->free_duplicated_key_func (key, etw->priv->closure);
}
static void
etw_destroy (GtkObject *object)
{
ETableWithout *etw = E_TABLE_WITHOUT (object);
if (etw->priv) {
if (etw->priv->hash) {
g_hash_table_foreach (etw->priv->hash, delete_hash_element, etw);
g_hash_table_destroy (etw->priv->hash);
etw->priv->hash = NULL;
}
g_free (etw->priv);
etw->priv = NULL;
}
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static void
etw_proxy_model_rows_inserted (ETableSubset *etss, ETableModel *etm, int model_row, int count)
{
int i;
ETableWithout *etw = E_TABLE_WITHOUT (etss);
/* i is View row */
for (i = 0; i < etss->n_map; i++) {
if (etss->map_table[i] > model_row)
etss->map_table[i] += count;
}
/* i is Model row */
for (i = model_row; i < model_row + count; i++) {
if (!check (etw, i)) {
add_row (etw, i);
}
}
}
static void
etw_proxy_model_rows_deleted (ETableSubset *etss, ETableModel *etm, int model_row, int count)
{
int i; /* View row */
ETableWithout *etw = E_TABLE_WITHOUT (etss);
for (i = 0; i < etss->n_map; i++) {
if (etss->map_table[i] >= model_row && etss->map_table[i] < model_row + count) {
remove_row (etw, i);
i--;
} else if (etss->map_table[i] >= model_row + count)
etss->map_table[i] -= count;
}
}
static void
etw_proxy_model_changed (ETableSubset *etss, ETableModel *etm)
{
int i; /* Model row */
int j; /* View row */
int row_count;
ETableWithout *etw = E_TABLE_WITHOUT (etss);
g_free (etss->map_table);
row_count = e_table_model_row_count (etm);
etss->map_table = g_new (int, row_count);
for (i = 0, j = 0; i < row_count; i++) {
if (!check (etw, i)) {
etss->map_table[j++] = i;
}
}
etss->n_map = j;
if (parent_class->proxy_model_changed)
parent_class->proxy_model_changed (etss, etm);
}
static void
etw_class_init (ETableWithoutClass *klass)
{
ETableSubsetClass *etss_class = E_TABLE_SUBSET_CLASS (klass);
GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
parent_class = gtk_type_class (PARENT_TYPE);
object_class->destroy = etw_destroy;
etss_class->proxy_model_rows_inserted = etw_proxy_model_rows_inserted;
etss_class->proxy_model_rows_deleted = etw_proxy_model_rows_deleted;
etss_class->proxy_model_changed = etw_proxy_model_changed;
}
static void
etw_init (ETableWithout *etw)
{
etw->priv = g_new (ETableWithoutPrivate, 1);
etw->priv->hash_func = NULL;
etw->priv->compare_func = NULL;
etw->priv->get_key_func = NULL;
etw->priv->duplicate_key_func = NULL;
etw->priv->free_gotten_key_func = NULL;
etw->priv->free_duplicated_key_func = NULL;
}
E_MAKE_TYPE(e_table_without, "ETableWithout", ETableWithout, etw_class_init, etw_init, PARENT_TYPE);
ETableModel *
e_table_without_construct (ETableWithout *etw,
ETableModel *source,
GHashFunc hash_func,
GCompareFunc compare_func,
ETableWithoutGetKeyFunc get_key_func,
ETableWithoutDuplicateKeyFunc duplicate_key_func,
ETableWithoutFreeKeyFunc free_gotten_key_func,
ETableWithoutFreeKeyFunc free_duplicated_key_func,
void *closure)
{
if (e_table_subset_construct (E_TABLE_SUBSET(etw), source, 1) == NULL)
return NULL;
E_TABLE_SUBSET(etw)->n_map = 0;
etw->priv->hash_func = hash_func;
etw->priv->compare_func = compare_func;
etw->priv->get_key_func = get_key_func;
etw->priv->duplicate_key_func = duplicate_key_func;
etw->priv->free_gotten_key_func = free_gotten_key_func;
etw->priv->free_duplicated_key_func = free_duplicated_key_func;
etw->priv->closure = closure;
etw->priv->hash = g_hash_table_new (etw->priv->hash_func, etw->priv->compare_func);
return E_TABLE_MODEL (etw);
}
ETableModel *
e_table_without_new (ETableModel *source,
GHashFunc hash_func,
GCompareFunc compare_func,
ETableWithoutGetKeyFunc get_key_func,
ETableWithoutDuplicateKeyFunc duplicate_key_func,
ETableWithoutFreeKeyFunc free_gotten_key_func,
ETableWithoutFreeKeyFunc free_duplicated_key_func,
void *closure)
{
ETableWithout *etw = gtk_type_new (E_TABLE_WITHOUT_TYPE);
if (e_table_without_construct (etw,
source,
hash_func,
compare_func,
get_key_func,
duplicate_key_func,
free_gotten_key_func,
free_duplicated_key_func,
closure)
== NULL) {
gtk_object_unref (GTK_OBJECT (etw));
return NULL;
}
return (ETableModel *) etw;
}
void e_table_without_hide (ETableWithout *etw,
void *key)
{
int i; /* View row */
ETableSubset *etss = E_TABLE_SUBSET (etw);
if (etw->priv->duplicate_key_func)
key = etw->priv->duplicate_key_func (key, etw->priv->closure);
g_hash_table_insert (etw->priv->hash, key, key);
for (i = 0; i < etss->n_map; i++) {
if (check_with_key (etw, key, etw_view_to_model_row (etw, i))) {
remove_row (etw, i);
i --;
}
}
}
/* An adopted key will later be freed using the free_duplicated_key function. */
void e_table_without_hide_adopt (ETableWithout *etw,
void *key)
{
int i; /* View row */
ETableSubset *etss = E_TABLE_SUBSET (etw);
g_hash_table_insert (etw->priv->hash, key, key);
for (i = 0; i < etss->n_map; i++) {
if (check_with_key (etw, key, etw_view_to_model_row (etw, i))) {
remove_row (etw, i);
i --;
}
}
}
void
e_table_without_show (ETableWithout *etw,
void *key)
{
int i; /* Model row */
ETableSubset *etss = E_TABLE_SUBSET (etw);
int count;
void *old_key;
count = e_table_model_row_count (etss->source);
for (i = 0; i < count; i++) {
if (check_with_key (etw, key, i)) {
add_row (etw, i);
}
}
if (g_hash_table_lookup_extended (etw->priv->hash, key, &old_key, NULL)) {
#if 0
if (etw->priv->free_duplicated_key_func)
etw->priv->free_duplicated_key_func (key, etw->priv->closure);
#endif
g_hash_table_remove (etw->priv->hash, key);
}
}
void
e_table_without_show_all (ETableWithout *etw)
{
int i; /* Model row */
int row_count;
ETableSubset *etss = E_TABLE_SUBSET (etw);
e_table_model_pre_change (E_TABLE_MODEL (etw));
if (etw->priv->hash) {
g_hash_table_foreach (etw->priv->hash, delete_hash_element, etw);
g_hash_table_destroy (etw->priv->hash);
etw->priv->hash = NULL;
}
etw->priv->hash = g_hash_table_new (etw->priv->hash_func, etw->priv->compare_func);
row_count = e_table_model_row_count (E_TABLE_MODEL(etss->source));
g_free (etss->map_table);
etss->map_table = g_new (int, row_count);
for (i = 0; i < row_count; i++) {
etss->map_table[i] = i;
}
etss->n_map = row_count;
e_table_model_changed (E_TABLE_MODEL (etw));
}
|