aboutsummaryrefslogblamecommitdiffstats
path: root/libgnomecanvas/gnome-canvas-widget.c
blob: d94942c8a5d33b561dfd8acf2971523918b56ce7 (plain) (tree)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599






















































































































































































































































































































































































































































































































































































































                                                                                                                      
/*
 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
 * All rights reserved.
 *
 * This file is part of the Gnome Library.
 *
 * The Gnome Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Gnome 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 the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
  @NOTATION@
 */
/* Widget item type for GnomeCanvas widget
 *
 * GnomeCanvas is basically a port of the Tk toolkit's most excellent canvas widget.  Tk is
 * copyrighted by the Regents of the University of California, Sun Microsystems, and other parties.
 *
 *
 * Author: Federico Mena <federico@nuclecu.unam.mx>
 */

#include <config.h>
#include <math.h>
#include <gtk/gtksignal.h>
#include "gnome-canvas-widget.h"

enum {
    PROP_0,
    PROP_WIDGET,
    PROP_X,
    PROP_Y,
    PROP_WIDTH,
    PROP_HEIGHT,
    PROP_ANCHOR,
    PROP_SIZE_PIXELS
};


static void gnome_canvas_widget_class_init (GnomeCanvasWidgetClass *class);
static void gnome_canvas_widget_init       (GnomeCanvasWidget      *witem);
static void gnome_canvas_widget_destroy    (GtkObject              *object);
static void gnome_canvas_widget_get_property (GObject            *object,
                          guint               param_id,
                          GValue             *value,
                          GParamSpec         *pspec);
static void gnome_canvas_widget_set_property (GObject            *object,
                          guint               param_id,
                          const GValue       *value,
                          GParamSpec         *pspec);

static void   gnome_canvas_widget_update      (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags);
static double gnome_canvas_widget_point       (GnomeCanvasItem *item, double x, double y,
                           int cx, int cy, GnomeCanvasItem **actual_item);
static void   gnome_canvas_widget_bounds      (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2);

static void gnome_canvas_widget_render (GnomeCanvasItem *item,
                    GnomeCanvasBuf *buf);
static void gnome_canvas_widget_draw (GnomeCanvasItem *item,
                      GdkDrawable *drawable,
                      int x, int y,
                      int width, int height);

static GnomeCanvasItemClass *parent_class;


GType
gnome_canvas_widget_get_type (void)
{
    static GType widget_type;

    if (!widget_type) {
        const GTypeInfo object_info = {
            sizeof (GnomeCanvasWidgetClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) gnome_canvas_widget_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,           /* class_data */
            sizeof (GnomeCanvasWidget),
            0,          /* n_preallocs */
            (GInstanceInitFunc) gnome_canvas_widget_init,
            NULL            /* value_table */
        };

        widget_type = g_type_register_static (GNOME_TYPE_CANVAS_ITEM, "GnomeCanvasWidget",
                              &object_info, 0);
    }

    return widget_type;
}

static void
gnome_canvas_widget_class_init (GnomeCanvasWidgetClass *class)
{
    GObjectClass *gobject_class;
    GtkObjectClass *object_class;
    GnomeCanvasItemClass *item_class;

    gobject_class = (GObjectClass *) class;
    object_class = (GtkObjectClass *) class;
    item_class = (GnomeCanvasItemClass *) class;

    parent_class = g_type_class_peek_parent (class);

    gobject_class->set_property = gnome_canvas_widget_set_property;
    gobject_class->get_property = gnome_canvas_widget_get_property;

        g_object_class_install_property
                (gobject_class,
                 PROP_WIDGET,
                 g_param_spec_object ("widget", NULL, NULL,
                                      GTK_TYPE_WIDGET,
                                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_X,
                 g_param_spec_double ("x", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_Y,
                 g_param_spec_double ("y", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_WIDTH,
                 g_param_spec_double ("width", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_HEIGHT,
                 g_param_spec_double ("height", NULL, NULL,
                      -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
                      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_ANCHOR,
                 g_param_spec_enum ("anchor", NULL, NULL,
                                    GTK_TYPE_ANCHOR_TYPE,
                                    GTK_ANCHOR_NW,
                                    (G_PARAM_READABLE | G_PARAM_WRITABLE)));
        g_object_class_install_property
                (gobject_class,
                 PROP_SIZE_PIXELS,
                 g_param_spec_boolean ("size_pixels", NULL, NULL,
                       FALSE,
                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));

    object_class->destroy = gnome_canvas_widget_destroy;

    item_class->update = gnome_canvas_widget_update;
    item_class->point = gnome_canvas_widget_point;
    item_class->bounds = gnome_canvas_widget_bounds;
    item_class->render = gnome_canvas_widget_render;
    item_class->draw = gnome_canvas_widget_draw;
}

static void
gnome_canvas_widget_init (GnomeCanvasWidget *witem)
{
    witem->x = 0.0;
    witem->y = 0.0;
    witem->width = 0.0;
    witem->height = 0.0;
    witem->anchor = GTK_ANCHOR_NW;
    witem->size_pixels = FALSE;
}

static void
gnome_canvas_widget_destroy (GtkObject *object)
{
    GnomeCanvasWidget *witem;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_WIDGET (object));

    witem = GNOME_CANVAS_WIDGET (object);

    if (witem->widget && !witem->in_destroy) {
        g_signal_handler_disconnect (witem->widget, witem->destroy_id);
        gtk_widget_destroy (witem->widget);
        witem->widget = NULL;
    }

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

static void
recalc_bounds (GnomeCanvasWidget *witem)
{
    GnomeCanvasItem *item;
    double wx, wy;

    item = GNOME_CANVAS_ITEM (witem);

    /* Get world coordinates */

    wx = witem->x;
    wy = witem->y;
    gnome_canvas_item_i2w (item, &wx, &wy);

    /* Get canvas pixel coordinates */

    gnome_canvas_w2c (item->canvas, wx, wy, &witem->cx, &witem->cy);

    /* Anchor widget item */

    switch (witem->anchor) {
    case GTK_ANCHOR_NW:
    case GTK_ANCHOR_W:
    case GTK_ANCHOR_SW:
        break;

    case GTK_ANCHOR_N:
    case GTK_ANCHOR_CENTER:
    case GTK_ANCHOR_S:
        witem->cx -= witem->cwidth / 2;
        break;

    case GTK_ANCHOR_NE:
    case GTK_ANCHOR_E:
    case GTK_ANCHOR_SE:
        witem->cx -= witem->cwidth;
        break;

        default:
                break;
    }

    switch (witem->anchor) {
    case GTK_ANCHOR_NW:
    case GTK_ANCHOR_N:
    case GTK_ANCHOR_NE:
        break;

    case GTK_ANCHOR_W:
    case GTK_ANCHOR_CENTER:
    case GTK_ANCHOR_E:
        witem->cy -= witem->cheight / 2;
        break;

    case GTK_ANCHOR_SW:
    case GTK_ANCHOR_S:
    case GTK_ANCHOR_SE:
        witem->cy -= witem->cheight;
        break;

        default:
                break;
    }

    /* Bounds */

    item->x1 = witem->cx;
    item->y1 = witem->cy;
    item->x2 = witem->cx + witem->cwidth;
    item->y2 = witem->cy + witem->cheight;

    if (witem->widget)
        gtk_layout_move (GTK_LAYOUT (item->canvas), witem->widget,
                 witem->cx + item->canvas->zoom_xofs,
                 witem->cy + item->canvas->zoom_yofs);
}

static void
do_destroy (GtkObject *object, gpointer data)
{
    GnomeCanvasWidget *witem;

    witem = data;

    witem->in_destroy = TRUE;

    gtk_object_destroy (data);
}

static void
gnome_canvas_widget_set_property (GObject            *object,
                  guint               param_id,
                  const GValue       *value,
                  GParamSpec         *pspec)
{
    GnomeCanvasItem *item;
    GnomeCanvasWidget *witem;
    GObject *obj;
    int update;
    int calc_bounds;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_WIDGET (object));

    item = GNOME_CANVAS_ITEM (object);
    witem = GNOME_CANVAS_WIDGET (object);

    update = FALSE;
    calc_bounds = FALSE;

    switch (param_id) {
    case PROP_WIDGET:
        if (witem->widget) {
            g_signal_handler_disconnect (witem->widget, witem->destroy_id);
            gtk_container_remove (GTK_CONTAINER (item->canvas), witem->widget);
        }

        obj = g_value_get_object (value);
        if (obj) {
            witem->widget = GTK_WIDGET (obj);
            witem->destroy_id = g_signal_connect (obj, "destroy",
                                  G_CALLBACK (do_destroy),
                                  witem);
            gtk_layout_put (GTK_LAYOUT (item->canvas), witem->widget,
                    witem->cx + item->canvas->zoom_xofs,
                    witem->cy + item->canvas->zoom_yofs);
        }

        update = TRUE;
        break;

    case PROP_X:
            if (witem->x != g_value_get_double (value))
        {
                witem->x = g_value_get_double (value);
            calc_bounds = TRUE;
        }
        break;

    case PROP_Y:
            if (witem->y != g_value_get_double (value))
        {
                witem->y = g_value_get_double (value);
            calc_bounds = TRUE;
        }
        break;

    case PROP_WIDTH:
            if (witem->width != fabs (g_value_get_double (value)))
        {
                witem->width = fabs (g_value_get_double (value));
            update = TRUE;
        }
        break;

    case PROP_HEIGHT:
            if (witem->height != fabs (g_value_get_double (value)))
        {
                witem->height = fabs (g_value_get_double (value));
            update = TRUE;
        }
        break;

    case PROP_ANCHOR:
            if (witem->anchor != g_value_get_enum (value))
        {
                witem->anchor = g_value_get_enum (value);
            update = TRUE;
        }
        break;

    case PROP_SIZE_PIXELS:
            if (witem->size_pixels != g_value_get_boolean (value))
        {
                witem->size_pixels = g_value_get_boolean (value);
            update = TRUE;
        }
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }

    if (update)
        (* GNOME_CANVAS_ITEM_GET_CLASS (item)->update) (item, NULL, NULL, 0);

    if (calc_bounds)
        recalc_bounds (witem);
}

static void
gnome_canvas_widget_get_property (GObject            *object,
                  guint               param_id,
                  GValue             *value,
                  GParamSpec         *pspec)
{
    GnomeCanvasWidget *witem;

    g_return_if_fail (object != NULL);
    g_return_if_fail (GNOME_IS_CANVAS_WIDGET (object));

    witem = GNOME_CANVAS_WIDGET (object);

    switch (param_id) {
    case PROP_WIDGET:
        g_value_set_object (value, (GObject *) witem->widget);
        break;

    case PROP_X:
        g_value_set_double (value, witem->x);
        break;

    case PROP_Y:
        g_value_set_double (value, witem->y);
        break;

    case PROP_WIDTH:
        g_value_set_double (value, witem->width);
        break;

    case PROP_HEIGHT:
        g_value_set_double (value, witem->height);
        break;

    case PROP_ANCHOR:
        g_value_set_enum (value, witem->anchor);
        break;

    case PROP_SIZE_PIXELS:
        g_value_set_boolean (value, witem->size_pixels);
        break;

    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
gnome_canvas_widget_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, int flags)
{
    GnomeCanvasWidget *witem;

    witem = GNOME_CANVAS_WIDGET (item);

    if (parent_class->update)
        (* parent_class->update) (item, affine, clip_path, flags);

    if (witem->widget) {
        if (witem->size_pixels) {
            witem->cwidth = (int) (witem->width + 0.5);
            witem->cheight = (int) (witem->height + 0.5);
        } else {
            witem->cwidth = (int) (witem->width * item->canvas->pixels_per_unit + 0.5);
            witem->cheight = (int) (witem->height * item->canvas->pixels_per_unit + 0.5);
        }

        gtk_widget_set_size_request (witem->widget, witem->cwidth, witem->cheight);
    } else {
        witem->cwidth = 0.0;
        witem->cheight = 0.0;
    }

    recalc_bounds (witem);
}

static void
gnome_canvas_widget_render (GnomeCanvasItem *item,
                GnomeCanvasBuf *buf)
{
#if 0
    GnomeCanvasWidget *witem;

    witem = GNOME_CANVAS_WIDGET (item);

    if (witem->widget) 
        gtk_widget_queue_draw (witem->widget);
#endif

}

static void
gnome_canvas_widget_draw (GnomeCanvasItem *item,
              GdkDrawable *drawable,
              int x, int y,
              int width, int height)
{
#if 0
    GnomeCanvasWidget *witem;

    witem = GNOME_CANVAS_WIDGET (item);

    if (witem->widget)
        gtk_widget_queue_draw (witem->widget);
#endif
}

static double
gnome_canvas_widget_point (GnomeCanvasItem *item, double x, double y,
               int cx, int cy, GnomeCanvasItem **actual_item)
{
    GnomeCanvasWidget *witem;
    double x1, y1, x2, y2;
    double dx, dy;

    witem = GNOME_CANVAS_WIDGET (item);

    *actual_item = item;

    gnome_canvas_c2w (item->canvas, witem->cx, witem->cy, &x1, &y1);

    x2 = x1 + (witem->cwidth - 1) / item->canvas->pixels_per_unit;
    y2 = y1 + (witem->cheight - 1) / item->canvas->pixels_per_unit;

    /* Is point inside widget bounds? */

    if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2))
        return 0.0;

    /* Point is outside widget bounds */

    if (x < x1)
        dx = x1 - x;
    else if (x > x2)
        dx = x - x2;
    else
        dx = 0.0;

    if (y < y1)
        dy = y1 - y;
    else if (y > y2)
        dy = y - y2;
    else
        dy = 0.0;

    return sqrt (dx * dx + dy * dy);
}

static void
gnome_canvas_widget_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
{
    GnomeCanvasWidget *witem;

    witem = GNOME_CANVAS_WIDGET (item);

    *x1 = witem->x;
    *y1 = witem->y;

    switch (witem->anchor) {
    case GTK_ANCHOR_NW:
    case GTK_ANCHOR_W:
    case GTK_ANCHOR_SW:
        break;

    case GTK_ANCHOR_N:
    case GTK_ANCHOR_CENTER:
    case GTK_ANCHOR_S:
        *x1 -= witem->width / 2.0;
        break;

    case GTK_ANCHOR_NE:
    case GTK_ANCHOR_E:
    case GTK_ANCHOR_SE:
        *x1 -= witem->width;
        break;

        default:
                break;
    }

    switch (witem->anchor) {
    case GTK_ANCHOR_NW:
    case GTK_ANCHOR_N:
    case GTK_ANCHOR_NE:
        break;

    case GTK_ANCHOR_W:
    case GTK_ANCHOR_CENTER:
    case GTK_ANCHOR_E:
        *y1 -= witem->height / 2.0;
        break;

    case GTK_ANCHOR_SW:
    case GTK_ANCHOR_S:
    case GTK_ANCHOR_SE:
        *y1 -= witem->height;
        break;

        default:
                break;
    }

    *x2 = *x1 + witem->width;
    *y2 = *y1 + witem->height;
}