2003-06-03 17:49:09 +00:00
|
|
|
/**
|
2007-04-25 01:24:38 +00:00
|
|
|
* \file RenderGraphic.cpp
|
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>
|
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "RenderGraphic.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
#include "insets/Inset.h"
|
2003-09-06 19:16:30 +00:00
|
|
|
|
2003-06-03 17:49:09 +00:00
|
|
|
#include "gettext.h"
|
2007-04-26 17:34:20 +00:00
|
|
|
#include "Color.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "LyX.h"
|
|
|
|
#include "LyXRC.h"
|
|
|
|
#include "MetricsInfo.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
#include "frontends/FontMetrics.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
|
|
|
#include "graphics/GraphicsImage.h"
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2004-04-13 17:25:23 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
using 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
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
RenderGraphic::RenderGraphic(Inset const * inset)
|
2004-04-13 17:25:23 +00:00
|
|
|
{
|
|
|
|
loader_.connect(boost::bind(&LyX::updateInset,
|
|
|
|
boost::cref(LyX::cref()), inset));
|
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
|
2004-04-13 17:25:23 +00:00
|
|
|
RenderGraphic::RenderGraphic(RenderGraphic const & other,
|
2007-04-29 13:39:47 +00:00
|
|
|
Inset const * inset)
|
2003-10-10 21:08:55 +00:00
|
|
|
: RenderBase(other),
|
2003-06-12 08:52:36 +00:00
|
|
|
loader_(other.loader_),
|
2004-04-16 16:01:47 +00:00
|
|
|
params_(other.params_)
|
2004-04-13 17:25:23 +00:00
|
|
|
{
|
|
|
|
loader_.connect(boost::bind(&LyX::updateInset,
|
|
|
|
boost::cref(LyX::cref()), inset));
|
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
auto_ptr<RenderBase> RenderGraphic::clone(Inset const * inset) const
|
2003-06-12 08:52:36 +00:00
|
|
|
{
|
2004-04-13 17:25:23 +00:00
|
|
|
return auto_ptr<RenderBase>(new RenderGraphic(*this, inset));
|
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()) {
|
|
|
|
loader_.reset(params_.filename, params_);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 11:12:31 +00:00
|
|
|
namespace {
|
2003-11-03 17:47:28 +00:00
|
|
|
|
2004-04-05 11:59:14 +00:00
|
|
|
bool displayGraphic(graphics::Params const & params)
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2004-04-05 11:59:14 +00:00
|
|
|
return params.display != graphics::NoDisplay &&
|
|
|
|
lyxrc.display_graphics != graphics::NoDisplay;
|
|
|
|
}
|
2004-04-20 08:51:15 +00:00
|
|
|
|
2004-04-05 11:59:14 +00:00
|
|
|
|
2006-11-11 20:55:40 +00:00
|
|
|
docstring const statusMessage(graphics::Params const & params,
|
2004-04-05 11:59:14 +00:00
|
|
|
graphics::ImageStatus status)
|
|
|
|
{
|
2006-09-09 15:27:44 +00:00
|
|
|
docstring ret;
|
|
|
|
|
2004-04-05 11:59:14 +00:00
|
|
|
if (!displayGraphic(params))
|
2006-09-09 15:27:44 +00:00
|
|
|
ret = _("Not shown.");
|
|
|
|
else {
|
|
|
|
switch (status) {
|
|
|
|
case graphics::WaitingToLoad:
|
|
|
|
ret = _("Not shown.");
|
|
|
|
break;
|
|
|
|
case graphics::Loading:
|
|
|
|
ret = _("Loading...");
|
|
|
|
break;
|
|
|
|
case graphics::Converting:
|
|
|
|
ret = _("Converting to loadable format...");
|
|
|
|
break;
|
|
|
|
case graphics::Loaded:
|
|
|
|
ret = _("Loaded into memory. Generating pixmap...");
|
|
|
|
break;
|
|
|
|
case graphics::ScalingEtc:
|
|
|
|
ret = _("Scaling etc...");
|
|
|
|
break;
|
|
|
|
case graphics::Ready:
|
|
|
|
ret = _("Ready to display");
|
|
|
|
break;
|
|
|
|
case graphics::ErrorNoFile:
|
|
|
|
ret = _("No file found!");
|
|
|
|
break;
|
|
|
|
case graphics::ErrorConverting:
|
|
|
|
ret = _("Error converting to loadable format");
|
|
|
|
break;
|
|
|
|
case graphics::ErrorLoading:
|
|
|
|
ret = _("Error loading file into memory");
|
|
|
|
break;
|
|
|
|
case graphics::ErrorGeneratingPixmap:
|
|
|
|
ret = _("Error generating the pixmap");
|
|
|
|
break;
|
|
|
|
case graphics::ErrorUnknown:
|
|
|
|
ret = _("No image");
|
|
|
|
break;
|
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
2006-09-09 15:27:44 +00:00
|
|
|
|
2006-11-11 20:55:40 +00:00
|
|
|
return ret;
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2006-11-28 15:15:49 +00:00
|
|
|
bool RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2004-04-05 11:59:14 +00:00
|
|
|
bool image_ready = displayGraphic(params_) && 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() +
|
2007-04-29 13:39:47 +00:00
|
|
|
2 * Inset::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;
|
|
|
|
|
2007-04-29 18:17:15 +00:00
|
|
|
Font msgFont(mi.base.font);
|
|
|
|
msgFont.setFamily(Font::SANS_FAMILY);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2006-10-17 14:46:45 +00:00
|
|
|
// FIXME UNICODE
|
|
|
|
docstring const justname =
|
2006-11-26 21:30:39 +00:00
|
|
|
from_utf8(onlyFilename(params_.filename.absFilename()));
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!justname.empty()) {
|
2007-04-29 18:17:15 +00:00
|
|
|
msgFont.setSize(Font::SIZE_FOOTNOTE);
|
2006-10-11 17:24:46 +00:00
|
|
|
font_width = theFontMetrics(msgFont)
|
2006-10-17 14:46:45 +00:00
|
|
|
.width(justname);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2006-11-11 20:55:40 +00:00
|
|
|
docstring const msg = statusMessage(params_, loader_.status());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!msg.empty()) {
|
2007-04-29 18:17:15 +00:00
|
|
|
msgFont.setSize(Font::SIZE_TINY);
|
2006-10-11 17:24:46 +00:00
|
|
|
font_width = std::max(font_width,
|
2006-10-17 14:46:45 +00:00
|
|
|
theFontMetrics(msgFont).width(msg));
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dim.wid = std::max(50, font_width + 15);
|
|
|
|
}
|
|
|
|
|
2006-11-28 15:15:49 +00:00
|
|
|
bool const changed = dim_ != dim;
|
2003-06-03 17:49:09 +00:00
|
|
|
dim_ = dim;
|
2006-11-28 15:15:49 +00:00
|
|
|
return changed;
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2004-04-05 11:59:14 +00:00
|
|
|
if (displayGraphic(params_)) {
|
|
|
|
if (loader_.status() == graphics::WaitingToLoad)
|
|
|
|
loader_.startLoading();
|
|
|
|
if (!loader_.monitoring())
|
|
|
|
loader_.startMonitoring();
|
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2003-11-25 16:49:02 +00:00
|
|
|
// This will draw the graphics. If the graphics has not been
|
|
|
|
// loaded yet, we draw just a rectangle.
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2004-04-05 11:59:14 +00:00
|
|
|
if (displayGraphic(params_) && readyToDisplay(loader_)) {
|
2007-04-29 13:39:47 +00:00
|
|
|
pi.pain.image(x + Inset::TEXT_TO_INSET_OFFSET,
|
2003-06-03 17:49:09 +00:00
|
|
|
y - dim_.asc,
|
2007-04-29 13:39:47 +00:00
|
|
|
dim_.wid - 2 * Inset::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 {
|
2007-04-29 13:39:47 +00:00
|
|
|
pi.pain.rectangle(x + Inset::TEXT_TO_INSET_OFFSET,
|
2003-06-03 17:49:09 +00:00
|
|
|
y - dim_.asc,
|
2007-04-29 13:39:47 +00:00
|
|
|
dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET,
|
2003-09-15 10:08:01 +00:00
|
|
|
dim_.asc + dim_.des,
|
2007-04-26 17:34:20 +00:00
|
|
|
Color::foreground);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
// Print the file name.
|
2007-04-29 18:17:15 +00:00
|
|
|
Font msgFont = pi.base.font;
|
|
|
|
msgFont.setFamily(Font::SANS_FAMILY);
|
2006-11-26 21:30:39 +00:00
|
|
|
string const justname = onlyFilename(params_.filename.absFilename());
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
if (!justname.empty()) {
|
2007-04-29 18:17:15 +00:00
|
|
|
msgFont.setSize(Font::SIZE_FOOTNOTE);
|
2007-04-29 13:39:47 +00:00
|
|
|
pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
|
2006-10-11 17:24:46 +00:00
|
|
|
y - theFontMetrics(msgFont).maxAscent() - 4,
|
2006-11-11 20:55:40 +00:00
|
|
|
from_utf8(justname), msgFont);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the message.
|
2006-11-11 20:55:40 +00:00
|
|
|
docstring const msg = statusMessage(params_, loader_.status());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!msg.empty()) {
|
2007-04-29 18:17:15 +00:00
|
|
|
msgFont.setSize(Font::SIZE_TINY);
|
2007-04-29 13:39:47 +00:00
|
|
|
pi.pain.text(x + Inset::TEXT_TO_INSET_OFFSET + 6,
|
2006-11-11 20:55:40 +00:00
|
|
|
y - 4, msg, msgFont);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|