]> Pileus Git - ~andy/gtk/blob - gtk/gtkinvisible.c
Fix bug 134982, reported by Morten Welinder:
[~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 <gdk/gdk.h>
28 #include "gtkinvisible.h"
29 #include "gtkintl.h"
30
31 enum {
32   PROP_0,
33   PROP_SCREEN,
34   LAST_ARG
35 };
36
37 static void gtk_invisible_class_init    (GtkInvisibleClass *klass);
38 static void gtk_invisible_init          (GtkInvisible      *invisible);
39 static void gtk_invisible_destroy       (GtkObject         *object);
40 static void gtk_invisible_realize       (GtkWidget         *widget);
41 static void gtk_invisible_style_set     (GtkWidget         *widget,
42                                          GtkStyle          *previous_style);
43 static void gtk_invisible_show          (GtkWidget         *widget);
44 static void gtk_invisible_size_allocate (GtkWidget         *widget,
45                                          GtkAllocation     *allocation);
46 static void gtk_invisible_set_property  (GObject           *object,
47                                          guint              prop_id,
48                                          const GValue      *value,
49                                          GParamSpec        *pspec);
50 static void gtk_invisible_get_property  (GObject           *object,
51                                          guint              prop_id,
52                                          GValue            *value,
53                                          GParamSpec        *pspec);
54
55 static GObject *gtk_invisible_constructor (GType                  type,
56                                            guint                  n_construct_properties,
57                                            GObjectConstructParam *construct_params);
58
59 static GObjectClass *parent_class;
60
61 GType
62 gtk_invisible_get_type (void)
63 {
64   static GType invisible_type = 0;
65
66   if (!invisible_type)
67     {
68       static const GTypeInfo invisible_info =
69       {
70         sizeof (GtkInvisibleClass),
71         NULL,           /* base_init */
72         NULL,           /* base_finalize */
73         (GClassInitFunc) gtk_invisible_class_init,
74         NULL,           /* class_finalize */
75         NULL,           /* class_data */
76         sizeof (GtkInvisible),
77         0,              /* n_preallocs */
78         (GInstanceInitFunc) gtk_invisible_init,
79       };
80
81       invisible_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkInvisible",
82                                                &invisible_info, 0);
83     }
84
85   return invisible_type;
86 }
87
88 static void
89 gtk_invisible_class_init (GtkInvisibleClass *class)
90 {
91   GObjectClass   *gobject_class;
92   GtkObjectClass *object_class;
93   GtkWidgetClass *widget_class;
94
95   widget_class = (GtkWidgetClass*) class;
96   object_class = (GtkObjectClass*) class;
97   gobject_class = (GObjectClass*) class;
98
99   parent_class = g_type_class_peek_parent (class);
100
101   widget_class->realize = gtk_invisible_realize;
102   widget_class->style_set = gtk_invisible_style_set;
103   widget_class->show = gtk_invisible_show;
104   widget_class->size_allocate = gtk_invisible_size_allocate;
105
106   object_class->destroy = gtk_invisible_destroy;
107   gobject_class->set_property = gtk_invisible_set_property;
108   gobject_class->get_property = gtk_invisible_get_property;
109   gobject_class->constructor = gtk_invisible_constructor;
110
111   g_object_class_install_property (gobject_class,
112                                    PROP_SCREEN,
113                                    g_param_spec_object ("screen",
114                                                         P_("Screen"),
115                                                         P_("The screen where this window will be displayed"),
116                                                         GDK_TYPE_SCREEN,
117                                                         G_PARAM_READWRITE));
118 }
119
120 static void
121 gtk_invisible_init (GtkInvisible *invisible)
122 {
123   GdkColormap *colormap;
124   
125   GTK_WIDGET_UNSET_FLAGS (invisible, GTK_NO_WINDOW);
126   GTK_WIDGET_SET_FLAGS (invisible, GTK_TOPLEVEL);
127
128   g_object_ref (invisible);
129   gtk_object_sink (GTK_OBJECT (invisible));
130
131   invisible->has_user_ref_count = TRUE;
132   invisible->screen = gdk_screen_get_default ();
133   
134   colormap = _gtk_widget_peek_colormap ();
135   if (colormap)
136     gtk_widget_set_colormap (GTK_WIDGET (invisible), colormap);
137 }
138
139 static void
140 gtk_invisible_destroy (GtkObject *object)
141 {
142   GtkInvisible *invisible = GTK_INVISIBLE (object);
143   
144   if (invisible->has_user_ref_count)
145     {
146       invisible->has_user_ref_count = FALSE;
147       g_object_unref (invisible);
148     }
149
150   GTK_OBJECT_CLASS (parent_class)->destroy (object);  
151 }
152
153 /**
154  * gtk_invisible_new_for_screen:
155  * @screen: a #GdkScreen which identifies on which
156  *          the new #GtkInvisible will be created.
157  *
158  * Creates a new #GtkInvisible object for a specified screen
159  *
160  * Return value: a newly created #GtkInvisible object
161  *
162  * Since: 2.2
163  **/
164 GtkWidget* 
165 gtk_invisible_new_for_screen (GdkScreen *screen)
166 {
167   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
168   
169   return g_object_new (GTK_TYPE_INVISIBLE, "screen", screen, NULL);
170 }
171
172 /**
173  * gtk_invisible_new:
174  * 
175  * Creates a new #GtkInvisible.
176  * 
177  * Return value: a new #GtkInvisible.
178  **/
179 GtkWidget*
180 gtk_invisible_new (void)
181 {
182   return g_object_new (GTK_TYPE_INVISIBLE, NULL);
183 }
184
185 /**
186  * gtk_invisible_set_screen:
187  * @invisible: a #GtkInvisible.
188  * @screen: a #GdkScreen.
189  *
190  * Sets the #GdkScreen where the #GtkInvisible object will be displayed.
191  *
192  * Since: 2.2
193  **/ 
194 void
195 gtk_invisible_set_screen (GtkInvisible *invisible,
196                           GdkScreen    *screen)
197 {
198   GtkWidget *widget;
199   GdkScreen *previous_screen;
200   gboolean was_realized;
201   
202   g_return_if_fail (GTK_IS_INVISIBLE (invisible));
203   g_return_if_fail (GDK_IS_SCREEN (screen));
204
205   if (screen == invisible->screen)
206     return;
207
208   widget = GTK_WIDGET (invisible);
209
210   previous_screen = invisible->screen;
211   was_realized = GTK_WIDGET_REALIZED (invisible);
212
213   if (was_realized)
214     gtk_widget_unrealize (widget);
215   
216   invisible->screen = screen;
217   if (screen != previous_screen)
218     _gtk_widget_propagate_screen_changed (widget, previous_screen);
219   g_object_notify (G_OBJECT (invisible), "screen");
220   
221   if (was_realized)
222     gtk_widget_realize (widget);
223 }
224
225 /**
226  * gtk_invisible_get_screen:
227  * @invisible: a #GtkInvisible.
228  *
229  * Returns the #GdkScreen object associated with @invisible
230  *
231  * Return value: the associated #GdkScreen.
232  *
233  * Since: 2.2
234  **/
235 GdkScreen *
236 gtk_invisible_get_screen (GtkInvisible *invisible)
237 {
238   g_return_val_if_fail (GTK_IS_INVISIBLE (invisible), NULL);
239   
240   return invisible->screen;
241 }
242
243 static void
244 gtk_invisible_realize (GtkWidget *widget)
245 {
246   GdkWindowAttr attributes;
247   gint attributes_mask;
248
249   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
250
251   attributes.x = -100;
252   attributes.y = -100;
253   attributes.width = 10;
254   attributes.height = 10;
255   attributes.window_type = GDK_WINDOW_TEMP;
256   attributes.wclass = GDK_INPUT_ONLY;
257   attributes.override_redirect = TRUE;
258   attributes.event_mask = gtk_widget_get_events (widget);
259
260   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
261
262   widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
263                                    &attributes, attributes_mask);
264                                               
265   gdk_window_set_user_data (widget->window, widget);
266   
267   widget->style = gtk_style_attach (widget->style, widget->window);
268 }
269
270 static void
271 gtk_invisible_style_set (GtkWidget *widget,
272                          GtkStyle  *previous_style)
273 {
274   /* Don't chain up to parent implementation */
275 }
276
277 static void
278 gtk_invisible_show (GtkWidget *widget)
279 {
280   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
281   gtk_widget_map (widget);
282 }
283
284 static void
285 gtk_invisible_size_allocate (GtkWidget     *widget,
286                             GtkAllocation *allocation)
287 {
288   widget->allocation = *allocation;
289
290
291
292 static void 
293 gtk_invisible_set_property  (GObject      *object,
294                              guint         prop_id,
295                              const GValue *value,
296                              GParamSpec   *pspec)
297 {
298   GtkInvisible *invisible = GTK_INVISIBLE (object);
299   
300   switch (prop_id)
301     {
302     case PROP_SCREEN:
303       gtk_invisible_set_screen (invisible, g_value_get_object (value));
304       break;
305     default:
306       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307       break;
308     }
309 }
310
311 static void 
312 gtk_invisible_get_property  (GObject      *object,
313                              guint         prop_id,
314                              GValue       *value,
315                              GParamSpec   *pspec)
316 {
317   GtkInvisible *invisible = GTK_INVISIBLE (object);
318
319   switch (prop_id)
320     {
321     case PROP_SCREEN:
322       g_value_set_object (value, invisible->screen);
323       break;
324     default:
325       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326       break;
327     }
328 }
329
330 /* We use a constructor here so that we can realize the invisible on
331  * the correct screen after the "screen" property has been set
332  */
333 static GObject*
334 gtk_invisible_constructor (GType                  type,
335                            guint                  n_construct_properties,
336                            GObjectConstructParam *construct_params)
337 {
338   GObject *object;
339
340   object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
341                                                            n_construct_properties,
342                                                            construct_params);
343
344   gtk_widget_realize (GTK_WIDGET (object));
345
346   return object;
347 }
348