]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetGraphics.cpp
Cleanup mouse/selection/context-menu interactions.
[lyx.git] / src / insets / InsetGraphics.cpp
index 8e592b8e3b0b7f533d8f47f5cfc9a73d007d70a6..eb98501ee81e56dc2c58c61dad112a80012dd6e5 100644 (file)
@@ -54,6 +54,7 @@ TODO
 #include "Converter.h"
 #include "Cursor.h"
 #include "DispatchResult.h"
+#include "EmbeddedFiles.h"
 #include "ErrorList.h"
 #include "Exporter.h"
 #include "Format.h"
@@ -66,7 +67,7 @@ TODO
 #include "Mover.h"
 #include "OutputParams.h"
 #include "sgml.h"
-#include "EmbeddedFiles.h"
+#include "TocBackend.h"
 
 #include "frontends/alert.h"
 
@@ -153,12 +154,30 @@ 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: {
-               InsetGraphicsParams p;
-               InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
+               InsetGraphicsParams p = params();
+               if (!cmd.argument().empty())
+                       InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
                editGraphics(p, buffer());
                break;
        }
@@ -166,16 +185,9 @@ void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_INSET_MODIFY: {
                InsetGraphicsParams p;
                InsetGraphicsMailer::string2params(to_utf8(cmd.argument()), buffer(), p);
-               if (!p.filename.empty()) {
-                       try {
-                               p.filename.enable(buffer().embedded(), &buffer());
-                       } catch (ExceptionMessage const & message) {
-                               Alert::error(message.title_, message.details_);
-                               // do not set parameter if an error happens
-                               break;
-                       }
+               if (!p.filename.empty())
                        setParams(p);
-               else
+               else
                        cur.noUpdate();
                break;
        }
@@ -185,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;
 
@@ -220,16 +232,8 @@ void InsetGraphics::registerEmbeddedFiles(EmbeddedFileList & files) const
 
 void InsetGraphics::updateEmbeddedFile(EmbeddedFile const & file)
 {
-       // when embedding is enabled, change of embedding status leads to actions
-       EmbeddedFile temp = file;
-       temp.enable(buffer().embedded(), &buffer());
-       // 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()
-               << ", enabled: " << params_.filename.enabled());
+       // only properties of an embedded file can be changed here.
+       params_.filename = file;
 }
 
 
@@ -273,7 +277,7 @@ void InsetGraphics::read(Lexer & lex)
        else
                LYXERR(Debug::GRAPHICS, "Not a Graphics inset!");
 
-       params_.filename.enable(buffer().embedded(), &buffer());
+       params_.filename.enable(buffer().embedded(), &buffer(), false);
        graphic_->update(params().as_grfxParams());
 }
 
@@ -923,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)