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