Add file type filters to the file open dialog

FWIW, since I've had to do this in another project already.
This commit is contained in:
Přemysl Eric Janouch 2021-10-23 23:56:59 +02:00
parent de9e91e9a5
commit befa7df481
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 12 additions and 2 deletions

View File

@ -356,13 +356,23 @@ open(const gchar *path)
static void static void
on_open(void) on_open(void)
{ {
// TODO(p): Populate and pass a GtkFileFilter.
// If we want to keep this functionality, that is.
GtkWidget *dialog = gtk_file_chooser_dialog_new("Open file", GtkWidget *dialog = gtk_file_chooser_dialog_new("Open file",
GTK_WINDOW(g.window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_WINDOW(g.window), GTK_FILE_CHOOSER_ACTION_OPEN,
"_Cancel", GTK_RESPONSE_CANCEL, "_Cancel", GTK_RESPONSE_CANCEL,
"_Open", GTK_RESPONSE_ACCEPT, NULL); "_Open", GTK_RESPONSE_ACCEPT, NULL);
// NOTE: gdk-pixbuf has gtk_file_filter_add_pixbuf_formats().
GtkFileFilter *filter = gtk_file_filter_new();
for (gsize i = 0; i < G_N_ELEMENTS(supported_media_types); i++)
gtk_file_filter_add_mime_type(filter, supported_media_types[i]);
gtk_file_filter_set_name(filter, "Supported images");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
GtkFileFilter *all_files = gtk_file_filter_new();
gtk_file_filter_set_name(all_files, "All files");
gtk_file_filter_add_pattern(all_files, "*");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), all_files);
// The default is local-only, single item. Paths are returned absolute. // The default is local-only, single item. Paths are returned absolute.
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
gchar *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); gchar *path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));