4 #include <clutter/clutter.h>
6 static const ClutterColor bg_color = { 0xcc, 0xcc, 0xcc, 0x99 };
8 static gboolean is_expanded = FALSE;
11 draw_background (ClutterCairoTexture *texture,
18 clutter_cairo_texture_get_surface_size (texture, &width, &height);
20 #define BG_ROUND_RADIUS 12
24 cairo_move_to (cr, BG_ROUND_RADIUS, y);
25 cairo_line_to (cr, width - BG_ROUND_RADIUS, y);
26 cairo_curve_to (cr, width, y, width, y, width, BG_ROUND_RADIUS);
27 cairo_line_to (cr, width, height - BG_ROUND_RADIUS);
28 cairo_curve_to (cr, width, height, width, height, width - BG_ROUND_RADIUS, height);
29 cairo_line_to (cr, BG_ROUND_RADIUS, height);
30 cairo_curve_to (cr, x, height, x, height, x, height - BG_ROUND_RADIUS);
31 cairo_line_to (cr, x, BG_ROUND_RADIUS);
32 cairo_curve_to (cr, x, y, x, y, BG_ROUND_RADIUS, y);
34 cairo_close_path (cr);
36 clutter_cairo_set_source_color (cr, &bg_color);
44 cairo_move_to (cr, BG_ROUND_RADIUS, y);
45 cairo_line_to (cr, width - BG_ROUND_RADIUS, y);
46 cairo_curve_to (cr, width, y, width, y, width, BG_ROUND_RADIUS);
47 cairo_line_to (cr, width, height - BG_ROUND_RADIUS);
48 cairo_curve_to (cr, width, height, width, height, width - BG_ROUND_RADIUS, height);
49 cairo_line_to (cr, BG_ROUND_RADIUS, height);
50 cairo_curve_to (cr, x, height, x, height, x, height - BG_ROUND_RADIUS);
51 cairo_line_to (cr, x, BG_ROUND_RADIUS);
52 cairo_curve_to (cr, x, y, x, y, BG_ROUND_RADIUS, y);
54 cairo_close_path (cr);
56 pat = cairo_pattern_create_linear (0, 0, 0, height);
57 cairo_pattern_add_color_stop_rgba (pat, 1, .85, .85, .85, 1);
58 cairo_pattern_add_color_stop_rgba (pat, .95, 1, 1, 1, 1);
59 cairo_pattern_add_color_stop_rgba (pat, .05, 1, 1, 1, 1);
60 cairo_pattern_add_color_stop_rgba (pat, 0, .85, .85, .85, 1);
62 cairo_set_source (cr, pat);
65 cairo_pattern_destroy (pat);
67 #undef BG_ROUND_RADIUS
73 on_box_enter (ClutterActor *box,
77 clutter_actor_set_opacity (emblem, 255);
79 return CLUTTER_EVENT_STOP;
83 on_box_leave (ClutterActor *box,
87 clutter_actor_set_opacity (emblem, 0);
89 return CLUTTER_EVENT_STOP;
93 on_rect_clicked (ClutterClickAction *action,
98 clutter_actor_set_size (box, 400, 400);
100 clutter_actor_set_size (box, 200, 200);
102 is_expanded = !is_expanded;
106 on_rect_long_press (ClutterClickAction *action,
108 ClutterLongPressState state,
113 case CLUTTER_LONG_PRESS_QUERY:
114 g_print ("*** long press: query ***\n");
117 case CLUTTER_LONG_PRESS_CANCEL:
118 g_print ("*** long press: cancel ***\n");
121 case CLUTTER_LONG_PRESS_ACTIVATE:
122 g_print ("*** long press: activate ***\n");
130 on_box_allocation_changed (ClutterActor *box,
131 const ClutterActorBox *allocation,
132 ClutterAllocationFlags flags,
133 ClutterActor *background)
135 gfloat new_width, new_height;
137 clutter_actor_box_get_size (allocation, &new_width, &new_height);
138 clutter_cairo_texture_set_surface_size (CLUTTER_CAIRO_TEXTURE (background),
141 clutter_cairo_texture_invalidate (CLUTTER_CAIRO_TEXTURE (background));
145 test_bin_layout_main (int argc, char *argv[])
147 ClutterActor *stage, *box, *rect;
148 ClutterLayoutManager *layout;
149 ClutterColor stage_color = { 0xe0, 0xf2, 0xfc, 0xff };
151 ClutterAction *action;
153 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
156 stage = clutter_stage_new ();
157 clutter_stage_set_title (CLUTTER_STAGE (stage), "BinLayout");
158 clutter_actor_set_background_color (stage, &stage_color);
159 clutter_actor_set_size (stage, 640, 480);
160 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
162 layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
163 CLUTTER_BIN_ALIGNMENT_CENTER);
165 box = clutter_box_new (layout);
166 clutter_container_add_actor (CLUTTER_CONTAINER (stage), box);
167 clutter_actor_set_anchor_point_from_gravity (box, CLUTTER_GRAVITY_CENTER);
168 clutter_actor_set_position (box, 320, 240);
169 clutter_actor_set_reactive (box, TRUE);
170 clutter_actor_set_easing_mode (box, CLUTTER_EASE_OUT_BOUNCE);
171 clutter_actor_set_easing_duration (box, 500);
172 clutter_actor_set_name (box, "box");
174 /* the contents of the texture are created every time the allocation
175 * of the box changes, to keep the background's size the same as the
178 rect = clutter_cairo_texture_new (100, 100);
179 g_signal_connect (rect, "draw", G_CALLBACK (draw_background), NULL);
181 /* first method: use clutter_box_pack() */
182 clutter_box_pack (CLUTTER_BOX (box), rect,
183 "x-align", CLUTTER_BIN_ALIGNMENT_FILL,
184 "y-align", CLUTTER_BIN_ALIGNMENT_FILL,
187 clutter_actor_lower_bottom (rect);
188 clutter_actor_set_name (rect, "background");
189 g_signal_connect (box, "allocation-changed",
190 G_CALLBACK (on_box_allocation_changed),
199 file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
200 tex = clutter_texture_new_from_file (file, &error);
202 g_error ("Unable to create texture: %s", error->message);
204 clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (tex), TRUE);
206 /* second method: use clutter_bin_layout_add() */
207 clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (layout), tex,
208 CLUTTER_BIN_ALIGNMENT_CENTER,
209 CLUTTER_BIN_ALIGNMENT_CENTER);
211 clutter_actor_raise (tex, rect);
212 clutter_actor_set_width (tex, 175);
213 clutter_actor_set_name (tex, "texture");
218 color = clutter_color_new (g_random_int_range (0, 255),
219 g_random_int_range (0, 255),
220 g_random_int_range (0, 255),
223 rect = clutter_rectangle_new_with_color (color);
225 /* third method: container_add() and set_alignment() */
226 clutter_container_add_actor (CLUTTER_CONTAINER (box), rect);
227 clutter_bin_layout_set_alignment (CLUTTER_BIN_LAYOUT (layout), rect,
228 CLUTTER_BIN_ALIGNMENT_END,
229 CLUTTER_BIN_ALIGNMENT_END);
231 clutter_actor_set_size (rect, 50, 50);
232 clutter_actor_set_opacity (rect, 0);
233 clutter_actor_set_reactive (rect, TRUE);
234 clutter_actor_raise_top (rect);
235 clutter_actor_set_name (rect, "emblem");
236 clutter_actor_set_easing_duration (rect, 150);
237 clutter_actor_set_easing_mode (rect, CLUTTER_LINEAR);
239 action = clutter_click_action_new ();
240 clutter_actor_add_action (rect, action);
241 g_signal_connect (action, "clicked", G_CALLBACK (on_rect_clicked), box);
242 g_signal_connect (action, "long-press", G_CALLBACK (on_rect_long_press), box);
243 g_signal_connect (box,
244 "enter-event", G_CALLBACK (on_box_enter),
246 g_signal_connect (box,
247 "leave-event", G_CALLBACK (on_box_leave),
250 rect = clutter_text_new ();
251 clutter_text_set_text (CLUTTER_TEXT (rect), "A simple test");
252 clutter_container_add_actor (CLUTTER_CONTAINER (box), rect);
253 clutter_bin_layout_set_alignment (CLUTTER_BIN_LAYOUT (layout), rect,
254 CLUTTER_BIN_ALIGNMENT_CENTER,
255 CLUTTER_BIN_ALIGNMENT_START);
256 clutter_actor_raise_top (rect);
257 clutter_actor_set_name (rect, "text");
259 clutter_actor_show_all (stage);
263 clutter_color_free (color);
268 G_MODULE_EXPORT const char *
269 test_bin_layout_describe (void)
271 return "BinLayout layout manager example";