2 * Copyright (C) 2003 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
34 # define localtime_r(t,b) *(b) = *localtime (t)
36 # define S_ISREG(m) ((m) & _S_IFREG)
40 #include "prop-editor.h"
42 static GtkWidget *preview_label;
43 static GtkWidget *preview_image;
44 static GtkFileChooserAction action;
47 print_current_folder (GtkFileChooser *chooser)
51 uri = gtk_file_chooser_get_current_folder_uri (chooser);
52 g_print ("Current folder changed :\n %s\n", uri ? uri : "(null)");
57 print_selected (GtkFileChooser *chooser)
59 GSList *uris = gtk_file_chooser_get_uris (chooser);
62 g_print ("Selection changed :\n");
63 for (tmp_list = uris; tmp_list; tmp_list = tmp_list->next)
65 gchar *uri = tmp_list->data;
66 g_print (" %s\n", uri);
74 response_cb (GtkDialog *dialog,
77 if (response_id == GTK_RESPONSE_OK)
81 list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog));
87 g_print ("Selected files:\n");
89 for (l = list; l; l = l->next)
91 g_print ("%s\n", (char *) l->data);
98 g_print ("No selected files\n");
101 g_print ("Dialog was closed\n");
107 no_backup_files_filter (const GtkFileFilterInfo *filter_info,
110 gsize len = filter_info->display_name ? strlen (filter_info->display_name) : 0;
111 if (len > 0 && filter_info->display_name[len - 1] == '~')
118 filter_changed (GtkFileChooserDialog *dialog,
121 g_print ("file filter changed\n");
125 format_time (time_t t)
129 time_t now = time (NULL);
132 if (abs (now - t) < 24*60*60)
137 localtime_r (&t, &tm_buf);
138 if (strftime (buf, sizeof (buf), format, &tm_buf) == 0)
139 return g_strdup ("<unknown>");
141 return g_strdup (buf);
145 format_size (gint64 size)
147 if (size < (gint64)1024)
148 return g_strdup_printf ("%d bytes", (gint)size);
149 else if (size < (gint64)1024*1024)
150 return g_strdup_printf ("%.1f K", size / (1024.));
151 else if (size < (gint64)1024*1024*1024)
152 return g_strdup_printf ("%.1f M", size / (1024.*1024.));
154 return g_strdup_printf ("%.1f G", size / (1024.*1024.*1024.));
162 size_prepared_cb (GdkPixbufLoader *loader,
167 int des_width = data[0];
168 int des_height = data[1];
170 if (des_height >= height && des_width >= width) {
172 } else if ((double)height * des_width > (double)width * des_height) {
173 width = 0.5 + (double)width * des_height / (double)height;
176 height = 0.5 + (double)height * des_width / (double)width;
180 gdk_pixbuf_loader_set_size (loader, width, height);
184 my_new_from_file_at_size (const char *filename,
189 GdkPixbufLoader *loader;
194 guchar buffer [4096];
198 g_return_val_if_fail (filename != NULL, NULL);
199 g_return_val_if_fail (width > 0 && height > 0, NULL);
201 if (stat (filename, &st) != 0) {
206 g_file_error_from_errno (errsv),
207 _("Could not get information for file '%s': %s"),
208 filename, g_strerror (errsv));
212 if (!S_ISREG (st.st_mode))
215 f = fopen (filename, "rb");
221 g_file_error_from_errno (errsv),
222 _("Failed to open file '%s': %s"),
223 filename, g_strerror (errsv));
227 loader = gdk_pixbuf_loader_new ();
228 #ifdef DONT_PRESERVE_ASPECT
229 gdk_pixbuf_loader_set_size (loader, width, height);
233 g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb), info);
237 length = fread (buffer, 1, sizeof (buffer), f);
239 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
240 gdk_pixbuf_loader_close (loader, NULL);
242 g_object_unref (loader);
249 g_assert (*error == NULL);
250 if (!gdk_pixbuf_loader_close (loader, error)) {
251 g_object_unref (loader);
255 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
258 g_object_unref (loader);
260 /* did the loader set an error? */
266 GDK_PIXBUF_ERROR_FAILED,
267 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
272 g_object_ref (pixbuf);
274 g_object_unref (loader);
280 update_preview_cb (GtkFileChooser *chooser)
282 gchar *filename = gtk_file_chooser_get_preview_filename (chooser);
283 gboolean have_preview = FALSE;
288 GError *error = NULL;
290 pixbuf = my_new_from_file_at_size (filename, 128, 128, &error);
293 gtk_image_set_from_pixbuf (GTK_IMAGE (preview_image), pixbuf);
294 g_object_unref (pixbuf);
295 gtk_widget_show (preview_image);
296 gtk_widget_hide (preview_label);
302 if (stat (filename, &buf) == 0)
306 gchar *modified_time;
308 size_str = format_size (buf.st_size);
309 modified_time = format_time (buf.st_mtime);
311 preview_text = g_strdup_printf ("<i>Modified:</i>\t%s\n"
312 "<i>Size:</i>\t%s\n",
315 gtk_label_set_markup (GTK_LABEL (preview_label), preview_text);
316 g_free (modified_time);
318 g_free (preview_text);
320 gtk_widget_hide (preview_image);
321 gtk_widget_show (preview_label);
329 g_error_free (error);
332 gtk_file_chooser_set_preview_widget_active (chooser, have_preview);
336 set_current_folder (GtkFileChooser *chooser,
339 if (!gtk_file_chooser_set_current_folder (chooser, name))
343 dialog = gtk_message_dialog_new (GTK_WINDOW (chooser),
344 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
347 "Could not set the folder to %s",
349 gtk_dialog_run (GTK_DIALOG (dialog));
350 gtk_widget_destroy (dialog);
355 set_folder_nonexistent_cb (GtkButton *button,
356 GtkFileChooser *chooser)
358 set_current_folder (chooser, "/nonexistent");
362 set_folder_existing_nonexistent_cb (GtkButton *button,
363 GtkFileChooser *chooser)
365 set_current_folder (chooser, "/usr/nonexistent");
369 set_filename (GtkFileChooser *chooser,
372 if (!gtk_file_chooser_set_filename (chooser, name))
376 dialog = gtk_message_dialog_new (GTK_WINDOW (chooser),
377 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
380 "Could not select %s",
382 gtk_dialog_run (GTK_DIALOG (dialog));
383 gtk_widget_destroy (dialog);
388 set_filename_nonexistent_cb (GtkButton *button,
389 GtkFileChooser *chooser)
391 set_filename (chooser, "/nonexistent");
395 set_filename_existing_nonexistent_cb (GtkButton *button,
396 GtkFileChooser *chooser)
398 set_filename (chooser, "/usr/nonexistent");
402 unmap_and_remap_cb (GtkButton *button,
403 GtkFileChooser *chooser)
405 gtk_widget_hide (GTK_WIDGET (chooser));
406 gtk_widget_show (GTK_WIDGET (chooser));
410 kill_dependent (GtkWindow *win, GtkObject *dep)
412 gtk_object_destroy (dep);
413 g_object_unref (dep);
417 notify_multiple_cb (GtkWidget *dialog,
423 multiple = gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (dialog));
425 gtk_widget_set_sensitive (button, multiple);
428 static GtkFileChooserConfirmation
429 confirm_overwrite_cb (GtkFileChooser *chooser,
435 GtkFileChooserConfirmation conf;
437 dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (chooser))),
438 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
439 GTK_MESSAGE_QUESTION,
441 "What do you want to do?");
443 button = gtk_button_new_with_label ("Use the stock confirmation dialog");
444 gtk_widget_show (button);
445 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, 1);
447 button = gtk_button_new_with_label ("Type a new file name");
448 gtk_widget_show (button);
449 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, 2);
451 button = gtk_button_new_with_label ("Accept the file name");
452 gtk_widget_show (button);
453 gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, 3);
455 response = gtk_dialog_run (GTK_DIALOG (dialog));
460 conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
464 conf = GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
468 conf = GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN;
472 gtk_widget_destroy (dialog);
478 main (int argc, char **argv)
480 GtkWidget *control_window;
484 GtkWidget *prop_editor;
486 GtkFileFilter *filter;
487 GtkWidget *preview_vbox;
488 gboolean force_rtl = FALSE;
489 gboolean multiple = FALSE;
490 char *action_arg = NULL;
491 char *backend = NULL;
492 char *initial_filename = NULL;
493 char *initial_folder = NULL;
494 GError *error = NULL;
495 GOptionEntry options[] = {
496 { "action", 'a', 0, G_OPTION_ARG_STRING, &action_arg, "Filechooser action", "ACTION" },
497 { "backend", 'b', 0, G_OPTION_ARG_STRING, &backend, "Filechooser backend (default: gtk+)", "BACKEND" },
498 { "multiple", 'm', 0, G_OPTION_ARG_NONE, &multiple, "Select-multiple", NULL },
499 { "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &force_rtl, "Force right-to-left layout.", NULL },
500 { "initial-filename", 'f', 0, G_OPTION_ARG_FILENAME, &initial_filename, "Initial filename to select", "FILENAME" },
501 { "initial-folder", 'F', 0, G_OPTION_ARG_FILENAME, &initial_folder, "Initial folder to show", "FILENAME" },
505 if (!gtk_init_with_args (&argc, &argv, "", options, NULL, &error))
507 g_print ("Failed to parse args: %s\n", error->message);
508 g_error_free (error);
512 if (initial_filename && initial_folder)
514 g_print ("Only one of --initial-filename and --initial-folder may be specified");
519 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
521 action = GTK_FILE_CHOOSER_ACTION_OPEN;
523 if (action_arg != NULL)
525 if (! strcmp ("open", action_arg))
526 action = GTK_FILE_CHOOSER_ACTION_OPEN;
527 else if (! strcmp ("save", action_arg))
528 action = GTK_FILE_CHOOSER_ACTION_SAVE;
529 else if (! strcmp ("select_folder", action_arg))
530 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
531 else if (! strcmp ("create_folder", action_arg))
532 action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
538 backend = g_strdup ("gtk+");
540 dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
542 "file-system-backend", backend,
543 "select-multiple", multiple,
550 case GTK_FILE_CHOOSER_ACTION_OPEN:
551 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
552 gtk_window_set_title (GTK_WINDOW (dialog), "Select a file");
553 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
554 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
555 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
558 case GTK_FILE_CHOOSER_ACTION_SAVE:
559 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
560 gtk_window_set_title (GTK_WINDOW (dialog), "Save a file");
561 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
562 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
563 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
567 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
569 g_signal_connect (dialog, "selection-changed",
570 G_CALLBACK (print_selected), NULL);
571 g_signal_connect (dialog, "current-folder-changed",
572 G_CALLBACK (print_current_folder), NULL);
573 g_signal_connect (dialog, "response",
574 G_CALLBACK (response_cb), NULL);
575 g_signal_connect (dialog, "confirm-overwrite",
576 G_CALLBACK (confirm_overwrite_cb), NULL);
579 filter = gtk_file_filter_new ();
580 gtk_file_filter_set_name (filter, "All Files");
581 gtk_file_filter_add_pattern (filter, "*");
582 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
584 filter = gtk_file_filter_new ();
585 gtk_file_filter_set_name (filter, "No backup files");
586 gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_DISPLAY_NAME,
587 no_backup_files_filter, NULL, NULL);
588 gtk_file_filter_add_mime_type (filter, "image/png");
589 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
591 g_signal_connect (dialog, "notify::filter",
592 G_CALLBACK (filter_changed), NULL);
594 /* Make this filter the default */
595 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
597 filter = gtk_file_filter_new ();
598 gtk_file_filter_set_name (filter, "PNG and JPEG");
599 gtk_file_filter_add_mime_type (filter, "image/jpeg");
600 gtk_file_filter_add_mime_type (filter, "image/png");
601 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
603 filter = gtk_file_filter_new ();
604 gtk_file_filter_set_name (filter, "Images");
605 gtk_file_filter_add_pixbuf_formats (filter);
606 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
609 /* THIS IS A TERRIBLE PREVIEW WIDGET, AND SHOULD NOT BE COPIED AT ALL.
611 preview_vbox = gtk_vbox_new (0, FALSE);
612 /*gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (dialog), preview_vbox);*/
614 preview_label = gtk_label_new (NULL);
615 gtk_box_pack_start (GTK_BOX (preview_vbox), preview_label, TRUE, TRUE, 0);
616 gtk_misc_set_padding (GTK_MISC (preview_label), 6, 6);
618 preview_image = gtk_image_new ();
619 gtk_box_pack_start (GTK_BOX (preview_vbox), preview_image, TRUE, TRUE, 0);
620 gtk_misc_set_padding (GTK_MISC (preview_image), 6, 6);
622 update_preview_cb (GTK_FILE_CHOOSER (dialog));
623 g_signal_connect (dialog, "update-preview",
624 G_CALLBACK (update_preview_cb), NULL);
628 extra = gtk_check_button_new_with_mnemonic ("Lar_t whoever asks about this button");
629 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (extra), TRUE);
630 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), extra);
634 gtk_file_chooser_add_shortcut_folder_uri (GTK_FILE_CHOOSER (dialog),
635 "file:///usr/share/pixmaps",
638 /* Initial filename or folder */
640 if (initial_filename)
641 set_filename (GTK_FILE_CHOOSER (dialog), initial_filename);
644 set_current_folder (GTK_FILE_CHOOSER (dialog), initial_folder);
646 /* show_all() to reveal bugs in composite widget handling */
647 gtk_widget_show_all (dialog);
649 /* Extra controls for manipulating the test environment
651 prop_editor = create_prop_editor (G_OBJECT (dialog), GTK_TYPE_FILE_CHOOSER);
653 control_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
655 vbbox = gtk_vbutton_box_new ();
656 gtk_container_add (GTK_CONTAINER (control_window), vbbox);
658 button = gtk_button_new_with_mnemonic ("_Select all");
659 gtk_widget_set_sensitive (button, multiple);
660 gtk_container_add (GTK_CONTAINER (vbbox), button);
661 g_signal_connect_swapped (button, "clicked",
662 G_CALLBACK (gtk_file_chooser_select_all), dialog);
663 g_signal_connect (dialog, "notify::select-multiple",
664 G_CALLBACK (notify_multiple_cb), button);
666 button = gtk_button_new_with_mnemonic ("_Unselect all");
667 gtk_container_add (GTK_CONTAINER (vbbox), button);
668 g_signal_connect_swapped (button, "clicked",
669 G_CALLBACK (gtk_file_chooser_unselect_all), dialog);
671 button = gtk_button_new_with_label ("set_current_folder (\"/nonexistent\")");
672 gtk_container_add (GTK_CONTAINER (vbbox), button);
673 g_signal_connect (button, "clicked",
674 G_CALLBACK (set_folder_nonexistent_cb), dialog);
676 button = gtk_button_new_with_label ("set_current_folder (\"/usr/nonexistent\")");
677 gtk_container_add (GTK_CONTAINER (vbbox), button);
678 g_signal_connect (button, "clicked",
679 G_CALLBACK (set_folder_existing_nonexistent_cb), dialog);
681 button = gtk_button_new_with_label ("set_filename (\"/nonexistent\")");
682 gtk_container_add (GTK_CONTAINER (vbbox), button);
683 g_signal_connect (button, "clicked",
684 G_CALLBACK (set_filename_nonexistent_cb), dialog);
686 button = gtk_button_new_with_label ("set_filename (\"/usr/nonexistent\")");
687 gtk_container_add (GTK_CONTAINER (vbbox), button);
688 g_signal_connect (button, "clicked",
689 G_CALLBACK (set_filename_existing_nonexistent_cb), dialog);
691 button = gtk_button_new_with_label ("Unmap and remap");
692 gtk_container_add (GTK_CONTAINER (vbbox), button);
693 g_signal_connect (button, "clicked",
694 G_CALLBACK (unmap_and_remap_cb), dialog);
696 gtk_widget_show_all (control_window);
698 g_object_ref (control_window);
699 g_signal_connect (dialog, "destroy",
700 G_CALLBACK (kill_dependent), control_window);
702 /* We need to hold a ref until we have destroyed the widgets, just in case
703 * someone else destroys them. We explicitly destroy windows to catch leaks.
705 g_object_ref (dialog);
707 gtk_widget_destroy (dialog);
708 g_object_unref (dialog);