2007-03-22 Matthew Allum <mallum@openedhand.com>
[clutter-gtk] / clutter-gtk / clutter-gtk.c
1 /*
2  * Clutter-Gtk
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 #GtkSocket 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/gtkwidget.h>
42
43 #include <clutter/clutter-main.h>
44 #include <clutter/clutter-stage.h>
45
46 #include "clutter-gtk.h"
47
48 #define GTK_CLUTTER_GET_PRIVATE(obj) \
49 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CLUTTER, GtkClutterPrivate))
50
51 struct _GtkClutterPrivate
52 {
53   ClutterActor *stage;
54   gboolean embedded;
55 };
56
57 G_DEFINE_TYPE (GtkClutter, gtk_clutter, GTK_TYPE_SOCKET);
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   GTK_WIDGET_CLASS (gtk_clutter_parent_class)->size_allocate (widget, 
89                                                               allocation);
90 }
91
92 static void
93 gtk_clutter_size_request (GtkWidget      *widget,
94                           GtkRequisition *req)
95 {
96   GtkClutterPrivate *priv;
97
98   priv = GTK_CLUTTER (widget)->priv;
99
100   req->width = clutter_actor_get_width (priv->stage);
101   req->height = clutter_actor_get_height (priv->stage);
102 }
103
104 static void
105 gtk_clutter_map (GtkWidget *widget)
106 {
107   GtkClutterPrivate *priv;
108
109   priv = GTK_CLUTTER (widget)->priv;
110
111   if (priv->embedded == FALSE) {
112     gtk_socket_add_id (GTK_SOCKET (widget), 
113                        clutter_stage_get_xwindow (CLUTTER_STAGE(priv->stage)));
114     priv->embedded = TRUE;
115   }
116
117   GTK_WIDGET_CLASS (gtk_clutter_parent_class)->map (widget);
118 }
119
120 static void
121 gtk_clutter_class_init (GtkClutterClass *klass)
122 {
123   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
124   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
125   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
126
127   object_class->destroy = gtk_clutter_destroy;
128
129   widget_class->size_request = gtk_clutter_size_request;
130   widget_class->size_allocate = gtk_clutter_size_allocate;
131   widget_class->map = gtk_clutter_map;
132
133   g_type_class_add_private (gobject_class, sizeof (GtkClutterPrivate));
134 }
135
136 static void
137 gtk_clutter_init (GtkClutter *clutter)
138 {
139   GtkClutterPrivate *priv;
140
141   clutter->priv = priv = GTK_CLUTTER_GET_PRIVATE (clutter);
142
143   gtk_widget_set_double_buffered (GTK_WIDGET (clutter), FALSE);
144
145   priv->stage = clutter_stage_get_default ();
146   priv->embedded = FALSE;
147 }
148
149 /**
150  * gtk_clutter_get_stage:
151  * @clutter: A #GtkClutter object.
152  *
153  * Obtains the #ClutterStage associated with this object.
154  *
155  * Return value: A #ClutterActor.
156  */
157 ClutterActor *
158 gtk_clutter_get_stage (GtkClutter *clutter)
159 {
160   g_return_val_if_fail (GTK_IS_CLUTTER (clutter), NULL);
161
162   return clutter->priv->stage;
163 }
164
165 GtkWidget *
166 gtk_clutter_new (void)
167 {
168   return g_object_new (GTK_TYPE_CLUTTER, NULL);
169 }