aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-canvas-vbox.c
blob: 8ed70d0be96e3ce26aed0853cee95bd97ce04ab0 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-canvas-vbox.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 <math.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtksignal.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);

static gint e_canvas_vbox_event   (GnomeCanvasItem *item, GdkEvent *event);
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_real_add_item_start(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_MINIMUM_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::minimum_width", GTK_TYPE_DOUBLE, 
                 GTK_ARG_READWRITE, ARG_MINIMUM_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;
    klass->add_item_start = e_canvas_vbox_real_add_item_start;
 
    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 */
    item_class->event       = e_canvas_vbox_event;
    item_class->realize     = e_canvas_vbox_realize;
}

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

    vbox->width = 10;
    vbox->minimum_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:
    case ARG_MINIMUM_WIDTH:
        e_canvas_vbox->minimum_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_MINIMUM_WIDTH:
        GTK_VALUE_DOUBLE (*arg) = e_canvas_vbox->minimum_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;
    }
}

/* Used from g_list_foreach(); disconnects from an item's signals */
static void
disconnect_item_cb (gpointer data, gpointer user_data)
{
    ECanvasVbox *vbox;
    GnomeCanvasItem *item;

    vbox = E_CANVAS_VBOX (user_data);

    item = GNOME_CANVAS_ITEM (data);
    gtk_signal_disconnect_by_data (GTK_OBJECT (item), vbox);
}

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

    if (vbox->items) {
        g_list_foreach(vbox->items, disconnect_item_cb, vbox);
        g_list_free(vbox->items);
        vbox->items = NULL;
    }
    
    GTK_OBJECT_CLASS(parent_class)->destroy (object);
}

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_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;
    
}

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);
}

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_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->minimum_width,
                      NULL);
        e_canvas_item_request_reflow(item);
    }
}


static void
e_canvas_vbox_real_add_item_start(ECanvasVbox *e_canvas_vbox, GnomeCanvasItem *item)
{
    e_canvas_vbox->items = g_list_prepend(e_canvas_vbox->items, item);
    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->minimum_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->minimum_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;
        gdouble old_width;
        gdouble max_width;

        old_width = e_canvas_vbox->width;
        max_width = e_canvas_vbox->minimum_width;

        old_height = e_canvas_vbox->height;
        running_height = 0;

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

            list = e_canvas_vbox->items;
            gtk_object_get (GTK_OBJECT(list->data),
                    "height", &item_height,
                    "width", &item_width,
                    NULL);
            e_canvas_item_move_absolute(GNOME_CANVAS_ITEM(list->data),
                            (double) 0,
                            (double) running_height);
            running_height += item_height;
            if (max_width < item_width)
                max_width = item_width;
            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,
                        "width", &item_width,
                        NULL);

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

                running_height += item_height;
                if (max_width < item_width)
                    max_width = item_width;
            }
                 
        }
        e_canvas_vbox->height = running_height;
        e_canvas_vbox->width = max_width;
        if (old_height != e_canvas_vbox->height ||
            old_width !=  e_canvas_vbox->width)
            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_GET_CLASS(e_canvas_vbox))->add_item)
        (E_CANVAS_VBOX_CLASS(GTK_OBJECT_GET_CLASS(e_canvas_vbox))->add_item) (e_canvas_vbox, item);
}

void
e_canvas_vbox_add_item_start(ECanvasVbox *e_canvas_vbox, GnomeCanvasItem *item)
{
    if (E_CANVAS_VBOX_CLASS(GTK_OBJECT_GET_CLASS(e_canvas_vbox))->add_item_start)
        (E_CANVAS_VBOX_CLASS(GTK_OBJECT_GET_CLASS(e_canvas_vbox))->add_item_start) (e_canvas_vbox, item);
}