3.9 Scrolling

3.9 Scrolling

If you have a widget or collection of widgets that is too large to fit in a window, you can create a smaller viewport and navigate the large item by scrolling . A GtkAdjustment object (see Section 3.6.4) serves as the link between a scrollbar and the widget that you want to view. In MVC terminology, the adjustment object is the model, the viewable area is the view, and the scrollbar is the controller (see Section 3.11).

Horizontal scrollbars in GTK+ are GtkHScrollbar ( GTK_TYPE_HSCROLLBAR ) objects; their vertical counterparts use the class GtkVScrollbar ( GTK_TYPE_VSCROLLBAR ). Both are subclasses of GtkScrollBar .

These classes do not have any significant properties and methods , and furthermore, you should create objects with the generator function gtk_scrollbar_new( adj ) , where adj is an adjustment object (or NULL if you want the generator to create one for you).

Scrollbars are derived from the GtkRange class, so in principle, they can perform operations similar to those of a slider. However, scrollbars also have step increment buttons , and the slider bar itself is proportional to the widget that you're looking at through the viewport. (And, of course, users normally expect that a scrollbar scrolls something. If your users want a slider with a step increment feature, don't use a scrollbar; use a slider with a spin button.)

You normally won't have to deal with individual scrollbars. A special container widget called GtkScrolledWindow ( GTK_TYPE_SCROLLED_WINDOW ) holds exactly one widget. Furthermore, there are special scrolled widgets like GtkTextView (see Section 3.11) that you can pack into a scrolled window that automatically take on the required properties. Section 3.11.4 contains an example for GtkTreeView .

If you want to build an arbitrary scrolled window, you need to build up the content inside a GtkViewport ( GTK_TYPE_VIEWPORT ) object and then pack the viewport into a GtkScrolledWindow object. To do all of this in one step, call

 gtk_scrolled_window_add_with_viewport(  scrolled  ,  widget  ) 

The example you're about to see creates a large table of buttons and places it in a scrolled window. The declarations are as follows :

 /* -*-coding: utf-8;-*- */ /* scroll.c -- demonstrate scrolled window */ #include <gtk/gtk.h>   << standard window handlers >> /* number of buttons to a side */ #define TABLE_SIZE 30 int main(int argc, char **argv) {   GtkWindow *window;   GtkTable *table;   GtkButton *button[TABLE_SIZE][TABLE_SIZE];   GtkScrolledWindow *scr_window;   gint i, j;   gchar *string;   /* initialize GTK+, create window */   gtk_init(&argc, &argv);   window = g_object_new(GTK_TYPE_WINDOW,                         "title", "Scrolled Window",                         "default-width", 600,                         "default-height", 400,                         NULL);   << attach standard handlers to window >> 

If you want to see how well your machine handles GTK+'s memory allocation for a lot of buttons, increase TABLE_SIZE .

Allocating and packing 900 buttons is a boring task suitable for a loop:

 /* create TABLE_SIZE**2 buttons inside the table */   table = g_object_new(GTK_TYPE_TABLE,                        "n-rows", TABLE_SIZE,                        "n-columns", TABLE_SIZE,                        "homogeneous", TRUE,                        NULL);   for (i=0; i < TABLE_SIZE; i++)   {     for (j=0; j < TABLE_SIZE; j++)     {       string = g_strdup_printf("Button (%d, %d)", i, j);       button[i][j] = g_object_new(GTK_TYPE_BUTTON, "label", string, NULL);       g_free(string);       gtk_table_attach_defaults(table, GTK_WIDGET(button[i][j]),                                 i, i+1, j, j+1);     }   } 

Finally, after all of the real work is out of the way, all you need to do is to put this big table into a scrolled window with the help of a viewport. Figure 3.15 shows the final application.

click to expand
Figure 3.15: Scrolled window.
 /* create scrolled window */   scr_window = g_object_new(GTK_TYPE_SCROLLED_WINDOW, NULL);   /* pack the table into the window with a viewport between */   gtk_scrolled_window_add_with_viewport(scr_window, GTK_WIDGET(table));   /* pack scrolled window and show everything */   gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(scr_window));   gtk_widget_show_all(GTK_WIDGET(window));   gtk_main();   return 0; } 

3.9.1 Scrolled Window Properties

The GtkScrolledWindow class has these properties:

  • hadjustment ( GtkAdjustment * ): The GtkAdjustment object for the horizontal position of the child widget.

  • vadjustment ( GtkAdjustment * ): The GtkAdjustment object for the vertical position of the child widget.

  • hscrollbar-policy ( GtkPolicyType ): The policy for showing the horizontal scrollbar. Possible values are

    • GTK_POLICY_ALWAYS : The scrollbar is always visible.

    • GTK_POLICY_AUTOMATIC : The scrollbar is visible only when the viewable area of the child is larger than the viewport.

    • GTK_POLICY_NEVER : The scrollbar is never visible. This is usually not a terribly good idea.

  • vscrollbar-policy ( GtkPolicyType ): Like the preceding , but for vertical scrollbars.

  • window-placement (GtkCornerType): Where the child widget goes relative to the scrollbars. Possible values are

    • GTK_CORNER_TOP_LEFT : Scrollbars at the right and bottom. This is the default.

    • GTK_CORNER_BOTTOM_LEFT : Scrollbars at the right and top.

    • GTK_CORNER_TOP_RIGHT : Scrollbars at the left and bottom.

    • GTK_CORNER_BOTTOM_RIGHT : Scrollbars at the left and top.

  • shadow-type (enumeration): The shadow style of the child widget; values are the same as for GtkFrame (see Section 3.7.1).


If you have a window with a large quantity of vertically scrolled content (such as a text editor or terminal), put the scrollbar at the left, not the right.

The Official GNOME 2 Developers Guide
The Official GNOME 2 Developers Guide
ISBN: 1593270305
EAN: 2147483647
Year: 2004
Pages: 108

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net