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