]> Pileus Git - ~andy/gtk/blob - gtk/gtkinvisible.c
4e1418e0ac82953f7254c580220f03135dc871b6
[~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                                                         _("Screen"),
115                                                         _("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
151 /**
152  * gtk_invisible_new_for_screen:
153  * @screen: a #GdkScreen which identifies on which
154  *          the new #GtkInvisible will be created.
155  *
156  * Creates a new #GtkInvisible object for a specified screen
157  *
158  * Return value: a newly created #GtkInvisible object
159  **/
160 GtkWidget* 
161 gtk_invisible_new_for_screen (GdkScreen *screen)
162 {
163   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
164   
165   return g_object_new (GTK_TYPE_INVISIBLE, "screen", screen, NULL);
166 }
167
168 /**
169  * gtk_invisible_new:
170  * 
171  * Creates a new #GtkInvisible.
172  * 
173  * Return value: a new #GtkInvisible.
174  **/
175 GtkWidget*
176 gtk_invisible_new (void)
177 {
178   return g_object_new (GTK_TYPE_INVISIBLE, NULL);
179 }
180
181 /**
182  * gtk_invisible_set_screen:
183  * @invisible: a #GtkInvisible.
184  * @screen: a #GdkScreen.
185  *
186  * Sets the #GdkScreen where the #GtkInvisible object will be displayed.
187  **/ 
188 void
189 gtk_invisible_set_screen (GtkInvisible *invisible,
190                           GdkScreen    *screen)
191 {
192   GtkWidget *widget;
193   GdkScreen *previous_screen;
194   gboolean was_realized;
195   
196   g_return_if_fail (GTK_IS_INVISIBLE (invisible));
197   g_return_if_fail (GDK_IS_SCREEN (screen));
198
199   if (screen == invisible->screen)
200     return;
201
202   widget = GTK_WIDGET (invisible);
203
204   previous_screen = invisible->screen;
205   was_realized = GTK_WIDGET_REALIZED (invisible);
206
207   if (was_realized)
208     gtk_widget_unrealize (widget);
209   
210   invisible->screen = screen;
211   if (screen != previous_screen)
212     _gtk_widget_propagate_screen_changed (widget, previous_screen);
213   g_object_notify (G_OBJECT (invisible), "screen");
214   
215   if (was_realized)
216     gtk_widget_realize (widget);
217 }
218
219 /**
220  * gtk_invisible_get_screen:
221  * @invisible: a #GtkInvisible.
222  *
223  * Returns the #GdkScreen object associated with @invisible
224  *
225  * Return value : the associated #GdkScreen.
226  **/
227 GdkScreen *
228 gtk_invisible_get_screen (GtkInvisible *invisible)
229 {
230   g_return_val_if_fail (GTK_IS_INVISIBLE (invisible), NULL);
231   
232   return invisible->screen;
233 }
234
235 static void
236 gtk_invisible_realize (GtkWidget *widget)
237 {
238   GdkWindowAttr attributes;
239   gint attributes_mask;
240
241   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
242
243   attributes.x = -100;
244   attributes.y = -100;
245   attributes.width = 10;
246   attributes.height = 10;
247   attributes.window_type = GDK_WINDOW_TEMP;
248   attributes.wclass = GDK_INPUT_ONLY;
249   attributes.override_redirect = TRUE;
250   attributes.event_mask = gtk_widget_get_events (widget);
251
252   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
253
254   widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
255                                    &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
311   switch (prop_id)
312     {
313     case PROP_SCREEN:
314       g_value_set_object (value, invisible->screen);
315       break;
316     default:
317       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318       break;
319     }
320 }
321
322 /* We use a constructor here so that we can realize the invisible on
323  * the correct screen after the "screen" property has been set
324  */
325 static GObject*
326 gtk_invisible_constructor (GType                  type,
327                            guint                  n_construct_properties,
328                            GObjectConstructParam *construct_params)
329 {
330   GObject *object;
331
332   object = (* G_OBJECT_CLASS (parent_class)->constructor) (type,
333                                                            n_construct_properties,
334                                                            construct_params);
335
336   gtk_widget_realize (GTK_WIDGET (object));
337
338   return object;
339 }
340