#include "insets/RenderPreview.h"
#include "insets/Inset.h"
+#include "Buffer.h"
#include "BufferView.h"
#include "Dimension.h"
-#include "gettext.h"
-#include "Color.h"
#include "LyX.h"
#include "LyXRC.h"
#include "MetricsInfo.h"
#include "graphics/PreviewImage.h"
#include "graphics/PreviewLoader.h"
-#include "graphics/Previews.h"
#include "support/FileName.h"
+#include "support/gettext.h"
+#include "support/lassert.h"
#include "support/lstrings.h"
-#include <boost/bind.hpp>
-
+using namespace std;
+using namespace lyx::support;
namespace lyx {
-using support::FileName;
-using std::string;
-using std::auto_ptr;
+bool RenderPreview::previewText()
+{
+ // Use a switch to trigger a warning if the enum is changed.
+ switch(lyxrc.preview) {
+ case LyXRC::PREVIEW_ON:
+ case LyXRC::PREVIEW_NO_MATH:
+ return true;
+ case LyXRC::PREVIEW_OFF:
+ break;
+ }
+ return false;
+}
-LyXRC_PreviewStatus RenderPreview::status()
+
+bool RenderPreview::previewMath()
{
- return graphics::Previews::status();
+ // Use a switch to trigger a warning if the enum is changed.
+ switch(lyxrc.preview) {
+ case LyXRC::PREVIEW_ON:
+ return true;
+ case LyXRC::PREVIEW_NO_MATH:
+ case LyXRC::PREVIEW_OFF:
+ break;
+ }
+
+ return false;
}
-RenderPreview::RenderPreview(InsetBase const * inset)
+RenderPreview::RenderPreview(Inset const * inset)
: parent_(inset)
{}
RenderPreview::RenderPreview(RenderPreview const & other,
- InsetBase const * inset)
+ Inset const * inset)
: RenderBase(other),
- boost::signals::trackable(),
snippet_(other.snippet_),
parent_(inset)
{}
-auto_ptr<RenderBase> RenderPreview::clone(InsetBase const * inset) const
+RenderBase * RenderPreview::clone(Inset const * inset) const
{
- return auto_ptr<RenderBase>(new RenderPreview(*this, inset));
+ return new RenderPreview(*this, inset);
}
namespace {
-graphics::PreviewLoader & getPreviewLoader(Buffer const & buffer)
-{
- graphics::Previews const & previews = graphics::Previews::get();
- return previews.loader(buffer);
-}
-
-
docstring const statusMessage(BufferView const * bv, string const & snippet)
{
- BOOST_ASSERT(bv && bv->buffer());
+ LASSERT(bv, return docstring());
- Buffer const & buffer = *bv->buffer();
- graphics::PreviewLoader const & loader = getPreviewLoader(buffer);
- graphics::PreviewLoader::Status const status = loader.status(snippet);
+ Buffer const & buffer = bv->buffer();
+ graphics::PreviewLoader const * loader = buffer.loader();
+ // please coverity (probably worth the check anyway)
+ if (!loader)
+ return docstring();
+ graphics::PreviewLoader::Status const status = loader->status(snippet);
docstring message;
switch (status) {
return message;
}
-} // namespace anon
+} // namespace
graphics::PreviewImage const *
RenderPreview::getPreviewImage(Buffer const & buffer) const
{
- graphics::PreviewLoader const & loader = getPreviewLoader(buffer);
- return loader.preview(snippet_);
+ graphics::PreviewLoader const * loader = buffer.loader();
+ LASSERT(loader, return 0);
+ return loader->preview(snippet_);
}
-bool RenderPreview::metrics(MetricsInfo & mi, Dimension & dim) const
+void RenderPreview::metrics(MetricsInfo & mi, Dimension & dim) const
{
- BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer());
+ LBUFERR(mi.base.bv);
graphics::PreviewImage const * const pimage =
- getPreviewImage(*mi.base.bv->buffer());
+ getPreviewImage(mi.base.bv->buffer());
if (pimage) {
- dim.asc = pimage->ascent();
- dim.des = pimage->descent();
- dim.wid = pimage->width();
+ // If prepared, load the picture before dim calculation. See bug #5627.
+ pimage->image();
+ dim = pimage->dim();
} else {
dim.asc = 50;
dim.des = 0;
- LyXFont font(mi.base.font);
- font.setFamily(LyXFont::SANS_FAMILY);
- font.setSize(LyXFont::SIZE_FOOTNOTE);
+ FontInfo font(mi.base.font);
+ font.setFamily(SANS_FAMILY);
+ font.setSize(FONT_SIZE_FOOTNOTE);
docstring const stat = statusMessage(mi.base.bv, snippet_);
dim.wid = 15 + theFontMetrics(font).width(stat);
}
- bool const changed = dim_ != dim;
dim_ = dim;
- return changed;
}
void RenderPreview::draw(PainterInfo & pi, int x, int y) const
{
- BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
+ LBUFERR(pi.base.bv);
graphics::PreviewImage const * const pimage =
- getPreviewImage(*pi.base.bv->buffer());
+ getPreviewImage(pi.base.bv->buffer());
graphics::Image const * const image = pimage ? pimage->image() : 0;
if (image) {
pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
*image);
-
} else {
- int const offset = InsetOld::TEXT_TO_INSET_OFFSET;
+ int const offset = Inset::TEXT_TO_INSET_OFFSET;
pi.pain.rectangle(x + offset,
y - dim_.asc,
dim_.wid - 2 * offset,
dim_.asc + dim_.des,
- Color::foreground);
+ Color_foreground);
- LyXFont font(pi.base.font);
- font.setFamily(LyXFont::SANS_FAMILY);
- font.setSize(LyXFont::SIZE_FOOTNOTE);
+ FontInfo font(pi.base.font);
+ font.setFamily(SANS_FAMILY);
+ font.setSize(FONT_SIZE_FOOTNOTE);
docstring const stat = statusMessage(pi.base.bv, snippet_);
pi.pain.text(x + offset + 6,
y - theFontMetrics(font).maxAscent() - 4,
stat, font);
}
+ pi.change_.paintCue(pi, x, y - dim_.asc,
+ x + dim_.width(), y - dim_.asc + dim_.height());
}
-void RenderPreview::startLoading(Buffer const & buffer) const
+void RenderPreview::startLoading(Buffer const & buffer, bool forexport) const
{
- if (status() == LyXRC::PREVIEW_OFF || snippet_.empty())
+ if (!forexport && (lyxrc.preview == LyXRC::PREVIEW_OFF || snippet_.empty()))
return;
- graphics::PreviewLoader const & loader = getPreviewLoader(buffer);
- loader.startLoading();
+ graphics::PreviewLoader * loader = buffer.loader();
+ LASSERT(loader, return);
+ loader->startLoading(forexport);
}
void RenderPreview::addPreview(docstring const & latex_snippet,
- Buffer const & buffer)
+ Buffer const & buffer,
+ bool ignore_lyxrc)
{
- if (status() == LyXRC::PREVIEW_OFF)
+ if (lyxrc.preview == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
return;
- graphics::PreviewLoader & loader = getPreviewLoader(buffer);
- addPreview(latex_snippet, loader);
+ graphics::PreviewLoader * loader = buffer.loader();
+ LASSERT(loader, return);
+ addPreview(latex_snippet, *loader, ignore_lyxrc);
}
void RenderPreview::addPreview(docstring const & latex_snippet,
- graphics::PreviewLoader & ploader)
+ graphics::PreviewLoader & ploader,
+ bool ignore_lyxrc)
{
- if (status() == LyXRC::PREVIEW_OFF)
+ if (lyxrc.preview == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
return;
// FIXME UNICODE
// If this is the first time of calling, connect to the
// PreviewLoader signal that'll inform us when the preview image
// is ready for loading.
- if (!ploader_connection_.connected()) {
- ploader_connection_ = ploader.connect(
- boost::bind(&RenderPreview::imageReady, this, _1));
- }
+ if (!ploader_connection_.connected())
+ // This is a scoped connection.
+ ploader_connection_ =
+ ploader.connect([this](graphics::PreviewImage const & pi){
+ imageReady(pi);
+ });
ploader.add(snippet_);
}
if (snippet_.empty())
return;
- graphics::PreviewLoader & loader = getPreviewLoader(buffer);
- loader.remove(snippet_);
+ graphics::PreviewLoader * loader = buffer.loader();
+ LASSERT(loader, return);
+ loader->remove(snippet_);
snippet_.erase();
}
{
// Check the current snippet is the same as that previewed.
if (snippet_ == pimage.snippet())
- LyX::cref().updateInset(parent_);
+ parent_->updateFrontend();
}
-RenderMonitoredPreview::RenderMonitoredPreview(InsetBase const * inset)
- : RenderPreview(inset),
- monitor_(FileName(), 2000)
-{}
+RenderMonitoredPreview::RenderMonitoredPreview(Inset const * inset)
+ : RenderPreview(inset)
+{
+ setAbsFile(FileName());
+}
void RenderMonitoredPreview::setAbsFile(FileName const & file)
{
- monitor_.reset(file);
+ bool mon = monitoring();
+ if (mon)
+ stopMonitoring();
+ filename_ = file;
+ if (mon)
+ startMonitoring();
}
void RenderMonitoredPreview::draw(PainterInfo & pi, int x, int y) const
{
RenderPreview::draw(pi, x, y);
- if (!monitoring())
- startMonitoring();
+ startMonitoring();
+ monitor_->checkModifiedAsync();
+}
+
+
+signals2::connection RenderMonitoredPreview::connect(slot const & slot)
+{
+ return changed_.connect(slot);
+}
+
+
+bool RenderMonitoredPreview::monitoring() const
+{
+ return (bool) monitor_;
}
-boost::signals::connection
-RenderMonitoredPreview::fileChanged(slot_type const & slot)
+void RenderMonitoredPreview::startMonitoring() const
{
- return monitor_.connect(slot);
+ if (!monitoring()) {
+ monitor_ = FileSystemWatcher::activeMonitor(filename_);
+ // Disconnected at the same time as this is destroyed.
+ monitor_->connect([this](bool /* exists */){ changed_(); });
+ }
}
+
+void RenderMonitoredPreview::stopMonitoring() const
+{
+ monitor_ = nullptr;
+}
+
+
+
+
} // namespace lyx