aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-canvas-vbox.c
blob: a49f7e93be06055d856c4bf51144d15aa5505c5e (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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include <config.h>

#include <math.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include <glib/gi18n.h>
#include "e-util/e-util.h"

#include "e-canvas.h"
#include "e-canvas-utils.h"
#include "e-canvas-vbox.h"

static void e_canvas_vbox_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void e_canvas_vbox_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void e_canvas_vbox_dispose (GObject *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, gint 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);

/* The arguments we take */
enum {
    PROP_0,
    PROP_WIDTH,
    PROP_MINIMUM_WIDTH,
    PROP_HEIGHT,
    PROP_SPACING
};

G_DEFINE_TYPE (
    ECanvasVbox,
    e_canvas_vbox,
    GNOME_TYPE_CANVAS_GROUP)

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

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

    klass->add_item       = e_canvas_vbox_real_add_item;
    klass->add_item_start = e_canvas_vbox_real_add_item_start;

    object_class->set_property = e_canvas_vbox_set_property;
    object_class->get_property = e_canvas_vbox_get_property;
    object_class->dispose      = e_canvas_vbox_dispose;

    /* GnomeCanvasItem method overrides */
    item_class->event       = e_canvas_vbox_event;
    item_class->realize     = e_canvas_vbox_realize;

    g_object_class_install_property (object_class, PROP_WIDTH,
                     g_param_spec_double ("width",
                                  "Width",
                                  "Width",
                                  0.0, G_MAXDOUBLE, 0.0,
                                  G_PARAM_READWRITE));
    g_object_class_install_property (object_class, PROP_MINIMUM_WIDTH,
                     g_param_spec_double ("minimum_width",
                                  "Minimum width",
                                  "Minimum Width",
                                  0.0, G_MAXDOUBLE, 0.0,
                                  G_PARAM_READWRITE));
    g_object_class_install_property (object_class, PROP_HEIGHT,
                     g_param_spec_double ("height",
                                  "Height",
                                  "Height",
                                  0.0, G_MAXDOUBLE, 0.0,
                                  G_PARAM_READABLE));
    g_object_class_install_property (object_class, PROP_SPACING,
                     g_param_spec_double ("spacing",
                                  "Spacing",
                                  "Spacing",
                                  0.0, G_MAXDOUBLE, 0.0,
                                  G_PARAM_READWRITE));
}

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_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    GnomeCanvasItem *item;
    ECanvasVbox *e_canvas_vbox;

    item = GNOME_CANVAS_ITEM (object);
    e_canvas_vbox = E_CANVAS_VBOX (object);

    switch (prop_id) {
    case PROP_WIDTH:
    case PROP_MINIMUM_WIDTH:
        e_canvas_vbox->minimum_width = g_value_get_double (value);
        e_canvas_vbox_resize_children (item);
        e_canvas_item_request_reflow (item);
        break;
    case PROP_SPACING:
        e_canvas_vbox->spacing = g_value_get_double (value);
        e_canvas_item_request_reflow (item);
        break;
    }
}

static void
e_canvas_vbox_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    ECanvasVbox *e_canvas_vbox;

    e_canvas_vbox = E_CANVAS_VBOX (object);

    switch (prop_id) {
    case PROP_WIDTH:
        g_value_set_double (value, e_canvas_vbox->width);
        break;
    case PROP_MINIMUM_WIDTH:
        g_value_set_double (value, e_canvas_vbox->minimum_width);
        break;
    case PROP_HEIGHT:
        g_value_set_double (value, e_canvas_vbox->height);
        break;
    case PROP_SPACING:
        g_value_set_double (value, e_canvas_vbox->spacing);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        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);
    g_signal_handlers_disconnect_matched (item,
                          G_SIGNAL_MATCH_DATA,
                          0, 0, NULL, NULL,
                          vbox);
}

static void
e_canvas_vbox_dispose (GObject *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;
    }

    G_OBJECT_CLASS (e_canvas_vbox_parent_class)->dispose (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 (e_canvas_vbox_parent_class)->event)
            return GNOME_CANVAS_ITEM_CLASS (e_canvas_vbox_parent_class)->event (item, event);
    }
    return return_val;

}

static void
e_canvas_vbox_realize (GnomeCanvasItem *item)
{
    if (GNOME_CANVAS_ITEM_CLASS (e_canvas_vbox_parent_class)->realize)
        (* GNOME_CANVAS_ITEM_CLASS (e_canvas_vbox_parent_class)->realize) (item);

    e_canvas_vbox_resize_children (item);
    e_canvas_item_request_reflow (item);
}

static void
e_canvas_vbox_remove_item (gpointer data, GObject *where_object_was)
{
    ECanvasVbox *vbox = data;
    vbox->items = g_list_remove (vbox->items, where_object_was);
}

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);
    g_object_weak_ref (G_OBJECT (item),
               e_canvas_vbox_remove_item, e_canvas_vbox);
    if (GNOME_CANVAS_ITEM (e_canvas_vbox)->flags & GNOME_CANVAS_ITEM_REALIZED) {
        gnome_canvas_item_set (item,
                      "width", (gdouble) 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);
    g_object_weak_ref (G_OBJECT (item),
               e_canvas_vbox_remove_item, e_canvas_vbox);
    if (GNOME_CANVAS_ITEM (e_canvas_vbox)->flags & GNOME_CANVAS_ITEM_REALIZED) {
        gnome_canvas_item_set (item,
                      "width", (gdouble) 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", (gdouble) e_canvas_vbox->minimum_width,
                      NULL);
    }
}

static void
e_canvas_vbox_reflow ( GnomeCanvasItem *item, gint flags )
{
    ECanvasVbox *e_canvas_vbox = E_CANVAS_VBOX (item);
    if (item->flags & 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;
            g_object_get (list->data,
                      "height", &item_height,
                      "width", &item_width,
                      NULL);
            e_canvas_item_move_absolute (GNOME_CANVAS_ITEM (list->data),
                            (gdouble) 0,
                            (gdouble) 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;

                g_object_get (list->data,
                          "height", &item_height,
                          "width", &item_width,
                          NULL);

                e_canvas_item_move_absolute (GNOME_CANVAS_ITEM (list->data),
                                (gdouble) 0,
                                (gdouble) 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);
}