Reputation: 3086
I'm currently playing a little bit with the gtk library, and trying to create a simple window application using C, but I'm having some difficult time aligning labels in a table.
To be more specific, this is what I've written so far:
#include<gtk/gtk.h>
static void dest(GtkWidget*, gpointer);
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1, *folderChooser1;
GtkWidget *label2, *folderChooser2;
gtk_init(&argc,&argv);
/*************************************
* setting the first row in table
*************************************/
label1=gtk_label_new("Browse For a Folder To Backup: ");
//gtk_misc_set_alignment(GTK_MISC(label1), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label1, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser1=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser1), g_get_home_dir());
/*************************************
* setting the second row
*************************************/
label2=gtk_label_new("Choose Backup Path: ");
//gtk_misc_set_alignment(GTK_MISC(label2), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label2, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser2=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser2), g_get_home_dir());
/***************************************
* setting table
***************************************/
table=gtk_table_new(2,2,TRUE);
gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 15, 0);
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 15, 0);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser1, 1, 2, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser2, 1, 2, 1, 2);
/********************************
* setting window
********************************/
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Backup");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(dest), NULL);
gtk_container_add(GTK_CONTAINER (window), table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void dest(GtkWidget *window, gpointer data) {
gtk_main_quit();
}
The above code will produce this:
As you can see, the labels are centred, where I want it to be left-alinged.
None of the commented-out lines worked for me.
Is there even a way to align labels inside table cells'?
Upvotes: 2
Views: 1585
Reputation: 3855
gtk_misc_set_alignment
works for me as long as I also use gtk_table_attach_defaults
to attach the labels:
Full code for reference:
#include<gtk/gtk.h>
static void dest(GtkWidget*, gpointer);
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1, *folderChooser1;
GtkWidget *label2, *folderChooser2;
gtk_init(&argc,&argv);
/*************************************
* setting the first row in table
*************************************/
label1=gtk_label_new("Browse For a Folder To Backup: ");
gtk_misc_set_alignment(GTK_MISC(label1), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label1, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser1=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser1), g_get_home_dir());
/*************************************
* setting the second row
*************************************/
label2=gtk_label_new("Choose Backup Path: ");
gtk_misc_set_alignment(GTK_MISC(label2), 0, .5); // neither this
//gtk_label_set_justify(GTK_LABEL(label2, GTK_JUSTIFY_LEFT); // nor this aligns the label to the left.
folderChooser2=gtk_file_chooser_button_new("Browse For Folder...", GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (folderChooser2), g_get_home_dir());
/***************************************
* setting table
***************************************/
table=gtk_table_new(2,2,TRUE);
//gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_SHRINK, GTK_SHRINK, 15, 0);
//gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_SHRINK, GTK_SHRINK, 15, 0);
gtk_table_attach_defaults(GTK_TABLE(table), label1, 0, 1, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(table), label2, 0, 1, 1, 2);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser1, 1, 2, 0, 1);
gtk_table_attach_defaults(GTK_TABLE(table), folderChooser2, 1, 2, 1, 2);
/********************************
* setting window
********************************/
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Backup");
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(dest), NULL);
gtk_container_add(GTK_CONTAINER (window), table);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void dest(GtkWidget *window, gpointer data) {
gtk_main_quit();
}
(tested with gtk 2.0 on Ubuntu 12.04)
Upvotes: 1