]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphics.C
fix #832
[lyx.git] / src / insets / insetgraphics.C
index 817c23b8bbc9c8d370ccfe0a5450c6338da929a8..f423210c58408ddf1d1db3f68e7cbf003925f4ac 100644 (file)
@@ -52,7 +52,6 @@ TODO
 
 #include <config.h>
 
-
 #include "insets/insetgraphics.h"
 #include "insets/insetgraphicsParams.h"
 
@@ -69,9 +68,12 @@ TODO
 #include "funcrequest.h"
 #include "gettext.h"
 #include "LaTeXFeatures.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 +85,20 @@ 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 <boost/weak_ptr.hpp>
 #include <boost/bind.hpp>
 #include <boost/signals/trackable.hpp>
-#include "BoostFormat.h"
+#include "support/BoostFormat.h"
 
 #include <algorithm> // For the std::max
 
 extern string system_tempdir;
+// set by Exporters
+extern bool pdf_mode;
 
 using std::ostream;
 using std::endl;
@@ -117,21 +122,16 @@ 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)
 {
-       // lyxrc.pdf_mode means:
+       // 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) {
+       if (pdf_mode) {
                lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode\n";
                if (contains(suffix, "ps") || suffix == "pdf")
                        return "pdf";
@@ -219,38 +219,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(cmd.view());
-       }
-       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;
 }
 
 
@@ -421,19 +413,6 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
 }
 
 
-void InsetGraphics::edit(BufferView * bv, int, int, mouse_button::state)
-{
-       InsetGraphicsMailer mailer(*this);
-       mailer.showDialog(bv);
-}
-
-
-void InsetGraphics::edit(BufferView * bv, bool)
-{
-       edit(bv, 0, 0, mouse_button::none);
-}
-
-
 Inset::EDITABLE InsetGraphics::editable() const
 {
        return IS_EDITABLE;
@@ -654,8 +633,16 @@ 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"));
+#if USE_BOOST_FORMAT
+                               boost::format fmt(_("Could not copy the file\n%1$s\ninto the temporary directory."));
+                               fmt % orig_file_with_path;
+                               string str = fmt.str();
+#else
+                               string str = _("Could not copy the file\n");
+                               str += orig_file_with_path;
+                               str += _("\ninto the temporary directory.");
+#endif
+                               Alert::error(_("Graphics display failed"), str);
                                return orig_file;
                        }
                }
@@ -689,15 +676,19 @@ string const InsetGraphics::prepareFile(Buffer const * buf) const
                        << command << endl;
                Systemcall one;
                one.startscript(Systemcall::Wait, command);
-               if (!IsFileReadable(ChangeExtension(outfile_base, to)))
+               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));
+                       boost::format fmt(_("No information for converting %1$s format files to %1$s.\n"
+                               "Try defining a convertor in the preferences."));
+                       fmt % from % to;
+                       string str = fmt.str();
 #else
-                       Alert::alert(_("Cannot convert Image (not existing file?)"),
-                                    _("No information for converting from ") + from + " to " + to);
+                       string str = _("No information for converting ");
+                       str += from + _(" format files to ") + to;
+                       str += _(".\nTry defining a convertor in the preferences.");
 #endif
+                       Alert::error(_("Could not convert image"), str);
+               }
        }
 
        return RemoveExtension(temp_file);
@@ -783,15 +774,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;
 }
 
@@ -835,7 +818,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);
 }
 
 
@@ -881,7 +864,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);
 
@@ -893,7 +879,7 @@ void InsetGraphicsMailer::string2params(string const & in,
        }
 
        if (lex.isOK()) {
-               InsetGraphics inset;    
+               InsetGraphics inset;
                inset.readInsetGraphics(lex);
                params = inset.params();
        }
@@ -907,6 +893,5 @@ InsetGraphicsMailer::params2string(InsetGraphicsParams const & params)
        data << name_ << ' ';
        params.Write(data);
        data << "\\end_inset\n";
-
-       return data.str();
+       return STRCONV(data.str());
 }