Mercurial > audlegacy
annotate src/audacious/ui_fileopener.c @ 2437:62d8f9c05832 trunk
[svn] More input plugin API work
author | iabervon |
---|---|
date | Sun, 28 Jan 2007 21:08:08 -0800 |
parents | 4a1d1c324685 |
children | 7f0318090690 |
rev | line source |
---|---|
2417 | 1 /* Audacious - Cross-platform multimedia player |
2 * Copyright (C) 2005-2007 Audacious development team | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; under version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * You should have received a copy of the GNU General Public License | |
14 * along with this program; if not, write to the Free Software | |
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
16 */ | |
17 | |
18 #include "ui_fileopener.h" | |
19 | |
20 #include <glib/gi18n.h> | |
21 #include <string.h> | |
2432
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
22 #include <gdk/gdkkeysyms.h> |
2417 | 23 |
24 #include "input.h" | |
25 #include "main.h" | |
26 #include "playback.h" | |
27 #include "strings.h" | |
28 #include "ui_playlist.h" | |
29 | |
30 static void | |
31 filebrowser_add_files(GtkFileChooser * browser, | |
32 GSList * files) | |
33 { | |
34 GSList *cur; | |
35 gchar *ptr; | |
36 guint ctr = 0; | |
37 Playlist *playlist = playlist_get_active(); | |
38 | |
39 if (GTK_IS_WIDGET(mainwin_jtf)) | |
40 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
41 | |
42 for (cur = files; cur; cur = g_slist_next(cur)) { | |
43 | |
44 if (g_file_test(cur->data,G_FILE_TEST_IS_DIR)) { | |
45 playlist_add_dir(playlist, (const gchar *) cur->data); | |
46 } else { | |
47 playlist_add(playlist, (const gchar *) cur->data); | |
48 } | |
49 | |
50 if (++ctr == 20) { | |
51 playlistwin_update_list(playlist); | |
52 ctr = 0; | |
53 while (gtk_events_pending() ) gtk_main_iteration(); | |
54 } | |
55 } | |
56 | |
57 playlistwin_update_list(playlist); | |
58 | |
59 if (GTK_IS_WIDGET(mainwin_jtf)) | |
60 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
61 | |
62 ptr = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(browser)); | |
63 | |
64 g_free(cfg.filesel_path); | |
65 cfg.filesel_path = ptr; | |
66 } | |
67 | |
68 static void | |
69 action_button_cb(GtkWidget *widget, gpointer data) | |
70 { | |
71 GtkWidget *window = g_object_get_data(data, "window"); | |
72 GtkWidget *chooser = g_object_get_data(data, "chooser"); | |
73 GtkWidget *toggle = g_object_get_data(data, "toggle-button"); | |
74 cfg.close_dialog_open = | |
75 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toggle)); | |
76 | |
77 gboolean play_button = | |
78 GPOINTER_TO_INT(g_object_get_data(data, "play-button")); | |
79 | |
80 | |
81 GSList *files; | |
82 files = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(chooser)); | |
83 if (!files) return; | |
84 | |
85 if (play_button) | |
86 playlist_clear(playlist_get_active()); | |
87 | |
88 filebrowser_add_files(GTK_FILE_CHOOSER(chooser), files); | |
89 g_slist_foreach(files, (GFunc) g_free, NULL); | |
90 g_slist_free(files); | |
91 | |
92 if (play_button) | |
93 playback_initiate(); | |
94 | |
95 | |
96 if (cfg.close_dialog_open) | |
97 gtk_widget_destroy(window); | |
98 } | |
99 | |
100 | |
101 static void | |
102 close_button_cb(GtkWidget *widget, gpointer data) | |
103 { | |
104 gtk_widget_destroy(GTK_WIDGET(data)); | |
105 } | |
106 | |
2432
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
107 static gboolean |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
108 filebrowser_on_keypress(GtkWidget * browser, |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
109 GdkEventKey * event, |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
110 gpointer data) |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
111 { |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
112 if (event->keyval == GDK_Escape) { |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
113 gtk_widget_destroy(browser); |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
114 return TRUE; |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
115 } |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
116 |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
117 return FALSE; |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
118 } |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
119 |
2417 | 120 void |
121 util_run_filebrowser_gtk2style(gboolean play_button) | |
122 { | |
123 GtkWidget *window; | |
124 GtkWidget *vbox, *hbox, *bbox; | |
125 GtkWidget *chooser; | |
126 GtkWidget *action_button, *close_button; | |
127 GtkWidget *toggle; | |
128 | |
129 gchar *window_title = play_button ? _("Open Files") : _("Add Files"); | |
130 gchar *toggle_text = play_button ? | |
131 _("Close dialog on Open") : _("Close dialog on Add"); | |
132 gpointer action_stock = play_button ? GTK_STOCK_OPEN : GTK_STOCK_ADD; | |
133 | |
134 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
135 gtk_window_set_title(GTK_WINDOW(window), window_title); | |
136 gtk_window_set_default_size(GTK_WINDOW(window), 700, 450); | |
137 gtk_container_set_border_width(GTK_CONTAINER(window), 10); | |
138 | |
139 vbox = gtk_vbox_new(FALSE, 0); | |
140 gtk_container_add(GTK_CONTAINER(window), vbox); | |
141 | |
142 chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN); | |
143 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE); | |
144 if (cfg.filesel_path) | |
145 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), | |
146 cfg.filesel_path); | |
2422 | 147 gtk_box_pack_start(GTK_BOX(vbox), chooser, TRUE, TRUE, 3); |
2417 | 148 |
149 hbox = gtk_hbox_new(TRUE, 0); | |
2422 | 150 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 3); |
2417 | 151 |
152 toggle = gtk_check_button_new_with_label(toggle_text); | |
153 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), | |
154 cfg.close_dialog_open ? TRUE : FALSE); | |
2422 | 155 gtk_box_pack_start(GTK_BOX(hbox), toggle, TRUE, TRUE, 3); |
2417 | 156 |
157 bbox = gtk_hbutton_box_new(); | |
158 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
2422 | 159 gtk_box_set_spacing(GTK_BOX(bbox), 6); |
160 gtk_box_pack_end(GTK_BOX(hbox), bbox, TRUE, TRUE, 3); | |
2417 | 161 |
162 close_button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
163 action_button = gtk_button_new_from_stock(action_stock); | |
164 gtk_container_add(GTK_CONTAINER(bbox), close_button); | |
165 gtk_container_add(GTK_CONTAINER(bbox), action_button); | |
166 | |
2420 | 167 /* this storage object holds several other objects which are used in the |
168 * callback functions | |
169 */ | |
2417 | 170 gpointer storage = g_object_new(G_TYPE_OBJECT, NULL); |
171 g_object_set_data(storage, "window", window); | |
172 g_object_set_data(storage, "chooser", chooser); | |
173 g_object_set_data(storage, "toggle-button", toggle); | |
174 g_object_set_data(storage, "play-button", GINT_TO_POINTER(play_button)); | |
175 | |
176 g_signal_connect(chooser, "file-activated", | |
177 G_CALLBACK(action_button_cb), storage); | |
178 g_signal_connect(action_button, "clicked", | |
179 G_CALLBACK(action_button_cb), storage); | |
180 g_signal_connect(close_button, "clicked", | |
181 G_CALLBACK(close_button_cb), window); | |
182 g_signal_connect(window, "destroy", | |
183 G_CALLBACK(gtk_widget_destroyed), &window); | |
184 | |
2432
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
185 g_signal_connect(window, "key_press_event", |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
186 G_CALLBACK(filebrowser_on_keypress), |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
187 NULL); |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
188 |
2417 | 189 gtk_widget_show_all(window); |
190 } | |
191 | |
192 /* | |
193 * Derived from Beep Media Player 0.9.6.1. | |
194 * Which is (C) 2003 - 2006 Milosz Derezynski &c | |
195 * | |
196 * Although I changed it quite a bit. -nenolod | |
197 */ | |
198 static void filebrowser_changed_classic(GtkFileSelection * filesel) | |
199 { | |
200 GList *list; | |
201 GList *node; | |
202 char *filename = (char *) | |
203 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); | |
204 GtkListStore *store; | |
205 GtkTreeIter iter; | |
206 | |
207 if ((list = input_scan_dir(filename)) != NULL) | |
208 { | |
209 /* | |
210 * We enter a directory that has been "hijacked" by an | |
211 * input-plugin. This is used by the CDDA plugin | |
212 */ | |
213 store = | |
214 GTK_LIST_STORE(gtk_tree_view_get_model | |
215 (GTK_TREE_VIEW(filesel->file_list))); | |
216 gtk_list_store_clear(store); | |
217 | |
218 node = list; | |
219 while (node) { | |
220 gtk_list_store_append(store, &iter); | |
221 gtk_list_store_set(store, &iter, 0, node->data, -1); | |
222 g_free(node->data); | |
223 node = g_list_next(node); | |
224 } | |
225 | |
226 g_list_free(list); | |
227 } | |
228 } | |
229 | |
230 static void filebrowser_entry_changed_classic(GtkEditable * entry, gpointer data) | |
231 { | |
232 filebrowser_changed_classic(GTK_FILE_SELECTION(data)); | |
233 } | |
234 | |
235 | |
236 static gboolean | |
237 util_filebrowser_is_dir_classic(GtkFileSelection * filesel) | |
238 { | |
239 char *text; | |
240 struct stat buf; | |
241 gboolean retv = FALSE; | |
242 | |
243 text = g_strdup(gtk_file_selection_get_filename(filesel)); | |
244 | |
245 if (stat(text, &buf) == 0 && S_ISDIR(buf.st_mode)) { | |
246 /* Selected directory */ | |
247 int len = strlen(text); | |
248 if (len > 3 && !strcmp(text + len - 4, "/../")) { | |
249 if (len == 4) | |
250 /* At the root already */ | |
251 *(text + len - 3) = '\0'; | |
252 else { | |
253 char *ptr; | |
254 *(text + len - 4) = '\0'; | |
255 ptr = strrchr(text, '/'); | |
256 *(ptr + 1) = '\0'; | |
257 } | |
258 } else if (len > 2 && !strcmp(text + len - 3, "/./")) | |
259 *(text + len - 2) = '\0'; | |
260 gtk_file_selection_set_filename(filesel, text); | |
261 retv = TRUE; | |
262 } | |
263 g_free(text); | |
264 return retv; | |
265 } | |
266 | |
267 static void filebrowser_add_files_classic(gchar ** files, | |
268 GtkFileSelection * filesel) | |
269 { | |
270 int ctr = 0; | |
271 char *ptr; | |
272 Playlist *playlist = playlist_get_active(); | |
273 | |
274 if (GTK_IS_WIDGET(mainwin_jtf)) | |
275 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
276 | |
277 while (files[ctr] != NULL) { | |
278 playlist_add(playlist, files[ctr++]); | |
279 } | |
280 playlistwin_update_list(playlist); | |
281 | |
282 if (GTK_IS_WIDGET(mainwin_jtf)) | |
283 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
284 | |
285 gtk_label_get(GTK_LABEL(GTK_BIN(filesel->history_pulldown)->child), | |
286 &ptr); | |
287 | |
288 /* This will give an extra slash if the current dir is the root. */ | |
289 cfg.filesel_path = g_strconcat(ptr, "/", NULL); | |
290 } | |
291 | |
292 static void filebrowser_ok_classic(GtkWidget * w, GtkWidget * filesel) | |
293 { | |
294 gchar **files; | |
295 | |
296 if (util_filebrowser_is_dir_classic(GTK_FILE_SELECTION(filesel))) | |
297 return; | |
298 files = gtk_file_selection_get_selections(GTK_FILE_SELECTION(filesel)); | |
299 filebrowser_add_files_classic(files, GTK_FILE_SELECTION(filesel)); | |
300 gtk_widget_destroy(filesel); | |
301 } | |
302 | |
303 static void filebrowser_play_classic(GtkWidget * w, GtkWidget * filesel) | |
304 { | |
305 gchar **files; | |
306 | |
307 if (util_filebrowser_is_dir_classic | |
308 (GTK_FILE_SELECTION(GTK_FILE_SELECTION(filesel)))) | |
309 return; | |
310 playlist_clear(playlist_get_active()); | |
311 files = gtk_file_selection_get_selections(GTK_FILE_SELECTION(filesel)); | |
312 filebrowser_add_files_classic(files, GTK_FILE_SELECTION(filesel)); | |
313 gtk_widget_destroy(filesel); | |
314 playback_initiate(); | |
315 } | |
316 | |
317 static void filebrowser_add_selected_files_classic(GtkWidget * w, gpointer data) | |
318 { | |
319 gchar **files; | |
320 | |
321 GtkFileSelection *filesel = GTK_FILE_SELECTION(data); | |
322 files = gtk_file_selection_get_selections(filesel); | |
323 | |
324 filebrowser_add_files_classic(files, filesel); | |
325 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection | |
326 (GTK_TREE_VIEW(filesel->file_list))); | |
327 | |
328 gtk_entry_set_text(GTK_ENTRY(filesel->selection_entry), ""); | |
329 } | |
330 | |
331 static void filebrowser_add_all_files_classic(GtkWidget * w, gpointer data) | |
332 { | |
333 gchar **files; | |
334 GtkFileSelection *filesel; | |
335 | |
336 filesel = data; | |
337 gtk_tree_selection_select_all(gtk_tree_view_get_selection | |
338 (GTK_TREE_VIEW(filesel->file_list))); | |
339 files = gtk_file_selection_get_selections(filesel); | |
340 filebrowser_add_files_classic(files, filesel); | |
341 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection | |
342 (GTK_TREE_VIEW(filesel->file_list))); | |
343 gtk_entry_set_text(GTK_ENTRY(filesel->selection_entry), ""); | |
344 } | |
345 | |
346 void | |
347 util_run_filebrowser_classic(gboolean play_button) | |
348 { | |
349 static GtkWidget *dialog; | |
350 GtkWidget *button_add_selected, *button_add_all, *button_close, | |
351 *button_add; | |
352 char *title; | |
353 | |
354 if (dialog != NULL) { | |
355 gtk_window_present(GTK_WINDOW(dialog)); | |
356 return; | |
357 } | |
358 | |
359 if (play_button) | |
360 title = _("Play files"); | |
361 else | |
362 title = _("Load files"); | |
363 | |
364 dialog = gtk_file_selection_new(title); | |
365 | |
366 gtk_file_selection_set_select_multiple | |
367 (GTK_FILE_SELECTION(dialog), TRUE); | |
368 | |
369 if (cfg.filesel_path) | |
370 gtk_file_selection_set_filename(GTK_FILE_SELECTION(dialog), | |
371 cfg.filesel_path); | |
372 | |
373 gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(dialog)); | |
374 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); | |
375 | |
376 gtk_widget_hide(GTK_FILE_SELECTION(dialog)->ok_button); | |
377 gtk_widget_destroy(GTK_FILE_SELECTION(dialog)->cancel_button); | |
378 | |
379 /* | |
380 * The mnemonics are quite unorthodox, but that should guarantee they're unique in any locale | |
381 * plus kinda easy to use | |
382 */ | |
383 button_add_selected = | |
384 gtk_dialog_add_button(GTK_DIALOG(dialog), "Add selected", | |
385 GTK_RESPONSE_NONE); | |
386 gtk_button_set_use_underline(GTK_BUTTON(button_add_selected), TRUE); | |
387 g_signal_connect(G_OBJECT(button_add_selected), "clicked", | |
388 G_CALLBACK(filebrowser_add_selected_files_classic), dialog); | |
389 | |
390 button_add_all = | |
391 gtk_dialog_add_button(GTK_DIALOG(dialog), "Add all", | |
392 GTK_RESPONSE_NONE); | |
393 gtk_button_set_use_underline(GTK_BUTTON(button_add_all), TRUE); | |
394 g_signal_connect(G_OBJECT(button_add_all), "clicked", | |
395 G_CALLBACK(filebrowser_add_all_files_classic), dialog); | |
396 | |
397 if (play_button) { | |
398 button_add = | |
399 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_MEDIA_PLAY, | |
400 GTK_RESPONSE_NONE); | |
401 gtk_button_set_use_stock(GTK_BUTTON(button_add), TRUE); | |
402 g_signal_connect(G_OBJECT(button_add), "clicked", | |
403 G_CALLBACK(filebrowser_play_classic), dialog); | |
404 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), | |
405 "clicked", G_CALLBACK(filebrowser_play_classic), dialog); | |
406 } else { | |
407 button_add = | |
408 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_ADD, | |
409 GTK_RESPONSE_NONE); | |
410 gtk_button_set_use_stock(GTK_BUTTON(button_add), TRUE); | |
411 g_signal_connect(G_OBJECT(button_add), "clicked", | |
412 G_CALLBACK(filebrowser_ok_classic), dialog); | |
413 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), | |
414 "clicked", G_CALLBACK(filebrowser_ok_classic), dialog); | |
415 } | |
416 | |
417 button_close = | |
418 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CLOSE, | |
419 GTK_RESPONSE_NONE); | |
420 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
421 g_signal_connect_swapped(G_OBJECT(button_close), "clicked", | |
422 G_CALLBACK(gtk_widget_destroy), | |
423 G_OBJECT(dialog)); | |
424 | |
425 gtk_widget_set_size_request(dialog, 600, 450); | |
426 gtk_widget_realize(dialog); | |
427 | |
428 g_signal_connect(G_OBJECT | |
429 (GTK_FILE_SELECTION(dialog)->history_pulldown), | |
430 "changed", G_CALLBACK(filebrowser_entry_changed_classic), | |
431 dialog); | |
432 | |
433 g_signal_connect(G_OBJECT(dialog), "destroy", | |
434 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
435 | |
436 filebrowser_changed_classic(GTK_FILE_SELECTION(dialog)); | |
437 | |
438 gtk_widget_show(dialog); | |
439 } | |
440 | |
441 /* | |
442 * util_run_filebrowser(gboolean play_button) | |
443 * | |
444 * Inputs: | |
445 * - whether or not a play button should be used | |
446 * | |
447 * Outputs: | |
448 * - none | |
449 * | |
450 * Side Effects: | |
451 * - either a GTK1 or a GTK2 fileselector is launched | |
452 */ | |
453 void | |
454 run_filebrowser(gboolean play_button) | |
455 { | |
456 if (!cfg.use_xmms_style_fileselector) | |
457 util_run_filebrowser_gtk2style(play_button); | |
458 else | |
459 util_run_filebrowser_classic(play_button); | |
460 } |