]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetinclude.C
Final touch 'inset display()'; fix 'is a bit silly' bug
[lyx.git] / src / insets / insetinclude.C
index 24e75ddfc7e6fac7831ae1cd77da0c7ac43002f4..5a02886a53564a4e42acb938db38f3edd0bcd55b 100644 (file)
 #include "gettext.h"
 #include "LaTeXFeatures.h"
 #include "latexrunparams.h"
+#include "lyx_main.h"
 #include "lyxlex.h"
 #include "metricsinfo.h"
 
+#include "frontends/LyXView.h"
 #include "frontends/Painter.h"
 
-#include "graphics/PreviewedInset.h"
-#include "graphics/PreviewImage.h"
+#include "graphics/PreviewLoader.h"
+
+#include "insets/render_preview.h"
 
 #include "support/FileInfo.h"
-#include "support/FileMonitor.h"
 #include "support/filetools.h"
 #include "support/lstrings.h" // contains
 #include "support/tostr.h"
 
 #include <boost/bind.hpp>
 
+#include "support/std_ostream.h"
 #include "support/std_sstream.h"
 
 using lyx::support::AddName;
 using lyx::support::ChangeExtension;
 using lyx::support::contains;
 using lyx::support::FileInfo;
-using lyx::support::FileMonitor;
 using lyx::support::GetFileContents;
 using lyx::support::IsFileReadable;
 using lyx::support::IsLyXFilename;
@@ -65,33 +67,6 @@ using std::ostringstream;
 extern BufferList bufferlist;
 
 
-class InsetInclude::PreviewImpl : public PreviewedInset {
-public:
-       ///
-       PreviewImpl(InsetInclude const & p) : parent_(p) {}
-
-       ///
-       bool previewWanted(Buffer const &) const;
-       ///
-       string const latexString(Buffer const &) const;
-       ///
-       ///
-       bool monitoring() const { return monitor_.get(); }
-       ///
-       void startMonitoring(string const & file);
-       ///
-       void stopMonitoring() { monitor_.reset(); }
-
-private:
-       /// Invoked by monitor_ should the parent file change.
-       void restartLoading();
-       ///
-       boost::scoped_ptr<FileMonitor> monitor_;
-       ///
-       InsetInclude const & parent_;
-};
-
-
 namespace {
 
 string const uniqueID()
@@ -105,10 +80,11 @@ string const uniqueID()
 
 InsetInclude::InsetInclude(InsetCommandParams const & p)
        : params_(p), include_label(uniqueID()),
-         preview_(new PreviewImpl(*this)),
+         preview_(new RenderMonitoredPreview),
          set_label_(false)
 {
        preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
+       preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
 
 
@@ -116,10 +92,11 @@ InsetInclude::InsetInclude(InsetInclude const & other)
        : InsetOld(other),
          params_(other.params_),
          include_label(other.include_label),
-         preview_(new PreviewImpl(*this)),
+         preview_(new RenderMonitoredPreview),
          set_label_(other.set_label_)
 {
        preview_->connect(boost::bind(&InsetInclude::statusChanged, this));
+       preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
 
 
@@ -130,7 +107,8 @@ InsetInclude::~InsetInclude()
 }
 
 
-dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
+dispatch_result
+InsetInclude::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
 {
        switch (cmd.action) {
 
@@ -158,7 +136,7 @@ dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
                return DISPATCHED;
 
        default:
-               return InsetOld::localDispatch(cmd);
+               return UNDISPATCHED;
        }
 }
 
@@ -197,10 +175,27 @@ Types type(InsetCommandParams const & params)
 bool isVerbatim(InsetCommandParams const & params)
 {
        string const command_name = params.getCmdName();
-       return command_name == "verbatiminput" || 
+       return command_name == "verbatiminput" ||
                command_name == "verbatiminput*";
 }
 
+
+string const masterFilename(Buffer const & buffer)
+{
+       return buffer.fileName();
+}
+
+
+string const includedFilename(Buffer const & buffer,
+                             InsetCommandParams const & params)
+{
+       return MakeAbsPath(params.getContents(),
+                          OnlyPath(masterFilename(buffer)));
+}
+
+
+void generate_preview(RenderPreview &, InsetInclude const &, Buffer const &);
+
 } // namespace anon
 
 
@@ -212,8 +207,8 @@ void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
        if (preview_->monitoring())
                preview_->stopMonitoring();
 
-       if (PreviewedInset::activated() && type(params_) == INPUT)
-               preview_->generatePreview(buffer);
+       if (type(params_) == INPUT)
+               generate_preview(*preview_, *this, buffer);
 }
 
 
@@ -272,20 +267,6 @@ string const InsetInclude::getScreenLabel(Buffer const &) const
 
 namespace {
 
-string const masterFilename(Buffer const & buffer)
-{
-       return buffer.fileName();
-}
-
-
-string const includedFilename(Buffer const & buffer,
-                             InsetCommandParams const & params)
-{
-       return MakeAbsPath(params.getContents(),
-                          OnlyPath(masterFilename(buffer)));
-}
-
-
 /// return true if the file is or got loaded.
 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
 {
@@ -539,11 +520,8 @@ void InsetInclude::fillWithBibKeys(Buffer const & buffer,
 
 void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       Buffer const * buffer_ptr = mi.base.bv ? mi.base.bv->buffer() : 0;
-       if (buffer_ptr && preview_->previewReady(*buffer_ptr)) {
-               dim.asc = preview_->pimage()->ascent();
-               dim.des = preview_->pimage()->descent();
-               dim.wid = preview_->pimage()->width();
+       if (RenderPreview::activated() && preview_->previewReady()) {
+               preview_->metrics(mi, dim);
        } else {
                if (!set_label_) {
                        set_label_ = true;
@@ -564,29 +542,19 @@ void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void InsetInclude::draw(PainterInfo & pi, int x, int y) const
 {
-       cache(pi.base.bv);
-       Buffer const * buffer_ptr = pi.base.bv ? pi.base.bv->buffer() : 0;
-       bool const use_preview = buffer_ptr && preview_->previewReady(*buffer_ptr);
-
-       if (!use_preview) {
+       if (!RenderPreview::activated() || !preview_->previewReady()) {
                button_.draw(pi, x + button_.box().x1, y);
                return;
        }
 
-       if (!preview_->monitoring()) {
-               string const included_file =
-                       includedFilename(*view()->buffer(), params_);
+       BOOST_ASSERT(pi.base.bv);
+       Buffer const * const buffer = pi.base.bv->buffer();
+       if (!preview_->monitoring() && buffer) {
+               string const included_file = includedFilename(*buffer, params_);
                preview_->startMonitoring(included_file);
        }
 
-       pi.pain.image(x + button_.box().x1, y - dim_.asc, dim_.wid, dim_.height(),
-                           *(preview_->pimage()->image()));
-}
-
-
-BufferView * InsetInclude::view() const
-{
-       return button_.view();
+       preview_->draw(pi, x + button_.box().x1, y);
 }
 
 
@@ -596,56 +564,63 @@ BufferView * InsetInclude::view() const
 
 void InsetInclude::statusChanged() const
 {
-       if (view())
-               view()->updateInset(this);
+       LyX::cref().updateInset(this);
 }
 
 
-void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
+void InsetInclude::fileChanged() const
 {
-       preview_->addPreview(ploader);
+       Buffer const * const buffer_ptr = LyX::cref().updateInset(this);
+       if (!buffer_ptr)
+               return;
+
+       Buffer const & buffer = *buffer_ptr;
+       preview_->removePreview(buffer);
+       generate_preview(*preview_.get(), *this, buffer);
 }
 
 
-bool InsetInclude::PreviewImpl::previewWanted(Buffer const & buffer) const
+namespace {
+
+bool preview_wanted(InsetCommandParams const & params, Buffer const & buffer)
 {
-       string const included_file = includedFilename(buffer, parent_.params());
+       string const included_file = includedFilename(buffer, params);
 
-       return type(parent_.params_) == INPUT &&
-               parent_.params_.preview() &&
+       return type(params) == INPUT && params.preview() &&
                IsFileReadable(included_file);
 }
 
 
-string const InsetInclude::PreviewImpl::latexString(Buffer const & buffer) const
+string const latex_string(InsetInclude const & inset, Buffer const & buffer)
 {
        ostringstream os;
        LatexRunParams runparams;
        runparams.flavor = LatexRunParams::LATEX;
-       parent_.latex(buffer, os, runparams);
+       inset.latex(buffer, os, runparams);
 
        return os.str();
 }
 
 
-void InsetInclude::PreviewImpl::startMonitoring(string const & file)
+void generate_preview(RenderPreview & renderer,
+                     InsetInclude const & inset,
+                     Buffer const & buffer)
 {
-       monitor_.reset(new FileMonitor(file, 2000));
-       monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
-       monitor_->start();
+       InsetCommandParams const & params = inset.params();
+       if (RenderPreview::activated() && preview_wanted(params, buffer)) {
+               string const snippet = latex_string(inset, buffer);
+               renderer.generatePreview(snippet, buffer);
+       }
 }
 
+} // namespace anon
+
 
-void InsetInclude::PreviewImpl::restartLoading()
+void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
 {
-       BufferView * const view = parent_.view();
-       if (!view)
-               return;
-       view->updateInset(&parent_);
-       if (view->buffer()) {
-               Buffer const & buffer = *view->buffer();
-               removePreview(buffer);
-               generatePreview(buffer);
+       if (preview_wanted(params(), ploader.buffer())) {
+               string const snippet = latex_string(*this, ploader.buffer());
+               preview_->addPreview(snippet, ploader);
        }
 }