gtk2xtbin.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * The GtkXtBin widget allows for Xt toolkit code to be used
  7. * inside a GTK application.
  8. */
  9. #include "xembed.h"
  10. #include "gtk2xtbin.h"
  11. #include <gtk/gtk.h>
  12. #include <gdk/gdkx.h>
  13. #include <glib.h>
  14. #include <assert.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. /* Xlib/Xt stuff */
  21. #include <X11/Xlib.h>
  22. #include <X11/Xutil.h>
  23. #include <X11/Shell.h>
  24. #include <X11/Intrinsic.h>
  25. #include <X11/StringDefs.h>
  26. /* uncomment this if you want debugging information about widget
  27. creation and destruction */
  28. #undef DEBUG_XTBIN
  29. #define XTBIN_MAX_EVENTS 30
  30. static void gtk_xtbin_class_init (GtkXtBinClass *klass);
  31. static void gtk_xtbin_init (GtkXtBin *xtbin);
  32. static void gtk_xtbin_realize (GtkWidget *widget);
  33. static void gtk_xtbin_unrealize (GtkWidget *widget);
  34. static void gtk_xtbin_destroy (GtkObject *object);
  35. /* Xt aware XEmbed */
  36. static void xt_client_handle_xembed_message (Widget w,
  37. XtPointer client_data,
  38. XEvent *event);
  39. static void xt_add_focus_listener( Widget w, XtPointer user_data );
  40. static void xt_add_focus_listener_tree ( Widget treeroot, XtPointer user_data);
  41. static void xt_remove_focus_listener(Widget w, XtPointer user_data);
  42. static void xt_client_event_handler (Widget w, XtPointer client_data, XEvent *event);
  43. static void xt_client_focus_listener (Widget w, XtPointer user_data, XEvent *event);
  44. static void xt_client_set_info (Widget xtplug, unsigned long flags);
  45. static void send_xembed_message (XtClient *xtclient,
  46. long message,
  47. long detail,
  48. long data1,
  49. long data2,
  50. long time);
  51. static int error_handler (Display *display,
  52. XErrorEvent *error);
  53. /* For error trap of XEmbed */
  54. static void trap_errors(void);
  55. static int untrap_error(void);
  56. static int (*old_error_handler) (Display *, XErrorEvent *);
  57. static int trapped_error_code = 0;
  58. static GtkWidgetClass *parent_class = NULL;
  59. static Display *xtdisplay = NULL;
  60. static String *fallback = NULL;
  61. static gboolean xt_is_initialized = FALSE;
  62. static gint num_widgets = 0;
  63. static GPollFD xt_event_poll_fd;
  64. static gint xt_polling_timer_id = 0;
  65. static guint tag = 0;
  66. static gboolean
  67. xt_event_prepare (GSource* source_data,
  68. gint *timeout)
  69. {
  70. int mask;
  71. mask = XPending(xtdisplay);
  72. return (gboolean)mask;
  73. }
  74. static gboolean
  75. xt_event_check (GSource* source_data)
  76. {
  77. if (xt_event_poll_fd.revents & G_IO_IN) {
  78. int mask;
  79. mask = XPending(xtdisplay);
  80. return (gboolean)mask;
  81. }
  82. return FALSE;
  83. }
  84. static gboolean
  85. xt_event_dispatch (GSource* source_data,
  86. GSourceFunc call_back,
  87. gpointer user_data)
  88. {
  89. XtAppContext ac;
  90. int i = 0;
  91. ac = XtDisplayToApplicationContext(xtdisplay);
  92. /* Process only real X traffic here. We only look for data on the
  93. * pipe, limit it to XTBIN_MAX_EVENTS and only call
  94. * XtAppProcessEvent so that it will look for X events. There's no
  95. * timer processing here since we already have a timer callback that
  96. * does it. */
  97. for (i=0; i < XTBIN_MAX_EVENTS && XPending(xtdisplay); i++) {
  98. XtAppProcessEvent(ac, XtIMXEvent);
  99. }
  100. return TRUE;
  101. }
  102. static GSourceFuncs xt_event_funcs = {
  103. xt_event_prepare,
  104. xt_event_check,
  105. xt_event_dispatch,
  106. NULL,
  107. (GSourceFunc)NULL,
  108. (GSourceDummyMarshal)NULL
  109. };
  110. static gboolean
  111. xt_event_polling_timer_callback(gpointer user_data)
  112. {
  113. Display * display;
  114. XtAppContext ac;
  115. int eventsToProcess = 20;
  116. display = (Display *)user_data;
  117. ac = XtDisplayToApplicationContext(display);
  118. /* We need to process many Xt events here. If we just process
  119. one event we might starve one or more Xt consumers. On the other hand
  120. this could hang the whole app if Xt events come pouring in. So process
  121. up to 20 Xt events right now and save the rest for later. This is a hack,
  122. but it oughta work. We *really* should have out of process plugins.
  123. */
  124. while (eventsToProcess-- && XtAppPending(ac))
  125. XtAppProcessEvent(ac, XtIMAll);
  126. return TRUE;
  127. }
  128. GType
  129. gtk_xtbin_get_type (void)
  130. {
  131. static GType xtbin_type = 0;
  132. if (!xtbin_type) {
  133. static const GTypeInfo xtbin_info =
  134. {
  135. sizeof (GtkXtBinClass), /* class_size */
  136. NULL, /* base_init */
  137. NULL, /* base_finalize */
  138. (GClassInitFunc) gtk_xtbin_class_init, /* class_init */
  139. NULL, /* class_finalize */
  140. NULL, /* class_data */
  141. sizeof (GtkXtBin), /* instance_size */
  142. 0, /* n_preallocs */
  143. (GInstanceInitFunc) gtk_xtbin_init, /* instance_init */
  144. NULL /* value_table */
  145. };
  146. xtbin_type = g_type_register_static(GTK_TYPE_SOCKET, "GtkXtBin",
  147. &xtbin_info, 0);
  148. }
  149. return xtbin_type;
  150. }
  151. static void
  152. gtk_xtbin_class_init (GtkXtBinClass *klass)
  153. {
  154. GtkWidgetClass *widget_class;
  155. GtkObjectClass *object_class;
  156. parent_class = g_type_class_peek_parent(klass);
  157. widget_class = GTK_WIDGET_CLASS (klass);
  158. widget_class->realize = gtk_xtbin_realize;
  159. widget_class->unrealize = gtk_xtbin_unrealize;
  160. object_class = GTK_OBJECT_CLASS (klass);
  161. object_class->destroy = gtk_xtbin_destroy;
  162. }
  163. static void
  164. gtk_xtbin_init (GtkXtBin *xtbin)
  165. {
  166. xtbin->xtdisplay = NULL;
  167. xtbin->parent_window = NULL;
  168. xtbin->xtwindow = 0;
  169. }
  170. static void
  171. gtk_xtbin_realize (GtkWidget *widget)
  172. {
  173. GtkXtBin *xtbin;
  174. GtkAllocation allocation = { 0, 0, 200, 200 };
  175. gint x, y, w, h, d; /* geometry of window */
  176. #ifdef DEBUG_XTBIN
  177. printf("gtk_xtbin_realize()\n");
  178. #endif
  179. g_return_if_fail (GTK_IS_XTBIN (widget));
  180. xtbin = GTK_XTBIN (widget);
  181. /* caculate the allocation before realize */
  182. gdk_window_get_geometry(xtbin->parent_window, &x, &y, &w, &h, &d);
  183. allocation.width = w;
  184. allocation.height = h;
  185. gtk_widget_size_allocate (widget, &allocation);
  186. #ifdef DEBUG_XTBIN
  187. printf("initial allocation %d %d %d %d\n", x, y, w, h);
  188. #endif
  189. /* use GtkSocket's realize */
  190. (*GTK_WIDGET_CLASS(parent_class)->realize)(widget);
  191. /* create the Xt client widget */
  192. xt_client_create(&(xtbin->xtclient),
  193. gtk_socket_get_id(GTK_SOCKET(xtbin)),
  194. h, w);
  195. xtbin->xtwindow = XtWindow(xtbin->xtclient.child_widget);
  196. gdk_flush();
  197. /* now that we have created the xt client, add it to the socket. */
  198. gtk_socket_add_id(GTK_SOCKET(widget), xtbin->xtwindow);
  199. }
  200. GtkWidget*
  201. gtk_xtbin_new (GdkWindow *parent_window, String * f)
  202. {
  203. GtkXtBin *xtbin;
  204. gpointer user_data;
  205. assert(parent_window != NULL);
  206. xtbin = g_object_new (GTK_TYPE_XTBIN, NULL);
  207. if (!xtbin)
  208. return (GtkWidget*)NULL;
  209. if (f)
  210. fallback = f;
  211. /* Initialize the Xt toolkit */
  212. xtbin->parent_window = parent_window;
  213. xt_client_init(&(xtbin->xtclient),
  214. GDK_VISUAL_XVISUAL(gdk_rgb_get_visual()),
  215. GDK_COLORMAP_XCOLORMAP(gdk_rgb_get_colormap()),
  216. gdk_rgb_get_visual()->depth);
  217. if (!xtbin->xtclient.xtdisplay) {
  218. /* If XtOpenDisplay failed, we can't go any further.
  219. * Bail out.
  220. */
  221. #ifdef DEBUG_XTBIN
  222. printf("gtk_xtbin_init: XtOpenDisplay() returned NULL.\n");
  223. #endif
  224. g_free (xtbin);
  225. return (GtkWidget *)NULL;
  226. }
  227. /* Launch X event loop */
  228. xt_client_xloop_create();
  229. /* Build the hierachy */
  230. xtbin->xtdisplay = xtbin->xtclient.xtdisplay;
  231. gtk_widget_set_parent_window(GTK_WIDGET(xtbin), parent_window);
  232. gdk_window_get_user_data(xtbin->parent_window, &user_data);
  233. if (user_data)
  234. gtk_container_add(GTK_CONTAINER(user_data), GTK_WIDGET(xtbin));
  235. /* This GtkSocket has a visible window, but the Xt plug will cover this
  236. * window. Normally GtkSockets let the X server paint their background and
  237. * this would happen immediately (before the plug is mapped). Setting the
  238. * background to None prevents the server from painting this window,
  239. * avoiding flicker.
  240. */
  241. gtk_widget_realize(GTK_WIDGET(xtbin));
  242. gdk_window_set_back_pixmap(GTK_WIDGET(xtbin)->window, NULL, FALSE);
  243. return GTK_WIDGET (xtbin);
  244. }
  245. static void
  246. gtk_xtbin_unrealize (GtkWidget *object)
  247. {
  248. GtkXtBin *xtbin;
  249. GtkWidget *widget;
  250. #ifdef DEBUG_XTBIN
  251. printf("gtk_xtbin_unrealize()\n");
  252. #endif
  253. /* gtk_object_destroy() will already hold a refcount on object
  254. */
  255. xtbin = GTK_XTBIN(object);
  256. widget = GTK_WIDGET(object);
  257. GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
  258. if (GTK_WIDGET_REALIZED (widget)) {
  259. xt_client_unrealize(&(xtbin->xtclient));
  260. }
  261. (*GTK_WIDGET_CLASS (parent_class)->unrealize)(widget);
  262. }
  263. static void
  264. gtk_xtbin_destroy (GtkObject *object)
  265. {
  266. GtkXtBin *xtbin;
  267. #ifdef DEBUG_XTBIN
  268. printf("gtk_xtbin_destroy()\n");
  269. #endif
  270. g_return_if_fail (object != NULL);
  271. g_return_if_fail (GTK_IS_XTBIN (object));
  272. xtbin = GTK_XTBIN (object);
  273. if(xtbin->xtwindow) {
  274. /* remove the event handler */
  275. xt_client_destroy(&(xtbin->xtclient));
  276. xtbin->xtwindow = 0;
  277. /* stop X event loop */
  278. xt_client_xloop_destroy();
  279. }
  280. GTK_OBJECT_CLASS(parent_class)->destroy(object);
  281. }
  282. /*
  283. * Following is the implementation of Xt XEmbedded for client side
  284. */
  285. /* Initial Xt plugin */
  286. void
  287. xt_client_init( XtClient * xtclient,
  288. Visual *xtvisual,
  289. Colormap xtcolormap,
  290. int xtdepth)
  291. {
  292. XtAppContext app_context;
  293. char *mArgv[1];
  294. int mArgc = 0;
  295. /*
  296. * Initialize Xt stuff
  297. */
  298. xtclient->top_widget = NULL;
  299. xtclient->child_widget = NULL;
  300. xtclient->xtdisplay = NULL;
  301. xtclient->xtvisual = NULL;
  302. xtclient->xtcolormap = 0;
  303. xtclient->xtdepth = 0;
  304. if (!xt_is_initialized) {
  305. #ifdef DEBUG_XTBIN
  306. printf("starting up Xt stuff\n");
  307. #endif
  308. XtToolkitInitialize();
  309. app_context = XtCreateApplicationContext();
  310. if (fallback)
  311. XtAppSetFallbackResources(app_context, fallback);
  312. xtdisplay = XtOpenDisplay(app_context, gdk_get_display(), NULL,
  313. "Wrapper", NULL, 0, &mArgc, mArgv);
  314. if (xtdisplay)
  315. xt_is_initialized = TRUE;
  316. }
  317. xtclient->xtdisplay = xtdisplay;
  318. xtclient->xtvisual = xtvisual;
  319. xtclient->xtcolormap = xtcolormap;
  320. xtclient->xtdepth = xtdepth;
  321. }
  322. void
  323. xt_client_xloop_create(void)
  324. {
  325. /* If this is the first running widget, hook this display into the
  326. mainloop */
  327. if (0 == num_widgets) {
  328. int cnumber;
  329. GSource* gs;
  330. /* Set up xtdisplay in case we're missing one */
  331. if (!xtdisplay) {
  332. (void)xt_client_get_display();
  333. }
  334. /*
  335. * hook Xt event loop into the glib event loop.
  336. */
  337. /* the assumption is that gtk_init has already been called */
  338. gs = g_source_new(&xt_event_funcs, sizeof(GSource));
  339. if (!gs) {
  340. return;
  341. }
  342. g_source_set_priority(gs, GDK_PRIORITY_EVENTS);
  343. g_source_set_can_recurse(gs, TRUE);
  344. tag = g_source_attach(gs, (GMainContext*)NULL);
  345. g_source_unref(gs);
  346. #ifdef VMS
  347. cnumber = XConnectionNumber(xtdisplay);
  348. #else
  349. cnumber = ConnectionNumber(xtdisplay);
  350. #endif
  351. xt_event_poll_fd.fd = cnumber;
  352. xt_event_poll_fd.events = G_IO_IN;
  353. xt_event_poll_fd.revents = 0; /* hmm... is this correct? */
  354. g_main_context_add_poll ((GMainContext*)NULL,
  355. &xt_event_poll_fd,
  356. G_PRIORITY_LOW);
  357. /* add a timer so that we can poll and process Xt timers */
  358. xt_polling_timer_id =
  359. g_timeout_add(25,
  360. (GtkFunction)xt_event_polling_timer_callback,
  361. xtdisplay);
  362. }
  363. /* Bump up our usage count */
  364. num_widgets++;
  365. }
  366. void
  367. xt_client_xloop_destroy(void)
  368. {
  369. num_widgets--; /* reduce our usage count */
  370. /* If this is the last running widget, remove the Xt display
  371. connection from the mainloop */
  372. if (0 == num_widgets) {
  373. #ifdef DEBUG_XTBIN
  374. printf("removing the Xt connection from the main loop\n");
  375. #endif
  376. g_main_context_remove_poll((GMainContext*)NULL, &xt_event_poll_fd);
  377. g_source_remove(tag);
  378. g_source_remove(xt_polling_timer_id);
  379. xt_polling_timer_id = 0;
  380. }
  381. }
  382. /* Get Xt Client display */
  383. Display *
  384. xt_client_get_display(void)
  385. {
  386. if (!xtdisplay) {
  387. XtClient tmp;
  388. xt_client_init(&tmp,NULL,0,0);
  389. }
  390. return xtdisplay;
  391. }
  392. /* Create the Xt client widgets
  393. * */
  394. void
  395. xt_client_create ( XtClient* xtclient ,
  396. Window embedderid,
  397. int height,
  398. int width )
  399. {
  400. int n;
  401. Arg args[6];
  402. Widget child_widget;
  403. Widget top_widget;
  404. #ifdef DEBUG_XTBIN
  405. printf("xt_client_create() \n");
  406. #endif
  407. top_widget = XtAppCreateShell("drawingArea", "Wrapper",
  408. applicationShellWidgetClass,
  409. xtclient->xtdisplay,
  410. NULL, 0);
  411. xtclient->top_widget = top_widget;
  412. /* set size of Xt window */
  413. n = 0;
  414. XtSetArg(args[n], XtNheight, height);n++;
  415. XtSetArg(args[n], XtNwidth, width);n++;
  416. XtSetValues(top_widget, args, n);
  417. child_widget = XtVaCreateWidget("form",
  418. compositeWidgetClass,
  419. top_widget, NULL);
  420. n = 0;
  421. XtSetArg(args[n], XtNheight, height);n++;
  422. XtSetArg(args[n], XtNwidth, width);n++;
  423. XtSetArg(args[n], XtNvisual, xtclient->xtvisual ); n++;
  424. XtSetArg(args[n], XtNdepth, xtclient->xtdepth ); n++;
  425. XtSetArg(args[n], XtNcolormap, xtclient->xtcolormap ); n++;
  426. XtSetArg(args[n], XtNborderWidth, 0); n++;
  427. XtSetValues(child_widget, args, n);
  428. XSync(xtclient->xtdisplay, FALSE);
  429. xtclient->oldwindow = top_widget->core.window;
  430. top_widget->core.window = embedderid;
  431. /* this little trick seems to finish initializing the widget */
  432. #if XlibSpecificationRelease >= 6
  433. XtRegisterDrawable(xtclient->xtdisplay,
  434. embedderid,
  435. top_widget);
  436. #else
  437. _XtRegisterWindow( embedderid,
  438. top_widget);
  439. #endif
  440. XtRealizeWidget(child_widget);
  441. /* listen to all Xt events */
  442. XSelectInput(xtclient->xtdisplay,
  443. embedderid,
  444. XtBuildEventMask(top_widget));
  445. xt_client_set_info (child_widget, 0);
  446. XtManageChild(child_widget);
  447. xtclient->child_widget = child_widget;
  448. /* set the event handler */
  449. XtAddEventHandler(child_widget,
  450. StructureNotifyMask | KeyPressMask,
  451. TRUE,
  452. (XtEventHandler)xt_client_event_handler, xtclient);
  453. XtAddEventHandler(child_widget,
  454. SubstructureNotifyMask | ButtonReleaseMask,
  455. FALSE,
  456. (XtEventHandler)xt_client_focus_listener,
  457. xtclient);
  458. XSync(xtclient->xtdisplay, FALSE);
  459. }
  460. void
  461. xt_client_unrealize ( XtClient* xtclient )
  462. {
  463. /* Explicitly destroy the child_widget window because this is actually a
  464. child of the socket window. It is not a child of top_widget's window
  465. when that is destroyed. */
  466. XtUnrealizeWidget(xtclient->child_widget);
  467. #if XlibSpecificationRelease >= 6
  468. XtUnregisterDrawable(xtclient->xtdisplay,
  469. xtclient->top_widget->core.window);
  470. #else
  471. _XtUnregisterWindow(xtclient->top_widget->core.window,
  472. xtclient->top_widget);
  473. #endif
  474. /* flush the queue before we returning origin top_widget->core.window
  475. or we can get X error since the window is gone */
  476. XSync(xtclient->xtdisplay, False);
  477. xtclient->top_widget->core.window = xtclient->oldwindow;
  478. XtUnrealizeWidget(xtclient->top_widget);
  479. }
  480. void
  481. xt_client_destroy (XtClient* xtclient)
  482. {
  483. if(xtclient->top_widget) {
  484. XtRemoveEventHandler(xtclient->child_widget,
  485. StructureNotifyMask | KeyPressMask,
  486. TRUE,
  487. (XtEventHandler)xt_client_event_handler, xtclient);
  488. XtDestroyWidget(xtclient->top_widget);
  489. xtclient->top_widget = NULL;
  490. }
  491. }
  492. void
  493. xt_client_set_info (Widget xtplug, unsigned long flags)
  494. {
  495. unsigned long buffer[2];
  496. Atom infoAtom = XInternAtom(XtDisplay(xtplug), "_XEMBED_INFO", False);
  497. buffer[1] = 0; /* Protocol version */
  498. buffer[1] = flags;
  499. XChangeProperty (XtDisplay(xtplug), XtWindow(xtplug),
  500. infoAtom, infoAtom, 32,
  501. PropModeReplace,
  502. (unsigned char *)buffer, 2);
  503. }
  504. static void
  505. xt_client_handle_xembed_message(Widget w, XtPointer client_data, XEvent *event)
  506. {
  507. XtClient *xtplug = (XtClient*)client_data;
  508. switch (event->xclient.data.l[1])
  509. {
  510. case XEMBED_EMBEDDED_NOTIFY:
  511. break;
  512. case XEMBED_WINDOW_ACTIVATE:
  513. #ifdef DEBUG_XTBIN
  514. printf("Xt client get XEMBED_WINDOW_ACTIVATE\n");
  515. #endif
  516. break;
  517. case XEMBED_WINDOW_DEACTIVATE:
  518. #ifdef DEBUG_XTBIN
  519. printf("Xt client get XEMBED_WINDOW_DEACTIVATE\n");
  520. #endif
  521. break;
  522. case XEMBED_MODALITY_ON:
  523. #ifdef DEBUG_XTBIN
  524. printf("Xt client get XEMBED_MODALITY_ON\n");
  525. #endif
  526. break;
  527. case XEMBED_MODALITY_OFF:
  528. #ifdef DEBUG_XTBIN
  529. printf("Xt client get XEMBED_MODALITY_OFF\n");
  530. #endif
  531. break;
  532. case XEMBED_FOCUS_IN:
  533. case XEMBED_FOCUS_OUT:
  534. {
  535. XEvent xevent;
  536. memset(&xevent, 0, sizeof(xevent));
  537. if(event->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  538. #ifdef DEBUG_XTBIN
  539. printf("XTEMBED got focus in\n");
  540. #endif
  541. xevent.xfocus.type = FocusIn;
  542. }
  543. else {
  544. #ifdef DEBUG_XTBIN
  545. printf("XTEMBED got focus out\n");
  546. #endif
  547. xevent.xfocus.type = FocusOut;
  548. }
  549. xevent.xfocus.window = XtWindow(xtplug->child_widget);
  550. xevent.xfocus.display = XtDisplay(xtplug->child_widget);
  551. XSendEvent(XtDisplay(xtplug->child_widget),
  552. xevent.xfocus.window,
  553. False, NoEventMask,
  554. &xevent );
  555. XSync( XtDisplay(xtplug->child_widget), False);
  556. }
  557. break;
  558. default:
  559. break;
  560. } /* End of XEmbed Message */
  561. }
  562. void
  563. xt_client_event_handler( Widget w, XtPointer client_data, XEvent *event)
  564. {
  565. XtClient *xtplug = (XtClient*)client_data;
  566. switch(event->type)
  567. {
  568. case ClientMessage:
  569. /* Handle xembed message */
  570. if (event->xclient.message_type==
  571. XInternAtom (XtDisplay(xtplug->child_widget),
  572. "_XEMBED", False)) {
  573. xt_client_handle_xembed_message(w, client_data, event);
  574. }
  575. break;
  576. case ReparentNotify:
  577. break;
  578. case MappingNotify:
  579. xt_client_set_info (w, XEMBED_MAPPED);
  580. break;
  581. case UnmapNotify:
  582. xt_client_set_info (w, 0);
  583. break;
  584. case KeyPress:
  585. #ifdef DEBUG_XTBIN
  586. printf("Key Press Got!\n");
  587. #endif
  588. break;
  589. default:
  590. break;
  591. } /* End of switch(event->type) */
  592. }
  593. static void
  594. send_xembed_message (XtClient *xtclient,
  595. long message,
  596. long detail,
  597. long data1,
  598. long data2,
  599. long time)
  600. {
  601. XEvent xevent;
  602. Window w=XtWindow(xtclient->top_widget);
  603. Display* dpy=xtclient->xtdisplay;
  604. int errorcode;
  605. memset(&xevent,0,sizeof(xevent));
  606. xevent.xclient.window = w;
  607. xevent.xclient.type = ClientMessage;
  608. xevent.xclient.message_type = XInternAtom(dpy,"_XEMBED",False);
  609. xevent.xclient.format = 32;
  610. xevent.xclient.data.l[0] = time;
  611. xevent.xclient.data.l[1] = message;
  612. xevent.xclient.data.l[2] = detail;
  613. xevent.xclient.data.l[3] = data1;
  614. xevent.xclient.data.l[4] = data2;
  615. trap_errors ();
  616. XSendEvent (dpy, w, False, NoEventMask, &xevent);
  617. XSync (dpy,False);
  618. if((errorcode = untrap_error())) {
  619. #ifdef DEBUG_XTBIN
  620. printf("send_xembed_message error(%d)!!!\n",errorcode);
  621. #endif
  622. }
  623. }
  624. static int
  625. error_handler(Display *display, XErrorEvent *error)
  626. {
  627. trapped_error_code = error->error_code;
  628. return 0;
  629. }
  630. static void
  631. trap_errors(void)
  632. {
  633. trapped_error_code =0;
  634. old_error_handler = XSetErrorHandler(error_handler);
  635. }
  636. static int
  637. untrap_error(void)
  638. {
  639. XSetErrorHandler(old_error_handler);
  640. if(trapped_error_code) {
  641. #ifdef DEBUG_XTBIN
  642. printf("Get X Window Error = %d\n", trapped_error_code);
  643. #endif
  644. }
  645. return trapped_error_code;
  646. }
  647. void
  648. xt_client_focus_listener( Widget w, XtPointer user_data, XEvent *event)
  649. {
  650. Display *dpy = XtDisplay(w);
  651. XtClient *xtclient = user_data;
  652. Window win = XtWindow(w);
  653. switch(event->type)
  654. {
  655. case CreateNotify:
  656. if(event->xcreatewindow.parent == win) {
  657. Widget child=XtWindowToWidget( dpy, event->xcreatewindow.window);
  658. if (child)
  659. xt_add_focus_listener_tree(child, user_data);
  660. }
  661. break;
  662. case DestroyNotify:
  663. xt_remove_focus_listener( w, user_data);
  664. break;
  665. case ReparentNotify:
  666. if(event->xreparent.parent == win) {
  667. /* I am the new parent */
  668. Widget child=XtWindowToWidget(dpy, event->xreparent.window);
  669. if (child)
  670. xt_add_focus_listener_tree( child, user_data);
  671. }
  672. else if(event->xreparent.window == win) {
  673. /* I am the new child */
  674. }
  675. else {
  676. /* I am the old parent */
  677. }
  678. break;
  679. case ButtonRelease:
  680. #if 0
  681. XSetInputFocus(dpy, XtWindow(xtclient->child_widget), RevertToParent, event->xbutton.time);
  682. #endif
  683. send_xembed_message ( xtclient,
  684. XEMBED_REQUEST_FOCUS, 0, 0, 0, 0);
  685. break;
  686. default:
  687. break;
  688. } /* End of switch(event->type) */
  689. }
  690. static void
  691. xt_add_focus_listener( Widget w, XtPointer user_data)
  692. {
  693. XtClient *xtclient = user_data;
  694. trap_errors ();
  695. XtAddEventHandler(w,
  696. SubstructureNotifyMask | ButtonReleaseMask,
  697. FALSE,
  698. (XtEventHandler)xt_client_focus_listener,
  699. xtclient);
  700. untrap_error();
  701. }
  702. static void
  703. xt_remove_focus_listener(Widget w, XtPointer user_data)
  704. {
  705. trap_errors ();
  706. XtRemoveEventHandler(w, SubstructureNotifyMask | ButtonReleaseMask, FALSE,
  707. (XtEventHandler)xt_client_focus_listener, user_data);
  708. untrap_error();
  709. }
  710. static void
  711. xt_add_focus_listener_tree ( Widget treeroot, XtPointer user_data)
  712. {
  713. Window win = XtWindow(treeroot);
  714. Window *children;
  715. Window root, parent;
  716. Display *dpy = XtDisplay(treeroot);
  717. unsigned int i, nchildren;
  718. /* ensure we don't add more than once */
  719. xt_remove_focus_listener( treeroot, user_data);
  720. xt_add_focus_listener( treeroot, user_data);
  721. trap_errors();
  722. if(!XQueryTree(dpy, win, &root, &parent, &children, &nchildren)) {
  723. untrap_error();
  724. return;
  725. }
  726. if(untrap_error())
  727. return;
  728. for(i=0; i<nchildren; ++i) {
  729. Widget child = XtWindowToWidget(dpy, children[i]);
  730. if (child)
  731. xt_add_focus_listener_tree( child, user_data);
  732. }
  733. XFree((void*)children);
  734. return;
  735. }