aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-canvas-vbox.c
blob: 4e1eb459dccf1e64ab1ccda1ae57b28bb007b4f9 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-canvas-vbox.c
 * Copyright (C) 2000  Helix Code, Inc.
 * Author: Chris Lahey <clahey@helixcode.com>
 *
 * This library 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gnome.h>
#include <math.h>
#include "e-canvas-vbox.h"
#include "e-canvas-utils.h"
#include "e-canvas.h"
#include "gal/util/e-util.h"

static void e_canvas_vbox_init      (ECanvasVbox         *CanvasVbox);
static void e_canvas_vbox_class_init    (ECanvasVboxClass    *klass);
static void e_canvas_vbox_set_arg (GtkObject *o, GtkArg *arg, guint arg_id);
static void e_canvas_vbox_get_arg (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_canvas_vbox_destroy (GtkObject *object);

#if 0
static gint e_canvas_vbox_event   (GnomeCanvasItem *item, GdkEvent *event);
#endif
static void e_canvas_vbox_realize (GnomeCanvasItem *item);

static void e_canvas_vbox_reflow (GnomeCanvasItem *item, int flags);

static void e_canvas_vbox_real_add_item(ECanvasVbox *e_canvas_vbox, GnomeCanvasItem *item);
static void e_canvas_vbox_resize_children (GnomeCanvasItem *item);

static GnomeCanvasGroupClass *parent_class = NULL;

/* The arguments we take */
enum {
    ARG_0,
    ARG_WIDTH,
    ARG_HEIGHT,
    ARG_SPACING,
};

GtkType
e_canvas_vbox_get_type (void)
{
  static GtkType type = 0;

  if (!type)
    {
      static const GtkTypeInfo info =
      {
        "ECanvasVbox",
        sizeof (ECanvasVbox),
        sizeof (ECanvasVboxClass),
        (GtkClassInitFunc) e_canvas_vbox_class_init,
        (GtkObjectInitFunc) e_canvas_vbox_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      type = gtk_type_unique (gnome_canvas_group_get_type (), &info);
    }

  return type;
}

static void
e_canvas_vbox_class_init (ECanvasVboxClass *klass)
{
    GtkObjectClass *object_class;
    GnomeCanvasItemClass *item_class;

    object_class = (GtkObjectClass*) klass;
    item_class = (GnomeCanvasItemClass *) klass;

    parent_class = gtk_type_class (gnome_canvas_group_get_type ());
  
    gtk_object_add_arg_type ("ECanvasVbox::width", GTK_TYPE_DOUBLE, 
                 GTK_ARG_READWRITE, ARG_WIDTH); 
    gtk_object_add_arg_type ("ECanvasVbox::height", GTK_TYPE_DOUBLE, 
                 GTK_ARG_READABLE, ARG_HEIGHT);
    gtk_object_add_arg_type ("ECanvasVbox::spacing", GTK_TYPE_DOUBLE, 
                 GTK_ARG_READWRITE, ARG_SPACING);

    klass->add_item       = e_canvas_vbox_real_add_item;
 
    object_class->set_arg   = e_canvas_vbox_set_arg;
    object_class->get_arg   = e_canvas_vbox_get_arg;
    object_class->destroy   = e_canvas_vbox_destroy;
  
    /* GnomeCanvasItem method overrides */
#if 0
    item_class->event       = e_canvas_vbox_event;
#endif
    item_class->realize     = e_canvas_vbox_realize;
}

static void
e_canvas_vbox_init (ECanvasVbox *vbox)
{
    vbox->items = NULL;

    vbox->width = 10;
    vbox->height = 10;
    vbox->spacing = 0;

    e_canvas_item_set_reflow_callback(GNOME_CANVAS_ITEM(vbox), e_canvas_vbox_reflow);
}

static void
e_canvas_vbox_set_arg (GtkObject *o, GtkArg *arg, guint arg_id)
{
    GnomeCanvasItem *item;
    ECanvasVbox *e_canvas_vbox;

    item = GNOME_CANVAS_ITEM (o);
    e_canvas_vbox = E_CANVAS_VBOX (o);
    
    switch (arg_id){
    case ARG_WIDTH:
        e_canvas_vbox->width = GTK_VALUE_DOUBLE (*arg);
        e_canvas_vbox_resize_children(item);
        e_canvas_item_request_reflow(item);
        break;
    case ARG_SPACING:
        e_canvas_vbox->spacing = GTK_VALUE_DOUBLE (*arg);
        e_canvas_item_request_reflow(item);
        break;
    }
}

static void
e_canvas_vbox_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ECanvasVbox *e_canvas_vbox;

    e_canvas_vbox = E_CANVAS_VBOX (object);

    switch (arg_id) {
    case ARG_WIDTH:
        GTK_VALUE_DOUBLE (*arg) = e_canvas_vbox->width;
        break;
    case ARG_HEIGHT:
        GTK_VALUE_DOUBLE (*arg) = e_canvas_vbox->height;
        break;
    case ARG_SPACING:
        GTK_VALUE_DOUBLE (*arg) = e_canvas_vbox->spacing;
        break;
    default:
        arg->type = GTK_TYPE_INVALID;
        break;
    }
}

static void
e_canvas_vbox_destroy (GtkObject *object)
{
    ECanvasVbox *vbox = E_CANVAS_VBOX(object);

    g_list_foreach(vbox->items, (GFunc) gtk_object_unref, NULL);
    g_list_free(vbox->items);
    vbox->items = NULL;
    
    GTK_OBJECT_CLASS(parent_class)->destroy (object);
}

#if 0
static gint
e_canvas_vbox_event (GnomeCanvasItem *item, GdkEvent *event)
{
    gint return_val = TRUE;

    switch (event->type) {
    case GDK_KEY_PRESS:
        switch (event->key.keyval) {
        case GDK_KP_Tab:
        case GDK_ISO_Left_Tab:
        case GDK_Left:
        case GDK_KP_Left:
        case GDK_Right:
        case GDK_KP_Right:
        case GDK_Down:
        case GDK_KP_Down:
        case GDK_Up:
        case GDK_KP_Up:
        case GDK_Return:
        case GDK_KP_Enter:
            return_val = TRUE;
            break;
        default:
            return_val = FALSE;
            break;
        }
        break;
    default:
        return_val = FALSE;
        break;
    }
    if (!return_val) {
        if (GNOME_CANVAS_ITEM_CLASS(parent_class)->event)
            return GNOME_CANVAS_ITEM_CLASS (parent_class)->event (item, event);
    }
    return return_val;
    
}
#endif

static void
e_canvas_vbox_realize (GnomeCanvasItem *item)
{
    if (GNOME_CANVAS_ITEM_CLASS(parent_class)->realize)
        (* GNOME_CANVAS_ITEM_CLASS(parent_class)->realize) (item);
    
    e_canvas_vbox_resize_children(item);
    e_canvas_item_request_reflow(item);
}

static void
e_canvas_vbox_remove_item (GnomeCanvasItem *item, ECanvasVbox *vbox)
{
    vbox->items = g_list_remove(vbox->items, item);
    gtk_object_unref(GTK_OBJECT(vbox));
}

static void
e_canvas_vbox_real_add_item(ECanvasVbox *e_canvas_vbox, GnomeCanvasItem *item)
{
    e_canvas_vbox->items = g_list_append(e_canvas_vbox->items, item);
    gtk_object_ref(GTK_OBJECT(item));
    gtk_object_ref(GTK_OBJECT(e_canvas_vbox));
    gtk_signal_connect(GTK_OBJECT(item), "destroy",
               GTK_SIGNAL_FUNC(e_canvas_vbox_remove_item), e_canvas_vbox);
    if ( GTK_OBJECT_FLAGS( e_canvas_vbox ) & GNOME_CANVAS_ITEM_REALIZED ) {
        gnome_canvas_item_set(item,
                      "width", (double) e_canvas_vbox->width,
                      NULL);
        e_canvas_item_request_reflow(item);
    }

}

static void
e_canvas_vbox_resize_children (GnomeCanvasItem *item)
{
    GList *list;
    ECanvasVbox *e_canvas_vbox;

    e_canvas_vbox = E_CANVAS_VBOX (item);
    for ( list = e_canvas_vbox->items; list; list = list->next ) {
        GnomeCanvasItem *child = GNOME_CANVAS_ITEM(list->data);
        gnome_canvas_item_set(child,
                      "width", (double) e_canvas_vbox->width,
                      NULL);
    }
}

static void
e_canvas_vbox_reflow( GnomeCanvasItem *item, int flags )
{
    ECanvasVbox *e_canvas_vbox = E_CANVAS_VBOX(item);
    if ( GTK_OBJECT_FLAGS( e_canvas_vbox ) & GNOME_CANVAS_ITEM_REALIZED ) {

        gdouble old_height;
        gdouble running_height;

        old_height = e_canvas_vbox->height;
        
        running_height = 0;

        if (e_canvas_vbox->items == NULL) {
        } else {
            GList *list;
            gdouble item_height;

            list = e_canvas_vbox->items;
            gtk_object_get (GTK_OBJECT(list->data),
                    "height", &item_height,
                    NULL);
            e_canvas_item_move_absolute(GNOME_CANVAS_ITEM(list->data),
                            (double) 0,
                            (double) running_height);
            running_height += item_height;
            list = g_list_next(list);
            
            for( ; list; list = g_list_next(list)) {
                running_height += e_canvas_vbox->spacing;

                gtk_object_get (GTK_OBJECT(list->data),
                        "height", &item_height,
                        NULL);

                e_canvas_item_move_absolute(GNOME_CANVAS_ITEM(list->data),
                                (double) 0,
                                (double) running_height);

                running_height += item_height;
            }
                 
        }
        e_canvas_vbox->height = running_height;
        if (old_height != e_canvas_vbox->height)
            e_canvas_item_request_parent_reflow(item);
    }
}

void
e_canvas_vbox_add_item(ECanvasVbox *e_canvas_vbox, GnomeCanvasItem *item)
{
    if (E_CANVAS_VBOX_CLASS(GTK_OBJECT(e_canvas_vbox)->klass)->add_item)
        (E_CANVAS_VBOX_CLASS(GTK_OBJECT(e_canvas_vbox)->klass)->add_item) (e_canvas_vbox, item);
}