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
|
|
|
|
2016-11-05 00:00:44 +00:00
|
|
|
#include "Buffer.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"
|
|
|
|
|
2008-06-18 18:54:31 +00:00
|
|
|
#include "support/FileName.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
#include "support/filetools.h"
|
2008-06-18 18:54:31 +00:00
|
|
|
#include "support/gettext.h"
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2010-04-22 11:16:58 +00:00
|
|
|
#include "support/bind.h"
|
2004-04-13 17:25:23 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
RenderGraphic::RenderGraphic(Inset const * inset)
|
2016-11-05 00:00:44 +00:00
|
|
|
: loader_(inset->buffer().fileName())
|
2004-04-13 17:25:23 +00:00
|
|
|
{
|
2010-04-22 11:16:58 +00:00
|
|
|
loader_.connect(bind(&Inset::updateFrontend, inset));
|
2004-04-13 17:25:23 +00:00
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
|
2007-11-17 15:32:12 +00:00
|
|
|
RenderGraphic::RenderGraphic(RenderGraphic const & other, Inset const * inset)
|
2008-04-20 03:08:11 +00:00
|
|
|
: RenderBase(other), loader_(other.loader_), params_(other.params_)
|
2004-04-13 17:25:23 +00:00
|
|
|
{
|
2010-04-22 11:16:58 +00:00
|
|
|
loader_.connect(bind(&Inset::updateFrontend, inset));
|
2004-04-13 17:25:23 +00:00
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
|
2007-08-30 18:03:17 +00:00
|
|
|
RenderBase * RenderGraphic::clone(Inset const * inset) const
|
2003-06-12 08:52:36 +00:00
|
|
|
{
|
2007-08-30 18:03:17 +00:00
|
|
|
return new RenderGraphic(*this, inset);
|
2003-06-12 08:52:36 +00:00
|
|
|
}
|
|
|
|
|
2009-08-13 17:21:59 +00:00
|
|
|
void RenderGraphic::reload() const
|
|
|
|
{
|
|
|
|
loader_.reload();
|
|
|
|
}
|
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;
|
|
|
|
|
2007-11-17 15:32:12 +00:00
|
|
|
if (!params_.filename.empty())
|
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
|
|
|
{
|
2008-06-13 07:34:55 +00:00
|
|
|
return params.display && lyxrc.display_graphics;
|
2004-04-05 11:59:14 +00:00
|
|
|
}
|
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
|
|
|
|
2007-09-21 20:39:47 +00:00
|
|
|
void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2008-06-06 12:39:43 +00:00
|
|
|
if (displayGraphic(params_)) {
|
|
|
|
if (loader_.status() == graphics::WaitingToLoad)
|
|
|
|
loader_.startLoading();
|
|
|
|
if (!loader_.monitoring())
|
|
|
|
loader_.startMonitoring();
|
2017-03-19 13:41:54 +00:00
|
|
|
loader_.checkModifiedAsync();
|
2008-06-06 12:39:43 +00:00
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2015-09-12 16:33:17 +00:00
|
|
|
bool const image_ready = displayGraphic(params_) && readyToDisplay(loader_);
|
2003-06-12 08:52:36 +00:00
|
|
|
if (image_ready) {
|
2007-11-23 22:15:17 +00:00
|
|
|
dim.wid = loader_.image()->width() + 2 * Inset::TEXT_TO_INSET_OFFSET;
|
2008-06-06 12:34:27 +00:00
|
|
|
dim.asc = loader_.image()->height();
|
|
|
|
dim_ = dim;
|
|
|
|
return;
|
|
|
|
}
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2015-09-12 16:33:17 +00:00
|
|
|
dim.asc = 50;
|
2008-06-06 12:34:27 +00:00
|
|
|
dim.des = 0;
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2008-06-06 12:34:27 +00:00
|
|
|
int font_width = 0;
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2008-06-06 12:34:27 +00:00
|
|
|
FontInfo msgFont(mi.base.font);
|
|
|
|
msgFont.setFamily(SANS_FAMILY);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2008-06-06 12:34:27 +00:00
|
|
|
// FIXME UNICODE
|
|
|
|
docstring const justname = from_utf8(params_.filename.onlyFileName());
|
|
|
|
if (!justname.empty()) {
|
|
|
|
msgFont.setSize(FONT_SIZE_FOOTNOTE);
|
|
|
|
font_width = theFontMetrics(msgFont).width(justname);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2008-06-06 12:34:27 +00:00
|
|
|
docstring const msg = statusMessage(params_, loader_.status());
|
|
|
|
if (!msg.empty()) {
|
|
|
|
msgFont.setSize(FONT_SIZE_TINY);
|
|
|
|
font_width = max(font_width,
|
|
|
|
theFontMetrics(msgFont).width(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
dim.wid = max(50, font_width + 15);
|
|
|
|
|
2003-06-03 17:49:09 +00:00
|
|
|
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-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.
|
2016-05-23 16:01:48 +00:00
|
|
|
int const x1 = x + Inset::TEXT_TO_INSET_OFFSET;
|
|
|
|
int const y1 = y - dim_.asc;
|
|
|
|
int const w = dim_.wid - 2 * Inset::TEXT_TO_INSET_OFFSET;
|
|
|
|
int const h = dim_.height();
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2016-05-23 16:01:48 +00:00
|
|
|
if (displayGraphic(params_) && readyToDisplay(loader_))
|
|
|
|
pi.pain.image(x1, y1, w, h, *loader_.image());
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2016-05-23 16:01:48 +00:00
|
|
|
else {
|
|
|
|
Color c = pi.change_.changed() ? pi.change_.color() : Color_foreground;
|
|
|
|
pi.pain.rectangle(x1, y1, w, h, c);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
// Print the file name.
|
2007-10-28 18:51:54 +00:00
|
|
|
FontInfo msgFont = pi.base.font;
|
2016-05-23 16:01:48 +00:00
|
|
|
msgFont.setPaintColor(c);
|
2007-10-28 18:51:54 +00:00
|
|
|
msgFont.setFamily(SANS_FAMILY);
|
2007-12-12 19:28:07 +00:00
|
|
|
string const justname = params_.filename.onlyFileName();
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
if (!justname.empty()) {
|
2007-10-28 18:51:54 +00:00
|
|
|
msgFont.setSize(FONT_SIZE_FOOTNOTE);
|
2016-05-23 16:01:48 +00:00
|
|
|
pi.pain.text(x1 + 6, y - theFontMetrics(msgFont).maxAscent() - 4,
|
|
|
|
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-10-28 18:51:54 +00:00
|
|
|
msgFont.setSize(FONT_SIZE_TINY);
|
2016-05-23 16:01:48 +00:00
|
|
|
pi.pain.text(x1 + 6, y - 4, msg, msgFont);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 16:01:48 +00:00
|
|
|
pi.change_.paintCue(pi, x1, y1, x1 + w, y1 + h);
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|