gliv-1.7
[gliv.git] / src / open_dialog.c
blob4530e6f3c2e3535ce94e1225488bf062674d8357
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <gfc@altern.org>
21 /********************
22 * The Open dialog. *
23 ********************/
25 #include "gliv.h"
27 #include <glob.h> /* glob_t, glob(), GLOB_NOSORT */
28 #include <gdk/gdkkeysyms.h> /* GDK_Escape */
30 static gchar *saved_path = NULL;
31 static gboolean shuffle_flag = FALSE;
32 static gboolean dir_flag = TRUE;
34 static void toggle_flag(GtkToggleButton * button, gboolean * flag)
36 *flag = gtk_toggle_button_get_active(button);
39 static void add_button(GtkFileSelection * dialog, const gchar * label,
40 gboolean * value)
42 GtkCheckButton *button;
44 label = add_mnemonic(label);
45 button = GTK_CHECK_BUTTON(gtk_check_button_new_with_mnemonic(label));
47 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *value);
49 g_signal_connect(button, "toggled", G_CALLBACK(toggle_flag), value);
51 gtk_container_add(GTK_CONTAINER(dialog->action_area), GTK_WIDGET(button));
54 /* A GtkFileSelection with two buttons. */
55 static GtkFileSelection *decorated_dialog(void)
57 GtkFileSelection *dialog;
58 const gchar *label;
60 label = _("GLiv: Select files or use wildcards");
61 dialog = GTK_FILE_SELECTION(gtk_file_selection_new(label));
63 if (saved_path != NULL)
64 gtk_file_selection_set_filename(dialog, saved_path);
66 gtk_file_selection_set_select_multiple(dialog, TRUE);
68 add_button(dialog, _("Shuffle"), &shuffle_flag);
69 add_button(dialog, _("All files in the directory"), &dir_flag);
71 return dialog;
74 static void single_selection(GtkFileSelection * dialog)
76 glob_t pglob;
78 /* Use what is in the entry. */
79 glob(gtk_file_selection_get_filename(dialog), GLOB_NOSORT, NULL, &pglob);
81 if (pglob.gl_pathc == 1)
82 /* No wildcard expansion. */
83 open_file(pglob.gl_pathv[0], dir_flag, shuffle_flag);
85 else if (insert_after_current(pglob.gl_pathv, pglob.gl_pathc, shuffle_flag))
86 /* Add all expansions from glob(). */
87 open_next_image();
89 globfree(&pglob);
92 static void process_selection(GtkFileSelection * dialog)
94 gchar **array;
95 gint nb;
97 gtk_widget_hide(GTK_WIDGET(dialog));
98 g_free(saved_path);
99 saved_path = g_strdup(gtk_file_selection_get_filename(dialog));
101 array = gtk_file_selection_get_selections(dialog);
103 for (nb = 0; array[nb] != NULL; nb++);
105 if (nb == 1)
106 single_selection(dialog);
108 else if (insert_after_current(array, nb, shuffle_flag))
109 open_next_image();
111 /* array[0] is constant. */
112 for (nb = 1; array[nb] != NULL; nb++)
113 g_free(array[nb]);
115 g_free(array);
116 gtk_widget_destroy(GTK_WIDGET(dialog));
119 /* Leaves if ESC is pressed. */
120 static gboolean key_press_event(GtkWidget * widget, GdkEventKey * event)
122 if (event->keyval == GDK_Escape) {
123 gtk_widget_destroy(widget);
124 return TRUE;
127 return FALSE;
130 static gboolean delete_open_dialog(GtkFileSelection ** dialog)
132 if (dialog)
133 *dialog = NULL;
135 return FALSE;
138 /* Called by File->Open (or the 'o' key). */
139 gboolean menu_open(void)
141 static GtkFileSelection *dialog = NULL;
143 if (dialog)
144 return TRUE;
146 dialog = decorated_dialog();
148 g_signal_connect_swapped(dialog->ok_button, "clicked",
149 G_CALLBACK(process_selection), dialog);
151 g_signal_connect_swapped(dialog->cancel_button, "clicked",
152 G_CALLBACK(gtk_widget_destroy), dialog);
154 g_signal_connect(dialog, "key-press-event", G_CALLBACK(key_press_event),
155 GINT_TO_POINTER(FALSE));
157 g_signal_connect_swapped(dialog, "delete-event",
158 G_CALLBACK(delete_open_dialog), &dialog);
160 g_signal_connect_swapped(dialog, "destroy",
161 G_CALLBACK(delete_open_dialog), &dialog);
163 show_dialog(GTK_WIDGET(dialog), NULL, TRUE);
164 return TRUE;