aboutsummaryrefslogtreecommitdiffstats
path: root/my-evolution/e-summary-table.c
blob: 7edc82e0b4411df5776a5aa1ae728d416864362b (plain) (blame)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Iain Holmes <iain@ximian.com>
 *
 *  Copyright 2002 Ximain, Inc. (www.ximian.com)
 *
 *  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., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include <gtk/gtkvbox.h>
#include <gtk/gtkenums.h>
#include <gtk/gtksignal.h>

#include <gal/util/e-util.h>
#include <gal/widgets/e-gui-utils.h>

#include <gal/e-table/e-table-header.h>
#include <gal/e-table/e-table-header-item.h>
#include <gal/e-table/e-table-item.h>
#include <gal/e-table/e-cell-text.h>
#include <gal/e-table/e-cell-tree.h>
#include <gal/e-table/e-cell-checkbox.h>
#include <gal/e-table/e-table.h>
#include <gal/e-table/e-tree-scrolled.h>
#include <gal/e-table/e-tree-memory.h>
#include <gal/e-table/e-tree-memory-callbacks.h>
#include <gal/e-table/e-tree-table-adapter.h>

#include <libgnome/gnome-i18n.h>

#include "e-summary-table.h"
#include "e-cell-tri.h"

#define COLS 2

#if 0 /* For translators */
char *headers[COLS] = {
    N_("Shown"),
    N_("Name")
};
#endif

#define SPEC "<ETableSpecification cursor-mode=\"line\" draw-focus=\"true\" no-headers=\"true\"> \
<ETableColumn model_col=\"0\" _title=\"Shown\" minimum_width=\"20\" resizable=\"false\" cell=\"tricell\" compare=\"integer\"/> \
<ETableColumn model_col=\"1\" _title=\"Name\" resizable=\"true\" minimum-width=\"32\" cell=\"render-name\" compare=\"string\"/> \
<ETableState> \
<column source=\"0\"/> \
<column source=\"1\"/> \
<grouping></grouping> \
</ETableState> \
</ETableSpecification>"

#define PARENT_TYPE (gtk_vbox_get_type ())

static GtkObjectClass *e_summary_table_parent_class;

struct _ESummaryTablePrivate {
    GtkWidget *etable;
    ETableExtras *extras;
    ETreeTableAdapter *adapter;
    ETreeModel *etm;

    ETreePath root_node;
};

enum {
    ITEM_CHANGED,
    LAST_SIGNAL
};
static guint32 table_signals[LAST_SIGNAL] = { 0 };

/* ETree callbacks */
static GdkPixbuf *
icon_at (ETreeModel *etm,
     ETreePath path,
     void *model_data)
{
    /* No icon, since the cell tree renderer takes care of +/- */
    return NULL;
}

static int
column_count (ETreeModel *etm,
          void *data)
{
    return COLS;
}

static void *
duplicate_value (ETreeModel *etm,
         int col,
         const void *value,
         void *data)
{
    switch (col) {
    case 0:
        return (void *) value;

    case 1:
        return g_strdup (value);

    default:
        g_assert_not_reached ();
    }

    return NULL;
}

static void
free_value (ETreeModel *etm,
        int col,
        void *value,
        void *data)
{
    if (col == 1) {
        g_free (value);
    }
}

static void *
initialise_value (ETreeModel *etm,
          int col,
          void *data)
{
    switch (col) {
    case 0:
        return GINT_TO_POINTER (1);

    case 1:
        return g_strdup ("2");

    default:
        g_assert_not_reached ();

    }

    return NULL;
}

static gboolean
value_is_empty (ETreeModel *etm,
        int col,
        const void *value,
        void *data)
{
    if (col == 1) {
        return !(value && *(char *)value);
    }

    return FALSE;
}

static char *
value_to_string (ETreeModel *etm,
         int col,
         const void *value,
         void *data)
{
    switch (col) {
    case 0:
        return g_strdup_printf ("%d", GPOINTER_TO_INT (value));

    case 1:
        return g_strdup (value);

    default:
        g_assert_not_reached ();
    }

    return NULL;
}

static void *
value_at (ETreeModel *etm,
      ETreePath path,
      int col,
      void *model_data)
{
    GHashTable *table;
    ESummaryTable *est = E_SUMMARY_TABLE (model_data);
    ESummaryTableModelEntry *entry;

    table = est->model;
    if (e_tree_model_node_is_root (etm, path)) {
        if (col == 1) {
            return "<Root>";
        } else {
            return GINT_TO_POINTER (0);
        }
    } else {
        entry = g_hash_table_lookup (table, path);
        g_return_val_if_fail (entry != NULL, NULL);

        if (col == 1) {
            return entry->name;

        } else {
            if (entry->editable == TRUE) {
                return GINT_TO_POINTER (entry->shown ? 2 : 1);
            } else {
                return GINT_TO_POINTER (0);
            }
        }
    }
}

static void
set_value_at (ETreeModel *etm,
          ETreePath path,
          int col,
          const void *val,
          void *model_data)
{
    GHashTable *table;
    ESummaryTable *est = E_SUMMARY_TABLE (model_data);
    ESummaryTableModelEntry *entry;

    if (e_tree_model_node_is_root (etm, path)) {
        return;
    }

    if (col != 0) {
        return;
    }

    table = est->model;
    entry = g_hash_table_lookup (table, path);
    g_return_if_fail (entry != NULL);

    if (entry->editable == TRUE) {
        entry->shown = GPOINTER_TO_INT (val) == 1 ? FALSE : TRUE;
        g_signal_emit (est, table_signals[ITEM_CHANGED], 0, path);
    }
}

static gboolean
is_editable (ETreeModel *etm,
         ETreePath path,
         int col,
         void *model_data)
{
    GHashTable *table;
    ESummaryTable *est = (ESummaryTable *) model_data;
    ESummaryTableModelEntry *entry;

    if (e_tree_model_node_is_root (etm, path)) {
        return FALSE;
    }

    if (col == 1) {
        return FALSE;
    }

    table = est->model;
    entry = g_hash_table_lookup (table, path);
    g_return_val_if_fail (entry != NULL, FALSE);

    return entry->editable;
}

/* GtkObject callbacks */

static void
free_model_entry (gpointer key,
          gpointer value,
          gpointer user_data)
{
    ESummaryTableModelEntry *entry;

    entry = value;
    g_free (entry->location);
    g_free (entry->name);
    g_free (entry);
}

static void
destroy (GtkObject *object)
{
    ESummaryTable *est;
    ESummaryTablePrivate *priv;

    est = E_SUMMARY_TABLE (object);
    priv = est->priv;

    if (priv == NULL) {
        return;
    }

    /* What do I need to free? */
    g_hash_table_foreach (est->model, free_model_entry, NULL);
    g_hash_table_destroy (est->model);
    est->model = NULL;
    
    g_free (priv);
    est->priv = NULL;

    e_summary_table_parent_class->destroy (object);
}

static void
e_summary_table_class_init (GtkObjectClass *object_class)
{
    object_class->destroy = destroy;

    e_summary_table_parent_class = gtk_type_class (PARENT_TYPE);

    table_signals[ITEM_CHANGED] = gtk_signal_new ("item-changed",
                              GTK_RUN_LAST,
                              GTK_CLASS_TYPE (object_class),
                              GTK_SIGNAL_OFFSET (ESummaryTableClass, item_changed),
                              gtk_marshal_NONE__POINTER,
                              GTK_TYPE_NONE, 1,
                              GTK_TYPE_POINTER);
}

static void
e_summary_table_init (ESummaryTable *est)
{
    ESummaryTablePrivate *priv;
    ETreeMemory *etmm;
    ECell *cell;
    ETree *tree;

    priv = g_new (ESummaryTablePrivate, 1);
    est->priv = priv;

    priv->etm = e_tree_memory_callbacks_new (icon_at,
                         column_count,
                         
                         NULL,
                         NULL,

                         NULL,
                         NULL,

                         value_at,
                         set_value_at,
                         is_editable,

                         duplicate_value,
                         free_value,
                         initialise_value,
                         value_is_empty,
                         value_to_string,
                         est);
    gtk_object_ref (GTK_OBJECT (priv->etm));
    gtk_object_sink (GTK_OBJECT (priv->etm));

    etmm = E_TREE_MEMORY (priv->etm);
    e_tree_memory_set_expanded_default (etmm, TRUE);
    
    priv->root_node = e_tree_memory_node_insert (etmm, NULL, 0, est);

    cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
    priv->extras = e_table_extras_new ();
    e_table_extras_add_cell (priv->extras, "render-name", e_cell_tree_new (NULL, NULL, FALSE, cell));
    e_table_extras_add_cell (priv->extras, "tricell", e_cell_tri_new ());
    
    priv->etable = e_tree_scrolled_new (priv->etm, priv->extras, SPEC, NULL);
    if (priv->etable == NULL) {
        g_warning ("Could not create ETable for ESummaryTable");
        return;
    }

    tree = e_tree_scrolled_get_tree (E_TREE_SCROLLED (priv->etable));
    e_tree_root_node_set_visible (tree, FALSE);

    gtk_box_pack_start (GTK_BOX (est), GTK_WIDGET (priv->etable),
                TRUE, TRUE, 0);
    gtk_widget_show (GTK_WIDGET (priv->etable));
}

E_MAKE_TYPE (e_summary_table, "ESummaryTable", ESummaryTable,
         e_summary_table_class_init, e_summary_table_init, PARENT_TYPE);

GtkWidget *
e_summary_table_new (GHashTable *model)
{
    ESummaryTable *table;

    table = gtk_type_new (e_summary_table_get_type ());
    table->model = model;

    return GTK_WIDGET (table);
}

ETreePath
e_summary_table_add_node (ESummaryTable *table,
              ETreePath path,
              int position,
              gpointer node_data)
{
    ETreeMemory *etmm;
    ETreePath p;

    g_return_val_if_fail (IS_E_SUMMARY_TABLE (table), NULL);

    if (path == NULL) {
        path = table->priv->root_node;
    }

    etmm = E_TREE_MEMORY (table->priv->etm);
    e_tree_memory_freeze (etmm);
    p = e_tree_memory_node_insert (etmm, path, position, node_data);
    e_tree_memory_thaw (etmm);

    return p;
}
          
guint
e_summary_table_get_num_children (ESummaryTable *table,
                  ETreePath path)
{
    if (path == NULL) {
        return e_tree_model_node_get_children (table->priv->etm,
                               table->priv->root_node, NULL);
    } else {
        return e_tree_model_node_get_children (table->priv->etm, path, NULL);
    }
}