]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32embedwidget.c
gtk/: fully remove gtkalias hacks
[~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 "gtkwin32embedwidget.h"
31 #include "gtkintl.h"
32 #include "gtkprivate.h"
33
34
35 static void            gtk_win32_embed_widget_realize               (GtkWidget        *widget);
36 static void            gtk_win32_embed_widget_unrealize             (GtkWidget        *widget);
37 static void            gtk_win32_embed_widget_show                  (GtkWidget        *widget);
38 static void            gtk_win32_embed_widget_hide                  (GtkWidget        *widget);
39 static void            gtk_win32_embed_widget_map                   (GtkWidget        *widget);
40 static void            gtk_win32_embed_widget_unmap                 (GtkWidget        *widget);
41 static void            gtk_win32_embed_widget_size_allocate         (GtkWidget        *widget,
42                                                                      GtkAllocation    *allocation);
43 static void            gtk_win32_embed_widget_set_focus             (GtkWindow        *window,
44                                                                      GtkWidget        *focus);
45 static gboolean        gtk_win32_embed_widget_focus                 (GtkWidget        *widget,
46                                                                      GtkDirectionType  direction);
47 static void            gtk_win32_embed_widget_check_resize          (GtkContainer     *container);
48
49 static GtkBinClass *bin_class = NULL;
50
51 G_DEFINE_TYPE (GtkWin32EmbedWidget, gtk_win32_embed_widget, GTK_TYPE_WINDOW)
52
53 static void
54 gtk_win32_embed_widget_class_init (GtkWin32EmbedWidgetClass *class)
55 {
56   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
57   GtkWindowClass *window_class = (GtkWindowClass *)class;
58   GtkContainerClass *container_class = (GtkContainerClass *)class;
59
60   bin_class = g_type_class_peek (GTK_TYPE_BIN);
61
62   widget_class->realize = gtk_win32_embed_widget_realize;
63   widget_class->unrealize = gtk_win32_embed_widget_unrealize;
64
65   widget_class->show = gtk_win32_embed_widget_show;
66   widget_class->hide = gtk_win32_embed_widget_hide;
67   widget_class->map = gtk_win32_embed_widget_map;
68   widget_class->unmap = gtk_win32_embed_widget_unmap;
69   widget_class->size_allocate = gtk_win32_embed_widget_size_allocate;
70
71   widget_class->focus = gtk_win32_embed_widget_focus;
72   
73   container_class->check_resize = gtk_win32_embed_widget_check_resize;
74
75   window_class->set_focus = gtk_win32_embed_widget_set_focus;
76 }
77
78 static void
79 gtk_win32_embed_widget_init (GtkWin32EmbedWidget *embed_widget)
80 {
81   GtkWindow *window;
82
83   window = GTK_WINDOW (embed_widget);
84
85   window->type = GTK_WINDOW_TOPLEVEL;
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 (GdkNativeWindow parent_id)
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_window_lookup_for_display (gdk_display_get_default (),
100                                    parent_id);
101   
102   if (!embed_widget->parent_window)
103     embed_widget->parent_window =
104       gdk_window_foreign_new_for_display (gdk_display_get_default (),
105                                           parent_id);
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   GtkWidget *widget = GTK_WIDGET (embed_widget);
115   
116  if (message == WM_SIZE)
117    {
118      widget->allocation.width = LOWORD(lparam);
119      widget->allocation.height = HIWORD(lparam);
120      
121      gtk_widget_queue_resize (widget);
122    }
123         
124  return 0;
125 }
126
127 static void
128 gtk_win32_embed_widget_unrealize (GtkWidget *widget)
129 {
130   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
131
132   embed_widget->old_window_procedure = NULL;
133   
134   if (embed_widget->parent_window != NULL)
135     {
136       gdk_window_set_user_data (embed_widget->parent_window, NULL);
137       g_object_unref (embed_widget->parent_window);
138       embed_widget->parent_window = NULL;
139     }
140
141   GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize (widget);
142 }
143
144 static LRESULT CALLBACK
145 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
146 {
147   GdkWindow *window;
148   GtkWin32EmbedWidget *embed_widget;
149   gpointer user_data;
150
151   window = gdk_window_lookup ((GdkNativeWindow)hwnd);
152   if (window == NULL) {
153     g_warning ("No such window!");
154     return 0;
155   }
156   gdk_window_get_user_data (window, &user_data);
157   embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
158
159   if (msg == WM_GETDLGCODE) {
160     return DLGC_WANTALLKEYS;
161   }
162
163   if (embed_widget && embed_widget->old_window_procedure)
164     return CallWindowProc(embed_widget->old_window_procedure,
165                           hwnd, msg, wparam, lparam);
166   else
167     return 0;
168 }
169
170 static void
171 gtk_win32_embed_widget_realize (GtkWidget *widget)
172 {
173   GtkWindow *window = GTK_WINDOW (widget);
174   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
175   GdkWindowAttr attributes;
176   gint attributes_mask;
177   LONG_PTR styles;
178
179   /* ensure widget tree is properly size allocated */
180   if (widget->allocation.x == -1 &&
181       widget->allocation.y == -1 &&
182       widget->allocation.width == 1 &&
183       widget->allocation.height == 1)
184     {
185       GtkRequisition requisition;
186       GtkAllocation allocation = { 0, 0, 200, 200 };
187
188       gtk_widget_size_request (widget, &requisition);
189       if (requisition.width || requisition.height)
190         {
191           /* non-empty window */
192           allocation.width = requisition.width;
193           allocation.height = requisition.height;
194         }
195       gtk_widget_size_allocate (widget, &allocation);
196       
197       _gtk_container_queue_resize (GTK_CONTAINER (widget));
198
199       g_return_if_fail (!gtk_widget_get_realized (widget));
200     }
201
202   gtk_widget_set_realized (widget, TRUE);
203
204   attributes.window_type = GDK_WINDOW_CHILD;
205   attributes.title = window->title;
206   attributes.wmclass_name = window->wmclass_name;
207   attributes.wmclass_class = window->wmclass_class;
208   attributes.width = widget->allocation.width;
209   attributes.height = widget->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.colormap = gtk_widget_get_colormap (widget);
216   attributes.event_mask = gtk_widget_get_events (widget);
217   attributes.event_mask |= (GDK_EXPOSURE_MASK |
218                             GDK_KEY_PRESS_MASK |
219                             GDK_KEY_RELEASE_MASK |
220                             GDK_ENTER_NOTIFY_MASK |
221                             GDK_LEAVE_NOTIFY_MASK |
222                             GDK_STRUCTURE_MASK |
223                             GDK_FOCUS_CHANGE_MASK);
224
225   attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
226   attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
227   attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
228
229   widget->window = gdk_window_new (embed_widget->parent_window, 
230                                    &attributes, attributes_mask);
231
232   gdk_window_set_user_data (widget->window, window);
233
234   embed_widget->old_window_procedure = (gpointer)
235     SetWindowLongPtrW(GDK_WINDOW_HWND (widget->window),
236                       GWLP_WNDPROC,
237                       (LONG_PTR)gtk_win32_embed_widget_window_process);
238
239   /* Enable tab to focus the widget */
240   styles = GetWindowLongPtr(GDK_WINDOW_HWND (widget->window), GWL_STYLE);
241   SetWindowLongPtrW(GDK_WINDOW_HWND (widget->window), GWL_STYLE, styles | WS_TABSTOP);
242   
243   widget->style = gtk_style_attach (widget->style, widget->window);
244   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
245 }
246
247 static void
248 gtk_win32_embed_widget_show (GtkWidget *widget)
249 {
250   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
251   
252   gtk_widget_realize (widget);
253   gtk_container_check_resize (GTK_CONTAINER (widget));
254   gtk_widget_map (widget);
255 }
256
257 static void
258 gtk_win32_embed_widget_hide (GtkWidget *widget)
259 {
260   GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
261   gtk_widget_unmap (widget);
262 }
263
264 static void
265 gtk_win32_embed_widget_map (GtkWidget *widget)
266 {
267   GtkBin *bin = GTK_BIN (widget);
268   
269   gtk_widget_set_mapped (widget, TRUE);
270   
271   if (bin->child &&
272       gtk_widget_get_visible (bin->child) &&
273       !gtk_widget_get_mapped (bin->child))
274     gtk_widget_map (bin->child);
275
276   gdk_window_show (widget->window);
277 }
278
279 static void
280 gtk_win32_embed_widget_unmap (GtkWidget *widget)
281 {
282   gtk_widget_set_mapped (widget, FALSE);
283   gdk_window_hide (widget->window);
284 }
285
286 static void
287 gtk_win32_embed_widget_size_allocate (GtkWidget     *widget,
288                                       GtkAllocation *allocation)
289 {
290   GtkBin *bin = GTK_BIN (widget);
291   
292   widget->allocation = *allocation;
293   
294   if (gtk_widget_get_realized (widget))
295     gdk_window_move_resize (widget->window,
296                             allocation->x, allocation->y,
297                             allocation->width, allocation->height);
298   
299   if (bin->child && gtk_widget_get_visible (bin->child))
300     {
301       GtkAllocation child_allocation;
302       
303       child_allocation.x = child_allocation.y = GTK_CONTAINER (widget)->border_width;
304       child_allocation.width =
305         MAX (1, (gint)allocation->width - child_allocation.x * 2);
306       child_allocation.height =
307         MAX (1, (gint)allocation->height - child_allocation.y * 2);
308       
309       gtk_widget_size_allocate (bin->child, &child_allocation);
310     }
311 }
312
313 static void
314 gtk_win32_embed_widget_check_resize (GtkContainer *container)
315 {
316   GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
317 }
318
319 static gboolean
320 gtk_win32_embed_widget_focus (GtkWidget        *widget,
321                               GtkDirectionType  direction)
322 {
323   GtkBin *bin = GTK_BIN (widget);
324   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
325   GtkWindow *window = GTK_WINDOW (widget);
326   GtkContainer *container = GTK_CONTAINER (widget);
327   GtkWidget *old_focus_child = container->focus_child;
328   GtkWidget *parent;
329
330   /* We override GtkWindow's behavior, since we don't want wrapping here.
331    */
332   if (old_focus_child)
333     {
334       if (gtk_widget_child_focus (old_focus_child, direction))
335         return TRUE;
336
337       if (window->focus_widget)
338         {
339           /* Wrapped off the end, clear the focus setting for the toplevel */
340           parent = window->focus_widget->parent;
341           while (parent)
342             {
343               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
344               parent = GTK_WIDGET (parent)->parent;
345             }
346           
347           gtk_window_set_focus (GTK_WINDOW (container), NULL);
348         }
349     }
350   else
351     {
352       /* Try to focus the first widget in the window */
353       if (bin->child && gtk_widget_child_focus (bin->child, direction))
354         return TRUE;
355     }
356
357   if (!GTK_CONTAINER (window)->focus_child)
358     {
359       int backwards = FALSE;
360
361       if (direction == GTK_DIR_TAB_BACKWARD ||
362           direction == GTK_DIR_LEFT)
363         backwards = TRUE;
364       
365       PostMessage(GDK_WINDOW_HWND (embed_widget->parent_window),
366                                    WM_NEXTDLGCTL,
367                                    backwards, 0);
368     }
369
370   return FALSE;
371 }
372
373 static void
374 gtk_win32_embed_widget_set_focus (GtkWindow *window,
375                                   GtkWidget *focus)
376 {
377   GTK_WINDOW_CLASS (gtk_win32_embed_widget_parent_class)->set_focus (window, focus);
378
379   gdk_window_focus (GTK_WIDGET(window)->window, 0);
380 }