1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
16  */
17 
18 /*
19  * Modified by the GTK+ Team and others 1997-2006.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 #include "config.h"
26 
27 #include "gtkmain.h"
28 #include "gtkmarshalers.h"
29 #include "gtksizerequest.h"
30 #include "gtkwin32embedwidget.h"
31 #include "gtkintl.h"
32 #include "gtkprivate.h"
33 #include "gtkwindowprivate.h"
34 #include "gtkwidgetprivate.h"
35 #include "gtkcontainerprivate.h"
36 
37 
38 static void            gtk_win32_embed_widget_realize               (GtkWidget        *widget);
39 static void            gtk_win32_embed_widget_unrealize             (GtkWidget        *widget);
40 static void            gtk_win32_embed_widget_show                  (GtkWidget        *widget);
41 static void            gtk_win32_embed_widget_hide                  (GtkWidget        *widget);
42 static void            gtk_win32_embed_widget_map                   (GtkWidget        *widget);
43 static void            gtk_win32_embed_widget_unmap                 (GtkWidget        *widget);
44 static void            gtk_win32_embed_widget_size_allocate         (GtkWidget        *widget,
45 								     GtkAllocation    *allocation);
46 static void            gtk_win32_embed_widget_set_focus             (GtkWindow        *window,
47 								     GtkWidget        *focus);
48 static gboolean        gtk_win32_embed_widget_focus                 (GtkWidget        *widget,
49 								     GtkDirectionType  direction);
50 static void            gtk_win32_embed_widget_check_resize          (GtkContainer     *container);
51 
52 static GtkBinClass *bin_class = NULL;
53 
G_DEFINE_TYPE(GtkWin32EmbedWidget,gtk_win32_embed_widget,GTK_TYPE_WINDOW)54 G_DEFINE_TYPE (GtkWin32EmbedWidget, gtk_win32_embed_widget, GTK_TYPE_WINDOW)
55 
56 static void
57 gtk_win32_embed_widget_class_init (GtkWin32EmbedWidgetClass *class)
58 {
59   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
60   GtkWindowClass *window_class = (GtkWindowClass *)class;
61   GtkContainerClass *container_class = (GtkContainerClass *)class;
62 
63   bin_class = g_type_class_peek (GTK_TYPE_BIN);
64 
65   widget_class->realize = gtk_win32_embed_widget_realize;
66   widget_class->unrealize = gtk_win32_embed_widget_unrealize;
67 
68   widget_class->show = gtk_win32_embed_widget_show;
69   widget_class->hide = gtk_win32_embed_widget_hide;
70   widget_class->map = gtk_win32_embed_widget_map;
71   widget_class->unmap = gtk_win32_embed_widget_unmap;
72   widget_class->size_allocate = gtk_win32_embed_widget_size_allocate;
73 
74   widget_class->focus = gtk_win32_embed_widget_focus;
75 
76   container_class->check_resize = gtk_win32_embed_widget_check_resize;
77 
78   window_class->set_focus = gtk_win32_embed_widget_set_focus;
79 }
80 
81 static void
gtk_win32_embed_widget_init(GtkWin32EmbedWidget * embed_widget)82 gtk_win32_embed_widget_init (GtkWin32EmbedWidget *embed_widget)
83 {
84   _gtk_widget_set_is_toplevel (GTK_WIDGET (embed_widget), TRUE);
85 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
86   gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
87 G_GNUC_END_IGNORE_DEPRECATIONS;
88   gtk_window_set_decorated (GTK_WINDOW (embed_widget), FALSE);
89 }
90 
91 GtkWidget*
_gtk_win32_embed_widget_new(HWND parent)92 _gtk_win32_embed_widget_new (HWND parent)
93 {
94   GtkWin32EmbedWidget *embed_widget;
95 
96   embed_widget = g_object_new (GTK_TYPE_WIN32_EMBED_WIDGET, NULL);
97 
98   embed_widget->parent_window =
99     gdk_win32_window_lookup_for_display (gdk_display_get_default (),
100 					 parent);
101 
102   if (!embed_widget->parent_window)
103     embed_widget->parent_window =
104       gdk_win32_window_foreign_new_for_display (gdk_display_get_default (),
105 					  parent);
106 
107   return GTK_WIDGET (embed_widget);
108 }
109 
110 BOOL
_gtk_win32_embed_widget_dialog_procedure(GtkWin32EmbedWidget * embed_widget,HWND wnd,UINT message,WPARAM wparam,LPARAM lparam)111 _gtk_win32_embed_widget_dialog_procedure (GtkWin32EmbedWidget *embed_widget,
112 					  HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
113 {
114   GtkAllocation allocation;
115   GtkWidget *widget = GTK_WIDGET (embed_widget);
116 
117  if (message == WM_SIZE)
118    {
119      allocation.width = LOWORD(lparam);
120      allocation.height = HIWORD(lparam);
121      gtk_widget_set_allocation (widget, &allocation);
122 
123      gtk_widget_queue_resize (widget);
124    }
125 
126  return 0;
127 }
128 
129 static void
gtk_win32_embed_widget_unrealize(GtkWidget * widget)130 gtk_win32_embed_widget_unrealize (GtkWidget *widget)
131 {
132   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
133 
134   embed_widget->old_window_procedure = NULL;
135 
136   g_clear_object (&embed_widget->parent_window);
137 
138   GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize (widget);
139 }
140 
141 static LRESULT CALLBACK
gtk_win32_embed_widget_window_process(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)142 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
143 {
144   GdkWindow *window;
145   GtkWin32EmbedWidget *embed_widget;
146   gpointer user_data;
147 
148   window = gdk_win32_window_lookup_for_display (gdk_display_get_default (), hwnd);
149   if (window == NULL) {
150     g_warning ("No such window!");
151     return 0;
152   }
153   gdk_window_get_user_data (window, &user_data);
154   embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
155 
156   if (msg == WM_GETDLGCODE) {
157     return DLGC_WANTALLKEYS;
158   }
159 
160   if (embed_widget && embed_widget->old_window_procedure)
161     return CallWindowProc(embed_widget->old_window_procedure,
162 			  hwnd, msg, wparam, lparam);
163   else
164     return 0;
165 }
166 
167 static void
gtk_win32_embed_widget_realize(GtkWidget * widget)168 gtk_win32_embed_widget_realize (GtkWidget *widget)
169 {
170   GtkWindow *window = GTK_WINDOW (widget);
171   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
172   GtkAllocation allocation;
173   GdkWindow *gdk_window;
174   GdkWindowAttr attributes;
175   gint attributes_mask;
176   LONG_PTR styles;
177 
178   gtk_widget_get_allocation (widget, &allocation);
179 
180   /* ensure widget tree is properly size allocated */
181   if (allocation.x == -1 && allocation.y == -1 &&
182       allocation.width == 1 && allocation.height == 1)
183     {
184       GtkRequisition requisition;
185       GtkAllocation allocation = { 0, 0, 200, 200 };
186 
187       gtk_widget_get_preferred_size (widget, &requisition, NULL);
188       if (requisition.width || requisition.height)
189 	{
190 	  /* non-empty window */
191 	  allocation.width = requisition.width;
192 	  allocation.height = requisition.height;
193 	}
194       gtk_widget_size_allocate (widget, &allocation);
195 
196       gtk_widget_queue_resize (widget);
197 
198       g_return_if_fail (!gtk_widget_get_realized (widget));
199     }
200 
201   gtk_widget_set_realized (widget, TRUE);
202 
203   gtk_widget_get_allocation (widget, &allocation);
204 
205   attributes.window_type = GDK_WINDOW_CHILD;
206   attributes.title = (gchar *) gtk_window_get_title (window);
207   _gtk_window_get_wmclass (window, &attributes.wmclass_name, &attributes.wmclass_class);
208   attributes.width = allocation.width;
209   attributes.height = allocation.height;
210   attributes.wclass = GDK_INPUT_OUTPUT;
211 
212   /* this isn't right - we should match our parent's visual/colormap.
213    * though that will require handling "foreign" colormaps */
214   attributes.visual = gtk_widget_get_visual (widget);
215   attributes.event_mask = gtk_widget_get_events (widget);
216   attributes.event_mask |= (GDK_EXPOSURE_MASK |
217 			    GDK_KEY_PRESS_MASK |
218 			    GDK_KEY_RELEASE_MASK |
219 			    GDK_ENTER_NOTIFY_MASK |
220 			    GDK_LEAVE_NOTIFY_MASK |
221 			    GDK_STRUCTURE_MASK |
222 			    GDK_FOCUS_CHANGE_MASK);
223 
224   attributes_mask = GDK_WA_VISUAL;
225   attributes_mask |= (attributes.title ? GDK_WA_TITLE : 0);
226   attributes_mask |= (attributes.wmclass_name ? GDK_WA_WMCLASS : 0);
227 
228   gdk_window = gdk_window_new (embed_widget->parent_window,
229                                &attributes, attributes_mask);
230   gtk_widget_set_window (widget, gdk_window);
231   gtk_widget_register_window (widget, gdk_window);
232 
233   embed_widget->old_window_procedure = (gpointer)
234     SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window),
235 		      GWLP_WNDPROC,
236 		      (LONG_PTR)gtk_win32_embed_widget_window_process);
237 
238   /* Enable tab to focus the widget */
239   styles = GetWindowLongPtr(GDK_WINDOW_HWND (gdk_window), GWL_STYLE);
240   SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window), GWL_STYLE, styles | WS_TABSTOP);
241 
242 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
243   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
244                                     gdk_window);
245 G_GNUC_END_IGNORE_DEPRECATIONS;
246 }
247 
248 static void
gtk_win32_embed_widget_show(GtkWidget * widget)249 gtk_win32_embed_widget_show (GtkWidget *widget)
250 {
251   _gtk_widget_set_visible_flag (widget, TRUE);
252 
253   gtk_widget_realize (widget);
254   gtk_container_check_resize (GTK_CONTAINER (widget));
255   gtk_widget_map (widget);
256 }
257 
258 static void
gtk_win32_embed_widget_hide(GtkWidget * widget)259 gtk_win32_embed_widget_hide (GtkWidget *widget)
260 {
261   _gtk_widget_set_visible_flag (widget, FALSE);
262   gtk_widget_unmap (widget);
263 }
264 
265 static void
gtk_win32_embed_widget_map(GtkWidget * widget)266 gtk_win32_embed_widget_map (GtkWidget *widget)
267 {
268   GtkBin    *bin = GTK_BIN (widget);
269   GtkWidget *child;
270 
271   gtk_widget_set_mapped (widget, TRUE);
272 
273   child = gtk_bin_get_child (bin);
274   if (child &&
275       gtk_widget_get_visible (child) &&
276       !gtk_widget_get_mapped (child))
277     gtk_widget_map (child);
278 
279   gdk_window_show (gtk_widget_get_window (widget));
280 }
281 
282 static void
gtk_win32_embed_widget_unmap(GtkWidget * widget)283 gtk_win32_embed_widget_unmap (GtkWidget *widget)
284 {
285   gtk_widget_set_mapped (widget, FALSE);
286   gdk_window_hide (gtk_widget_get_window (widget));
287 }
288 
289 static void
gtk_win32_embed_widget_size_allocate(GtkWidget * widget,GtkAllocation * allocation)290 gtk_win32_embed_widget_size_allocate (GtkWidget     *widget,
291 				      GtkAllocation *allocation)
292 {
293   GtkBin    *bin = GTK_BIN (widget);
294   GtkWidget *child;
295 
296   gtk_widget_set_allocation (widget, allocation);
297 
298   if (gtk_widget_get_realized (widget))
299     gdk_window_move_resize (gtk_widget_get_window (widget),
300 			    allocation->x, allocation->y,
301 			    allocation->width, allocation->height);
302 
303   child = gtk_bin_get_child (bin);
304   if (child && gtk_widget_get_visible (child))
305     {
306       GtkAllocation child_allocation;
307 
308       child_allocation.x = gtk_container_get_border_width (GTK_CONTAINER (widget));
309       child_allocation.y = child_allocation.x;
310       child_allocation.width =
311 	MAX (1, (gint)allocation->width - child_allocation.x * 2);
312       child_allocation.height =
313 	MAX (1, (gint)allocation->height - child_allocation.y * 2);
314 
315       gtk_widget_size_allocate (child, &child_allocation);
316     }
317 }
318 
319 static void
gtk_win32_embed_widget_check_resize(GtkContainer * container)320 gtk_win32_embed_widget_check_resize (GtkContainer *container)
321 {
322   GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
323 }
324 
325 static gboolean
gtk_win32_embed_widget_focus(GtkWidget * widget,GtkDirectionType direction)326 gtk_win32_embed_widget_focus (GtkWidget        *widget,
327 			      GtkDirectionType  direction)
328 {
329   GtkBin *bin = GTK_BIN (widget);
330   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
331   GtkWindow *window = GTK_WINDOW (widget);
332   GtkContainer *container = GTK_CONTAINER (widget);
333   GtkWidget *old_focus_child = gtk_container_get_focus_child (container);
334   GtkWidget *parent;
335   GtkWidget *child;
336 
337   /* We override GtkWindow's behavior, since we don't want wrapping here.
338    */
339   if (old_focus_child)
340     {
341       if (gtk_widget_child_focus (old_focus_child, direction))
342 	return TRUE;
343 
344       if (gtk_window_get_focus (window))
345 	{
346 	  /* Wrapped off the end, clear the focus setting for the toplevel */
347 	  parent = gtk_widget_get_parent (gtk_window_get_focus (window));
348 	  while (parent)
349 	    {
350 	      gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
351 	      parent = gtk_widget_get_parent (GTK_WIDGET (parent));
352 	    }
353 
354 	  gtk_window_set_focus (GTK_WINDOW (container), NULL);
355 	}
356     }
357   else
358     {
359       /* Try to focus the first widget in the window */
360       child = gtk_bin_get_child (bin);
361       if (child && gtk_widget_child_focus (child, direction))
362         return TRUE;
363     }
364 
365   if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
366     {
367       int backwards = FALSE;
368 
369       if (direction == GTK_DIR_TAB_BACKWARD ||
370 	  direction == GTK_DIR_LEFT)
371 	backwards = TRUE;
372 
373       PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
374 				   WM_NEXTDLGCTL,
375 				   backwards, 0);
376     }
377 
378   return FALSE;
379 }
380 
381 static void
gtk_win32_embed_widget_set_focus(GtkWindow * window,GtkWidget * focus)382 gtk_win32_embed_widget_set_focus (GtkWindow *window,
383 				  GtkWidget *focus)
384 {
385   GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
386 
387   gdk_window_focus (gtk_widget_get_window (GTK_WIDGET(window)), 0);
388 }
389