Use GtkFileChooserNative

This commit is contained in:
Patrick Griffis 2017-05-22 09:30:49 -04:00 committed by Arx Cruz
parent 3496685dd5
commit 110a851747

View File

@ -30,14 +30,18 @@
static ZenityData *zen_data; static ZenityData *zen_data;
static void zenity_fileselection_dialog_response ( static void zenity_fileselection_dialog_response (
GtkWidget *widget, int response, gpointer data); gpointer obj, int response, gpointer data);
void void
zenity_fileselection (ZenityData *data, ZenityFileData *file_data) { zenity_fileselection (ZenityData *data, ZenityFileData *file_data) {
GtkWidget *dialog;
gchar *dir; gchar *dir;
gchar *basename; gchar *basename;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
#if GTK_CHECK_VERSION(3, 20, 0)
GtkFileChooserNative *dialog;
#else
GtkWidget *dialog;
#endif
zen_data = data; zen_data = data;
@ -51,22 +55,37 @@ zenity_fileselection (ZenityData *data, ZenityFileData *file_data) {
action = GTK_FILE_CHOOSER_ACTION_SAVE; action = GTK_FILE_CHOOSER_ACTION_SAVE;
} }
#if GTK_CHECK_VERSION(3, 20, 0)
dialog = gtk_file_chooser_native_new (data->dialog_title,
NULL, /* TODO: Get parent from xid */
action,
_ ("_OK"),
_ ("_Cancel")
);
if (data->modal)
gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG(dialog), TRUE);
if (data->extra_label)
g_warning ("Cannot add extra labels to GtkFileChooserNative");
#else
dialog = gtk_file_chooser_dialog_new (NULL, dialog = gtk_file_chooser_dialog_new (NULL,
NULL, NULL,
action, action,
_ ("_Cancel"), _ ("_Cancel"),
GTK_RESPONSE_CANCEL, GTK_RESPONSE_CANCEL,
_ ("_OK"), _ ("_OK"),
GTK_RESPONSE_OK, GTK_RESPONSE_ACCEPT,
NULL); NULL);
gtk_file_chooser_set_do_overwrite_confirmation ( if (data->dialog_title)
GTK_FILE_CHOOSER (dialog), file_data->confirm_overwrite); gtk_window_set_title (GTK_WINDOW (dialog), data->dialog_title);
g_signal_connect (G_OBJECT (dialog), if (data->modal)
"response", gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
G_CALLBACK (zenity_fileselection_dialog_response),
file_data); zenity_util_set_window_icon (
dialog, data->window_icon, ZENITY_IMAGE_FULLPATH ("zenity-file.png"));
if (data->extra_label) { if (data->extra_label) {
gint i = 0; gint i = 0;
@ -76,15 +95,15 @@ zenity_fileselection (ZenityData *data, ZenityFileData *file_data) {
i++; i++;
} }
} }
#endif
if (data->dialog_title) gtk_file_chooser_set_do_overwrite_confirmation (
gtk_window_set_title (GTK_WINDOW (dialog), data->dialog_title); GTK_FILE_CHOOSER (dialog), file_data->confirm_overwrite);
zenity_util_set_window_icon ( g_signal_connect (G_OBJECT (dialog),
dialog, data->window_icon, ZENITY_IMAGE_FULLPATH ("zenity-file.png")); "response",
G_CALLBACK (zenity_fileselection_dialog_response),
if (data->modal) file_data);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
if (file_data->uri) { if (file_data->uri) {
dir = g_path_get_dirname (file_data->uri); dir = g_path_get_dirname (file_data->uri);
@ -156,7 +175,11 @@ zenity_fileselection (ZenityData *data, ZenityFileData *file_data) {
} }
} }
#if GTK_CHECK_VERSION(3, 20, 0)
gtk_native_dialog_show (GTK_NATIVE_DIALOG(dialog));
#else
zenity_util_show_dialog (dialog, data->attach); zenity_util_show_dialog (dialog, data->attach);
#endif
if (data->timeout_delay > 0) { if (data->timeout_delay > 0) {
g_timeout_add_seconds (data->timeout_delay, g_timeout_add_seconds (data->timeout_delay,
@ -169,9 +192,9 @@ zenity_fileselection (ZenityData *data, ZenityFileData *file_data) {
static void static void
zenity_fileselection_dialog_output ( zenity_fileselection_dialog_output (
GtkWidget *widget, ZenityFileData *file_data) { GtkFileChooser *chooser, ZenityFileData *file_data) {
GSList *selections, *iter; GSList *selections, *iter;
selections = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (widget)); selections = gtk_file_chooser_get_filenames (chooser);
for (iter = selections; iter != NULL; iter = iter->next) { for (iter = selections; iter != NULL; iter = iter->next) {
g_print ("%s", g_print ("%s",
g_filename_to_utf8 ((gchar *) iter->data, -1, NULL, NULL, NULL)); g_filename_to_utf8 ((gchar *) iter->data, -1, NULL, NULL, NULL));
@ -185,12 +208,14 @@ zenity_fileselection_dialog_output (
static void static void
zenity_fileselection_dialog_response ( zenity_fileselection_dialog_response (
GtkWidget *widget, int response, gpointer data) { gpointer obj, int response, gpointer data) {
ZenityFileData *file_data = data; ZenityFileData *file_data = data;
GtkFileChooser *chooser = GTK_FILE_CHOOSER(obj);
switch (response) { switch (response) {
case GTK_RESPONSE_OK: case GTK_RESPONSE_ACCEPT:
zenity_fileselection_dialog_output (widget, file_data); zenity_fileselection_dialog_output (chooser, file_data);
zenity_util_exit_code_with_data (ZENITY_OK, zen_data); zenity_util_exit_code_with_data (ZENITY_OK, zen_data);
break; break;
@ -199,7 +224,7 @@ zenity_fileselection_dialog_response (
break; break;
case ZENITY_TIMEOUT: case ZENITY_TIMEOUT:
zenity_fileselection_dialog_output (widget, file_data); zenity_fileselection_dialog_output (chooser, file_data);
zen_data->exit_code = zenity_util_return_exit_code (ZENITY_TIMEOUT); zen_data->exit_code = zenity_util_return_exit_code (ZENITY_TIMEOUT);
break; break;