gtksocket.c 45.2 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
16 17 18 19
 */

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

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

27
#include "config.h"
28

29
#include "gtksocketprivate.h"
30

31 32
#include <string.h>

33
#include "gtkmarshalers.h"
34
#include "gtksizerequest.h"
35
#include "gtkplug.h"
36
#include "gtkprivate.h"
37
#include "gtkrender.h"
38
#include "gtkdnd.h"
39
#include "gtkdebug.h"
Matthias Clasen's avatar
Matthias Clasen committed
40
#include "gtkintl.h"
41
#include "gtkmain.h"
Benjamin Otte's avatar
Benjamin Otte committed
42
#include "gtkwidgetprivate.h"
43

44
#include <gdk/gdkx.h>
45 46 47 48 49 50 51
#include <gdk/gdkprivate.h>

#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif

#include "gtkxembed.h"
52

53

54 55 56 57
/**
 * SECTION:gtksocket
 * @Short_description: Container for widgets from other processes
 * @Title: GtkSocket
58
 * @include: gtk/gtkx.h
59
 * @See_also: #GtkPlug, [XEmbed Protocol](http://www.freedesktop.org/Standards/xembed-spec)
60
 *
61 62 63
 * Together with #GtkPlug, #GtkSocket 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
64
 * and passes that widget’s window ID to the other process, which then
65
 * creates a #GtkPlug with that window ID. Any widgets contained in the
66
 * #GtkPlug then will appear inside the first application’s window.
67
 *
68
 * The socket’s window ID is obtained by using gtk_socket_get_id().
69 70
 * Before using this function, the socket must have been realized,
 * and for hence, have been added to its parent.
71
 *
72 73
 * ## Obtaining the window ID of a socket.
 *
74
 * |[<!-- language="C" -->
75
 * GtkWidget *socket = gtk_socket_new ();
76 77 78
 * gtk_widget_show (socket);
 * gtk_container_add (GTK_CONTAINER (parent), socket);
 *
79 80
 * // The following call is only necessary if one of
 * // the ancestors of the socket is not yet visible.
81 82 83
 * gtk_widget_realize (socket);
 * g_print ("The ID of the sockets window is %#x\n",
 *          gtk_socket_get_id (socket));
84
 * ]|
85 86
 *
 * Note that if you pass the window ID of the socket to another
87 88 89 90 91 92 93
 * process that will create a plug in the socket, you must make
 * sure that the socket widget is not destroyed until that plug
 * is created. Violating this rule will cause unpredictable
 * consequences, the most likely consequence being that the plug
 * will appear as a separate toplevel window. You can check if
 * the plug has been created by using gtk_socket_get_plug_window().
 * If it returns a non-%NULL value, then the plug has been
94 95
 * successfully created inside of the socket.
 *
96 97 98 99 100
 * When GTK+ is notified that the embedded window has been destroyed,
 * then it will destroy the socket as well. You should always,
 * therefore, be prepared for your sockets to be destroyed at any
 * time when the main event loop is running. To prevent this from
 * happening, you can connect to the #GtkSocket::plug-removed signal.
101 102
 *
 * The communication between a #GtkSocket and a #GtkPlug follows the
103 104
 * [XEmbed Protocol](http://www.freedesktop.org/Standards/xembed-spec).
 * This protocol has also been implemented in other toolkits,
105 106
 * e.g. Qt, allowing the same level of
 * integration when embedding a Qt widget
107
 * in GTK or vice versa.
108
 *
109 110 111
 * The #GtkPlug and #GtkSocket widgets are only available when GTK+
 * is compiled for the X11 platform and %GDK_WINDOWING_X11 is defined.
 * They can only be used on a #GdkX11Display. To use #GtkPlug and
112
 * #GtkSocket, you need to include the `gtk/gtkx.h`
113
 * header.
114 115
 */

116 117
/* Forward declararations */

118
static void     gtk_socket_finalize             (GObject          *object);
119 120
static void     gtk_socket_notify               (GObject          *object,
						 GParamSpec       *pspec);
121 122
static void     gtk_socket_realize              (GtkWidget        *widget);
static void     gtk_socket_unrealize            (GtkWidget        *widget);
123 124 125 126 127 128
static void     gtk_socket_get_preferred_width  (GtkWidget        *widget,
                                                 gint             *minimum,
                                                 gint             *natural);
static void     gtk_socket_get_preferred_height (GtkWidget        *widget,
                                                 gint             *minimum,
                                                 gint             *natural);
129 130 131 132 133 134
static void     gtk_socket_size_allocate        (GtkWidget        *widget,
						 GtkAllocation    *allocation);
static void     gtk_socket_hierarchy_changed    (GtkWidget        *widget,
						 GtkWidget        *old_toplevel);
static void     gtk_socket_grab_notify          (GtkWidget        *widget,
						 gboolean          was_grabbed);
135
static gboolean gtk_socket_key_event            (GtkWidget        *widget,
136 137 138 139 140 141 142 143 144
						 GdkEventKey      *event);
static gboolean gtk_socket_focus                (GtkWidget        *widget,
						 GtkDirectionType  direction);
static void     gtk_socket_remove               (GtkContainer     *container,
						 GtkWidget        *widget);
static void     gtk_socket_forall               (GtkContainer     *container,
						 gboolean          include_internals,
						 GtkCallback       callback,
						 gpointer          callback_data);
145
static void     gtk_socket_add_window           (GtkSocket        *socket,
146
                                                 Window            xid,
147 148 149 150 151 152 153 154 155 156 157
                                                 gboolean          need_reparent);
static GdkFilterReturn gtk_socket_filter_func   (GdkXEvent        *gdk_xevent,
                                                 GdkEvent         *event,
                                                 gpointer          data);

static gboolean xembed_get_info                 (GdkWindow        *gdk_window,
                                                 unsigned long    *version,
                                                 unsigned long    *flags);

/* From Tk */
#define EMBEDDED_APP_WANTS_FOCUS NotifyNormal+20
158 159


160 161
/* Local data */

162 163 164 165 166 167
typedef struct
{
  guint			 accel_key;
  GdkModifierType	 accel_mods;
} GrabbedKey;

168 169 170 171 172 173 174 175
enum {
  PLUG_ADDED,
  PLUG_REMOVED,
  LAST_SIGNAL
}; 

static guint socket_signals[LAST_SIGNAL] = { 0 };

176
G_DEFINE_TYPE_WITH_PRIVATE (GtkSocket, gtk_socket, GTK_TYPE_CONTAINER)
177

178 179 180 181
static void
gtk_socket_finalize (GObject *object)
{
  GtkSocket *socket = GTK_SOCKET (object);
182 183 184
  GtkSocketPrivate *priv = socket->priv;

  g_object_unref (priv->accel_group);
185

Matthias Clasen's avatar
Matthias Clasen committed
186
  G_OBJECT_CLASS (gtk_socket_parent_class)->finalize (object);
187 188
}

189 190 191 192 193 194 195 196 197 198 199 200
static gboolean
gtk_socket_draw (GtkWidget *widget,
                 cairo_t   *cr)
{
  gtk_render_background (gtk_widget_get_style_context (widget), cr,
                         0, 0,
                         gtk_widget_get_allocated_width (widget),
                         gtk_widget_get_allocated_height (widget));

  return GTK_WIDGET_CLASS (gtk_socket_parent_class)->draw (widget, cr);
}

201 202 203 204 205
static void
gtk_socket_class_init (GtkSocketClass *class)
{
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;
206
  GObjectClass *gobject_class;
207

208
  gobject_class = (GObjectClass *) class;
209 210 211
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;

212
  gobject_class->finalize = gtk_socket_finalize;
213
  gobject_class->notify = gtk_socket_notify;
214

215 216
  widget_class->realize = gtk_socket_realize;
  widget_class->unrealize = gtk_socket_unrealize;
217 218
  widget_class->get_preferred_width = gtk_socket_get_preferred_width;
  widget_class->get_preferred_height = gtk_socket_get_preferred_height;
219
  widget_class->size_allocate = gtk_socket_size_allocate;
220 221
  widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
  widget_class->grab_notify = gtk_socket_grab_notify;
222 223
  widget_class->key_press_event = gtk_socket_key_event;
  widget_class->key_release_event = gtk_socket_key_event;
224
  widget_class->focus = gtk_socket_focus;
225
  widget_class->draw = gtk_socket_draw;
226

227
  /* We don't want to show_all the in-process plug, if any.
228 229
   */
  widget_class->show_all = gtk_widget_show;
230

231 232
  container_class->remove = gtk_socket_remove;
  container_class->forall = gtk_socket_forall;
233

Matthias Clasen's avatar
Matthias Clasen committed
234 235 236 237 238 239 240
  /**
   * GtkSocket::plug-added:
   * @socket_: the object which received the signal
   *
   * This signal is emitted when a client is successfully
   * added to the socket. 
   */
241
  socket_signals[PLUG_ADDED] =
242
    g_signal_new (I_("plug-added"),
243 244 245 246
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkSocketClass, plug_added),
		  NULL, NULL,
247
		  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
248
		  G_TYPE_NONE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
249 250 251 252 253 254 255 256 257

  /**
   * GtkSocket::plug-removed:
   * @socket_: the object which received the signal
   *
   * This signal is emitted when a client is removed from the socket. 
   * The default action is to destroy the #GtkSocket widget, so if you 
   * want to reuse it you must add a signal handler that returns %TRUE. 
   *
258
   * Returns: %TRUE to stop other handlers from being invoked.
Matthias Clasen's avatar
Matthias Clasen committed
259
   */
260
  socket_signals[PLUG_REMOVED] =
261
    g_signal_new (I_("plug-removed"),
262 263 264 265
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkSocketClass, plug_removed),
                  _gtk_boolean_handled_accumulator, NULL,
266
		  _gtk_marshal_BOOLEAN__VOID,
267
		  G_TYPE_BOOLEAN, 0);
268 269 270 271 272
}

static void
gtk_socket_init (GtkSocket *socket)
{
273 274
  GtkSocketPrivate *priv;

275
  priv = gtk_socket_get_instance_private (socket);
276
  socket->priv = priv;
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291
  priv->request_width = 0;
  priv->request_height = 0;
  priv->current_width = 0;
  priv->current_height = 0;

  priv->plug_window = NULL;
  priv->plug_widget = NULL;
  priv->focus_in = FALSE;
  priv->have_size = FALSE;
  priv->need_map = FALSE;
  priv->active = FALSE;

  priv->accel_group = gtk_accel_group_new ();
  g_object_set_data (G_OBJECT (priv->accel_group), I_("gtk-socket"), socket);
292 293
}

294 295 296 297 298
/**
 * gtk_socket_new:
 * 
 * Create a new empty #GtkSocket.
 * 
299
 * Returns:  the new #GtkSocket.
300
 **/
301
GtkWidget*
Tim Janik's avatar
Tim Janik committed
302
gtk_socket_new (void)
303 304 305
{
  GtkSocket *socket;

306
  socket = g_object_new (GTK_TYPE_SOCKET, NULL);
307 308 309 310

  return GTK_WIDGET (socket);
}

311 312
/**
 * gtk_socket_add_id:
313
 * @socket_: a #GtkSocket
314
 * @window: the Window of a client participating in the XEMBED protocol.
315 316 317 318 319
 *
 * Adds an XEMBED client, such as a #GtkPlug, to the #GtkSocket.  The
 * client may be in the same process or in a different process. 
 * 
 * To embed a #GtkPlug in a #GtkSocket, you can either create the
320
 * #GtkPlug with `gtk_plug_new (0)`, call 
321
 * gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
322 323 324 325 326 327 328 329
 * gtk_socket_add_id(), or you can call gtk_socket_get_id() to get the
 * window ID for the socket, and call gtk_plug_new() passing in that
 * ID.
 *
 * The #GtkSocket must have already be added into a toplevel window
 *  before you can make this call.
 **/
void           
330
gtk_socket_add_id (GtkSocket      *socket,
331
		   Window          window)
332 333
{
  g_return_if_fail (GTK_IS_SOCKET (socket));
334
  g_return_if_fail (_gtk_widget_get_anchored (GTK_WIDGET (socket)));
335

336
  if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
337 338
    gtk_widget_realize (GTK_WIDGET (socket));

339
  gtk_socket_add_window (socket, window, TRUE);
340 341 342 343
}

/**
 * gtk_socket_get_id:
344
 * @socket_: a #GtkSocket.
345 346 347
 * 
 * Gets the window ID of a #GtkSocket widget, which can then
 * be used to create a client embedded inside the socket, for
348
 * instance with gtk_plug_new(). 
Matthias Clasen's avatar
Matthias Clasen committed
349 350 351
 *
 * The #GtkSocket must have already be added into a toplevel window 
 * before you can make this call.
352
 * 
353
 * Returns: the window ID for the socket
354
 **/
355
Window
356 357 358
gtk_socket_get_id (GtkSocket *socket)
{
  g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
359
  g_return_val_if_fail (_gtk_widget_get_anchored (GTK_WIDGET (socket)), 0);
360

361
  if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
362 363
    gtk_widget_realize (GTK_WIDGET (socket));

364
  return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (socket)));
365 366
}

Tim Janik's avatar
Tim Janik committed
367 368 369 370 371 372 373
/**
 * gtk_socket_get_plug_window:
 * @socket_: a #GtkSocket.
 *
 * Retrieves the window of the plug. Use this to check if the plug has
 * been created inside of the socket.
 *
374
 * Returns: (transfer none): the window of the plug if available, or %NULL
Tim Janik's avatar
Tim Janik committed
375
 *
376
 * Since:  2.14
Tim Janik's avatar
Tim Janik committed
377 378 379 380 381 382
 **/
GdkWindow*
gtk_socket_get_plug_window (GtkSocket *socket)
{
  g_return_val_if_fail (GTK_IS_SOCKET (socket), NULL);

383
  return socket->priv->plug_window;
Tim Janik's avatar
Tim Janik committed
384 385
}

386 387 388
static void
gtk_socket_realize (GtkWidget *widget)
{
389 390
  GtkAllocation allocation;
  GdkWindow *window;
391
  GdkWindowAttr attributes;
392
  XWindowAttributes xattrs;
393 394
  gint attributes_mask;

395
  gtk_widget_set_realized (widget, TRUE);
396

397 398
  gtk_widget_get_allocation (widget, &allocation);

399
  attributes.window_type = GDK_WINDOW_CHILD;
400 401 402 403
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
404 405 406 407
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask = GDK_FOCUS_CHANGE_MASK;

408
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
409

410 411 412
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
413
  gtk_widget_register_window (widget, window);
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  XGetWindowAttributes (GDK_WINDOW_XDISPLAY (window),
			GDK_WINDOW_XID (window),
			&xattrs);

  /* Sooooo, it turns out that mozilla, as per the gtk2xt code selects
     for input on the socket with a mask of 0x0fffff (for god knows why)
     which includes ButtonPressMask causing a BadAccess if someone else
     also selects for this. As per the client-side windows merge we always
     normally selects for button press so we can emulate it on client
     side children that selects for button press. However, we don't need
     this for GtkSocket, so we unselect it here, fixing the crashes in
     firefox. */
  XSelectInput (GDK_WINDOW_XDISPLAY (window),
		GDK_WINDOW_XID (window), 
		(xattrs.your_event_mask & ~ButtonPressMask) |
		SubstructureNotifyMask | SubstructureRedirectMask);
431

432
  gdk_window_add_filter (window,
433
			 gtk_socket_filter_func,
434
			 widget);
435 436 437 438 439

  /* We sync here so that we make sure that if the XID for
   * our window is passed to another application, SubstructureRedirectMask
   * will be set by the time the other app creates its window.
   */
440
  gdk_display_sync (gtk_widget_get_display (widget));
441 442
}

443
/**
444
 * gtk_socket_end_embedding:
445 446 447 448
 * @socket: a #GtkSocket
 *
 * Called to end the embedding of a plug in the socket.
 */
449 450
static void
gtk_socket_end_embedding (GtkSocket *socket)
451
{
452
  GtkSocketPrivate *private = socket->priv;
453

454 455 456 457
  g_object_unref (private->plug_window);
  private->plug_window = NULL;
  private->current_width = 0;
  private->current_height = 0;
458
  private->resize_count = 0;
459

460
  gtk_accel_group_disconnect (private->accel_group, NULL);
461 462
}

463 464 465
static void
gtk_socket_unrealize (GtkWidget *widget)
{
466
  GtkSocket *socket = GTK_SOCKET (widget);
467
  GtkSocketPrivate *private = socket->priv;
468

469
  gtk_widget_set_realized (widget, FALSE);
470

471
  if (private->plug_widget)
472
    {
473
      _gtk_plug_remove_from_socket (GTK_PLUG (private->plug_widget), socket);
474
    }
475
  else if (private->plug_window)
476
    {
477
      gtk_socket_end_embedding (socket);
478 479
    }

480
  GTK_WIDGET_CLASS (gtk_socket_parent_class)->unrealize (widget);
481
}
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static void
gtk_socket_size_request (GtkSocket *socket)
{
  GtkSocketPrivate *private = socket->priv;
  XSizeHints hints;
  long supplied;
	  
  gdk_error_trap_push ();

  private->request_width = 1;
  private->request_height = 1;
	  
  if (XGetWMNormalHints (GDK_WINDOW_XDISPLAY (private->plug_window),
			 GDK_WINDOW_XID (private->plug_window),
			 &hints, &supplied))
    {
      if (hints.flags & PMinSize)
	{
	  private->request_width = MAX (hints.min_width, 1);
	  private->request_height = MAX (hints.min_height, 1);
	}
      else if (hints.flags & PBaseSize)
	{
	  private->request_width = MAX (hints.base_width, 1);
	  private->request_height = MAX (hints.base_height, 1);
	}
    }
  private->have_size = TRUE;
  
  gdk_error_trap_pop_ignored ();
}

515
static void
516 517 518
gtk_socket_get_preferred_width (GtkWidget *widget,
                                gint      *minimum,
                                gint      *natural)
519
{
520
  GtkSocket *socket = GTK_SOCKET (widget);
521
  GtkSocketPrivate *private = socket->priv;
522

523
  if (private->plug_widget)
524
    {
525
      gtk_widget_get_preferred_width (private->plug_widget, minimum, natural);
526 527 528
    }
  else
    {
529
      if (private->is_mapped && !private->have_size && private->plug_window)
530
        gtk_socket_size_request (socket);
531

532 533
      if (private->is_mapped && private->have_size)
        *minimum = *natural = MAX (private->request_width, 1);
534
      else
535 536 537 538 539 540 541 542 543 544
        *minimum = *natural = 1;
    }
}

static void
gtk_socket_get_preferred_height (GtkWidget *widget,
                                 gint      *minimum,
                                 gint      *natural)
{
  GtkSocket *socket = GTK_SOCKET (widget);
545
  GtkSocketPrivate *private = socket->priv;
546

547
  if (private->plug_widget)
548
    {
549
      gtk_widget_get_preferred_height (private->plug_widget, minimum, natural);
550 551 552
    }
  else
    {
553
      if (private->is_mapped && !private->have_size && private->plug_window)
554
        gtk_socket_size_request (socket);
555

556 557
      if (private->is_mapped && private->have_size)
        *minimum = *natural = MAX (private->request_height, 1);
558 559
      else
        *minimum = *natural = 1;
560 561 562
    }
}

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
static void
gtk_socket_send_configure_event (GtkSocket *socket)
{
  GtkAllocation allocation;
  XConfigureEvent xconfigure;
  gint x, y;

  g_return_if_fail (socket->priv->plug_window != NULL);

  memset (&xconfigure, 0, sizeof (xconfigure));
  xconfigure.type = ConfigureNotify;

  xconfigure.event = GDK_WINDOW_XID (socket->priv->plug_window);
  xconfigure.window = GDK_WINDOW_XID (socket->priv->plug_window);

  /* The ICCCM says that synthetic events should have root relative
   * coordinates. We still aren't really ICCCM compliant, since
   * we don't send events when the real toplevel is moved.
   */
  gdk_error_trap_push ();
  gdk_window_get_origin (socket->priv->plug_window, &x, &y);
  gdk_error_trap_pop_ignored ();

  gtk_widget_get_allocation (GTK_WIDGET(socket), &allocation);
  xconfigure.x = x;
  xconfigure.y = y;
  xconfigure.width = allocation.width;
  xconfigure.height = allocation.height;

  xconfigure.border_width = 0;
  xconfigure.above = None;
  xconfigure.override_redirect = False;

  gdk_error_trap_push ();
  XSendEvent (GDK_WINDOW_XDISPLAY (socket->priv->plug_window),
	      GDK_WINDOW_XID (socket->priv->plug_window),
	      False, NoEventMask, (XEvent *)&xconfigure);
  gdk_error_trap_pop_ignored ();
}

603 604 605 606
static void
gtk_socket_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
607
  GtkSocket *socket = GTK_SOCKET (widget);
608
  GtkSocketPrivate *private = socket->priv;
609

610
  gtk_widget_set_allocation (widget, allocation);
611
  if (gtk_widget_get_realized (widget))
612
    {
613
      gdk_window_move_resize (gtk_widget_get_window (widget),
614 615 616
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);

617
      if (private->plug_widget)
618 619 620 621 622 623 624 625
	{
	  GtkAllocation child_allocation;

	  child_allocation.x = 0;
	  child_allocation.y = 0;
	  child_allocation.width = allocation->width;
	  child_allocation.height = allocation->height;

626
	  gtk_widget_size_allocate (private->plug_widget, &child_allocation);
627
	}
628
      else if (private->plug_window)
629
	{
630
	  gdk_error_trap_push ();
631 632 633

	  if (allocation->width != private->current_width ||
	      allocation->height != private->current_height)
634
	    {
635
	      gdk_window_move_resize (private->plug_window,
636 637
				      0, 0,
				      allocation->width, allocation->height);
638 639 640
	      if (private->resize_count)
		private->resize_count--;
	      
641 642 643
	      GTK_NOTE (PLUGSOCKET,
			g_message ("GtkSocket - allocated: %d %d",
				   allocation->width, allocation->height));
644 645
	      private->current_width = allocation->width;
	      private->current_height = allocation->height;
646 647
	    }

648
	  if (private->need_map)
649
	    {
650 651
	      gdk_window_show (private->plug_window);
	      private->need_map = FALSE;
652 653
	    }

654 655
	  while (private->resize_count)
 	    {
656
 	      gtk_socket_send_configure_event (socket);
657
 	      private->resize_count--;
658 659 660
 	      GTK_NOTE (PLUGSOCKET,
			g_message ("GtkSocket - sending synthetic configure: %d %d",
				   allocation->width, allocation->height));
661
 	    }
Matthias Clasen's avatar
Matthias Clasen committed
662

663
	  gdk_error_trap_pop_ignored ();
664 665 666 667
	}
    }
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
static void
gtk_socket_send_key_event (GtkSocket *socket,
			   GdkEvent  *gdk_event,
			   gboolean   mask_key_presses)
{
  XKeyEvent xkey;
  GdkScreen *screen = gdk_window_get_screen (socket->priv->plug_window);

  memset (&xkey, 0, sizeof (xkey));
  xkey.type = (gdk_event->type == GDK_KEY_PRESS) ? KeyPress : KeyRelease;
  xkey.window = GDK_WINDOW_XID (socket->priv->plug_window);
  xkey.root = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
  xkey.subwindow = None;
  xkey.time = gdk_event->key.time;
  xkey.x = 0;
  xkey.y = 0;
  xkey.x_root = 0;
  xkey.y_root = 0;
  xkey.state = gdk_event->key.state;
  xkey.keycode = gdk_event->key.hardware_keycode;
  xkey.same_screen = True;/* FIXME ? */

  gdk_error_trap_push ();
  XSendEvent (GDK_WINDOW_XDISPLAY (socket->priv->plug_window),
	      GDK_WINDOW_XID (socket->priv->plug_window),
	      False,
	      (mask_key_presses ? KeyPressMask : NoEventMask),
	      (XEvent *)&xkey);
  gdk_error_trap_pop_ignored ();
}

699 700 701 702 703 704
static gboolean
activate_key (GtkAccelGroup  *accel_group,
	      GObject        *acceleratable,
	      guint           accel_key,
	      GdkModifierType accel_mods,
	      GrabbedKey     *grabbed_key)
705
{
706
  GdkEvent *gdk_event = gtk_get_current_event ();
707
  
708
  GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
709
  gboolean retval = FALSE;
710

711
  if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->priv->plug_window)
712
    {
713
      gtk_socket_send_key_event (socket, gdk_event, FALSE);
714
      retval = TRUE;
715 716 717 718
    }

  if (gdk_event)
    gdk_event_free (gdk_event);
719 720

  return retval;
721 722 723
}

static gboolean
724 725 726
find_accel_key (GtkAccelKey *key,
		GClosure    *closure,
		gpointer     data)
727
{
728 729 730 731
  GrabbedKey *grabbed_key = data;
  
  return (key->accel_key == grabbed_key->accel_key &&
	  key->accel_mods == grabbed_key->accel_mods);
732 733
}

734
/**
735
 * gtk_socket_add_grabbed_key:
736 737 738 739 740 741 742
 * @socket: a #GtkSocket
 * @keyval: a key
 * @modifiers: modifiers for the key
 *
 * Called from the GtkSocket platform-specific backend when the
 * corresponding plug has told the socket to grab a key.
 */
743 744 745 746
static void
gtk_socket_add_grabbed_key (GtkSocket       *socket,
			    guint            keyval,
			    GdkModifierType  modifiers)
747
{
748 749
  GClosure *closure;
  GrabbedKey *grabbed_key;
750

751 752 753 754
  grabbed_key = g_new (GrabbedKey, 1);
  
  grabbed_key->accel_key = keyval;
  grabbed_key->accel_mods = modifiers;
755

756
  if (gtk_accel_group_find (socket->priv->accel_group,
757 758 759 760 761 762 763
			    find_accel_key,
			    &grabbed_key))
    {
      g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
		 keyval, modifiers);
      g_free (grabbed_key);
      return;
764 765
    }

766
  closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
767

768
  gtk_accel_group_connect (socket->priv->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
769
			   closure);
770 771
}

772
/**
773
 * gtk_socket_remove_grabbed_key:
774 775 776 777 778 779 780
 * @socket: a #GtkSocket
 * @keyval: a key
 * @modifiers: modifiers for the key
 *
 * Called from the GtkSocket backend when the corresponding plug has
 * told the socket to remove a key grab.
 */
781 782 783 784
static void
gtk_socket_remove_grabbed_key (GtkSocket      *socket,
			       guint           keyval,
			       GdkModifierType modifiers)
785
{
786
  if (!gtk_accel_group_disconnect_key (socket->priv->accel_group, keyval, modifiers))
787 788
    g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
	       keyval, modifiers);
789 790
}

791 792
static void
socket_update_focus_in (GtkSocket *socket)
793
{
794
  GtkSocketPrivate *private = socket->priv;
795 796
  gboolean focus_in = FALSE;

797
  if (private->plug_window)
798 799
    {
      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
800

801
      if (gtk_widget_is_toplevel (toplevel) &&
802
	  gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)) &&
803 804 805 806
	  gtk_widget_is_focus (GTK_WIDGET (socket)))
	focus_in = TRUE;
    }

807
  if (focus_in != private->focus_in)
808
    {
809
      private->focus_in = focus_in;
810

811 812 813 814 815 816
      if (focus_in)
        _gtk_xembed_send_focus_message (private->plug_window,
                                        XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT);
      else
        _gtk_xembed_send_message (private->plug_window,
                                  XEMBED_FOCUS_OUT, 0, 0, 0);
817
    }
818 819
}

820 821
static void
socket_update_active (GtkSocket *socket)
822
{
823
  GtkSocketPrivate *private = socket->priv;
824
  gboolean active = FALSE;
825

826
  if (private->plug_window)
827 828
    {
      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
829

830
      if (gtk_widget_is_toplevel (toplevel) &&
831
	  gtk_window_is_active  (GTK_WINDOW (toplevel)))
832 833 834
	active = TRUE;
    }

835
  if (active != private->active)
836
    {
837
      private->active = active;
838

839 840 841
      _gtk_xembed_send_message (private->plug_window,
                                active ? XEMBED_WINDOW_ACTIVATE : XEMBED_WINDOW_DEACTIVATE,
                                0, 0, 0);
842
    }
843 844 845
}

static void
846 847
gtk_socket_hierarchy_changed (GtkWidget *widget,
			      GtkWidget *old_toplevel)
848 849
{
  GtkSocket *socket = GTK_SOCKET (widget);
850
  GtkSocketPrivate *private = socket->priv;
851 852 853 854 855
  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);

  if (toplevel && !GTK_IS_WINDOW (toplevel))
    toplevel = NULL;

856
  if (toplevel != private->toplevel)
857
    {
858
      if (private->toplevel)
859
	{
860 861
	  gtk_window_remove_accel_group (GTK_WINDOW (private->toplevel), private->accel_group);
	  g_signal_handlers_disconnect_by_func (private->toplevel,
Manish Singh's avatar
Manish Singh committed
862 863
						socket_update_focus_in,
						socket);
864
	  g_signal_handlers_disconnect_by_func (private->toplevel,
Manish Singh's avatar
Manish Singh committed
865 866
						socket_update_active,
						socket);
867 868
	}

869
      private->toplevel = toplevel;
870 871 872

      if (toplevel)
	{
873 874
	  gtk_window_add_accel_group (GTK_WINDOW (private->toplevel), private->accel_group);
	  g_signal_connect_swapped (private->toplevel, "notify::has-toplevel-focus",
875
				    G_CALLBACK (socket_update_focus_in), socket);
876
	  g_signal_connect_swapped (private->toplevel, "notify::is-active",
877
				    G_CALLBACK (socket_update_active), socket);
878
	}
879 880 881

      socket_update_focus_in (socket);
      socket_update_active (socket);
882 883 884 885 886 887 888
    }
}

static void
gtk_socket_grab_notify (GtkWidget *widget,
			gboolean   was_grabbed)
{
889 890
  GtkSocket *socket = GTK_SOCKET (widget);

891
  if (!socket->priv->same_app)
892 893 894
    _gtk_xembed_send_message (socket->priv->plug_window,
                              was_grabbed ? XEMBED_MODALITY_OFF : XEMBED_MODALITY_ON,
                              0, 0, 0);
895 896 897
}

static gboolean
898 899
gtk_socket_key_event (GtkWidget   *widget,
                      GdkEventKey *event)
900 901
{
  GtkSocket *socket = GTK_SOCKET (widget);
902
  GtkSocketPrivate *private = socket->priv;
903
  
904
  if (gtk_widget_has_focus (widget) && private->plug_window && !private->plug_widget)
905
    {
906
      gtk_socket_send_key_event (socket, (GdkEvent *) event, FALSE);
907

908 909 910 911 912 913
      return TRUE;
    }
  else
    return FALSE;
}

914 915 916
static void
gtk_socket_notify (GObject    *object,
		   GParamSpec *pspec)
917
{
918 919
  if (strcmp (pspec->name, "is-focus") == 0)
    socket_update_focus_in (GTK_SOCKET (object));
920 921 922

  if (G_OBJECT_CLASS (gtk_socket_parent_class)->notify)
    G_OBJECT_CLASS (gtk_socket_parent_class)->notify (object, pspec);
923 924
}

925
/**
926
 * gtk_socket_claim_focus:
927 928 929 930 931
 * @socket: a #GtkSocket
 * @send_event: huh?
 *
 * Claims focus for the socket. XXX send_event?
 */
932 933 934
static void
gtk_socket_claim_focus (GtkSocket *socket,
			gboolean   send_event)
935
{
936
  GtkWidget *widget = GTK_WIDGET (socket);
937
  GtkSocketPrivate *private = socket->priv;
938

939
  if (!send_event)
940
    private->focus_in = TRUE;	/* Otherwise, our notify handler will send FOCUS_IN  */
941 942 943
      
  /* Oh, the trickery... */
  
944 945 946
  gtk_widget_set_can_focus (widget, TRUE);
  gtk_widget_grab_focus (widget);
  gtk_widget_set_can_focus (widget, FALSE);
947 948
}

949
static gboolean
950 951
gtk_socket_focus (GtkWidget       *widget,
		  GtkDirectionType direction)
952
{
953
  GtkSocket *socket = GTK_SOCKET (widget);
954
  GtkSocketPrivate *private = socket->priv;
955

956 957
  if (private->plug_widget)
    return gtk_widget_child_focus (private->plug_widget, direction);
958

959
  if (!gtk_widget_is_focus (widget))
960
    {
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
      gint detail = -1;

      switch (direction)
        {
        case GTK_DIR_UP:
        case GTK_DIR_LEFT:
        case GTK_DIR_TAB_BACKWARD:
          detail = XEMBED_FOCUS_LAST;
          break;
        case GTK_DIR_DOWN:
        case GTK_DIR_RIGHT:
        case GTK_DIR_TAB_FORWARD:
          detail = XEMBED_FOCUS_FIRST;
          break;
        }
      
      _gtk_xembed_send_focus_message (private->plug_window, XEMBED_FOCUS_IN, detail);
      gtk_socket_claim_focus (socket, FALSE);
979 980 981 982 983
 
      return TRUE;
    }
  else
    return FALSE;
984 985
}

986 987 988 989 990
static void
gtk_socket_remove (GtkContainer *container,
		   GtkWidget    *child)
{
  GtkSocket *socket = GTK_SOCKET (container);
991
  GtkSocketPrivate *private = socket->priv;
992

993
  g_return_if_fail (child == private->plug_widget);
994

995
  _gtk_plug_remove_from_socket (GTK_PLUG (private->plug_widget), socket);
996 997 998 999 1000 1001 1002 1003 1004
}

static void
gtk_socket_forall (GtkContainer *container,
		   gboolean      include_internals,
		   GtkCallback   callback,
		   gpointer      callback_data)
{
  GtkSocket *socket = GTK_SOCKET (container);
1005
  GtkSocketPrivate *private = socket->priv;
1006

1007 1008
  if (private->plug_widget)
    (* callback) (private->plug_widget, callback_data);
1009 1010
}

1011
/**
1012
 * gtk_socket_add_window:
1013 1014
 * @socket: a #GtkSocket
 * @xid: the native identifier for a window
1015
 * @need_reparent: whether the socket’s plug’s window needs to be
Matthias Clasen's avatar
Matthias Clasen committed
1016
 *                 reparented to the socket
1017 1018 1019
 *
 * Adds a window to a GtkSocket.
 */
1020 1021
static void
gtk_socket_add_window (GtkSocket       *socket,
1022
		       Window           xid,
1023
		       gboolean         need_reparent)
1024
{
1025
  GtkWidget *widget = GTK_WIDGET (socket);
1026
  GdkDisplay *display = gtk_widget_get_display (widget);
1027
  gpointer user_data = NULL;
1028
  GtkSocketPrivate *private = socket->priv;
1029 1030
  unsigned long version;
  unsigned long flags;
1031

1032
  if (GDK_IS_X11_DISPLAY (display))
1033 1034 1035
    private->plug_window = gdk_x11_window_lookup_for_display (display, xid);
  else
    private->plug_window = NULL;
1036

1037
  if (private->plug_window)
1038
    {
1039 1040
      g_object_ref (private->plug_window);
      gdk_window_get_user_data (private->plug_window, &user_data);
1041 1042
    }

1043
  if (user_data) /* A widget's window in this process */
1044 1045 1046 1047
    {
      GtkWidget *child_widget = user_data;

      if (!GTK_IS_PLUG (child_widget))
1048 1049 1050 1051 1052 1053 1054
        {
          g_warning (G_STRLOC ": Can't add non-GtkPlug to GtkSocket");
          private->plug_window = NULL;
          gdk_error_trap_pop_ignored ();

          return;
        }
1055 1056 1057

      _gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
    }
1058
  else  /* A foreign window */
1059 1060
    {
      GdkDragProtocol protocol;
1061 1062

      gdk_error_trap_push ();
1063