]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetexternal.C
prevent crash when inserting minipage in table cell,
[lyx.git] / src / insets / insetexternal.C
index a40280cd68feab1ebc79679215bef2f6144c02a3..d8f4c83af2c889fa4fa8423d7af0a751eaea5f5e 100644 (file)
 #include "support/lstrings.h"
 #include "support/lyxalgo.h"
 #include "support/path.h"
+#include "support/path_defines.h"
 #include "support/tostr.h"
+#include "support/LAssert.h"
+#include "support/translator.h"
 
 #include <boost/bind.hpp>
 
@@ -48,6 +51,14 @@ using namespace lyx::support;
 
 using std::ostream;
 using std::endl;
+using std::auto_ptr;
+
+namespace lyx {
+namespace graphics {
+/// The translator between the DisplayType and the corresponding lyx string.
+extern Translator<DisplayType, string> displayTranslator;
+}
+}
 
 namespace {
 
@@ -88,7 +99,7 @@ InsetExternal::InsetExternal()
 
 
 InsetExternal::InsetExternal(InsetExternal const & other)
-       : Inset(other),
+       : InsetOld(other),
          boost::signals::trackable(),
          params_(other.params_),
          renderer_(other.renderer_->clone())
@@ -100,10 +111,9 @@ InsetExternal::InsetExternal(InsetExternal const & other)
 }
 
 
-InsetBase * InsetExternal::clone() const
+auto_ptr<InsetBase> InsetExternal::clone() const
 {
-       InsetExternal * inset = new InsetExternal(*this);
-       return inset;
+       return auto_ptr<InsetBase>(new InsetExternal(*this));
 }
 
 
@@ -138,10 +148,10 @@ dispatch_result InsetExternal::localDispatch(FuncRequest const & cmd)
        case LFUN_INSET_MODIFY: {
                Assert(cmd.view());
 
-               Buffer const & buffer = *cmd.view()->buffer();
+               Buffer const * buffer = cmd.view()->buffer();
                InsetExternal::Params p;
-               InsetExternalMailer::string2params(cmd.argument, buffer, p);
-               setParams(p);
+               InsetExternalMailer::string2params(cmd.argument, *buffer, p);
+               setParams(p, buffer);
                cmd.view()->updateInset(this);
                return DISPATCHED;
        }
@@ -205,13 +215,14 @@ ExternalTemplate const * getTemplatePtr(InsetExternal::Params const & params)
 }
 
 
-string const getScreenLabel(InsetExternal::Params const & params)
+string const getScreenLabel(InsetExternal::Params const & params,
+                           Buffer const * buffer)
 {
        ExternalTemplate const * const ptr = getTemplatePtr(params);
        if (!ptr)
                return bformat(_("External template %1$s is not installed"),
                               params.templatename);
-       return doSubstitution(params, 0, ptr->guiName);
+       return doSubstitution(params, buffer, ptr->guiName);
 }
 
 } // namespace anon
@@ -223,7 +234,7 @@ InsetExternal::Params const & InsetExternal::params() const
 }
 
 
-void InsetExternal::setParams(Params const & p)
+void InsetExternal::setParams(Params const & p, Buffer const * buffer)
 {
        // The stored params; what we would like to happen in an ideal world.
        params_.filename = p.filename;
@@ -244,7 +255,7 @@ void InsetExternal::setParams(Params const & p)
                        renderer_.reset(button_ptr);
                }
 
-               button_ptr->update(getScreenLabel(params_), true);
+               button_ptr->update(getScreenLabel(params_, buffer), true);
 
        } else {
                GraphicRenderer * graphic_ptr =
@@ -261,12 +272,6 @@ void InsetExternal::setParams(Params const & p)
 }
 
 
-string const InsetExternal::editMessage() const
-{
-       return getScreenLabel(params_);
-}
-
-
 void InsetExternal::write(Buffer const * buffer, ostream & os) const
 {
        os << "External\n"
@@ -304,7 +309,7 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex)
                { "template", EX_TEMPLATE }
        };
 
-       lex.pushTable(external_tags, EX_END);
+       pushpophelper pph(lex, external_tags, EX_END);
 
        bool found_end  = false;
        bool read_error = false;
@@ -358,10 +363,8 @@ void InsetExternal::read(Buffer const * buffer, LyXLex & lex)
                               "Missing \\end_inset.");
        }
 
-       lex.popTable();
-
        // Replace the inset's store
-       setParams(params);
+       setParams(params, buffer);
 
        lyxerr[Debug::INFO] << "InsetExternal::Read: "
                            << "template: '" << params_.templatename
@@ -546,15 +549,16 @@ string const doSubstitution(InsetExternal::Params const & params,
                            Buffer const * buffer, string const & s)
 {
        string result;
-       string const absfilename = params.filename.absFilename();
-       string const basename = ChangeExtension(absfilename, string());
-       string filepath;
+       string const buffer_path = buffer ? buffer->filePath() : string();
+       string const filename = params.filename.outputFilename(buffer_path);
+       string const basename = ChangeExtension(filename, string());
+       string const filepath = OnlyPath(filename);
 
-       result = subst(s, "$$FName", absfilename);
+       result = subst(s, "$$FName", filename);
        result = subst(result, "$$Basename", basename);
        result = subst(result, "$$FPath", filepath);
        result = subst(result, "$$Tempname", params.tempname);
-       result = subst(result, "$$Sysdir", system_lyxdir);
+       result = subst(result, "$$Sysdir", system_lyxdir());
 
        // Handle the $$Contents(filename) syntax
        if (contains(result, "$$Contents(\"")) {
@@ -662,7 +666,7 @@ InsetExternalMailer::params2string(InsetExternal::Params const & params,
                                   Buffer const & buffer)
 {
        InsetExternal inset;
-       inset.setParams(params);
+       inset.setParams(params, &buffer);
        ostringstream data;
        data << name_ << ' ';
        inset.write(&buffer, data);