I have code with the following two lines. (See below for full working example.)
/* Instantiate a font button */
GtkWidget *font_button = gtk_font_button_new ();
/* Set the font to an existing font on the system */
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(font_button),"Monospace Bold 12");
These two lines open the font chooser, but the first font in the list is selected. I want the font Monospace Bold
to be selected. How do I achieve this effect? TYIA.
Full working example
#include <gtk/gtk.h>
/* Compile with gcc `pkg-config --cflags gtk4` main.c `pkg-config --libs gtk4` */
void activate(GtkApplication *app, gpointer data) {
GtkWidget *window = gtk_application_window_new(app);
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
/* Instantiate a font button */
GtkWidget *font_button = gtk_font_button_new ();
/* Set the font to an existing font on the system */
gtk_font_chooser_set_font(GTK_FONT_CHOOSER(font_button),"Monospace Bold 12");
gtk_box_append(GTK_BOX(box), font_button);
gtk_window_set_child(GTK_WINDOW(window), box);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char *argv[]) {
GtkApplication *app;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref(app);
return 1;
}
There is already one example:
https://stackoverflow.com/a/78990144/22768315
The point is that the string must follow exactly the following scheme:
"[FAMILY-LIST] [STYLE-OPTIONS] [SIZE] [VARIATIONS]"
Further explanations can be found in the documentation of GTK4:
https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html
Have fun testing.