Introduction
This example is similar to the example for GtkText except that here we have added horizontal and vertical scrollbars. Also, in the GtkText example, we packed the text area directly inside the main window. That won’t work here because we have more than one widget to show. In this example, we use a 2×2 GtkTable to control the widget layout. In the upper left corner is the text area. The upper right corner contains the vertical scrollbar, the lower left corner contains the horizontal scrollbar, and the lower right corner is empty.
Source Code
/*
*File name: scrollbar.c
*/#include
#include/*– This function allows the program to exit properly when the window is closed –*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
g_print (“Quitting…\n”);
gtk_main_quit();
return (FALSE);
}int main (int argc, char *argv[])
{
/*– Declare the GTK Widgets used in the program –*/
GtkWidget *window;
GtkWidget *text;
GtkWidget *table;
GtkWidget *hscrollbar;
GtkWidget *vscrollbar;/*– Create some text to put into the text area –*/
gchar *buffer = “Line 1 \nLine 2 \nLine 3 \nLine 4 \nLine 5 \nLine 6 \nLine 7
\nLine 8 \nLine 9 \nLine 10 \nLine 11 \nLine 12″;/*– Initialize GTK –*/
gtk_init (&argc, &argv);/*– Create the new window –*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);/*– Create the packing table –*/
table = gtk_table_new(2, 2, FALSE);/*– Create a text area –*/
text = gtk_text_new(NULL, NULL);/*– Create the vertical and horizontal scrollbars –*/
hscrollbar = gtk_hscrollbar_new(GTK_TEXT (text) -> hadj);
vscrollbar = gtk_vscrollbar_new(GTK_TEXT (text) -> vadj);/*– Set text area to be editable –*/
gtk_text_set_editable(GTK_TEXT (text), TRUE);/*– Connect the window to the destroyapp function –*/
gtk_signal_connect(GTK_OBJECT(window), “delete_event”, GTK_SIGNAL_FUNC(destroyapp), NULL);/*– Add some text to the window –*/
gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, strlen(buffer));/*– Add the items to the table –*/
gtk_table_attach(GTK_TABLE(table), text, 0, 1, 0, 1, GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
gtk_table_attach(GTK_TABLE(table), hscrollbar, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
gtk_table_attach(GTK_TABLE(table), vscrollbar, 1, 2, 0, 1, GTK_FILL, GTK_EXPAND | GTK_FILL | GTK_SHRINK , 0, 0);/*– Add the table to the window –*/
gtk_container_add(GTK_CONTAINER(window), table);/*– Set window border to zero so that text area takes up the whole window –*/
gtk_container_border_width (GTK_CONTAINER (window), 0);/*– Set the window title –*/
gtk_window_set_title(GTK_WINDOW (window), “Scrollbars”);/*– Display the widgets –*/
gtk_widget_show(text);
gtk_widget_show(table);
gtk_widget_show(hscrollbar);
gtk_widget_show(vscrollbar);
gtk_widget_show(window);/*– Start the GTK event loop –*/
gtk_main();/*– Return 0 if exit is successful –*/
return 0;
}
Compile the Source Code
gcc -Wall -g scrollbars.c -o scrollbars `gtk-config –cflags` `gtk-config –libs`
Execute the Program
./scrollbars






