]> git.cworth.org Git - wordgame/blob - demo-item.c
Add shell for rack-fancy (graphical version of rack)
[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 "goocanvas.h"
8 #include "demo-item.h"
9
10 /* Use the GLib convenience macro to define the type. GooDemoItem is the
11    class struct, goo_demo_item is the function prefix, and our class is a
12    subclass of GOO_TYPE_CANVAS_ITEM_SIMPLE. */
13 G_DEFINE_TYPE (GooDemoItem, goo_demo_item, GOO_TYPE_CANVAS_ITEM_SIMPLE)
14
15 /* The standard object initialization function. */
16     static void
17 goo_demo_item_init (GooDemoItem *demo_item)
18 {
19     demo_item->x = 0.0;
20     demo_item->y = 0.0;
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                    ...)
35 {
36     GooCanvasItem *item;
37     GooDemoItem *demo_item;
38     const char *first_property;
39     va_list var_args;
40
41     item = g_object_new (GOO_TYPE_DEMO_ITEM, NULL);
42
43     demo_item = (GooDemoItem*) item;
44     demo_item->x = x;
45     demo_item->y = y;
46     demo_item->width = width;
47     demo_item->height = height;
48
49     va_start (var_args, height);
50     first_property = va_arg (var_args, char*);
51     if (first_property)
52         g_object_set_valist ((GObject*) item, first_property, var_args);
53     va_end (var_args);
54
55     if (parent)
56     {
57         goo_canvas_item_add_child (parent, item, -1);
58         g_object_unref (item);
59     }
60
61     return item;
62 }
63
64 /* The update method. This is called when the canvas is initially shown and
65    also whenever the object is updated and needs to change its size and/or
66    shape. It should calculate its new bounds, storing them in simple->bounds,
67    and it should convert these to device coordinates. */
68 static void
69 goo_demo_item_update  (GooCanvasItemSimple *simple,
70                        cairo_t             *cr)
71 {
72     GooDemoItem *demo_item = (GooDemoItem*) simple;
73
74     /* Compute the new bounds. */
75     simple->bounds.x1 = demo_item->x;
76     simple->bounds.y1 = demo_item->y;
77     simple->bounds.x2 = demo_item->x + demo_item->width;
78     simple->bounds.y2 = demo_item->y + demo_item->height;
79
80     /* Convert to device coordinates. */
81     goo_canvas_item_simple_user_bounds_to_device (simple, cr, &simple->bounds);
82 }
83
84 /* The paint method. This should draw the item on the given cairo_t, using
85    the item's own coordinate space. */
86 static void
87 goo_demo_item_paint (GooCanvasItemSimple *simple,
88                      cairo_t             *cr,
89                      GooCanvasBounds     *bounds)
90 {
91     GooDemoItem *demo_item = (GooDemoItem*) simple;
92
93     cairo_move_to (cr, demo_item->x, demo_item->y);
94     cairo_line_to (cr, demo_item->x, demo_item->y + demo_item->height);
95     cairo_line_to (cr, demo_item->x + demo_item->width,
96                    demo_item->y + demo_item->height);
97     cairo_line_to (cr, demo_item->x + demo_item->width, demo_item->y);
98     cairo_close_path (cr);
99     goo_canvas_style_set_fill_options (simple->simple_data->style, cr);
100     cairo_fill (cr);
101 }
102
103 /* Hit detection. This should check if the given coordinate (in the item's
104    coordinate space) is within the item. If it is it should return the item,
105    otherwise it should return NULL. */
106 static GooCanvasItem*
107 goo_demo_item_get_item_at (GooCanvasItemSimple *simple,
108                            gdouble              x,
109                            gdouble              y,
110                            cairo_t             *cr,
111                            gboolean             is_pointer_event)
112 {
113     GooDemoItem *demo_item = (GooDemoItem*) simple;
114
115     if (x < demo_item->x || (x > demo_item->x + demo_item->width)
116         || y < demo_item->y || (y > demo_item->y + demo_item->height))
117         return NULL;
118
119     return (GooCanvasItem*) simple;
120 }
121
122 /* The class initialization function. Here we set the class' update(), paint()
123    and get_item_at() methods. */
124 static void
125 goo_demo_item_class_init (GooDemoItemClass *klass)
126 {
127     GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass;
128
129     simple_class->update        = goo_demo_item_update;
130     simple_class->paint         = goo_demo_item_paint;
131     simple_class->get_item_at   = goo_demo_item_get_item_at;
132 }