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>
25 #include <glob.h> /* glob_t, glob(), GLOB_NOSORT */
26 #include <gdk/gdkkeysyms.h> /* GDK_Escape */
29 #include "open_dialog.h"
30 #include "str_utils.h"
32 #include "next_image.h"
33 #include "files_list.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
,
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
;
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
);
81 static void single_selection(GtkFileSelection
* dialog
)
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
);
93 /* Add all expansions from glob(). */
96 nb_inserted
= insert_after_current(pglob
.gl_pathv
, pglob
.gl_pathc
,
97 shuffle_flag
, !shuffle_flag
);
99 open_next_image(nb_inserted
== 1);
105 static void process_selection(GtkFileSelection
* dialog
)
110 gtk_widget_hide(GTK_WIDGET(dialog
));
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
++);
120 single_selection(dialog
);
125 nb_inserted
= insert_after_current(array
, nb
,
126 shuffle_flag
, !shuffle_flag
);
128 open_next_image(nb_inserted
== 1);
131 /* array[0] is constant. */
132 for (nb
= 1; array
[nb
] != NULL
; nb
++)
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
);
150 static gboolean
delete_open_dialog(GtkFileSelection
** dialog
)
158 /* Called by File->Open (or the 'o' key). */
159 gboolean
menu_open(void)
161 static GtkFileSelection
*dialog
= NULL
;
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
);