Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Gtk+/Gnome Application Development
Prev Home Next

Size Negotiation

The size negotiation process has already been described in the section called Size Allocation in the chapter called GTK+ Basics and the section called Size Negotiation. The two signals/methods involved are size_request and size_allocate. GtkWidget provides default implementations of each.

Here is the default size_request method:


static void
gtk_widget_real_size_request (GtkWidget         *widget,
                              GtkRequisition    *requisition)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WIDGET (widget));

  requisition->width = widget->requisition.width;
  requisition->height = widget->requisition.height;
}

      

This implementation is appropriate for widgets that always request the same size; GtkArrow and GtkDrawingArea, for example, use this default. Widgets using the default must initialize widget->requisition with their fixed request; GtkArrow does this in gtk_arrow_init(). GtkDrawingArea begins with a default size, but allows users to change the request with gtk_drawing_area_set_size().

Widgets whose request depends on their children, or the amount of text they contain, or some other factor, should override the default size request method with a method that calculates the size they want.

Size allocation is mildly more complicated; here is its default implementation:


static void
gtk_widget_real_size_allocate (GtkWidget     *widget,
                               GtkAllocation *allocation)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WIDGET (widget));

  widget->allocation = *allocation;
  
  if (GTK_WIDGET_REALIZED (widget) &&
      !GTK_WIDGET_NO_WINDOW (widget))
     {
        gdk_window_move_resize (widget->window,
                                allocation->x, allocation->y,
                                allocation->width, allocation->height);
     }
}
      

This should suffice for most simple widgets. Widgets like GtkEv, or any container, need to update internal data structures or distribute the allocation they receive among child widgets; these widgets will override size_allocate. It is possible but not required to chain up to the default implementation.

The wrapper function which emits the "size_allocate" signal is significantly more involved than the signal handlers. gtk_widget_size_allocate() takes into account gtk_widget_set_usize() and ensures widgets are redrawn if their size changes. (Unsurprisingly, gtk_widget_size_request() also exists and should be used instead of emitting "size_request" directly.)

Gtk+/Gnome Application Development
Prev Home Next

 
 
  Published under free license. Design by Interspire