]> git.lyx.org Git - lyx.git/blobdiff - src/insets/render_graphic.C
convert graphics for .tex export, fixes bug 1231
[lyx.git] / src / insets / render_graphic.C
index 52186bc9f4c78c28bb508d72fb38e89359059ee2..31d4605b117778504a61b4afb6e48a546a2712fc 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "gettext.h"
 #include "LColor.h"
+#include "lyx_main.h"
+#include "lyxrc.h"
 #include "metricsinfo.h"
 
 #include "frontends/font_metrics.h"
 
 #include "support/filetools.h"
 
+#include <boost/bind.hpp>
+
+namespace graphics = lyx::graphics;
 
 using lyx::support::AbsolutePath;
 using lyx::support::OnlyFilename;
 
 using std::string;
+using std::auto_ptr;
 
 
-RenderGraphic::RenderGraphic()
-       : checksum_(0)
-{}
+RenderGraphic::RenderGraphic(InsetBase const * inset)
+{
+       loader_.connect(boost::bind(&LyX::updateInset,
+                                   boost::cref(LyX::cref()), inset));
+}
 
 
-RenderGraphic::RenderGraphic(RenderGraphic const & other)
+RenderGraphic::RenderGraphic(RenderGraphic const & other,
+                            InsetBase const * inset)
        : RenderBase(other),
          loader_(other.loader_),
-         params_(other.params_),
-         checksum_(0)
-{}
+         params_(other.params_)
+{
+       loader_.connect(boost::bind(&LyX::updateInset,
+                                   boost::cref(LyX::cref()), inset));
+}
 
 
-RenderBase * RenderGraphic::clone() const
+auto_ptr<RenderBase> RenderGraphic::clone(InsetBase const * inset) const
 {
-       return new RenderGraphic(*this);
+       return auto_ptr<RenderBase>(new RenderGraphic(*this, inset));
 }
 
 
-void RenderGraphic::update(lyx::graphics::Params const & params)
+void RenderGraphic::update(graphics::Params const & params)
 {
        params_ = params;
 
@@ -62,63 +73,62 @@ void RenderGraphic::update(lyx::graphics::Params const & params)
 }
 
 
-bool RenderGraphic::hasFileChanged() const
-{
-       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;
-}
-
+namespace {
 
-boost::signals::connection RenderGraphic::connect(slot_type const & slot) const
+bool displayGraphic(graphics::Params const & params)
 {
-       return loader_.connect(slot);
+       return params.display != graphics::NoDisplay &&
+               lyxrc.display_graphics != graphics::NoDisplay;
 }
 
 
-string const RenderGraphic::statusMessage() const
+string const statusMessage(graphics::Params const & params,
+                          graphics::ImageStatus status)
 {
-       switch (loader_.status()) {
-               case lyx::graphics::WaitingToLoad:
-                       return _("Not shown.");
-               case lyx::graphics::Loading:
-                       return _("Loading...");
-               case lyx::graphics::Converting:
-                       return _("Converting to loadable format...");
-               case lyx::graphics::Loaded:
-                       return _("Loaded into memory. Must now generate pixmap.");
-               case lyx::graphics::ScalingEtc:
-                       return _("Scaling etc...");
-               case lyx::graphics::Ready:
-                       return _("Ready to display");
-               case lyx::graphics::ErrorNoFile:
-                       return _("No file found!");
-               case lyx::graphics::ErrorConverting:
-                       return _("Error converting to loadable format");
-               case lyx::graphics::ErrorLoading:
-                       return _("Error loading file into memory");
-               case lyx::graphics::ErrorGeneratingPixmap:
-                       return _("Error generating the pixmap");
-               case lyx::graphics::ErrorUnknown:
-                       return _("No image");
+       if (!displayGraphic(params))
+               return _("Not shown.");
+
+       switch (status) {
+       case graphics::WaitingToLoad:
+               return _("Not shown.");
+       case graphics::Loading:
+               return _("Loading...");
+       case graphics::Converting:
+               return _("Converting to loadable format...");
+       case graphics::Loaded:
+               return _("Loaded into memory. Generating pixmap...");
+       case graphics::ScalingEtc:
+               return _("Scaling etc...");
+       case graphics::Ready:
+               return _("Ready to display");
+       case graphics::ErrorNoFile:
+               return _("No file found!");
+       case graphics::ErrorConverting:
+               return _("Error converting to loadable format");
+       case graphics::ErrorLoading:
+               return _("Error loading file into memory");
+       case graphics::ErrorGeneratingPixmap:
+               return _("Error generating the pixmap");
+       case graphics::ErrorUnknown:
+               return _("No image");
        }
        return string();
 }
 
 
-bool RenderGraphic::readyToDisplay() const
+bool readyToDisplay(graphics::Loader const & loader)
 {
-       if (!loader_.image() || loader_.status() != lyx::graphics::Ready)
+       if (!loader.image() || loader.status() != graphics::Ready)
                return false;
-       return loader_.image()->isDrawable();
+       return loader.image()->isDrawable();
 }
 
+} // namespace anon
+
 
 void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       bool image_ready = readyToDisplay();
+       bool image_ready = displayGraphic(params_) && readyToDisplay(loader_);
 
        dim.asc = image_ready ? loader_.image()->getHeight() : 50;
        dim.des = 0;
@@ -138,7 +148,7 @@ void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
                        font_width = font_metrics::width(justname, msgFont);
                }
 
-               string const msg = statusMessage();
+               string const msg = statusMessage(params_, loader_.status());
                if (!msg.empty()) {
                        msgFont.setSize(LyXFont::SIZE_TINY);
                        font_width = std::max(font_width,
@@ -154,18 +164,17 @@ void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
 {
-       if (params_.display != lyx::graphics::NoDisplay &&
-           loader_.status() == lyx::graphics::WaitingToLoad)
-               loader_.startLoading();
-
-       if (params_.display != lyx::graphics::NoDisplay &&
-           !loader_.monitoring())
-               loader_.startMonitoring();
+       if (displayGraphic(params_)) {
+               if (loader_.status() == graphics::WaitingToLoad)
+                       loader_.startLoading();
+               if (!loader_.monitoring())
+                       loader_.startMonitoring();
+       }
 
-       // This will draw the graphics. If the graphics has not been loaded yet,
-       // we draw just a rectangle.
+       // This will draw the graphics. If the graphics has not been
+       // loaded yet, we draw just a rectangle.
 
-       if (readyToDisplay()) {
+       if (displayGraphic(params_) && readyToDisplay(loader_)) {
                pi.pain.image(x + InsetOld::TEXT_TO_INSET_OFFSET,
                              y - dim_.asc,
                              dim_.wid - 2 * InsetOld::TEXT_TO_INSET_OFFSET,
@@ -192,7 +201,7 @@ void RenderGraphic::draw(PainterInfo & pi, int x, int y) const
                }
 
                // Print the message.
-               string const msg = statusMessage();
+               string const msg = statusMessage(params_, loader_.status());
                if (!msg.empty()) {
                        msgFont.setSize(LyXFont::SIZE_TINY);
                        pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6,