2007-06-11 Neil Jagdish Patel <njp@o-hand.com>
[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_rotate_x (app->hand, 
30                           gtk_spin_button_get_value (button),
31                           clutter_actor_get_height (app->hand),
32                           0);
33 }
34
35 static void
36 on_y_changed (GtkSpinButton *button, EventApp *app)
37 {
38   clutter_actor_rotate_y (app->hand, 
39                           gtk_spin_button_get_value (button),
40                           clutter_actor_get_width (app->hand) /2,
41                           0);
42 }
43
44 static void
45 on_z_changed (GtkSpinButton *button, EventApp *app)
46 {
47   clutter_actor_rotate_z (app->hand, 
48                           gtk_spin_button_get_value (button),
49                           clutter_actor_get_width (app->hand) /2,
50                           clutter_actor_get_height (app->hand)/2);
51 }
52
53 static void
54 on_opacity_changed (GtkSpinButton *button, EventApp *app)
55 {
56   clutter_actor_set_opacity (app->hand, gtk_spin_button_get_value (button)); 
57 }
58
59 /* Set the clutter colors form the current gtk theme */
60 static void
61 create_colors (EventApp *app, ClutterColor *stage, ClutterColor *text)
62 {
63   GtkStyle *style = gtk_widget_get_style (app->window);
64   GdkColor color;
65
66   /* Set the stage color to base[NORMAL] */
67   color = style->bg[GTK_STATE_NORMAL];
68   stage->red = (guint8) ((color.red/65535.0) * 255);
69   stage->green = (guint8) ((color.green/65535.0) * 255);
70   stage->blue  = (guint8) ((color.blue/65535.0) * 255);
71   g_print ("%d %d %d\n", stage->red, stage->green, stage->blue);
72
73   /* Now the text color */
74   color = style->text[GTK_STATE_NORMAL];
75   text->red =(guint8) ((color.red/65535.0) * 255);
76   text->green = (guint8) ((color.green/65535.0) * 255);
77   text->blue = (guint8) ((color.blue/65535.0) * 255);
78 }
79   
80 gint
81 main (gint argc, gchar **argv)
82 {
83   EventApp      *app = g_new0 (EventApp, 1);
84   GtkWidget     *widget, *vbox, *hbox, *button, *label, *box;
85   ClutterActor  *actor;
86   GdkPixbuf     *pixbuf = NULL;
87   guint          width, height;
88   ClutterColor   stage_color = {255, 255, 255, 255};
89   ClutterColor   text_color = {0, 0, 0, 255};
90
91   clutter_init (&argc, &argv);
92   gtk_init (&argc, &argv);
93
94
95   /* Create the inital gtk window and widgets, just like normal */
96   widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
97   app->window = widget;
98   gtk_window_set_title (GTK_WINDOW (widget), "Gtk-Clutter Interaction demo");
99   gtk_window_set_default_size (GTK_WINDOW (widget), 800, 600);
100   gtk_window_set_resizable (GTK_WINDOW (widget), FALSE);
101   gtk_container_set_border_width (GTK_CONTAINER (widget), 12);
102   g_signal_connect (G_OBJECT (widget), "destroy-event",
103                     G_CALLBACK (gtk_main_quit), NULL);
104  
105   /* Create our layout box */
106   vbox = gtk_vbox_new (FALSE, 12);
107   gtk_container_add (GTK_CONTAINER (app->window), vbox);
108
109   widget = gtk_entry_new ();
110   app->gtk_entry = widget;
111   gtk_entry_set_text (GTK_ENTRY (widget), "Enter some text");
112   gtk_box_pack_start (GTK_BOX (vbox), widget, FALSE, FALSE, 0);
113   g_signal_connect (G_OBJECT (box), "changed",
114                     G_CALLBACK (on_gtk_entry_changed), app);
115
116   hbox = gtk_hbox_new (FALSE, 12);
117   gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
118   
119   /* Set up clutter & create our stage */
120   create_colors (app, &stage_color, &text_color);
121   widget = gtk_clutter_new ();
122   gtk_box_pack_start (GTK_BOX (hbox), widget, TRUE, TRUE, 0);
123   app->stage = gtk_clutter_get_stage (GTK_CLUTTER (widget));
124   clutter_stage_set_color (CLUTTER_STAGE (app->stage), &stage_color);
125
126   /* Create the main texture that the spin buttons manipulate */
127   pixbuf = gdk_pixbuf_new_from_file ("redhand.png", NULL);
128   if (pixbuf == NULL)
129     g_error ("Unable to load pixbuf\n");
130
131   actor = clutter_texture_new_from_pixbuf (pixbuf);
132   app->hand = actor;
133   clutter_group_add (CLUTTER_GROUP (app->stage), actor);
134   clutter_actor_get_size (actor, &width, &height);
135   clutter_actor_set_position (actor,
136                               (CLUTTER_STAGE_WIDTH ()/2) - (width/2),
137                               (CLUTTER_STAGE_HEIGHT ()/2) - (height/2));
138
139   /* Setup the clutter entry */
140   actor = clutter_entry_new_full ("Sans 10", "", &text_color);
141   app->clutter_entry = actor;
142   clutter_group_add (CLUTTER_GROUP (app->stage), actor);
143   clutter_actor_set_position (actor, 0, 0);
144   clutter_actor_set_size (actor, 500, 20);
145
146   /* Create our adjustment widgets */
147   vbox = gtk_vbox_new (FALSE, 6);
148   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
149
150   box = gtk_hbox_new (TRUE, 6);
151   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
152   label = gtk_label_new ("Rotate x-axis");
153   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
154   button = gtk_spin_button_new_with_range (0, 360, 1);
155   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
156   g_signal_connect (G_OBJECT (button), "value-changed", 
157                     G_CALLBACK (on_x_changed), app);
158   
159   box = gtk_hbox_new (TRUE, 6);
160   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
161   label = gtk_label_new ("Rotate y-axis");
162   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
163   button = gtk_spin_button_new_with_range (0, 360, 1);
164   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
165   g_signal_connect (G_OBJECT (button), "value-changed", 
166                     G_CALLBACK (on_y_changed), app);
167
168   box = gtk_hbox_new (TRUE, 6);
169   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
170   label = gtk_label_new ("Rotate z-axis");
171   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
172   button = gtk_spin_button_new_with_range (0, 360, 1);
173   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
174   g_signal_connect (G_OBJECT (button), "value-changed", 
175                     G_CALLBACK (on_z_changed), app);
176
177   box = gtk_hbox_new (TRUE, 6);
178   gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, TRUE, 0);
179   label = gtk_label_new ("Adjust opacity");
180   gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
181   button = gtk_spin_button_new_with_range (0, 255, 1);
182   gtk_spin_button_set_value (GTK_SPIN_BUTTON (button), 255);
183   gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
184   g_signal_connect (G_OBJECT (button), "value-changed", 
185                     G_CALLBACK (on_opacity_changed), app);
186
187   clutter_actor_show_all (app->stage);
188   gtk_widget_show_all (app->window);
189
190   gtk_main ();
191   
192   return 0;
193 }