1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 2005 Novell, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* By Tor Lillqvist <tml@novell.com> 2005 */
23 * Modified by the GTK+ Team and others 1997-2005. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkmarshalers.h"
31 #include "gtkplugprivate.h"
33 #include "win32/gdkwin32.h"
35 #include "gtkwin32embed.h"
37 #if defined(_MSC_VER) && (WINVER < 0x0500)
41 WINUSERAPI HWND WINAPI GetAncestor(HWND,UINT);
45 _gtk_plug_windowing_get_id (GtkPlug *plug)
47 return (GdkNativeWindow) GDK_WINDOW_HWND (GTK_WIDGET (plug)->window);
51 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
53 if (plug->socket_window)
55 _gtk_win32_embed_send (plug->socket_window,
56 GTK_WIN32_EMBED_PARENT_NOTIFY,
57 (WPARAM) GDK_WINDOW_HWND (GTK_WIDGET (plug)->window),
58 GTK_WIN32_EMBED_PROTOCOL_VERSION);
59 _gtk_win32_embed_send (plug->socket_window,
60 GTK_WIN32_EMBED_EVENT_PLUG_MAPPED, 0, 0);
65 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
67 if (plug->socket_window)
68 _gtk_win32_embed_send (plug->socket_window,
69 GTK_WIN32_EMBED_EVENT_PLUG_MAPPED,
74 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
76 if (plug->socket_window)
77 _gtk_win32_embed_send (plug->socket_window,
78 GTK_WIN32_EMBED_EVENT_PLUG_MAPPED,
83 _gtk_plug_windowing_set_focus (GtkPlug *plug)
85 if (plug->socket_window)
86 _gtk_win32_embed_send (plug->socket_window,
87 GTK_WIN32_EMBED_REQUEST_FOCUS,
92 _gtk_plug_windowing_add_grabbed_key (GtkPlug *plug,
93 guint accelerator_key,
94 GdkModifierType accelerator_mods)
96 if (plug->socket_window)
97 _gtk_win32_embed_send (plug->socket_window,
98 GTK_WIN32_EMBED_GRAB_KEY,
99 accelerator_key, accelerator_mods);
103 _gtk_plug_windowing_remove_grabbed_key (GtkPlug *plug,
104 guint accelerator_key,
105 GdkModifierType accelerator_mods)
107 if (plug->socket_window)
108 _gtk_win32_embed_send (plug->socket_window,
109 GTK_WIN32_EMBED_UNGRAB_KEY,
110 accelerator_key, accelerator_mods);
114 _gtk_plug_windowing_focus_to_parent (GtkPlug *plug,
115 GtkDirectionType direction)
117 GtkWin32EmbedMessageType message = GTK_WIN32_EMBED_FOCUS_PREV;
123 case GTK_DIR_TAB_BACKWARD:
124 message = GTK_WIN32_EMBED_FOCUS_PREV;
128 case GTK_DIR_TAB_FORWARD:
129 message = GTK_WIN32_EMBED_FOCUS_NEXT;
133 _gtk_win32_embed_send_focus_message (plug->socket_window, message, 0);
137 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
141 GtkPlug *plug = GTK_PLUG (data);
142 MSG *msg = (MSG *) gdk_xevent;
143 GdkFilterReturn return_val = GDK_FILTER_CONTINUE;
145 switch (msg->message)
147 /* What message should we look for to notice the reparenting?
148 * Maybe WM_WINDOWPOSCHANGED will work? This is handled in the
149 * X11 implementation by handling ReparentNotify. Handle this
150 * only for cross-process embedding, otherwise we get odd
151 * crashes in testsocket.
153 case WM_WINDOWPOSCHANGED:
156 HWND parent = GetAncestor (msg->hwnd, GA_PARENT);
157 gboolean was_embedded = plug->socket_window != NULL;
158 GdkScreen *screen = gdk_drawable_get_screen (event->any.window);
159 GdkDisplay *display = gdk_screen_get_display (screen);
161 GTK_NOTE (PLUGSOCKET, g_printerr ("WM_WINDOWPOSCHANGED: hwnd=%p GA_PARENT=%p socket_window=%p\n", msg->hwnd, parent, plug->socket_window));
165 /* End of embedding protocol for previous socket */
166 if (parent != GDK_WINDOW_HWND (plug->socket_window))
168 GtkWidget *widget = GTK_WIDGET (plug);
170 GTK_NOTE (PLUGSOCKET, g_printerr ("was_embedded, current parent != socket_window\n"));
171 gdk_window_set_user_data (plug->socket_window, NULL);
172 g_object_unref (plug->socket_window);
173 plug->socket_window = NULL;
175 /* Emit a delete window, as if the user attempted to
176 * close the toplevel. Only do this if we are being
177 * reparented to the desktop window. Moving from one
178 * embedder to another should be invisible to the app.
180 if (parent == GetDesktopWindow ())
182 GTK_NOTE (PLUGSOCKET, g_printerr ("current parent is root window\n"));
183 _gtk_plug_send_delete_event (widget);
184 return_val = GDK_FILTER_REMOVE;
189 GTK_NOTE (PLUGSOCKET, g_printerr ("still same parent\n"));
194 if (parent != GetDesktopWindow ())
196 /* Start of embedding protocol */
198 GTK_NOTE (PLUGSOCKET, g_printerr ("start of embedding\n"));
199 plug->socket_window = gdk_window_lookup_for_display (display, (GdkNativeWindow) parent);
200 if (plug->socket_window)
202 gpointer user_data = NULL;
204 GTK_NOTE (PLUGSOCKET, g_printerr ("already had socket_window\n"));
205 gdk_window_get_user_data (plug->socket_window, &user_data);
209 g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
210 plug->socket_window = NULL;
214 g_object_ref (plug->socket_window);
218 plug->socket_window = gdk_window_foreign_new_for_display (display, (GdkNativeWindow) parent);
219 if (!plug->socket_window) /* Already gone */
223 _gtk_plug_add_all_grabbed_keys (plug);
226 g_signal_emit_by_name (plug, "embedded");
229 g_object_unref (plug);
234 if (!plug->same_app && plug->socket_window)
236 _gtk_win32_embed_send (plug->socket_window,
237 GTK_WIN32_EMBED_PLUG_RESIZED,
243 if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_WINDOW_ACTIVATE))
245 GTK_NOTE (PLUGSOCKET, g_printerr ("GtkPlug: WINDOW_ACTIVATE received\n"));
246 _gtk_win32_embed_push_message (msg);
247 _gtk_window_set_is_active (GTK_WINDOW (plug), TRUE);
248 _gtk_win32_embed_pop_message ();
249 return_val = GDK_FILTER_REMOVE;
251 else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_WINDOW_DEACTIVATE))
253 GTK_NOTE (PLUGSOCKET, g_printerr ("GtkPlug: WINDOW_DEACTIVATE received\n"));
254 _gtk_win32_embed_push_message (msg);
255 _gtk_window_set_is_active (GTK_WINDOW (plug), FALSE);
256 _gtk_win32_embed_pop_message ();
257 return_val = GDK_FILTER_REMOVE;
259 else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_FOCUS_IN))
261 GTK_NOTE (PLUGSOCKET, g_printerr ("GtkPlug: FOCUS_IN received\n"));
262 _gtk_win32_embed_push_message (msg);
263 _gtk_window_set_has_toplevel_focus (GTK_WINDOW (plug), TRUE);
266 case GTK_WIN32_EMBED_FOCUS_CURRENT:
268 case GTK_WIN32_EMBED_FOCUS_FIRST:
269 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
271 case GTK_WIN32_EMBED_FOCUS_LAST:
272 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
275 _gtk_win32_embed_pop_message ();
276 return_val = GDK_FILTER_REMOVE;
278 else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_FOCUS_OUT))
280 GTK_NOTE (PLUGSOCKET, g_printerr ("GtkPlug: FOCUS_OUT received\n"));
281 _gtk_win32_embed_push_message (msg);
282 _gtk_window_set_has_toplevel_focus (GTK_WINDOW (plug), FALSE);
283 _gtk_win32_embed_pop_message ();
284 return_val = GDK_FILTER_REMOVE;
286 else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_MODALITY_ON))
288 GTK_NOTE (PLUGSOCKET, g_printerr ("GtkPlug: MODALITY_ON received\n"));
289 _gtk_win32_embed_push_message (msg);
290 _gtk_plug_handle_modality_on (plug);
291 _gtk_win32_embed_pop_message ();
292 return_val = GDK_FILTER_REMOVE;
294 else if (msg->message == _gtk_win32_embed_message_type (GTK_WIN32_EMBED_MODALITY_OFF))
296 GTK_NOTE (PLUGSOCKET, g_printerr ("GtkPlug: MODALITY_OFF received\n"));
297 _gtk_win32_embed_push_message (msg);
298 _gtk_plug_handle_modality_off (plug);
299 _gtk_win32_embed_pop_message ();
300 return_val = GDK_FILTER_REMOVE;