]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32embedwidget.c
Fix a compiler warning
[~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 #include "gtkalias.h"
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   window->type = GTK_WINDOW_TOPLEVEL;
87
88   GTK_WIDGET_SET_FLAGS (embed_widget, GTK_TOPLEVEL);
89   gtk_container_set_resize_mode (GTK_CONTAINER (embed_widget), GTK_RESIZE_QUEUE);
90 }
91
92 GtkWidget*
93 _gtk_win32_embed_widget_new (GdkNativeWindow parent_id)
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_window_lookup_for_display (gdk_display_get_default (),
101                                    parent_id);
102   
103   if (!embed_widget->parent_window)
104     embed_widget->parent_window =
105       gdk_window_foreign_new_for_display (gdk_display_get_default (),
106                                           parent_id);
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   GtkWidget *widget = GTK_WIDGET (embed_widget);
116   
117  if (message == WM_SIZE)
118    {
119      widget->allocation.width = LOWORD(lparam);
120      widget->allocation.height = HIWORD(lparam);
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;
132
133   g_return_if_fail (GTK_IS_WIN32_EMBED_WIDGET (widget));
134
135   embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
136
137   embed_widget->old_window_procedure = NULL;
138   
139   if (embed_widget->parent_window != NULL)
140     {
141       gdk_window_set_user_data (embed_widget->parent_window, NULL);
142       g_object_unref (embed_widget->parent_window);
143       embed_widget->parent_window = NULL;
144     }
145   
146   if (GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize)
147     (* GTK_WIDGET_CLASS (gtk_win32_embed_widget_parent_class)->unrealize) (widget);
148 }
149
150 static LRESULT CALLBACK
151 gtk_win32_embed_widget_window_process (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
152 {
153   GdkWindow *window;
154   GtkWin32EmbedWidget *embed_widget;
155   gpointer user_data;
156
157   window = gdk_window_lookup ((GdkNativeWindow)hwnd);
158   if (window == NULL) {
159     g_warning ("No such window!");
160     return 0;
161   }
162   gdk_window_get_user_data (window, &user_data);
163   embed_widget = GTK_WIN32_EMBED_WIDGET (user_data);
164
165   if (msg == WM_GETDLGCODE) {
166     return DLGC_WANTALLKEYS;
167   }
168
169   if (embed_widget && embed_widget->old_window_procedure)
170     return CallWindowProc(embed_widget->old_window_procedure,
171                           hwnd, msg, wparam, lparam);
172   else
173     return 0;
174 }
175
176 static void
177 gtk_win32_embed_widget_realize (GtkWidget *widget)
178 {
179   GtkWindow *window;
180   GtkWin32EmbedWidget *embed_widget;
181   GdkWindowAttr attributes;
182   gint attributes_mask;
183   LONG_PTR styles;
184
185   g_return_if_fail (GTK_IS_WIN32_EMBED_WIDGET (widget));
186
187   window = GTK_WINDOW (widget);
188   embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
189   
190   /* ensure widget tree is properly size allocated */
191   if (widget->allocation.x == -1 &&
192       widget->allocation.y == -1 &&
193       widget->allocation.width == 1 &&
194       widget->allocation.height == 1)
195     {
196       GtkRequisition requisition;
197       GtkAllocation allocation = { 0, 0, 200, 200 };
198
199       gtk_widget_size_request (widget, &requisition);
200       if (requisition.width || requisition.height)
201         {
202           /* non-empty window */
203           allocation.width = requisition.width;
204           allocation.height = requisition.height;
205         }
206       gtk_widget_size_allocate (widget, &allocation);
207       
208       _gtk_container_queue_resize (GTK_CONTAINER (widget));
209
210       g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
211     }
212
213   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
214
215   attributes.window_type = GDK_WINDOW_CHILD;
216   attributes.title = window->title;
217   attributes.wmclass_name = window->wmclass_name;
218   attributes.wmclass_class = window->wmclass_class;
219   attributes.width = widget->allocation.width;
220   attributes.height = widget->allocation.height;
221   attributes.wclass = GDK_INPUT_OUTPUT;
222
223   /* this isn't right - we should match our parent's visual/colormap.
224    * though that will require handling "foreign" colormaps */
225   attributes.visual = gtk_widget_get_visual (widget);
226   attributes.colormap = gtk_widget_get_colormap (widget);
227   attributes.event_mask = gtk_widget_get_events (widget);
228   attributes.event_mask |= (GDK_EXPOSURE_MASK |
229                             GDK_KEY_PRESS_MASK |
230                             GDK_KEY_RELEASE_MASK |
231                             GDK_ENTER_NOTIFY_MASK |
232                             GDK_LEAVE_NOTIFY_MASK |
233                             GDK_STRUCTURE_MASK |
234                             GDK_FOCUS_CHANGE_MASK);
235
236   attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
237   attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
238   attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
239
240   widget->window = gdk_window_new (embed_widget->parent_window, 
241                                    &attributes, attributes_mask);
242
243   gdk_window_set_user_data (widget->window, window);
244
245   embed_widget->old_window_procedure = (gpointer)
246     SetWindowLongPtrW(GDK_WINDOW_HWND (widget->window),
247                       GWLP_WNDPROC,
248                       (LONG_PTR)gtk_win32_embed_widget_window_process);
249
250   /* Enable tab to focus the widget */
251   styles = GetWindowLongPtr(GDK_WINDOW_HWND (widget->window), GWL_STYLE);
252   SetWindowLongPtrW(GDK_WINDOW_HWND (widget->window), GWL_STYLE, styles | WS_TABSTOP);
253   
254   widget->style = gtk_style_attach (widget->style, widget->window);
255   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
256 }
257
258 static void
259 gtk_win32_embed_widget_show (GtkWidget *widget)
260 {
261   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
262   
263   gtk_widget_realize (widget);
264   gtk_container_check_resize (GTK_CONTAINER (widget));
265   gtk_widget_map (widget);
266 }
267
268 static void
269 gtk_win32_embed_widget_hide (GtkWidget *widget)
270 {
271   GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
272   gtk_widget_unmap (widget);
273 }
274
275 static void
276 gtk_win32_embed_widget_map (GtkWidget *widget)
277 {
278   GtkBin *bin = GTK_BIN (widget);
279   
280   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
281   
282   if (bin->child &&
283       GTK_WIDGET_VISIBLE (bin->child) &&
284       !GTK_WIDGET_MAPPED (bin->child))
285     gtk_widget_map (bin->child);
286
287   gdk_window_show (widget->window);
288 }
289
290 static void
291 gtk_win32_embed_widget_unmap (GtkWidget *widget)
292 {
293   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
294   gdk_window_hide (widget->window);
295 }
296
297 static void
298 gtk_win32_embed_widget_size_allocate (GtkWidget     *widget,
299                                       GtkAllocation *allocation)
300 {
301   GtkBin *bin = GTK_BIN (widget);
302   
303   widget->allocation = *allocation;
304   
305   if (GTK_WIDGET_REALIZED (widget))
306     gdk_window_move_resize (widget->window,
307                             allocation->x, allocation->y,
308                             allocation->width, allocation->height);
309   
310   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
311     {
312       GtkAllocation child_allocation;
313       
314       child_allocation.x = child_allocation.y = GTK_CONTAINER (widget)->border_width;
315       child_allocation.width =
316         MAX (1, (gint)allocation->width - child_allocation.x * 2);
317       child_allocation.height =
318         MAX (1, (gint)allocation->height - child_allocation.y * 2);
319       
320       gtk_widget_size_allocate (bin->child, &child_allocation);
321     }
322 }
323
324 static void
325 gtk_win32_embed_widget_check_resize (GtkContainer *container)
326 {
327   GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
328 }
329
330 static gboolean
331 gtk_win32_embed_widget_focus (GtkWidget        *widget,
332                               GtkDirectionType  direction)
333 {
334   GtkBin *bin = GTK_BIN (widget);
335   GtkWin32EmbedWidget *embed_widget = GTK_WIN32_EMBED_WIDGET (widget);
336   GtkWindow *window = GTK_WINDOW (widget);
337   GtkContainer *container = GTK_CONTAINER (widget);
338   GtkWidget *old_focus_child = container->focus_child;
339   GtkWidget *parent;
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 (window->focus_widget)
349         {
350           /* Wrapped off the end, clear the focus setting for the toplevel */
351           parent = window->focus_widget->parent;
352           while (parent)
353             {
354               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
355               parent = GTK_WIDGET (parent)->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       if (bin->child && gtk_widget_child_focus (bin->child, direction))
365         return TRUE;
366     }
367
368   if (!GTK_CONTAINER (window)->focus_child)
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(window)->window, 0);
391 }
392
393 #define __GTK_WIN32_EMBED_WIDGET_C__
394 #include "gtkaliasdef.c"