1 #include <config.h>
2 #include <string.h>
3 #include <gtk/gtk.h>
4 #include <goocanvas.h>
5 
6 
7 static gboolean
on_motion_notify(GooCanvasItem * item,GooCanvasItem * target,GdkEventMotion * event,gpointer data)8 on_motion_notify (GooCanvasItem *item,
9 		  GooCanvasItem *target,
10 		  GdkEventMotion *event,
11 		  gpointer data)
12 {
13   GooCanvasItemModel *model = NULL;
14   char *item_id = NULL;
15 
16   if (target)
17     model = goo_canvas_item_get_model (target);
18 
19   if (model)
20     item_id = g_object_get_data (G_OBJECT (model), "id");
21 
22   if (item_id)
23     g_print ("%s item received 'motion-notify' signal\n", item_id);
24 
25   return FALSE;
26 }
27 
28 
29 static void
on_item_created(GooCanvas * view,GooCanvasItem * item,GooCanvasItemModel * model,gpointer data)30 on_item_created (GooCanvas          *view,
31 		 GooCanvasItem      *item,
32 		 GooCanvasItemModel *model,
33 		 gpointer            data)
34 {
35   g_signal_connect (item, "motion_notify_event",
36 		    G_CALLBACK (on_motion_notify), NULL);
37 }
38 
39 
40 static void
create_events_area(GooCanvasItemModel * root,gint area_num,GooCanvasPointerEvents pointer_events,gchar * label)41 create_events_area (GooCanvasItemModel     *root,
42 		    gint                    area_num,
43 		    GooCanvasPointerEvents  pointer_events,
44 		    gchar                  *label)
45 {
46   gint row = area_num / 3, col = area_num % 3;
47   gdouble x = col * 200, y = row * 150;
48   GooCanvasItemModel *rect;
49   char *view_id;
50   GooCanvasLineDash *dash;
51 
52   dash = goo_canvas_line_dash_new (2, 5.0, 5.0);
53 
54   /* Create invisible item. */
55   rect = goo_canvas_rect_model_new (root, x + 45, y + 35, 30, 30,
56 				    "fill-color", "red",
57 				    "visibility", GOO_CANVAS_ITEM_INVISIBLE,
58 				    "line-width", 5.0,
59 				    "pointer-events", pointer_events,
60 				    NULL);
61   view_id = g_strdup_printf ("%s invisible", label);
62   g_object_set_data_full (G_OBJECT (rect), "id", view_id, g_free);
63 
64   /* Display a thin rect around it to indicate it is there. */
65 #if 1
66   rect = goo_canvas_rect_model_new (root, x + 42.5, y + 32.5, 36, 36,
67 				    "line-dash", dash,
68 				    "line-width", 1.0,
69 				    "stroke-color", "gray",
70 				    NULL);
71 #endif
72 
73   /* Create unpainted item. */
74   rect = goo_canvas_rect_model_new (root, x + 85, y + 35, 30, 30,
75 				    "stroke-pattern", NULL,
76 				    "line-width", 5.0,
77 				    "pointer-events", pointer_events,
78 				    NULL);
79   view_id = g_strdup_printf ("%s unpainted", label);
80   g_object_set_data_full (G_OBJECT (rect), "id", view_id, g_free);
81 
82   /* Display a thin rect around it to indicate it is there. */
83 #if 1
84   rect = goo_canvas_rect_model_new (root, x + 82.5, y + 32.5, 36, 36,
85 				    "line-dash", dash,
86 				    "line-width", 1.0,
87 				    "stroke-color", "gray",
88 				    NULL);
89 #endif
90 
91   /* Create stroked item. */
92   rect = goo_canvas_rect_model_new (root, x + 125, y + 35, 30, 30,
93 				    "line-width", 5.0,
94 				    "pointer-events", pointer_events,
95 				    NULL);
96   view_id = g_strdup_printf ("%s stroked", label);
97   g_object_set_data_full (G_OBJECT (rect), "id", view_id, g_free);
98 
99   /* Create filled item. */
100   rect = goo_canvas_rect_model_new (root, x + 60, y + 75, 30, 30,
101 				    "fill-color", "red",
102 				    "stroke-pattern", NULL,
103 				    "line-width", 5.0,
104 				    "pointer-events", pointer_events,
105 				    NULL);
106   view_id = g_strdup_printf ("%s filled", label);
107   g_object_set_data_full (G_OBJECT (rect), "id", view_id, g_free);
108 
109   /* Create stroked & filled item. */
110   rect = goo_canvas_rect_model_new (root, x + 100, y + 75, 30, 30,
111 				    "fill-color", "red",
112 				    "line-width", 5.0,
113 				    "pointer-events", pointer_events,
114 				    NULL);
115   view_id = g_strdup_printf ("%s stroked & filled", label);
116   g_object_set_data_full (G_OBJECT (rect), "id", view_id, g_free);
117 
118   goo_canvas_text_model_new (root, label, x + 100, y + 130, -1,
119 			     GOO_CANVAS_ANCHOR_CENTER,
120 			     "font", "Sans 12",
121 			     "fill-color", "blue",
122 			     NULL);
123 
124   goo_canvas_line_dash_unref (dash);
125 }
126 
127 
128 GtkWidget *
create_events_page(void)129 create_events_page (void)
130 {
131   GtkWidget *vbox, *frame, *label, *canvas;
132   GooCanvasItemModel *root;
133 
134   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
135   gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
136   gtk_widget_show (vbox);
137 
138   /* Instructions */
139 
140   label = gtk_label_new ("Move the mouse over the items to check they receive the right motion events.\nThe first 2 items in each group are 1) invisible and 2) visible but unpainted.");
141   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
142   gtk_widget_show (label);
143 
144   /* Frame and canvas */
145 
146   frame = gtk_frame_new (NULL);
147   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
148   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
149   gtk_widget_show (frame);
150 
151   g_object_set (frame,
152 		"halign", GTK_ALIGN_CENTER,
153 		"valign", GTK_ALIGN_CENTER,
154 		NULL);
155 
156   canvas = goo_canvas_new ();
157 
158   g_signal_connect (canvas, "item_created",
159 		    G_CALLBACK (on_item_created), NULL);
160 
161   root = goo_canvas_group_model_new (NULL, NULL);
162 
163   gtk_widget_set_size_request (canvas, 600, 450);
164   goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 600, 450);
165   gtk_container_add (GTK_CONTAINER (frame), canvas);
166   gtk_widget_show (canvas);
167 
168   create_events_area (root, 0, GOO_CANVAS_EVENTS_NONE, "none");
169   create_events_area (root, 1, GOO_CANVAS_EVENTS_VISIBLE_PAINTED, "visible-painted");
170   create_events_area (root, 2, GOO_CANVAS_EVENTS_VISIBLE_FILL, "visible-fill");
171   create_events_area (root, 3, GOO_CANVAS_EVENTS_VISIBLE_STROKE, "visible-stroke");
172   create_events_area (root, 4, GOO_CANVAS_EVENTS_VISIBLE, "visible");
173   create_events_area (root, 5, GOO_CANVAS_EVENTS_PAINTED, "painted");
174   create_events_area (root, 6, GOO_CANVAS_EVENTS_FILL, "fill");
175   create_events_area (root, 7, GOO_CANVAS_EVENTS_STROKE, "stroke");
176   create_events_area (root, 8, GOO_CANVAS_EVENTS_ALL, "all");
177 
178   goo_canvas_set_root_item_model (GOO_CANVAS (canvas), root);
179   g_object_unref (root);
180 
181   return vbox;
182 }
183