]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32embedwidget.c
Make it compile again for Windows
[~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
35
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,
45                                                                      GtkWidget        *focus);
46 static gboolean        gtk_win32_embed_widget_focus                 (GtkWidget        *widget,
47                                                                      GtkDirectionType  direction);
48 static void            gtk_win32_embed_widget_check_resize          (GtkContainer     *container);
49
50 static GtkBinClass *bin_class = NULL;
51
52 G_DEFINE_TYPE (GtkWin32EmbedWidget, gtk_win32_embed_widget, GTK_TYPE_WINDOW)
53
54 static void
55 gtk_win32_embed_widget_class_init (GtkWin32EmbedWidgetClass *class)
56 {
57   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
58   GtkWindowClass *window_class = (GtkWindowClass *)class;
59   GtkContainerClass *container_class = (GtkContainerClass *)class;
60
61   bin_class = g_type_class_peek (GTK_TYPE_BIN);
62
63   widget_class->realize = gtk_win32_embed_widget_realize;
64   widget_class->unrealize = gtk_win32_embed_widget_unrealize;
65
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;
71
72   widget_class->focus = gtk_win32_embed_widget_focus;
73   
74   container_class->check_resize = gtk_win32_embed_widget_check_resize;
75
76   window_class->set_focus = gtk_win32_embed_widget_set_focus;
77 }
78
79 static void
80 gtk_win32_embed_widget_init (GtkWin32EmbedWidget *embed_widget)
81 {
82   GtkWindow *window;
83
84   window = GTK_WINDOW (embed_widget);
85
86   _gtk_widget_set_is_toplevel (GTK_WIDGET (embed_widget), TRUE);
87   gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
88 }
89
90 GtkWidget*
91 _gtk_win32_embed_widget_new (GdkNativeWindow parent_id)
92 {
93   GtkWin32EmbedWidget *embed_widget;
94
95   embed_widget = g_object_new (GTK_TYPE_WIN32_EMBED_WIDGET, NULL);
96   
97   embed_widget->parent_window =
98     gdk_window_lookup_for_display (gdk_display_get_default (),
99                                    parent_id);
100   
101   if (!embed_widget->parent_window)
102     embed_widget->parent_window =
103       gdk_window_foreign_new_for_display (gdk_display_get_default (),
104                                           parent_id);
105   
106   return GTK_WIDGET (embed_widget);
107 }
108
109 BOOL
110 _gtk_win32_embed_widget_dialog_procedure (GtkWin32EmbedWidget *embed_widget,
111                                           HWND wnd, UINT message, WPARAM wparam, LPARAM lparam)
112 {
113   GtkAllocation allocation;
114   GtkWidget *widget = GTK_WIDGET (embed_widget);
115   
116  if (message == WM_SIZE)
117    {
118      allocation.width = LOWORD(lparam);
119      allocation.height = HIWORD(lparam);
120      gtk_widget_set_allocation (widget, &allocation);
121
122      gtk_widget_queue_resize (widget);
123    }
124         
125  return 0;
126 }
127
128 static void
129 gtk_win32_embed_widget_unrealize (GtkWidget *widget)
130 {
131   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
132
133   embed_widget->old_window_procedure = NULL;
134   
135   if (embed_widget->parent_window != NULL)
136     {
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;
140     }
141
142   GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize (widget);
143 }
144
145 static LRESULT CALLBACK
146 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
147 {
148   GdkWindow *window;
149   GtkWin32EmbedWidget *embed_widget;
150   gpointer user_data;
151
152   window = gdk_window_lookup ((GdkNativeWindow)hwnd);
153   if (window == NULL) {
154     g_warning ("No such window!");
155     return 0;
156   }
157   gdk_window_get_user_data (window, &user_data);
158   embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
159
160   if (msg == WM_GETDLGCODE) {
161     return DLGC_WANTALLKEYS;
162   }
163
164   if (embed_widget && embed_widget->old_window_procedure)
165     return CallWindowProc(embed_widget->old_window_procedure,
166                           hwnd, msg, wparam, lparam);
167   else
168     return 0;
169 }
170
171 static void
172 gtk_win32_embed_widget_realize (GtkWidget *widget)
173 {
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;
180   LONG_PTR styles;
181
182   gtk_widget_get_allocation (widget, &allocation);
183
184   /* ensure widget tree is properly size allocated */
185   if (allocation.x == -1 && allocation.y == -1 &&
186       allocation.width == 1 && allocation.height == 1)
187     {
188       GtkRequisition requisition;
189       GtkAllocation allocation = { 0, 0, 200, 200 };
190
191       gtk_widget_get_preferred_size (widget, &requisition, NULL);
192       if (requisition.width || requisition.height)
193         {
194           /* non-empty window */
195           allocation.width = requisition.width;
196           allocation.height = requisition.height;
197         }
198       gtk_widget_size_allocate (widget, &allocation);
199       
200       _gtk_container_queue_resize (GTK_CONTAINER (widget));
201
202       g_return_if_fail (!gtk_widget_get_realized (widget));
203     }
204
205   gtk_widget_set_realized (widget, TRUE);
206
207   gtk_widget_get_allocation (widget, &allocation);
208
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;
215
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 |
221                             GDK_KEY_PRESS_MASK |
222                             GDK_KEY_RELEASE_MASK |
223                             GDK_ENTER_NOTIFY_MASK |
224                             GDK_LEAVE_NOTIFY_MASK |
225                             GDK_STRUCTURE_MASK |
226                             GDK_FOCUS_CHANGE_MASK);
227
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);
231
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);
236
237   embed_widget->old_window_procedure = (gpointer)
238     SetWindowLongPtrW(GDK_WINDOW_HWND (gdk_window),
239                       GWLP_WNDPROC,
240                       (LONG_PTR)gtk_win32_embed_widget_window_process);
241
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);
245
246   gtk_widget_style_attach (widget);
247   gtk_style_set_background (gtk_widget_get_style (widget), gdk_window, GTK_STATE_NORMAL);
248 }
249
250 static void
251 gtk_win32_embed_widget_show (GtkWidget *widget)
252 {
253   gtk_widget_set_visible (widget, TRUE);
254   
255   gtk_widget_realize (widget);
256   gtk_container_check_resize (GTK_CONTAINER (widget));
257   gtk_widget_map (widget);
258 }
259
260 static void
261 gtk_win32_embed_widget_hide (GtkWidget *widget)
262 {
263   gtk_widget_set_visible (widget, FALSE);
264   gtk_widget_unmap (widget);
265 }
266
267 static void
268 gtk_win32_embed_widget_map (GtkWidget *widget)
269 {
270   GtkBin    *bin = GTK_BIN (widget);
271   GtkWidget *child;
272
273   gtk_widget_set_mapped (widget, TRUE);
274
275   child = gtk_bin_get_child (bin);
276   if (child &&
277       gtk_widget_get_visible (child) &&
278       !gtk_widget_get_mapped (child))
279     gtk_widget_map (child);
280
281   gdk_window_show (gtk_widget_get_window (widget));
282 }
283
284 static void
285 gtk_win32_embed_widget_unmap (GtkWidget *widget)
286 {
287   gtk_widget_set_mapped (widget, FALSE);
288   gdk_window_hide (gtk_widget_get_window (widget));
289 }
290
291 static void
292 gtk_win32_embed_widget_size_allocate (GtkWidget     *widget,
293                                       GtkAllocation *allocation)
294 {
295   GtkBin    *bin = GTK_BIN (widget);
296   GtkWidget *child;
297   
298   gtk_widget_set_allocation (widget, allocation);
299   
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);
304
305   child = gtk_bin_get_child (bin);
306   if (child && gtk_widget_get_visible (child))
307     {
308       GtkAllocation child_allocation;
309       
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);
316       
317       gtk_widget_size_allocate (child, &child_allocation);
318     }
319 }
320
321 static void
322 gtk_win32_embed_widget_check_resize (GtkContainer *container)
323 {
324   GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
325 }
326
327 static gboolean
328 gtk_win32_embed_widget_focus (GtkWidget        *widget,
329                               GtkDirectionType  direction)
330 {
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);
336   GtkWidget *parent;
337   GtkWidget *child;
338
339   /* We override GtkWindow's behavior, since we don't want wrapping here.
340    */
341   if (old_focus_child)
342     {
343       if (gtk_widget_child_focus (old_focus_child, direction))
344         return TRUE;
345
346       if (gtk_window_get_focus (window))
347         {
348           /* Wrapped off the end, clear the focus setting for the toplevel */
349           parent = gtk_widget_get_parent (gtk_window_get_focus (window));
350           while (parent)
351             {
352               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
353               parent = gtk_widget_get_parent (GTK_WIDGET (parent));
354             }
355           
356           gtk_window_set_focus (GTK_WINDOW (container), NULL);
357         }
358     }
359   else
360     {
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))
364         return TRUE;
365     }
366
367   if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
368     {
369       int backwards = FALSE;
370
371       if (direction == GTK_DIR_TAB_BACKWARD ||
372           direction == GTK_DIR_LEFT)
373         backwards = TRUE;
374       
375       PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
376                                    WM_NEXTDLGCTL,
377                                    backwards, 0);
378     }
379
380   return FALSE;
381 }
382
383 static void
384 gtk_win32_embed_widget_set_focus (GtkWindow *window,
385                                   GtkWidget *focus)
386 {
387   GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
388
389   gdk_window_focus (gtk_widget_get_window (GTK_WIDGET(window)), 0);
390 }