2003-06-03 17:49:09 +00:00
|
|
|
/**
|
2003-06-12 08:52:36 +00:00
|
|
|
* \file renderers.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-06-12 08:52:36 +00:00
|
|
|
#include "insets/renderers.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 "BufferView.h"
|
|
|
|
#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/LyXView.h"
|
|
|
|
#include "frontends/Painter.h"
|
|
|
|
|
|
|
|
#include "graphics/GraphicsImage.h"
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
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-07-04 08:23:23 +00:00
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
RenderInset::RenderInset()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
RenderInset::RenderInset(RenderInset const &)
|
|
|
|
{
|
|
|
|
// Cached variables are not copied
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RenderInset::~RenderInset()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
RenderInset & RenderInset::operator=(RenderInset const &)
|
|
|
|
{
|
|
|
|
// Cached variables are not copied
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BufferView * RenderInset::view() const
|
|
|
|
{
|
|
|
|
return view_.lock().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ButtonRenderer::ButtonRenderer()
|
|
|
|
: editable_(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
RenderInset * ButtonRenderer::clone() const
|
|
|
|
{
|
|
|
|
return new ButtonRenderer(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonRenderer::update(string const & text, bool editable)
|
|
|
|
{
|
|
|
|
text_ = text;
|
|
|
|
editable_ = editable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(mi.base.bv);
|
2003-06-12 08:52:36 +00:00
|
|
|
|
|
|
|
LyXFont font(LyXFont::ALL_SANE);
|
|
|
|
font.decSize();
|
|
|
|
|
|
|
|
if (editable_)
|
|
|
|
font_metrics::buttonText(text_, font, dim.wid, dim.asc, dim.des);
|
|
|
|
else
|
|
|
|
font_metrics::rectText(text_, font, dim.wid, dim.asc, dim.des);
|
|
|
|
|
|
|
|
dim.wid += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ButtonRenderer::draw(PainterInfo & pi, int x, int y) const
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(pi.base.bv);
|
2003-06-12 08:52:36 +00:00
|
|
|
view_ = pi.base.bv->owner()->view();
|
|
|
|
|
|
|
|
// Draw it as a box with the LaTeX text
|
|
|
|
LyXFont font(LyXFont::ALL_SANE);
|
2003-09-16 09:01:15 +00:00
|
|
|
font.setColor(LColor::command);
|
|
|
|
font.decSize();
|
2003-06-12 08:52:36 +00:00
|
|
|
|
|
|
|
if (editable_) {
|
|
|
|
pi.pain.buttonText(x + 2, y, text_, font);
|
|
|
|
} else {
|
|
|
|
pi.pain.rectText(x + 2, y, text_, font,
|
|
|
|
LColor::commandbg, LColor::commandframe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GraphicRenderer::GraphicRenderer()
|
2003-06-04 09:16:29 +00:00
|
|
|
: checksum_(0)
|
2003-06-03 17:49:09 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
GraphicRenderer::GraphicRenderer(GraphicRenderer const & other)
|
|
|
|
: RenderInset(other),
|
|
|
|
loader_(other.loader_),
|
2003-06-03 17:49:09 +00:00
|
|
|
params_(other.params_),
|
|
|
|
checksum_(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
RenderInset * GraphicRenderer::clone() const
|
|
|
|
{
|
|
|
|
return new GraphicRenderer(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-21 21:30:57 +00:00
|
|
|
void GraphicRenderer::update(lyx::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-06-12 08:52:36 +00:00
|
|
|
bool GraphicRenderer::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-06-12 08:52:36 +00:00
|
|
|
boost::signals::connection GraphicRenderer::connect(slot_type const & slot) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
|
|
|
return loader_.connect(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
string const GraphicRenderer::statusMessage() const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
|
|
|
switch (loader_.status()) {
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::WaitingToLoad:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Not shown.");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::Loading:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Loading...");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::Converting:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Converting to loadable format...");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::Loaded:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Loaded into memory. Must now generate pixmap.");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::ScalingEtc:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Scaling etc...");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::Ready:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Ready to display");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::ErrorNoFile:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("No file found!");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::ErrorConverting:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Error converting to loadable format");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::ErrorLoading:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Error loading file into memory");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::ErrorGeneratingPixmap:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("Error generating the pixmap");
|
2003-07-21 21:30:57 +00:00
|
|
|
case lyx::graphics::ErrorUnknown:
|
2003-06-03 17:49:09 +00:00
|
|
|
return _("No image");
|
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
bool GraphicRenderer::readyToDisplay() const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-07-21 21:30:57 +00:00
|
|
|
if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
|
2003-06-12 08:52:36 +00:00
|
|
|
return false;
|
|
|
|
return loader_.image()->isDrawable();
|
2003-06-03 17:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-12 08:52:36 +00:00
|
|
|
void GraphicRenderer::metrics(MetricsInfo & mi, Dimension & dim) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-06-12 08:52:36 +00:00
|
|
|
bool image_ready = readyToDisplay();
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
string const msg = statusMessage();
|
|
|
|
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-06-12 08:52:36 +00:00
|
|
|
void GraphicRenderer::draw(PainterInfo & pi, int x, int y) const
|
2003-06-03 17:49:09 +00:00
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(pi.base.bv);
|
2003-06-12 08:52:36 +00:00
|
|
|
view_ = pi.base.bv->owner()->view();
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
#if 0
|
2003-06-04 15:14:42 +00:00
|
|
|
// Comment this out and see if anything goes wrong.
|
|
|
|
// The explanation for why it _was_ needed once upon a time is below.
|
|
|
|
|
2003-06-03 17:49:09 +00:00
|
|
|
// MakeAbsPath returns filename_ unchanged if it is absolute
|
|
|
|
// already.
|
|
|
|
string const file_with_path =
|
2003-06-04 09:16:29 +00:00
|
|
|
MakeAbsPath(params_.filename, view_->buffer()->filePath());
|
2003-06-03 17:49:09 +00:00
|
|
|
|
|
|
|
// A 'paste' operation creates a new inset with the correct filepath,
|
|
|
|
// but then the 'old' inset stored in the 'copy' operation is actually
|
|
|
|
// added to the buffer.
|
|
|
|
|
|
|
|
// Thus, pasting a graphic into a new buffer with different
|
|
|
|
// buffer->filePath() will result in the image being displayed in LyX even
|
|
|
|
// though the relative path now points at nothing at all. Subsequent
|
|
|
|
// loading of the file into LyX will therefore fail.
|
|
|
|
|
|
|
|
// We should ensure that the filepath is correct.
|
|
|
|
if (file_with_path != loader_.filename()) {
|
|
|
|
params_.filename = file_with_path;
|
|
|
|
update(params_);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-21 21:30:57 +00:00
|
|
|
if (params_.display != lyx::graphics::NoDisplay &&
|
|
|
|
loader_.status() == lyx::graphics::WaitingToLoad)
|
2003-06-03 17:49:09 +00:00
|
|
|
loader_.startLoading();
|
|
|
|
|
2003-07-21 21:30:57 +00:00
|
|
|
if (params_.display != lyx::graphics::NoDisplay &&
|
|
|
|
!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-06-12 08:52:36 +00:00
|
|
|
if (readyToDisplay()) {
|
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.
|
|
|
|
string const msg = statusMessage();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|