gtkplug.c 28.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"
Matthias Clasen's avatar
Matthias Clasen committed
33
#include "gtkintl.h"
34
#include "gtkprivate.h"
35
#include "gtkplugprivate.h"
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/**
 * SECTION:gtkplug
 * @Short_description: Toplevel for embedding into other processes
 * @Title: GtkPlug
 * @See_also: #GtkSocket
 *
 * Together with #GtkSocket, #GtkPlug provides the ability
 * to embed widgets from one process into another process
 * in a fashion that is transparent to the user. One
 * process creates a #GtkSocket widget and passes the
 * ID of that widget's window to the other process,
 * which then creates a #GtkPlug with that window ID.
 * Any widgets contained in the #GtkPlug then will appear
 * inside the first application's window.
 *
 * <note>
 * The #GtkPlug and #GtkSocket widgets are currently not available
 * on all platforms supported by GTK+.
 * </note>
 */

58 59 60 61
static void            gtk_plug_get_property          (GObject     *object,
						       guint        prop_id,
						       GValue      *value,
						       GParamSpec  *pspec);
62
static void            gtk_plug_finalize              (GObject          *object);
63 64
static void            gtk_plug_realize               (GtkWidget        *widget);
static void            gtk_plug_unrealize             (GtkWidget        *widget);
65 66 67 68 69 70
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);
71 72
static gboolean        gtk_plug_key_press_event       (GtkWidget        *widget,
						       GdkEventKey      *event);
73 74
static gboolean        gtk_plug_focus_event           (GtkWidget        *widget,
						       GdkEventFocus    *event);
75 76
static void            gtk_plug_set_focus             (GtkWindow        *window,
						       GtkWidget        *focus);
77
static gboolean        gtk_plug_focus                 (GtkWidget        *widget,
78
						       GtkDirectionType  direction);
79
static void            gtk_plug_check_resize          (GtkContainer     *container);
80
static void            gtk_plug_keys_changed          (GtkWindow        *window);
81

82
static GtkBinClass *bin_class = NULL;
83

84 85 86 87 88 89
typedef struct
{
  guint			 accelerator_key;
  GdkModifierType	 accelerator_mods;
} GrabbedKey;

90 91 92
enum {
  PROP_0,
  PROP_EMBEDDED,
Tim Janik's avatar
Tim Janik committed
93
  PROP_SOCKET_WINDOW
94 95
};

96 97 98 99 100 101 102
enum {
  EMBEDDED,
  LAST_SIGNAL
}; 

static guint plug_signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
103
G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
104

105 106 107 108 109 110 111 112 113 114 115 116 117
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;
Tim Janik's avatar
Tim Janik committed
118 119 120
    case PROP_SOCKET_WINDOW:
      g_value_set_object (value, plug->socket_window);
      break;
121 122 123 124 125 126
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

127 128 129
static void
gtk_plug_class_init (GtkPlugClass *class)
{
130
  GObjectClass *gobject_class = (GObjectClass *)class;
131 132
  GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
  GtkWindowClass *window_class = (GtkWindowClass *)class;
133
  GtkContainerClass *container_class = (GtkContainerClass *)class;
134

Manish Singh's avatar
Manish Singh committed
135
  bin_class = g_type_class_peek (GTK_TYPE_BIN);
136

137
  gobject_class->get_property = gtk_plug_get_property;
138 139
  gobject_class->finalize = gtk_plug_finalize;
  
140
  widget_class->realize = gtk_plug_realize;
141
  widget_class->unrealize = gtk_plug_unrealize;
142
  widget_class->key_press_event = gtk_plug_key_press_event;
143 144
  widget_class->focus_in_event = gtk_plug_focus_event;
  widget_class->focus_out_event = gtk_plug_focus_event;
145

146 147 148 149 150 151 152 153 154
  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;
155 156

  window_class->set_focus = gtk_plug_set_focus;
157
  window_class->keys_changed = gtk_plug_keys_changed;
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  /**
   * 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));

Tim Janik's avatar
Tim Janik committed
174 175 176 177 178
  /**
   * GtkPlug:socket-window:
   *
   * The window of the socket the plug is embedded in.
   *
179
   * Since: 2.14
Tim Janik's avatar
Tim Janik committed
180 181 182
   */
  g_object_class_install_property (gobject_class,
				   PROP_SOCKET_WINDOW,
183
				   g_param_spec_object ("socket-window",
Tim Janik's avatar
Tim Janik committed
184 185 186 187 188
							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
189 190 191 192
  /**
   * GtkPlug::embedded:
   * @plug: the object on which the signal was emitted
   *
193
   * Gets emitted when the plug becomes embedded in a socket.
Matthias Clasen's avatar
Matthias Clasen committed
194
   */ 
195
  plug_signals[EMBEDDED] =
Matthias Clasen's avatar
Matthias Clasen committed
196
    g_signal_new (I_("embedded"),
197 198 199 200
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPlugClass, embedded),
		  NULL, NULL,
201
		  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
202
		  G_TYPE_NONE, 0);
203 204 205 206 207 208 209 210 211 212 213
}

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

  window = GTK_WINDOW (plug);

  window->type = GTK_WINDOW_TOPLEVEL;
}
214

215 216 217 218
static void
gtk_plug_set_is_child (GtkPlug  *plug,
		       gboolean  is_child)
{
219 220
  g_assert (!GTK_WIDGET (plug)->parent);
      
221 222
  if (is_child)
    {
223
      if (plug->modality_window)
224
	_gtk_plug_handle_modality_off (plug);
225 226 227 228 229 230 231 232

      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;
	}
      
233 234 235 236 237
      /* 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 ().
       */
238
      if (gtk_widget_get_mapped (GTK_WIDGET (plug)))
239 240
	gtk_widget_unmap (GTK_WIDGET (plug));
      
241
      _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
242
      gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
243 244 245 246 247

      _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), GTK_WIDGET (plug));
    }
  else
    {
248 249 250 251 252
      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);
	  
253 254 255
      plug->modality_group = gtk_window_group_new ();
      gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug));
      
256
      _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
257 258 259
      gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);

      _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
260 261 262
    }
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
/**
 * 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);

278
  if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
279 280 281 282 283
    gtk_widget_realize (GTK_WIDGET (plug));

  return _gtk_plug_windowing_get_id (plug);
}

Tim Janik's avatar
Tim Janik committed
284 285 286 287 288
/**
 * gtk_plug_get_embedded:
 * @plug: a #GtkPlug
 *
 * Determines whether the plug is embedded in a socket.
Tim Janik's avatar
Tim Janik committed
289
 *
Tim Janik's avatar
Tim Janik committed
290
 * Return value: %TRUE if the plug is embedded in a socket
291 292
 *
 * Since: 2.14
Tim Janik's avatar
Tim Janik committed
293 294 295 296
 **/
gboolean
gtk_plug_get_embedded (GtkPlug *plug)
{
297
  g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
Tim Janik's avatar
Tim Janik committed
298 299 300 301

  return plug->socket_window != NULL;
}

Tim Janik's avatar
Tim Janik committed
302 303 304 305 306 307 308
/**
 * 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
309 310
 *
 * Since: 2.14
Tim Janik's avatar
Tim Janik committed
311 312 313 314 315 316 317 318 319
 **/
GdkWindow *
gtk_plug_get_socket_window (GtkPlug *plug)
{
  g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);

  return plug->socket_window;
}

320 321 322
/**
 * _gtk_plug_add_to_socket:
 * @plug: a #GtkPlug
323
 * @socket_: a #GtkSocket
324
 * 
325
 * Adds a plug to a socket within the same application.
326
 **/
327
void
328
_gtk_plug_add_to_socket (GtkPlug   *plug,
Matthias Clasen's avatar
Matthias Clasen committed
329
			 GtkSocket *socket_)
330
{
331
  GtkWidget *widget;
332
  gint w, h;
333 334
  
  g_return_if_fail (GTK_IS_PLUG (plug));
Matthias Clasen's avatar
Matthias Clasen committed
335
  g_return_if_fail (GTK_IS_SOCKET (socket_));
336
  g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
337

338 339
  widget = GTK_WIDGET (plug);

340 341
  gtk_plug_set_is_child (plug, TRUE);
  plug->same_app = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
342 343
  socket_->same_app = TRUE;
  socket_->plug_widget = widget;
344

Matthias Clasen's avatar
Matthias Clasen committed
345
  plug->socket_window = GTK_WIDGET (socket_)->window;
346
  g_object_ref (plug->socket_window);
347 348
  g_signal_emit (plug, plug_signals[EMBEDDED], 0);
  g_object_notify (G_OBJECT (plug), "embedded");
349

350
  if (gtk_widget_get_realized (widget))
351 352 353 354
    {
      gdk_drawable_get_size (GDK_DRAWABLE (widget->window), &w, &h);
      gdk_window_reparent (widget->window, plug->socket_window, -w, -h);
    }
355

Matthias Clasen's avatar
Matthias Clasen committed
356
  gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
357

358
  g_signal_emit_by_name (socket_, "plug-added");
359 360
}

361 362
/**
 * _gtk_plug_send_delete_event:
Matthias Clasen's avatar
Matthias Clasen committed
363
 * @widget: a #GtkWidget
364 365 366 367 368 369 370
 *
 * 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)
371 372
{
  GdkEvent *event = gdk_event_new (GDK_DELETE);
373

374 375 376
  event->any.window = g_object_ref (widget->window);
  event->any.send_event = FALSE;

377 378
  g_object_ref (widget);

379 380
  if (!gtk_widget_event (widget, event))
    gtk_widget_destroy (widget);
381 382

  g_object_unref (widget);
383 384 385 386

  gdk_event_free (event);
}

387
/**
388
 * _gtk_plug_remove_from_socket:
389
 * @plug: a #GtkPlug
Matthias Clasen's avatar
Matthias Clasen committed
390
 * @socket_: a #GtkSocket
391
 * 
392
 * Removes a plug from a socket within the same application.
393 394 395
 **/
void
_gtk_plug_remove_from_socket (GtkPlug   *plug,
Matthias Clasen's avatar
Matthias Clasen committed
396
			      GtkSocket *socket_)
397 398 399 400 401 402
{
  GtkWidget *widget;
  gboolean result;
  gboolean widget_was_visible;

  g_return_if_fail (GTK_IS_PLUG (plug));
Matthias Clasen's avatar
Matthias Clasen committed
403
  g_return_if_fail (GTK_IS_SOCKET (socket_));
404
  g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
405 406 407

  widget = GTK_WIDGET (plug);

408 409 410
  if (GTK_WIDGET_IN_REPARENT (widget))
    return;

411
  g_object_ref (plug);
Matthias Clasen's avatar
Matthias Clasen committed
412
  g_object_ref (socket_);
413

414
  widget_was_visible = gtk_widget_get_visible (widget);
415 416
  
  gdk_window_hide (widget->window);
417
  GTK_PRIVATE_SET_FLAG (plug, GTK_IN_REPARENT);
418 419 420
  gdk_window_reparent (widget->window,
		       gtk_widget_get_root_window (widget),
		       0, 0);
421 422 423
  gtk_widget_unparent (GTK_WIDGET (plug));
  GTK_PRIVATE_UNSET_FLAG (plug, GTK_IN_REPARENT);
  
Matthias Clasen's avatar
Matthias Clasen committed
424 425
  socket_->plug_widget = NULL;
  if (socket_->plug_window != NULL)
426
    {
Matthias Clasen's avatar
Matthias Clasen committed
427 428
      g_object_unref (socket_->plug_window);
      socket_->plug_window = NULL;
429
    }
430
  
Matthias Clasen's avatar
Matthias Clasen committed
431
  socket_->same_app = FALSE;
432 433

  plug->same_app = FALSE;
434 435 436 437 438
  if (plug->socket_window != NULL)
    {
      g_object_unref (plug->socket_window);
      plug->socket_window = NULL;
    }
439
  gtk_plug_set_is_child (plug, FALSE);
440

441
  g_signal_emit_by_name (socket_, "plug-removed", &result);
442
  if (!result)
Matthias Clasen's avatar
Matthias Clasen committed
443
    gtk_widget_destroy (GTK_WIDGET (socket_));
444

445 446
  if (widget->window)
    _gtk_plug_send_delete_event (widget);
447

448 449
  g_object_unref (plug);

450
  if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
Matthias Clasen's avatar
Matthias Clasen committed
451
    gtk_widget_queue_resize (GTK_WIDGET (socket_));
452

Matthias Clasen's avatar
Matthias Clasen committed
453
  g_object_unref (socket_);
454 455
}

456 457 458 459 460
/**
 * gtk_plug_construct:
 * @plug: a #GtkPlug.
 * @socket_id: the XID of the socket's window.
 *
461
 * Finish the initialization of @plug for a given #GtkSocket identified by
462 463
 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
 **/
464 465 466
void
gtk_plug_construct (GtkPlug         *plug,
		    GdkNativeWindow  socket_id)
467
{
Owen Taylor's avatar
Owen Taylor committed
468
  gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
469 470 471 472
}

/**
 * gtk_plug_construct_for_display:
473
 * @plug: a #GtkPlug.
474 475 476 477
 * @display: the #GdkDisplay associated with @socket_id's 
 *	     #GtkSocket.
 * @socket_id: the XID of the socket's window.
 *
478
 * Finish the initialization of @plug for a given #GtkSocket identified by
479 480
 * @socket_id which is currently displayed on @display.
 * This function will generally only be used by classes deriving from #GtkPlug.
481 482
 *
 * Since: 2.2
483 484
 **/
void
485 486
gtk_plug_construct_for_display (GtkPlug         *plug,
				GdkDisplay	*display,
487
				GdkNativeWindow  socket_id)
488
{
489
  if (socket_id)
490
    {
491 492
      gpointer user_data = NULL;

493
      plug->socket_window = gdk_window_lookup_for_display (display, socket_id);
494
      if (plug->socket_window)
495
	{
496 497 498
	  gdk_window_get_user_data (plug->socket_window, &user_data);

	  if (user_data)
499
	    {
500 501 502 503 504 505 506
	      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;
		}
507
	    }
508 509
	  else
	    g_object_ref (plug->socket_window);
510
	}
511 512
      else
	plug->socket_window = gdk_window_foreign_new_for_display (display, socket_id);
513

514
      if (plug->socket_window) {
Manish Singh's avatar
Manish Singh committed
515
	g_signal_emit (plug, plug_signals[EMBEDDED], 0);
516 517 518

        g_object_notify (G_OBJECT (plug), "embedded");
      }
519 520
    }
}
521

522 523 524 525 526 527 528 529 530 531
/**
 * 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.
 **/
532
GtkWidget*
533
gtk_plug_new (GdkNativeWindow socket_id)
534
{
Owen Taylor's avatar
Owen Taylor committed
535
  return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
536 537 538 539 540 541 542
}

/**
 * gtk_plug_new_for_display:
 * @display : the #GdkDisplay on which @socket_id is displayed
 * @socket_id: the XID of the socket's window.
 * 
543
 * Create a new plug widget inside the #GtkSocket identified by socket_id.
544 545
 *
 * Return value: the new #GtkPlug widget.
546 547
 *
 * Since: 2.2
548 549 550 551
 */
GtkWidget*
gtk_plug_new_for_display (GdkDisplay	  *display,
			  GdkNativeWindow  socket_id)
552 553 554
{
  GtkPlug *plug;

Manish Singh's avatar
Manish Singh committed
555
  plug = g_object_new (GTK_TYPE_PLUG, NULL);
556
  gtk_plug_construct_for_display (plug, display, socket_id);
557 558 559
  return GTK_WIDGET (plug);
}

560 561 562 563 564 565 566 567 568 569 570
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
571
  G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
572 573
}

574 575 576
static void
gtk_plug_unrealize (GtkWidget *widget)
{
577
  GtkPlug *plug = GTK_PLUG (widget);
578 579 580 581

  if (plug->socket_window != NULL)
    {
      gdk_window_set_user_data (plug->socket_window, NULL);
Manish Singh's avatar
Manish Singh committed
582
      g_object_unref (plug->socket_window);
583
      plug->socket_window = NULL;
584 585

      g_object_notify (G_OBJECT (widget), "embedded");
586 587
    }

588 589 590
  if (!plug->same_app)
    {
      if (plug->modality_window)
591
	_gtk_plug_handle_modality_off (plug);
592

593 594 595
      gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
      g_object_unref (plug->modality_group);
    }
596

597
  GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
598 599
}

600 601 602
static void
gtk_plug_realize (GtkWidget *widget)
{
603 604
  GtkWindow *window = GTK_WINDOW (widget);
  GtkPlug *plug = GTK_PLUG (widget);
605 606 607
  GdkWindowAttr attributes;
  gint attributes_mask;

608
  gtk_widget_set_realized (widget, TRUE);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

  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 |
625
			    GDK_KEY_RELEASE_MASK |
626 627 628 629 630 631 632 633
			    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);

634
  if (gtk_widget_is_toplevel (widget))
635
    {
636 637
      attributes.window_type = GDK_WINDOW_TOPLEVEL;

638
      gdk_error_trap_push ();
639 640 641 642 643 644 645 646
      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));
647 648 649 650 651 652
      if (gdk_error_trap_pop ()) /* Uh-oh */
	{
	  gdk_error_trap_push ();
	  gdk_window_destroy (widget->window);
	  gdk_flush ();
	  gdk_error_trap_pop ();
653 654
	  widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
					   &attributes, attributes_mask);
655
	}
656
      
657 658 659
      gdk_window_add_filter (widget->window,
			     _gtk_plug_windowing_filter_func,
			     widget);
660 661 662 663

      plug->modality_group = gtk_window_group_new ();
      gtk_window_group_add_window (plug->modality_group, window);
      
664
      _gtk_plug_windowing_realize_toplevel (plug);
665
    }
666
  else
667 668
    widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
				     &attributes, attributes_mask);      
669
  
670 671 672 673
  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);
674 675

  gdk_window_enable_synchronized_configure (widget->window);
676 677 678 679 680
}

static void
gtk_plug_show (GtkWidget *widget)
{
681
  if (gtk_widget_is_toplevel (widget))
Matthias Clasen's avatar
Matthias Clasen committed
682
    GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
683 684 685 686 687 688 689
  else
    GTK_WIDGET_CLASS (bin_class)->show (widget);
}

static void
gtk_plug_hide (GtkWidget *widget)
{
690
  if (gtk_widget_is_toplevel (widget))
Matthias Clasen's avatar
Matthias Clasen committed
691
    GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
692 693 694 695 696 697 698 699 700 701 702 703
  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)
{
704
  if (gtk_widget_is_toplevel (widget))
705 706
    {
      GtkBin *bin = GTK_BIN (widget);
707
      GtkPlug *plug = GTK_PLUG (widget);
708
      
709
      gtk_widget_set_mapped (widget, TRUE);
710 711

      if (bin->child &&
712
	  gtk_widget_get_visible (bin->child) &&
713
	  !gtk_widget_get_mapped (bin->child))
714 715
	gtk_widget_map (bin->child);

716
      _gtk_plug_windowing_map_toplevel (plug);
717 718 719 720 721 722 723 724 725 726 727 728
      
      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)
{
729
  if (gtk_widget_is_toplevel (widget))
730
    {
731 732
      GtkPlug *plug = GTK_PLUG (widget);

733
      gtk_widget_set_mapped (widget, FALSE);
734 735

      gdk_window_hide (widget->window);
736 737

      _gtk_plug_windowing_unmap_toplevel (plug);
738 739 740 741 742 743 744 745
      
      gdk_synthesize_window_state (widget->window,
				   0,
				   GDK_WINDOW_STATE_WITHDRAWN);
    }
  else
    GTK_WIDGET_CLASS (bin_class)->unmap (widget);
}
746

747 748 749 750
static void
gtk_plug_size_allocate (GtkWidget     *widget,
			GtkAllocation *allocation)
{
751
  if (gtk_widget_is_toplevel (widget))
Matthias Clasen's avatar
Matthias Clasen committed
752
    GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
753 754 755 756
  else
    {
      GtkBin *bin = GTK_BIN (widget);

757 758
      widget->allocation = *allocation;

759
      if (gtk_widget_get_realized (widget))
760 761 762
	gdk_window_move_resize (widget->window,
				allocation->x, allocation->y,
				allocation->width, allocation->height);
763

764
      if (bin->child && gtk_widget_get_visible (bin->child))
765 766 767 768 769 770 771 772 773 774 775 776 777
	{
	  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);
	}
      
    }
778 779
}

780
static gboolean
781 782 783
gtk_plug_key_press_event (GtkWidget   *widget,
			  GdkEventKey *event)
{
784
  if (gtk_widget_is_toplevel (widget))
Matthias Clasen's avatar
Matthias Clasen committed
785
    return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
786
  else
787
    return FALSE;
788 789
}

790 791 792 793 794 795 796 797 798 799 800
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;
}

801 802 803
static void
gtk_plug_set_focus (GtkWindow *window,
		    GtkWidget *focus)
804
{
805
  GtkPlug *plug = GTK_PLUG (window);
806

Matthias Clasen's avatar
Matthias Clasen committed
807
  GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
808 809
  
  /* Ask for focus from embedder
810
   */
811

812
  if (focus && !window->has_toplevel_focus)
813
    _gtk_plug_windowing_set_focus (plug);
814 815
}

816 817 818 819 820 821 822 823 824
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;
825

826 827
  return h;
}
828

829 830 831 832 833
static gboolean
grabbed_key_equal (gconstpointer a, gconstpointer b)
{
  const GrabbedKey *keya = a;
  const GrabbedKey *keyb = b;
834

835 836 837
  return (keya->accelerator_key == keyb->accelerator_key &&
	  keya->accelerator_mods == keyb->accelerator_mods);
}
838

839
static void
840
add_grabbed_key (gpointer key, gpointer val, gpointer data)
841 842 843 844 845 846 847
{
  GrabbedKey *grabbed_key = key;
  GtkPlug *plug = data;

  if (!plug->grabbed_keys ||
      !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
    {
848 849 850
      _gtk_plug_windowing_add_grabbed_key (plug,
					   grabbed_key->accelerator_key,
					   grabbed_key->accelerator_mods);
851
    }
852
}
853

854
static void
855 856 857
add_grabbed_key_always (gpointer key,
			gpointer val,
			gpointer data)
858 859 860 861
{
  GrabbedKey *grabbed_key = key;
  GtkPlug *plug = data;

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
  _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);
880 881 882 883
}

static void
remove_grabbed_key (gpointer key, gpointer val, gpointer data)
884 885 886 887 888 889 890
{
  GrabbedKey *grabbed_key = key;
  GtkPlug *plug = data;

  if (!plug->grabbed_keys ||
      !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
    {
891 892 893
      _gtk_plug_windowing_remove_grabbed_key (plug, 
					      grabbed_key->accelerator_key,
					      grabbed_key->accelerator_mods);
894
    }
895 896 897
}

static void
898 899 900 901 902
keys_foreach (GtkWindow      *window,
	      guint           keyval,
	      GdkModifierType modifiers,
	      gboolean        is_mnemonic,
	      gpointer        data)
903
{
904
  GHashTable *new_grabbed_keys = data;
905
  GrabbedKey *key = g_slice_new (GrabbedKey);
906 907 908 909 910

  key->accelerator_key = keyval;
  key->accelerator_mods = modifiers;
  
  g_hash_table_replace (new_grabbed_keys, key, key);
911
}
912

913 914 915 916 917 918
static void
grabbed_key_free (gpointer data)
{
  g_slice_free (GrabbedKey, data);
}

919
static void
920
gtk_plug_keys_changed (GtkWindow *window)
921 922 923
{
  GHashTable *new_grabbed_keys, *old_grabbed_keys;
  GtkPlug *plug = GTK_PLUG (window);
924

925
  new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
926
  _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
927

928 929
  if (plug->socket_window)
    g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
930

931 932 933 934 935
  old_grabbed_keys = plug->grabbed_keys;
  plug->grabbed_keys = new_grabbed_keys;

  if (old_grabbed_keys)
    {
936 937 938
      if (plug->socket_window)
	g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
      g_hash_table_destroy (old_grabbed_keys);
939 940 941 942
    }
}

static gboolean
943
gtk_plug_focus (GtkWidget        *widget,
944 945
		GtkDirectionType  direction)
{
946 947 948 949
  GtkBin *bin = GTK_BIN (widget);
  GtkPlug *plug = GTK_PLUG (widget);
  GtkWindow *window = GTK_WINDOW (widget);
  GtkContainer *container = GTK_CONTAINER (widget);
950 951 952 953 954 955 956
  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)
    {
957
      if (gtk_widget_child_focus (old_focus_child, direction))
958
	return TRUE;
959 960 961

      if (window->focus_widget)
	{
962 963 964 965 966 967 968 969 970 971 972 973 974 975
	  /* 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 */
976
      if (bin->child && gtk_widget_child_focus (bin->child, direction))
977
        return TRUE;
978 979
    }

980
  if (!GTK_CONTAINER (window)->focus_child)
981
    _gtk_plug_windowing_focus_to_parent (plug, direction);
982

983 984
  return FALSE;
}
985

986 987 988
static void
gtk_plug_check_resize (GtkContainer *container)
{
989
  if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
Matthias Clasen's avatar
Matthias Clasen committed
990
    GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
991 992 993 994
  else
    GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
}

995 996 997 998 999 1000 1001 1002 1003 1004
/**
 * _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)
1005 1006 1007 1008
{
  if (!plug->modality_window)
    {
      plug->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
1009 1010
      gtk_window_set_screen (GTK_WINDOW (plug->modality_window),
			     gtk_widget_get_screen (GTK_WIDGET (plug)));
1011
      gtk_widget_realize (plug->modality_window);
1012
      gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug->modality_window));
1013 1014 1015 1016
      gtk_grab_add (plug->modality_window);
    }
}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
/**
 * _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)
1027 1028 1029 1030 1031 1032 1033 1034
{
  if (plug->modality_window)
    {
      gtk_widget_destroy (plug->modality_window);
      plug->modality_window = NULL;
    }
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
/**
 * _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)
1047
{
1048
  GtkWindow *window = GTK_WINDOW (plug);
1049
  GtkWidget *parent;
1050

1051
  if (window->focus_widget)
1052
    {
1053 1054
      parent = window->focus_widget->parent;
      while (parent)
1055
	{
1056 1057
	  gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
	  parent = GTK_WIDGET (parent)->parent;
1058 1059
	}
      
1060
      gtk_window_set_focus (GTK_WINDOW (plug), NULL);
1061 1062
    }

1063
  gtk_widget_child_focus (GTK_WIDGET (plug), direction);
1064
}