]> Pileus Git - ~andy/gtk/blob - gtk/gtkinvisible.c
GtkInvisible: move public members to private structure
[~andy/gtk] / gtk / gtkinvisible.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include <gdk/gdk.h>
29 #include "gtkinvisible.h"
30 #include "gtkprivate.h"
31 #include "gtkintl.h"
32
33 struct _GtkInvisiblePriv
34 {
35   GdkScreen    *screen;
36   gboolean      has_user_ref_count;
37 };
38
39 enum {
40   PROP_0,
41   PROP_SCREEN,
42   LAST_ARG
43 };
44
45 static void gtk_invisible_destroy       (GtkObject         *object);
46 static void gtk_invisible_realize       (GtkWidget         *widget);
47 static void gtk_invisible_style_set     (GtkWidget         *widget,
48                                          GtkStyle          *previous_style);
49 static void gtk_invisible_show          (GtkWidget         *widget);
50 static void gtk_invisible_size_allocate (GtkWidget         *widget,
51                                          GtkAllocation     *allocation);
52 static void gtk_invisible_set_property  (GObject           *object,
53                                          guint              prop_id,
54                                          const GValue      *value,
55                                          GParamSpec        *pspec);
56 static void gtk_invisible_get_property  (GObject           *object,
57                                          guint              prop_id,
58                                          GValue            *value,
59                                          GParamSpec        *pspec);
60
61 static GObject *gtk_invisible_constructor (GType                  type,
62                                            guint                  n_construct_properties,
63                                            GObjectConstructParam *construct_params);
64
65 G_DEFINE_TYPE (GtkInvisible, gtk_invisible, GTK_TYPE_WIDGET)
66
67 static void
68 gtk_invisible_class_init (GtkInvisibleClass *class)
69 {
70   GObjectClass   *gobject_class;
71   GtkObjectClass *object_class;
72   GtkWidgetClass *widget_class;
73
74   widget_class = (GtkWidgetClass*) class;
75   object_class = (GtkObjectClass*) class;
76   gobject_class = (GObjectClass*) class;
77
78   widget_class->realize = gtk_invisible_realize;
79   widget_class->style_set = gtk_invisible_style_set;
80   widget_class->show = gtk_invisible_show;
81   widget_class->size_allocate = gtk_invisible_size_allocate;
82
83   object_class->destroy = gtk_invisible_destroy;
84   gobject_class->set_property = gtk_invisible_set_property;
85   gobject_class->get_property = gtk_invisible_get_property;
86   gobject_class->constructor = gtk_invisible_constructor;
87
88   g_object_class_install_property (gobject_class,
89                                    PROP_SCREEN,
90                                    g_param_spec_object ("screen",
91                                                         P_("Screen"),
92                                                         P_("The screen where this window will be displayed"),
93                                                         GDK_TYPE_SCREEN,
94                                                         GTK_PARAM_READWRITE));
95
96   g_type_class_add_private (class, sizeof (GtkInvisiblePriv));
97 }
98
99 static void
100 gtk_invisible_init (GtkInvisible *invisible)
101 {
102   GtkInvisiblePriv *priv;
103   GdkColormap *colormap;
104
105   invisible->priv = G_TYPE_INSTANCE_GET_PRIVATE (invisible,
106                                                  GTK_TYPE_INVISIBLE,
107                                                  GtkInvisiblePriv);
108   priv = invisible->priv;
109
110   gtk_widget_set_has_window (GTK_WIDGET (invisible), TRUE);
111   _gtk_widget_set_is_toplevel (GTK_WIDGET (invisible), TRUE);
112
113   g_object_ref_sink (invisible);
114
115   priv->has_user_ref_count = TRUE;
116   priv->screen = gdk_screen_get_default ();
117
118   colormap = _gtk_widget_peek_colormap ();
119   if (colormap)
120     gtk_widget_set_colormap (GTK_WIDGET (invisible), colormap);
121 }
122
123 static void
124 gtk_invisible_destroy (GtkObject *object)
125 {
126   GtkInvisible *invisible = GTK_INVISIBLE (object);
127   GtkInvisiblePriv *priv = invisible->priv;
128
129   if (priv->has_user_ref_count)
130     {
131       priv->has_user_ref_count = FALSE;
132       g_object_unref (invisible);
133     }
134
135   GTK_OBJECT_CLASS (gtk_invisible_parent_class)->destroy (object);  
136 }
137
138 /**
139  * gtk_invisible_new_for_screen:
140  * @screen: a #GdkScreen which identifies on which
141  *          the new #GtkInvisible will be created.
142  *
143  * Creates a new #GtkInvisible object for a specified screen
144  *
145  * Return value: a newly created #GtkInvisible object
146  *
147  * Since: 2.2
148  **/
149 GtkWidget* 
150 gtk_invisible_new_for_screen (GdkScreen *screen)
151 {
152   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
153   
154   return g_object_new (GTK_TYPE_INVISIBLE, "screen", screen, NULL);
155 }
156
157 /**
158  * gtk_invisible_new:
159  * 
160  * Creates a new #GtkInvisible.
161  * 
162  * Return value: a new #GtkInvisible.
163  **/
164 GtkWidget*
165 gtk_invisible_new (void)
166 {
167   return g_object_new (GTK_TYPE_INVISIBLE, NULL);
168 }
169
170 /**
171  * gtk_invisible_set_screen:
172  * @invisible: a #GtkInvisible.
173  * @screen: a #GdkScreen.
174  *
175  * Sets the #GdkScreen where the #GtkInvisible object will be displayed.
176  *
177  * Since: 2.2
178  **/ 
179 void
180 gtk_invisible_set_screen (GtkInvisible *invisible,
181                           GdkScreen    *screen)
182 {
183   GtkInvisiblePriv *priv;
184   GtkWidget *widget;
185   GdkScreen *previous_screen;
186   gboolean was_realized;
187
188   g_return_if_fail (GTK_IS_INVISIBLE (invisible));
189   g_return_if_fail (GDK_IS_SCREEN (screen));
190
191   priv = invisible->priv;
192
193   if (screen == priv->screen)
194     return;
195
196   widget = GTK_WIDGET (invisible);
197
198   previous_screen = priv->screen;
199   was_realized = gtk_widget_get_realized (widget);
200
201   if (was_realized)
202     gtk_widget_unrealize (widget);
203
204   priv->screen = screen;
205   if (screen != previous_screen)
206     _gtk_widget_propagate_screen_changed (widget, previous_screen);
207   g_object_notify (G_OBJECT (invisible), "screen");
208   
209   if (was_realized)
210     gtk_widget_realize (widget);
211 }
212
213 /**
214  * gtk_invisible_get_screen:
215  * @invisible: a #GtkInvisible.
216  *
217  * Returns the #GdkScreen object associated with @invisible
218  *
219  * Return value: the associated #GdkScreen.
220  *
221  * Since: 2.2
222  **/
223 GdkScreen *
224 gtk_invisible_get_screen (GtkInvisible *invisible)
225 {
226   g_return_val_if_fail (GTK_IS_INVISIBLE (invisible), NULL);
227
228   return invisible->priv->screen;
229 }
230
231 static void
232 gtk_invisible_realize (GtkWidget *widget)
233 {
234   GdkWindow *parent;
235   GdkWindowAttr attributes;
236   gint attributes_mask;
237
238   gtk_widget_set_realized (widget, TRUE);
239
240   parent = gtk_widget_get_parent_window (widget);
241   if (parent == NULL)
242     parent = gtk_widget_get_root_window (widget);
243
244   attributes.x = -100;
245   attributes.y = -100;
246   attributes.width = 10;
247   attributes.height = 10;
248   attributes.window_type = GDK_WINDOW_TEMP;
249   attributes.wclass = GDK_INPUT_ONLY;
250   attributes.override_redirect = TRUE;
251   attributes.event_mask = gtk_widget_get_events (widget);
252
253   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
254
255   widget->window = gdk_window_new (parent, &attributes, attributes_mask);
256                                               
257   gdk_window_set_user_data (widget->window, widget);
258   
259   widget->style = gtk_style_attach (widget->style, widget->window);
260 }
261
262 static void
263 gtk_invisible_style_set (GtkWidget *widget,
264                          GtkStyle  *previous_style)
265 {
266   /* Don't chain up to parent implementation */
267 }
268
269 static void
270 gtk_invisible_show (GtkWidget *widget)
271 {
272   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
273   gtk_widget_map (widget);
274 }
275
276 static void
277 gtk_invisible_size_allocate (GtkWidget     *widget,
278                             GtkAllocation *allocation)
279 {
280   widget->allocation = *allocation;
281
282
283
284 static void 
285 gtk_invisible_set_property  (GObject      *object,
286                              guint         prop_id,
287                              const GValue *value,
288                              GParamSpec   *pspec)
289 {
290   GtkInvisible *invisible = GTK_INVISIBLE (object);
291   
292   switch (prop_id)
293     {
294     case PROP_SCREEN:
295       gtk_invisible_set_screen (invisible, g_value_get_object (value));
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300     }
301 }
302
303 static void 
304 gtk_invisible_get_property  (GObject      *object,
305                              guint         prop_id,
306                              GValue       *value,
307                              GParamSpec   *pspec)
308 {
309   GtkInvisible *invisible = GTK_INVISIBLE (object);
310   GtkInvisiblePriv *priv = invisible->priv;
311
312   switch (prop_id)
313     {
314     case PROP_SCREEN:
315       g_value_set_object (value, priv->screen);
316       break;
317     default:
318       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319       break;
320     }
321 }
322
323 /* We use a constructor here so that we can realize the invisible on
324  * the correct screen after the "screen" property has been set
325  */
326 static GObject*
327 gtk_invisible_constructor (GType                  type,
328                            guint                  n_construct_properties,
329                            GObjectConstructParam *construct_params)
330 {
331   GObject *object;
332
333   object = G_OBJECT_CLASS (gtk_invisible_parent_class)->constructor (type,
334                                                                      n_construct_properties,
335                                                                      construct_params);
336
337   gtk_widget_realize (GTK_WIDGET (object));
338
339   return object;
340 }