a94327fd1b52bf7bb9d1f8b51c69f4715b7711fd
[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       clutter_actor_destroy (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   if (CLUTTER_ACTOR_IS_VISIBLE (priv->stage))
86     clutter_actor_queue_redraw (priv->stage);
87 }
88
89 static void
90 gtk_clutter_size_request (GtkWidget      *widget,
91                           GtkRequisition *req)
92 {
93   GtkClutterPrivate *priv;
94
95   priv = GTK_CLUTTER (widget)->priv;
96
97   req->width = clutter_actor_get_width (priv->stage);
98   req->height = clutter_actor_get_height (priv->stage);
99 }
100
101 static void
102 gtk_clutter_realize (GtkWidget *widget)
103 {
104   GtkClutterPrivate *priv;
105   const XVisualInfo *xvinfo;
106   GdkVisual *visual;
107   GdkColormap *colormap;
108
109   priv = GTK_CLUTTER (widget)->priv;
110
111   /* We need to use the colormap from the Clutter visual */
112   xvinfo = clutter_stage_get_xvisual (CLUTTER_STAGE (priv->stage));
113   visual = gdk_x11_screen_lookup_visual (gdk_screen_get_default (),
114                                          xvinfo->visualid);
115   colormap = gdk_colormap_new (visual, FALSE);
116   gtk_widget_set_colormap (widget, colormap);
117
118   /* And turn off double buffering, cos GL doesn't like it */
119   gtk_widget_set_double_buffered (widget, FALSE);
120
121   GTK_WIDGET_CLASS (gtk_clutter_parent_class)->realize (widget);
122
123   gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
124
125   priv = GTK_CLUTTER (widget)->priv;
126
127   clutter_stage_set_xwindow_foreign (CLUTTER_STAGE (priv->stage), 
128                                      GDK_WINDOW_XID (widget->window));
129 }
130
131 static void
132 gtk_clutter_class_init (GtkClutterClass *klass)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
136   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
137
138   object_class->destroy = gtk_clutter_destroy;
139
140   widget_class->size_request = gtk_clutter_size_request;
141   widget_class->size_allocate = gtk_clutter_size_allocate;
142   widget_class->realize = gtk_clutter_realize;
143
144   g_type_class_add_private (gobject_class, sizeof (GtkClutterPrivate));
145 }
146
147 static void
148 gtk_clutter_init (GtkClutter *clutter)
149 {
150   GtkClutterPrivate *priv;
151
152   clutter->priv = priv = GTK_CLUTTER_GET_PRIVATE (clutter);
153
154   gtk_widget_set_double_buffered (GTK_WIDGET (clutter), FALSE);
155
156   priv->stage = clutter_stage_get_default ();
157 }
158
159 /**
160  * gtk_clutter_get_stage:
161  * @clutter: A #GtkClutter object.
162  *
163  * Obtains the #ClutterStage associated with this object.
164  *
165  * Return value: A #ClutterActor.
166  */
167 ClutterActor *
168 gtk_clutter_get_stage (GtkClutter *clutter)
169 {
170   g_return_val_if_fail (GTK_IS_CLUTTER (clutter), NULL);
171
172   return clutter->priv->stage;
173 }
174
175 GtkWidget *
176 gtk_clutter_new (void)
177 {
178   return g_object_new (GTK_TYPE_CLUTTER, NULL);
179 }