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
|
|
|
|
2008-01-08 16:22:05 +00:00
|
|
|
#include "support/FileName.h"
|
|
|
|
#include "support/filetools.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.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>
|
|
|
|
|
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)
|
2004-04-13 17:25:23 +00:00
|
|
|
{
|
2007-11-17 15:32:12 +00:00
|
|
|
loader_.connect(boost::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
|
|
|
{
|
2007-11-17 15:32:12 +00:00
|
|
|
loader_.connect(boost::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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
2007-09-21 20:39:47 +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
|
|
|
|
2007-11-23 22:15:17 +00:00
|
|
|
dim.asc = image_ready ? loader_.image()->height() : 50;
|
2003-06-03 17:49:09 +00:00
|
|
|
dim.des = 0;
|
|
|
|
|
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;
|
2003-06-12 08:52:36 +00:00
|
|
|
} else {
|
2003-06-03 17:49:09 +00:00
|
|
|
int font_width = 0;
|
|
|
|
|
2007-10-28 18:51:54 +00:00
|
|
|
FontInfo msgFont(mi.base.font);
|
|
|
|
msgFont.setFamily(SANS_FAMILY);
|
2003-06-03 17:49:09 +00:00
|
|
|
|
2006-10-17 14:46:45 +00:00
|
|
|
// FIXME UNICODE
|
2007-11-17 15:32:12 +00:00
|
|
|
docstring const justname = from_utf8(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);
|
2007-11-17 15:32:12 +00:00
|
|
|
font_width = theFontMetrics(msgFont).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-10-28 18:51:54 +00:00
|
|
|
msgFont.setSize(FONT_SIZE_TINY);
|
2007-12-12 19:28:07 +00:00
|
|
|
font_width = max(font_width,
|
2006-10-17 14:46:45 +00:00
|
|
|
theFontMetrics(msgFont).width(msg));
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
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
|
|
|
{
|
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-10-25 12:41:02 +00:00
|
|
|
Color_foreground);
|
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;
|
|
|
|
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);
|
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-10-28 18:51:54 +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
|