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