1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 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 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
|
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
*/
/* By Owen Taylor <otaylor@gtk.org> 98/4/4 */
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* 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/.
*/
#include "config.h"
#include "gtksocketprivate.h"
#include <string.h>
#include "gtkmarshalers.h"
#include "gtksizerequest.h"
#include "gtkplug.h"
#include "gtkprivate.h"
#include "gtkrender.h"
#include "gtkdnd.h"
#include "gtkdragdest.h"
#include "gtkdebug.h"
#include "gtkintl.h"
#include "gtkmain.h"
#include "gtkwidgetprivate.h"
#include <gdk/gdkx.h>
#include <gdk/gdkprivate.h>
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif
#include "gtkxembed.h"
/**
* SECTION:gtksocket
* @Short_description: Container for widgets from other processes
* @Title: GtkSocket
* @include: gtk/gtkx.h
* @See_also: #GtkPlug, [XEmbed Protocol](http://www.freedesktop.org/Standards/xembed-spec)
*
* 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
* and passes that widget’s window ID 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.
*
* The socket’s window ID is obtained by using gtk_socket_get_id().
* Before using this function, the socket must have been realized,
* and for hence, have been added to its parent.
*
* ## Obtaining the window ID of a socket.
*
* |[<!-- language="C" -->
* GtkWidget *socket = gtk_socket_new ();
* gtk_widget_show (socket);
* gtk_container_add (GTK_CONTAINER (parent), socket);
*
* // The following call is only necessary if one of
* // the ancestors of the socket is not yet visible.
* gtk_widget_realize (socket);
* g_print ("The ID of the sockets window is %#x\n",
* gtk_socket_get_id (socket));
* ]|
*
* Note that if you pass the window ID of the socket to another
* 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
* successfully created inside of the socket.
*
* 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.
*
* The communication between a #GtkSocket and a #GtkPlug follows the
* [XEmbed Protocol](http://www.freedesktop.org/Standards/xembed-spec).
* This protocol has also been implemented in other toolkits, e.g. Qt,
* allowing the same level of integration when embedding a Qt widget
* in GTK or vice versa.
*
* 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
* #GtkSocket, you need to include the `gtk/gtkx.h` header.
*/
/* Forward declararations */
static void gtk_socket_finalize (GObject *object);
static void gtk_socket_notify (GObject *object,
GParamSpec *pspec);
static void gtk_socket_realize (GtkWidget *widget);
static void gtk_socket_unrealize (GtkWidget *widget);
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);
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);
static gboolean gtk_socket_key_event (GtkWidget *widget,
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);
static void gtk_socket_add_window (GtkSocket *socket,
Window xid,
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
/* Local data */
typedef struct
{
guint accel_key;
GdkModifierType accel_mods;
} GrabbedKey;
enum {
PLUG_ADDED,
PLUG_REMOVED,
LAST_SIGNAL
};
static guint socket_signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GtkSocket, gtk_socket, GTK_TYPE_CONTAINER)
static void
gtk_socket_finalize (GObject *object)
{
GtkSocket *socket = GTK_SOCKET (object);
GtkSocketPrivate *priv = socket->priv;
g_object_unref (priv->accel_group);
G_OBJECT_CLASS (gtk_socket_parent_class)->finalize (object);
}
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);
}
static void
gtk_socket_class_init (GtkSocketClass *class)
{
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
GObjectClass *gobject_class;
gobject_class = (GObjectClass *) class;
widget_class = (GtkWidgetClass*) class;
container_class = (GtkContainerClass*) class;
gobject_class->finalize = gtk_socket_finalize;
gobject_class->notify = gtk_socket_notify;
widget_class->realize = gtk_socket_realize;
widget_class->unrealize = gtk_socket_unrealize;
widget_class->get_preferred_width = gtk_socket_get_preferred_width;
widget_class->get_preferred_height = gtk_socket_get_preferred_height;
widget_class->size_allocate = gtk_socket_size_allocate;
widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
widget_class->grab_notify = gtk_socket_grab_notify;
widget_class->key_press_event = gtk_socket_key_event;
widget_class->key_release_event = gtk_socket_key_event;
widget_class->focus = gtk_socket_focus;
widget_class->draw = gtk_socket_draw;
/* We don't want to show_all the in-process plug, if any.
*/
widget_class->show_all = gtk_widget_show;
container_class->remove = gtk_socket_remove;
container_class->forall = gtk_socket_forall;
/**
* GtkSocket::plug-added:
* @socket_: the object which received the signal
*
* This signal is emitted when a client is successfully
* added to the socket.
*/
socket_signals[PLUG_ADDED] =
g_signal_new (I_("plug-added"),
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkSocketClass, plug_added),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
/**
* 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.
*
* Returns: %TRUE to stop other handlers from being invoked.
*/
socket_signals[PLUG_REMOVED] =
g_signal_new (I_("plug-removed"),
G_OBJECT_CLASS_TYPE (class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkSocketClass, plug_removed),
_gtk_boolean_handled_accumulator, NULL,
_gtk_marshal_BOOLEAN__VOID,
G_TYPE_BOOLEAN, 0);
}
static void
gtk_socket_init (GtkSocket *socket)
{
GtkSocketPrivate *priv;
priv = gtk_socket_get_instance_private (socket);
socket->priv = priv;
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);
}
/**
* gtk_socket_new:
*
* Create a new empty #GtkSocket.
*
* Returns: the new #GtkSocket.
**/
GtkWidget*
gtk_socket_new (void)
{
GtkSocket *socket;
socket = g_object_new (GTK_TYPE_SOCKET, NULL);
return GTK_WIDGET (socket);
}
/**
* gtk_socket_add_id:
* @socket_: a #GtkSocket
* @window: the Window of a client participating in the XEMBED protocol.
*
* 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
* #GtkPlug with `gtk_plug_new (0)`, call
* gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
* 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
gtk_socket_add_id (GtkSocket *socket,
Window window)
{
g_return_if_fail (GTK_IS_SOCKET (socket));
g_return_if_fail (_gtk_widget_get_anchored (GTK_WIDGET (socket)));
if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
gtk_widget_realize (GTK_WIDGET (socket));
gtk_socket_add_window (socket, window, TRUE);
}
/**
* gtk_socket_get_id:
* @socket_: a #GtkSocket.
*
* Gets the window ID of a #GtkSocket widget, which can then
* be used to create a client embedded inside the socket, for
* instance with gtk_plug_new().
*
* The #GtkSocket must have already be added into a toplevel window
* before you can make this call.
*
* Returns: the window ID for the socket
**/
Window
gtk_socket_get_id (GtkSocket *socket)
{
g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
g_return_val_if_fail (_gtk_widget_get_anchored (GTK_WIDGET (socket)), 0);
if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
gtk_widget_realize (GTK_WIDGET (socket));
return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (socket)));
}
/**
* 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.
*
* Returns: (nullable) (transfer none): the window of the plug if
* available, or %NULL
*
* Since: 2.14
**/
GdkWindow*
gtk_socket_get_plug_window (GtkSocket *socket)
{
g_return_val_if_fail (GTK_IS_SOCKET (socket), NULL);
return socket->priv->plug_window;
}
static void
gtk_socket_realize (GtkWidget *widget)
{
GtkAllocation allocation;
GdkWindow *window;
GdkWindowAttr attributes;
XWindowAttributes xattrs;
gint attributes_mask;
GdkScreen *screen;
gtk_widget_set_realized (widget, TRUE);
screen = gtk_widget_get_screen (widget);
if (!GDK_IS_X11_SCREEN (screen))
g_warning ("GtkSocket: only works under X11");
gtk_widget_get_allocation (widget, &allocation);
attributes.window_type = GDK_WINDOW_CHILD;
attributes.x = allocation.x;
attributes.y = allocation.y;
attributes.width = allocation.width;
attributes.height = allocation.height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gtk_widget_get_visual (widget);
attributes.event_mask = GDK_FOCUS_CHANGE_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask);
gtk_widget_set_window (widget, window);
gtk_widget_register_window (widget, window);
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);
gdk_window_add_filter (window,
gtk_socket_filter_func,
widget);
/* 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.
*/
gdk_display_sync (gtk_widget_get_display (widget));
}
/**
* gtk_socket_end_embedding:
* @socket: a #GtkSocket
*
* Called to end the embedding of a plug in the socket.
*/
static void
gtk_socket_end_embedding (GtkSocket *socket)
{
GtkSocketPrivate *private = socket->priv;
g_object_unref (private->plug_window);
private->plug_window = NULL;
private->current_width = 0;
private->current_height = 0;
private->resize_count = 0;
gtk_accel_group_disconnect (private->accel_group, NULL);
}
static void
gtk_socket_unrealize (GtkWidget *widget)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
gtk_widget_set_realized (widget, FALSE);
if (private->plug_widget)
{
_gtk_plug_remove_from_socket (GTK_PLUG (private->plug_widget), socket);
}
else if (private->plug_window)
{
gtk_socket_end_embedding (socket);
}
GTK_WIDGET_CLASS (gtk_socket_parent_class)->unrealize (widget);
}
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 ();
}
static void
gtk_socket_get_preferred_width (GtkWidget *widget,
gint *minimum,
gint *natural)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
if (private->plug_widget)
{
gtk_widget_get_preferred_width (private->plug_widget, minimum, natural);
}
else
{
if (private->is_mapped && !private->have_size && private->plug_window)
gtk_socket_size_request (socket);
if (private->is_mapped && private->have_size)
*minimum = *natural = MAX (private->request_width, 1);
else
*minimum = *natural = 1;
}
}
static void
gtk_socket_get_preferred_height (GtkWidget *widget,
gint *minimum,
gint *natural)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
if (private->plug_widget)
{
gtk_widget_get_preferred_height (private->plug_widget, minimum, natural);
}
else
{
if (private->is_mapped && !private->have_size && private->plug_window)
gtk_socket_size_request (socket);
if (private->is_mapped && private->have_size)
*minimum = *natural = MAX (private->request_height, 1);
else
*minimum = *natural = 1;
}
}
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 ();
}
static void
gtk_socket_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
gtk_widget_set_allocation (widget, allocation);
if (gtk_widget_get_realized (widget))
{
gdk_window_move_resize (gtk_widget_get_window (widget),
allocation->x, allocation->y,
allocation->width, allocation->height);
if (private->plug_widget)
{
GtkAllocation child_allocation;
child_allocation.x = 0;
child_allocation.y = 0;
child_allocation.width = allocation->width;
child_allocation.height = allocation->height;
gtk_widget_size_allocate (private->plug_widget, &child_allocation);
}
else if (private->plug_window)
{
gdk_error_trap_push ();
if (allocation->width != private->current_width ||
allocation->height != private->current_height)
{
gdk_window_move_resize (private->plug_window,
0, 0,
allocation->width, allocation->height);
if (private->resize_count)
private->resize_count--;
GTK_NOTE (PLUGSOCKET,
g_message ("GtkSocket - allocated: %d %d",
allocation->width, allocation->height));
private->current_width = allocation->width;
private->current_height = allocation->height;
}
if (private->need_map)
{
gdk_window_show (private->plug_window);
private->need_map = FALSE;
}
while (private->resize_count)
{
gtk_socket_send_configure_event (socket);
private->resize_count--;
GTK_NOTE (PLUGSOCKET,
g_message ("GtkSocket - sending synthetic configure: %d %d",
allocation->width, allocation->height));
}
gdk_error_trap_pop_ignored ();
}
}
}
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 ();
}
static gboolean
activate_key (GtkAccelGroup *accel_group,
GObject *acceleratable,
guint accel_key,
GdkModifierType accel_mods,
GrabbedKey *grabbed_key)
{
GdkEvent *gdk_event = gtk_get_current_event ();
GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
gboolean retval = FALSE;
if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->priv->plug_window)
{
gtk_socket_send_key_event (socket, gdk_event, FALSE);
retval = TRUE;
}
if (gdk_event)
gdk_event_free (gdk_event);
return retval;
}
static gboolean
find_accel_key (GtkAccelKey *key,
GClosure *closure,
gpointer data)
{
GrabbedKey *grabbed_key = data;
return (key->accel_key == grabbed_key->accel_key &&
key->accel_mods == grabbed_key->accel_mods);
}
/**
* gtk_socket_add_grabbed_key:
* @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.
*/
static void
gtk_socket_add_grabbed_key (GtkSocket *socket,
guint keyval,
GdkModifierType modifiers)
{
GClosure *closure;
GrabbedKey *grabbed_key;
grabbed_key = g_new (GrabbedKey, 1);
grabbed_key->accel_key = keyval;
grabbed_key->accel_mods = modifiers;
if (gtk_accel_group_find (socket->priv->accel_group,
find_accel_key,
&grabbed_key))
{
g_warning ("GtkSocket: request to add already present grabbed key %u,%#x",
keyval, modifiers);
g_free (grabbed_key);
return;
}
closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
gtk_accel_group_connect (socket->priv->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
closure);
}
/**
* gtk_socket_remove_grabbed_key:
* @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.
*/
static void
gtk_socket_remove_grabbed_key (GtkSocket *socket,
guint keyval,
GdkModifierType modifiers)
{
if (!gtk_accel_group_disconnect_key (socket->priv->accel_group, keyval, modifiers))
g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x",
keyval, modifiers);
}
static void
socket_update_focus_in (GtkSocket *socket)
{
GtkSocketPrivate *private = socket->priv;
gboolean focus_in = FALSE;
if (private->plug_window)
{
GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
if (gtk_widget_is_toplevel (toplevel) &&
gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)) &&
gtk_widget_is_focus (GTK_WIDGET (socket)))
focus_in = TRUE;
}
if (focus_in != private->focus_in)
{
private->focus_in = focus_in;
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);
}
}
static void
socket_update_active (GtkSocket *socket)
{
GtkSocketPrivate *private = socket->priv;
gboolean active = FALSE;
if (private->plug_window)
{
GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
if (gtk_widget_is_toplevel (toplevel) &&
gtk_window_is_active (GTK_WINDOW (toplevel)))
active = TRUE;
}
if (active != private->active)
{
private->active = active;
_gtk_xembed_send_message (private->plug_window,
active ? XEMBED_WINDOW_ACTIVATE : XEMBED_WINDOW_DEACTIVATE,
0, 0, 0);
}
}
static void
gtk_socket_hierarchy_changed (GtkWidget *widget,
GtkWidget *old_toplevel)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
if (toplevel && !GTK_IS_WINDOW (toplevel))
toplevel = NULL;
if (toplevel != private->toplevel)
{
if (private->toplevel)
{
gtk_window_remove_accel_group (GTK_WINDOW (private->toplevel), private->accel_group);
g_signal_handlers_disconnect_by_func (private->toplevel,
socket_update_focus_in,
socket);
g_signal_handlers_disconnect_by_func (private->toplevel,
socket_update_active,
socket);
}
private->toplevel = toplevel;
if (toplevel)
{
gtk_window_add_accel_group (GTK_WINDOW (private->toplevel), private->accel_group);
g_signal_connect_swapped (private->toplevel, "notify::has-toplevel-focus",
G_CALLBACK (socket_update_focus_in), socket);
g_signal_connect_swapped (private->toplevel, "notify::is-active",
G_CALLBACK (socket_update_active), socket);
}
socket_update_focus_in (socket);
socket_update_active (socket);
}
}
static void
gtk_socket_grab_notify (GtkWidget *widget,
gboolean was_grabbed)
{
GtkSocket *socket = GTK_SOCKET (widget);
if (!socket->priv->same_app)
_gtk_xembed_send_message (socket->priv->plug_window,
was_grabbed ? XEMBED_MODALITY_OFF : XEMBED_MODALITY_ON,
0, 0, 0);
}
static gboolean
gtk_socket_key_event (GtkWidget *widget,
GdkEventKey *event)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
if (gtk_widget_has_focus (widget) && private->plug_window && !private->plug_widget)
{
gtk_socket_send_key_event (socket, (GdkEvent *) event, FALSE);
return TRUE;
}
else
return FALSE;
}
static void
gtk_socket_notify (GObject *object,
GParamSpec *pspec)
{
if (strcmp (pspec->name, "is-focus") == 0)
socket_update_focus_in (GTK_SOCKET (object));
if (G_OBJECT_CLASS (gtk_socket_parent_class)->notify)
G_OBJECT_CLASS (gtk_socket_parent_class)->notify (object, pspec);
}
/**
* gtk_socket_claim_focus:
* @socket: a #GtkSocket
* @send_event: huh?
*
* Claims focus for the socket. XXX send_event?
*/
static void
gtk_socket_claim_focus (GtkSocket *socket,
gboolean send_event)
{
GtkWidget *widget = GTK_WIDGET (socket);
GtkSocketPrivate *private = socket->priv;
if (!send_event)
private->focus_in = TRUE; /* Otherwise, our notify handler will send FOCUS_IN */
/* Oh, the trickery... */
gtk_widget_set_can_focus (widget, TRUE);
gtk_widget_grab_focus (widget);
gtk_widget_set_can_focus (widget, FALSE);
}
static gboolean
gtk_socket_focus (GtkWidget *widget,
GtkDirectionType direction)
{
GtkSocket *socket = GTK_SOCKET (widget);
GtkSocketPrivate *private = socket->priv;
if (private->plug_widget)
return gtk_widget_child_focus (private->plug_widget, direction);
if (!gtk_widget_is_focus (widget))
{
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);
return TRUE;
}
else
return FALSE;
}
static void
gtk_socket_remove (GtkContainer *container,
GtkWidget *child)
{
GtkSocket *socket = GTK_SOCKET (container);
GtkSocketPrivate *private = socket->priv;
g_return_if_fail (child == private->plug_widget);
_gtk_plug_remove_from_socket (GTK_PLUG (private->plug_widget), socket);
}
static void
gtk_socket_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
GtkSocket *socket = GTK_SOCKET (container);
GtkSocketPrivate *private = socket->priv;
if (private->plug_widget)
(* callback) (private->plug_widget, callback_data);
}
/**
* gtk_socket_add_window:
* @socket: a #GtkSocket
* @xid: the native identifier for a window
* @need_reparent: whether the socket’s plug’s window needs to be
* reparented to the socket
*
* Adds a window to a GtkSocket.
*/
static void
gtk_socket_add_window (GtkSocket *socket,
Window xid,
gboolean need_reparent)
{
GtkWidget *widget = GTK_WIDGET (socket);
GdkDisplay *display = gtk_widget_get_display (widget);
gpointer user_data = NULL;
GtkSocketPrivate *private = socket->priv;
unsigned long version;
unsigned long flags;
if (GDK_IS_X11_DISPLAY (display))
private->plug_window = gdk_x11_window_lookup_for_display (display, xid);
else
private->plug_window = NULL;
if (private->plug_window)
{
g_object_ref (private->plug_window);
gdk_window_get_user_data (private->plug_window, &user_data);
}
if (user_data) /* A widget's window in this process */
{
GtkWidget *child_widget = user_data;
if (!GTK_IS_PLUG (child_widget))
{
g_warning (G_STRLOC ": Can't add non-GtkPlug to GtkSocket");
private->plug_window = NULL;
gdk_error_trap_pop_ignored ();
return;
}
_gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
}
else /* A foreign window */
{
GdkDragProtocol protocol;
gdk_error_trap_push ();
if (!private->plug_window)
{
if (GDK_IS_X11_DISPLAY (display))
private->plug_window = gdk_x11_window_foreign_new_for_display (display, xid);
if (!private->plug_window) /* was deleted before we could get it */
{
gdk_error_trap_pop_ignored ();
return;
}
}
XSelectInput (GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (socket))),
GDK_WINDOW_XID (private->plug_window),
StructureNotifyMask | PropertyChangeMask);
if (gdk_error_trap_pop ())
{
g_object_unref (private->plug_window);
private->plug_window = NULL;
return;
}
/* OK, we now will reliably get destroy notification on socket->plug_window */
gdk_error_trap_push ();
if (need_reparent)
{
gdk_window_hide (private->plug_window); /* Shouldn't actually be necessary for XEMBED, but just in case */
gdk_window_reparent (private->plug_window,
gtk_widget_get_window (widget),
0, 0);
}
private->have_size = FALSE;
private->xembed_version = -1;
if (xembed_get_info (private->plug_window, &version, &flags))
{
private->xembed_version = MIN (GTK_XEMBED_PROTOCOL_VERSION, version);
private->is_mapped = (flags & XEMBED_MAPPED) != 0;
}
else
{
/* FIXME, we should probably actually check the state before we started */
private->is_mapped = TRUE;
}
private->need_map = private->is_mapped;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
protocol = gdk_window_get_drag_protocol (private->plug_window, NULL);
if (protocol)
gtk_drag_dest_set_proxy (GTK_WIDGET (socket), private->plug_window,
protocol, TRUE);
G_GNUC_END_IGNORE_DEPRECATIONS
gdk_error_trap_pop_ignored ();
gdk_window_add_filter (private->plug_window,
gtk_socket_filter_func,
socket);
#ifdef HAVE_XFIXES
gdk_error_trap_push ();
XFixesChangeSaveSet (GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (socket))),
GDK_WINDOW_XID (private->plug_window),
SetModeInsert, SaveSetRoot, SaveSetUnmap);
gdk_error_trap_pop_ignored ();
#endif
_gtk_xembed_send_message (private->plug_window,
XEMBED_EMBEDDED_NOTIFY, 0,
GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (socket))),
private->xembed_version);
socket_update_active (socket);
socket_update_focus_in (socket);
gtk_widget_queue_resize (GTK_WIDGET (socket));
}
if (private->plug_window)
g_signal_emit (socket, socket_signals[PLUG_ADDED], 0);
}
/**
* gtk_socket_handle_map_request:
* @socket: a #GtkSocket
*
* Called from the GtkSocket backend when the plug has been mapped.
*/
static void
gtk_socket_handle_map_request (GtkSocket *socket)
{
GtkSocketPrivate *private = socket->priv;
if (!private->is_mapped)
{
private->is_mapped = TRUE;
private->need_map = TRUE;
gtk_widget_queue_resize (GTK_WIDGET (socket));
}
}
/**
* gtk_socket_unmap_notify:
* @socket: a #GtkSocket
*
* Called from the GtkSocket backend when the plug has been unmapped ???
*/
static void
gtk_socket_unmap_notify (GtkSocket *socket)
{
GtkSocketPrivate *private = socket->priv;
if (private->is_mapped)
{
private->is_mapped = FALSE;
gtk_widget_queue_resize (GTK_WIDGET (socket));
}
}
/**
* gtk_socket_advance_toplevel_focus:
* @socket: a #GtkSocket
* @direction: a direction
*
* Called from the GtkSocket backend when the corresponding plug
* has told the socket to move the focus.
*/
static void
gtk_socket_advance_toplevel_focus (GtkSocket *socket,
GtkDirectionType direction)
{
GtkBin *bin;
GtkWindow *window;
GtkContainer *container;
GtkWidget *child;
GtkWidget *focus_widget;
GtkWidget *toplevel;
GtkWidget *old_focus_child;
GtkWidget *parent;
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
if (!toplevel)
return;
if (!gtk_widget_is_toplevel (toplevel) || GTK_IS_PLUG (toplevel))
{
gtk_widget_child_focus (toplevel,direction);
return;
}
container = GTK_CONTAINER (toplevel);
window = GTK_WINDOW (toplevel);
bin = GTK_BIN (toplevel);
/* This is a copy of gtk_window_focus(), modified so that we
* can detect wrap-around.
*/
old_focus_child = gtk_container_get_focus_child (container);
if (old_focus_child)
{
if (gtk_widget_child_focus (old_focus_child, direction))
return;
/* We are allowed exactly one wrap-around per sequence of focus
* events
*/
if (_gtk_xembed_get_focus_wrapped ())
return;
else
_gtk_xembed_set_focus_wrapped ();
}
focus_widget = gtk_window_get_focus (window);
if (window)
{
/* Wrapped off the end, clear the focus setting for the toplevel */
parent = gtk_widget_get_parent (focus_widget);
while (parent)
{
gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
parent = gtk_widget_get_parent (parent);
}
gtk_window_set_focus (GTK_WINDOW (container), NULL);
}
/* Now try to focus the first widget in the window */
child = gtk_bin_get_child (bin);
if (child)
{
if (gtk_widget_child_focus (child, direction))
return;
}
}
static gboolean
xembed_get_info (GdkWindow *window,
unsigned long *version,
unsigned long *flags)
{
GdkDisplay *display = gdk_window_get_display (window);
Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
Atom type;
int format;
unsigned long nitems, bytes_after;
unsigned char *data;
unsigned long *data_long;
int status;
gdk_error_trap_push ();
status = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
GDK_WINDOW_XID (window),
xembed_info_atom,
0, 2, False,
xembed_info_atom, &type, &format,
&nitems, &bytes_after, &data);
gdk_error_trap_pop_ignored ();
if (status != Success)
return FALSE; /* Window vanished? */
if (type == None) /* No info property */
return FALSE;
if (type != xembed_info_atom)
{
g_warning ("_XEMBED_INFO property has wrong type");
return FALSE;
}
if (nitems < 2)
{
g_warning ("_XEMBED_INFO too short");
XFree (data);
return FALSE;
}
data_long = (unsigned long *)data;
if (version)
*version = data_long[0];
if (flags)
*flags = data_long[1] & XEMBED_MAPPED;
XFree (data);
return TRUE;
}
static void
handle_xembed_message (GtkSocket *socket,
XEmbedMessageType message,
glong detail,
glong data1,
glong data2,
guint32 time)
{
GTK_NOTE (PLUGSOCKET,
g_message ("GtkSocket: %s received", _gtk_xembed_message_name (message)));
switch (message)
{
case XEMBED_EMBEDDED_NOTIFY:
case XEMBED_WINDOW_ACTIVATE:
case XEMBED_WINDOW_DEACTIVATE:
case XEMBED_MODALITY_ON:
case XEMBED_MODALITY_OFF:
case XEMBED_FOCUS_IN:
case XEMBED_FOCUS_OUT:
g_warning ("GtkSocket: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
break;
case XEMBED_REQUEST_FOCUS:
gtk_socket_claim_focus (socket, TRUE);
break;
case XEMBED_FOCUS_NEXT:
case XEMBED_FOCUS_PREV:
gtk_socket_advance_toplevel_focus (socket,
(message == XEMBED_FOCUS_NEXT ?
GTK_DIR_TAB_FORWARD : GTK_DIR_TAB_BACKWARD));
break;
case XEMBED_GTK_GRAB_KEY:
gtk_socket_add_grabbed_key (socket, data1, data2);
break;
case XEMBED_GTK_UNGRAB_KEY:
gtk_socket_remove_grabbed_key (socket, data1, data2);
break;
case XEMBED_GRAB_KEY:
case XEMBED_UNGRAB_KEY:
break;
default:
GTK_NOTE (PLUGSOCKET,
g_message ("GtkSocket: Ignoring unknown _XEMBED message of type %d", message));
break;
}
}
static GdkFilterReturn
gtk_socket_filter_func (GdkXEvent *gdk_xevent,
GdkEvent *event,
gpointer data)
{
GtkSocket *socket;
GtkWidget *widget;
GdkDisplay *display;
XEvent *xevent;
GtkSocketPrivate *private;
GdkFilterReturn return_val;
socket = GTK_SOCKET (data);
private = socket->priv;
return_val = GDK_FILTER_CONTINUE;
if (private->plug_widget)
return return_val;
widget = GTK_WIDGET (socket);
xevent = (XEvent *)gdk_xevent;
display = gtk_widget_get_display (widget);
switch (xevent->type)
{
case ClientMessage:
if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
{
_gtk_xembed_push_message (xevent);
handle_xembed_message (socket,
xevent->xclient.data.l[1],
xevent->xclient.data.l[2],
xevent->xclient.data.l[3],
xevent->xclient.data.l[4],
xevent->xclient.data.l[0]);
_gtk_xembed_pop_message ();
return_val = GDK_FILTER_REMOVE;
}
break;
case CreateNotify:
{
XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
if (!private->plug_window)
{
gtk_socket_add_window (socket, xcwe->window, FALSE);
if (private->plug_window)
{
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - window created"));
}
}
return_val = GDK_FILTER_REMOVE;
break;
}
case ConfigureRequest:
{
XConfigureRequestEvent *xcre = &xevent->xconfigurerequest;
if (!private->plug_window)
gtk_socket_add_window (socket, xcre->window, FALSE);
if (private->plug_window)
{
if (xcre->value_mask & (CWWidth | CWHeight))
{
GTK_NOTE (PLUGSOCKET,
g_message ("GtkSocket - configure request: %d %d",
private->request_width,
private->request_height));
private->resize_count++;
gtk_widget_queue_resize (widget);
}
else if (xcre->value_mask & (CWX | CWY))
{
gtk_socket_send_configure_event (socket);
}
/* Ignore stacking requests. */
return_val = GDK_FILTER_REMOVE;
}
break;
}
case DestroyNotify:
{
XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
/* Note that we get destroy notifies both from SubstructureNotify on
* our window and StructureNotify on socket->plug_window
*/
if (private->plug_window && (xdwe->window == GDK_WINDOW_XID (private->plug_window)))
{
gboolean result;
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - destroy notify"));
gdk_window_destroy_notify (private->plug_window);
gtk_socket_end_embedding (socket);
g_object_ref (widget);
g_signal_emit_by_name (widget, "plug-removed", &result);
if (!result)
gtk_widget_destroy (widget);
g_object_unref (widget);
return_val = GDK_FILTER_REMOVE;
}
break;
}
case FocusIn:
if (xevent->xfocus.mode == EMBEDDED_APP_WANTS_FOCUS)
{
gtk_socket_claim_focus (socket, TRUE);
}
return_val = GDK_FILTER_REMOVE;
break;
case FocusOut:
return_val = GDK_FILTER_REMOVE;
break;
case MapRequest:
if (!private->plug_window)
{
gtk_socket_add_window (socket, xevent->xmaprequest.window, FALSE);
}
if (private->plug_window)
{
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - Map Request"));
gtk_socket_handle_map_request (socket);
return_val = GDK_FILTER_REMOVE;
}
break;
case PropertyNotify:
if (private->plug_window &&
xevent->xproperty.window == GDK_WINDOW_XID (private->plug_window))
{
GdkDragProtocol protocol;
if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_NORMAL_HINTS"))
{
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - received PropertyNotify for plug's WM_NORMAL_HINTS"));
private->have_size = FALSE;
gtk_widget_queue_resize (widget);
return_val = GDK_FILTER_REMOVE;
}
else if ((xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndAware")) ||
(xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO")))
{
gdk_error_trap_push ();
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
protocol = gdk_window_get_drag_protocol (private->plug_window, NULL);
if (protocol)
gtk_drag_dest_set_proxy (GTK_WIDGET (socket),
private->plug_window,
protocol, TRUE);
G_GNUC_END_IGNORE_DEPRECATIONS
gdk_error_trap_pop_ignored ();
return_val = GDK_FILTER_REMOVE;
}
else if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO"))
{
unsigned long flags;
if (xembed_get_info (private->plug_window, NULL, &flags))
{
gboolean was_mapped = private->is_mapped;
gboolean is_mapped = (flags & XEMBED_MAPPED) != 0;
if (was_mapped != is_mapped)
{
if (is_mapped)
gtk_socket_handle_map_request (socket);
else
{
gdk_error_trap_push ();
gdk_window_show (private->plug_window);
gdk_error_trap_pop_ignored ();
gtk_socket_unmap_notify (socket);
}
}
}
return_val = GDK_FILTER_REMOVE;
}
}
break;
case ReparentNotify:
{
GdkWindow *window;
XReparentEvent *xre = &xevent->xreparent;
window = gtk_widget_get_window (widget);
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - ReparentNotify received"));
if (!private->plug_window &&
xre->parent == GDK_WINDOW_XID (window))
{
gtk_socket_add_window (socket, xre->window, FALSE);
if (private->plug_window)
{
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - window reparented"));
}
return_val = GDK_FILTER_REMOVE;
}
else
{
if (private->plug_window &&
xre->window == GDK_WINDOW_XID (private->plug_window) &&
xre->parent != GDK_WINDOW_XID (window))
{
gboolean result;
gtk_socket_end_embedding (socket);
g_object_ref (widget);
g_signal_emit_by_name (widget, "plug-removed", &result);
if (!result)
gtk_widget_destroy (widget);
g_object_unref (widget);
return_val = GDK_FILTER_REMOVE;
}
}
break;
}
case UnmapNotify:
if (private->plug_window &&
xevent->xunmap.window == GDK_WINDOW_XID (private->plug_window))
{
GTK_NOTE (PLUGSOCKET, g_message ("GtkSocket - Unmap notify"));
gtk_socket_unmap_notify (socket);
return_val = GDK_FILTER_REMOVE;
}
break;
}
return return_val;
}
|