1 /*
2  * Copyright 2009 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU Lesser General Public License,
6  * version 2.1, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  * Boston, MA 02111-1307, USA.
17  *
18  */
19 #include <stdlib.h>
20 #include <clutter/clutter.h>
21 
22 #define INSTRUCTIONS \
23         "Press v\t\342\236\236\tSwitch horizontal/vertical\n"           \
24         "Press h\t\342\236\236\tToggle homogeneous\n"			\
25         "Press p\t\342\236\236\tToggle pack start/end\n"                \
26         "Press s\t\342\236\236\tIncrement spacing (up to 12px)\n"       \
27         "Press +\t\342\236\236\tAdd a new actor\n"                      \
28         "Press a\t\342\236\236\tToggle animations\n"                    \
29         "Press q\t\342\236\236\tQuit"
30 
31 
32 static const gchar *
get_align_name(ClutterActorAlign align)33 get_align_name (ClutterActorAlign align)
34 {
35   switch (align)
36     {
37     case CLUTTER_ACTOR_ALIGN_FILL:
38       return "fill";
39 
40     case CLUTTER_ACTOR_ALIGN_START:
41       return "start";
42 
43     case CLUTTER_ACTOR_ALIGN_CENTER:
44       return "center";
45 
46     case CLUTTER_ACTOR_ALIGN_END:
47       return "end";
48 
49     default:
50       g_assert_not_reached ();
51     }
52 }
53 
54 static gboolean
button_release_cb(ClutterActor * rect,ClutterEvent * event,gpointer user_data)55 button_release_cb (ClutterActor *rect,
56                    ClutterEvent *event,
57                    gpointer      user_data)
58 {
59   ClutterActorAlign x_align, y_align;
60   gboolean x_expand, y_expand;
61 
62   g_object_get (rect,
63                 "x-align", &x_align,
64                 "y-align", &y_align,
65                 "x-expand", &x_expand,
66                 "y-expand", &y_expand,
67                 NULL);
68 
69   switch (clutter_event_get_button (event))
70     {
71     case CLUTTER_BUTTON_PRIMARY:
72       if (clutter_event_has_shift_modifier (event))
73         {
74           if (y_align < 3)
75             y_align += 1;
76           else
77             y_align = 0;
78           break;
79         }
80       else
81         {
82           if (x_align < 3)
83             x_align += 1;
84           else
85             x_align = 0;
86         }
87       break;
88 
89     case CLUTTER_BUTTON_SECONDARY:
90       if (clutter_event_has_shift_modifier (event))
91         y_expand = !y_expand;
92       else
93         x_expand = !x_expand;
94       break;
95 
96     default:
97       break;
98     }
99 
100   g_object_set (rect,
101                 "x-align", x_align,
102                 "y-align", y_align,
103                 "x-expand", x_expand,
104                 "y-expand", y_expand,
105                 NULL);
106   return TRUE;
107 }
108 
109 static void
changed_cb(ClutterActor * actor,GParamSpec * pspec,ClutterActor * text)110 changed_cb (ClutterActor *actor,
111             GParamSpec   *pspec,
112             ClutterActor *text)
113 {
114   ClutterActorAlign x_align, y_align;
115   gboolean x_expand, y_expand;
116   gchar *label;
117 
118   g_object_get (actor,
119                 "x-align", &x_align,
120                 "y-align", &y_align,
121                 "x-expand", &x_expand,
122                 "y-expand", &y_expand,
123                 NULL);
124 
125   label = g_strdup_printf ("%d,%d\n"
126                            "%s\n%s",
127                            x_expand, y_expand,
128                            get_align_name (x_align),
129                            get_align_name (y_align));
130   clutter_text_set_text (CLUTTER_TEXT (text), label);
131   free (label);
132 }
133 
134 static void
add_actor(ClutterActor * box,gint position)135 add_actor (ClutterActor *box,
136            gint          position)
137 {
138   ClutterActor *rect, *text;
139   ClutterColor color;
140   ClutterLayoutManager *layout;
141 
142   clutter_color_from_hls (&color,
143                           g_random_double_range (0.0, 360.0),
144                           0.5,
145                           0.5);
146   color.alpha = 255;
147 
148   layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
149                                    CLUTTER_BIN_ALIGNMENT_CENTER);
150   rect = clutter_actor_new ();
151   clutter_actor_set_layout_manager (rect, layout);
152   clutter_actor_set_background_color (rect, &color);
153   clutter_actor_set_reactive (rect, TRUE);
154   clutter_actor_set_size (rect, 32, 64);
155   clutter_actor_set_x_expand (rect, TRUE);
156   clutter_actor_set_y_expand (rect, TRUE);
157   clutter_actor_set_x_align (rect, CLUTTER_ACTOR_ALIGN_CENTER);
158   clutter_actor_set_y_align (rect, CLUTTER_ACTOR_ALIGN_CENTER);
159 
160   text = clutter_text_new_with_text ("Sans 8px", NULL);
161   clutter_text_set_line_alignment (CLUTTER_TEXT (text),
162                                    PANGO_ALIGN_CENTER);
163   clutter_actor_add_child (rect, text);
164 
165   g_signal_connect (rect, "button-release-event",
166                     G_CALLBACK (button_release_cb), NULL);
167   g_signal_connect (rect, "notify::x-expand",
168                     G_CALLBACK (changed_cb), text);
169   g_signal_connect (rect, "notify::y-expand",
170                     G_CALLBACK (changed_cb), text);
171   g_signal_connect (rect, "notify::x-align",
172                     G_CALLBACK (changed_cb), text);
173   g_signal_connect (rect, "notify::y-align",
174                     G_CALLBACK (changed_cb), text);
175   changed_cb (rect, NULL, text);
176 
177   clutter_actor_insert_child_at_index (box, rect, position);
178 }
179 
180 static gboolean
key_release_cb(ClutterActor * stage,ClutterEvent * event,ClutterActor * box)181 key_release_cb (ClutterActor *stage,
182                 ClutterEvent *event,
183                 ClutterActor *box)
184 {
185   ClutterBoxLayout *layout;
186   gboolean toggle;
187   guint spacing;
188 
189   layout = CLUTTER_BOX_LAYOUT (clutter_actor_get_layout_manager (box));
190 
191   switch (clutter_event_get_key_symbol (event))
192     {
193     case CLUTTER_KEY_a:
194       {
195         ClutterActorIter iter;
196         ClutterActor *child;
197 
198         clutter_actor_iter_init (&iter, box);
199         while (clutter_actor_iter_next (&iter, &child))
200           {
201             guint duration;
202 
203             duration = clutter_actor_get_easing_duration (child);
204             if (duration != 0)
205               duration = 0;
206             else
207               duration = 250;
208 
209             clutter_actor_set_easing_duration (child, duration);
210           }
211       }
212       break;
213 
214     case CLUTTER_KEY_v:
215       {
216         ClutterOrientation orientation;
217 
218         orientation = clutter_box_layout_get_orientation (layout);
219 
220         if (orientation == CLUTTER_ORIENTATION_HORIZONTAL)
221           orientation = CLUTTER_ORIENTATION_VERTICAL;
222         else
223           orientation = CLUTTER_ORIENTATION_HORIZONTAL;
224 
225         clutter_box_layout_set_orientation (layout, orientation);
226       }
227       break;
228 
229     case CLUTTER_KEY_h:
230       toggle = clutter_box_layout_get_homogeneous (layout);
231       clutter_box_layout_set_homogeneous (layout, !toggle);
232       break;
233 
234     case CLUTTER_KEY_p:
235       toggle = clutter_box_layout_get_pack_start (layout);
236       clutter_box_layout_set_pack_start (layout, !toggle);
237       break;
238 
239     case CLUTTER_KEY_s:
240       spacing = clutter_box_layout_get_spacing (layout);
241 
242       if (spacing > 12)
243         spacing = 0;
244       else
245         spacing++;
246 
247       clutter_box_layout_set_spacing (layout, spacing);
248       break;
249 
250     case CLUTTER_KEY_plus:
251       add_actor (box, g_random_int_range (0, clutter_actor_get_n_children (box)));
252       break;
253 
254     case CLUTTER_KEY_q:
255       clutter_main_quit ();
256       break;
257 
258     default:
259       return FALSE;
260     }
261 
262   return TRUE;
263 }
264 
265 int
main(int argc,char * argv[])266 main (int argc, char *argv[])
267 {
268   ClutterActor *stage, *box, *instructions;
269   ClutterLayoutManager *layout;
270   gint i;
271 
272   if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
273     return EXIT_FAILURE;
274 
275   stage = clutter_stage_new ();
276   clutter_stage_set_title (CLUTTER_STAGE (stage), "Box Layout");
277   clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
278 
279   /* make the stage a vbox */
280   layout = clutter_box_layout_new ();
281   clutter_box_layout_set_orientation (CLUTTER_BOX_LAYOUT (layout),
282                                       CLUTTER_ORIENTATION_VERTICAL);
283   clutter_actor_set_layout_manager (stage, layout);
284 
285   box = clutter_actor_new ();
286   clutter_actor_set_background_color (box, CLUTTER_COLOR_LightGray);
287   clutter_actor_set_x_expand (box, TRUE);
288   clutter_actor_set_y_expand (box, TRUE);
289   layout = clutter_box_layout_new ();
290   clutter_actor_set_layout_manager (box, layout);
291   clutter_actor_add_child (stage, box);
292 
293   instructions = clutter_text_new_with_text ("Sans 12px", INSTRUCTIONS);
294   clutter_actor_set_x_expand (instructions, TRUE);
295   clutter_actor_set_y_expand (instructions, FALSE);
296   clutter_actor_set_x_align (instructions, CLUTTER_ACTOR_ALIGN_START);
297   clutter_actor_set_margin_top (instructions, 4);
298   clutter_actor_set_margin_left (instructions, 4);
299   clutter_actor_set_margin_bottom (instructions, 4);
300   clutter_actor_add_child (stage, instructions);
301 
302   for (i = 0; i < 5; i++)
303     add_actor (box, i);
304 
305   g_signal_connect (stage, "destroy",
306                     G_CALLBACK (clutter_main_quit), NULL);
307   g_signal_connect (stage, "key-release-event",
308                     G_CALLBACK (key_release_cb), box);
309 
310   clutter_actor_show (stage);
311   clutter_main ();
312 
313   return EXIT_SUCCESS;
314 }
315