Reputation: 7198
I have a Gtk Label
and I want to show a single line text onto it. For example the text is:
Linux is a Unix-like computer operating system
Now I want to display this text in the centre of the label. But the problem is it only appears on the top.
Here is the code I am using for it:
Message_Label = gtk_label_new (" ");
gtk_grid_attach (GTK_GRID (grid), Message_Label, 0, 1, 2, 1);
gtk_label_set_line_wrap_mode(GTK_LABEL(Message_Label),PANGO_WRAP_WORD);
gtk_label_set_line_wrap(GTK_LABEL(Message_Label),TRUE);
gtk_misc_set_alignment (GTK_MISC (Message_Label), 0.5, 0.5);
gtk_label_set_justify(GTK_LABEL(Message_Label),GTK_JUSTIFY_CENTER);
and this is how it looks like:
Can anyone give me any ideas on what is wrong here.
Upvotes: 1
Views: 2549
Reputation: 2198
You should the vexpand property to make the label fill the free vertical space. See the working sample:
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
GtkWidget *window, *grid, *label;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "App Sample");
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
label = gtk_label_new ("Linux is a Unix-like computer operating system ");
gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
gtk_widget_set_vexpand (label, TRUE);
gtk_widget_set_hexpand (label, TRUE);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
But as José Fonte asked, why are you using a Grid when you only have a label? Probably to make the question simpler, but out of its original context, the grid seems bloat.
I'd recommend Glade to create the UI, your code will be cleaner and you'll easily put your UI together without fighting the API and wasting hours in a try-compile-assert-repeat cycle. GtkInspector is also of great help.
Upvotes: 3