Debian Codegraph @

tests/interactive/test-content.c

raw
1 #include <stdlib.h>
2 #include <gmodule.h>
3 #include <clutter/clutter.h>
4
5 typedef struct _ColorContent {
6 GObject parent_instance;
7
8 double red;
9 double green;
10 double blue;
11 double alpha;
12
13 float padding;
14 } ColorContent;
15
16 typedef struct _ColorContentClass {
17 GObjectClass parent_class;
18 } ColorContentClass;
19
20 static void clutter_content_iface_init (ClutterContentIface *iface);
21
22 G_DEFINE_TYPE_WITH_CODE (ColorContent, color_content, G_TYPE_OBJECT,
23 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_CONTENT,
24 clutter_content_iface_init))
25
26 static void
27 color_content_paint_content (ClutterContent *content,
28 ClutterActor *actor,
29 ClutterPaintNode *root)
30 {
31 ColorContent *self = (ColorContent *) content;
32 ClutterActorBox box, content_box;
33 ClutterColor color;
34 PangoLayout *layout;
35 PangoRectangle logical;
36 ClutterPaintNode *node;
37
38 #if 0
39 g_debug ("Painting content [%p] "
40 "{ r:%.2f, g:%.2f, b:%.2f, a:%.2f } "
41 "for actor [%p] (context: [%p])",
42 content,
43 self->red,
44 self->green,
45 self->blue,
46 self->alpha,
47 actor, context);
48 #endif
49
50 clutter_actor_get_content_box (actor, &content_box);
51
52 box = content_box;
53 box.x1 += self->padding;
54 box.y1 += self->padding;
55 box.x2 -= self->padding;
56 box.y2 -= self->padding;
57
58 color.alpha = self->alpha * 255;
59
60 color.red = self->red * 255;
61 color.green = self->green * 255;
62 color.blue = self->blue * 255;
63
64 node = clutter_color_node_new (&color);
65 clutter_paint_node_add_rectangle (node, &box);
66 clutter_paint_node_add_child (root, node);
67 clutter_paint_node_unref (node);
68
69 color.red = (1.0 - self->red) * 255;
70 color.green = (1.0 - self->green) * 255;
71 color.blue = (1.0 - self->blue) * 255;
72
73 layout = clutter_actor_create_pango_layout (actor, "A");
74 pango_layout_get_pixel_extents (layout, NULL, &logical);
75
76 node = clutter_text_node_new (layout, &color);
77
78 /* top-left */
79 box.x1 = clutter_actor_box_get_x (&content_box);
80 box.y1 = clutter_actor_box_get_y (&content_box);
81 box.x2 = box.x1 + logical.width;
82 box.y2 = box.y1 + logical.height;
83 clutter_paint_node_add_rectangle (node, &box);
84
85 /* top-right */
86 box.x1 = clutter_actor_box_get_x (&content_box)
87 + clutter_actor_box_get_width (&content_box)
88 - logical.width;
89 box.y1 = clutter_actor_box_get_y (&content_box);
90 box.x2 = box.x1 + logical.width;
91 box.y2 = box.y1 + logical.height;
92 clutter_paint_node_add_rectangle (node, &box);
93
94 /* bottom-right */
95 box.x1 = clutter_actor_box_get_x (&content_box)
96 + clutter_actor_box_get_width (&content_box)
97 - logical.width;
98 box.y1 = clutter_actor_box_get_y (&content_box)
99 + clutter_actor_box_get_height (&content_box)
100 - logical.height;
101 box.x2 = box.x1 + logical.width;
102 box.y2 = box.y1 + logical.height;
103 clutter_paint_node_add_rectangle (node, &box);
104
105 /* bottom-left */
106 box.x1 = clutter_actor_box_get_x (&content_box);
107 box.y1 = clutter_actor_box_get_y (&content_box)
108 + clutter_actor_box_get_height (&content_box)
109 - logical.height;
110 box.x2 = box.x1 + logical.width;
111 box.y2 = box.y1 + logical.height;
112 clutter_paint_node_add_rectangle (node, &box);
113
114 /* center */
115 box.x1 = clutter_actor_box_get_x (&content_box)
116 + (clutter_actor_box_get_width (&content_box) - logical.width) / 2.0;
117 box.y1 = clutter_actor_box_get_y (&content_box)
118 + (clutter_actor_box_get_height (&content_box) - logical.height) / 2.0;
119 box.x2 = box.x1 + logical.width;
120 box.y2 = box.y1 + logical.height;
121 clutter_paint_node_add_rectangle (node, &box);
122
123 clutter_paint_node_add_child (root, node);
124 clutter_paint_node_unref (node);
125
126 g_object_unref (layout);
127 }
128
129 static void
130 clutter_content_iface_init (ClutterContentIface *iface)
131 {
132 iface->paint_content = color_content_paint_content;
133 }
134
135 static void
136 color_content_class_init (ColorContentClass *klass)
137 {
138 }
139
140 static void
141 color_content_init (ColorContent *self)
142 {
143 }
144
145 static ClutterContent *
146 color_content_new (double red,
147 double green,
148 double blue,
149 double alpha,
150 float padding)
151 {
152 ColorContent *self = g_object_new (color_content_get_type (), NULL);
153
154 self->red = red;
155 self->green = green;
156 self->blue = blue;
157 self->alpha = alpha;
158 self->padding = padding;
159
160 return (ClutterContent *) self;
161 }
162
163 G_MODULE_EXPORT int
164 test_content_main (int argc, char *argv[])
165 {
166 ClutterActor *stage, *grid;
167 ClutterContent *content;
168 int i, n_rects;
169
170 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
171 return EXIT_FAILURE;
172
173 stage = clutter_stage_new ();
174 clutter_actor_set_name (stage, "Stage");
175 clutter_stage_set_title (CLUTTER_STAGE (stage), "Content");
176 clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
177 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
178 clutter_actor_show (stage);
179
180 grid = clutter_actor_new ();
181 clutter_actor_set_name (grid, "Grid");
182 clutter_actor_set_margin_top (grid, 12);
183 clutter_actor_set_margin_right (grid, 12);
184 clutter_actor_set_margin_bottom (grid, 12);
185 clutter_actor_set_margin_left (grid, 12);
186 clutter_actor_set_layout_manager (grid, clutter_flow_layout_new (CLUTTER_FLOW_HORIZONTAL));
187 clutter_actor_add_constraint (grid, clutter_bind_constraint_new (stage, CLUTTER_BIND_SIZE, 0.0));
188 clutter_actor_add_child (stage, grid);
189
190 content = color_content_new (g_random_double_range (0.0, 1.0),
191 g_random_double_range (0.0, 1.0),
192 g_random_double_range (0.0, 1.0),
193 1.0,
194 2.0);
195
196 n_rects = g_random_int_range (12, 24);
197 for (i = 0; i < n_rects; i++)
198 {
199 ClutterActor *box = clutter_actor_new ();
200 ClutterColor bg_color = {
201 g_random_int_range (0, 255),
202 g_random_int_range (0, 255),
203 g_random_int_range (0, 255),
204 255
205 };
206 char *name, *color;
207
208 color = clutter_color_to_string (&bg_color);
209 name = g_strconcat ("Box <", color, ">", NULL);
210 clutter_actor_set_name (box, name);
211
212 g_free (name);
213 g_free (color);
214
215 clutter_actor_set_background_color (box, &bg_color);
216 clutter_actor_set_content (box, content);
217 clutter_actor_set_size (box, 64, 64);
218
219 clutter_actor_add_child (grid, box);
220 }
221
222 clutter_main ();
223
224 g_object_unref (content);
225
226 return EXIT_SUCCESS;
227 }
228
229 G_MODULE_EXPORT const char *
230 test_content_describe (void)
231 {
232 return "A simple test for ClutterContent";
233 }