Commit 6a7be726 authored by Tor Lillqvist's avatar Tor Lillqvist Committed by Tor Lillqvist

gtk/gtksocket.c Splice out backend-dependend parts to separate files, see

2005-07-21  Tor Lillqvist  <tml@novell.com>

	* gtk/gtksocket.c
	* gtk/gtkplug.c: Splice out backend-dependend parts to separate
	files, see below.

	* gtk/gtksocketprivate.h
	* gtk/gtkplugprivate.h: New files. Declare the backend-specific
	functions, and the private backend-independent functions they call
	back to.
parent 27940c43
2005-07-21 Tor Lillqvist <tml@novell.com>
* gtk/gtksocket.c
* gtk/gtkplug.c: Splice out backend-dependend parts to separate
files, see below.
* gtk/gtksocketprivate.h
* gtk/gtkplugprivate.h: New files. Declare the backend-specific
functions, and the private backend-independent functions they call
back to.
* gtk/gtksocket-stub.c
* gtk/gtkplug-stub.c
* gtk/gtksocket-x11.c
......
2005-07-21 Tor Lillqvist <tml@novell.com>
* gtk/gtksocket.c
* gtk/gtkplug.c: Splice out backend-dependend parts to separate
files, see below.
* gtk/gtksocketprivate.h
* gtk/gtkplugprivate.h: New files. Declare the backend-specific
functions, and the private backend-independent functions they call
back to.
* gtk/gtksocket-stub.c
* gtk/gtkplug-stub.c
* gtk/gtksocket-x11.c
......
2005-07-21 Tor Lillqvist <tml@novell.com>
* gtk/gtksocket.c
* gtk/gtkplug.c: Splice out backend-dependend parts to separate
files, see below.
* gtk/gtksocketprivate.h
* gtk/gtkplugprivate.h: New files. Declare the backend-specific
functions, and the private backend-independent functions they call
back to.
* gtk/gtksocket-stub.c
* gtk/gtkplug-stub.c
* gtk/gtksocket-x11.c
......
......@@ -29,11 +29,7 @@
#include "gtkmarshalers.h"
#include "gtkplug.h"
#include "gtkprivate.h"
#include "gdk/gdkkeysyms.h"
#include "x11/gdkx.h"
#include "gtkxembed.h"
#include "gtkplugprivate.h"
#include "gtkalias.h"
......@@ -58,20 +54,16 @@ static gboolean gtk_plug_focus (GtkWidget *widget,
GtkDirectionType direction);
static void gtk_plug_check_resize (GtkContainer *container);
static void gtk_plug_keys_changed (GtkWindow *window);
static GdkFilterReturn gtk_plug_filter_func (GdkXEvent *gdk_xevent,
GdkEvent *event,
gpointer data);
static void handle_modality_off (GtkPlug *plug);
static void xembed_set_info (GdkWindow *window,
unsigned long flags);
/* From Tk */
#define EMBEDDED_APP_WANTS_FOCUS NotifyNormal+20
static GtkWindowClass *parent_class = NULL;
static GtkBinClass *bin_class = NULL;
typedef struct
{
guint accelerator_key;
GdkModifierType accelerator_mods;
} GrabbedKey;
enum {
EMBEDDED,
LAST_SIGNAL
......@@ -167,7 +159,7 @@ gtk_plug_set_is_child (GtkPlug *plug,
if (is_child)
{
if (plug->modality_window)
handle_modality_off (plug);
_gtk_plug_handle_modality_off (plug);
if (plug->modality_group)
{
......@@ -206,6 +198,27 @@ gtk_plug_set_is_child (GtkPlug *plug,
}
}
/**
* gtk_plug_get_id:
* @plug: a #GtkPlug.
*
* Gets the window ID of a #GtkPlug widget, which can then
* be used to embed this window inside another window, for
* instance with gtk_socket_add_id().
*
* Return value: the window ID for the plug
**/
GdkNativeWindow
gtk_plug_get_id (GtkPlug *plug)
{
g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
if (!GTK_WIDGET_REALIZED (plug))
gtk_widget_realize (GTK_WIDGET (plug));
return _gtk_plug_windowing_get_id (plug);
}
/**
* _gtk_plug_add_to_socket:
* @plug: a #GtkPlug
......@@ -244,8 +257,16 @@ _gtk_plug_add_to_socket (GtkPlug *plug,
g_signal_emit_by_name (socket, "plug_added", 0);
}
static void
send_delete_event (GtkWidget *widget)
/**
* _gtk_plug_send_delete_event:
* widget: a #GtkWidget
*
* Send a GDK_DELETE event to the @widget and destroy it if
* necessary. Internal GTK function, called from this file or the
* backend-specific GtkPlug implementation.
*/
void
_gtk_plug_send_delete_event (GtkWidget *widget)
{
GdkEvent *event = gdk_event_new (GDK_DELETE);
......@@ -265,7 +286,7 @@ send_delete_event (GtkWidget *widget)
/**
* _gtk_plug_remove_from_socket:
* @plug: a #GtkPlug
* @socket_: a #GtkSocket
* @socket: a #GtkSocket
*
* Removes a plug from a socket within the same application.
**/
......@@ -283,17 +304,19 @@ _gtk_plug_remove_from_socket (GtkPlug *plug,
widget = GTK_WIDGET (plug);
if (GTK_WIDGET_IN_REPARENT (widget))
return;
g_object_ref (plug);
g_object_ref (socket);
widget_was_visible = GTK_WIDGET_VISIBLE (plug);
gdk_window_hide (widget->window);
GTK_PRIVATE_SET_FLAG (plug, GTK_IN_REPARENT);
gdk_window_reparent (widget->window,
gtk_widget_get_root_window (widget),
0, 0);
GTK_PRIVATE_SET_FLAG (plug, GTK_IN_REPARENT);
gtk_widget_unparent (GTK_WIDGET (plug));
GTK_PRIVATE_UNSET_FLAG (plug, GTK_IN_REPARENT);
......@@ -315,7 +338,8 @@ _gtk_plug_remove_from_socket (GtkPlug *plug,
if (!result)
gtk_widget_destroy (GTK_WIDGET (socket));
send_delete_event (widget);
if (widget->window)
_gtk_plug_send_delete_event (widget);
g_object_unref (plug);
......@@ -423,27 +447,6 @@ gtk_plug_new_for_display (GdkDisplay *display,
return GTK_WIDGET (plug);
}
/**
* gtk_plug_get_id:
* @plug: a #GtkPlug.
*
* Gets the window ID of a #GtkPlug widget, which can then
* be used to embed this window inside another window, for
* instance with gtk_socket_add_id().
*
* Return value: the window ID for the plug
**/
GdkNativeWindow
gtk_plug_get_id (GtkPlug *plug)
{
g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
if (!GTK_WIDGET_REALIZED (plug))
gtk_widget_realize (GTK_WIDGET (plug));
return GDK_WINDOW_XWINDOW (GTK_WIDGET (plug)->window);
}
static void
gtk_plug_finalize (GObject *object)
{
......@@ -477,7 +480,7 @@ gtk_plug_unrealize (GtkWidget *widget)
if (!plug->same_app)
{
if (plug->modality_window)
handle_modality_off (plug);
_gtk_plug_handle_modality_off (plug);
gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
g_object_unref (plug->modality_group);
......@@ -548,12 +551,14 @@ gtk_plug_realize (GtkWidget *widget)
&attributes, attributes_mask);
}
gdk_window_add_filter (widget->window, gtk_plug_filter_func, widget);
gdk_window_add_filter (widget->window,
_gtk_plug_windowing_filter_func,
widget);
plug->modality_group = gtk_window_group_new ();
gtk_window_group_add_window (plug->modality_group, window);
xembed_set_info (widget->window, 0);
_gtk_plug_windowing_realize_toplevel (plug);
}
else
widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
......@@ -596,6 +601,7 @@ gtk_plug_map (GtkWidget *widget)
if (GTK_WIDGET_TOPLEVEL (widget))
{
GtkBin *bin = GTK_BIN (widget);
GtkPlug *plug = GTK_PLUG (widget);
GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
......@@ -604,7 +610,7 @@ gtk_plug_map (GtkWidget *widget)
!GTK_WIDGET_MAPPED (bin->child))
gtk_widget_map (bin->child);
xembed_set_info (widget->window, XEMBED_MAPPED);
_gtk_plug_windowing_map_toplevel (plug);
gdk_synthesize_window_state (widget->window,
GDK_WINDOW_STATE_WITHDRAWN,
......@@ -619,10 +625,13 @@ gtk_plug_unmap (GtkWidget *widget)
{
if (GTK_WIDGET_TOPLEVEL (widget))
{
GtkPlug *plug = GTK_PLUG (widget);
GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
gdk_window_hide (widget->window);
xembed_set_info (widget->window, 0);
_gtk_plug_windowing_unmap_toplevel (plug);
gdk_synthesize_window_state (widget->window,
0,
......@@ -698,18 +707,9 @@ gtk_plug_set_focus (GtkWindow *window,
*/
if (focus && !window->has_toplevel_focus)
{
_gtk_xembed_send_message (plug->socket_window,
XEMBED_REQUEST_FOCUS, 0, 0, 0);
}
_gtk_plug_windowing_set_focus (plug);
}
typedef struct
{
guint accelerator_key;
GdkModifierType accelerator_mods;
} GrabbedKey;
static guint
grabbed_key_hash (gconstpointer a)
{
......@@ -742,19 +742,38 @@ add_grabbed_key (gpointer key, gpointer val, gpointer data)
if (!plug->grabbed_keys ||
!g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
{
_gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_GRAB_KEY, 0,
grabbed_key->accelerator_key, grabbed_key->accelerator_mods);
_gtk_plug_windowing_add_grabbed_key (plug,
grabbed_key->accelerator_key,
grabbed_key->accelerator_mods);
}
}
static void
add_grabbed_key_always (gpointer key, gpointer val, gpointer data)
add_grabbed_key_always (gpointer key,
gpointer val,
gpointer data)
{
GrabbedKey *grabbed_key = key;
GtkPlug *plug = data;
_gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_GRAB_KEY, 0,
grabbed_key->accelerator_key, grabbed_key->accelerator_mods);
_gtk_plug_windowing_add_grabbed_key (plug,
grabbed_key->accelerator_key,
grabbed_key->accelerator_mods);
}
/**
* _gtk_plug_add_all_grabbed_keys:
*
* @plug: a #GtkPlug
*
* Calls _gtk_plug_windowing_add_grabbed_key() on all the grabbed keys
* in the @plug.
*/
void
_gtk_plug_add_all_grabbed_keys (GtkPlug *plug)
{
if (plug->grabbed_keys)
g_hash_table_foreach (plug->grabbed_keys, add_grabbed_key_always, plug);
}
static void
......@@ -766,8 +785,9 @@ remove_grabbed_key (gpointer key, gpointer val, gpointer data)
if (!plug->grabbed_keys ||
!g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
{
_gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
grabbed_key->accelerator_key, grabbed_key->accelerator_mods);
_gtk_plug_windowing_remove_grabbed_key (plug,
grabbed_key->accelerator_key,
grabbed_key->accelerator_mods);
}
}
......@@ -810,29 +830,6 @@ gtk_plug_keys_changed (GtkWindow *window)
}
}
static void
focus_to_parent (GtkPlug *plug,
GtkDirectionType direction)
{
XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
switch (direction)
{
case GTK_DIR_UP:
case GTK_DIR_LEFT:
case GTK_DIR_TAB_BACKWARD:
message = XEMBED_FOCUS_PREV;
break;
case GTK_DIR_DOWN:
case GTK_DIR_RIGHT:
case GTK_DIR_TAB_FORWARD:
message = XEMBED_FOCUS_NEXT;
break;
}
_gtk_xembed_send_focus_message (plug->socket_window, message, 0);
}
static gboolean
gtk_plug_focus (GtkWidget *widget,
GtkDirectionType direction)
......@@ -872,7 +869,7 @@ gtk_plug_focus (GtkWidget *widget,
}
if (!GTK_CONTAINER (window)->focus_child)
focus_to_parent (plug, direction);
_gtk_plug_windowing_focus_to_parent (plug, direction);
return FALSE;
}
......@@ -886,30 +883,16 @@ gtk_plug_check_resize (GtkContainer *container)
GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
}
static void
focus_first_last (GtkPlug *plug,
GtkDirectionType direction)
{
GtkWindow *window = GTK_WINDOW (plug);
GtkWidget *parent;
if (window->focus_widget)
{
parent = window->focus_widget->parent;
while (parent)
{
gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
parent = GTK_WIDGET (parent)->parent;
}
gtk_window_set_focus (GTK_WINDOW (plug), NULL);
}
gtk_widget_child_focus (GTK_WIDGET (plug), direction);
}
static void
handle_modality_on (GtkPlug *plug)
/**
* _gtk_plug_handle_modality_on:
*
* @plug: a #GtkPlug
*
* Called from the GtkPlug backend when the corresponding socket has
* told the plug that it modality has toggled on.
*/
void
_gtk_plug_handle_modality_on (GtkPlug *plug)
{
if (!plug->modality_window)
{
......@@ -922,8 +905,16 @@ handle_modality_on (GtkPlug *plug)
}
}
static void
handle_modality_off (GtkPlug *plug)
/**
* _gtk_plug_handle_modality_off:
*
* @plug: a #GtkPlug
*
* Called from the GtkPlug backend when the corresponding socket has
* told the plug that it modality has toggled off.
*/
void
_gtk_plug_handle_modality_off (GtkPlug *plug)
{
if (plug->modality_window)
{
......@@ -932,212 +923,35 @@ handle_modality_off (GtkPlug *plug)
}
}
static void
xembed_set_info (GdkWindow *window,
unsigned long flags)
{
GdkDisplay *display = gdk_drawable_get_display (window);
unsigned long buffer[2];
Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
buffer[1] = flags;
XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
GDK_WINDOW_XWINDOW (window),
xembed_info_atom, xembed_info_atom, 32,
PropModeReplace,
(unsigned char *)buffer, 2);
}
static void
handle_xembed_message (GtkPlug *plug,
XEmbedMessageType message,
glong detail,
glong data1,
glong data2,
guint32 time)
/**
* _gtk_plug_focus_first_last:
*
* @plug: a #GtkPlug
* @direction: a direction
*
* Called from the GtkPlug backend when the corresponding socket has
* told the plug that it has received the focus.
*/
void
_gtk_plug_focus_first_last (GtkPlug *plug,
GtkDirectionType direction)
{
GtkWindow *window = GTK_WINDOW (plug);
GtkWidget *parent;
GTK_NOTE (PLUGSOCKET,
g_message ("GtkPlug: Message of type %d received", message));
switch (message)
if (window->focus_widget)
{
case XEMBED_EMBEDDED_NOTIFY:
break;
case XEMBED_WINDOW_ACTIVATE:
_gtk_window_set_is_active (window, TRUE);
break;
case XEMBED_WINDOW_DEACTIVATE:
_gtk_window_set_is_active (window, FALSE);
break;
case XEMBED_MODALITY_ON:
handle_modality_on (plug);
break;
case XEMBED_MODALITY_OFF:
handle_modality_off (plug);
break;
case XEMBED_FOCUS_IN:
_gtk_window_set_has_toplevel_focus (window, TRUE);
switch (detail)
parent = window->focus_widget->parent;
while (parent)
{
case XEMBED_FOCUS_FIRST:
focus_first_last (plug, GTK_DIR_TAB_FORWARD);
break;
case XEMBED_FOCUS_LAST:
focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
break;
case XEMBED_FOCUS_CURRENT:
break;
gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
parent = GTK_WIDGET (parent)->parent;
}
break;
case XEMBED_FOCUS_OUT:
_gtk_window_set_has_toplevel_focus (window, FALSE);
break;
case XEMBED_GRAB_KEY:
case XEMBED_UNGRAB_KEY:
case XEMBED_GTK_GRAB_KEY:
case XEMBED_GTK_UNGRAB_KEY:
case XEMBED_REQUEST_FOCUS:
case XEMBED_FOCUS_NEXT:
case XEMBED_FOCUS_PREV:
g_warning ("GtkPlug: Invalid _XEMBED message of type %d received", message);
break;
default:
GTK_NOTE(PLUGSOCKET,
g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
break;
}
}
static GdkFilterReturn
gtk_plug_filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
{
GdkScreen *screen = gdk_drawable_get_screen (event->any.window);
GdkDisplay *display = gdk_screen_get_display (screen);
GtkPlug *plug = GTK_PLUG (data);
XEvent *xevent = (XEvent *)gdk_xevent;
GdkFilterReturn return_val;
return_val = GDK_FILTER_CONTINUE;
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 (plug,
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 GDK_FILTER_REMOVE;
}
else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
{
/* We filter these out because we take being reparented back to the
* root window as the reliable end of the embedding protocol
*/
return GDK_FILTER_REMOVE;
}
break;
case ReparentNotify:
{
XReparentEvent *xre = &xevent->xreparent;
gboolean was_embedded = plug->socket_window != NULL;
return_val = GDK_FILTER_REMOVE;
g_object_ref (plug);
if (was_embedded)
{
/* End of embedding protocol for previous socket */
/* FIXME: race if we remove from another socket and
* then add to a local window before we get notification
* Probably need check in _gtk_plug_add_to_socket
*/
if (xre->parent != GDK_WINDOW_XWINDOW (plug->socket_window))
{
GtkWidget *widget = GTK_WIDGET (plug);
gdk_window_set_user_data (plug->socket_window, NULL);
g_object_unref (plug->socket_window);
plug->socket_window = NULL;
/* Emit a delete window, as if the user attempted
* to close the toplevel. Simple as to how we
* handle WM_DELETE_WINDOW, if it isn't handled
* we destroy the widget. BUt only do this if
* we are being reparented to the root window.
* Moving from one embedder to another should
* be invisible to the app.
*/
if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
send_delete_event (widget);
}
else
goto done;
}
if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
{
/* Start of embedding protocol */
plug->socket_window = gdk_window_lookup_for_display (display, xre->parent);
if (plug->socket_window)
{
gpointer user_data = NULL;
gdk_window_get_user_data (plug->socket_window, &user_data);
if (user_data)
{
g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
plug->socket_window = NULL;
break;
}
g_object_ref (plug->socket_window);
}
else
{
plug->socket_window = gdk_window_foreign_new_for_display (display, xre->parent);
if (!plug->socket_window) /* Already gone */
break;
}
if (plug->grabbed_keys)
g_hash_table_foreach (plug->grabbed_keys, add_grabbed_key_always, plug);
if (!was_embedded)
g_signal_emit (plug, plug_signals[EMBEDDED], 0);
}
done:
g_object_unref (plug);
break;
}
gtk_window_set_focus (GTK_WINDOW (plug), NULL);
}
return GDK_FILTER_CONTINUE;
gtk_widget_child_focus (GTK_WIDGET (plug), direction);
}
#define __GTK_PLUG_C__
......
/* 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, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* 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/.
*/
#ifndef __GTK_PLUG_PRIVATE_H__
#define __GTK_PLUG_PRIVATE_H__
/* In gtkplug.c: */
void _gtk_plug_send_delete_event (GtkWidget *widget);
void _gtk_plug_add_all_grabbed_keys (GtkPlug *plug);
void _gtk_plug_focus_first_last (GtkPlug *plug,
GtkDirectionType direction);
void _gtk_plug_handle_modality_on (GtkPlug *plug);
void _gtk_plug_handle_modality_off (GtkPlug *plug);
/* In backend-specific file: */
/**
* _gtk_plug_windowing_get_id:
*
* @plug: a #GtkPlug
*
* Returns the native window system identifier for the plug's window.
*/
GdkNativeWindow _gtk_plug_windowing_get_id (GtkPlug *plug);
/**
* _gtk_plug_windowing_realize_toplevel:
*
* @plug_window: a #GtkPlug's #GdkWindow
*
* Called from GtkPlug's realize method. Should tell the corresponding
* socket that the plug has been realized.
*/
void _gtk_plug_windowing_realize_toplevel (GtkPlug *plug);
/**
* _gtk_plug_windowing_map_toplevel:
*
* @plug: a #GtkPlug
*
* Called from GtkPlug's map method. Should tell the corresponding
* #GtkSocket that the plug has been mapped.
*/
void _gtk_plug_windowing_map_toplevel (GtkPlug *plug);
/**
* _gtk_plug_windowing_map_toplevel:
*
* @plug: a #GtkPlug
*
* Called from GtkPlug's unmap method. Should tell the corresponding
* #GtkSocket that the plug has been unmapped.
*/
void _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug);
/**
* _gtk_plug_windowing_set_focus:
*
* @plug: a #GtkPlug
*
* Called from GtkPlug's set_focus method. Should tell the corresponding
* #GtkSocket to request focus.
*/
void _gtk_plug_windowing_set_focus (GtkPlug *plug);
/**
* _gtk_plug_windowing_add_grabbed_key:
*
* @plug: a #GtkPlug
* @accelerator_key: a key
* @accelerator_mods: modifiers for it
*
* Called from GtkPlug's keys_changed method. Should tell the
* corresponding #GtkSocket to grab the key.
*/
void _gtk_plug_windowing_add_grabbed_key (GtkPlug *plug,
guint accelerator_key,
GdkModifierType accelerator_mods);
/**
* _gtk_plug_windowing_remove_grabbed_key:
*