Commit c7f9141a authored by estade@chromium.org's avatar estade@chromium.org

GTK: Preview images in file chooser.

BUG=http://crbug.com/15500
TEST=select an image in a file chooser

Review URL: http://codereview.chromium.org/151100

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19874 0039d316-1c4b-4281-b951-d872f2087c98
parent 7e43da7c
......@@ -17,6 +17,14 @@
#include "chrome/browser/shell_dialogs.h"
#include "grit/generated_resources.h"
// The size of the preview we display for selected image files. We set height
// larger than width because generally there is more free space vertically
// than horiztonally (setting the preview image will alway expand the width of
// the dialog, but usually not the height). The image's aspect ratio will always
// be preserved.
static const int kPreviewWidth = 256;
static const int kPreviewHeight = 512;
// Implementation of SelectFileDialog that shows a Gtk common dialog for
// choosing a file or folder.
// This acts as a modal dialog. Ideally we want to only act modally for the
......@@ -88,6 +96,10 @@ class SelectFileDialogImpl : public SelectFileDialog {
static void OnSelectMultiFileDialogResponse(
GtkWidget* dialog, gint response_id, SelectFileDialogImpl* dialog_impl);
// Callback for when we update the preview for the selection.
static void OnUpdatePreview(GtkFileChooser* chooser,
SelectFileDialogImpl* dialog);
// The listener to be notified of selection completion.
Listener* listener_;
......@@ -112,6 +124,9 @@ class SelectFileDialogImpl : public SelectFileDialog {
static FilePath* last_saved_path_;
static FilePath* last_opened_path_;
// The GtkImage widget for showing previews of selected images.
GtkWidget* preview_;
DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl);
};
......@@ -188,6 +203,10 @@ void SelectFileDialogImpl::SelectFile(
return;
}
preview_ = gtk_image_new();
g_signal_connect(dialog, "update-preview", G_CALLBACK(OnUpdatePreview), this);
gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(dialog), preview_);
params_map_[dialog] = params;
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_widget_show_all(dialog);
......@@ -409,3 +428,20 @@ void SelectFileDialogImpl::OnSelectMultiFileDialogResponse(
g_slist_free(filenames);
dialog_impl->MultiFilesSelected(dialog, filenames_fp);
}
// static
void SelectFileDialogImpl::OnUpdatePreview(GtkFileChooser* chooser,
SelectFileDialogImpl* dialog) {
gchar* filename = gtk_file_chooser_get_preview_filename(chooser);
if (!filename)
return;
// This will preserve the image's aspect ratio.
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth,
kPreviewHeight, NULL);
g_free(filename);
if (pixbuf) {
gtk_image_set_from_pixbuf(GTK_IMAGE(dialog->preview_), pixbuf);
g_object_unref(pixbuf);
}
gtk_file_chooser_set_preview_widget_active(chooser, pixbuf ? TRUE : FALSE);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment