ec5fb9eb05d70fb76b6799bbd70d67fe77568088
[clutter-gtk] / examples / gtk-clutter-events.c
1 #include <gtk/gtk.h>
2 #include <clutter/clutter.h>
3
4 #include <clutter-gtk/clutter-gtk.h>
5
6 typedef struct {
7
8   GtkWidget    *window;
9   GtkWidget    *popup;
10   GtkWidget    *gtk_entry;
11
12   ClutterActor *stage;
13   ClutterActor *hand;
14   ClutterActor *clutter_entry;
15
16 } EventApp;
17
18 static void
19 on_gtk_entry_changed (GtkEditable *editable, EventApp *app)
20 {
21   const gchar *text = gtk_entry_get_text (GTK_ENTRY (editable));
22
23   clutter_entry_set_text (CLUTTER_ENTRY (app->clutter_entry), text);
24 }
25
26 static void
27 on_x_changed (GtkSpinButton *button, EventApp *app)
28 {
29   clutter_actor_set_rotation (app->hand, CLUTTER_X_AXIS,
30                               gtk_spin_button_get_value (button),
31                               0,
32                               clutter_actor_get_height (app->hand) / 2,
33                               0);
34 }
35
36 static void
37 on_y_changed (GtkSpinButton *button, EventApp *app)
38 {
39   clutter_actor_set_rotation (app->hand, CLUTTER_Y_AXIS,
40                               gtk_spin_button_get_value (button),
41                               clutter_actor_get_width (app->hand) / 2,
42                               0,
43                               0);
44 }
45
46 static void
47 on_z_changed (GtkSpinButton *button, EventApp *app)
48 {
49   clutter_actor_set_rotation (app->hand, CLUTTER_Z_AXIS,
50                               gtk_spin_button_get_value (button),
51                               clutter_actor_get_width (app->hand) / 2,
52                               clutter_actor_get_height (app->hand) / 2,
53                               0);
54 }
55
56 static void
57 on_opacity_changed (GtkSpinButton *button, EventApp *app)
58 {
59   clutter_actor_set_opacity (app->hand, gtk_spin_button_get_value (button)); 
60 }
61
62 /* Set the clutter colors form the current gtk theme */
63 static void
64 create_colors (EventApp *app, ClutterColor *stage, ClutterColor *text)
65 {
66   gtk_clutter_get_bg_color (app->window, GTK_STATE_NORMAL, stage);
67   gtk_clutter_get_text_color (app->window, GTK_STATE_NORMAL, text);
68 }
69
70 static gboolean
71 on_stage_capture (ClutterActor *actor,
72                   ClutterEvent *event,
73                   gpointer      dummy)
74 {
75   if (event->type == CLUTTER_BUTTON_RELEASE)
76     {
77       gint x, y;
78
79       clutter_event_get_coords (event, &x, &y);
80
81       g_print ("Event captured at (%d, %d)\n", x, y);
82     }
83
84   return FALSE;
85 }
86
87 static gboolean
88 on_hand_button_press (ClutterActor       *actor,
89                       ClutterButtonEvent *event,
90                       gpointer            dummy)
91 {
92   g_print ("Button press on hand ('%s')\n",
93            g_type_name (G_OBJECT_TYPE (actor)));
94
95   return FALSE;
96 }
97
98 gint
99 main (gint argc, gchar **argv)
100 {
101   EventApp      *app = g_new0 (EventApp, 1);
102   GtkWidget     *widget, *vbox, *hbox, *button, *label, *box;
103   ClutterActor  *actor;
104   GdkPixbuf     *pixbuf = NULL;
105   guint          width, height;
106   ClutterColor   stage_color = {255, 255, 255, 255};
107   ClutterColor   text_color = {0, 0, 0, 255};
108
109   if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
110     g_error ("Unable to initialize GtkClutter");
111
112   /* Create the inital gtk window and widgets, just like normal */
113   widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
114   app->window = widget;
115   gtk_window_set_title (GTK_WINDOW (widget), "Gtk-Clutter Interaction demo");
116   gtk_window_set_default_size (GTK_WINDOW (widget), 800, 600);
117   gtk_window_set_resizable (GTK_WINDOW (widget), FALSE);
118   gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
119   g_signal_connect (widget, "destroy", G_CALLBACK (gtk_main_quit), NULL);
120  
121   /* Create our layout box */
122   vbox = gtk_vbox_new (FALSE, 12);
123   gtk_container_add (GTK_CONTAINER (app->window), vbox);
124
125   widget = gtk_entry_new ();
126   app->gtk_entry = widget;
127   gtk_entry_set_text (GTK_ENTRY (widget), "Enter some text");
128   gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
129   g_signal_connect (widget, "changed", G_CALLBACK (on_gtk_entry_changed), app);
130
131   hbox = gtk_hbox_new (FALSE, 12);
132   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
133   
134   /* Set up clutter & create our stage */
135   create_colors (app, &stage_color, &text_color);
136   widget = gtk_clutter_embed_new ();
137   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
138   app->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (widget));
139   clutter_stage_set_color (CLUTTER_STAGE (app->stage), &stage_color);
140   gtk_widget_set_size_request (widget, 640, 480);
141   g_signal_connect (app->stage, "captured-event",
142                     G_CALLBACK (on_stage_capture),
143                     NULL);
144
145   /* Create the main texture that the spin buttons manipulate */
146   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
147   if (pixbuf == NULL)
148     g_error ("Unable to load pixbuf\n");
149
150   actor = gtk_clutter_texture_new_from_pixbuf (pixbuf);
151   app->hand = actor;
152   clutter_group_add (CLUTTER_GROUP (app->stage), actor);
153   clutter_actor_get_size (actor, &width, &height);
154   clutter_actor_set_position (actor,
155                               (CLUTTER_STAGE_WIDTH ()/2) - (width/2),
156                               (CLUTTER_STAGE_HEIGHT ()/2) - (height/2));
157   clutter_actor_set_reactive (actor, TRUE);
158   g_signal_connect (actor, "button-press-event",
159                     G_CALLBACK (on_hand_button_press),
160                     NULL);
161
162   /* Setup the clutter entry */
163   actor = clutter_entry_new_full ("Sans 10", "", &text_color);
164   app->clutter_entry = actor;
165   clutter_group_add (CLUTTER_GROUP (app->stage), actor);
166   clutter_actor_set_position (actor, 0, 0);
167   clutter_actor_set_size (actor, 500, 20);
168
169   /* Create our adjustment widgets */
170   vbox = gtk_vbox_new (FALSE, 6);
171   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
172
173   box = gtk_hbox_new (TRUE, 6);
174   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
175   label = gtk_label_new ("Rotate x-axis");
176   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
177   button = gtk_spin_button_new_with_range (0, 360, 1);
178   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
179   g_signal_connect (button, "value-changed", G_CALLBACK (on_x_changed), app);
180   
181   box = gtk_hbox_new (TRUE, 6);
182   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
183   label = gtk_label_new ("Rotate y-axis");
184   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
185   button = gtk_spin_button_new_with_range (0, 360, 1);
186   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
187   g_signal_connect (button, "value-changed", G_CALLBACK (on_y_changed), app);
188
189   box = gtk_hbox_new (TRUE, 6);
190   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
191   label = gtk_label_new ("Rotate z-axis");
192   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
193   button = gtk_spin_button_new_with_range (0, 360, 1);
194   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
195   g_signal_connect (button, "value-changed", G_CALLBACK (on_z_changed), app);
196
197   box = gtk_hbox_new (TRUE, 6);
198   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
199   label = gtk_label_new ("Adjust opacity");
200   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
201   button = gtk_spin_button_new_with_range (0, 255, 1);
202   gtk_spin_button_set_value (GTK_SPIN_BUTTON (button), 255);
203   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
204   g_signal_connect (button, "value-changed", G_CALLBACK (on_opacity_changed), app);
205
206   gtk_widget_show_all (app->window);
207
208   /* Only show/show_all the stage after parent show. widget_show will call
209    * show on the stage.
210   */
211   clutter_actor_show_all (app->stage);
212
213   gtk_main ();
214   
215   return 0;
216 }