Adding A Label At A Fixed Position
MainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); //GTK_WINDOW_TOPLEVEL = Has a titlebar and border, managed by the window manager.
//Get screen size
int ScreenWidth;
int ScreenHeight;
GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(MainWindow));
ScreenWidth = gdk_screen_get_width(screen); // in pixels
ScreenHeight = gdk_screen_get_height(screen);
//Create a Fixed Container
GtkWidget *fixed;
fixed = gtk_fixed_new();
gtk_widget_set_size_request(fixed, ScreenWidth, ScreenHeight);
gtk_container_add(GTK_CONTAINER(MainWindow), fixed);
gtk_widget_show(fixed);
//Add status label
GtkWidget *StatusLabel;
StatusLabel = gtk_label_new(NULL);
gtk_label_set_markup(GTK_LABEL(StatusLabel), "<span foreground=\"white\" size=\"x-large\">Hello World</span>");
gtk_label_set_justify(GTK_LABEL(StatusLabel), GTK_JUSTIFY_CENTER);
gtk_fixed_put(GTK_FIXED(fixed), StatusLabel, 10, 20); //x, y from top left
gtk_widget_show_all(MainWindow);
Update Label
gtk_label_set_markup(GTK_LABEL(StatusLabel), "<span foreground=\"white\" size=\"x-large\">Helloooooooooooooo\nWorld</span>");
#define KEYBOARD_TEXT_SPAN_MARKUP "<span font_desc=\"Roboto\" foreground=\"black\" size=\"x-large\" weight=\"bold\" >"
char temp_string[512];
strcpy(temp_string, KEYBOARD_TEXT_SPAN_MARKUP);
strcat(temp_string, g_markup_escape_text(&edit_text[0], -1)); //g_markup_escape_text will escape any special (&, <, >) characters in your string
strcat(temp_string, "</span>");
gtk_label_set_markup(GTK_LABEL(lblKeyboardText), temp_string);
Styling Text
See the available Pango styling options here.
Setting A Font Name
gtk_label_set_markup(GTK_LABEL(StatusLabel), "<span font_desc=\"URW Palladio L 20\" foreground=\"white\" size=\"x-large\">Hello World</span>");
Font Size
gtk_label_set_markup(GTK_LABEL(TimerLabel), "<span foreground=\"white\" font_desc=\"40.0\">Hello World</span>");
Using Newly Installed Fonts
We originally copied new ttf fonts into ~/.fonts (/home/pi/.fonts ) and couldn’t use them in GTK. We moved them to /usr/share/fonts/truetype, rebooted and they worked fine, e.g.
gtk_label_set_markup(GTK_LABEL(StatusLabel), "<span font_desc=\"Arcade Interlaced 20\" foreground=\"white\" size=\"x-large\">Hello World</span>");
Multi Line
Just use newline “\n” in the string.
Alignment
Centre align a label:
gtk_widget_set_size_request(lblStatus, 200, 0); //Set width of the label (so centre align will work)
gtk_widget_set_halign(lblStatus, GTK_ALIGN_CENTER);
gtk_fixed_put(GTK_FIXED(fixed), lblStatus, 396, 76); //x, y from top left
Alignment of multi line labels
gtk_label_set_justify() sets the alignment of the lines in the text of the label relative to each other. It has no effect on labels containing only a single line.
gtk_label_set_justify(GTK_LABEL(StatusLabel), GTK_JUSTIFY_CENTER); //Only sets alignment of multiple lines
Delete label
gtk_widget_destroy(lblStatus);
lblStatus = NULL;
You can safely used destroy on a widget that hasn’t actually been displayed yet. You can re-create and place the widget again later after it has previously been destroyed, you’re just removing its current usage.
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.