GTK Box Layout

Advertisement

A GTK box layout widget in acts as an invisible container, it can hold unlimited number of widgets but acts as a single widget to its parents.

prerequisite:

Creating a Box Layout

GtkWidget * gtk_box_new (GtkOrientation orientation,  gint spacing);

gtk_box_new – creates a new box widget in which we can add multiple widgets.

The first parameter it takes is orientation of type GtkOrientation. This represents the orientation of widgets.

  1. GTK_ORIENTATION_HORIZONTAL
  2. GTK_ORIENTATION_VERTICAL

The second parameter is the spacing between the widgets.

Add Widgets to GTK Box

In a box layout, we can either stack widgets from the start or the end, using the function:

  • gtk_box_pack_start
void gtk_box_pack_start (GtkBox *box, GtkWidget *child, gboolean expand, gboolean fill, guint padding);

Packs child to the box, with reference to the start of box.

  • gtk_box_pack_end
void gtk_box_pack_end (GtkBox *box, GtkWidget *child, gboolean expand, gboolean fill, guint padding);

Packs child to the box, with reference to the end of box.

Parameters:

  • GtkBox *box – The first parameter is the reference of the box. The gtk_box_new() returns a GtkWidget and must be cast to the GtkBox type.
  • GtkWidget *child – The Second parameter takes the child widget of type GtkWidget, which is to be placed inside the box.

The next two parameters are of type gboolean (True or False).

  • Expand – True if the new child is to be given extra space allocated to the box.
  • Fill – True if space is given to the child by the expand the option is actually allocated to the child, rather than just padding it.

Note: Fill parameter has no effect if expand is set to FALSE.

  • guint padding – puts extra space in pixels between this child and its neighbors.

Note: If you have already defined space between widgets when creating a box layout, this padding parameter will be applied to the space already given and will not override it.

Example:

Let’s bring all this together in an example:

include 
 int main(int argc,char *argv[])
 {
     gtk_init(&argc,&argv);
     GtkWidget *win, *label, *btn, *vbox;

     win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     g_signal_connect(win,"delete_event",gtk_main_quit,NULL); 

     label = gtk_label_new("Label 1"); 
     btn = gtk_button_new_with_label("Button 1"); 

     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL,10); 
     gtk_box_pack_start(GTK_BOX(vbox),label,0,0,0); 
     gtk_box_pack_start(GTK_BOX(vbox),btn,0,0,0); 

     gtk_container_add(GTK_CONTAINER(win),vbox); 

     gtk_widget_show_all(win); 
     gtk_main(); 
     return 0;
 }
GTK Box Layout
GTK Box Layout

source:

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top