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 "gtkwin32embedwidget.h"
32 #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 window->type = GTK_WINDOW_TOPLEVEL;
88 GTK_WIDGET_SET_FLAGS (embed_widget, GTK_TOPLEVEL);
89 gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
93 _gtk_win32_embed_widget_new (GdkNativeWindow parent_id)
95 GtkWin32EmbedWidget *embed_widget;
97 embed_widget = g_object_new (GTK_TYPE_WIN32_EMBED_WIDGET, NULL);
99 embed_widget->parent_window =
100 gdk_window_lookup_for_display (gdk_display_get_default (),
103 if (!embed_widget->parent_window)
104 embed_widget->parent_window =
105 gdk_window_foreign_new_for_display (gdk_display_get_default (),
108 return GTK_WIDGET (embed_widget);
112 _gtk_win32_embed_widget_dialog_procedure (GtkWin32EmbedWidget *embed_widget,
113 HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
115 GtkWidget *widget = GTK_WIDGET (embed_widget);
117 if (message == WM_SIZE)
119 widget->allocation.width = LOWORD(lparam);
120 widget->allocation.height = HIWORD(lparam);
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 if (GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize)
143 (* GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize) (widget);
146 static LRESULT CALLBACK
147 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
150 GtkWin32EmbedWidget *embed_widget;
153 window = gdk_window_lookup ((GdkNativeWindow)hwnd);
154 if (window == NULL) {
155 g_warning ("No such window!");
158 gdk_window_get_user_data (window, &user_data);
159 embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
161 if (msg == WM_GETDLGCODE) {
162 return DLGC_WANTALLKEYS;
165 if (embed_widget && embed_widget->old_window_procedure)
166 return CallWindowProc(embed_widget->old_window_procedure,
167 hwnd, msg, wparam, lparam);
173 gtk_win32_embed_widget_realize (GtkWidget *widget)
175 GtkWindow *window = GTK_WINDOW (widget);
176 GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
177 GdkWindowAttr attributes;
178 gint attributes_mask;
181 /* ensure widget tree is properly size allocated */
182 if (widget->allocation.x == -1 &&
183 widget->allocation.y == -1 &&
184 widget->allocation.width == 1 &&
185 widget->allocation.height == 1)
187 GtkRequisition requisition;
188 GtkAllocation allocation = { 0, 0, 200, 200 };
190 gtk_widget_size_request (widget, &requisition);
191 if (requisition.width || requisition.height)
193 /* non-empty window */
194 allocation.width = requisition.width;
195 allocation.height = requisition.height;
197 gtk_widget_size_allocate (widget, &allocation);
199 _gtk_container_queue_resize (GTK_CONTAINER (widget));
201 g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
204 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
206 attributes.window_type = GDK_WINDOW_CHILD;
207 attributes.title = window->title;
208 attributes.wmclass_name = window->wmclass_name;
209 attributes.wmclass_class = window->wmclass_class;
210 attributes.width = widget->allocation.width;
211 attributes.height = widget->allocation.height;
212 attributes.wclass = GDK_INPUT_OUTPUT;
214 /* this isn't right - we should match our parent's visual/colormap.
215 * though that will require handling "foreign" colormaps */
216 attributes.visual = gtk_widget_get_visual (widget);
217 attributes.colormap = gtk_widget_get_colormap (widget);
218 attributes.event_mask = gtk_widget_get_events (widget);
219 attributes.event_mask |= (GDK_EXPOSURE_MASK |
221 GDK_KEY_RELEASE_MASK |
222 GDK_ENTER_NOTIFY_MASK |
223 GDK_LEAVE_NOTIFY_MASK |
225 GDK_FOCUS_CHANGE_MASK);
227 attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
228 attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
229 attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
231 widget->window = gdk_window_new (embed_widget->parent_window,
232 &attributes, attributes_mask);
234 gdk_window_set_user_data (widget->window, window);
236 embed_widget->old_window_procedure = (gpointer)
237 SetWindowLongPtrW(GDK_WINDOW_HWND (widget->window),
239 (LONG_PTR)gtk_win32_embed_widget_window_process);
241 /* Enable tab to focus the widget */
242 styles = GetWindowLongPtr(GDK_WINDOW_HWND (widget->window), GWL_STYLE);
243 SetWindowLongPtrW(GDK_WINDOW_HWND (widget->window), GWL_STYLE, styles | WS_TABSTOP);
245 widget->style = gtk_style_attach (widget->style, widget->window);
246 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
250 gtk_win32_embed_widget_show (GtkWidget *widget)
252 GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
254 gtk_widget_realize (widget);
255 gtk_container_check_resize (GTK_CONTAINER (widget));
256 gtk_widget_map (widget);
260 gtk_win32_embed_widget_hide (GtkWidget *widget)
262 GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
263 gtk_widget_unmap (widget);
267 gtk_win32_embed_widget_map (GtkWidget *widget)
269 GtkBin *bin = GTK_BIN (widget);
271 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
274 GTK_WIDGET_VISIBLE (bin->child) &&
275 !GTK_WIDGET_MAPPED (bin->child))
276 gtk_widget_map (bin->child);
278 gdk_window_show (widget->window);
282 gtk_win32_embed_widget_unmap (GtkWidget *widget)
284 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
285 gdk_window_hide (widget->window);
289 gtk_win32_embed_widget_size_allocate (GtkWidget *widget,
290 GtkAllocation *allocation)
292 GtkBin *bin = GTK_BIN (widget);
294 widget->allocation = *allocation;
296 if (GTK_WIDGET_REALIZED (widget))
297 gdk_window_move_resize (widget->window,
298 allocation->x, allocation->y,
299 allocation->width, allocation->height);
301 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
303 GtkAllocation child_allocation;
305 child_allocation.x = child_allocation.y = GTK_CONTAINER (widget)->border_width;
306 child_allocation.width =
307 MAX (1, (gint)allocation->width - child_allocation.x * 2);
308 child_allocation.height =
309 MAX (1, (gint)allocation->height - child_allocation.y * 2);
311 gtk_widget_size_allocate (bin->child, &child_allocation);
316 gtk_win32_embed_widget_check_resize (GtkContainer *container)
318 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
322 gtk_win32_embed_widget_focus (GtkWidget *widget,
323 GtkDirectionType direction)
325 GtkBin *bin = GTK_BIN (widget);
326 GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
327 GtkWindow *window = GTK_WINDOW (widget);
328 GtkContainer *container = GTK_CONTAINER (widget);
329 GtkWidget *old_focus_child = container->focus_child;
332 /* We override GtkWindow's behavior, since we don't want wrapping here.
336 if (gtk_widget_child_focus (old_focus_child, direction))
339 if (window->focus_widget)
341 /* Wrapped off the end, clear the focus setting for the toplevel */
342 parent = window->focus_widget->parent;
345 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
346 parent = GTK_WIDGET (parent)->parent;
349 gtk_window_set_focus (GTK_WINDOW (container), NULL);
354 /* Try to focus the first widget in the window */
355 if (bin->child && gtk_widget_child_focus (bin->child, direction))
359 if (!GTK_CONTAINER (window)->focus_child)
361 int backwards = FALSE;
363 if (direction == GTK_DIR_TAB_BACKWARD ||
364 direction == GTK_DIR_LEFT)
367 PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
376 gtk_win32_embed_widget_set_focus (GtkWindow *window,
379 GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
381 gdk_window_focus (GTK_WIDGET(window)->window, 0);
384 #define __GTK_WIN32_EMBED_WIDGET_C__
385 #include "gtkaliasdef.c"