Next: , Previous: GtkButtonBox, Up: Top


129 GtkContainer

Base class for widgets which contain other widgets

129.1 Overview

A GTK+ user interface is constructed by nesting widgets inside widgets. Container widgets are the inner nodes in the resulting tree of widgets: they contain other widgets. So, for example, you might have a <gtk-window> containing a <gtk-frame> containing a GtkLabel. If you wanted an image instead of a textual label inside the frame, you might replace the <gtk-label> widget with a <gtk-image> widget.

There are two major kinds of container widgets in GTK+. Both are subclasses of the abstract <gtk-container> base class.

The first type of container widget has a single child widget and derives from <gtk-bin>. These containers are decorators, which add some kind of functionality to the child. For example, a <gtk-button> makes its child into a clickable button; a <gtk-frame> draws a frame around its child and a <gtk-window> places its child widget inside a top-level window.

The second type of container can have more than one child; its purpose is to manage layout. This means that these containers assign sizes and positions to their children. For example, a <gtk-hbox> arranges its children in a horizontal row, and a <gtk-table> arranges the widgets it contains in a two-dimensional grid.

To fulfill its task, a layout container must negotiate the size requirements with its parent and its children. This negotiation is carried out in two phases, size requisition and size allocation.

129.2 Size Requisition

The size requisition of a widget is it's desired width and height. This is represented by a <gtk-requisition>.

How a widget determines its desired size depends on the widget. A <gtk-label>, for example, requests enough space to display all its text. Container widgets generally base their size request on the requisitions of their children.

The size requisition phase of the widget layout process operates top-down. It starts at a top-level widget, typically a <gtk-window>. The top-level widget asks its child for its size requisition by calling gtk-widget-size-request. To determine its requisition, the child asks its own children for their requisitions and so on. Finally, the top-level widget will get a requisition back from its child.

129.3 Size Allocation

When the top-level widget has determined how much space its child would like to have, the second phase of the size negotiation, size allocation, begins. Depending on its configuration (see gtk-window-set-resizable), the top-level widget may be able to expand in order to satisfy the size request or it may have to ignore the size request and keep its fixed size. It then tells its child widget how much space it gets by calling gtk-widget-size-allocate. The child widget divides the space among its children and tells each child how much space it got, and so on. Under normal circumstances, a <gtk-window> will always give its child the amount of space the child requested.

A child's size allocation is represented by a <gtk-allocation>. This struct contains not only a width and height, but also a position (i.e. X and Y coordinates), so that containers can tell their children not only how much space they have gotten, but also where they are positioned inside the space available to the container.

Widgets are required to honor the size allocation they receive; a size request is only a request, and widgets must be able to cope with any size.

129.4 Child properties

introduces child properties - these are object properties that are not specific to either the container or the contained widget, but rather to their relation. Typical examples of child properties are the position or pack-type of a widget which is contained in a <gtk-box>.

Use gtk-container-class-install-child-property to install child properties for a container class and gtk-container-class-find-child-property or gtk-container-class-list-child-properties to get information about existing child properties.

To set the value of a child property, use gtk-container-child-set-property, gtk-container-child-set or gtk-container-child-set-valist. To obtain the value of a child property, use gtk-container-child-get-property, gtk-container-child-get or gtk-container-child-get-valist. To emit notification about child property changes, use gtk-widget-child-notify.

129.5 Usage

— Class: <gtk-container>

Derives from <gtk-widget>.

This class defines the following slots:

border-width
The width of the empty border outside the containers children
resize-mode
Specify how resize events are handled
child
Can be used to add a new child to the container
— Signal on <gtk-container>: add (arg0 <gtk-widget>)
— Signal on <gtk-container>: remove (arg0 <gtk-widget>)
— Signal on <gtk-container>: check-resize
— Signal on <gtk-container>: set-focus-child (arg0 <gtk-widget>)
— Function: gtk-container-add (self <gtk-container>) (widget <gtk-widget>)
— Method: add

Adds widget to container. Typically used for simple containers such as <gtk-window>, <gtk-frame>, or <gtk-button>; for more complicated layout containers such as <gtk-box> or <gtk-table>, this function will pick default packing parameters that may not be correct. So consider functions such as gtk-box-pack-start and gtk-table-attach as an alternative to gtk-container-add in those cases. A widget may be added to only one container at a time; you can't place the same widget inside two different containers.

container
a <gtk-container>
widget
a widget to be placed inside container
— Function: gtk-container-remove (self <gtk-container>) (widget <gtk-widget>)
— Method: remove

Removes widget from container. widget must be inside container. Note that container will own a reference to widget, and that this may be the last reference held; so removing a widget from its container can destroy that widget. If you want to use widget again, you need to add a reference to it while it's not inside a container, using g-object-ref. If you don't want to use widget again it's usually more efficient to simply destroy it directly using gtk-widget-destroy since this will remove it from the container and help break any circular reference count cycles.

container
a <gtk-container>
widget
a current child of container
— Function: gtk-container-get-resize-mode (self <gtk-container>) ⇒  (ret <gtk-resize-mode>)
— Method: get-resize-mode

Returns the resize mode for the container. See gtk-container-set-resize-mode.

container
a <gtk-container>
ret
the current resize mode
— Function: gtk-container-set-resize-mode (self <gtk-container>) (resize_mode <gtk-resize-mode>)
— Method: set-resize-mode

Sets the resize mode for the container.

The resize mode of a container determines whether a resize request will be passed to the container's parent, queued for later execution or executed immediately.

container
a <gtk-container>.
resize-mode
the new resize mode.
— Function: gtk-container-check-resize (self <gtk-container>)
— Method: check-resize
container
— Function: gtk-container-get-children (self <gtk-container>) ⇒  (ret glist-of)
— Method: get-children

Returns the container's non-internal children. See gtk-container-forall for details on what constitutes an "internal" child.

container
a <gtk-container>.
ret
a newly-allocated list of the container's non-internal children.
— Function: gtk-container-set-focus-child (self <gtk-container>) (child <gtk-widget>)
— Method: set-focus-child
container

child
— Function: gtk-container-get-focus-vadjustment (self <gtk-container>) ⇒  (ret <gtk-adjustment>)
— Method: get-focus-vadjustment

Retrieves the vertical focus adjustment for the container. See gtk-container-set-focus-vadjustment.

container
a <gtk-container>
ret
the vertical focus adjustment, or ‘#f’ if none has been set.
— Function: gtk-container-set-focus-vadjustment (self <gtk-container>) (adjustment <gtk-adjustment>)
— Method: set-focus-vadjustment

Hooks up an adjustment to focus handling in a container, so when a child of the container is focused, the adjustment is scrolled to show that widget. This function sets the vertical alignment. See gtk-scrolled-window-get-vadjustment for a typical way of obtaining the adjustment and gtk-container-set-focus-hadjustment for setting the horizontal adjustment.

The adjustments have to be in pixel units and in the same coordinate system as the allocation for immediate children of the container.

container
a <gtk-container>
adjustment
an adjustment which should be adjusted when the focus is moved among the descendents of container
— Function: gtk-container-get-focus-hadjustment (self <gtk-container>) ⇒  (ret <gtk-adjustment>)
— Method: get-focus-hadjustment

Retrieves the horizontal focus adjustment for the container. See gtk-container-set-focus-hadjustment.

container
a <gtk-container>
ret
the horizontal focus adjustment, or ‘#f’ if none has been set.
— Function: gtk-container-set-focus-hadjustment (self <gtk-container>) (adjustment <gtk-adjustment>)
— Method: set-focus-hadjustment

Hooks up an adjustment to focus handling in a container, so when a child of the container is focused, the adjustment is scrolled to show that widget. This function sets the horizontal alignment. See gtk-scrolled-window-get-hadjustment for a typical way of obtaining the adjustment and gtk-container-set-focus-vadjustment for setting the vertical adjustment.

The adjustments have to be in pixel units and in the same coordinate system as the allocation for immediate children of the container.

container
a <gtk-container>
adjustment
an adjustment which should be adjusted when the focus is moved among the descendents of container
— Function: gtk-container-resize-children (self <gtk-container>)
— Method: resize-children
container
— Function: gtk-container-child-type (self <gtk-container>) ⇒  (ret <gtype>)
— Method: child-type

Returns the type of the children supported by the container.

Note that this may return ‘G_TYPE_NONE’ to indicate that no more children can be added, e.g. for a <gtk-paned> which already has two children.

container
a <gtk-container>.
ret
a <g-type>.
— Function: gtk-container-child-get-property (self <gtk-container>) (child <gtk-widget>) (property_name mchars) (value <gvalue>)
— Method: child-get-property

Gets the value of a child property for child and container.

container
a <gtk-container>
child
a widget which is a child of container
property-name
the name of the property to get
value
a location to return the value
— Function: gtk-container-child-set-property (self <gtk-container>) (child <gtk-widget>) (property_name mchars) (value <gvalue>)
— Method: child-set-property

Sets a child property for child and container.

container
a <gtk-container>
child
a widget which is a child of container
property-name
the name of the property to set
value
the value to set the property to
— Function: gtk-container-get-border-width (self <gtk-container>) ⇒  (ret unsigned-int)
— Method: get-border-width

Retrieves the border width of the container. See gtk-container-set-border-width.

container
a <gtk-container>
ret
the current border width
— Function: gtk-container-set-border-width (self <gtk-container>) (border_width unsigned-int)
— Method: set-border-width

Sets the border width of the container.

The border width of a container is the amount of space to leave around the outside of the container. The only exception to this is <gtk-window>; because toplevel windows can't leave space outside, they leave the space inside. The border is added on all sides of the container. To add space to only one side, one approach is to create a <gtk-alignment> widget, call gtk-widget-set-usize to give it a size, and place it on the side of the container as a spacer.

container
a <gtk-container>
border-width
amount of blank space to leave outside the container. Valid values are in the range 0-65535 pixels.
— Function: gtk-container-propagate-expose (self <gtk-container>) (child <gtk-widget>) (event <gdk-event-expose>)
— Method: propagate-expose

When a container receives an expose event, it must send synthetic expose events to all children that don't have their own <gdk-windows>. This function provides a convenient way of doing this. A container, when it receives an expose event, calls gtk-container-propagate-expose once for each child, passing in the event the container received.

gtk-container-propagate-expose takes care of deciding whether an expose event needs to be sent to the child, intersecting the event's area with the child area, and sending the event.

In most cases, a container can simply either simply inherit the ::expose implementation from <gtk-container>, or, do some drawing and then chain to the ::expose implementation from <gtk-container>.

container
a <gtk-container>
child
a child of container
event
a expose event sent to container
— Function: gtk-container-set-focus-chain (self <gtk-container>) (focusable_widgets glist-of)
— Method: set-focus-chain

Sets a focus chain, overriding the one computed automatically by GTK+.

In principle each widget in the chain should be a descendant of the container, but this is not enforced by this method, since it's allowed to set the focus chain before you pack the widgets, or have a widget in the chain that isn't always packed. The necessary checks are done when the focus chain is actually traversed.

container
a <gtk-container>.
focusable-widgets
the new focus chain.
— Function: gtk-container-unset-focus-chain (self <gtk-container>)
— Method: unset-focus-chain

Removes a focus chain explicitly set with gtk-container-set-focus-chain.

container
a <gtk-container>.