CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / gtk2 / mozcontainer.c
blob68e4ebfdb6839b16687466045cf6c58240873480
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is mozilla.org code.
19 * The Initial Developer of the Original Code is Christopher Blizzard
20 * <blizzard@mozilla.org>. Portions created by the Initial Developer
21 * are Copyright (C) 2001 the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "mozcontainer.h"
40 #include <gtk/gtk.h>
41 #include <stdio.h>
43 #ifdef ACCESSIBILITY
44 #include <atk/atk.h>
45 #include "maiRedundantObjectFactory.h"
46 #endif
48 /* init methods */
49 static void moz_container_class_init (MozContainerClass *klass);
50 static void moz_container_init (MozContainer *container);
52 /* widget class methods */
53 static void moz_container_map (GtkWidget *widget);
54 static void moz_container_unmap (GtkWidget *widget);
55 static void moz_container_realize (GtkWidget *widget);
56 static void moz_container_size_allocate (GtkWidget *widget,
57 GtkAllocation *allocation);
59 /* container class methods */
60 static void moz_container_remove (GtkContainer *container,
61 GtkWidget *child_widget);
62 static void moz_container_forall (GtkContainer *container,
63 gboolean include_internals,
64 GtkCallback callback,
65 gpointer callback_data);
66 static void moz_container_add (GtkContainer *container,
67 GtkWidget *widget);
69 typedef struct _MozContainerChild MozContainerChild;
71 struct _MozContainerChild {
72 GtkWidget *widget;
73 gint x;
74 gint y;
77 static void moz_container_allocate_child (MozContainer *container,
78 MozContainerChild *child);
79 static MozContainerChild *
80 moz_container_get_child (MozContainer *container, GtkWidget *child);
82 static GtkContainerClass *parent_class = NULL;
84 /* public methods */
86 GType
87 moz_container_get_type(void)
89 static GType moz_container_type = 0;
91 if (!moz_container_type) {
92 static GTypeInfo moz_container_info = {
93 sizeof(MozContainerClass), /* class_size */
94 NULL, /* base_init */
95 NULL, /* base_finalize */
96 (GClassInitFunc) moz_container_class_init, /* class_init */
97 NULL, /* class_destroy */
98 NULL, /* class_data */
99 sizeof(MozContainer), /* instance_size */
100 0, /* n_preallocs */
101 (GInstanceInitFunc) moz_container_init, /* instance_init */
102 NULL, /* value_table */
105 moz_container_type = g_type_register_static (GTK_TYPE_CONTAINER,
106 "MozContainer",
107 &moz_container_info, 0);
108 #ifdef ACCESSIBILITY
109 /* Set a factory to return accessible object with ROLE_REDUNDANT for
110 * MozContainer, so that gail won't send focus notification for it */
111 atk_registry_set_factory_type(atk_get_default_registry(),
112 moz_container_type,
113 mai_redundant_object_factory_get_type());
114 #endif
117 return moz_container_type;
120 GtkWidget *
121 moz_container_new (void)
123 MozContainer *container;
125 container = g_object_new (MOZ_CONTAINER_TYPE, NULL);
127 return GTK_WIDGET(container);
130 void
131 moz_container_put (MozContainer *container, GtkWidget *child_widget,
132 gint x, gint y)
134 MozContainerChild *child;
136 child = g_new (MozContainerChild, 1);
138 child->widget = child_widget;
139 child->x = x;
140 child->y = y;
142 /* printf("moz_container_put %p %p %d %d\n", (void *)container,
143 (void *)child_widget, x, y); */
145 container->children = g_list_append (container->children, child);
147 /* we assume that the caller of this function will have already set
148 the parent GdkWindow because we can have many anonymous children. */
149 gtk_widget_set_parent(child_widget, GTK_WIDGET(container));
152 void
153 moz_container_move (MozContainer *container, GtkWidget *child_widget,
154 gint x, gint y, gint width, gint height)
156 MozContainerChild *child;
157 GtkAllocation new_allocation;
159 child = moz_container_get_child (container, child_widget);
161 child->x = x;
162 child->y = y;
164 new_allocation.x = x;
165 new_allocation.y = y;
166 new_allocation.width = width;
167 new_allocation.height = height;
169 /* printf("moz_container_move %p %p will allocate to %d %d %d %d\n",
170 (void *)container, (void *)child_widget,
171 new_allocation.x, new_allocation.y,
172 new_allocation.width, new_allocation.height); */
174 gtk_widget_size_allocate(child_widget, &new_allocation);
177 /* static methods */
179 void
180 moz_container_class_init (MozContainerClass *klass)
182 /*GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass); */
184 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
185 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
187 parent_class = g_type_class_peek_parent (klass);
189 widget_class->map = moz_container_map;
190 widget_class->unmap = moz_container_unmap;
191 widget_class->realize = moz_container_realize;
192 widget_class->size_allocate = moz_container_size_allocate;
194 container_class->remove = moz_container_remove;
195 container_class->forall = moz_container_forall;
196 container_class->add = moz_container_add;
199 void
200 moz_container_init (MozContainer *container)
202 GTK_WIDGET_SET_FLAGS(container, GTK_CAN_FOCUS);
203 container->container.resize_mode = GTK_RESIZE_IMMEDIATE;
204 gtk_widget_set_redraw_on_allocate(GTK_WIDGET(container),
205 FALSE);
207 /* Mozilla uses the the gdbrgb colormap and visual throughout the
208 backend so for widgets we just use that colormap instead of the
209 default one. */
210 gtk_widget_set_colormap(GTK_WIDGET(container), gdk_rgb_get_colormap());
213 void
214 moz_container_map (GtkWidget *widget)
216 MozContainer *container;
217 GList *tmp_list;
218 GtkWidget *tmp_child;
220 g_return_if_fail (IS_MOZ_CONTAINER(widget));
221 container = MOZ_CONTAINER (widget);
223 GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
225 tmp_list = container->children;
226 while (tmp_list) {
227 tmp_child = ((MozContainerChild *)tmp_list->data)->widget;
229 if (GTK_WIDGET_VISIBLE(tmp_child)) {
230 if (!GTK_WIDGET_MAPPED(tmp_child))
231 gtk_widget_map(tmp_child);
233 tmp_list = tmp_list->next;
236 gdk_window_show (widget->window);
239 void
240 moz_container_unmap (GtkWidget *widget)
242 g_return_if_fail (IS_MOZ_CONTAINER (widget));
244 GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
246 gdk_window_hide (widget->window);
249 void
250 moz_container_realize (GtkWidget *widget)
252 GdkWindowAttr attributes;
253 gint attributes_mask = 0;
254 MozContainer *container;
256 g_return_if_fail(IS_MOZ_CONTAINER(widget));
258 container = MOZ_CONTAINER(widget);
260 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
262 /* create the shell window */
264 attributes.event_mask = (gtk_widget_get_events (widget) |
265 GDK_EXPOSURE_MASK | GDK_STRUCTURE_MASK |
266 GDK_VISIBILITY_NOTIFY_MASK |
267 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
268 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
269 #ifdef HAVE_GTK_MOTION_HINTS
270 GDK_POINTER_MOTION_HINT_MASK |
271 #endif
272 GDK_POINTER_MOTION_MASK);
273 attributes.x = widget->allocation.x;
274 attributes.y = widget->allocation.y;
275 attributes.width = widget->allocation.width;
276 attributes.height = widget->allocation.height;
277 attributes.wclass = GDK_INPUT_OUTPUT;
278 attributes.visual = gtk_widget_get_visual (widget);
279 attributes.colormap = gtk_widget_get_colormap (widget);
280 attributes.window_type = GDK_WINDOW_CHILD;
282 attributes_mask |= GDK_WA_VISUAL | GDK_WA_COLORMAP |
283 GDK_WA_X | GDK_WA_Y;
285 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
286 &attributes, attributes_mask);
287 /* printf("widget->window is %p\n", (void *)widget->window); */
288 gdk_window_set_user_data (widget->window, container);
290 widget->style = gtk_style_attach (widget->style, widget->window);
292 /* set the back pixmap to None so that you don't end up with the gtk
293 default which is BlackPixel */
294 gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
297 void
298 moz_container_size_allocate (GtkWidget *widget,
299 GtkAllocation *allocation)
301 MozContainer *container;
302 GList *tmp_list;
303 GtkAllocation tmp_allocation;
304 GtkRequisition tmp_requisition;
305 GtkWidget *tmp_child;
307 g_return_if_fail (IS_MOZ_CONTAINER (widget));
309 /* printf("moz_container_size_allocate %p %d %d %d %d\n",
310 (void *)widget,
311 allocation->x,
312 allocation->y,
313 allocation->width,
314 allocation->height); */
316 /* short circuit if you can */
317 container = MOZ_CONTAINER (widget);
318 if (!container->children &&
319 widget->allocation.x == allocation->x &&
320 widget->allocation.y == allocation->y &&
321 widget->allocation.width == allocation->width &&
322 widget->allocation.height == allocation->height) {
323 return;
326 widget->allocation = *allocation;
328 tmp_list = container->children;
330 while (tmp_list) {
331 MozContainerChild *child = tmp_list->data;
333 moz_container_allocate_child (container, child);
335 tmp_list = tmp_list->next;
338 if (GTK_WIDGET_REALIZED (widget)) {
339 gdk_window_move_resize(widget->window,
340 widget->allocation.x,
341 widget->allocation.y,
342 widget->allocation.width,
343 widget->allocation.height);
347 void
348 moz_container_remove (GtkContainer *container, GtkWidget *child_widget)
350 MozContainerChild *child;
351 MozContainer *moz_container;
352 GdkWindow* parent_window;
354 g_return_if_fail (IS_MOZ_CONTAINER(container));
355 g_return_if_fail (GTK_IS_WIDGET(child_widget));
357 moz_container = MOZ_CONTAINER(container);
359 child = moz_container_get_child (moz_container, child_widget);
360 g_return_if_fail (child);
362 /* gtk_widget_unparent will remove the parent window (as well as the
363 * parent widget), but, in Mozilla's window hierarchy, the parent window
364 * may need to be kept because it may be part of a GdkWindow sub-hierarchy
365 * that is being moved to another MozContainer.
367 * (In a conventional GtkWidget hierarchy, GdkWindows being reparented
368 * would have their own GtkWidget and that widget would be the one being
369 * reparented. In Mozilla's hierarchy, the parent_window needs to be
370 * retained so that the GdkWindow sub-hierarchy is maintained.)
372 parent_window = gtk_widget_get_parent_window(child_widget);
373 if (parent_window)
374 g_object_ref(parent_window);
376 gtk_widget_unparent(child_widget);
378 if (parent_window) {
379 /* The child_widget will always still exist because g_signal_emit,
380 * which invokes this function, holds a reference.
382 * If parent_window is the container's root window then it will not be
383 * the parent_window if the child_widget is placed in another
384 * container.
386 if (parent_window != GTK_WIDGET(container)->window)
387 gtk_widget_set_parent_window(child_widget, parent_window);
389 g_object_unref(parent_window);
392 moz_container->children = g_list_remove(moz_container->children, child);
393 g_free(child);
396 void
397 moz_container_forall (GtkContainer *container, gboolean include_internals,
398 GtkCallback callback, gpointer callback_data)
400 MozContainer *moz_container;
401 GList *tmp_list;
403 g_return_if_fail (IS_MOZ_CONTAINER(container));
404 g_return_if_fail (callback != NULL);
406 moz_container = MOZ_CONTAINER(container);
408 tmp_list = moz_container->children;
409 while (tmp_list) {
410 MozContainerChild *child;
411 child = tmp_list->data;
412 tmp_list = tmp_list->next;
413 (* callback) (child->widget, callback_data);
417 static void
418 moz_container_allocate_child (MozContainer *container,
419 MozContainerChild *child)
421 GtkAllocation allocation;
422 GtkRequisition requisition;
424 allocation.x = child->x;
425 allocation.y = child->y;
426 /* gtk_widget_get_child_requisition (child->widget, &requisition); */
427 /* gtk_widget_size_request (child->widget, &requisition); */
428 allocation.width = child->widget->allocation.width;
429 allocation.height = child->widget->allocation.height;
431 gtk_widget_size_allocate (child->widget, &allocation);
434 MozContainerChild *
435 moz_container_get_child (MozContainer *container, GtkWidget *child_widget)
437 GList *tmp_list;
439 tmp_list = container->children;
440 while (tmp_list) {
441 MozContainerChild *child;
443 child = tmp_list->data;
444 tmp_list = tmp_list->next;
446 if (child->widget == child_widget)
447 return child;
450 return NULL;
453 static void
454 moz_container_add(GtkContainer *container, GtkWidget *widget)
456 moz_container_put(MOZ_CONTAINER(container), widget, 0, 0);