I want to use font dialog on gtk4. This is how I did it for gtk3 using GtkFontChooserDialog–
static gboolean filter_font_cb (const PangoFontFamily *family,
const PangoFontFace *face,
gpointer data)
{
const char *alias_families[] = {
"DejaVu Sans Mono",
"FreeMono",
"Liberation Mono",
"Mitra",
"Monospace",
"Nimbus Mono PS",
"Noto Mono",
"Tlwg Mono",
"Tlwg Typo",
"Ubuntu Mono",
NULL
};
const char *family_name;
family_name = pango_font_family_get_name (PANGO_FONT_FAMILY (family));
return g_strv_contains (alias_families, family_name);
}
void select_font(GtkWidget *widget, gpointer data) {
GtkResponseType result;
GtkWidget *dialog = gtk_font_chooser_dialog_new("Select Font", (GtkWindow *)preferences);
gtk_font_chooser_set_font ((GtkFontChooser *)dialog, data);
gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (dialog), filter_font_cb, NULL, NULL);
result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY) {
gchar *fontname = gtk_font_chooser_get_font((GtkFontChooser *)dialog);
printf("%sn", fontname);
g_free(fontname);
}
gtk_widget_destroy(dialog);
}
But since gtk 4.10 GtkFontChooserDialog is Deprecated. It has been replaced with GtkFontDialog. Can someone tell me how to implement GtkFontDialog?
On gtk4 i tried this way but unable to filter font–
GtkFontDialog *fontDialog = gtk_font_dialog_new ();
GtkWidget *fontButton = gtk_font_dialog_button_new(fontDialog);
2
Answers
Gtk4 has the claim to work object-oriented and with lists.
Accordingly, the fonts of GtkFontDialog are represented as GtkFilterListModel. So if you only want to show one selection, you need a GtkFilter. Since GtkFilter is an abstract class, you have to derive your own and implement the filter criteria there. For this purpose, the function ‘match’ is overwritten, which iterated with the parameter "item" over all fonds passed by GtkFontDialog.
"item" is passed as PangoFondFace.A return value of TRUE means: the fond is displayed. FALSE, it will not be displayed.
A simple variant can look like this:
class-font-filter.h
class-font-filter.c
This is then integrated into the program:
font-dialog.h
font-dialog.c
And the completeness sake:
main-font-dialog.c
If you want to make it a little more elaborate, you can also put the fonts to be selected in a list, which is then given when the class FontFilter is initiated.Of course, then, the function font-filter-match(…)
be adjusted accordingly.
But the principle should have become clear.
Alternatively, there is also the possibility to write an own PangoFontMap class, which can then also be derived from the corresponding abstract class. Their object instance can then be used with gtk-font-dialog-set-font-map() to be made available to the GtkFond dialog.
Have fun experimenting.
Since I am interested in the matter itself, I searched and experimented a little. In fact, there is a simpler way to provide the selection for the
GtkFondDialog
.For this purpose, Gtk4 offers theGtkCustomFilter
, i.e. a filter object, which has already been derived from GtkFilter.In my opinion, this makes it even easier than with Gtk3.
Here the code:
Have fun programming with GTK4