⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk2xtbin.c

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 C
📖 第 1 页 / 共 2 页
字号:
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * vim:expandtab:shiftwidth=2:tabstop=2: */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is the Gtk2XtBin Widget Implementation. * * The Initial Developer of the Original Code is * Sun Microsystems, Inc. * Portions created by the Initial Developer are Copyright (C) 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ /* * The GtkXtBin widget allows for Xt toolkit code to be used * inside a GTK application.   */#undef GTK_DISABLE_DEPRECATED#include "xembed.h"#include "gtk2xtbin.h"#include <gtk/gtk.h>#include <gdk/gdkx.h>#include <glib.h>#include <assert.h>#include <sys/time.h>#include <sys/types.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>/* Xlib/Xt stuff */#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Shell.h>#include <X11/Intrinsic.h>#include <X11/StringDefs.h>/* uncomment this if you want debugging information about widget   creation and destruction */#undef DEBUG_XTBIN#define XTBIN_MAX_EVENTS 30static void            gtk_xtbin_class_init (GtkXtBinClass *klass);static void            gtk_xtbin_init       (GtkXtBin      *xtbin);static void            gtk_xtbin_realize    (GtkWidget      *widget);static void            gtk_xtbin_unrealize    (GtkWidget      *widget);static void            gtk_xtbin_destroy    (GtkObject      *object);static void            gtk_xtbin_shutdown   (GtkObject      *object);/* Xt aware XEmbed */static void       xt_client_init      (XtClient * xtclient,                                        Visual *xtvisual,                                        Colormap xtcolormap,                                        int xtdepth);static void       xt_client_create    (XtClient * xtclient,                                        Window embeder,                                        int height,                                        int width );static void       xt_client_unrealize (XtClient* xtclient);static void       xt_client_destroy   (XtClient* xtclient);static void       xt_client_set_info  (Widget xtplug,                                        unsigned long flags);static void       xt_client_event_handler (Widget w,                                            XtPointer client_data,                                            XEvent *event);static void       xt_client_handle_xembed_message (Widget w,                                                    XtPointer client_data,                                                    XEvent *event);static void       xt_client_focus_listener       (Widget w,                                                    XtPointer user_data,                                                    XEvent *event);static void       xt_add_focus_listener( Widget w, XtPointer user_data );static void       xt_add_focus_listener_tree ( Widget treeroot, XtPointer user_data); static void       xt_remove_focus_listener(Widget w, XtPointer user_data);static void       send_xembed_message (XtClient *xtclient,                                       long message,                                        long detail,                                        long data1,                                        long data2,                                       long time);  static int        error_handler       (Display *display,                                        XErrorEvent *error);/* For error trap of XEmbed */static void       trap_errors(void);static int        untrap_error(void);static int        (*old_error_handler) (Display *, XErrorEvent *);static int        trapped_error_code = 0;static GtkWidgetClass *parent_class = NULL;static Display         *xtdisplay = NULL;static String          *fallback = NULL;static gboolean         xt_is_initialized = FALSE;static gint             num_widgets = 0;static GPollFD          xt_event_poll_fd;static gint             xt_polling_timer_id = 0;static guint            tag = 0;static gbooleanxt_event_prepare (GSource*  source_data,                   gint     *timeout){     int mask;  GDK_THREADS_ENTER();  mask = XPending(xtdisplay);  GDK_THREADS_LEAVE();  return (gboolean)mask;}static gbooleanxt_event_check (GSource*  source_data){  GDK_THREADS_ENTER ();  if (xt_event_poll_fd.revents & G_IO_IN) {    int mask;    mask = XPending(xtdisplay);    GDK_THREADS_LEAVE ();    return (gboolean)mask;  }  GDK_THREADS_LEAVE ();  return FALSE;}   static gbooleanxt_event_dispatch (GSource*  source_data,                    GSourceFunc call_back,                    gpointer  user_data){  XEvent event;  XtAppContext ac;  int i = 0;  ac = XtDisplayToApplicationContext(xtdisplay);  GDK_THREADS_ENTER ();  /* Process only real X traffic here.  We only look for data on the   * pipe, limit it to XTBIN_MAX_EVENTS and only call   * XtAppProcessEvent so that it will look for X events.  There's no   * timer processing here since we already have a timer callback that   * does it.  */  for (i=0; i < XTBIN_MAX_EVENTS && XPending(xtdisplay); i++) {    XtAppProcessEvent(ac, XtIMXEvent);  }  GDK_THREADS_LEAVE ();  return TRUE;  }static GSourceFuncs xt_event_funcs = {  xt_event_prepare,  xt_event_check,  xt_event_dispatch,  g_free,  (GSourceFunc)NULL,  (GSourceDummyMarshal)NULL};static gbooleanxt_event_polling_timer_callback(gpointer user_data){  Display * display;  XtAppContext ac;  int eventsToProcess = 20;  display = (Display *)user_data;  ac = XtDisplayToApplicationContext(display);  /* We need to process many Xt events here. If we just process     one event we might starve one or more Xt consumers. On the other hand     this could hang the whole app if Xt events come pouring in. So process     up to 20 Xt events right now and save the rest for later. This is a hack,     but it oughta work. We *really* should have out of process plugins.  */  while (eventsToProcess-- && XtAppPending(ac))    XtAppProcessEvent(ac, XtIMAll);  return TRUE;}GTypegtk_xtbin_get_type (void){  static GType xtbin_type = 0;  if (!xtbin_type) {      static const GTypeInfo xtbin_info =      {        sizeof (GtkXtBinClass),        NULL,        NULL,        (GClassInitFunc)gtk_xtbin_class_init,        NULL,        NULL,        sizeof (GtkXtBin),        0,        (GInstanceInitFunc)gtk_xtbin_init,      };      xtbin_type = g_type_register_static (GTK_TYPE_SOCKET,                                           "GtkXtBin",					   &xtbin_info,					   0);  }  return xtbin_type;}static voidgtk_xtbin_class_init (GtkXtBinClass *klass){  GtkWidgetClass *widget_class;  GtkObjectClass *object_class;  parent_class = gtk_type_class (GTK_TYPE_SOCKET);  widget_class = GTK_WIDGET_CLASS (klass);  widget_class->realize = gtk_xtbin_realize;  widget_class->unrealize = gtk_xtbin_unrealize;  object_class = GTK_OBJECT_CLASS (klass);  object_class->destroy = gtk_xtbin_destroy;}static voidgtk_xtbin_init (GtkXtBin *xtbin){  xtbin->xtdisplay = NULL;  xtbin->parent_window = NULL;  xtbin->xtwindow = 0;  xtbin->x = 0;  xtbin->y = 0;}static voidgtk_xtbin_realize (GtkWidget *widget){  GtkXtBin     *xtbin;  GtkAllocation allocation = { 0, 0, 200, 200 };  gint  x, y, w, h, d; /* geometry of window */#ifdef DEBUG_XTBIN  printf("gtk_xtbin_realize()\n");#endif  g_return_if_fail (GTK_IS_XTBIN (widget));  xtbin = GTK_XTBIN (widget);  /* caculate the allocation before realize */  gdk_window_get_geometry(xtbin->parent_window, &x, &y, &w, &h, &d);  allocation.width = w;  allocation.height = h;  gtk_widget_size_allocate (widget, &allocation);#ifdef DEBUG_XTBIN  printf("initial allocation %d %d %d %d\n", x, y, w, h);#endif  xtbin->width = widget->allocation.width;  xtbin->height = widget->allocation.height;  /* use GtkSocket's realize */  (*GTK_WIDGET_CLASS(parent_class)->realize)(widget);  /* create the Xt client widget */  xt_client_create(&(xtbin->xtclient),        gtk_socket_get_id(GTK_SOCKET(xtbin)),        xtbin->height,        xtbin->width);  xtbin->xtwindow = XtWindow(xtbin->xtclient.child_widget);  gdk_flush();  /* now that we have created the xt client, add it to the socket. */  gtk_socket_add_id(GTK_SOCKET(widget), xtbin->xtwindow);}GtkWidget*gtk_xtbin_new (GdkWindow *parent_window, String * f){  GtkXtBin *xtbin;  gpointer user_data;  assert(parent_window != NULL);  xtbin = gtk_type_new (GTK_TYPE_XTBIN);  if (!xtbin)    return (GtkWidget*)NULL;  if (f)    fallback = f;  /* Initialize the Xt toolkit */  xtbin->parent_window = parent_window;  xt_client_init(&(xtbin->xtclient),       GDK_VISUAL_XVISUAL(gdk_rgb_get_visual()),      GDK_COLORMAP_XCOLORMAP(gdk_rgb_get_colormap()),      gdk_rgb_get_visual()->depth);  if (!xtbin->xtclient.xtdisplay) {    /* If XtOpenDisplay failed, we can't go any further.     *  Bail out.     */#ifdef DEBUG_XTBIN    printf("gtk_xtbin_init: XtOpenDisplay() returned NULL.\n");#endif    g_free (xtbin);    return (GtkWidget *)NULL;  }  /* If this is the first running widget, hook this display into the     mainloop */  if (0 == num_widgets) {    int           cnumber;    /*     * hook Xt event loop into the glib event loop.     */    /* the assumption is that gtk_init has already been called */    GSource* gs = g_source_new(&xt_event_funcs, sizeof(GSource));      if (!gs) {       return NULL;      }        g_source_set_priority(gs, GDK_PRIORITY_EVENTS);    g_source_set_can_recurse(gs, TRUE);    tag = g_source_attach(gs, (GMainContext*)NULL);#ifdef VMS    cnumber = XConnectionNumber(xtdisplay);#else    cnumber = ConnectionNumber(xtdisplay);#endif    xt_event_poll_fd.fd = cnumber;    xt_event_poll_fd.events = G_IO_IN;     xt_event_poll_fd.revents = 0;    /* hmm... is this correct? */    g_main_context_add_poll ((GMainContext*)NULL,                              &xt_event_poll_fd,                              G_PRIORITY_LOW);    /* add a timer so that we can poll and process Xt timers */    xt_polling_timer_id =      gtk_timeout_add(25,                      (GtkFunction)xt_event_polling_timer_callback,                      xtdisplay);  }  /* Bump up our usage count */  num_widgets++;  /* Build the hierachy */  xtbin->xtdisplay = xtbin->xtclient.xtdisplay;  gtk_widget_set_parent_window(GTK_WIDGET(xtbin), parent_window);  gdk_window_get_user_data(xtbin->parent_window, &user_data);  if (user_data)    gtk_container_add(GTK_CONTAINER(user_data), GTK_WIDGET(xtbin));  return GTK_WIDGET (xtbin);}voidgtk_xtbin_set_position (GtkXtBin *xtbin,                        gint       x,                        gint       y){  xtbin->x = x;  xtbin->y = y;  if (GTK_WIDGET_REALIZED (xtbin))    gdk_window_move (GTK_WIDGET (xtbin)->window, x, y);}voidgtk_xtbin_resize (GtkWidget *widget,                  gint       width,                  gint       height){  Arg args[2];  GtkXtBin *xtbin = GTK_XTBIN (widget);  GtkAllocation allocation;#ifdef DEBUG_XTBIN  printf("gtk_xtbin_resize %p %d %d\n", (void *)widget, width, height);#endif  xtbin->height = height;  xtbin->width  = width;  // Avoid BadValue errors in XtSetValues  if (height <= 0 || width <=0) {    height = 1;    width = 1;  }  XtSetArg(args[0], XtNheight, height);  XtSetArg(args[1], XtNwidth,  width);  XtSetValues(xtbin->xtclient.top_widget, args, 2);  /* we need to send a size allocate so the socket knows about the     size changes */  allocation.x = xtbin->x;  allocation.y = xtbin->y;  allocation.width = xtbin->width;  allocation.height = xtbin->height;  gtk_widget_size_allocate(widget, &allocation);}static voidgtk_xtbin_unrealize (GtkWidget *object){  GtkXtBin *xtbin;  GtkWidget *widget;#ifdef DEBUG_XTBIN  printf("gtk_xtbin_unrealize()\n");#endif  /* gtk_object_destroy() will already hold a refcount on object   */  xtbin = GTK_XTBIN(object);  widget = GTK_WIDGET(object);  GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);  if (GTK_WIDGET_REALIZED (widget)) {    xt_client_unrealize(&(xtbin->xtclient));  }  (*GTK_WIDGET_CLASS (parent_class)->unrealize)(widget);}static voidgtk_xtbin_destroy (GtkObject *object){  GtkXtBin *xtbin;#ifdef DEBUG_XTBIN

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -