2003-06-03 17:49:09 +00:00
|
|
|
/**
|
2003-10-10 21:08:55 +00:00
|
|
|
* \file render_graphic.C
|
2003-06-03 17:49:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-06-03 17:49:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
#include "render_graphic.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2003-09-06 19:16:30 +00:00
|
|
|
#include "insets/inset.h"
|
|
|
|
|
2003-06-03 17:49:09 +00:00
|
|
|
#include "gettext.h"
|
2003-09-16 09:44:34 +00:00
|
|
|
#include "LColor.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
#include "metricsinfo.h"
|
|
|
|
|
|
|
|
#include "frontends/font_metrics.h"
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
|
|
|
#include "graphics/GraphicsImage.h"
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
namespace graphics = lyx::graphics;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
using lyx::support::AbsolutePath;
|
|
|
|
using lyx::support::OnlyFilename;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-11-03 17:47:28 +00:00
|
|
|
using std::auto_ptr;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
RenderGraphic::RenderGraphic()
|
2003-06-04 09:16:29 +00:00
|
|
|
: checksum_(0)
|
2003-06-03 17:49:09 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
RenderGraphic::RenderGraphic(RenderGraphic const & other)
|
|
|
|
: RenderBase(other),
|
2003-06-12 08:52:36 +00:00
|
|
|
loader_(other.loader_),
|
2003-06-03 17:49:09 +00:00
|
|
|
params_(other.params_),
|
|
|
|
checksum_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
auto_ptr<RenderBase> RenderGraphic::clone() const
|
2003-06-12 08:52:36 +00:00
|
|
|
{
|
2003-11-03 17:47:28 +00:00
|
|
|
return auto_ptr<RenderBase>(new RenderGraphic(*this));
|
2003-06-12 08:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
void RenderGraphic::update(graphics::Params const & params)
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-06-04 09:16:29 +00:00
|
|
|
params_ = params;
|
|
|
|
|
2003-06-04 15:14:42 +00:00
|
|
|
if (!params_.filename.empty()) {
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(AbsolutePath(params_.filename));
|
2003-06-04 15:14:42 +00:00
|
|
|
loader_.reset(params_.filename, params_);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
bool RenderGraphic::hasFileChanged() const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
|
|
|
unsigned long const new_checksum = loader_.checksum();
|
|
|
|
bool const file_has_changed = checksum_ != new_checksum;
|
|
|
|
if (file_has_changed)
|
|
|
|
checksum_ = new_checksum;
|
|
|
|
return file_has_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
|
|
|
return loader_.connect(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
namespace {
|
2003-11-03 17:47:28 +00:00
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
string const statusMessage(graphics::ImageStatus status)
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-10-22 11:12:31 +00:00
|
|
|
switch (status) {
|
|
|
|
case graphics::WaitingToLoad:
|
|
|
|
return _("Not shown.");
|
|
|
|
case graphics::Loading:
|
|
|
|
return _("Loading...");
|
|
|
|
case graphics::Converting:
|
|
|
|
return _("Converting to loadable format...");
|
|
|
|
case graphics::Loaded:
|
|
|
|
return _("Loaded into memory. Must now generate pixmap.");
|
|
|
|
case graphics::ScalingEtc:
|
|
|
|
return _("Scaling etc...");
|
|
|
|
case graphics::Ready:
|
|
|
|
return _("Ready to display");
|
|
|
|
case graphics::ErrorNoFile:
|
|
|
|
return _("No file found!");
|
|
|
|
case graphics::ErrorConverting:
|
|
|
|
return _("Error converting to loadable format");
|
|
|
|
case graphics::ErrorLoading:
|
|
|
|
return _("Error loading file into memory");
|
|
|
|
case graphics::ErrorGeneratingPixmap:
|
|
|
|
return _("Error generating the pixmap");
|
|
|
|
case graphics::ErrorUnknown:
|
|
|
|
return _("No image");
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
bool readyToDisplay(graphics::Loader const & loader)
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-10-22 11:12:31 +00:00
|
|
|
if (!loader.image() || loader.status() != graphics::Ready)
|
2003-06-12 08:52:36 +00:00
|
|
|
return false;
|
2003-10-22 11:12:31 +00:00
|
|
|
return loader.image()->isDrawable();
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-10-22 11:12:31 +00:00
|
|
|
bool image_ready = readyToDisplay(loader_);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
dim.asc = image_ready ? loader_.image()->getHeight() : 50;
|
2003-06-03 17:49:09 +00:00
|
|
|
dim.des = 0;
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
if (image_ready) {
|
2003-06-03 17:49:09 +00:00
|
|
|
dim.wid = loader_.image()->getWidth() +
|
2003-07-25 21:20:24 +00:00
|
|
|
2 * InsetOld::TEXT_TO_INSET_OFFSET;
|
2003-06-12 08:52:36 +00:00
|
|
|
} else {
|
2003-06-03 17:49:09 +00:00
|
|
|
int font_width = 0;
|
|
|
|
|
|
|
|
LyXFont msgFont(mi.base.font);
|
|
|
|
msgFont.setFamily(LyXFont::SANS_FAMILY);
|
|
|
|
|
|
|
|
string const justname = OnlyFilename(params_.filename);
|
|
|
|
if (!justname.empty()) {
|
|
|
|
msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
|
|
|
|
font_width = font_metrics::width(justname, msgFont);
|
|
|
|
}
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
string const msg = statusMessage(loader_.status());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!msg.empty()) {
|
|
|
|
msgFont.setSize(LyXFont::SIZE_TINY);
|
|
|
|
font_width = std::max(font_width,
|
|
|
|
font_metrics::width(msg, msgFont));
|
|
|
|
}
|
|
|
|
|
|
|
|
dim.wid = std::max(50, font_width + 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
dim_ = dim;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-10-22 11:12:31 +00:00
|
|
|
if (params_.display != graphics::NoDisplay &&
|
|
|
|
loader_.status() == graphics::WaitingToLoad)
|
2003-06-03 17:49:09 +00:00
|
|
|
loader_.startLoading();
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
if (params_.display != graphics::NoDisplay &&
|
2003-07-21 21:30:57 +00:00
|
|
|
!loader_.monitoring())
|
2003-06-03 17:49:09 +00:00
|
|
|
loader_.startMonitoring();
|
|
|
|
|
|
|
|
// This will draw the graphics. If the graphics has not been loaded yet,
|
|
|
|
// we draw just a rectangle.
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
if (readyToDisplay(loader_)) {
|
2003-07-25 21:20:24 +00:00
|
|
|
pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
|
2003-06-03 17:49:09 +00:00
|
|
|
y - dim_.asc,
|
2003-07-25 21:20:24 +00:00
|
|
|
dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
|
2003-06-03 17:49:09 +00:00
|
|
|
dim_.asc + dim_.des,
|
|
|
|
*loader_.image());
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
} else {
|
2003-07-25 21:20:24 +00:00
|
|
|
pi.pain.rectangle(x + InsetOld::TEXT_TO_INSET_OFFSET,
|
2003-06-03 17:49:09 +00:00
|
|
|
y - dim_.asc,
|
2003-07-25 21:20:24 +00:00
|
|
|
dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
|
2003-09-15 10:08:01 +00:00
|
|
|
dim_.asc + dim_.des,
|
|
|
|
LColor::foreground);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
// Print the file name.
|
|
|
|
LyXFont msgFont = pi.base.font;
|
|
|
|
msgFont.setFamily(LyXFont::SANS_FAMILY);
|
|
|
|
string const justname = OnlyFilename(params_.filename);
|
|
|
|
|
|
|
|
if (!justname.empty()) {
|
|
|
|
msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
|
2003-07-25 21:20:24 +00:00
|
|
|
pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
|
2003-06-03 17:49:09 +00:00
|
|
|
y - font_metrics::maxAscent(msgFont) - 4,
|
|
|
|
justname, msgFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the message.
|
2003-10-22 11:12:31 +00:00
|
|
|
string const msg = statusMessage(loader_.status());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!msg.empty()) {
|
|
|
|
msgFont.setSize(LyXFont::SIZE_TINY);
|
2003-07-25 21:20:24 +00:00
|
|
|
pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
|
2003-06-03 17:49:09 +00:00
|
|
|
y - 4, msg, msgFont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|