gtkplug.c 27.1 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
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
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.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17 18 19 20
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* By Owen Taylor <otaylor@gtk.org>              98/4/4 */

21
/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27
 * 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/. 
 */

28
#include "config.h"
29

30
#include "gtkmain.h"
31
#include "gtkmarshalers.h"
32
#include "gtkplug.h"
33
#include "gtkintl.h"
34
#include "gtkprivate.h"
35
#include "gtkplugprivate.h"
36

37 38
#include "gtkalias.h"

39 40 41 42
static void            gtk_plug_get_property          (GObject     *object,
						       guint        prop_id,
						       GValue      *value,
						       GParamSpec  *pspec);
43
static void            gtk_plug_finalize              (GObject          *object);
44 45
static void            gtk_plug_realize               (GtkWidget        *widget);
static void            gtk_plug_unrealize             (GtkWidget        *widget);
46 47 48 49 50 51
static void            gtk_plug_show                  (GtkWidget        *widget);
static void            gtk_plug_hide                  (GtkWidget        *widget);
static void            gtk_plug_map                   (GtkWidget        *widget);
static void            gtk_plug_unmap                 (GtkWidget        *widget);
static void            gtk_plug_size_allocate         (GtkWidget        *widget,
						       GtkAllocation    *allocation);
52 53
static gboolean        gtk_plug_key_press_event       (GtkWidget        *widget,
						       GdkEventKey      *event);
54 55
static gboolean        gtk_plug_focus_event           (GtkWidget        *widget,
						       GdkEventFocus    *event);
56 57
static void            gtk_plug_set_focus             (GtkWindow        *window,
						       GtkWidget        *focus);
58
static gboolean        gtk_plug_focus                 (GtkWidget        *widget,
59
						       GtkDirectionType  direction);
60
static void            gtk_plug_check_resize          (GtkContainer     *container);
61
static void            gtk_plug_keys_changed          (GtkWindow        *window);
62

63
static GtkBinClass *bin_class = NULL;
64

65 66 67 68 69 70
typedef struct
{
  guint			 accelerator_key;
  GdkModifierType	 accelerator_mods;
} GrabbedKey;

71 72 73
enum {
  PROP_0,
  PROP_EMBEDDED,
74
  PROP_SOCKET_WINDOW
75 76
};

77 78 79 80 81 82 83
enum {
  EMBEDDED,
  LAST_SIGNAL
}; 

static guint plug_signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
84
G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
85

86 87 88 89 90 91 92 93 94 95 96 97 98
static void
gtk_plug_get_property (GObject    *object,
		       guint       prop_id,
		       GValue     *value,
		       GParamSpec *pspec)
{
  GtkPlug *plug = GTK_PLUG (object);

  switch (prop_id)
    {
    case PROP_EMBEDDED:
      g_value_set_boolean (value, plug->socket_window != NULL);
      break;
99 100 101
    case PROP_SOCKET_WINDOW:
      g_value_set_object (value, plug->socket_window);
      break;
102 103 104 105 106 107
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

108 109 110
static void
gtk_plug_class_init (GtkPlugClass *class)
{
111
  GObjectClass *gobject_class = (GObjectClass *)class;
112 113
  GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
  GtkWindowClass *window_class = (GtkWindowClass *)class;
114
  GtkContainerClass *container_class = (GtkContainerClass *)class;
115

Manish Singh's avatar
Manish Singh committed
116
  bin_class = g_type_class_peek (GTK_TYPE_BIN);
117

118
  gobject_class->get_property = gtk_plug_get_property;
119 120
  gobject_class->finalize = gtk_plug_finalize;
  
121
  widget_class->realize = gtk_plug_realize;
122
  widget_class->unrealize = gtk_plug_unrealize;
123
  widget_class->key_press_event = gtk_plug_key_press_event;
124 125
  widget_class->focus_in_event = gtk_plug_focus_event;
  widget_class->focus_out_event = gtk_plug_focus_event;
126

127 128 129 130 131 132 133 134 135
  widget_class->show = gtk_plug_show;
  widget_class->hide = gtk_plug_hide;
  widget_class->map = gtk_plug_map;
  widget_class->unmap = gtk_plug_unmap;
  widget_class->size_allocate = gtk_plug_size_allocate;

  widget_class->focus = gtk_plug_focus;

  container_class->check_resize = gtk_plug_check_resize;
136 137

  window_class->set_focus = gtk_plug_set_focus;
138
  window_class->keys_changed = gtk_plug_keys_changed;
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  /**
   * GtkPlug:embedded:
   *
   * %TRUE if the plug is embedded in a socket.
   *
   * Since: 2.12
   */
  g_object_class_install_property (gobject_class,
				   PROP_EMBEDDED,
				   g_param_spec_boolean ("embedded",
							 P_("Embedded"),
							 P_("Whether or not the plug is embedded"),
							 FALSE,
							 GTK_PARAM_READABLE));

155 156 157 158 159
  /**
   * GtkPlug:socket-window:
   *
   * The window of the socket the plug is embedded in.
   *
160
   * Since: 2.14
161 162 163
   */
  g_object_class_install_property (gobject_class,
				   PROP_SOCKET_WINDOW,
164
				   g_param_spec_object ("socket-window",
165 166 167 168 169
							P_("Socket Window"),
							P_("The window of the socket the plug is embedded in"),
							GDK_TYPE_WINDOW,
							GTK_PARAM_READABLE));

Matthias Clasen's avatar
Matthias Clasen committed
170 171 172 173 174 175 176
  /**
   * GtkPlug::embedded:
   * @plug: the object on which the signal was emitted
   *
   * Gets emitted when the plug becomes embedded in a socket
   * and when the embedding ends.
   */ 
177
  plug_signals[EMBEDDED] =
178
    g_signal_new (I_("embedded"),
179 180 181 182
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPlugClass, embedded),
		  NULL, NULL,
183
		  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
184
		  G_TYPE_NONE, 0);
185 186 187 188 189 190 191 192 193 194 195
}

static void
gtk_plug_init (GtkPlug *plug)
{
  GtkWindow *window;

  window = GTK_WINDOW (plug);

  window->type = GTK_WINDOW_TOPLEVEL;
}
196

197 198 199 200
static void
gtk_plug_set_is_child (GtkPlug  *plug,
		       gboolean  is_child)
{
201 202
  g_assert (!GTK_WIDGET (plug)->parent);
      
203 204
  if (is_child)
    {
205
      if (plug->modality_window)
206
	_gtk_plug_handle_modality_off (plug);
207 208 209 210 211 212 213 214

      if (plug->modality_group)
	{
	  gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
	  g_object_unref (plug->modality_group);
	  plug->modality_group = NULL;
	}
      
215 216 217 218 219 220 221 222
      /* As a toplevel, the MAPPED flag doesn't correspond
       * to whether the widget->window is mapped; we unmap
       * here, but don't bother remapping -- we will get mapped
       * by gtk_widget_set_parent ().
       */
      if (GTK_WIDGET_MAPPED (plug))
	gtk_widget_unmap (GTK_WIDGET (plug));
      
223
      _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
224
      gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
225 226 227 228 229

      _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), GTK_WIDGET (plug));
    }
  else
    {
230 231 232 233 234
      if (GTK_WINDOW (plug)->focus_widget)
	gtk_window_set_focus (GTK_WINDOW (plug), NULL);
      if (GTK_WINDOW (plug)->default_widget)
	gtk_window_set_default (GTK_WINDOW (plug), NULL);
	  
235 236 237
      plug->modality_group = gtk_window_group_new ();
      gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug));
      
238
      _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
239 240 241
      gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);

      _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
242 243 244
    }
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/**
 * gtk_plug_get_id:
 * @plug: a #GtkPlug.
 * 
 * Gets the window ID of a #GtkPlug widget, which can then
 * be used to embed this window inside another window, for
 * instance with gtk_socket_add_id().
 * 
 * Return value: the window ID for the plug
 **/
GdkNativeWindow
gtk_plug_get_id (GtkPlug *plug)
{
  g_return_val_if_fail (GTK_IS_PLUG (plug), 0);

  if (!GTK_WIDGET_REALIZED (plug))
    gtk_widget_realize (GTK_WIDGET (plug));

  return _gtk_plug_windowing_get_id (plug);
}

Tim Janik's avatar
Tim Janik committed
266 267 268 269 270
/**
 * gtk_plug_get_embedded:
 * @plug: a #GtkPlug
 *
 * Determines whether the plug is embedded in a socket.
271
 *
Tim Janik's avatar
Tim Janik committed
272
 * Return value: %TRUE if the plug is embedded in a socket
273 274
 *
 * Since: 2.14
Tim Janik's avatar
Tim Janik committed
275 276 277 278
 **/
gboolean
gtk_plug_get_embedded (GtkPlug *plug)
{
279
  g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
Tim Janik's avatar
Tim Janik committed
280 281 282 283

  return plug->socket_window != NULL;
}

284 285 286 287 288 289 290
/**
 * gtk_plug_get_socket_window:
 * @plug: a #GtkPlug
 *
 * Retrieves the socket the plug is embedded in.
 *
 * Return value: the window of the socket, or %NULL
291 292
 *
 * Since: 2.14
293 294 295 296 297 298 299 300 301
 **/
GdkWindow *
gtk_plug_get_socket_window (GtkPlug *plug)
{
  g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);

  return plug->socket_window;
}

302 303 304
/**
 * _gtk_plug_add_to_socket:
 * @plug: a #GtkPlug
305
 * @socket_: a #GtkSocket
306
 * 
307
 * Adds a plug to a socket within the same application.
308
 **/
309
void
310
_gtk_plug_add_to_socket (GtkPlug   *plug,
Matthias Clasen's avatar
Matthias Clasen committed
311
			 GtkSocket *socket_)
312
{
313
  GtkWidget *widget;
314
  gint w, h;
315 316
  
  g_return_if_fail (GTK_IS_PLUG (plug));
Matthias Clasen's avatar
Matthias Clasen committed
317 318
  g_return_if_fail (GTK_IS_SOCKET (socket_));
  g_return_if_fail (GTK_WIDGET_REALIZED (socket_));
319

320 321
  widget = GTK_WIDGET (plug);

322 323
  gtk_plug_set_is_child (plug, TRUE);
  plug->same_app = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
324 325
  socket_->same_app = TRUE;
  socket_->plug_widget = widget;
326

Matthias Clasen's avatar
Matthias Clasen committed
327
  plug->socket_window = GTK_WIDGET (socket_)->window;
328

329
  if (GTK_WIDGET_REALIZED (widget))
330 331 332 333
    {
      gdk_drawable_get_size (GDK_DRAWABLE (widget->window), &w, &h);
      gdk_window_reparent (widget->window, plug->socket_window, -w, -h);
    }
334

Matthias Clasen's avatar
Matthias Clasen committed
335
  gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
336

337
  g_signal_emit_by_name (socket_, "plug-added");
338 339
}

340 341
/**
 * _gtk_plug_send_delete_event:
Matthias Clasen's avatar
Matthias Clasen committed
342
 * @widget: a #GtkWidget
343 344 345 346 347 348 349
 *
 * Send a GDK_DELETE event to the @widget and destroy it if
 * necessary. Internal GTK function, called from this file or the
 * backend-specific GtkPlug implementation.
 */
void
_gtk_plug_send_delete_event (GtkWidget *widget)
350 351
{
  GdkEvent *event = gdk_event_new (GDK_DELETE);
352

353 354 355
  event->any.window = g_object_ref (widget->window);
  event->any.send_event = FALSE;

356 357
  g_object_ref (widget);

358 359
  if (!gtk_widget_event (widget, event))
    gtk_widget_destroy (widget);
360 361

  g_object_unref (widget);
362 363 364 365

  gdk_event_free (event);
}

366
/**
367
 * _gtk_plug_remove_from_socket:
368
 * @plug: a #GtkPlug
Matthias Clasen's avatar
Matthias Clasen committed
369
 * @socket_: a #GtkSocket
370
 * 
371
 * Removes a plug from a socket within the same application.
372 373 374
 **/
void
_gtk_plug_remove_from_socket (GtkPlug   *plug,
Matthias Clasen's avatar
Matthias Clasen committed
375
			      GtkSocket *socket_)
376 377 378 379 380 381
{
  GtkWidget *widget;
  gboolean result;
  gboolean widget_was_visible;

  g_return_if_fail (GTK_IS_PLUG (plug));
Matthias Clasen's avatar
Matthias Clasen committed
382
  g_return_if_fail (GTK_IS_SOCKET (socket_));
383 384 385 386
  g_return_if_fail (GTK_WIDGET_REALIZED (plug));

  widget = GTK_WIDGET (plug);

387 388 389
  if (GTK_WIDGET_IN_REPARENT (widget))
    return;

390
  g_object_ref (plug);
Matthias Clasen's avatar
Matthias Clasen committed
391
  g_object_ref (socket_);
392 393 394 395

  widget_was_visible = GTK_WIDGET_VISIBLE (plug);
  
  gdk_window_hide (widget->window);
396
  GTK_PRIVATE_SET_FLAG (plug, GTK_IN_REPARENT);
397 398 399
  gdk_window_reparent (widget->window,
		       gtk_widget_get_root_window (widget),
		       0, 0);
400 401 402
  gtk_widget_unparent (GTK_WIDGET (plug));
  GTK_PRIVATE_UNSET_FLAG (plug, GTK_IN_REPARENT);
  
Matthias Clasen's avatar
Matthias Clasen committed
403 404
  socket_->plug_widget = NULL;
  if (socket_->plug_window != NULL)
405
    {
Matthias Clasen's avatar
Matthias Clasen committed
406 407
      g_object_unref (socket_->plug_window);
      socket_->plug_window = NULL;
408
    }
409
  
Matthias Clasen's avatar
Matthias Clasen committed
410
  socket_->same_app = FALSE;
411 412

  plug->same_app = FALSE;
413
  plug->socket_window = NULL;
414 415 416

  gtk_plug_set_is_child (plug, FALSE);
		    
417
  g_signal_emit_by_name (socket_, "plug-removed", &result);
418
  if (!result)
Matthias Clasen's avatar
Matthias Clasen committed
419
    gtk_widget_destroy (GTK_WIDGET (socket_));
420

421 422
  if (widget->window)
    _gtk_plug_send_delete_event (widget);
423

424 425
  g_object_unref (plug);

Matthias Clasen's avatar
Matthias Clasen committed
426 427
  if (widget_was_visible && GTK_WIDGET_VISIBLE (socket_))
    gtk_widget_queue_resize (GTK_WIDGET (socket_));
428

Matthias Clasen's avatar
Matthias Clasen committed
429
  g_object_unref (socket_);
430 431
}

432 433 434 435 436
/**
 * gtk_plug_construct:
 * @plug: a #GtkPlug.
 * @socket_id: the XID of the socket's window.
 *
437
 * Finish the initialization of @plug for a given #GtkSocket identified by
438 439
 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
 **/
440 441 442
void
gtk_plug_construct (GtkPlug         *plug,
		    GdkNativeWindow  socket_id)
443
{
Owen Taylor's avatar
Owen Taylor committed
444
  gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
445 446 447 448
}

/**
 * gtk_plug_construct_for_display:
449
 * @plug: a #GtkPlug.
450 451 452 453
 * @display: the #GdkDisplay associated with @socket_id's 
 *	     #GtkSocket.
 * @socket_id: the XID of the socket's window.
 *
454
 * Finish the initialization of @plug for a given #GtkSocket identified by
455 456
 * @socket_id which is currently displayed on @display.
 * This function will generally only be used by classes deriving from #GtkPlug.
457 458
 *
 * Since: 2.2
459 460
 **/
void
461 462
gtk_plug_construct_for_display (GtkPlug         *plug,
				GdkDisplay	*display,
463
				GdkNativeWindow  socket_id)
464
{
465
  if (socket_id)
466
    {
467 468
      gpointer user_data = NULL;

469 470
      plug->socket_window = gdk_window_lookup_for_display (display, socket_id);
      
471 472 473
      if (plug->socket_window)
	gdk_window_get_user_data (plug->socket_window, &user_data);
      else
474 475
	plug->socket_window = gdk_window_foreign_new_for_display (display, socket_id);
	  
476
      if (user_data)
477
	{
478 479 480 481 482 483 484
	  if (GTK_IS_SOCKET (user_data))
	    _gtk_plug_add_to_socket (plug, user_data);
	  else
	    {
	      g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
	      plug->socket_window = NULL;
	    }
485
	}
486

487
      if (plug->socket_window) {
Manish Singh's avatar
Manish Singh committed
488
	g_signal_emit (plug, plug_signals[EMBEDDED], 0);
489 490 491

        g_object_notify (G_OBJECT (plug), "embedded");
      }
492 493
    }
}
494

495 496 497 498 499 500 501 502 503 504
/**
 * gtk_plug_new:
 * @socket_id:  the window ID of the socket, or 0.
 * 
 * Creates a new plug widget inside the #GtkSocket identified
 * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
 * can later be plugged into a #GtkSocket by  gtk_socket_add_id().
 * 
 * Return value: the new #GtkPlug widget.
 **/
505
GtkWidget*
506
gtk_plug_new (GdkNativeWindow socket_id)
507
{
Owen Taylor's avatar
Owen Taylor committed
508
  return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
509 510 511 512 513 514 515
}

/**
 * gtk_plug_new_for_display:
 * @display : the #GdkDisplay on which @socket_id is displayed
 * @socket_id: the XID of the socket's window.
 * 
516
 * Create a new plug widget inside the #GtkSocket identified by socket_id.
517 518
 *
 * Return value: the new #GtkPlug widget.
519 520
 *
 * Since: 2.2
521 522 523 524
 */
GtkWidget*
gtk_plug_new_for_display (GdkDisplay	  *display,
			  GdkNativeWindow  socket_id)
525 526 527
{
  GtkPlug *plug;

Manish Singh's avatar
Manish Singh committed
528
  plug = g_object_new (GTK_TYPE_PLUG, NULL);
529
  gtk_plug_construct_for_display (plug, display, socket_id);
530 531 532
  return GTK_WIDGET (plug);
}

533 534 535 536 537 538 539 540 541 542 543
static void
gtk_plug_finalize (GObject *object)
{
  GtkPlug *plug = GTK_PLUG (object);

  if (plug->grabbed_keys)
    {
      g_hash_table_destroy (plug->grabbed_keys);
      plug->grabbed_keys = NULL;
    }
  
Matthias Clasen's avatar
Matthias Clasen committed
544
  G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
545 546
}

547 548 549
static void
gtk_plug_unrealize (GtkWidget *widget)
{
550
  GtkPlug *plug = GTK_PLUG (widget);
551 552 553 554

  if (plug->socket_window != NULL)
    {
      gdk_window_set_user_data (plug->socket_window, NULL);
Manish Singh's avatar
Manish Singh committed
555
      g_object_unref (plug->socket_window);
556
      plug->socket_window = NULL;
557 558

      g_object_notify (G_OBJECT (widget), "embedded");
559 560
    }

561 562 563
  if (!plug->same_app)
    {
      if (plug->modality_window)
564
	_gtk_plug_handle_modality_off (plug);
565

566 567 568
      gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
      g_object_unref (plug->modality_group);
    }
569

570
  GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
571 572
}

573 574 575
static void
gtk_plug_realize (GtkWidget *widget)
{
576 577
  GtkWindow *window = GTK_WINDOW (widget);
  GtkPlug *plug = GTK_PLUG (widget);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  GdkWindowAttr attributes;
  gint attributes_mask;

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  attributes.window_type = GDK_WINDOW_CHILD;	/* XXX GDK_WINDOW_PLUG ? */
  attributes.title = window->title;
  attributes.wmclass_name = window->wmclass_name;
  attributes.wmclass_class = window->wmclass_class;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;

  /* this isn't right - we should match our parent's visual/colormap.
   * though that will require handling "foreign" colormaps */
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK |
			    GDK_KEY_PRESS_MASK |
598
			    GDK_KEY_RELEASE_MASK |
599 600 601 602 603 604 605 606
			    GDK_ENTER_NOTIFY_MASK |
			    GDK_LEAVE_NOTIFY_MASK |
			    GDK_STRUCTURE_MASK);

  attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
  attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
  attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);

607
  if (GTK_WIDGET_TOPLEVEL (widget))
608
    {
609 610
      attributes.window_type = GDK_WINDOW_TOPLEVEL;

611
      gdk_error_trap_push ();
612 613 614 615 616 617 618 619
      if (plug->socket_window)
	widget->window = gdk_window_new (plug->socket_window, 
					 &attributes, attributes_mask);
      else /* If it's a passive plug, we use the root window */
	widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
					 &attributes, attributes_mask);

      gdk_display_sync (gtk_widget_get_display (widget));
620 621 622 623 624 625
      if (gdk_error_trap_pop ()) /* Uh-oh */
	{
	  gdk_error_trap_push ();
	  gdk_window_destroy (widget->window);
	  gdk_flush ();
	  gdk_error_trap_pop ();
626 627
	  widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
					   &attributes, attributes_mask);
628
	}
629
      
630 631 632
      gdk_window_add_filter (widget->window,
			     _gtk_plug_windowing_filter_func,
			     widget);
633 634 635 636

      plug->modality_group = gtk_window_group_new ();
      gtk_window_group_add_window (plug->modality_group, window);
      
637
      _gtk_plug_windowing_realize_toplevel (plug);
638
    }
639
  else
640 641
    widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
				     &attributes, attributes_mask);      
642
  
643 644 645 646
  gdk_window_set_user_data (widget->window, window);

  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
647 648

  gdk_window_enable_synchronized_configure (widget->window);
649 650 651 652 653 654
}

static void
gtk_plug_show (GtkWidget *widget)
{
  if (GTK_WIDGET_TOPLEVEL (widget))
Matthias Clasen's avatar
Matthias Clasen committed
655
    GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
656 657 658 659 660 661 662 663
  else
    GTK_WIDGET_CLASS (bin_class)->show (widget);
}

static void
gtk_plug_hide (GtkWidget *widget)
{
  if (GTK_WIDGET_TOPLEVEL (widget))
Matthias Clasen's avatar
Matthias Clasen committed
664
    GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
  else
    GTK_WIDGET_CLASS (bin_class)->hide (widget);
}

/* From gdkinternals.h */
void gdk_synthesize_window_state (GdkWindow     *window,
                                  GdkWindowState unset_flags,
                                  GdkWindowState set_flags);

static void
gtk_plug_map (GtkWidget *widget)
{
  if (GTK_WIDGET_TOPLEVEL (widget))
    {
      GtkBin *bin = GTK_BIN (widget);
680
      GtkPlug *plug = GTK_PLUG (widget);
681 682 683 684 685 686 687 688
      
      GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

      if (bin->child &&
	  GTK_WIDGET_VISIBLE (bin->child) &&
	  !GTK_WIDGET_MAPPED (bin->child))
	gtk_widget_map (bin->child);

689
      _gtk_plug_windowing_map_toplevel (plug);
690 691 692 693 694 695 696 697 698 699 700 701 702 703
      
      gdk_synthesize_window_state (widget->window,
				   GDK_WINDOW_STATE_WITHDRAWN,
				   0);
    }
  else
    GTK_WIDGET_CLASS (bin_class)->map (widget);
}

static void
gtk_plug_unmap (GtkWidget *widget)
{
  if (GTK_WIDGET_TOPLEVEL (widget))
    {
704 705
      GtkPlug *plug = GTK_PLUG (widget);

706 707 708
      GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);

      gdk_window_hide (widget->window);
709 710

      _gtk_plug_windowing_unmap_toplevel (plug);
711 712 713 714 715 716 717 718
      
      gdk_synthesize_window_state (widget->window,
				   0,
				   GDK_WINDOW_STATE_WITHDRAWN);
    }
  else
    GTK_WIDGET_CLASS (bin_class)->unmap (widget);
}
719

720 721 722 723 724
static void
gtk_plug_size_allocate (GtkWidget     *widget,
			GtkAllocation *allocation)
{
  if (GTK_WIDGET_TOPLEVEL (widget))
Matthias Clasen's avatar
Matthias Clasen committed
725
    GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
726 727 728 729
  else
    {
      GtkBin *bin = GTK_BIN (widget);

730 731
      widget->allocation = *allocation;

732 733 734 735
      if (GTK_WIDGET_REALIZED (widget))
	gdk_window_move_resize (widget->window,
				allocation->x, allocation->y,
				allocation->width, allocation->height);
736

737 738 739 740 741 742 743 744 745 746 747 748 749 750
      if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
	{
	  GtkAllocation child_allocation;
	  
	  child_allocation.x = child_allocation.y = GTK_CONTAINER (widget)->border_width;
	  child_allocation.width =
	    MAX (1, (gint)allocation->width - child_allocation.x * 2);
	  child_allocation.height =
	    MAX (1, (gint)allocation->height - child_allocation.y * 2);
	  
	  gtk_widget_size_allocate (bin->child, &child_allocation);
	}
      
    }
751 752
}

753
static gboolean
754 755 756
gtk_plug_key_press_event (GtkWidget   *widget,
			  GdkEventKey *event)
{
757
  if (GTK_WIDGET_TOPLEVEL (widget))
Matthias Clasen's avatar
Matthias Clasen committed
758
    return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
759
  else
760
    return FALSE;
761 762
}

763 764 765 766 767 768 769 770 771 772 773
static gboolean
gtk_plug_focus_event (GtkWidget      *widget,
		      GdkEventFocus  *event)
{
  /* We eat focus-in events and focus-out events, since they
   * can be generated by something like a keyboard grab on
   * a child of the plug.
   */
  return FALSE;
}

774 775 776
static void
gtk_plug_set_focus (GtkWindow *window,
		    GtkWidget *focus)
777
{
778
  GtkPlug *plug = GTK_PLUG (window);
779

Matthias Clasen's avatar
Matthias Clasen committed
780
  GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
781 782
  
  /* Ask for focus from embedder
783
   */
784

785
  if (focus && !window->has_toplevel_focus)
786
    _gtk_plug_windowing_set_focus (plug);
787 788
}

789 790 791 792 793 794 795 796 797
static guint
grabbed_key_hash (gconstpointer a)
{
  const GrabbedKey *key = a;
  guint h;
  
  h = key->accelerator_key << 16;
  h ^= key->accelerator_key >> 16;
  h ^= key->accelerator_mods;
798

799 800
  return h;
}
801

802 803 804 805 806
static gboolean
grabbed_key_equal (gconstpointer a, gconstpointer b)
{
  const GrabbedKey *keya = a;
  const GrabbedKey *keyb = b;
807

808 809 810
  return (keya->accelerator_key == keyb->accelerator_key &&
	  keya->accelerator_mods == keyb->accelerator_mods);
}
811

812
static void
813
add_grabbed_key (gpointer key, gpointer val, gpointer data)
814 815 816 817 818 819 820
{
  GrabbedKey *grabbed_key = key;
  GtkPlug *plug = data;

  if (!plug->grabbed_keys ||
      !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
    {
821 822 823
      _gtk_plug_windowing_add_grabbed_key (plug,
					   grabbed_key->accelerator_key,
					   grabbed_key->accelerator_mods);
824
    }
825
}
826

827
static void
828 829 830
add_grabbed_key_always (gpointer key,
			gpointer val,
			gpointer data)
831 832 833 834
{
  GrabbedKey *grabbed_key = key;
  GtkPlug *plug = data;

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  _gtk_plug_windowing_add_grabbed_key (plug,
				       grabbed_key->accelerator_key,
				       grabbed_key->accelerator_mods);
}

/**
 * _gtk_plug_add_all_grabbed_keys:
 *
 * @plug: a #GtkPlug
 *
 * Calls _gtk_plug_windowing_add_grabbed_key() on all the grabbed keys
 * in the @plug.
 */
void
_gtk_plug_add_all_grabbed_keys (GtkPlug *plug)
{
  if (plug->grabbed_keys)
    g_hash_table_foreach (plug->grabbed_keys, add_grabbed_key_always, plug);
853 854 855 856
}

static void
remove_grabbed_key (gpointer key, gpointer val, gpointer data)
857 858 859 860 861 862 863
{
  GrabbedKey *grabbed_key = key;
  GtkPlug *plug = data;

  if (!plug->grabbed_keys ||
      !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
    {
864 865 866
      _gtk_plug_windowing_remove_grabbed_key (plug, 
					      grabbed_key->accelerator_key,
					      grabbed_key->accelerator_mods);
867
    }
868 869 870
}

static void
871 872 873 874 875
keys_foreach (GtkWindow      *window,
	      guint           keyval,
	      GdkModifierType modifiers,
	      gboolean        is_mnemonic,
	      gpointer        data)
876
{
877
  GHashTable *new_grabbed_keys = data;
878
  GrabbedKey *key = g_slice_new (GrabbedKey);
879 880 881 882 883

  key->accelerator_key = keyval;
  key->accelerator_mods = modifiers;
  
  g_hash_table_replace (new_grabbed_keys, key, key);
884
}
885

886 887 888 889 890 891
static void
grabbed_key_free (gpointer data)
{
  g_slice_free (GrabbedKey, data);
}

892
static void
893
gtk_plug_keys_changed (GtkWindow *window)
894 895 896
{
  GHashTable *new_grabbed_keys, *old_grabbed_keys;
  GtkPlug *plug = GTK_PLUG (window);
897

898
  new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
899
  _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
900

901 902
  if (plug->socket_window)
    g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
903

904 905 906 907 908
  old_grabbed_keys = plug->grabbed_keys;
  plug->grabbed_keys = new_grabbed_keys;

  if (old_grabbed_keys)
    {
909 910 911
      if (plug->socket_window)
	g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
      g_hash_table_destroy (old_grabbed_keys);
912 913 914 915
    }
}

static gboolean
916
gtk_plug_focus (GtkWidget        *widget,
917 918
		GtkDirectionType  direction)
{
919 920 921 922
  GtkBin *bin = GTK_BIN (widget);
  GtkPlug *plug = GTK_PLUG (widget);
  GtkWindow *window = GTK_WINDOW (widget);
  GtkContainer *container = GTK_CONTAINER (widget);
923 924 925 926 927 928 929
  GtkWidget *old_focus_child = container->focus_child;
  GtkWidget *parent;
  
  /* We override GtkWindow's behavior, since we don't want wrapping here.
   */
  if (old_focus_child)
    {
930
      if (gtk_widget_child_focus (old_focus_child, direction))
931
	return TRUE;
932 933 934

      if (window->focus_widget)
	{
935 936 937 938 939 940 941 942 943 944 945 946 947 948
	  /* Wrapped off the end, clear the focus setting for the toplevel */
	  parent = window->focus_widget->parent;
	  while (parent)
	    {
	      gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
	      parent = GTK_WIDGET (parent)->parent;
	    }
	  
	  gtk_window_set_focus (GTK_WINDOW (container), NULL);
	}
    }
  else
    {
      /* Try to focus the first widget in the window */
949
      if (bin->child && gtk_widget_child_focus (bin->child, direction))
950
        return TRUE;
951 952
    }

953
  if (!GTK_CONTAINER (window)->focus_child)
954
    _gtk_plug_windowing_focus_to_parent (plug, direction);
955

956 957
  return FALSE;
}
958

959 960 961 962
static void
gtk_plug_check_resize (GtkContainer *container)
{
  if (GTK_WIDGET_TOPLEVEL (container))
Matthias Clasen's avatar
Matthias Clasen committed
963
    GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
964 965 966 967
  else
    GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
}

968 969 970 971 972 973 974 975 976 977
/**
 * _gtk_plug_handle_modality_on:
 *
 * @plug: a #GtkPlug
 *
 * Called from the GtkPlug backend when the corresponding socket has
 * told the plug that it modality has toggled on.
 */
void
_gtk_plug_handle_modality_on (GtkPlug *plug)
978 979 980 981
{
  if (!plug->modality_window)
    {
      plug->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
982 983
      gtk_window_set_screen (GTK_WINDOW (plug->modality_window),
			     gtk_widget_get_screen (GTK_WIDGET (plug)));
984
      gtk_widget_realize (plug->modality_window);
985
      gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug->modality_window));
986 987 988 989
      gtk_grab_add (plug->modality_window);
    }
}

990 991 992 993 994 995 996 997 998 999
/**
 * _gtk_plug_handle_modality_off:
 *
 * @plug: a #GtkPlug
 *
 * Called from the GtkPlug backend when the corresponding socket has
 * told the plug that it modality has toggled off.
 */
void
_gtk_plug_handle_modality_off (GtkPlug *plug)
1000 1001 1002 1003 1004 1005 1006 1007
{
  if (plug->modality_window)
    {
      gtk_widget_destroy (plug->modality_window);
      plug->modality_window = NULL;
    }
}

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
/**
 * _gtk_plug_focus_first_last:
 *
 * @plug: a #GtkPlug
 * @direction: a direction
 *
 * Called from the GtkPlug backend when the corresponding socket has
 * told the plug that it has received the focus.
 */
void
_gtk_plug_focus_first_last (GtkPlug          *plug,
			    GtkDirectionType  direction)
1020
{
1021
  GtkWindow *window = GTK_WINDOW (plug);
1022
  GtkWidget *parent;
1023

1024
  if (window->focus_widget)
1025
    {
1026 1027
      parent = window->focus_widget->parent;
      while (parent)
1028
	{
1029 1030
	  gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
	  parent = GTK_WIDGET (parent)->parent;
1031 1032
	}
      
1033
      gtk_window_set_focus (GTK_WINDOW (plug), NULL);
1034 1035
    }

1036
  gtk_widget_child_focus (GTK_WIDGET (plug), direction);
1037
}
1038 1039 1040

#define __GTK_PLUG_C__
#include "gtkaliasdef.c"