]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetGraphics.cpp
Cleanup mouse/selection/context-menu interactions.
[lyx.git] / src / insets / InsetGraphics.cpp
index 4368ffb6f9622e0fa6652831be0d563bc00bf5ab..eb98501ee81e56dc2c58c61dad112a80012dd6e5 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
@@ -23,8 +22,6 @@ TODO
       filename.
     * Add support for the 'picins' package.
     * Add support for the 'picinpar' package.
-    * Improve support for 'subfigure' - Allow to set the various options
-      that are possible.
 */
 
 /* NOTES:
@@ -56,14 +53,13 @@ TODO
 #include "BufferView.h"
 #include "Converter.h"
 #include "Cursor.h"
-#include "support/debug.h"
 #include "DispatchResult.h"
+#include "EmbeddedFiles.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"
@@ -71,14 +67,16 @@ TODO
 #include "Mover.h"
 #include "OutputParams.h"
 #include "sgml.h"
-#include "EmbeddedFiles.h"
+#include "TocBackend.h"
 
 #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"
@@ -156,35 +154,40 @@ InsetGraphics::~InsetGraphics()
 }
 
 
+void InsetGraphics::setBuffer(Buffer & buffer)
+{
+       if (buffer_) {
+               try {
+                       // a file may not be copied successfully when, e.g. buffer_
+                       // has already been closed.
+                       params_.filename = params_.filename.copyTo(&buffer);
+               } catch (ExceptionMessage const & message) {
+                       Alert::error(message.title_, message.details_);
+                       // failed to embed
+                       params_.filename.setEmbed(false);
+               }
+       }
+       Inset::setBuffer(buffer);
+}
+
+
 void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
        case LFUN_GRAPHICS_EDIT: {
-               Buffer const & buffer = cur.bv().buffer();
-               InsetGraphicsParams p;
-               InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer, p);
-               editGraphics(p, buffer);
+               InsetGraphicsParams p = params();
+               if (!cmd.argument().empty())
+                       InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
+               editGraphics(p, buffer());
                break;
        }
 
        case LFUN_INSET_MODIFY: {
-               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() ) {
-                       try {
-                               if (p.filename.embedded())
-                                       p.filename.updateFromExternalFile(&buffer);
-                               else
-                                       p.filename.extract(&buffer);
-                       } catch (ExceptionMessage const & message) {
-                               Alert::error(message.title_, message.details_);
-                               // do not set parameter if an error happens
-                               break;
-                       }
+               InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
+               if (!p.filename.empty())
                        setParams(p);
-               else
+               else
                        cur.noUpdate();
                break;
        }
@@ -194,7 +197,7 @@ void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_MOUSE_RELEASE:
-               if (!cur.selection())
+               if (!cur.selection() && cmd.button() == mouse_button::button1)
                        InsetGraphicsMailer(*this).showDialog(&cur.bv());
                break;
 
@@ -221,25 +224,20 @@ bool InsetGraphics::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetGraphics::registerEmbeddedFiles(Buffer const &, 
-       EmbeddedFiles & files) const
+void InsetGraphics::registerEmbeddedFiles(EmbeddedFileList & files) const
 {
-       files.registerFile(params().filename, this);
+       files.registerFile(params().filename, this, buffer());
 }
 
 
-void InsetGraphics::updateEmbeddedFile(Buffer const & buf,
-       EmbeddedFile const & file)
+void InsetGraphics::updateEmbeddedFile(EmbeddedFile const & file)
 {
-       BOOST_ASSERT(buf.embeddedFiles().enabled());
+       // only properties of an embedded file can be changed here.
        params_.filename = file;
-       LYXERR(Debug::FILES, "Update InsetGraphic with File " 
-               << params_.filename.toFilesystemEncoding() 
-               << ", embedding status: " << params_.filename.embedded());
 }
 
 
-void InsetGraphics::edit(Cursor & cur, bool)
+void InsetGraphics::edit(Cursor & cur, bool, EntryDirection)
 {
        InsetGraphicsMailer(*this).showDialog(&cur.bv());
 }
@@ -263,32 +261,23 @@ Inset::EDITABLE InsetGraphics::editable() const
 }
 
 
-void InsetGraphics::write(Buffer const & buf, ostream & os) const
+void InsetGraphics::write(ostream & os) const
 {
        os << "Graphics\n";
-       params().Write(os, buf);
+       params().Write(os, buffer());
 }
 
 
-void InsetGraphics::read(Buffer const & buf, Lexer & lex)
+void InsetGraphics::read(Lexer & lex)
 {
        string const token = lex.getString();
 
        if (token == "Graphics")
-               readInsetGraphics(lex, buf.filePath());
+               readInsetGraphics(lex, buffer().filePath());
        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(buffer().embedded(), &buffer(), false);
        graphic_->update(params().as_grfxParams());
 }
 
@@ -570,15 +559,15 @@ string const stripExtensionIfPossible(string const & file, string const & to, bo
 } // namespace anon
 
 
-string const InsetGraphics::prepareFile(Buffer const & buf,
-                                       OutputParams const & runparams) const
+string InsetGraphics::prepareFile(OutputParams const & runparams) const
 {
        // The following code depends on non-empty filenames
        if (params().filename.empty())
                return string();
 
-       string const orig_file = params().filename.absFilename();
-       string const rel_file = params().filename.relFilename(buf.filePath());
+       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(buffer().filePath());
 
        // previewing source code, no file copying or file format conversion
        if (runparams.dryrun)
@@ -586,11 +575,11 @@ 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
-       Buffer const * masterBuffer = buf.masterBuffer();
+       Buffer const * masterBuffer = buffer().masterBuffer();
 
        // Return the output name if we are inside a comment or the file does
        // not exist.
@@ -606,7 +595,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;
@@ -742,7 +731,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
 
        // FIXME (Abdel 12/08/06): Is there a need to show these errors?
        ErrorList el;
-       if (theConverters().convert(&buf, temp_file, to_file, params().filename,
+       if (theConverters().convert(&buffer(), temp_file, to_file, params().filename,
                               from, to, el,
                               Converters::try_default | Converters::try_cache)) {
                runparams.exportdata->addExternalFile(tex_format,
@@ -755,7 +744,7 @@ string const InsetGraphics::prepareFile(Buffer const & buf,
 }
 
 
-int InsetGraphics::latex(Buffer const & buf, odocstream & os,
+int InsetGraphics::latex(odocstream & os,
                         OutputParams const & runparams) const
 {
        // If there is no file specified or not existing,
@@ -763,9 +752,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 ?
@@ -780,13 +766,6 @@ int InsetGraphics::latex(Buffer const & buf, odocstream & os,
        // after the actual includegraphics command.
        string before;
        string after;
-       // Do we want subcaptions?
-       if (params().subcaption) {
-               if (runparams.moving_arg)
-                       before += "\\protect";
-               before += "\\subfigure[" + params().subcaptionText + "]{";
-               after = '}';
-       }
 
        if (runparams.moving_arg)
                before += "\\protect";
@@ -809,7 +788,7 @@ int InsetGraphics::latex(Buffer const & buf, odocstream & os,
        // Convert the file if necessary.
        // Remove the extension so LaTeX will use whatever is appropriate
        // (when there are several versions in different formats)
-       latex_str += prepareFile(buf, runparams);
+       latex_str += prepareFile(runparams);
        latex_str += '}' + after;
        // FIXME UNICODE
        os << from_utf8(latex_str);
@@ -820,8 +799,7 @@ int InsetGraphics::latex(Buffer const & buf, odocstream & os,
 }
 
 
-int InsetGraphics::plaintext(Buffer const & buf, odocstream & os,
-                            OutputParams const &) const
+int InsetGraphics::plaintext(odocstream & os, OutputParams const &) const
 {
        // No graphics in ascii output. Possible to use gifscii to convert
        // images to ascii approximation.
@@ -831,7 +809,7 @@ int InsetGraphics::plaintext(Buffer const & buf, odocstream & os,
        // FIXME UNICODE
        // FIXME: We have no idea what the encoding of the filename is
 
-       docstring const str = bformat(buf.B_("Graphics file: %1$s"),
+       docstring const str = bformat(buffer().B_("Graphics file: %1$s"),
                                      from_utf8(params().filename.absFilename()));
        os << '<' << str << '>';
 
@@ -871,7 +849,7 @@ static int writeImageObject(char const * format, odocstream & os,
 // For explanation on inserting graphics into DocBook checkout:
 // http://en.tldp.org/LDP/LDP-Author-Guide/html/inserting-pictures.html
 // See also the docbook guide at http://www.docbook.org/
-int InsetGraphics::docbook(Buffer const &, odocstream & os,
+int InsetGraphics::docbook(odocstream & os,
                           OutputParams const & runparams) const
 {
        // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
@@ -915,9 +893,6 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
                if (contains(rel_file, "."))
                        features.require("lyxdot");
        }
-
-       if (params().subcaption)
-               features.require("subfigure");
 }
 
 
@@ -952,6 +927,25 @@ void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
 }
 
 
+void InsetGraphics::addToToc(ParConstIterator const & cpit) const
+{
+       TocBackend & backend = buffer().tocBackend();
+
+       docstring str = params_.filename.displayName();
+       if (params_.filename.embedded()) {
+               backend.toc("embedded").push_back(TocItem(cpit, 0, str));
+               str += _(" (embedded)");
+       }
+       backend.toc("graphics").push_back(TocItem(cpit, 0, str));
+}
+
+
+docstring InsetGraphics::contextMenu(BufferView const &, int, int) const
+{
+       return from_ascii("context-graphics");
+}
+
+
 string const InsetGraphicsMailer::name_("graphics");
 
 InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)