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"
|
2004-04-13 17:25:23 +00:00
|
|
|
#include "lyx_main.h"
|
2004-04-05 11:59:14 +00:00
|
|
|
#include "lyxrc.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
#include "metricsinfo.h"
|
|
|
|
|
2006-10-07 16:15:06 +00:00
|
|
|
#include "frontends/Application.h"
|
|
|
|
#include "frontends/FontLoader.h"
|
|
|
|
#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-22 11:12:31 +00:00
|
|
|
namespace graphics = lyx::graphics;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
using lyx::docstring;
|
2006-04-08 22:31:11 +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
|
|
|
|
2004-04-13 17:25:23 +00:00
|
|
|
RenderGraphic::RenderGraphic(InsetBase const * inset)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
InsetBase 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
|
|
|
|
|
|
|
|
2004-04-13 17:25:23 +00:00
|
|
|
auto_ptr<RenderBase> RenderGraphic::clone(InsetBase 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()) {
|
2006-04-08 22:31:11 +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-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
|
|
|
|
|
|
|
string const statusMessage(graphics::Params const & params,
|
|
|
|
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
|
|
|
|
|
|
|
// FIXME UNICODE
|
|
|
|
return lyx::to_utf8(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
|
|
|
|
2003-10-10 21:08:55 +00:00
|
|
|
void 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() +
|
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);
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
string const justname = onlyFilename(params_.filename);
|
2006-09-09 15:27:44 +00:00
|
|
|
docstring djust(justname.begin(), justname.end());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!justname.empty()) {
|
|
|
|
msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
|
2006-10-07 16:15:06 +00:00
|
|
|
font_width = theApp->fontLoader().metrics(msgFont)
|
|
|
|
.width(djust);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2004-04-05 11:59:14 +00:00
|
|
|
string const msg = statusMessage(params_, loader_.status());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!msg.empty()) {
|
2006-09-09 15:27:44 +00:00
|
|
|
docstring dmsg(msg.begin(), msg.end());
|
2003-06-03 17:49:09 +00:00
|
|
|
msgFont.setSize(LyXFont::SIZE_TINY);
|
2006-10-07 16:15:06 +00:00
|
|
|
font_width = std::max(font_width, theApp->fontLoader()
|
|
|
|
.metrics(msgFont).width(dmsg));
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
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_)) {
|
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);
|
2006-04-08 22:31:11 +00:00
|
|
|
string const justname = onlyFilename(params_.filename);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
if (!justname.empty()) {
|
2006-09-09 15:27:44 +00:00
|
|
|
docstring djust(justname.begin(), justname.end());
|
2003-06-03 17:49:09 +00:00
|
|
|
msgFont.setSize(LyXFont::SIZE_FOOTNOTE);
|
2003-07-25 21:20:24 +00:00
|
|
|
pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
|
2006-10-07 16:15:06 +00:00
|
|
|
y - theApp->fontLoader().metrics(msgFont).maxAscent() - 4,
|
2006-08-13 22:54:59 +00:00
|
|
|
djust, msgFont);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print the message.
|
2004-04-05 11:59:14 +00:00
|
|
|
string const msg = statusMessage(params_, loader_.status());
|
2003-06-03 17:49:09 +00:00
|
|
|
if (!msg.empty()) {
|
2006-09-09 15:27:44 +00:00
|
|
|
docstring dmsg(msg.begin(), msg.end());
|
2003-06-03 17:49:09 +00:00
|
|
|
msgFont.setSize(LyXFont::SIZE_TINY);
|
2003-07-25 21:20:24 +00:00
|
|
|
pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,
|
2006-08-13 22:54:59 +00:00
|
|
|
y - 4, dmsg, msgFont);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|