Commit 019c7ed4 authored by Murray Cumming's avatar Murray Cumming Committed by Murray Cumming
Browse files

Scale images down to fit the allocated size of the Image widget.

2005-07-12  Murray Cumming  <murrayc@murrayc.com>

        * glom/utility_widgets/imageglom.cc:
        * glom/utility_widgets/imageglom.h: Scale images down to fit the
        allocated size of the Image widget.
parent 357393d0
2005-07-12 Murray Cumming <murrayc@murrayc.com>
* glom/utility_widgets/imageglom.cc:
* glom/utility_widgets/imageglom.h: Scale images down to fit the
allocated size of the Image widget.
2005-07-12 Murray Cumming <murrayc@murrayc.com>
* glom/utility_widgets/Makefile.am:
......
......@@ -40,8 +40,6 @@ ImageGlom::~ImageGlom()
bool ImageGlom::on_button_press_event(GdkEventButton *event)
{
g_warning("ImageGlom::on_button_press_event()");
GdkModifierType mods;
gdk_window_get_pointer( Gtk::Widget::gobj()->window, 0, 0, &mods );
......@@ -68,24 +66,47 @@ bool ImageGlom::on_button_press_event(GdkEventButton *event)
return true; //We handled this event.
}
}
}
//Single-click to select file:
if(mods & GDK_BUTTON1_MASK)
{
Gtk::FileChooserDialog dialog(_("Choose image"), Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
int response = dialog.run();
dialog.hide();
if(response != Gtk::RESPONSE_CANCEL)
//Single-click to select file:
if(mods & GDK_BUTTON1_MASK)
{
const std::string filepath = dialog.get_filename();
if(!filepath.empty())
Gtk::FileChooserDialog dialog(_("Choose image"), Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(_("Select"), Gtk::RESPONSE_OK);
int response = dialog.run();
dialog.hide();
if(response != Gtk::RESPONSE_CANCEL)
{
//TODO: Scale it down to the preferred size:
m_image.set(filepath); //Load the image.
return true; //We handled this event.
const std::string filepath = dialog.get_filename();
if(!filepath.empty())
{
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
try
{
pixbuf = Gdk::Pixbuf::create_from_file(filepath);
//Scale it down to the preferred size:
//TODO : This will be silly if we allow different image sizes on different layouts:
const Gtk::Allocation allocation = m_image.get_allocation();
const int pixbuf_height = pixbuf->get_height();
const int pixbuf_width = pixbuf->get_width();
if( (pixbuf_height > allocation.get_height()) ||
(pixbuf_width > allocation.get_width()) )
{
pixbuf = scale_keeping_ratio(pixbuf, allocation.get_height(), allocation.get_width());
}
if(pixbuf)
m_image.set(pixbuf); //Load the image.
}
catch(const Glib::Exception& ex)
{
Frame_Glom::show_ok_dialog(_("Image loading failed"), _("The image file could not be opened:\n") + ex.what(), *pApp);
}
return true; //We handled this event.
}
}
}
}
......@@ -112,3 +133,56 @@ Gnome::Gda::Value ImageGlom::get_value() const
//TODO
return result;
}
//static:
Glib::RefPtr<Gdk::Pixbuf> ImageGlom::scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixbuf> pixbuf, int target_height, int target_width)
{
enum enum_scale_mode
{
SCALE_WIDTH,
SCALE_HEIGHT,
SCALE_NONE
};
enum_scale_mode scale_mode = SCALE_NONE; //Start with either the width or height, and scale the other according to the ratio.
const int pixbuf_height = pixbuf->get_height();
const int pixbuf_width = pixbuf->get_width();
if(pixbuf_height > target_height)
{
if(pixbuf_width > target_width)
{
//Both are bigger than the target, so find the biggest one:
if(pixbuf_width > pixbuf_height)
scale_mode = SCALE_WIDTH;
else
scale_mode = SCALE_HEIGHT;
}
else
{
//Only the height is bigger:
scale_mode = SCALE_HEIGHT;
}
}
else if(pixbuf_width > target_width)
{
//Only the height is bigger:
scale_mode = SCALE_WIDTH;
}
if(scale_mode == SCALE_NONE)
return pixbuf;
else if(scale_mode == SCALE_HEIGHT)
{
float ratio = (float)target_height / (float)pixbuf_height;
target_width = (int)((float)pixbuf_width * ratio);
}
else if(scale_mode == SCALE_WIDTH)
{
float ratio = (float)target_width / (float) pixbuf_width;
target_height = (int)((float)pixbuf_height * ratio);
}
return pixbuf->scale_simple(target_width, target_height, Gdk::INTERP_NEAREST);
}
......@@ -44,6 +44,7 @@ protected:
virtual bool on_button_press_event(GdkEventButton *event);
virtual App_Glom* get_application();
static Glib::RefPtr<Gdk::Pixbuf> scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixbuf> pixbu, int target_height, int target_width);
Gtk::Image m_image;
};
......
Supports Markdown
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