]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphics.C
Rename LatexRunParams::fragile as moving_arg.
[lyx.git] / src / insets / insetgraphics.C
index ab60e9ba70be7a7262b2bcb451e6beafc516ab51..3517ec5b194e592b21469f17efe38dd2939f2449 100644 (file)
@@ -52,7 +52,6 @@ TODO
 
 #include <config.h>
 
-
 #include "insets/insetgraphics.h"
 #include "insets/insetgraphicsParams.h"
 
@@ -61,6 +60,7 @@ TODO
 #include "graphics/GraphicsParams.h"
 
 #include "lyxtext.h"
+#include "dimension.h"
 #include "buffer.h"
 #include "BufferView.h"
 #include "converter.h"
@@ -69,9 +69,13 @@ TODO
 #include "funcrequest.h"
 #include "gettext.h"
 #include "LaTeXFeatures.h"
+#include "latexrunparams.h"
+#include "Lsstream.h"
 #include "lyxlex.h"
 #include "lyxrc.h"
+#include "Lsstream.h"
 
+#include "frontends/lyx_gui.h"
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
 #include "frontends/font_metrics.h"
@@ -83,17 +87,19 @@ TODO
 #include "support/lyxalgo.h" // lyx::count
 #include "support/lyxlib.h" // float_equal
 #include "support/path.h"
+#include "support/tostr.h"
 #include "support/systemcall.h"
 #include "support/os.h"
+#include "support/lstrings.h"
 
 #include <boost/weak_ptr.hpp>
 #include <boost/bind.hpp>
 #include <boost/signals/trackable.hpp>
-#include "BoostFormat.h"
 
 #include <algorithm> // For the std::max
 
 extern string system_tempdir;
+// set by Exporters
 
 using std::ostream;
 using std::endl;
@@ -117,21 +123,14 @@ string const RemoveExtension(string const & filename)
 string const uniqueID()
 {
        static unsigned int seed = 1000;
-
-       ostringstream ost;
-       ost << "graph" << ++seed;
-
-       // Needed if we use lyxstring.
-       return STRCONV(ost.str());
+       return "graph" + tostr(++seed);
 }
 
 
-string findTargetFormat(string const & suffix)
+string findTargetFormat(string const & suffix, LatexRunParams const & runparams)
 {
-       // lyxrc.pdf_mode means:
-       // Are we creating a PDF or a PS file?
-       // (Should actually mean, are we using latex or pdflatex).
-       if (lyxrc.pdf_mode) {
+       // Are we using latex or pdflatex).
+       if (runparams.flavor == LatexRunParams::PDFLATEX) {
                lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode\n";
                if (contains(suffix, "ps") || suffix == "pdf")
                        return "pdf";
@@ -219,38 +218,30 @@ InsetGraphics::~InsetGraphics()
 
 dispatch_result InsetGraphics::localDispatch(FuncRequest const & cmd)
 {
-       dispatch_result result = UNDISPATCHED;
-
        switch (cmd.action) {
        case LFUN_INSET_MODIFY: {
                InsetGraphicsParams p;
                InsetGraphicsMailer::string2params(cmd.argument, p);
-               if (p.filename.empty())
-                       break;
-
-               string const filepath = cmd.view()->buffer()->filePath();
-               setParams(p, filepath);
-               cmd.view()->updateInset(this, true);
-               result = DISPATCHED;
+               if (!p.filename.empty()) {
+                       string const filepath = cmd.view()->buffer()->filePath();
+                       setParams(p, filepath);
+                       cmd.view()->updateInset(this);
+               }
+               return DISPATCHED;
        }
-       break;
 
-       case LFUN_INSET_DIALOG_UPDATE: {
-               InsetGraphicsMailer mailer(*this);
-               mailer.updateDialog();
-       }
-       break;
+       case LFUN_INSET_DIALOG_UPDATE: 
+               InsetGraphicsMailer(*this).updateDialog(cmd.view());
+               return DISPATCHED;
 
+       case LFUN_INSET_EDIT:
        case LFUN_MOUSE_RELEASE:
-               edit(cmd.view(), cmd.x, cmd.y, cmd.button());
-               break;
+               InsetGraphicsMailer(*this).showDialog(cmd.view());
+               return DISPATCHED;
 
        default:
-               result = DISPATCHED;
-               break;
+               return Inset::localDispatch(cmd);
        }
-
-       return result;
 }
 
 
@@ -295,45 +286,36 @@ bool InsetGraphics::imageIsDrawable() const
 }
 
 
-int InsetGraphics::ascent(BufferView *, LyXFont const &) const
+void InsetGraphics::dimension(BufferView *, LyXFont const & font,
+       Dimension & dim) const
 {
        cache_->old_ascent = 50;
        if (imageIsDrawable())
                cache_->old_ascent = cache_->loader.image()->getHeight();
-       return cache_->old_ascent;
-}
-
-
-int InsetGraphics::descent(BufferView *, LyXFont const &) const
-{
-       return 0;
-}
-
-
-int InsetGraphics::width(BufferView *, LyXFont const & font) const
-{
+       dim.a = cache_->old_ascent;
+       dim.d = 0;
        if (imageIsDrawable())
-               return cache_->loader.image()->getWidth() + 2 * TEXT_TO_INSET_OFFSET;
+               dim.w = cache_->loader.image()->getWidth() + 2 * TEXT_TO_INSET_OFFSET;
+       else {
+               int font_width = 0;
 
-       int font_width = 0;
+               LyXFont msgFont(font);
+               msgFont.setFamily(LyXFont::SANS_FAMILY);
 
-       LyXFont msgFont(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 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));
+               }
 
-       string const msg = statusMessage();
-       if (!msg.empty()) {
-               msgFont.setSize(LyXFont::SIZE_TINY);
-               int const msg_width = font_metrics::width(msg, msgFont);
-               font_width = std::max(font_width, msg_width);
+               dim.w = std::max(50, font_width + 15);
        }
-
-       return std::max(50, font_width + 15);
 }
 
 
@@ -344,7 +326,7 @@ BufferView * InsetGraphics::view() const
 
 
 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
-                        int baseline, float & x, bool) const
+                        int baseline, float & x) const
 {
        // MakeAbsPath returns params().filename unchanged if it absolute
        // already.
@@ -418,34 +400,6 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
                        paint.text(old_x + TEXT_TO_INSET_OFFSET + 6, baseline - 4, msg, msgFont);
                }
        }
-
-       // the status message may mean we changed size, so indicate
-       // we need a row redraw
-#if 0
-       if (old_status_ != grfx::ErrorUnknown && old_status_ != cached_status_) {
-               bv->getLyXText()->status(bv, LyXText::CHANGED_IN_DRAW);
-       }
-#endif
-
-       // Reset the cache, ready for the next draw request
-#if 0
-       cached_status_ = grfx::ErrorUnknown;
-       cached_image_.reset();
-       cache_filled_ = false;
-#endif
-}
-
-
-void InsetGraphics::edit(BufferView *, int, int, mouse_button::state)
-{
-       InsetGraphicsMailer mailer(*this);
-       mailer.showDialog();
-}
-
-
-void InsetGraphics::edit(BufferView * bv, bool)
-{
-       edit(bv, 0, 0, mouse_button::none);
 }
 
 
@@ -560,7 +514,8 @@ string const InsetGraphics::createLatexOptions() const
 }
 
 
-string const InsetGraphics::prepareFile(Buffer const * buf) const
+string const InsetGraphics::prepareFile(Buffer const * buf,
+                                       LatexRunParams const & runparams) const
 {
        // LaTeX can cope if the graphics file doesn't exist, so just return the
        // filename.
@@ -621,7 +576,7 @@ string const InsetGraphics::prepareFile(Buffer const * buf) const
        }
 
        string const from = getExtFromContents(orig_file_with_path);
-       string const to   = findTargetFormat(from);
+       string const to   = findTargetFormat(from, runparams);
        lyxerr[Debug::GRAPHICS]
                << "\t we have: from " << from << " to " << to << '\n';
 
@@ -669,8 +624,9 @@ string const InsetGraphics::prepareFile(Buffer const * buf) const
                                << temp_file
                                << (success ? " succeeded\n" : " failed\n");
                        if (!success) {
-                               Alert::alert(_("Cannot copy file"), orig_file_with_path,
-                                       _("into tempdir"));
+                               string str = bformat(_("Could not copy the file\n%1$s\n"
+                                       "into the temporary directory."), orig_file_with_path);
+                               Alert::error(_("Graphics display failed"), str);
                                return orig_file;
                        }
                }
@@ -704,15 +660,12 @@ string const InsetGraphics::prepareFile(Buffer const * buf) const
                        << command << endl;
                Systemcall one;
                one.startscript(Systemcall::Wait, command);
-               if (!IsFileReadable(ChangeExtension(outfile_base, to)))
-#if USE_BOOST_FORMAT
-                       Alert::alert(_("Cannot convert Image (not existing file?)"),
-                                    boost::io::str(boost::format(_("No information for converting from %1$s to %2$s"))
-                               % from % to));
-#else
-                       Alert::alert(_("Cannot convert Image (not existing file?)"),
-                                    _("No information for converting from ") + from + " to " + to);
-#endif
+               if (!IsFileReadable(ChangeExtension(outfile_base, to))) {
+                       string str = bformat(_("No information for converting %1$s "
+                               "format files to %1$s.\n"
+                               "Try defining a convertor in the preferences."), from, to);
+                       Alert::error(_("Could not convert image"), str);
+               }
        }
 
        return RemoveExtension(temp_file);
@@ -720,7 +673,7 @@ string const InsetGraphics::prepareFile(Buffer const * buf) const
 
 
 int InsetGraphics::latex(Buffer const * buf, ostream & os,
-                        bool /*fragile*/, bool/*fs*/) const
+                        LatexRunParams const & runparams) const
 {
        // If there is no file specified or not existing,
        // just output a message about it in the latex output.
@@ -773,7 +726,7 @@ int InsetGraphics::latex(Buffer const * buf, ostream & os,
 
        // "nice" means that the buffer is exported to LaTeX format but not
        //        run through the LaTeX compiler.
-       if (buf->niceFile) {
+       if (runparams.nice) {
                os << before <<'{' << params().filename << '}' << after;
                return 1;
        }
@@ -782,7 +735,7 @@ int InsetGraphics::latex(Buffer const * buf, ostream & os,
        // and remove the extension so the LaTeX will use whatever is
        // appropriate (when there are several versions in different formats)
        string const latex_str = message.empty() ?
-               (before + '{' + os::external_path(prepareFile(buf)) + '}' + after) :
+               (before + '{' + os::external_path(prepareFile(buf, runparams)) + '}' + after) :
                (before + '{' + params().filename + " not found!}" + after);
        os << latex_str;
 
@@ -798,15 +751,7 @@ int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
        // 1. Convert file to ascii using gifscii
        // 2. Read ascii output file and add it to the output stream.
        // at least we send the filename
-#if USE_BOOST_FORMAT
-       os << '<'
-          << boost::format(_("Graphics file: %1$s")) % params().filename
-          << ">\n";
-#else
-       os << '<'
-          << _("Graphics file: ") << params().filename
-          << ">\n";
-#endif
+       os << '<' << bformat(_("Graphics file: %1$s"), params().filename) << ">\n";
        return 0;
 }
 
@@ -850,7 +795,7 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
 void InsetGraphics::statusChanged()
 {
        if (!cache_->view.expired())
-               cache_->view.lock()->updateInset(this, false);
+               cache_->view.lock()->updateInset(this);
 }
 
 
@@ -896,7 +841,10 @@ void InsetGraphicsMailer::string2params(string const & in,
 {
        params = InsetGraphicsParams();
 
-       istringstream data(in);
+       if (in.empty())
+               return;
+
+       istringstream data(STRCONV(in));
        LyXLex lex(0,0);
        lex.setStream(data);
 
@@ -907,9 +855,11 @@ void InsetGraphicsMailer::string2params(string const & in,
                        return;
        }
 
-       InsetGraphics inset;    
-       inset.readInsetGraphics(lex);
-       params = inset.params();
+       if (lex.isOK()) {
+               InsetGraphics inset;
+               inset.readInsetGraphics(lex);
+               params = inset.params();
+       }
 }
 
 
@@ -920,6 +870,5 @@ InsetGraphicsMailer::params2string(InsetGraphicsParams const & params)
        data << name_ << ' ';
        params.Write(data);
        data << "\\end_inset\n";
-
-       return data.str();
+       return STRCONV(data.str());
 }