2006-11-29 Emmanuele Bassi <ebassi@openedhand.com>
[clutter-gtk] / clutter-gtk / gtk-clutter.c
1 /*
2  * GTK-Clutter.
3  *
4  * GTK+ widget for Clutter.
5  *
6  * Authored By Iain Holmes  <iain@openedhand.com>
7  *
8  * Copyright (C) 2006 OpenedHand
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /**
27  * SECTION:gtk-clutter
28  * @short_description: GTK+ widget displaying a #ClutterStage.
29  *
30  * #GtkClutter is a GTK+ widget, derived from #GtkDrawingArea that contains a
31  * #ClutterStage, allowing it to be used in a GTK+ based program like any 
32  * normal GTK+ widget.
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <gdk/gdkx.h>
40
41 #include <gtk/gtkdrawingarea.h>
42 #include <gtk/gtkwidget.h>
43
44 #include <clutter/clutter-main.h>
45 #include <clutter/clutter-stage.h>
46
47 #include "gtk-clutter.h"
48
49 #define GTK_CLUTTER_GET_PRIVATE(obj) \
50 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CLUTTER, GtkClutterPrivate))
51
52 struct _GtkClutterPrivate
53 {
54   ClutterActor *stage;
55 };
56
57 G_DEFINE_TYPE (GtkClutter, gtk_clutter, GTK_TYPE_DRAWING_AREA);
58
59 static void
60 gtk_clutter_destroy (GtkObject *object)
61 {
62   GtkClutterPrivate *priv;
63
64   priv = GTK_CLUTTER (object)->priv;
65
66   if (priv->stage)
67     {
68       g_object_unref (G_OBJECT (priv->stage));
69       priv->stage = NULL;
70     }
71
72   GTK_OBJECT_CLASS (gtk_clutter_parent_class)->destroy (object);
73 }
74
75 static void
76 gtk_clutter_size_allocate (GtkWidget     *widget,
77                            GtkAllocation *allocation)
78 {
79   GtkClutterPrivate *priv = GTK_CLUTTER (widget)->priv;
80
81   clutter_actor_set_size (priv->stage,
82                           allocation->width,
83                           allocation->height);
84
85   clutter_actor_queue_redraw (priv->stage);
86 }
87
88 static void
89 gtk_clutter_size_request (GtkWidget      *widget,
90                           GtkRequisition *req)
91 {
92   GtkClutterPrivate *priv;
93
94   priv = GTK_CLUTTER (widget)->priv;
95
96   req->width = clutter_actor_get_width (priv->stage);
97   req->height = clutter_actor_get_height (priv->stage);
98 }
99
100 static void
101 gtk_clutter_realize (GtkWidget *widget)
102 {
103   GtkClutterPrivate *priv;
104   const XVisualInfo *xvinfo;
105   GdkVisual *visual;
106   GdkColormap *colormap;
107
108   priv = GTK_CLUTTER (widget)->priv;
109
110   /* We need to use the colormap from the Clutter visual */
111   xvinfo = clutter_stage_get_xvisual (CLUTTER_STAGE (priv->stage));
112   visual = gdk_x11_screen_lookup_visual (gdk_screen_get_default (),
113                                          xvinfo->visualid);
114   colormap = gdk_colormap_new (visual, FALSE);
115   gtk_widget_set_colormap (widget, colormap);
116
117   /* And turn off double buffering, cos GL doesn't like it */
118   gtk_widget_set_double_buffered (widget, FALSE);
119
120   GTK_WIDGET_CLASS (gtk_clutter_parent_class)->realize (widget);
121
122   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
123
124   priv = GTK_CLUTTER (widget)->priv;
125
126   clutter_stage_set_xwindow_foreign (CLUTTER_STAGE (priv->stage), 
127                                      GDK_WINDOW_XID (widget->window));
128   
129   /* force a realize */
130   clutter_actor_realize (priv->stage);
131 }
132
133 static void
134 gtk_clutter_class_init (GtkClutterClass *klass)
135 {
136   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
138   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
139
140   object_class->destroy = gtk_clutter_destroy;
141
142   widget_class->size_request = gtk_clutter_size_request;
143   widget_class->size_allocate = gtk_clutter_size_allocate;
144   widget_class->realize = gtk_clutter_realize;
145
146   g_type_class_add_private (gobject_class, sizeof (GtkClutterPrivate));
147 }
148
149 static void
150 gtk_clutter_init (GtkClutter *clutter)
151 {
152   GtkClutterPrivate *priv;
153
154   clutter->priv = priv = GTK_CLUTTER_GET_PRIVATE (clutter);
155
156   gtk_widget_set_double_buffered (GTK_WIDGET (clutter), FALSE);
157
158   priv->stage = clutter_stage_get_default ();
159 }
160
161 /**
162  * gtk_clutter_get_stage:
163  * @clutter: A #GtkClutter object.
164  *
165  * Obtains the #ClutterStage associated with this object.
166  *
167  * Return value: A #ClutterActor.
168  */
169 ClutterActor *
170 gtk_clutter_get_stage (GtkClutter *clutter)
171 {
172   g_return_val_if_fail (GTK_IS_CLUTTER (clutter), NULL);
173
174   return clutter->priv->stage;
175 }
176
177 GtkWidget *
178 gtk_clutter_new (void)
179 {
180   return g_object_new (GTK_TYPE_CLUTTER, NULL);
181 }