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