gtkinvisible.c 8.44 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Owen Taylor's avatar
Owen Taylor committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Owen Taylor's avatar
Owen Taylor committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#include "config.h"
26
#include <gdk/gdk.h>
Owen Taylor's avatar
Owen Taylor committed
27
#include "gtkinvisible.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
28
#include "gtkwidgetprivate.h"
29
#include "gtkprivate.h"
30 31
#include "gtkintl.h"

32 33 34 35 36 37 38 39 40 41 42 43 44 45

/**
 * SECTION:gtkinvisible
 * @Short_description: A widget which is not displayed
 * @Title: GtkInvisible
 *
 * The #GtkInvisible widget is used internally in GTK+, and is probably not
 * very useful for application developers.
 *
 * It is used for reliable pointer grabs and selection handling in the code
 * for drag-and-drop.
 */


46
struct _GtkInvisiblePrivate
47 48 49 50 51
{
  GdkScreen    *screen;
  gboolean      has_user_ref_count;
};

52 53 54 55 56
enum {
  PROP_0,
  PROP_SCREEN,
  LAST_ARG
};
Owen Taylor's avatar
Owen Taylor committed
57

58
static void gtk_invisible_destroy       (GtkWidget         *widget);
59
static void gtk_invisible_realize       (GtkWidget         *widget);
60
static void gtk_invisible_style_updated (GtkWidget         *widget);
61 62 63
static void gtk_invisible_show          (GtkWidget         *widget);
static void gtk_invisible_size_allocate (GtkWidget         *widget,
					 GtkAllocation     *allocation);
64 65 66 67 68 69 70 71
static void gtk_invisible_set_property  (GObject           *object,
					 guint              prop_id,
					 const GValue      *value,
					 GParamSpec        *pspec);
static void gtk_invisible_get_property  (GObject           *object,
					 guint              prop_id,
					 GValue		   *value,
					 GParamSpec        *pspec);
72
static void gtk_invisible_constructed   (GObject           *object);
73

74
G_DEFINE_TYPE_WITH_PRIVATE (GtkInvisible, gtk_invisible, GTK_TYPE_WIDGET)
Owen Taylor's avatar
Owen Taylor committed
75 76 77 78

static void
gtk_invisible_class_init (GtkInvisibleClass *class)
{
79
  GObjectClass	 *gobject_class;
Owen Taylor's avatar
Owen Taylor committed
80 81 82
  GtkWidgetClass *widget_class;

  widget_class = (GtkWidgetClass*) class;
83 84
  gobject_class = (GObjectClass*) class;

Owen Taylor's avatar
Owen Taylor committed
85
  widget_class->realize = gtk_invisible_realize;
86
  widget_class->style_updated = gtk_invisible_style_updated;
Owen Taylor's avatar
Owen Taylor committed
87 88
  widget_class->show = gtk_invisible_show;
  widget_class->size_allocate = gtk_invisible_size_allocate;
89
  widget_class->destroy = gtk_invisible_destroy;
90

91 92
  gobject_class->set_property = gtk_invisible_set_property;
  gobject_class->get_property = gtk_invisible_get_property;
93
  gobject_class->constructed = gtk_invisible_constructed;
94 95 96 97

  g_object_class_install_property (gobject_class,
				   PROP_SCREEN,
				   g_param_spec_object ("screen",
98 99
 							P_("Screen"),
 							P_("The screen where this window will be displayed"),
100
							GDK_TYPE_SCREEN,
101
 							GTK_PARAM_READWRITE));
Owen Taylor's avatar
Owen Taylor committed
102 103 104 105 106
}

static void
gtk_invisible_init (GtkInvisible *invisible)
{
107
  GtkInvisiblePrivate *priv;
108

109
  invisible->priv = gtk_invisible_get_instance_private (invisible);
110 111
  priv = invisible->priv;

112
  gtk_widget_set_has_window (GTK_WIDGET (invisible), TRUE);
113
  _gtk_widget_set_is_toplevel (GTK_WIDGET (invisible), TRUE);
Owen Taylor's avatar
Owen Taylor committed
114

115
  g_object_ref_sink (invisible);
116

117 118
  priv->has_user_ref_count = TRUE;
  priv->screen = gdk_screen_get_default ();
119 120 121
}

static void
122
gtk_invisible_destroy (GtkWidget *widget)
123
{
124
  GtkInvisible *invisible = GTK_INVISIBLE (widget);
125
  GtkInvisiblePrivate *priv = invisible->priv;
126 127

  if (priv->has_user_ref_count)
128
    {
129
      priv->has_user_ref_count = FALSE;
Manish Singh's avatar
Manish Singh committed
130
      g_object_unref (invisible);
131
    }
132

133
  GTK_WIDGET_CLASS (gtk_invisible_parent_class)->destroy (widget);
Owen Taylor's avatar
Owen Taylor committed
134 135
}

136 137 138 139 140 141 142
/**
 * gtk_invisible_new_for_screen:
 * @screen: a #GdkScreen which identifies on which
 *	    the new #GtkInvisible will be created.
 *
 * Creates a new #GtkInvisible object for a specified screen
 *
143
 * Returns: a newly created #GtkInvisible object
144 145
 *
 * Since: 2.2
146 147 148 149 150 151 152 153 154 155 156 157 158 159
 **/
GtkWidget* 
gtk_invisible_new_for_screen (GdkScreen *screen)
{
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  
  return g_object_new (GTK_TYPE_INVISIBLE, "screen", screen, NULL);
}

/**
 * gtk_invisible_new:
 * 
 * Creates a new #GtkInvisible.
 * 
160
 * Returns: a new #GtkInvisible.
161
 **/
Owen Taylor's avatar
Owen Taylor committed
162 163 164
GtkWidget*
gtk_invisible_new (void)
{
165 166
  return g_object_new (GTK_TYPE_INVISIBLE, NULL);
}
167

168 169 170 171 172 173
/**
 * gtk_invisible_set_screen:
 * @invisible: a #GtkInvisible.
 * @screen: a #GdkScreen.
 *
 * Sets the #GdkScreen where the #GtkInvisible object will be displayed.
174 175
 *
 * Since: 2.2
176 177 178 179 180
 **/ 
void
gtk_invisible_set_screen (GtkInvisible *invisible,
			  GdkScreen    *screen)
{
181
  GtkInvisiblePrivate *priv;
182 183
  GtkWidget *widget;
  GdkScreen *previous_screen;
184
  gboolean was_realized;
185

186 187 188
  g_return_if_fail (GTK_IS_INVISIBLE (invisible));
  g_return_if_fail (GDK_IS_SCREEN (screen));

189 190 191
  priv = invisible->priv;

  if (screen == priv->screen)
192 193
    return;

194 195
  widget = GTK_WIDGET (invisible);

196
  previous_screen = priv->screen;
197
  was_realized = gtk_widget_get_realized (widget);
198 199

  if (was_realized)
200
    gtk_widget_unrealize (widget);
201 202

  priv->screen = screen;
203 204
  if (screen != previous_screen)
    _gtk_widget_propagate_screen_changed (widget, previous_screen);
205 206
  g_object_notify (G_OBJECT (invisible), "screen");
  
207
  if (was_realized)
208
    gtk_widget_realize (widget);
209 210 211 212 213 214 215 216
}

/**
 * gtk_invisible_get_screen:
 * @invisible: a #GtkInvisible.
 *
 * Returns the #GdkScreen object associated with @invisible
 *
217
 * Returns: (transfer none): the associated #GdkScreen.
218 219
 *
 * Since: 2.2
220 221 222 223 224
 **/
GdkScreen *
gtk_invisible_get_screen (GtkInvisible *invisible)
{
  g_return_val_if_fail (GTK_IS_INVISIBLE (invisible), NULL);
225 226

  return invisible->priv->screen;
Owen Taylor's avatar
Owen Taylor committed
227 228 229 230 231
}

static void
gtk_invisible_realize (GtkWidget *widget)
{
232
  GdkWindow *parent;
233
  GdkWindow *window;
Owen Taylor's avatar
Owen Taylor committed
234 235 236
  GdkWindowAttr attributes;
  gint attributes_mask;

237
  gtk_widget_set_realized (widget, TRUE);
Owen Taylor's avatar
Owen Taylor committed
238

239 240
  parent = gtk_widget_get_parent_window (widget);
  if (parent == NULL)
241
    parent = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
242

Owen Taylor's avatar
Owen Taylor committed
243 244 245 246 247 248 249 250 251 252 253
  attributes.x = -100;
  attributes.y = -100;
  attributes.width = 10;
  attributes.height = 10;
  attributes.window_type = GDK_WINDOW_TEMP;
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.override_redirect = TRUE;
  attributes.event_mask = gtk_widget_get_events (widget);

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;

254 255
  window = gdk_window_new (parent, &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
256
  gtk_widget_register_window (widget, window);
Owen Taylor's avatar
Owen Taylor committed
257 258
}

259
static void
260
gtk_invisible_style_updated (GtkWidget *widget)
261 262 263 264
{
  /* Don't chain up to parent implementation */
}

Owen Taylor's avatar
Owen Taylor committed
265 266 267
static void
gtk_invisible_show (GtkWidget *widget)
{
268
  _gtk_widget_set_visible_flag (widget, TRUE);
Owen Taylor's avatar
Owen Taylor committed
269 270 271 272 273
  gtk_widget_map (widget);
}

static void
gtk_invisible_size_allocate (GtkWidget     *widget,
274
                             GtkAllocation *allocation)
Owen Taylor's avatar
Owen Taylor committed
275
{
276 277
  gtk_widget_set_allocation (widget, allocation);
}
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296


static void 
gtk_invisible_set_property  (GObject      *object,
			     guint         prop_id,
			     const GValue *value,
			     GParamSpec   *pspec)
{
  GtkInvisible *invisible = GTK_INVISIBLE (object);
  
  switch (prop_id)
    {
    case PROP_SCREEN:
      gtk_invisible_set_screen (invisible, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
Owen Taylor's avatar
Owen Taylor committed
297
}
298 299 300 301 302 303 304 305

static void 
gtk_invisible_get_property  (GObject      *object,
			     guint         prop_id,
			     GValue	  *value,
			     GParamSpec   *pspec)
{
  GtkInvisible *invisible = GTK_INVISIBLE (object);
306
  GtkInvisiblePrivate *priv = invisible->priv;
307 308 309 310

  switch (prop_id)
    {
    case PROP_SCREEN:
311
      g_value_set_object (value, priv->screen);
312 313 314 315 316 317 318 319
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/* 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 323
static void
gtk_invisible_constructed (GObject *object)
324
{
325
  G_OBJECT_CLASS (gtk_invisible_parent_class)->constructed (object);
326 327 328

  gtk_widget_realize (GTK_WIDGET (object));
}