daydayup
daydayup

Reputation: 2317

how to align widget in GtkGrid

I am new GTK and trying to write a small interface. However, I didn't find a function to help with alignment of the widgets inside a GtkGrid. The following picture shows that (1) the buttons are not of same size (2) the texts inside label are not left-justified. How can I adjust it?

#include <gtk/gtk.h>
//#include <iostream>
//using namespace std;



void load_file(GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
gint res;

dialog = gtk_file_chooser_dialog_new ("Open File",
                                      NULL,
                                      action,
                                      "Cancel",
                                      GTK_RESPONSE_CANCEL,
                                      "Open",
                                      GTK_RESPONSE_ACCEPT,
                                      NULL);

res = gtk_dialog_run (GTK_DIALOG (dialog));
if (res == GTK_RESPONSE_ACCEPT)
  {
    char *filename;
    GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
    filename = gtk_file_chooser_get_filename (chooser);
    //open_file (filename);
    //g_free (filename);
  }

gtk_widget_destroy (dialog);

}


int main( int   argc,char *argv[] )
{
    gtk_init (&argc, &argv);

    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window),800,400);

    GtkWidget *lbl_ldmap = gtk_label_new("Load Map");
    GtkWidget *lbl_capacity = gtk_label_new("Capacity");
    GtkWidget *lbl_npassenger = gtk_label_new("Passengers");
    GtkWidget *lbl_nveh = gtk_label_new("Vehicles");
    GtkWidget *lbl_optby = gtk_label_new("Optimize By");

//    gtk_label_set_justify(GTK_LABEL(lbl_ldmap),gtk_label_set_justify);


    GtkWidget *btn_fchoose = gtk_button_new_with_label("...");
    GtkWidget *btn_start = gtk_button_new_with_label("Start");
    GtkWidget *btn_save = gtk_button_new_with_label("Save");
    GtkWidget *btn_about = gtk_button_new_with_label("About");
    GtkWidget *btn_close = gtk_button_new_with_label("Close");


    GtkWidget *mainwindow = gtk_grid_new();
    gtk_grid_set_row_spacing (GTK_GRID (mainwindow), 16);

    gtk_grid_attach(GTK_GRID(mainwindow),lbl_ldmap,0,0,2,2);  
    gtk_grid_attach(GTK_GRID(mainwindow),btn_fchoose,3,0,2,2); 
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_capacity,0,2,2,2);
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_npassenger,0,4,2,2);
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_nveh,0,6,2,2);
    gtk_grid_attach(GTK_GRID(mainwindow),lbl_optby,0,8,2,2);

    gtk_grid_attach(GTK_GRID(mainwindow),btn_start,0,10,5,2);
    gtk_grid_attach(GTK_GRID(mainwindow),btn_save,6,10,5,2);
    gtk_grid_attach(GTK_GRID(mainwindow),btn_about,0,12,5,2);
    gtk_grid_attach(GTK_GRID(mainwindow),btn_close,6,12,5,2);


    GtkWidget *area = gtk_drawing_area_new();
    gtk_grid_attach(GTK_GRID(mainwindow),area,13,0,20,20);

    gtk_container_add(GTK_CONTAINER(window),mainwindow);

    g_signal_connect(G_OBJECT(btn_fchoose),"clicked",
                       G_CALLBACK(load_file),NULL);





    gtk_widget_show_all(window);

    gtk_main ();

    return(0);
}

enter image description here

Upvotes: 1

Views: 1659

Answers (1)

andlabs
andlabs

Reputation: 11588

Container widgets like GtkBox and GtkGrid will not fill the available space unless they know how to fill them. You need to make one or more of the children of the GtkBox or GtkGrid expand by setting the hexpand, halign, vexpand, and/or valign properties on those widgets (NOT on the grid itself!). For more details, see this page.

Upvotes: 3

Related Questions