]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32embedwidget.c
win32: Make GtkWin32EmbedWidget use HWND instead of GdkNativeWindow
[~andy/gtk] / gtk / gtkwin32embedwidget.c
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, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /*
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/. 
24  */
25
26 #include "config.h"
27
28 #include "gtkmain.h"
29 #include "gtkmarshalers.h"
30 #include "gtksizerequest.h"
31 #include "gtkwin32embedwidget.h"
32 #include "gtkintl.h"
33 #include "gtkprivate.h"
34 #include "gtkwindowprivate.h"
35
36
37 static void            gtk_win32_embed_widget_realize               (GtkWidget        *widget);
38 static void            gtk_win32_embed_widget_unrealize             (GtkWidget        *widget);
39 static void            gtk_win32_embed_widget_show                  (GtkWidget        *widget);
40 static void            gtk_win32_embed_widget_hide                  (GtkWidget        *widget);
41 static void            gtk_win32_embed_widget_map                   (GtkWidget        *widget);
42 static void            gtk_win32_embed_widget_unmap                 (GtkWidget        *widget);
43 static void            gtk_win32_embed_widget_size_allocate         (GtkWidget        *widget,
44                                                                      GtkAllocation    *allocation);
45 static void            gtk_win32_embed_widget_set_focus             (GtkWindow        *window,
46                                                                      GtkWidget        *focus);
47 static gboolean        gtk_win32_embed_widget_focus                 (GtkWidget        *widget,
48                                                                      GtkDirectionType  direction);
49 static void            gtk_win32_embed_widget_check_resize          (GtkContainer     *container);
50
51 static GtkBinClass *bin_class = NULL;
52
53 G_DEFINE_TYPE (GtkWin32EmbedWidget, gtk_win32_embed_widget, GTK_TYPE_WINDOW)
54
55 static void
56 gtk_win32_embed_widget_class_init (GtkWin32EmbedWidgetClass *class)
57 {
58   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
59   GtkWindowClass *window_class = (GtkWindowClass *)class;
60   GtkContainerClass *container_class = (GtkContainerClass *)class;
61
62   bin_class = g_type_class_peek (GTK_TYPE_BIN);
63
64   widget_class->realize = gtk_win32_embed_widget_realize;
65   widget_class->unrealize = gtk_win32_embed_widget_unrealize;
66
67   widget_class->show = gtk_win32_embed_widget_show;
68   widget_class->hide = gtk_win32_embed_widget_hide;
69   widget_class->map = gtk_win32_embed_widget_map;
70   widget_class->unmap = gtk_win32_embed_widget_unmap;
71   widget_class->size_allocate = gtk_win32_embed_widget_size_allocate;
72
73   widget_class->focus = gtk_win32_embed_widget_focus;
74   
75   container_class->check_resize = gtk_win32_embed_widget_check_resize;
76
77   window_class->set_focus = gtk_win32_embed_widget_set_focus;
78 }
79
80 static void
81 gtk_win32_embed_widget_init (GtkWin32EmbedWidget *embed_widget)
82 {
83   GtkWindow *window;
84
85   window = GTK_WINDOW (embed_widget);
86
87   _gtk_widget_set_is_toplevel (GTK_WIDGET (embed_widget), TRUE);
88   gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
89 }
90
91 GtkWidget*
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
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
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   if (embed_widget->parent_window != NULL)
137     {
138       gdk_window_set_user_data (embed_widget->parent_window, NULL);
139       g_object_unref (embed_widget->parent_window);
140       embed_widget->parent_window = NULL;
141     }
142
143   GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize (widget);
144 }
145
146 static LRESULT CALLBACK
147 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
148 {
149   GdkWindow *window;
150   GtkWin32EmbedWidget *embed_widget;
151   gpointer user_data;
152
153   window = gdk_win32_window_lookup_for_display (gdk_display_get_default (), hwnd);
154   if (window == NULL) {
155     g_warning ("No such window!");
156     return 0;
157   }
158   gdk_window_get_user_data (window, &user_data);
159   embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
160
161   if (msg == WM_GETDLGCODE) {
162     return DLGC_WANTALLKEYS;
163   }
164
165   if (embed_widget && embed_widget->old_window_procedure)
166     return CallWindowProc(embed_widget->old_window_procedure,
167                           hwnd, msg, wparam, lparam);
168   else
169     return 0;
170 }
171
172 static void
173 gtk_win32_embed_widget_realize (GtkWidget *widget)
174 {
175   GtkWindow *window = GTK_WINDOW (widget);
176   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
177   GtkAllocation allocation;
178   GdkWindow *gdk_window;
179   GdkWindowAttr attributes;
180   gint attributes_mask;
181   LONG_PTR styles;
182
183   gtk_widget_get_allocation (widget, &allocation);
184
185   /* ensure widget tree is properly size allocated */
186   if (allocation.x == -1 && allocation.y == -1 &&
187       allocation.width == 1 && allocation.height == 1)
188     {
189       GtkRequisition requisition;
190       GtkAllocation allocation = { 0, 0, 200, 200 };
191
192       gtk_widget_get_preferred_size (widget, &requisition, NULL);
193       if (requisition.width || requisition.height)
194         {
195           /* non-empty window */
196           allocation.width = requisition.width;
197           allocation.height = requisition.height;
198         }
199       gtk_widget_size_allocate (widget, &allocation);
200       
201       _gtk_container_queue_resize (GTK_CONTAINER (widget));
202
203       g_return_if_fail (!gtk_widget_get_realized (widget));
204     }
205
206   gtk_widget_set_realized (widget, TRUE);
207
208   gtk_widget_get_allocation (widget, &allocation);
209
210   attributes.window_type = GDK_WINDOW_CHILD;
211   attributes.title = gtk_window_get_title (window);
212   _gtk_window_get_wmclass (window, &attributes.wmclass_name, &attributes.wmclass_class);
213   attributes.width = allocation.width;
214   attributes.height = allocation.height;
215   attributes.wclass = GDK_INPUT_OUTPUT;
216
217   /* this isn't right - we should match our parent's visual/colormap.
218    * though that will require handling "foreign" colormaps */
219   attributes.visual = gtk_widget_get_visual (widget);
220   attributes.event_mask = gtk_widget_get_events (widget);
221   attributes.event_mask |= (GDK_EXPOSURE_MASK |
222                             GDK_KEY_PRESS_MASK |
223                             GDK_KEY_RELEASE_MASK |
224                             GDK_ENTER_NOTIFY_MASK |
225                             GDK_LEAVE_NOTIFY_MASK |
226                             GDK_STRUCTURE_MASK |
227                             GDK_FOCUS_CHANGE_MASK);
228
229   attributes_mask = GDK_WA_VISUAL;
230   attributes_mask |= (gtk_window_get_title (window) ? GDK_WA_TITLE : 0);
231   attributes_mask |= (attributes.wmclass_name ? GDK_WA_WMCLASS : 0);
232
233   gdk_window = gdk_window_new (embed_widget->parent_window,
234                                &attributes, attributes_mask);
235   gtk_widget_set_window (widget, gdk_window);
236   gdk_window_set_user_data (gdk_window, window);
237
238   embed_widget->old_window_procedure = (gpointer)
239     SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window),
240                       GWLP_WNDPROC,
241                       (LONG_PTR)gtk_win32_embed_widget_window_process);
242
243   /* Enable tab to focus the widget */
244   styles = GetWindowLongPtr(GDK_WINDOW_HWND (gdk_window), GWL_STYLE);
245   SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window), GWL_STYLE, styles | WS_TABSTOP);
246
247   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
248                                     gdk_window);
249 }
250
251 static void
252 gtk_win32_embed_widget_show (GtkWidget *widget)
253 {
254   gtk_widget_set_visible (widget, TRUE);
255   
256   gtk_widget_realize (widget);
257   gtk_container_check_resize (GTK_CONTAINER (widget));
258   gtk_widget_map (widget);
259 }
260
261 static void
262 gtk_win32_embed_widget_hide (GtkWidget *widget)
263 {
264   gtk_widget_set_visible (widget, FALSE);
265   gtk_widget_unmap (widget);
266 }
267
268 static void
269 gtk_win32_embed_widget_map (GtkWidget *widget)
270 {
271   GtkBin    *bin = GTK_BIN (widget);
272   GtkWidget *child;
273
274   gtk_widget_set_mapped (widget, TRUE);
275
276   child = gtk_bin_get_child (bin);
277   if (child &&
278       gtk_widget_get_visible (child) &&
279       !gtk_widget_get_mapped (child))
280     gtk_widget_map (child);
281
282   gdk_window_show (gtk_widget_get_window (widget));
283 }
284
285 static void
286 gtk_win32_embed_widget_unmap (GtkWidget *widget)
287 {
288   gtk_widget_set_mapped (widget, FALSE);
289   gdk_window_hide (gtk_widget_get_window (widget));
290 }
291
292 static void
293 gtk_win32_embed_widget_size_allocate (GtkWidget     *widget,
294                                       GtkAllocation *allocation)
295 {
296   GtkBin    *bin = GTK_BIN (widget);
297   GtkWidget *child;
298   
299   gtk_widget_set_allocation (widget, allocation);
300   
301   if (gtk_widget_get_realized (widget))
302     gdk_window_move_resize (gtk_widget_get_window (widget),
303                             allocation->x, allocation->y,
304                             allocation->width, allocation->height);
305
306   child = gtk_bin_get_child (bin);
307   if (child && gtk_widget_get_visible (child))
308     {
309       GtkAllocation child_allocation;
310       
311       child_allocation.x = gtk_container_get_border_width (GTK_CONTAINER (widget));
312       child_allocation.y = child_allocation.x;
313       child_allocation.width =
314         MAX (1, (gint)allocation->width - child_allocation.x * 2);
315       child_allocation.height =
316         MAX (1, (gint)allocation->height - child_allocation.y * 2);
317       
318       gtk_widget_size_allocate (child, &child_allocation);
319     }
320 }
321
322 static void
323 gtk_win32_embed_widget_check_resize (GtkContainer *container)
324 {
325   GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
326 }
327
328 static gboolean
329 gtk_win32_embed_widget_focus (GtkWidget        *widget,
330                               GtkDirectionType  direction)
331 {
332   GtkBin *bin = GTK_BIN (widget);
333   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
334   GtkWindow *window = GTK_WINDOW (widget);
335   GtkContainer *container = GTK_CONTAINER (widget);
336   GtkWidget *old_focus_child = gtk_container_get_focus_child (container);
337   GtkWidget *parent;
338   GtkWidget *child;
339
340   /* We override GtkWindow's behavior, since we don't want wrapping here.
341    */
342   if (old_focus_child)
343     {
344       if (gtk_widget_child_focus (old_focus_child, direction))
345         return TRUE;
346
347       if (gtk_window_get_focus (window))
348         {
349           /* Wrapped off the end, clear the focus setting for the toplevel */
350           parent = gtk_widget_get_parent (gtk_window_get_focus (window));
351           while (parent)
352             {
353               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
354               parent = gtk_widget_get_parent (GTK_WIDGET (parent));
355             }
356           
357           gtk_window_set_focus (GTK_WINDOW (container), NULL);
358         }
359     }
360   else
361     {
362       /* Try to focus the first widget in the window */
363       child = gtk_bin_get_child (bin);
364       if (child && gtk_widget_child_focus (child, direction))
365         return TRUE;
366     }
367
368   if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
369     {
370       int backwards = FALSE;
371
372       if (direction == GTK_DIR_TAB_BACKWARD ||
373           direction == GTK_DIR_LEFT)
374         backwards = TRUE;
375       
376       PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
377                                    WM_NEXTDLGCTL,
378                                    backwards, 0);
379     }
380
381   return FALSE;
382 }
383
384 static void
385 gtk_win32_embed_widget_set_focus (GtkWindow *window,
386                                   GtkWidget *focus)
387 {
388   GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
389
390   gdk_window_focus (gtk_widget_get_window (GTK_WIDGET(window)), 0);
391 }