]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32embedwidget.c
stylecontext: Do invalidation on first resize container
[~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, 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
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
82 gtk_win32_embed_widget_init (GtkWin32EmbedWidget *embed_widget)
83 {
84   GtkWindow *window;
85
86   window = GTK_WINDOW (embed_widget);
87
88   _gtk_widget_set_is_toplevel (GTK_WIDGET (embed_widget), TRUE);
89   gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
90 }
91
92 GtkWidget*
93 _gtk_win32_embed_widget_new (HWND parent)
94 {
95   GtkWin32EmbedWidget *embed_widget;
96
97   embed_widget = g_object_new (GTK_TYPE_WIN32_EMBED_WIDGET, NULL);
98   
99   embed_widget->parent_window =
100     gdk_win32_window_lookup_for_display (gdk_display_get_default (),
101                                          parent);
102   
103   if (!embed_widget->parent_window)
104     embed_widget->parent_window =
105       gdk_win32_window_foreign_new_for_display (gdk_display_get_default (),
106                                           parent);
107   
108   return GTK_WIDGET (embed_widget);
109 }
110
111 BOOL
112 _gtk_win32_embed_widget_dialog_procedure (GtkWin32EmbedWidget *embed_widget,
113                                           HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
114 {
115   GtkAllocation allocation;
116   GtkWidget *widget = GTK_WIDGET (embed_widget);
117   
118  if (message == WM_SIZE)
119    {
120      allocation.width = LOWORD(lparam);
121      allocation.height = HIWORD(lparam);
122      gtk_widget_set_allocation (widget, &allocation);
123
124      gtk_widget_queue_resize (widget);
125    }
126         
127  return 0;
128 }
129
130 static void
131 gtk_win32_embed_widget_unrealize (GtkWidget *widget)
132 {
133   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
134
135   embed_widget->old_window_procedure = NULL;
136   
137   if (embed_widget->parent_window != NULL)
138     {
139       gtk_widget_unregister_window (widget, embed_widget->parent_window);
140       g_object_unref (embed_widget->parent_window);
141       embed_widget->parent_window = NULL;
142     }
143
144   GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize (widget);
145 }
146
147 static LRESULT CALLBACK
148 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
149 {
150   GdkWindow *window;
151   GtkWin32EmbedWidget *embed_widget;
152   gpointer user_data;
153
154   window = gdk_win32_window_lookup_for_display (gdk_display_get_default (), hwnd);
155   if (window == NULL) {
156     g_warning ("No such window!");
157     return 0;
158   }
159   gdk_window_get_user_data (window, &user_data);
160   embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
161
162   if (msg == WM_GETDLGCODE) {
163     return DLGC_WANTALLKEYS;
164   }
165
166   if (embed_widget && embed_widget->old_window_procedure)
167     return CallWindowProc(embed_widget->old_window_procedure,
168                           hwnd, msg, wparam, lparam);
169   else
170     return 0;
171 }
172
173 static void
174 gtk_win32_embed_widget_realize (GtkWidget *widget)
175 {
176   GtkWindow *window = GTK_WINDOW (widget);
177   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
178   GtkAllocation allocation;
179   GdkWindow *gdk_window;
180   GdkWindowAttr attributes;
181   gint attributes_mask;
182   LONG_PTR styles;
183
184   gtk_widget_get_allocation (widget, &allocation);
185
186   /* ensure widget tree is properly size allocated */
187   if (allocation.x == -1 && allocation.y == -1 &&
188       allocation.width == 1 && allocation.height == 1)
189     {
190       GtkRequisition requisition;
191       GtkAllocation allocation = { 0, 0, 200, 200 };
192
193       gtk_widget_get_preferred_size (widget, &requisition, NULL);
194       if (requisition.width || requisition.height)
195         {
196           /* non-empty window */
197           allocation.width = requisition.width;
198           allocation.height = requisition.height;
199         }
200       gtk_widget_size_allocate (widget, &allocation);
201       
202       _gtk_container_queue_resize (GTK_CONTAINER (widget));
203
204       g_return_if_fail (!gtk_widget_get_realized (widget));
205     }
206
207   gtk_widget_set_realized (widget, TRUE);
208
209   gtk_widget_get_allocation (widget, &allocation);
210
211   attributes.window_type = GDK_WINDOW_CHILD;
212   attributes.title = gtk_window_get_title (window);
213   _gtk_window_get_wmclass (window, &attributes.wmclass_name, &attributes.wmclass_class);
214   attributes.width = allocation.width;
215   attributes.height = allocation.height;
216   attributes.wclass = GDK_INPUT_OUTPUT;
217
218   /* this isn't right - we should match our parent's visual/colormap.
219    * though that will require handling "foreign" colormaps */
220   attributes.visual = gtk_widget_get_visual (widget);
221   attributes.event_mask = gtk_widget_get_events (widget);
222   attributes.event_mask |= (GDK_EXPOSURE_MASK |
223                             GDK_KEY_PRESS_MASK |
224                             GDK_KEY_RELEASE_MASK |
225                             GDK_ENTER_NOTIFY_MASK |
226                             GDK_LEAVE_NOTIFY_MASK |
227                             GDK_STRUCTURE_MASK |
228                             GDK_FOCUS_CHANGE_MASK);
229
230   attributes_mask = GDK_WA_VISUAL;
231   attributes_mask |= (gtk_window_get_title (window) ? GDK_WA_TITLE : 0);
232   attributes_mask |= (attributes.wmclass_name ? GDK_WA_WMCLASS : 0);
233
234   gdk_window = gdk_window_new (embed_widget->parent_window,
235                                &attributes, attributes_mask);
236   gtk_widget_set_window (widget, gdk_window);
237   gtk_widget_register_window (widget, gdk_window);
238
239   embed_widget->old_window_procedure = (gpointer)
240     SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window),
241                       GWLP_WNDPROC,
242                       (LONG_PTR)gtk_win32_embed_widget_window_process);
243
244   /* Enable tab to focus the widget */
245   styles = GetWindowLongPtr(GDK_WINDOW_HWND (gdk_window), GWL_STYLE);
246   SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window), GWL_STYLE, styles | WS_TABSTOP);
247
248   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
249                                     gdk_window);
250 }
251
252 static void
253 gtk_win32_embed_widget_show (GtkWidget *widget)
254 {
255   gtk_widget_set_visible (widget, TRUE);
256   
257   gtk_widget_realize (widget);
258   gtk_container_check_resize (GTK_CONTAINER (widget));
259   gtk_widget_map (widget);
260 }
261
262 static void
263 gtk_win32_embed_widget_hide (GtkWidget *widget)
264 {
265   gtk_widget_set_visible (widget, FALSE);
266   gtk_widget_unmap (widget);
267 }
268
269 static void
270 gtk_win32_embed_widget_map (GtkWidget *widget)
271 {
272   GtkBin    *bin = GTK_BIN (widget);
273   GtkWidget *child;
274
275   gtk_widget_set_mapped (widget, TRUE);
276
277   child = gtk_bin_get_child (bin);
278   if (child &&
279       gtk_widget_get_visible (child) &&
280       !gtk_widget_get_mapped (child))
281     gtk_widget_map (child);
282
283   gdk_window_show (gtk_widget_get_window (widget));
284 }
285
286 static void
287 gtk_win32_embed_widget_unmap (GtkWidget *widget)
288 {
289   gtk_widget_set_mapped (widget, FALSE);
290   gdk_window_hide (gtk_widget_get_window (widget));
291 }
292
293 static void
294 gtk_win32_embed_widget_size_allocate (GtkWidget     *widget,
295                                       GtkAllocation *allocation)
296 {
297   GtkBin    *bin = GTK_BIN (widget);
298   GtkWidget *child;
299   
300   gtk_widget_set_allocation (widget, allocation);
301   
302   if (gtk_widget_get_realized (widget))
303     gdk_window_move_resize (gtk_widget_get_window (widget),
304                             allocation->x, allocation->y,
305                             allocation->width, allocation->height);
306
307   child = gtk_bin_get_child (bin);
308   if (child && gtk_widget_get_visible (child))
309     {
310       GtkAllocation child_allocation;
311       
312       child_allocation.x = gtk_container_get_border_width (GTK_CONTAINER (widget));
313       child_allocation.y = child_allocation.x;
314       child_allocation.width =
315         MAX (1, (gint)allocation->width - child_allocation.x * 2);
316       child_allocation.height =
317         MAX (1, (gint)allocation->height - child_allocation.y * 2);
318       
319       gtk_widget_size_allocate (child, &child_allocation);
320     }
321 }
322
323 static void
324 gtk_win32_embed_widget_check_resize (GtkContainer *container)
325 {
326   GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
327 }
328
329 static gboolean
330 gtk_win32_embed_widget_focus (GtkWidget        *widget,
331                               GtkDirectionType  direction)
332 {
333   GtkBin *bin = GTK_BIN (widget);
334   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
335   GtkWindow *window = GTK_WINDOW (widget);
336   GtkContainer *container = GTK_CONTAINER (widget);
337   GtkWidget *old_focus_child = gtk_container_get_focus_child (container);
338   GtkWidget *parent;
339   GtkWidget *child;
340
341   /* We override GtkWindow's behavior, since we don't want wrapping here.
342    */
343   if (old_focus_child)
344     {
345       if (gtk_widget_child_focus (old_focus_child, direction))
346         return TRUE;
347
348       if (gtk_window_get_focus (window))
349         {
350           /* Wrapped off the end, clear the focus setting for the toplevel */
351           parent = gtk_widget_get_parent (gtk_window_get_focus (window));
352           while (parent)
353             {
354               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
355               parent = gtk_widget_get_parent (GTK_WIDGET (parent));
356             }
357           
358           gtk_window_set_focus (GTK_WINDOW (container), NULL);
359         }
360     }
361   else
362     {
363       /* Try to focus the first widget in the window */
364       child = gtk_bin_get_child (bin);
365       if (child && gtk_widget_child_focus (child, direction))
366         return TRUE;
367     }
368
369   if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
370     {
371       int backwards = FALSE;
372
373       if (direction == GTK_DIR_TAB_BACKWARD ||
374           direction == GTK_DIR_LEFT)
375         backwards = TRUE;
376       
377       PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
378                                    WM_NEXTDLGCTL,
379                                    backwards, 0);
380     }
381
382   return FALSE;
383 }
384
385 static void
386 gtk_win32_embed_widget_set_focus (GtkWindow *window,
387                                   GtkWidget *focus)
388 {
389   GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
390
391   gdk_window_focus (gtk_widget_get_window (GTK_WIDGET(window)), 0);
392 }