]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetExternal.cpp
* InsetCollapsable:
[lyx.git] / src / insets / InsetExternal.cpp
index eb78e97a5479cdbf898879fc60fb54af83261f79..2ca4511163fb54e422e1e7ff463dd77074c3a877 100644 (file)
@@ -18,7 +18,7 @@
 #include "insets/RenderPreview.h"
 
 #include "Buffer.h"
-#include "LCursor.h"
+#include "Cursor.h"
 #include "debug.h"
 #include "DispatchResult.h"
 #include "Exporter.h"
@@ -27,7 +27,7 @@
 #include "gettext.h"
 #include "LaTeXFeatures.h"
 #include "LyX.h"
-#include "LyXLex.h"
+#include "Lexer.h"
 #include "LyXRC.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/convert.h"
-#include "support/translator.h"
+#include "support/Translator.h"
 
 #include <boost/bind.hpp>
 
 #include <sstream>
 
-using std::endl;
 using std::string;
-using std::auto_ptr;
 using std::istringstream;
 using std::ostream;
 using std::ostringstream;
 using std::vector;
 
+using lyx::support::DocFileName;
 
 namespace {
 
@@ -59,7 +58,7 @@ lyx::external::DisplayType const defaultDisplayType = lyx::external::NoDisplay;
 
 unsigned int const defaultLyxScale = 100;
 
-string defaultTemplateName = "RasterImage";
+string defaultTemplateName;
 
 } // namespace anon
 
@@ -132,9 +131,15 @@ Translator<DisplayType, string> const & displayTranslator()
 InsetExternalParams::InsetExternalParams()
        : display(defaultDisplayType),
          lyxscale(defaultLyxScale),
-         draft(false),
-         templatename_(defaultTemplateName)
-{}
+         draft(false)
+{
+       if (defaultTemplateName.empty()) {
+               external::TemplateManager const & etm =
+                       external::TemplateManager::get();
+               templatename_ = etm.getTemplates().begin()->first;
+       } else
+               templatename_ = defaultTemplateName;
+}
 
 
 namespace {
@@ -182,10 +187,15 @@ void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
        os << "External\n"
           << "\ttemplate " << templatename() << '\n';
 
-       if (!filename.empty())
-               os << "\tfilename "
-                  << filename.outputFilename(buffer.filePath())
-                  << '\n';
+       if (!filename.empty()) {
+               // when we save, we still use the original filename
+               EmbeddedFiles::EmbeddedFileList::const_iterator it = 
+                       buffer.embeddedFiles().find(filename.toFilesystemEncoding());
+               if (it != buffer.embeddedFiles().end())
+                       os << "\tfilename " << DocFileName(it->absFilename()).outputFilename(buffer.filePath()) << '\n';
+               else
+                       os << "\tfilename " << filename.outputFilename(buffer.filePath()) << '\n';
+       }
 
        if (display != defaultDisplayType)
                os << "\tdisplay "
@@ -239,7 +249,7 @@ void InsetExternalParams::write(Buffer const & buffer, ostream & os) const
 }
 
 
-bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
+bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
 {
        enum ExternalTags {
                EX_TEMPLATE = 1,
@@ -277,7 +287,7 @@ bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
                { "width",           EX_WIDTH }
        };
 
-       pushpophelper pph(lex, external_tags, EX_END);
+       PushPopHelper pph(lex, external_tags, EX_END);
 
        bool found_end  = false;
        bool read_error = false;
@@ -293,6 +303,12 @@ bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
                        lex.eatLine();
                        string const name = lex.getString();
                        filename.set(name, buffer.filePath());
+                       // maybe this file is embedded
+                       EmbeddedFiles::EmbeddedFileList::const_iterator it = buffer.embeddedFiles().find(filename.toFilesystemEncoding());
+                       if (it != buffer.embeddedFiles().end())
+                               // using available file, embedded or external, depending on file availability and
+                               // embedding status.
+                               filename = DocFileName(it->availableFile(&buffer));
                        break;
                }
 
@@ -337,7 +353,7 @@ bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
 
                case EX_HEIGHT:
                        lex.next();
-                       resizedata.height = LyXLength(lex.getString());
+                       resizedata.height = Length(lex.getString());
                        break;
 
                case EX_KEEPASPECTRATIO:
@@ -361,7 +377,7 @@ bool InsetExternalParams::read(Buffer const & buffer, LyXLex & lex)
 
                case EX_WIDTH:
                        lex.next();
-                       resizedata.width = LyXLength(lex.getString());
+                       resizedata.width = Length(lex.getString());
                        break;
 
                case EX_END:
@@ -400,16 +416,16 @@ InsetExternal::InsetExternal()
 
 
 InsetExternal::InsetExternal(InsetExternal const & other)
-       : InsetOld(other),
+       : Inset(other),
          boost::signals::trackable(),
          params_(other.params_),
          renderer_(other.renderer_->clone(this))
 {}
 
 
-auto_ptr<InsetBase> InsetExternal::doClone() const
+Inset * InsetExternal::clone() const
 {
-       return auto_ptr<InsetBase>(new InsetExternal(*this));
+       return new InsetExternal(*this);
 }
 
 
@@ -425,7 +441,7 @@ void InsetExternal::statusChanged() const
 }
 
 
-void InsetExternal::doDispatch(LCursor & cur, FuncRequest & cmd)
+void InsetExternal::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
 
@@ -450,16 +466,17 @@ void InsetExternal::doDispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_MOUSE_RELEASE:
-               InsetExternalMailer(*this).showDialog(&cur.bv());
+               if (!cur.selection())
+                       InsetExternalMailer(*this).showDialog(&cur.bv());
                break;
 
        default:
-               InsetBase::doDispatch(cur, cmd);
+               Inset::doDispatch(cur, cmd);
        }
 }
 
 
-bool InsetExternal::getStatus(LCursor & cur, FuncRequest const & cmd,
+bool InsetExternal::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
        switch (cmd.action) {
@@ -471,29 +488,32 @@ bool InsetExternal::getStatus(LCursor & cur, FuncRequest const & cmd,
                return true;
 
        default:
-               return InsetBase::getStatus(cur, cmd, flag);
+               return Inset::getStatus(cur, cmd, flag);
        }
 }
 
 
-void InsetExternal::edit(LCursor & cur, bool)
+void InsetExternal::registerEmbeddedFiles(Buffer const &,
+       EmbeddedFiles & files) const
+{
+       files.registerFile(params_.filename.absFilename(), false, this);
+}
+
+
+void InsetExternal::edit(Cursor & cur, bool)
 {
        InsetExternalMailer(*this).showDialog(&cur.bv());
 }
 
 
-bool InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
+void InsetExternal::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        renderer_->metrics(mi, dim);
-       bool const changed = dim_ != dim;
-       dim_ = dim;
-       return changed;
 }
 
 
 void InsetExternal::draw(PainterInfo & pi, int x, int y) const
 {
-       setPosCache(pi, x, y);
        renderer_->draw(pi, x, y);
 }
 
@@ -557,7 +577,7 @@ graphics::Params get_grfx_params(InsetExternalParams const & eparams)
                BOOST_ASSERT(false);
        }
        if (gparams.display == graphics::DefaultDisplay)
-               gparams.display = lyxrc.display_graphics;
+               gparams.display = graphics::DisplayType(lyxrc.display_graphics);
        // Override the above if we're not using a gui
        if (!use_gui)
                gparams.display = graphics::NoDisplay;
@@ -576,8 +596,9 @@ docstring const getScreenLabel(InsetExternalParams const & params,
                return support::bformat((_("External template %1$s is not installed")),
                                        from_utf8(params.templatename()));
        // FIXME UNICODE
+       docstring gui = _(ptr->guiName);
        return from_utf8(external::doSubstitution(params, buffer,
-                               ptr->guiName, false));
+                               to_utf8(gui), false));
 }
 
 void add_preview_and_start_loading(RenderMonitoredPreview &,
@@ -665,7 +686,7 @@ void InsetExternal::write(Buffer const & buffer, ostream & os) const
 }
 
 
-void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
+void InsetExternal::read(Buffer const & buffer, Lexer & lex)
 {
        InsetExternalParams params;
        if (params.read(buffer, lex))
@@ -674,7 +695,7 @@ void InsetExternal::read(Buffer const & buffer, LyXLex & lex)
 
 
 int InsetExternal::latex(Buffer const & buf, odocstream & os,
-                         OutputParams const & runparams) const
+                        OutputParams const & runparams) const
 {
        if (params_.draft) {
                // FIXME UNICODE
@@ -705,37 +726,37 @@ int InsetExternal::latex(Buffer const & buf, odocstream & os,
 
                if (cit != et.formats.end()) {
                        return external::writeExternal(params_, "PDFLaTeX",
-                                                       buf, os,
-                                                       *(runparams.exportdata),
-                                                       external_in_tmpdir,
-                                                       dryrun);
-                }
+                                                      buf, os,
+                                                      *(runparams.exportdata),
+                                                      external_in_tmpdir,
+                                                      dryrun);
+               }
        }
 
        return external::writeExternal(params_, "LaTeX", buf, os,
-                                       *(runparams.exportdata),
-                                       external_in_tmpdir,
-                                       dryrun);
+                                      *(runparams.exportdata),
+                                      external_in_tmpdir,
+                                      dryrun);
 }
 
 
 int InsetExternal::plaintext(Buffer const & buf, odocstream & os,
-                             OutputParams const & runparams) const
+                            OutputParams const & runparams) const
 {
        os << '\n'; // output external material on a new line
        external::writeExternal(params_, "Ascii", buf, os,
-                               *(runparams.exportdata), false,
-                               runparams.dryrun || runparams.inComment);
+                               *(runparams.exportdata), false,
+                               runparams.dryrun || runparams.inComment);
        return PLAINTEXT_NEWLINE;
 }
 
 
 int InsetExternal::docbook(Buffer const & buf, odocstream & os,
-                           OutputParams const & runparams) const
+                          OutputParams const & runparams) const
 {
        return external::writeExternal(params_, "DocBook", buf, os,
                                       *(runparams.exportdata), false,
-                                      runparams.dryrun || runparams.inComment);
+                                      runparams.dryrun || runparams.inComment);
 }
 
 
@@ -796,7 +817,7 @@ namespace {
 bool preview_wanted(InsetExternalParams const & params)
 {
        return params.display == external::PreviewDisplay &&
-               support::isFileReadable(params.filename);
+               params.filename.isFileReadable();
 }
 
 
@@ -868,7 +889,7 @@ void InsetExternalMailer::string2params(string const & in,
                return;
 
        istringstream data(in);
-       LyXLex lex(0,0);
+       Lexer lex(0,0);
        lex.setStream(data);
 
        string name;
@@ -877,7 +898,7 @@ void InsetExternalMailer::string2params(string const & in,
                return print_mailer_error("InsetExternalMailer", in, 1, name_);
 
        // This is part of the inset proper that is usually swallowed
-       // by LyXText::readInset
+       // by Text::readInset
        string id;
        lex >> id;
        if (!lex || id != "External")