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>
26 #include "open_dialog.h"
28 #include "next_image.h"
29 #include "files_list.h"
31 #include "mnemonics.h"
34 #include "glade_image_nr.h"
35 #include "thumbnails.h"
37 extern options_struct
*options
;
38 extern GlivImage
*current_image
;
40 static gchar
*saved_path
= NULL
;
42 /* From the gtk+-2.4 api documentation */
43 static void update_preview_cb(GtkFileChooser
* file_chooser
, gpointer data
)
47 GdkPixbuf
*pixbuf
= NULL
;
49 preview
= GTK_WIDGET(data
);
50 filename
= gtk_file_chooser_get_preview_filename(file_chooser
);
56 if (options
->force
|| get_loader(filename
) == LOADER_PIXBUF
)
57 pixbuf
= gdk_pixbuf_new_from_file_at_size(filename
, 128, 128, NULL
);
63 gtk_image_set_from_pixbuf(GTK_IMAGE(preview
), pixbuf
);
65 gdk_pixbuf_unref(pixbuf
);
67 gtk_file_chooser_set_preview_widget_active(file_chooser
, pixbuf
!= NULL
);
70 static gboolean
toggle_button(GtkToggleButton
* button
, gboolean
* flag
)
72 *flag
= gtk_toggle_button_get_active(button
);
76 static void add_button(GtkHBox
* buttons
, const gchar
* label
, gboolean
* flag
)
78 GtkCheckButton
*button
;
81 GTK_CHECK_BUTTON(gtk_check_button_new_with_mnemonic
82 (add_mnemonic(label
)));
84 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button
), *flag
);
85 g_signal_connect_after(button
, "toggled", G_CALLBACK(toggle_button
), flag
);
87 gtk_box_pack_start_defaults(GTK_BOX(buttons
), GTK_WIDGET(button
));
88 gtk_widget_show(GTK_WIDGET(button
));
91 /* A GtkFileChooser */
92 static GtkFileChooser
*decorated_dialog(gboolean select_dir
)
94 GtkFileChooserAction action
;
96 GtkFileChooser
*chooser
;
100 action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
101 label
= _("GLiv: Select a folder to open");
103 action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
104 label
= _("GLiv: Select files to open");
107 chooser
= GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(label
, NULL
,
115 if (saved_path
!= NULL
)
116 gtk_file_chooser_set_current_folder(chooser
, saved_path
);
119 GtkImage
*preview
= GTK_IMAGE(gtk_image_new());
120 gtk_file_chooser_set_select_multiple(chooser
, TRUE
);
122 gtk_file_chooser_set_preview_widget(chooser
, GTK_WIDGET(preview
));
123 g_signal_connect(chooser
, "update-preview",
124 G_CALLBACK(update_preview_cb
), preview
);
127 buttons
= GTK_HBOX(gtk_hbox_new(FALSE
, 10));
129 add_button(buttons
, _("Try to load every file"), &options
->force
);
130 add_button(buttons
, _("Recursive directory traversal"),
131 &options
->recursive
);
134 gtk_file_chooser_set_extra_widget(chooser
, GTK_WIDGET(buttons
));
139 static void open_images(gchar
** paths
, gint nb
)
141 gint nb_inserted
= insert_after_current(paths
, nb
, FALSE
, TRUE
);
143 new_images(nb_inserted
);
146 static void load_selection(GSList
* selection
)
148 gint nb
= g_slist_length(selection
);
149 gchar
**filenames
= g_new(gchar
*, nb
+ 1);
150 GSList
*list_ptr
= selection
;
151 gchar
**tab_ptr
= filenames
;
153 while (list_ptr
!= NULL
) {
154 *tab_ptr
= list_ptr
->data
;
156 list_ptr
= list_ptr
->next
;
161 open_images(filenames
, nb
);
163 g_strfreev(filenames
);
166 gboolean
menu_open(gboolean select_dir
)
168 GtkFileChooser
*dialog
;
170 GSList
*selection
= NULL
;
173 dialog
= decorated_dialog(select_dir
);
174 response
= run_modal_dialog(GTK_DIALOG(dialog
));
177 saved_path
= gtk_file_chooser_get_current_folder(dialog
);
179 if (!select_dir
&& response
== GTK_RESPONSE_ACCEPT
)
180 selection
= gtk_file_chooser_get_filenames(dialog
);
182 gtk_widget_destroy(GTK_WIDGET(dialog
));
184 if (selection
!= NULL
) {
186 load_selection(selection
);
187 g_slist_free(selection
);
188 } else if (response
== GTK_RESPONSE_ACCEPT
)
190 open_images(&saved_path
, 1);
195 static GtkWidget
*create_pixmap(void *unused1
, void *unused2
)
197 return gtk_image_new();
200 static const gchar
*selected
;
201 static GtkLabel
*dirname_label
;
202 static GtkLabel
*filename_label
;
203 static GtkImage
*preview
;
205 static gboolean
on_image_nr_value_changed(GtkSpinButton
* spin
)
211 int val
= gtk_spin_button_get_value_as_int(spin
) - 1;
212 selected
= get_nth_filename(val
);
214 dirname
= g_path_get_dirname(selected
);
215 filename
= g_path_get_basename(selected
);
217 gtk_label_set_text(dirname_label
, dirname
);
218 gtk_label_set_text(filename_label
, filename
);
223 thumb
= get_thumbnail(selected
, NULL
);
224 gtk_image_set_from_pixbuf(preview
, thumb
);
229 #include "glade_image_nr.c"
231 gboolean
menu_image_nr(void)
236 gint len
= get_list_length();
238 if (preview
!= NULL
) {
239 /* Currently displayed */
248 dialog
= GTK_DIALOG(create_image_nr_dialog());
250 spin
= g_object_get_data(G_OBJECT(dialog
), "spinbutton");
251 dirname_label
= g_object_get_data(G_OBJECT(dialog
), "dirname");
252 filename_label
= g_object_get_data(G_OBJECT(dialog
), "filename");
253 preview
= g_object_get_data(G_OBJECT(dialog
), "image");
255 gtk_spin_button_set_range(spin
, len
!= 0, len
);
257 if (current_image
!= NULL
) {
258 gint im_nr
= get_image_number(current_image
);
259 gtk_spin_button_set_value(spin
, (gdouble
) im_nr
+ 1.0);
262 on_image_nr_value_changed(spin
);
264 response
= run_modal_dialog(dialog
);
265 gtk_widget_destroy(GTK_WIDGET(dialog
));
268 if (response
== GTK_RESPONSE_OK
)