Commit eefed4f2 authored by Murray Cumming's avatar Murray Cumming
Browse files

ImageGlom: Move scale_keeping_ratio() somewhere more sensible.

* glom/utility_widgets/imageglom.[h|cc]: Move scale_keeping_ratio() to
glom/utils_ui.[h]cc] as Utils::image_scale_keeping_ratio().
* glom/utility_widgets/canvas/canvas_image_movable.cc:
* glom/utility_widgets/db_adddel/db_adddel.cc:
* glom/utility_widgets/flowtable_dnd.cc: Adapted.
parent 2c40be36
2010-04-08 Murray Cumming <murrayc@murrayc.com>
ImageGlom: Move scale_keeping_ratio() somewhere more sensible.
* glom/utility_widgets/imageglom.[h|cc]: Move scale_keeping_ratio() to
glom/utils_ui.[h]cc] as Utils::image_scale_keeping_ratio().
* glom/utility_widgets/canvas/canvas_image_movable.cc:
* glom/utility_widgets/db_adddel/db_adddel.cc:
* glom/utility_widgets/flowtable_dnd.cc: Adapted.
2010-04-08 Murray Cumming <murrayc@murrayc.com>
 
glom/mode_data/datawidget/: Clean up classes, putting them in a namespace.
......
......@@ -24,7 +24,7 @@
#include "canvas_image_movable.h"
#include <goocanvasmm/canvas.h>
#include <gtkmm/stock.h>
#include <glom/utility_widgets/imageglom.h> //For ImageGlom::scale_keeping_ratio().
#include <glom/utils_ui.h> //For Utils::image_scale_keeping_ratio().
#include <iostream>
namespace Glom
......@@ -205,7 +205,7 @@ void CanvasImageMovable::scale_to_size()
if(width && height)
{
Glib::RefPtr<Gdk::Pixbuf> pixbuf = ImageGlom::scale_keeping_ratio(m_pixbuf, (int)height, (int)width);
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Utils::image_scale_keeping_ratio(m_pixbuf, (int)height, (int)width);
#ifdef GLIBMM_PROPERTIES_ENABLED
property_pixbuf() = pixbuf;
#else
......
......@@ -29,7 +29,7 @@
#include <glom/utils_ui.h>
#include "cellrenderer_buttonimage.h"
#include "cellrenderer_buttontext.h"
#include <glom/utility_widgets/imageglom.h> //For ImageGlom::scale_keeping_ratio().
#include <glom/utils_ui.h> //For Utils::image_scale_keeping_ratio().
#include <iostream> //For debug output.
#include <gtk/gtktreeview.h>
......@@ -2299,7 +2299,7 @@ void DbAddDel::treeviewcolumn_on_cell_data(Gtk::CellRenderer* renderer, const Gt
//Scale it down to a sensible size.
if(pixbuf)
pixbuf = ImageGlom::scale_keeping_ratio(pixbuf, get_fixed_cell_height(), pixbuf->get_width());
pixbuf = Utils::image_scale_keeping_ratio(pixbuf, get_fixed_cell_height(), pixbuf->get_width());
g_object_set(pDerived->gobj(), "pixbuf", pixbuf ? pixbuf->gobj() : 0, (gpointer)0);
}
......
......@@ -24,7 +24,6 @@
#include <glom/mode_data/placeholder-glom.h>
#include "layouttoolbarbutton.h"
#include <glom/mode_data/buttonglom.h>
#include "imageglom.h"
namespace Glom
{
......
......@@ -310,10 +310,10 @@ void ImageGlom::scale()
{
if(allocation.get_height() > 10 || allocation.get_width() > 10)
{
Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = scale_keeping_ratio(pixbuf, allocation.get_height(), allocation.get_width());
Glib::RefPtr<Gdk::Pixbuf> pixbuf_scaled = Utils::image_scale_keeping_ratio(pixbuf, allocation.get_height(), allocation.get_width());
if(!pixbuf_scaled)
{
std::cerr << "ImageGlom::scale(): scale_keeping_ratio() returned NULL pixbuf." << std::endl;
std::cerr << "Utils::image_scale_keeping_ratio() returned NULL pixbuf." << std::endl;
}
else
{
......@@ -333,70 +333,6 @@ void ImageGlom::scale()
// g_warning("ImageGlom::scale(): attempt to scale a null pixbuf.");
}
//static:
Glib::RefPtr<Gdk::Pixbuf> ImageGlom::scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf, int target_height, int target_width)
{
if( (target_height == 0) || (target_width == 0) )
return Glib::RefPtr<Gdk::Pixbuf>(); //This shouldn't happen anyway.
if(!pixbuf)
return pixbuf;
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)
{
const float ratio = (float)target_height / (float)pixbuf_height;
target_width = (int)((float)pixbuf_width * ratio);
}
else if(scale_mode == SCALE_WIDTH)
{
const float ratio = (float)target_width / (float) pixbuf_width;
target_height = (int)((float)pixbuf_height * ratio);
}
if( (target_height == 0) || (target_width == 0) )
{
return Glib::RefPtr<Gdk::Pixbuf>(); //This shouldn't happen anyway. It seems to happen sometimes though, when ratio is very small.
}
return pixbuf->scale_simple(target_width, target_height, Gdk::INTERP_NEAREST);
}
void ImageGlom::on_menupopup_activate_select_file()
{
if(m_read_only)
......
......@@ -56,8 +56,6 @@ public:
void set_read_only(bool read_only = true);
static Glib::RefPtr<Gdk::Pixbuf> scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf, int target_height, int target_width);
private:
void init();
......
......@@ -283,11 +283,6 @@ Glib::RefPtr<Gdk::Pixbuf> Utils::get_pixbuf_for_gda_value(const Gnome::Gda::Valu
#ifdef GLIBMM_EXCEPTIONS_ENABLED
try
{
//g_warning("ImageGlom::set_value(): debug: from db: ");
//for(int i = 0; i < 10; ++i)
// g_warning("%02X (%c), ", (guint8)puiData[i], (char)puiData[i]);
refPixbufLoader->write(puiData, (glong)buffer_binary_length);
result = refPixbufLoader->get_pixbuf();
......@@ -438,4 +433,69 @@ std::string Utils::get_filepath_with_extension(const std::string& filepath, cons
}
//static:
Glib::RefPtr<Gdk::Pixbuf> Utils::image_scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf, int target_height, int target_width)
{
if( (target_height == 0) || (target_width == 0) )
return Glib::RefPtr<Gdk::Pixbuf>(); //This shouldn't happen anyway.
if(!pixbuf)
return pixbuf;
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)
{
const float ratio = (float)target_height / (float)pixbuf_height;
target_width = (int)((float)pixbuf_width * ratio);
}
else if(scale_mode == SCALE_WIDTH)
{
const float ratio = (float)target_width / (float) pixbuf_width;
target_height = (int)((float)pixbuf_height * ratio);
}
if( (target_height == 0) || (target_width == 0) )
{
return Glib::RefPtr<Gdk::Pixbuf>(); //This shouldn't happen anyway. It seems to happen sometimes though, when ratio is very small.
}
return pixbuf->scale_simple(target_width, target_height, Gdk::INTERP_NEAREST);
}
} //namespace Glom
......@@ -85,6 +85,8 @@ int get_suitable_field_width_for_widget(Gtk::Widget& widget, const sharedptr<con
/// Add the @a extension if no extension is there already:
std::string get_filepath_with_extension(const std::string& filepath, const std::string& extension);
Glib::RefPtr<Gdk::Pixbuf> image_scale_keeping_ratio(const Glib::RefPtr<Gdk::Pixbuf>& pixbuf, int target_height, int target_width);
} //namespace Utils
} //namespace Glom
......
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