6235d9921871cd76db9a746e713b0c6ead3f8260
[clutter-gtk] / examples / gtk-clutter-events.c
1 #include <gtk/gtk.h>
2 #include <clutter/clutter.h>
3
4 #include <clutter-gtk/gtk-clutter-embed.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   GtkStyle *style = gtk_widget_get_style (app->window);
67   GdkColor color;
68
69   /* Set the stage color to base[NORMAL] */
70   color = style->bg[GTK_STATE_NORMAL];
71   stage->red = (guint8) ((color.red/65535.0) * 255);
72   stage->green = (guint8) ((color.green/65535.0) * 255);
73   stage->blue  = (guint8) ((color.blue/65535.0) * 255);
74   
75   /* Now the text color */
76   color = style->text[GTK_STATE_NORMAL];
77   text->red =(guint8) ((color.red/65535.0) * 255);
78   text->green = (guint8) ((color.green/65535.0) * 255);
79   text->blue = (guint8) ((color.blue/65535.0) * 255);
80 }
81
82 static gboolean
83 on_stage_capture (ClutterActor *actor,
84                   ClutterEvent *event,
85                   gpointer      dummy)
86 {
87   if (event->type == CLUTTER_BUTTON_RELEASE)
88     {
89       gint x, y;
90
91       clutter_event_get_coords (event, &x, &y);
92
93       g_print ("Event captured at (%d, %d)\n", x, y);
94     }
95
96   return FALSE;
97 }
98
99 static gboolean
100 on_hand_button_press (ClutterActor       *actor,
101                       ClutterButtonEvent *event,
102                       gpointer            dummy)
103 {
104   g_print ("Button press on hand ('%s')\n",
105            g_type_name (G_OBJECT_TYPE (actor)));
106
107   return FALSE;
108 }
109
110 gint
111 main (gint argc, gchar **argv)
112 {
113   EventApp      *app = g_new0 (EventApp, 1);
114   GtkWidget     *widget, *vbox, *hbox, *button, *label, *box;
115   ClutterActor  *actor;
116   GdkPixbuf     *pixbuf = NULL;
117   guint          width, height;
118   ClutterColor   stage_color = {255, 255, 255, 255};
119   ClutterColor   text_color = {0, 0, 0, 255};
120
121   clutter_init (&argc, &argv);
122   
123   gtk_init (&argc, &argv);
124
125   /* Create the inital gtk window and widgets, just like normal */
126   widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
127   app->window = widget;
128   gtk_window_set_title (GTK_WINDOW (widget), "Gtk-Clutter Interaction demo");
129   gtk_window_set_default_size (GTK_WINDOW (widget), 800, 600);
130   gtk_window_set_resizable (GTK_WINDOW (widget), FALSE);
131   gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
132   g_signal_connect (widget, "destroy", G_CALLBACK (gtk_main_quit), NULL);
133  
134   /* Create our layout box */
135   vbox = gtk_vbox_new (FALSE, 12);
136   gtk_container_add (GTK_CONTAINER (app->window), vbox);
137
138   widget = gtk_entry_new ();
139   app->gtk_entry = widget;
140   gtk_entry_set_text (GTK_ENTRY (widget), "Enter some text");
141   gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
142   g_signal_connect (widget, "changed", G_CALLBACK (on_gtk_entry_changed), app);
143
144   hbox = gtk_hbox_new (FALSE, 12);
145   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
146   
147   /* Set up clutter & create our stage */
148   create_colors (app, &stage_color, &text_color);
149   widget = gtk_clutter_embed_new ();
150   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
151   app->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (widget));
152   clutter_stage_set_color (CLUTTER_STAGE (app->stage), &stage_color);
153   gtk_widget_set_size_request (widget, 640, 480);
154   g_signal_connect (app->stage, "captured-event",
155                     G_CALLBACK (on_stage_capture),
156                     NULL);
157
158   /* Create the main texture that the spin buttons manipulate */
159   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
160   if (pixbuf == NULL)
161     g_error ("Unable to load pixbuf\n");
162
163   actor = clutter_texture_new_from_pixbuf (pixbuf);
164   app->hand = actor;
165   clutter_group_add (CLUTTER_GROUP (app->stage), actor);
166   clutter_actor_get_size (actor, &width, &height);
167   clutter_actor_set_position (actor,
168                               (CLUTTER_STAGE_WIDTH ()/2) - (width/2),
169                               (CLUTTER_STAGE_HEIGHT ()/2) - (height/2));
170   clutter_actor_set_reactive (actor, TRUE);
171   g_signal_connect (actor, "button-press-event",
172                     G_CALLBACK (on_hand_button_press),
173                     NULL);
174
175   /* Setup the clutter entry */
176   actor = clutter_entry_new_full ("Sans 10", "", &text_color);
177   app->clutter_entry = actor;
178   clutter_group_add (CLUTTER_GROUP (app->stage), actor);
179   clutter_actor_set_position (actor, 0, 0);
180   clutter_actor_set_size (actor, 500, 20);
181
182   /* Create our adjustment widgets */
183   vbox = gtk_vbox_new (FALSE, 6);
184   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
185
186   box = gtk_hbox_new (TRUE, 6);
187   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
188   label = gtk_label_new ("Rotate x-axis");
189   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
190   button = gtk_spin_button_new_with_range (0, 360, 1);
191   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
192   g_signal_connect (button, "value-changed", G_CALLBACK (on_x_changed), app);
193   
194   box = gtk_hbox_new (TRUE, 6);
195   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
196   label = gtk_label_new ("Rotate y-axis");
197   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
198   button = gtk_spin_button_new_with_range (0, 360, 1);
199   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
200   g_signal_connect (button, "value-changed", G_CALLBACK (on_y_changed), app);
201
202   box = gtk_hbox_new (TRUE, 6);
203   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
204   label = gtk_label_new ("Rotate z-axis");
205   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
206   button = gtk_spin_button_new_with_range (0, 360, 1);
207   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
208   g_signal_connect (button, "value-changed", G_CALLBACK (on_z_changed), app);
209
210   box = gtk_hbox_new (TRUE, 6);
211   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
212   label = gtk_label_new ("Adjust opacity");
213   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
214   button = gtk_spin_button_new_with_range (0, 255, 1);
215   gtk_spin_button_set_value (GTK_SPIN_BUTTON (button), 255);
216   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
217   g_signal_connect (button, "value-changed", G_CALLBACK (on_opacity_changed), app);
218
219   clutter_actor_show_all (app->stage);
220   gtk_widget_show_all (app->window);
221
222   gtk_main ();
223   
224   return 0;
225 }