1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Modified by the GTK+ Team and others 1997-2006. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkmarshalers.h"
30 #include "gtksizerequest.h"
31 #include "gtkwin32embedwidget.h"
33 #include "gtkprivate.h"
36 static void gtk_win32_embed_widget_realize (GtkWidget *widget);
37 static void gtk_win32_embed_widget_unrealize (GtkWidget *widget);
38 static void gtk_win32_embed_widget_show (GtkWidget *widget);
39 static void gtk_win32_embed_widget_hide (GtkWidget *widget);
40 static void gtk_win32_embed_widget_map (GtkWidget *widget);
41 static void gtk_win32_embed_widget_unmap (GtkWidget *widget);
42 static void gtk_win32_embed_widget_size_allocate (GtkWidget *widget,
43 GtkAllocation *allocation);
44 static void gtk_win32_embed_widget_set_focus (GtkWindow *window,
46 static gboolean gtk_win32_embed_widget_focus (GtkWidget *widget,
47 GtkDirectionType direction);
48 static void gtk_win32_embed_widget_check_resize (GtkContainer *container);
50 static GtkBinClass *bin_class = NULL;
52 G_DEFINE_TYPE (GtkWin32EmbedWidget, gtk_win32_embed_widget, GTK_TYPE_WINDOW)
55 gtk_win32_embed_widget_class_init (GtkWin32EmbedWidgetClass *class)
57 GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
58 GtkWindowClass *window_class = (GtkWindowClass *)class;
59 GtkContainerClass *container_class = (GtkContainerClass *)class;
61 bin_class = g_type_class_peek (GTK_TYPE_BIN);
63 widget_class->realize = gtk_win32_embed_widget_realize;
64 widget_class->unrealize = gtk_win32_embed_widget_unrealize;
66 widget_class->show = gtk_win32_embed_widget_show;
67 widget_class->hide = gtk_win32_embed_widget_hide;
68 widget_class->map = gtk_win32_embed_widget_map;
69 widget_class->unmap = gtk_win32_embed_widget_unmap;
70 widget_class->size_allocate = gtk_win32_embed_widget_size_allocate;
72 widget_class->focus = gtk_win32_embed_widget_focus;
74 container_class->check_resize = gtk_win32_embed_widget_check_resize;
76 window_class->set_focus = gtk_win32_embed_widget_set_focus;
80 gtk_win32_embed_widget_init (GtkWin32EmbedWidget *embed_widget)
84 window = GTK_WINDOW (embed_widget);
86 _gtk_widget_set_is_toplevel (GTK_WIDGET (embed_widget), TRUE);
87 gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
91 _gtk_win32_embed_widget_new (GdkNativeWindow parent_id)
93 GtkWin32EmbedWidget *embed_widget;
95 embed_widget = g_object_new (GTK_TYPE_WIN32_EMBED_WIDGET, NULL);
97 embed_widget->parent_window =
98 gdk_window_lookup_for_display (gdk_display_get_default (),
101 if (!embed_widget->parent_window)
102 embed_widget->parent_window =
103 gdk_window_foreign_new_for_display (gdk_display_get_default (),
106 return GTK_WIDGET (embed_widget);
110 _gtk_win32_embed_widget_dialog_procedure (GtkWin32EmbedWidget *embed_widget,
111 HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
113 GtkAllocation allocation;
114 GtkWidget *widget = GTK_WIDGET (embed_widget);
116 if (message == WM_SIZE)
118 allocation.width = LOWORD(lparam);
119 allocation.height = HIWORD(lparam);
120 gtk_widget_set_allocation (widget, &allocation);
122 gtk_widget_queue_resize (widget);
129 gtk_win32_embed_widget_unrealize (GtkWidget *widget)
131 GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
133 embed_widget->old_window_procedure = NULL;
135 if (embed_widget->parent_window != NULL)
137 gdk_window_set_user_data (embed_widget->parent_window, NULL);
138 g_object_unref (embed_widget->parent_window);
139 embed_widget->parent_window = NULL;
142 GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize (widget);
145 static LRESULT CALLBACK
146 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
149 GtkWin32EmbedWidget *embed_widget;
152 window = gdk_window_lookup ((GdkNativeWindow)hwnd);
153 if (window == NULL) {
154 g_warning ("No such window!");
157 gdk_window_get_user_data (window, &user_data);
158 embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
160 if (msg == WM_GETDLGCODE) {
161 return DLGC_WANTALLKEYS;
164 if (embed_widget && embed_widget->old_window_procedure)
165 return CallWindowProc(embed_widget->old_window_procedure,
166 hwnd, msg, wparam, lparam);
172 gtk_win32_embed_widget_realize (GtkWidget *widget)
174 GtkWindow *window = GTK_WINDOW (widget);
175 GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
176 GtkAllocation allocation;
177 GdkWindow *gdk_window;
178 GdkWindowAttr attributes;
179 gint attributes_mask;
182 gtk_widget_get_allocation (widget, &allocation);
184 /* ensure widget tree is properly size allocated */
185 if (allocation.x == -1 && allocation.y == -1 &&
186 allocation.width == 1 && allocation.height == 1)
188 GtkRequisition requisition;
189 GtkAllocation allocation = { 0, 0, 200, 200 };
191 gtk_widget_get_preferred_size (widget, &requisition, NULL);
192 if (requisition.width || requisition.height)
194 /* non-empty window */
195 allocation.width = requisition.width;
196 allocation.height = requisition.height;
198 gtk_widget_size_allocate (widget, &allocation);
200 _gtk_container_queue_resize (GTK_CONTAINER (widget));
202 g_return_if_fail (!gtk_widget_get_realized (widget));
205 gtk_widget_set_realized (widget, TRUE);
207 gtk_widget_get_allocation (widget, &allocation);
209 attributes.window_type = GDK_WINDOW_CHILD;
210 attributes.title = gtk_window_get_title (window);
211 _gtk_window_get_wmclass (window, &attributes.wmclass_name, &attributes.wmclass_class);
212 attributes.width = allocation.width;
213 attributes.height = allocation.height;
214 attributes.wclass = GDK_INPUT_OUTPUT;
216 /* this isn't right - we should match our parent's visual/colormap.
217 * though that will require handling "foreign" colormaps */
218 attributes.visual = gtk_widget_get_visual (widget);
219 attributes.event_mask = gtk_widget_get_events (widget);
220 attributes.event_mask |= (GDK_EXPOSURE_MASK |
222 GDK_KEY_RELEASE_MASK |
223 GDK_ENTER_NOTIFY_MASK |
224 GDK_LEAVE_NOTIFY_MASK |
226 GDK_FOCUS_CHANGE_MASK);
228 attributes_mask = GDK_WA_VISUAL;
229 attributes_mask |= (gtk_window_get_title (window) ? GDK_WA_TITLE : 0);
230 attributes_mask |= (attributes.wmclass_name ? GDK_WA_WMCLASS : 0);
232 gdk_window = gdk_window_new (embed_widget->parent_window,
233 &attributes, attributes_mask);
234 gtk_widget_set_window (widget, gdk_window);
235 gdk_window_set_user_data (gdk_window, window);
237 embed_widget->old_window_procedure = (gpointer)
238 SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window),
240 (LONG_PTR)gtk_win32_embed_widget_window_process);
242 /* Enable tab to focus the widget */
243 styles = GetWindowLongPtr(GDK_WINDOW_HWND (gdk_window), GWL_STYLE);
244 SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window), GWL_STYLE, styles | WS_TABSTOP);
246 gtk_widget_style_attach (widget);
247 gtk_style_set_background (gtk_widget_get_style (widget), gdk_window, GTK_STATE_NORMAL);
251 gtk_win32_embed_widget_show (GtkWidget *widget)
253 gtk_widget_set_visible (widget, TRUE);
255 gtk_widget_realize (widget);
256 gtk_container_check_resize (GTK_CONTAINER (widget));
257 gtk_widget_map (widget);
261 gtk_win32_embed_widget_hide (GtkWidget *widget)
263 gtk_widget_set_visible (widget, FALSE);
264 gtk_widget_unmap (widget);
268 gtk_win32_embed_widget_map (GtkWidget *widget)
270 GtkBin *bin = GTK_BIN (widget);
273 gtk_widget_set_mapped (widget, TRUE);
275 child = gtk_bin_get_child (bin);
277 gtk_widget_get_visible (child) &&
278 !gtk_widget_get_mapped (child))
279 gtk_widget_map (child);
281 gdk_window_show (gtk_widget_get_window (widget));
285 gtk_win32_embed_widget_unmap (GtkWidget *widget)
287 gtk_widget_set_mapped (widget, FALSE);
288 gdk_window_hide (gtk_widget_get_window (widget));
292 gtk_win32_embed_widget_size_allocate (GtkWidget *widget,
293 GtkAllocation *allocation)
295 GtkBin *bin = GTK_BIN (widget);
298 gtk_widget_set_allocation (widget, allocation);
300 if (gtk_widget_get_realized (widget))
301 gdk_window_move_resize (gtk_widget_get_window (widget),
302 allocation->x, allocation->y,
303 allocation->width, allocation->height);
305 child = gtk_bin_get_child (bin);
306 if (child && gtk_widget_get_visible (child))
308 GtkAllocation child_allocation;
310 child_allocation.x = gtk_container_get_border_width (GTK_CONTAINER (widget));
311 child_allocation.y = child_allocation.x;
312 child_allocation.width =
313 MAX (1, (gint)allocation->width - child_allocation.x * 2);
314 child_allocation.height =
315 MAX (1, (gint)allocation->height - child_allocation.y * 2);
317 gtk_widget_size_allocate (child, &child_allocation);
322 gtk_win32_embed_widget_check_resize (GtkContainer *container)
324 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
328 gtk_win32_embed_widget_focus (GtkWidget *widget,
329 GtkDirectionType direction)
331 GtkBin *bin = GTK_BIN (widget);
332 GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
333 GtkWindow *window = GTK_WINDOW (widget);
334 GtkContainer *container = GTK_CONTAINER (widget);
335 GtkWidget *old_focus_child = gtk_container_get_focus_child (container);
339 /* We override GtkWindow's behavior, since we don't want wrapping here.
343 if (gtk_widget_child_focus (old_focus_child, direction))
346 if (gtk_window_get_focus (window))
348 /* Wrapped off the end, clear the focus setting for the toplevel */
349 parent = gtk_widget_get_parent (gtk_window_get_focus (window));
352 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
353 parent = gtk_widget_get_parent (GTK_WIDGET (parent));
356 gtk_window_set_focus (GTK_WINDOW (container), NULL);
361 /* Try to focus the first widget in the window */
362 child = gtk_bin_get_child (bin);
363 if (child && gtk_widget_child_focus (child, direction))
367 if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
369 int backwards = FALSE;
371 if (direction == GTK_DIR_TAB_BACKWARD ||
372 direction == GTK_DIR_LEFT)
375 PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
384 gtk_win32_embed_widget_set_focus (GtkWindow *window,
387 GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
389 gdk_window_focus (gtk_widget_get_window (GTK_WIDGET(window)), 0);