aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-cell-tree.c
blob: c3459ce4f846dfa99a434a5dbb4680ae6515674a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-cell-tree.c - Tree cell renderer
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Author: Chris Toshok <toshok@helixcode.com>
 *
 * A majority of code taken from:
 *
 * the ECellText renderer.
 *
 * Copyright (C) 1998 The Free Software Foundation
 *
 */

#include <config.h>
#include <gtk/gtkenums.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkwindow.h>
#include <gtk/gtkinvisible.h>
#include <gtk/gtksignal.h>
#include <gdk/gdkkeysyms.h>
#include <libgnomeui/gnome-canvas.h>
#include <stdio.h>
#include "e-table-sorted-variable.h"
#include "e-tree-model.h"
#include "e-cell-tree.h"
#include "e-util/e-util.h"
#include "e-table-item.h"

#include <gdk/gdkx.h> /* for BlackPixel */
#include <ctype.h>
#include <math.h>

#include "tree-expanded.xpm"
#include "tree-unexpanded.xpm"

#define PARENT_TYPE e_cell_get_type ()

typedef struct {
    ECellView    cell_view;
    ECellView   *subcell_view;
    GdkGC       *gc;

    GnomeCanvas *canvas;

} ECellTreeView;

static ECellClass *parent_class;

static GdkPixbuf   *tree_expanded_pixbuf, *tree_unexpanded_pixbuf;

#define INDENT_AMOUNT 16

static int
visible_depth_of_node (ETreeModel *tree_model, ETreePath *path)
{
    return (e_tree_model_node_depth (tree_model, path) 
        - (e_tree_model_root_node_is_visible (tree_model) ? 0 : 1));
}

static gint
offset_of_node (ETreeModel *tree_model, ETreePath *path)
{
    return (visible_depth_of_node(tree_model, path) + 1) * INDENT_AMOUNT;
}

static ETreePath*
e_cell_tree_get_node (ETreeModel *tree_model, int row)
{
    return (ETreePath*)e_table_model_value_at (E_TABLE_MODEL(tree_model), -1, row);
}

static ETreeModel*
e_cell_tree_get_tree_model (ETableModel *table_model, int row)
{
    return (ETreeModel*)e_table_model_value_at (table_model, -2, row);
}

/*
 * ECell::new_view method
 */
static ECellView *
ect_new_view (ECell *ecell, ETableModel *table_model, void *e_table_item_view)
{
    ECellTree *ect = E_CELL_TREE (ecell);
    ECellTreeView *tree_view = g_new0 (ECellTreeView, 1);
    GnomeCanvas *canvas = GNOME_CANVAS_ITEM (e_table_item_view)->canvas;
    
    tree_view->cell_view.ecell = ecell;
    tree_view->cell_view.e_table_model = table_model;
    tree_view->cell_view.e_table_item_view = e_table_item_view;
    
    /* create our subcell view */
    tree_view->subcell_view = e_cell_new_view (ect->subcell, table_model, e_table_item_view /* XXX */);

    tree_view->canvas = canvas;

    return (ECellView *)tree_view;
}

/*
 * ECell::kill_view method
 */
static void
ect_kill_view (ECellView *ecv)
{
    ECellTreeView *tree_view = (ECellTreeView *) ecv;

    /* kill our subcell view */
    e_cell_kill_view (tree_view->subcell_view);

    g_free (tree_view);
}

/*
 * ECell::realize method
 */
static void
ect_realize (ECellView *ecell_view)
{
    ECellTreeView *tree_view = (ECellTreeView *) ecell_view;

    /* realize our subcell view */
    e_cell_realize (tree_view->subcell_view);

    tree_view->gc = gdk_gc_new (GTK_WIDGET (tree_view->canvas)->window);

    gdk_gc_set_line_attributes (tree_view->gc, 1, 
                    GDK_LINE_ON_OFF_DASH, None, None);
    gdk_gc_set_dashes (tree_view->gc, 0, "\1\1", 2);

    if (parent_class->realize)
        (* parent_class->realize) (ecell_view);
}

/*
 * ECell::unrealize method
 */
static void
ect_unrealize (ECellView *ecv)
{
    ECellTreeView *tree_view = (ECellTreeView *) ecv;

    /* unrealize our subcell view. */
    e_cell_unrealize (tree_view->subcell_view);

    gdk_gc_unref (tree_view->gc);
    tree_view->gc = NULL;

    if (parent_class->unrealize)
        (* parent_class->unrealize) (ecv);
}

/*
 * ECell::draw method
 */
static void
ect_draw (ECellView *ecell_view, GdkDrawable *drawable,
      int model_col, int view_col, int row, gboolean selected,
      int x1, int y1, int x2, int y2)
{
    ECellTreeView *tree_view = (ECellTreeView *)ecell_view;
    ETreeModel *tree_model = e_cell_tree_get_tree_model(ecell_view->e_table_model, row);
    ETreePath *node;
    GdkRectangle rect, *clip_rect;
    GtkWidget *canvas = GTK_WIDGET (tree_view->canvas);
    GdkGC *fg_gc = canvas->style->fg_gc[GTK_STATE_ACTIVE];
    GdkColor *background, *foreground;

    int offset, subcell_offset;
    gboolean expanded, expandable;

    /* only draw the tree effects if we're the active sort */
    if (/* XXX */ TRUE) {
        /*
         * need to get the following things from the model
         * 1. depth of item.
         * 2. whether or not it has any children.
         * 3. whether the item is a toplevel item.
         * 3. ... anything else?
         */
        node = e_cell_tree_get_node (tree_model, row);

        offset = offset_of_node (tree_model, node);
        expandable = e_tree_model_node_is_expandable (tree_model, node);
        expanded = e_tree_model_node_is_expanded (tree_model, node);
        subcell_offset = offset;

        /*
         * Be a nice citizen: clip to the region we are supposed to draw on
         */
        rect.x = x1;
        rect.y = y1;
        rect.width = offset;
        rect.height = y2 - y1;
    
        gdk_gc_set_clip_rectangle (tree_view->gc, &rect);
        gdk_gc_set_clip_rectangle (fg_gc, &rect);
        clip_rect = &rect;

        if (selected){
            background = &canvas->style->bg [GTK_STATE_SELECTED];
            foreground = &canvas->style->text [GTK_STATE_SELECTED];
        } else {
            background = &canvas->style->base [GTK_STATE_NORMAL];
            foreground = &canvas->style->text [GTK_STATE_NORMAL];
        }
        gdk_gc_set_foreground (tree_view->gc, background);
        gdk_draw_rectangle (drawable, tree_view->gc, TRUE,
                    rect.x, rect.y, rect.width, rect.height);
        gdk_gc_set_foreground (tree_view->gc, foreground);

        /* draw our lines */
        if (E_CELL_TREE(tree_view->cell_view.ecell)->draw_lines) {

            gdk_draw_line (drawable, tree_view->gc,
                       rect.x + offset - INDENT_AMOUNT / 2 + 1,
                       rect.y + rect.height / 2,
                       rect.x + offset,
                       rect.y + rect.height / 2);

            if (visible_depth_of_node (tree_model, node) != 0) {
                gdk_draw_line (drawable, tree_view->gc,
                           rect.x + offset - INDENT_AMOUNT / 2,
                           rect.y,
                           rect.x + offset - INDENT_AMOUNT / 2,
                           (e_tree_model_node_get_next (tree_model, node)
                        ? rect.y + rect.height
                        : rect.y + rect.height / 2));
            }

            /* now traverse back up to the root of the tree, checking at
               each level if the node has siblings, and drawing the
               correct vertical pipe for it's configuration. */
            node = e_tree_model_node_get_parent (tree_model, node);
            offset -= INDENT_AMOUNT;
            while (node && visible_depth_of_node (tree_model, node) != 0) {
                if (e_tree_model_node_get_next(tree_model, node)) {
                    gdk_draw_line (drawable, tree_view->gc,
                               rect.x + offset - INDENT_AMOUNT / 2,
                               rect.y,
                               rect.x + offset - INDENT_AMOUNT / 2,
                               rect.y + rect.height);
                }
                node = e_tree_model_node_get_parent (tree_model, node);
                offset -= INDENT_AMOUNT;
            }
        }

        /* now draw our icon if we're expandable */
        if (expandable) {
            GdkPixbuf *image = expanded ? tree_expanded_pixbuf : tree_unexpanded_pixbuf;
            int width, height;

            width = gdk_pixbuf_get_width(image);
            height = gdk_pixbuf_get_height(image);

            gdk_pixbuf_render_to_drawable_alpha (image,
                                 drawable,
                                 0, 0,
                                 x1 + subcell_offset - INDENT_AMOUNT / 2 - width / 2,
                                 y1 + (y2 - y1) / 2 - height / 2,
                                 width, height,
                                 GDK_PIXBUF_ALPHA_BILEVEL,
                                 128,
                                 GDK_RGB_DITHER_NORMAL,
                                 width, 0);
        }
    }

    /* Now cause our subcell to draw its contents, shifted by
       subcell_offset pixels */
    e_cell_draw (tree_view->subcell_view, drawable,
             model_col, view_col, row, selected,
             x1 + subcell_offset, y1, x2, y2);
}

/*
 * ECell::event method
 */
static gint
ect_event (ECellView *ecell_view, GdkEvent *event, int model_col, int view_col, int row)
{
    ECellTreeView *tree_view = (ECellTreeView *) ecell_view;

    switch (event->type) {
    case GDK_BUTTON_PRESS: {
        /* if the event happened in our area of control (and
                   we care about it), handle it. */
        ETreeModel *tree_model = e_cell_tree_get_tree_model (ecell_view->e_table_model, row);
        ETreePath *node = e_cell_tree_get_node (tree_model, row);
        int offset = offset_of_node (tree_model, node);

        /* only activate the tree control if the click/release happens in the icon's area. */
        if (event->button.x > (offset - INDENT_AMOUNT) && event->button.x < offset) {
            if (e_tree_model_node_is_expandable (tree_model, node)) {
                e_tree_model_node_set_expanded (tree_model,
                                node,
                                !e_tree_model_node_is_expanded(tree_model, node));
            }
            return TRUE;
        }
        else if (event->button.x < (offset - INDENT_AMOUNT))
            return TRUE;
    }
    default:
        /* otherwise, pass it off to our subcell_view */
        e_cell_event(tree_view->subcell_view, event, model_col, view_col, row);
        return TRUE;
    }
}

/*
 * ECell::height method
 */
static int
ect_height (ECellView *ecell_view, int model_col, int view_col, int row) 
{
    ECellTreeView *tree_view = (ECellTreeView *) ecell_view;

    return e_cell_height (tree_view->subcell_view, model_col, view_col, row);
}

/*
 * ECellView::enter_edit method
 */
static void *
ect_enter_edit (ECellView *ecell_view, int model_col, int view_col, int row)
{
    /* just defer to our subcell's view */
    ECellTreeView *tree_view = (ECellTreeView *) ecell_view;

    return e_cell_enter_edit (tree_view->subcell_view, model_col, view_col, row);
}

/*
 * ECellView::leave_edit method
 */
static void
ect_leave_edit (ECellView *ecell_view, int model_col, int view_col, int row, void *edit_context)
{
    /* just defer to our subcell's view */
    ECellTreeView *tree_view = (ECellTreeView *) ecell_view;

    e_cell_leave_edit (tree_view->subcell_view, model_col, view_col, row, edit_context);
}

/*
 * GtkObject::destroy method
 */
static void
ect_destroy (GtkObject *object)
{
    ECellTree *ect = E_CELL_TREE (object);

    /* destroy our subcell */
    gtk_object_destroy (GTK_OBJECT (ect->subcell));

    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
e_cell_tree_class_init (GtkObjectClass *object_class)
{
    ECellClass *ecc = (ECellClass *) object_class;

    object_class->destroy = ect_destroy;

    ecc->new_view   = ect_new_view;
    ecc->kill_view  = ect_kill_view;
    ecc->realize    = ect_realize;
    ecc->unrealize  = ect_unrealize;
    ecc->draw       = ect_draw;
    ecc->event      = ect_event;
    ecc->height     = ect_height;
    ecc->enter_edit = ect_enter_edit;
    ecc->leave_edit = ect_leave_edit;

    parent_class = gtk_type_class (PARENT_TYPE);

    /*
     * Create our pixbuf for expanding/unexpanding
     */
    tree_expanded_pixbuf = gdk_pixbuf_new_from_xpm_data(tree_expanded_xpm);
    tree_unexpanded_pixbuf = gdk_pixbuf_new_from_xpm_data(tree_unexpanded_xpm);
}

E_MAKE_TYPE(e_cell_tree, "ECellTree", ECellTree, e_cell_tree_class_init, NULL, PARENT_TYPE);

void
e_cell_tree_construct (ECellTree *ect,
               gboolean draw_lines,
               ECell *subcell)
{              
    ect->subcell = subcell;
    ect->draw_lines = draw_lines;
}


ECell *
e_cell_tree_new (ETableModel *etm,
         gboolean draw_lines,
         ECell *subcell)
{
    ECellTree *ect = gtk_type_new (e_cell_tree_get_type ());

    e_cell_tree_construct (ect, draw_lines, subcell);

    return (ECell *) ect;
}