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