Skip to content

Commit

Permalink
LinGUI: Allow use of non-native file chooser
Browse files Browse the repository at this point in the history
Adds a hidden option "NativeFileChooser" to the preferences.
Setting this to false disables the native file chooser dialog,
for situations where the GTK built-in dialog works better.
  • Loading branch information
robxnano authored and galad87 committed Apr 28, 2024
1 parent 21405b6 commit 7274e66
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 121 deletions.
1 change: 1 addition & 0 deletions gtk/data/internal_defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"use_dvdnav": true,
"reduce_hd_preview": true,
"MinTitleDuration": 10,
"NativeFileChooser": true,
"preview_count": 10,
"preview_show_crop": false,
"preview_x": -1,
Expand Down
57 changes: 28 additions & 29 deletions gtk/src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void quit_dialog_show(void);
static void quit_dialog_response(GtkDialog *dialog, int response, gpointer data);
static void queue_done_action(signal_user_data_t *ud);
static gboolean has_drive = FALSE;
static GtkFileChooserNative* source_dialog = NULL;
static GtkFileChooser *source_dialog = NULL;
static void source_dialog_start_scan(GtkFileChooser *chooser, int title_id);

#if !defined(_WIN32)
Expand Down Expand Up @@ -1642,7 +1642,7 @@ single_title_dialog_response (GtkMessageDialog *dialog, int response,
else
{
source_dialog = NULL;
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_destroy(chooser);
}
}

Expand Down Expand Up @@ -1688,7 +1688,7 @@ source_dialog_response_cb(GtkFileChooser *chooser,
else
{
source_dialog = NULL;
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_destroy(chooser);
}
}

Expand Down Expand Up @@ -1836,7 +1836,7 @@ source_dialog_start_scan (GtkFileChooser *chooser, int title_id)
ghb_do_scan_list(ud, files, title_id, TRUE);
}
source_dialog = NULL;
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_destroy(chooser);
}

/* This set of functions became very complicated in GTK4 due to the removal
Expand All @@ -1853,7 +1853,7 @@ source_dialog_start_scan (GtkFileChooser *chooser, int title_id)
static void
do_source_dialog(gboolean dir, signal_user_data_t *ud)
{
GtkFileChooserNative *chooser;
GtkFileChooser *chooser;
GtkWindow *hb_window;
const gchar *sourcename;

Expand All @@ -1862,7 +1862,7 @@ do_source_dialog(gboolean dir, signal_user_data_t *ud)
return;

hb_window = gtk_application_get_active_window(GTK_APPLICATION(GHB_APPLICATION_DEFAULT));
chooser = source_dialog = gtk_file_chooser_native_new(
chooser = source_dialog = ghb_file_chooser_new(
dir ? _("Open Source Directory") : _("Open Source"),
hb_window,
dir ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER : GTK_FILE_CHOOSER_ACTION_OPEN,
Expand All @@ -1873,26 +1873,25 @@ do_source_dialog(gboolean dir, signal_user_data_t *ud)

if (dir)
{
gtk_file_chooser_add_choice(GTK_FILE_CHOOSER(chooser), "recursive",
gtk_file_chooser_add_choice(chooser, "recursive",
_("Recursively scan directories"), NULL, NULL);
gtk_file_chooser_set_choice(GTK_FILE_CHOOSER(chooser), "recursive",
gtk_file_chooser_set_choice(chooser, "recursive",
ghb_dict_get_bool(ud->prefs, "RecursiveFolderScan") ? "true" : "false");
}
else
{
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE);
source_dialog_drive_list(GTK_FILE_CHOOSER(chooser), ud);
add_video_file_filters(GTK_FILE_CHOOSER(chooser));
gtk_file_chooser_set_select_multiple(chooser, TRUE);
source_dialog_drive_list(chooser, ud);
add_video_file_filters(chooser);
}

gtk_file_chooser_add_choice(GTK_FILE_CHOOSER(chooser), "single",
gtk_file_chooser_add_choice(chooser, "single",
_("Single Title"), NULL, NULL);

gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(chooser), TRUE);
gtk_native_dialog_set_transient_for(GTK_NATIVE_DIALOG(chooser), hb_window);
ghb_file_chooser_set_modal(chooser, TRUE);
g_signal_connect(chooser, "response", G_CALLBACK(source_dialog_response_cb), ud);
ghb_file_chooser_set_initial_file(GTK_FILE_CHOOSER(chooser), sourcename);
gtk_native_dialog_show(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_set_initial_file(chooser, sourcename);
ghb_file_chooser_show(chooser);
}

#if 0
Expand Down Expand Up @@ -2099,15 +2098,15 @@ dest_file_changed_cb (GtkEntry *entry, gpointer data)
}

static void
destination_response_cb(GtkFileChooserNative *chooser,
destination_response_cb(GtkFileChooser *chooser,
GtkResponseType response, signal_user_data_t *ud)
{
GtkEditable *entry;
g_autofree char *basename = NULL;

if (response == GTK_RESPONSE_ACCEPT)
{
g_autoptr(GFile) file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(chooser));
g_autoptr(GFile) file = gtk_file_chooser_get_file(chooser);
const char *filename = g_file_peek_path(file);
g_autofree char *dirname = g_path_get_dirname(filename);
basename = g_path_get_basename(filename);
Expand All @@ -2116,28 +2115,28 @@ destination_response_cb(GtkFileChooserNative *chooser,
GhbFileButton *dest_chooser = GHB_FILE_BUTTON(ghb_builder_widget("dest_dir"));
ghb_file_button_set_filename(dest_chooser, dirname);
}
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_destroy(chooser);
}

G_MODULE_EXPORT void
destination_action_cb(GSimpleAction *action, GVariant *param,
signal_user_data_t *ud)
{
GtkFileChooserNative *chooser;
GtkFileChooser *chooser;
GtkWindow *hb_window;
const gchar *destname;

hb_window = GTK_WINDOW(ghb_builder_widget("hb_window"));
destname = ghb_dict_get_string(ud->settings, "destination");
chooser = gtk_file_chooser_native_new("Choose Destination",
hb_window,
GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Save"),
_("_Cancel"));
ghb_file_chooser_set_initial_file(GTK_FILE_CHOOSER(chooser), destname);
gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(chooser), TRUE);
chooser = ghb_file_chooser_new("Choose Destination",
hb_window,
GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Save"),
_("_Cancel"));
ghb_file_chooser_set_initial_file(chooser, destname);
ghb_file_chooser_set_modal(chooser, TRUE);
g_signal_connect(chooser, "response", G_CALLBACK(destination_response_cb), ud);
gtk_native_dialog_show(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_show(chooser);
}

G_MODULE_EXPORT gboolean
Expand Down Expand Up @@ -5596,7 +5595,7 @@ presets_list_context_menu_cb (GtkGesture *gest, gint n_press, double x,
GtkFileFilter *ghb_add_file_filter(GtkFileChooser *chooser,
const char *name, const char *id)
{
g_autoptr(GtkFileFilter) filter = GTK_FILE_FILTER(ghb_builder_object(id));
GtkFileFilter *filter = GTK_FILE_FILTER(ghb_builder_object(id));
gtk_file_filter_set_name(filter, name);
gtk_file_chooser_add_filter(chooser, filter);
return filter;
Expand Down
61 changes: 29 additions & 32 deletions gtk/src/chapters.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ chapter_list_export_xml (const gchar *filename, GhbValue *chapter_dict)
}

static void
chapter_list_export (GtkFileChooserNative *dialog,
chapter_list_export (GtkFileChooser *chooser,
GtkResponseType response, signal_user_data_t *ud)
{
gchar *filename, *dir;
Expand All @@ -243,11 +243,11 @@ chapter_list_export (GtkFileChooserNative *dialog,

if (chapter_list == NULL)
{
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(dialog));
ghb_file_chooser_destroy(chooser);
return;
}

filename = ghb_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
filename = ghb_file_chooser_get_filename(chooser);

chapter_list_export_xml(filename, chapter_list);
exportDir = ghb_dict_get_string(ud->prefs, "ExportDirectory");
Expand All @@ -260,7 +260,7 @@ chapter_list_export (GtkFileChooserNative *dialog,
g_free(dir);
g_free(filename);
}
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(dialog));
ghb_file_chooser_destroy(chooser);
}

static xmlNodePtr
Expand Down Expand Up @@ -437,62 +437,59 @@ chapters_import_response_cb (GtkFileChooser *dialog,
chapter_list_import_xml(filename, ud);
g_free(filename);
}
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(dialog));
ghb_file_chooser_destroy(dialog);
}

G_MODULE_EXPORT void
chapters_export_action_cb (GSimpleAction *action, GVariant *param,
signal_user_data_t *ud)
{
GtkWindow *hb_window;
GtkFileChooserNative *dialog;
GtkFileChooser *dialog;
const gchar *exportDir;
GtkFileFilter *filter;

hb_window = GTK_WINDOW(ghb_builder_widget("hb_window"));
dialog = gtk_file_chooser_native_new("Export Chapters", hb_window,
GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Save"),
_("_Cancel"));
dialog = ghb_file_chooser_new("Export Chapters", hb_window,
GTK_FILE_CHOOSER_ACTION_SAVE,
_("_Save"),
_("_Cancel"));

ghb_add_file_filter(GTK_FILE_CHOOSER(dialog), _("All Files"), "FilterAll");
filter = ghb_add_file_filter(GTK_FILE_CHOOSER(dialog), _("Chapters (*.xml)"), "FilterXML");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "chapters.xml");
ghb_add_file_filter(dialog, _("All Files"), "FilterAll");
filter = ghb_add_file_filter(dialog, _("Chapters (*.xml)"), "FilterXML");
gtk_file_chooser_set_filter(dialog, filter);
gtk_file_chooser_set_current_name(dialog, "chapters.xml");

exportDir = ghb_dict_get_string(ud->prefs, "ExportDirectory");
if (exportDir && exportDir[0] != '\0')
ghb_file_chooser_set_initial_file(GTK_FILE_CHOOSER(dialog), exportDir);
ghb_file_chooser_set_initial_file(dialog, exportDir);

gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(dialog), TRUE);
gtk_native_dialog_set_transient_for(GTK_NATIVE_DIALOG(dialog), GTK_WINDOW(hb_window));
ghb_file_chooser_set_modal(dialog, TRUE);
g_signal_connect(dialog, "response", G_CALLBACK(chapter_list_export), ud);
gtk_native_dialog_show(GTK_NATIVE_DIALOG(dialog));
ghb_file_chooser_show(dialog);
}

G_MODULE_EXPORT void
chapters_import_action_cb (GSimpleAction *action, GVariant *param,
signal_user_data_t *ud)
{
GtkWindow *hb_window;
GtkFileChooserNative *dialog;
GtkFileChooser *dialog;
GtkFileFilter *filter;

hb_window = GTK_WINDOW(ghb_builder_widget("hb_window"));
dialog = gtk_file_chooser_native_new("Import Chapters", hb_window,
GTK_FILE_CHOOSER_ACTION_OPEN,
_("_Open"),
_("_Cancel"));

ghb_add_file_filter(GTK_FILE_CHOOSER(dialog), _("All Files"), "FilterAll");
filter = ghb_add_file_filter(GTK_FILE_CHOOSER(dialog), _("Chapters (*.xml)"), "FilterXML");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
ghb_file_chooser_set_initial_file(GTK_FILE_CHOOSER(dialog),
dialog = ghb_file_chooser_new("Import Chapters", hb_window,
GTK_FILE_CHOOSER_ACTION_OPEN,
_("_Open"),
_("_Cancel"));

ghb_add_file_filter(dialog, _("All Files"), "FilterAll");
filter = ghb_add_file_filter(dialog, _("Chapters (*.xml)"), "FilterXML");
gtk_file_chooser_set_filter(dialog, filter);
ghb_file_chooser_set_initial_file(dialog,
ghb_dict_get_string(ud->prefs, "ExportDirectory"));

gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(dialog), TRUE);
gtk_native_dialog_set_transient_for(GTK_NATIVE_DIALOG(dialog), GTK_WINDOW(hb_window));
ghb_file_chooser_set_modal(dialog, TRUE);
g_signal_connect(dialog, "response", G_CALLBACK(chapters_import_response_cb), ud);
gtk_native_dialog_show(GTK_NATIVE_DIALOG(dialog));

ghb_file_chooser_show(dialog);
}
17 changes: 9 additions & 8 deletions gtk/src/ghb-file-button.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,27 +336,28 @@ chooser_response_cb (GtkFileChooser *chooser, GtkResponseType response,
ghb_file_button_set_file(self, file);
g_object_unref(file);
}
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_destroy(chooser);
}

static void
ghb_file_button_clicked (GtkButton *button)
{
GtkWindow *window;
GtkFileChooserNative *chooser;
GtkFileChooser *chooser;
GhbFileButton *self = GHB_FILE_BUTTON(button);

g_return_if_fail(GHB_IS_FILE_BUTTON(self));

window = GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(self)));
chooser = gtk_file_chooser_native_new(ghb_file_button_get_title(self), window,
ghb_file_button_get_action(self),
ghb_file_button_get_accept_label(self), NULL);
chooser = ghb_file_chooser_new(ghb_file_button_get_title(self), window,
ghb_file_button_get_action(self),
ghb_file_button_get_accept_label(self),
_("_Cancel"));
g_autofree char *selected_name = ghb_file_button_get_filename(self);
ghb_file_chooser_set_initial_file(GTK_FILE_CHOOSER(chooser), selected_name);
ghb_file_chooser_set_initial_file(chooser, selected_name);

g_signal_connect(chooser, "response", G_CALLBACK(chooser_response_cb), self);

gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(chooser), GTK_IS_WINDOW(window));
gtk_native_dialog_show(GTK_NATIVE_DIALOG(chooser));
ghb_file_chooser_set_modal(chooser, GTK_IS_WINDOW(window));
ghb_file_chooser_show(chooser);
}
Loading

0 comments on commit 7274e66

Please sign in to comment.