Remove the trace of a TODO file.
[gliv.git] / src / open_dialog.c
blob357427efeba755ede5523fc4634ab904e326c0c6
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 <guichaz@yahoo.fr>
21 /*******************
22 * The Open dialog *
23 *******************/
25 #include <glob.h> /* glob_t, glob(), GLOB_NOSORT */
26 #include <gdk/gdkkeysyms.h> /* GDK_Escape */
28 #include "gliv.h"
29 #include "open_dialog.h"
30 #include "str_utils.h"
31 #include "messages.h"
32 #include "next_image.h"
33 #include "files_list.h"
34 #include "windows.h"
36 static gchar *saved_path = NULL;
37 static gboolean shuffle_flag = FALSE;
38 static gboolean dir_flag = TRUE;
40 static void toggle_flag(GtkToggleButton * button, gboolean * flag)
42 *flag = gtk_toggle_button_get_active(button);
45 /* Shuffle button or add dir button. */
46 static void add_button(GtkFileSelection * dialog, const gchar * label,
47 gboolean * value)
49 GtkCheckButton *button;
51 label = add_mnemonic(label);
52 button = GTK_CHECK_BUTTON(gtk_check_button_new_with_mnemonic(label));
54 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), *value);
56 g_signal_connect(button, "toggled", G_CALLBACK(toggle_flag), value);
58 gtk_container_add(GTK_CONTAINER(dialog->action_area), GTK_WIDGET(button));
61 /* A GtkFileSelection with two buttons. */
62 static GtkFileSelection *decorated_dialog(void)
64 GtkFileSelection *dialog;
65 const gchar *label;
67 label = _("GLiv: Select files or use wildcards");
68 dialog = GTK_FILE_SELECTION(gtk_file_selection_new(label));
70 if (saved_path != NULL)
71 gtk_file_selection_set_filename(dialog, saved_path);
73 gtk_file_selection_set_select_multiple(dialog, TRUE);
75 add_button(dialog, _("Shuffle"), &shuffle_flag);
76 add_button(dialog, _("All files in the directory"), &dir_flag);
78 return dialog;
81 static void single_selection(GtkFileSelection * dialog)
83 glob_t pglob;
85 /* Use what is in the entry. */
86 glob(gtk_file_selection_get_filename(dialog), GLOB_NOSORT, NULL, &pglob);
88 if (pglob.gl_pathc == 1)
89 /* No wildcard expansion. */
90 open_file(pglob.gl_pathv[0], dir_flag, shuffle_flag);
92 else {
93 /* Add all expansions from glob(). */
94 gint nb_inserted;
96 nb_inserted = insert_after_current(pglob.gl_pathv, pglob.gl_pathc,
97 shuffle_flag, !shuffle_flag);
98 if (nb_inserted > 0)
99 open_next_image(nb_inserted == 1);
102 globfree(&pglob);
105 static void process_selection(GtkFileSelection * dialog)
107 gchar **array;
108 gint nb;
110 gtk_widget_hide(GTK_WIDGET(dialog));
111 g_free(saved_path);
112 saved_path = g_strdup(gtk_file_selection_get_filename(dialog));
114 array = gtk_file_selection_get_selections(dialog);
116 /* Count selected files. */
117 for (nb = 0; array[nb] != NULL; nb++);
119 if (nb == 1)
120 single_selection(dialog);
122 else {
123 gint nb_inserted;
125 nb_inserted = insert_after_current(array, nb,
126 shuffle_flag, !shuffle_flag);
127 if (nb_inserted > 0)
128 open_next_image(nb_inserted == 1);
131 /* array[0] is constant. */
132 for (nb = 1; array[nb] != NULL; nb++)
133 g_free(array[nb]);
135 g_free(array);
136 gtk_widget_destroy(GTK_WIDGET(dialog));
139 /* Leaves if ESC is pressed. */
140 static gboolean key_press_event(GtkWidget * widget, GdkEventKey * event)
142 if (event->keyval == GDK_Escape) {
143 gtk_widget_destroy(widget);
144 return TRUE;
147 return FALSE;
150 static gboolean delete_open_dialog(GtkFileSelection ** dialog)
152 if (dialog)
153 *dialog = NULL;
155 return FALSE;
158 /* Called by File->Open (or the 'o' key). */
159 gboolean menu_open(void)
161 static GtkFileSelection *dialog = NULL;
163 if (dialog)
164 return TRUE;
166 dialog = decorated_dialog();
168 g_signal_connect_swapped(dialog->ok_button, "clicked",
169 G_CALLBACK(process_selection), dialog);
171 g_signal_connect_swapped(dialog->cancel_button, "clicked",
172 G_CALLBACK(gtk_widget_destroy), dialog);
174 g_signal_connect(dialog, "key-press-event", G_CALLBACK(key_press_event),
175 GINT_TO_POINTER(FALSE));
177 g_signal_connect_swapped(dialog, "delete-event",
178 G_CALLBACK(delete_open_dialog), &dialog);
180 g_signal_connect_swapped(dialog, "destroy",
181 G_CALLBACK(delete_open_dialog), &dialog);
183 show_dialog(GTK_WIDGET(dialog), NULL);
184 return TRUE;