]> git.cworth.org Git - wordgame/blob - demo-item.c
Increase the window size a bit
[wordgame] / demo-item.c
1 /*
2  * GooCanvas Demo. Copyright (C) 2006 Damon Chaplin.
3  * Released under the GNU LGPL license. See COPYING for details.
4  *
5  * demo-item.c - a simple demo item.
6  */
7 #include <math.h>
8
9 #include "goocanvas.h"
10 #include "demo-item.h"
11
12 /* Use the GLib convenience macro to define the type. GooDemoItem is the
13    class struct, goo_demo_item is the function prefix, and our class is a
14    subclass of GOO_TYPE_CANVAS_ITEM_SIMPLE. */
15 G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE)
16
17 /* The standard object initialization function. */
18     static void
19 goo_demo_item_init (GooDemoItem *demo_item)
20 {
21     demo_item->width = 0.0;
22     demo_item->height = 0.0;
23 }
24
25 /* The convenience function to create new items. This should start with a 
26    parent argument and end with a variable list of object properties to fit
27    in with the standard canvas items. */
28 GooCanvasItem*
29 goo_demo_item_new (GooCanvasItem        *parent,
30                    gdouble               x,
31                    gdouble               y,
32                    gdouble               width,
33                    gdouble               height,
34                    GooDemoItemPaintFunc  paint,
35                    void                 *closure,
36                    ...)
37 {
38     GooCanvasItem *item;
39     GooDemoItem *demo_item;
40     const char *first_property;
41     va_list var_args;
42     cairo_matrix_t matrix;
43
44     item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL);
45
46     demo_item = (GooDemoItem*) item;
47     demo_item->width = width;
48     demo_item->height = height;
49     demo_item->paint = paint;
50     demo_item->closure = closure;
51
52     va_start (var_args, closure);
53     first_property = va_arg (var_args, char*);
54     if (first_property)
55         g_object_set_valist ((GObject*) item, first_property, var_args);
56     va_end (var_args);
57
58     if (parent)
59     {
60         goo_canvas_item_add_child (parent, item, -1);
61         g_object_unref (item);
62     }
63
64     cairo_matrix_init_translate (&matrix, x, y);
65     goo_canvas_item_set_transform (item, &matrix);
66
67     return item;
68 }
69
70 /* The update method. This is called when the canvas is initially shown and
71    also whenever the object is updated and needs to change its size and/or
72    shape. It should calculate its new bounds, storing them in simple->bounds,
73    and it should convert these to device coordinates. */
74 static void
75 goo_demo_item_update  (GooCanvasItemSimple *simple,
76                        cairo_t             *cr)
77 {
78     GooDemoItem *demo_item = (GooDemoItem*) simple;
79
80     /* Compute the new bounds. */
81     simple->bounds.x1 = 0;
82     simple->bounds.y1 = 0;
83     simple->bounds.x2 = demo_item->width;
84     simple->bounds.y2 = demo_item->height;
85
86     /* Convert to device coordinates. */
87     goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds);
88 }
89
90 /* The paint method. This should draw the item on the given cairo_t, using
91    the item's own coordinate space. */
92 static void
93 goo_demo_item_paint (GooCanvasItemSimple *simple,
94                      cairo_t             *cr,
95                      GooCanvasBounds     *bounds)
96 {
97     GooDemoItem *item = (GooDemoItem*) simple;
98
99     item->paint (cr, item->closure, item->width, item->height);
100 }
101
102 /* Hit detection. This should check if the given coordinate (in the item's
103    coordinate space) is within the item. If it is it should return the item,
104    otherwise it should return NULL. */
105 static GooCanvasItem*
106 goo_demo_item_get_item_at (GooCanvasItemSimple *simple,
107                            gdouble              x,
108                            gdouble              y,
109                            cairo_t             *cr,
110                            gboolean             is_pointer_event)
111 {
112     GooDemoItem *demo_item = (GooDemoItem*) simple;
113
114     if (x < 0 || (x > demo_item->width)
115         || y < 0 || (y > demo_item->height))
116         return NULL;
117
118     return (GooCanvasItem*) simple;
119 }
120
121 /* The class initialization function. Here we set the class' update(), paint()
122    and get_item_at() methods. */
123 static void
124 goo_demo_item_class_init (GooDemoItemClass *klass)
125 {
126     GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass;
127
128     simple_class->update        = goo_demo_item_update;
129     simple_class->paint         = goo_demo_item_paint;
130     simple_class->get_item_at   = goo_demo_item_get_item_at;
131 }