Mercurial > audlegacy
annotate src/audacious/ui_fileopener.c @ 2494:59661bd074b4 trunk
[svn] Try to put some skinned window code in a common place.
author | nenolod |
---|---|
date | Sat, 10 Feb 2007 17:01:44 -0800 |
parents | 49e27e4d8426 |
children | 04642d2d0941 |
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 if (cfg.close_dialog_open) | |
96 gtk_widget_destroy(window); | |
97 } | |
98 | |
99 | |
100 static void | |
101 close_button_cb(GtkWidget *widget, gpointer data) | |
102 { | |
103 gtk_widget_destroy(GTK_WIDGET(data)); | |
104 } | |
105 | |
2432
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
106 static gboolean |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
107 filebrowser_on_keypress(GtkWidget * browser, |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
108 GdkEventKey * event, |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
109 gpointer data) |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
110 { |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
111 if (event->keyval == GDK_Escape) { |
2449 | 112 gtk_widget_destroy(browser); |
2432
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
113 return TRUE; |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
114 } |
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 return FALSE; |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
117 } |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
118 |
2417 | 119 void |
120 util_run_filebrowser_gtk2style(gboolean play_button) | |
121 { | |
2449 | 122 static GtkWidget *window = NULL; |
2417 | 123 GtkWidget *vbox, *hbox, *bbox; |
124 GtkWidget *chooser; | |
125 GtkWidget *action_button, *close_button; | |
126 GtkWidget *toggle; | |
127 | |
2490
49e27e4d8426
[svn] - trying to open filebrowser when it is already open will raise the filebrowser.
yaz
parents:
2449
diff
changeset
|
128 if(window) { |
49e27e4d8426
[svn] - trying to open filebrowser when it is already open will raise the filebrowser.
yaz
parents:
2449
diff
changeset
|
129 gtk_window_present(GTK_WINDOW(window)); /* raise filebrowser */ |
2449 | 130 return; |
2490
49e27e4d8426
[svn] - trying to open filebrowser when it is already open will raise the filebrowser.
yaz
parents:
2449
diff
changeset
|
131 } |
2449 | 132 |
2417 | 133 gchar *window_title = play_button ? _("Open Files") : _("Add Files"); |
134 gchar *toggle_text = play_button ? | |
135 _("Close dialog on Open") : _("Close dialog on Add"); | |
136 gpointer action_stock = play_button ? GTK_STOCK_OPEN : GTK_STOCK_ADD; | |
137 | |
138 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
139 gtk_window_set_title(GTK_WINDOW(window), window_title); | |
140 gtk_window_set_default_size(GTK_WINDOW(window), 700, 450); | |
2440
7f0318090690
[svn] - set the gtk2 fileopener position to the center of the screen
giacomo
parents:
2432
diff
changeset
|
141 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
2417 | 142 gtk_container_set_border_width(GTK_CONTAINER(window), 10); |
143 | |
144 vbox = gtk_vbox_new(FALSE, 0); | |
145 gtk_container_add(GTK_CONTAINER(window), vbox); | |
146 | |
147 chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN); | |
148 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), TRUE); | |
149 if (cfg.filesel_path) | |
150 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), | |
151 cfg.filesel_path); | |
2422 | 152 gtk_box_pack_start(GTK_BOX(vbox), chooser, TRUE, TRUE, 3); |
2417 | 153 |
154 hbox = gtk_hbox_new(TRUE, 0); | |
2422 | 155 gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 3); |
2417 | 156 |
157 toggle = gtk_check_button_new_with_label(toggle_text); | |
158 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), | |
159 cfg.close_dialog_open ? TRUE : FALSE); | |
2422 | 160 gtk_box_pack_start(GTK_BOX(hbox), toggle, TRUE, TRUE, 3); |
2417 | 161 |
162 bbox = gtk_hbutton_box_new(); | |
163 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
2422 | 164 gtk_box_set_spacing(GTK_BOX(bbox), 6); |
165 gtk_box_pack_end(GTK_BOX(hbox), bbox, TRUE, TRUE, 3); | |
2417 | 166 |
167 close_button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
168 action_button = gtk_button_new_from_stock(action_stock); | |
169 gtk_container_add(GTK_CONTAINER(bbox), close_button); | |
170 gtk_container_add(GTK_CONTAINER(bbox), action_button); | |
171 | |
2420 | 172 /* this storage object holds several other objects which are used in the |
173 * callback functions | |
174 */ | |
2417 | 175 gpointer storage = g_object_new(G_TYPE_OBJECT, NULL); |
176 g_object_set_data(storage, "window", window); | |
177 g_object_set_data(storage, "chooser", chooser); | |
178 g_object_set_data(storage, "toggle-button", toggle); | |
179 g_object_set_data(storage, "play-button", GINT_TO_POINTER(play_button)); | |
180 | |
181 g_signal_connect(chooser, "file-activated", | |
182 G_CALLBACK(action_button_cb), storage); | |
183 g_signal_connect(action_button, "clicked", | |
184 G_CALLBACK(action_button_cb), storage); | |
185 g_signal_connect(close_button, "clicked", | |
186 G_CALLBACK(close_button_cb), window); | |
187 g_signal_connect(window, "destroy", | |
188 G_CALLBACK(gtk_widget_destroyed), &window); | |
189 | |
2432
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
190 g_signal_connect(window, "key_press_event", |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
191 G_CALLBACK(filebrowser_on_keypress), |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
192 NULL); |
4a1d1c324685
[svn] - restore shortcut key (esc) to close gtk2 fileopener.
yaz
parents:
2422
diff
changeset
|
193 |
2417 | 194 gtk_widget_show_all(window); |
195 } | |
196 | |
197 /* | |
198 * Derived from Beep Media Player 0.9.6.1. | |
199 * Which is (C) 2003 - 2006 Milosz Derezynski &c | |
200 * | |
201 * Although I changed it quite a bit. -nenolod | |
202 */ | |
203 static void filebrowser_changed_classic(GtkFileSelection * filesel) | |
204 { | |
205 GList *list; | |
206 GList *node; | |
207 char *filename = (char *) | |
208 gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); | |
209 GtkListStore *store; | |
210 GtkTreeIter iter; | |
211 | |
212 if ((list = input_scan_dir(filename)) != NULL) | |
213 { | |
214 /* | |
215 * We enter a directory that has been "hijacked" by an | |
216 * input-plugin. This is used by the CDDA plugin | |
217 */ | |
218 store = | |
219 GTK_LIST_STORE(gtk_tree_view_get_model | |
220 (GTK_TREE_VIEW(filesel->file_list))); | |
221 gtk_list_store_clear(store); | |
222 | |
223 node = list; | |
224 while (node) { | |
225 gtk_list_store_append(store, &iter); | |
226 gtk_list_store_set(store, &iter, 0, node->data, -1); | |
227 g_free(node->data); | |
228 node = g_list_next(node); | |
229 } | |
230 | |
231 g_list_free(list); | |
232 } | |
233 } | |
234 | |
235 static void filebrowser_entry_changed_classic(GtkEditable * entry, gpointer data) | |
236 { | |
237 filebrowser_changed_classic(GTK_FILE_SELECTION(data)); | |
238 } | |
239 | |
240 | |
241 static gboolean | |
242 util_filebrowser_is_dir_classic(GtkFileSelection * filesel) | |
243 { | |
244 char *text; | |
245 struct stat buf; | |
246 gboolean retv = FALSE; | |
247 | |
248 text = g_strdup(gtk_file_selection_get_filename(filesel)); | |
249 | |
250 if (stat(text, &buf) == 0 && S_ISDIR(buf.st_mode)) { | |
251 /* Selected directory */ | |
252 int len = strlen(text); | |
253 if (len > 3 && !strcmp(text + len - 4, "/../")) { | |
254 if (len == 4) | |
255 /* At the root already */ | |
256 *(text + len - 3) = '\0'; | |
257 else { | |
258 char *ptr; | |
259 *(text + len - 4) = '\0'; | |
260 ptr = strrchr(text, '/'); | |
261 *(ptr + 1) = '\0'; | |
262 } | |
263 } else if (len > 2 && !strcmp(text + len - 3, "/./")) | |
264 *(text + len - 2) = '\0'; | |
265 gtk_file_selection_set_filename(filesel, text); | |
266 retv = TRUE; | |
267 } | |
268 g_free(text); | |
269 return retv; | |
270 } | |
271 | |
272 static void filebrowser_add_files_classic(gchar ** files, | |
273 GtkFileSelection * filesel) | |
274 { | |
275 int ctr = 0; | |
276 char *ptr; | |
277 Playlist *playlist = playlist_get_active(); | |
278 | |
279 if (GTK_IS_WIDGET(mainwin_jtf)) | |
280 gtk_widget_set_sensitive(mainwin_jtf, FALSE); | |
281 | |
282 while (files[ctr] != NULL) { | |
283 playlist_add(playlist, files[ctr++]); | |
284 } | |
285 playlistwin_update_list(playlist); | |
286 | |
287 if (GTK_IS_WIDGET(mainwin_jtf)) | |
288 gtk_widget_set_sensitive(mainwin_jtf, TRUE); | |
289 | |
290 gtk_label_get(GTK_LABEL(GTK_BIN(filesel->history_pulldown)->child), | |
291 &ptr); | |
292 | |
293 /* This will give an extra slash if the current dir is the root. */ | |
294 cfg.filesel_path = g_strconcat(ptr, "/", NULL); | |
295 } | |
296 | |
297 static void filebrowser_ok_classic(GtkWidget * w, GtkWidget * filesel) | |
298 { | |
299 gchar **files; | |
300 | |
301 if (util_filebrowser_is_dir_classic(GTK_FILE_SELECTION(filesel))) | |
302 return; | |
303 files = gtk_file_selection_get_selections(GTK_FILE_SELECTION(filesel)); | |
304 filebrowser_add_files_classic(files, GTK_FILE_SELECTION(filesel)); | |
305 gtk_widget_destroy(filesel); | |
306 } | |
307 | |
308 static void filebrowser_play_classic(GtkWidget * w, GtkWidget * filesel) | |
309 { | |
310 gchar **files; | |
311 | |
312 if (util_filebrowser_is_dir_classic | |
313 (GTK_FILE_SELECTION(GTK_FILE_SELECTION(filesel)))) | |
314 return; | |
315 playlist_clear(playlist_get_active()); | |
316 files = gtk_file_selection_get_selections(GTK_FILE_SELECTION(filesel)); | |
317 filebrowser_add_files_classic(files, GTK_FILE_SELECTION(filesel)); | |
318 gtk_widget_destroy(filesel); | |
319 playback_initiate(); | |
320 } | |
321 | |
322 static void filebrowser_add_selected_files_classic(GtkWidget * w, gpointer data) | |
323 { | |
324 gchar **files; | |
325 | |
326 GtkFileSelection *filesel = GTK_FILE_SELECTION(data); | |
327 files = gtk_file_selection_get_selections(filesel); | |
328 | |
329 filebrowser_add_files_classic(files, filesel); | |
330 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection | |
331 (GTK_TREE_VIEW(filesel->file_list))); | |
332 | |
333 gtk_entry_set_text(GTK_ENTRY(filesel->selection_entry), ""); | |
334 } | |
335 | |
336 static void filebrowser_add_all_files_classic(GtkWidget * w, gpointer data) | |
337 { | |
338 gchar **files; | |
339 GtkFileSelection *filesel; | |
340 | |
341 filesel = data; | |
342 gtk_tree_selection_select_all(gtk_tree_view_get_selection | |
343 (GTK_TREE_VIEW(filesel->file_list))); | |
344 files = gtk_file_selection_get_selections(filesel); | |
345 filebrowser_add_files_classic(files, filesel); | |
346 gtk_tree_selection_unselect_all(gtk_tree_view_get_selection | |
347 (GTK_TREE_VIEW(filesel->file_list))); | |
348 gtk_entry_set_text(GTK_ENTRY(filesel->selection_entry), ""); | |
349 } | |
350 | |
351 void | |
352 util_run_filebrowser_classic(gboolean play_button) | |
353 { | |
354 static GtkWidget *dialog; | |
355 GtkWidget *button_add_selected, *button_add_all, *button_close, | |
356 *button_add; | |
357 char *title; | |
358 | |
359 if (dialog != NULL) { | |
360 gtk_window_present(GTK_WINDOW(dialog)); | |
361 return; | |
362 } | |
363 | |
364 if (play_button) | |
365 title = _("Play files"); | |
366 else | |
367 title = _("Load files"); | |
368 | |
369 dialog = gtk_file_selection_new(title); | |
370 | |
371 gtk_file_selection_set_select_multiple | |
372 (GTK_FILE_SELECTION(dialog), TRUE); | |
373 | |
374 if (cfg.filesel_path) | |
375 gtk_file_selection_set_filename(GTK_FILE_SELECTION(dialog), | |
376 cfg.filesel_path); | |
377 | |
378 gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(dialog)); | |
379 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); | |
380 | |
381 gtk_widget_hide(GTK_FILE_SELECTION(dialog)->ok_button); | |
382 gtk_widget_destroy(GTK_FILE_SELECTION(dialog)->cancel_button); | |
383 | |
384 /* | |
385 * The mnemonics are quite unorthodox, but that should guarantee they're unique in any locale | |
386 * plus kinda easy to use | |
387 */ | |
388 button_add_selected = | |
389 gtk_dialog_add_button(GTK_DIALOG(dialog), "Add selected", | |
390 GTK_RESPONSE_NONE); | |
391 gtk_button_set_use_underline(GTK_BUTTON(button_add_selected), TRUE); | |
392 g_signal_connect(G_OBJECT(button_add_selected), "clicked", | |
393 G_CALLBACK(filebrowser_add_selected_files_classic), dialog); | |
394 | |
395 button_add_all = | |
396 gtk_dialog_add_button(GTK_DIALOG(dialog), "Add all", | |
397 GTK_RESPONSE_NONE); | |
398 gtk_button_set_use_underline(GTK_BUTTON(button_add_all), TRUE); | |
399 g_signal_connect(G_OBJECT(button_add_all), "clicked", | |
400 G_CALLBACK(filebrowser_add_all_files_classic), dialog); | |
401 | |
402 if (play_button) { | |
403 button_add = | |
404 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_MEDIA_PLAY, | |
405 GTK_RESPONSE_NONE); | |
406 gtk_button_set_use_stock(GTK_BUTTON(button_add), TRUE); | |
407 g_signal_connect(G_OBJECT(button_add), "clicked", | |
408 G_CALLBACK(filebrowser_play_classic), dialog); | |
409 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), | |
410 "clicked", G_CALLBACK(filebrowser_play_classic), dialog); | |
411 } else { | |
412 button_add = | |
413 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_ADD, | |
414 GTK_RESPONSE_NONE); | |
415 gtk_button_set_use_stock(GTK_BUTTON(button_add), TRUE); | |
416 g_signal_connect(G_OBJECT(button_add), "clicked", | |
417 G_CALLBACK(filebrowser_ok_classic), dialog); | |
418 g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(dialog)->ok_button), | |
419 "clicked", G_CALLBACK(filebrowser_ok_classic), dialog); | |
420 } | |
421 | |
422 button_close = | |
423 gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CLOSE, | |
424 GTK_RESPONSE_NONE); | |
425 gtk_button_set_use_stock(GTK_BUTTON(button_close), TRUE); | |
426 g_signal_connect_swapped(G_OBJECT(button_close), "clicked", | |
427 G_CALLBACK(gtk_widget_destroy), | |
428 G_OBJECT(dialog)); | |
429 | |
430 gtk_widget_set_size_request(dialog, 600, 450); | |
431 gtk_widget_realize(dialog); | |
432 | |
433 g_signal_connect(G_OBJECT | |
434 (GTK_FILE_SELECTION(dialog)->history_pulldown), | |
435 "changed", G_CALLBACK(filebrowser_entry_changed_classic), | |
436 dialog); | |
437 | |
438 g_signal_connect(G_OBJECT(dialog), "destroy", | |
439 G_CALLBACK(gtk_widget_destroyed), &dialog); | |
440 | |
441 filebrowser_changed_classic(GTK_FILE_SELECTION(dialog)); | |
442 | |
443 gtk_widget_show(dialog); | |
444 } | |
445 | |
446 /* | |
447 * util_run_filebrowser(gboolean play_button) | |
448 * | |
449 * Inputs: | |
450 * - whether or not a play button should be used | |
451 * | |
452 * Outputs: | |
453 * - none | |
454 * | |
455 * Side Effects: | |
456 * - either a GTK1 or a GTK2 fileselector is launched | |
457 */ | |
458 void | |
459 run_filebrowser(gboolean play_button) | |
460 { | |
461 if (!cfg.use_xmms_style_fileselector) | |
462 util_run_filebrowser_gtk2style(play_button); | |
463 else | |
464 util_run_filebrowser_classic(play_button); | |
465 } |