1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
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
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.
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"
45 #include "maiRedundantObjectFactory.h"
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
,
65 gpointer callback_data
);
66 static void moz_container_add (GtkContainer
*container
,
69 typedef struct _MozContainerChild MozContainerChild
;
71 struct _MozContainerChild
{
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
;
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 */
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 */
101 (GInstanceInitFunc
) moz_container_init
, /* instance_init */
102 NULL
, /* value_table */
105 moz_container_type
= g_type_register_static (GTK_TYPE_CONTAINER
,
107 &moz_container_info
, 0);
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(),
113 mai_redundant_object_factory_get_type());
117 return moz_container_type
;
121 moz_container_new (void)
123 MozContainer
*container
;
125 container
= g_object_new (MOZ_CONTAINER_TYPE
, NULL
);
127 return GTK_WIDGET(container
);
131 moz_container_put (MozContainer
*container
, GtkWidget
*child_widget
,
134 MozContainerChild
*child
;
136 child
= g_new (MozContainerChild
, 1);
138 child
->widget
= child_widget
;
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
));
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
);
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
);
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
;
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
),
207 /* Mozilla uses the the gdbrgb colormap and visual throughout the
208 backend so for widgets we just use that colormap instead of the
210 gtk_widget_set_colormap(GTK_WIDGET(container
), gdk_rgb_get_colormap());
214 moz_container_map (GtkWidget
*widget
)
216 MozContainer
*container
;
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
;
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
);
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
);
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
|
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
|
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
);
298 moz_container_size_allocate (GtkWidget
*widget
,
299 GtkAllocation
*allocation
)
301 MozContainer
*container
;
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",
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
) {
326 widget
->allocation
= *allocation
;
328 tmp_list
= container
->children
;
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
);
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
);
374 g_object_ref(parent_window
);
376 gtk_widget_unparent(child_widget
);
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
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
);
397 moz_container_forall (GtkContainer
*container
, gboolean include_internals
,
398 GtkCallback callback
, gpointer callback_data
)
400 MozContainer
*moz_container
;
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
;
410 MozContainerChild
*child
;
411 child
= tmp_list
->data
;
412 tmp_list
= tmp_list
->next
;
413 (* callback
) (child
->widget
, callback_data
);
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
);
435 moz_container_get_child (MozContainer
*container
, GtkWidget
*child_widget
)
439 tmp_list
= container
->children
;
441 MozContainerChild
*child
;
443 child
= tmp_list
->data
;
444 tmp_list
= tmp_list
->next
;
446 if (child
->widget
== child_widget
)
454 moz_container_add(GtkContainer
*container
, GtkWidget
*widget
)
456 moz_container_put(MOZ_CONTAINER(container
), widget
, 0, 0);