]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetGraphics.cpp
This should be the last of the commits refactoring the InsetLayout code.
[lyx.git] / src / insets / InsetGraphics.cpp
index 4368ffb6f9622e0fa6652831be0d563bc00bf5ab..421dcf1a6b3e31c7b26896275b73008415afb9ba 100644 (file)
@@ -15,7 +15,6 @@ TODO
     * What advanced features the users want to do?
       Implement them in a non latex dependent way, but a logical way.
       LyX should translate it to latex or any other fitting format.
-    * Add a way to roll the image file into the file format.
     * When loading, if the image is not found in the expected place, try
       to find it in the clipart, or in the same directory with the image.
     * The image choosing dialog could show thumbnails of the image formats
@@ -56,14 +55,12 @@ TODO
 #include "BufferView.h"
 #include "Converter.h"
 #include "Cursor.h"
-#include "support/debug.h"
 #include "DispatchResult.h"
 #include "ErrorList.h"
 #include "Exporter.h"
 #include "Format.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "support/gettext.h"
 #include "LaTeXFeatures.h"
 #include "Length.h"
 #include "Lexer.h"
@@ -76,9 +73,11 @@ TODO
 #include "frontends/alert.h"
 
 #include "support/convert.h"
+#include "support/debug.h"
 #include "support/docstream.h"
 #include "support/ExceptionMessage.h"
 #include "support/filetools.h"
+#include "support/gettext.h"
 #include "support/lyxlib.h"
 #include "support/lstrings.h"
 #include "support/os.h"
@@ -171,13 +170,9 @@ void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
                Buffer const & buffer = cur.buffer();
                InsetGraphicsParams p;
                InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, p);
-               // when embedding is enabled, change of embedding status leads to actions
-               if (!p.filename.empty() && buffer.embeddedFiles().enabled() ) {
+               if (!p.filename.empty()) {
                        try {
-                               if (p.filename.embedded())
-                                       p.filename.updateFromExternalFile(&buffer);
-                               else
-                                       p.filename.extract(&buffer);
+                               p.filename.enable(buffer.embedded(), &buffer);
                        } catch (ExceptionMessage const & message) {
                                Alert::error(message.title_, message.details_);
                                // do not set parameter if an error happens
@@ -221,25 +216,30 @@ bool InsetGraphics::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetGraphics::registerEmbeddedFiles(Buffer const &
-       EmbeddedFiles & files) const
+void InsetGraphics::registerEmbeddedFiles(Buffer const & buffer,
+       EmbeddedFileList & files) const
 {
-       files.registerFile(params().filename, this);
+       files.registerFile(params().filename, this, buffer);
 }
 
 
 void InsetGraphics::updateEmbeddedFile(Buffer const & buf,
        EmbeddedFile const & file)
 {
-       BOOST_ASSERT(buf.embeddedFiles().enabled());
-       params_.filename = file;
+       // when embedding is enabled, change of embedding status leads to actions
+       EmbeddedFile temp = file;
+       temp.enable(buf.embedded(), &buf);
+       // this will not be set if an exception is thorwn in enable()
+       params_.filename = temp;
+
        LYXERR(Debug::FILES, "Update InsetGraphic with File " 
                << params_.filename.toFilesystemEncoding() 
-               << ", embedding status: " << params_.filename.embedded());
+               << ", embedding status: " << params_.filename.embedded()
+               << ", enabled: " << params_.filename.enabled());
 }
 
 
-void InsetGraphics::edit(Cursor & cur, bool)
+void InsetGraphics::edit(Cursor & cur, bool, EntryDirection)
 {
        InsetGraphicsMailer(*this).showDialog(&cur.bv());
 }
@@ -279,16 +279,7 @@ void InsetGraphics::read(Buffer const & buf, Lexer & lex)
        else
                LYXERR(Debug::GRAPHICS, "Not a Graphics inset!");
 
-       // InsetGraphics is read, with filename in params_. We do not know if this file actually
-       // exists or is embedded so we need to get the 'availableFile' from buf.embeddedFiles()
-       if (buf.embeddedFiles().enabled()) {
-               EmbeddedFiles::EmbeddedFileList::const_iterator it = 
-                       buf.embeddedFiles().find(params_.filename.toFilesystemEncoding());
-               if (it != buf.embeddedFiles().end())
-                       // using available file, embedded or external, depending on file availability and
-                       // embedding status.
-                       params_.filename = *it;
-       }
+       params_.filename.enable(buf.embedded(), &buf);
        graphic_->update(params().as_grfxParams());
 }
 
@@ -577,7 +568,8 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
        if (params().filename.empty())
                return string();
 
-       string const orig_file = params().filename.absFilename();
+       string const orig_file = params().filename.availableFile().absFilename();
+       // this is for dryrun and display purposes, do not use latexFilename
        string const rel_file = params().filename.relFilename(buf.filePath());
 
        // previewing source code, no file copying or file format conversion
@@ -586,7 +578,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
 
        // temp_file will contain the file for LaTeX to act on if, for example,
        // we move it to a temp dir or uncompress it.
-       FileName temp_file = params().filename;
+       FileName temp_file = params().filename.availableFile();
 
        // The master buffer. This is useful when there are multiple levels
        // of include files
@@ -606,7 +598,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
 
        GraphicsCopyStatus status;
        boost::tie(status, temp_file) =
-                       copyToDirIfNeeded(params().filename, temp_path);
+                       copyToDirIfNeeded(params().filename.availableFile(), temp_path);
 
        if (status == FAILURE)
                return orig_file;
@@ -763,9 +755,6 @@ int InsetGraphics::latex(Buffer const & buf, odocstream & os,
        LYXERR(Debug::GRAPHICS, "insetgraphics::latex: Filename = "
                << params().filename.absFilename());
 
-       string const relative_file =
-               params().filename.relFilename(buf.filePath());
-
        bool const file_exists = !params().filename.empty()
                        && params().filename.isReadableFile();
        string const message = file_exists ?