[zoomable] Fix Introspection data generation
[clutter-gtk] / examples / gtk-clutter-test.c
1 #include <gtk/gtk.h>
2 #include <clutter/clutter.h>
3 #include <math.h>
4
5 #include <clutter-gtk/clutter-gtk.h>
6
7 #define TRAILS 0
8 #define NHANDS  2
9 #define WINWIDTH   400
10 #define WINHEIGHT  400
11 #define RADIUS     150
12
13 typedef struct SuperOH
14 {
15   ClutterActor *hand[NHANDS], *bgtex;
16   ClutterGroup   *group;
17   GdkPixbuf      *bgpixb;
18
19 } SuperOH; 
20
21 static gboolean fade = FALSE;
22 static gboolean fullscreen = FALSE;
23
24 /* input handler */
25 void 
26 input_cb (ClutterStage *stage,
27           ClutterEvent *event,
28           gpointer      data)
29 {
30   if (event->type == CLUTTER_BUTTON_PRESS)
31     {
32       ClutterActor *a;
33       gfloat x, y;
34
35       clutter_event_get_coords (event, &x, &y);
36
37       a = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, x, y);
38       if (a && (CLUTTER_IS_TEXTURE (a) || CLUTTER_IS_CLONE (a)))
39         clutter_actor_hide (a);
40     }
41   else if (event->type == CLUTTER_KEY_PRESS)
42     {
43       g_print ("*** key press event (key:%c) ***\n",
44                clutter_event_get_key_symbol (event));
45       
46       if (clutter_event_get_key_symbol (event) == CLUTTER_q)
47         gtk_main_quit ();
48     }
49 }
50
51
52 /* Timeline handler */
53 void
54 frame_cb (ClutterTimeline *timeline, 
55           gint             msecs,
56           gpointer         data)
57 {
58   SuperOH        *oh = (SuperOH *)data;
59   gint            i;
60   guint           rotation = clutter_timeline_get_progress (timeline) * 360.0f;
61
62 #if TRAILS
63   oh->bgpixb = clutter_stage_snapshot (CLUTTER_STAGE (stage),
64                                        0, 0,
65                                        WINWIDTH,
66                                        WINHEIGHT);
67   clutter_texture_set_pixbuf (CLUTTER_TEXTURE (oh->bgtex), oh->bgpixb);
68   g_object_unref (G_OBJECT (oh->bgpixb));
69   g_object_unref (stage);
70 #endif
71
72   /* Rotate everything clockwise about stage center*/
73   clutter_actor_set_rotation (CLUTTER_ACTOR (oh->group),
74                               CLUTTER_Z_AXIS,
75                               rotation,
76                               WINWIDTH / 2, WINHEIGHT / 2, 0);
77
78   for (i = 0; i < NHANDS; i++)
79     {
80       /* rotate each hand around there centers */
81       clutter_actor_set_rotation (oh->hand[i],
82                                   CLUTTER_Z_AXIS,
83                                   - 6.0 * rotation,
84                                   clutter_actor_get_width (oh->hand[i]) / 2,
85                                   clutter_actor_get_height (oh->hand[i]) / 2,
86                                   0);
87       if (fade == TRUE)
88         clutter_actor_set_opacity (oh->hand[i], (255 - (rotation % 255)));
89     }
90
91   /*
92   clutter_actor_rotate_x (CLUTTER_ACTOR(oh->group),
93                             75.0,
94                             WINHEIGHT/2, 0);
95   */
96 }
97
98 static void
99 clickity (GtkButton *button,
100           gpointer ud)
101 {
102         fade = !fade;
103 }
104
105 static void
106 on_fullscreen (GtkButton *button,
107                GtkWindow *window)
108 {
109   if (!fullscreen)
110     {
111       gtk_window_fullscreen (window);
112       fullscreen = TRUE;
113     }
114   else
115     {
116       gtk_window_unfullscreen (window);
117       fullscreen = FALSE;
118     }
119 }
120
121 int
122 main (int argc, char *argv[])
123 {
124   ClutterTimeline *timeline;
125   ClutterActor    *stage;
126   ClutterColor     stage_color = { 0x61, 0x64, 0x8c, 0xff };
127   GtkWidget       *window, *clutter;
128   GtkWidget       *label, *button, *vbox;
129   GdkPixbuf       *pixbuf;
130   SuperOH         *oh;
131   gint             i;
132
133   if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
134     g_error ("Unable to initialize GtkClutter");
135
136   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
137
138   if (!pixbuf)
139     g_error("pixbuf load failed");
140
141   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
142   g_signal_connect (window, "destroy",
143                     G_CALLBACK (gtk_main_quit), NULL);
144
145   vbox = gtk_vbox_new (FALSE, 6);
146   gtk_container_add (GTK_CONTAINER (window), vbox);
147
148   clutter = gtk_clutter_embed_new ();
149   gtk_widget_set_size_request (clutter, WINWIDTH, WINHEIGHT);
150
151   gtk_container_add (GTK_CONTAINER (vbox), clutter);
152
153   stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (clutter));
154
155   label = gtk_label_new ("This is a label");
156   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
157
158   button = gtk_button_new_with_label ("This is a button...clicky");
159   g_signal_connect (button, "clicked",
160                     G_CALLBACK (clickity), NULL);
161   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
162
163   button = gtk_button_new_with_label ("Fullscreen");
164   gtk_button_set_image (GTK_BUTTON (button),
165                         gtk_image_new_from_stock (GTK_STOCK_FULLSCREEN,
166                                                   GTK_ICON_SIZE_BUTTON));
167   g_signal_connect (button, "clicked",
168                     G_CALLBACK (on_fullscreen),
169                     window);
170   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
171
172   button = gtk_button_new_from_stock (GTK_STOCK_QUIT);
173   g_signal_connect_swapped (button, "clicked",
174                             G_CALLBACK (gtk_widget_destroy),
175                             window);
176   gtk_box_pack_end (GTK_BOX (vbox), button, FALSE, FALSE, 0);
177   
178   /* and its background color */
179
180   clutter_stage_set_color (CLUTTER_STAGE (stage),
181                            &stage_color);
182
183   oh = g_new(SuperOH, 1);
184
185 #if TRAILS
186   oh->bgtex = clutter_texture_new();
187   clutter_actor_set_size (oh->bgtex, WINWIDTH, WINHEIGHT);
188   clutter_actor_set_opacity (oh->bgtex, 0x99);
189   clutter_group_add (CLUTTER_GROUP (stage), oh->bgtex);
190 #endif
191
192   /* create a new group to hold multiple actors in a group */
193   oh->group = CLUTTER_GROUP (clutter_group_new ());
194   
195   for (i = 0; i < NHANDS; i++)
196     {
197       gint x, y, w, h;
198 #if 1
199       /* Create a texture from pixbuf, then clone in to same resources */
200       if (i == 0)
201        oh->hand[i] = gtk_clutter_texture_new_from_pixbuf (pixbuf);
202      else
203        oh->hand[i] = clutter_clone_new (oh->hand[0]);
204 #else
205       ClutterColor colour = { 255, 0, 0, 255 };
206
207       oh->hand[i] = clutter_rectangle_new_with_color (&colour);
208       clutter_actor_set_size (oh->hand[i], 50, 50);
209 #endif
210       /* Place around a circle */
211       w = clutter_actor_get_width (oh->hand[0]);
212       h = clutter_actor_get_height (oh->hand[0]);
213
214       x = WINWIDTH/2  + RADIUS * cos (i * M_PI / (NHANDS/2)) - w/2;
215       y = WINHEIGHT/2 + RADIUS * sin (i * M_PI / (NHANDS/2)) - h/2;
216
217       clutter_actor_set_position (oh->hand[i], x, y);
218
219       /* Add to our group group */
220       clutter_group_add (oh->group, oh->hand[i]);
221     }
222
223   /* Add the group to the stage */
224   clutter_container_add_actor (CLUTTER_CONTAINER (stage),
225                                CLUTTER_ACTOR (oh->group));
226
227   g_signal_connect (stage, "button-press-event",
228                     G_CALLBACK (input_cb), 
229                     oh);
230   g_signal_connect (stage, "key-release-event",
231                     G_CALLBACK (input_cb),
232                     oh);
233
234   gtk_widget_show_all (window);
235
236   /* Only show the actors after parent show otherwise it will just be
237    * unrealized when the clutter foreign window is set. widget_show
238    * will call show on the stage.
239    */
240   clutter_actor_show_all (CLUTTER_ACTOR (oh->group));
241
242   /* Create a timeline to manage animation */
243   timeline = clutter_timeline_new (6000);
244   g_object_set(timeline, "loop", TRUE, NULL);   /* have it loop */
245
246   /* fire a callback for frame change */
247   g_signal_connect(timeline, "new-frame",  G_CALLBACK (frame_cb), oh);
248
249   /* and start it */
250   clutter_timeline_start (timeline);
251
252   gtk_main();
253
254   return 0;
255 }