]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetGraphics.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[lyx.git] / src / insets / InsetGraphics.cpp
index 704f57f0536cc884e885038209110ad46afd1a86..9decdaaa80cc0b0ee08a5ee6bb54bb22d922b202 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Baruch Even
- * \author Herbert Voß
+ * \author Herbert Voß
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -54,12 +54,12 @@ TODO
 #include "Converter.h"
 #include "Cursor.h"
 #include "DispatchResult.h"
-#include "EmbeddedFiles.h"
 #include "ErrorList.h"
 #include "Exporter.h"
 #include "Format.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
+#include "InsetIterator.h"
 #include "LaTeXFeatures.h"
 #include "Length.h"
 #include "Lexer.h"
@@ -83,7 +83,6 @@ TODO
 #include "support/os.h"
 #include "support/Systemcall.h"
 
-#include <boost/bind.hpp>
 #include <boost/tuple/tuple.hpp>
 
 #include <algorithm>
@@ -102,8 +101,9 @@ namespace {
 /// Note that \p format may be unknown (i. e. an empty string)
 string findTargetFormat(string const & format, OutputParams const & runparams)
 {
-       // Are we using latex or pdflatex?
-       if (runparams.flavor == OutputParams::PDFLATEX) {
+       // Are we using latex or XeTeX/pdflatex?
+       if (runparams.flavor == OutputParams::PDFLATEX
+           || runparams.flavor == OutputParams::XETEX) {
                LYXERR(Debug::GRAPHICS, "findTargetFormat: PDF mode");
                Format const * const f = formats.getFormat(format);
                // Convert vector graphics to pdf
@@ -143,8 +143,10 @@ void readInsetGraphics(Lexer & lex, string const & bufpath,
                        finished = true;
                } else {
                        if (!params.Read(lex, token, bufpath))
-                               lyxerr << "Unknown token, " << token << ", skipping."
-                                       << endl;
+                               lyxerr << "Unknown token, "
+                                      << token
+                                      << ", skipping."
+                                      << endl;
                }
        }
 }
@@ -162,8 +164,7 @@ InsetGraphics::InsetGraphics(Buffer & buf)
 
 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
        : Inset(ig),
-         boost::signals::trackable(),
-               graphic_label(sgml::uniqueID(from_ascii("graph"))),
+         graphic_label(sgml::uniqueID(from_ascii("graph"))),
          graphic_(new RenderGraphic(*ig.graphic_, this))
 {
        setParams(ig.params());
@@ -179,30 +180,14 @@ Inset * InsetGraphics::clone() const
 InsetGraphics::~InsetGraphics()
 {
        hideDialogs("graphics", this);
-}
-
-
-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);
+       delete graphic_;
 }
 
 
 void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
-       case LFUN_GRAPHICS_EDIT: {
+       case LFUN_INSET_EDIT: {
                InsetGraphicsParams p = params();
                if (!cmd.argument().empty())
                        string2params(to_utf8(cmd.argument()), buffer(), p);
@@ -213,22 +198,22 @@ void InsetGraphics::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_INSET_MODIFY: {
                InsetGraphicsParams p;
                string2params(to_utf8(cmd.argument()), buffer(), p);
-               if (!p.filename.empty())
-                       setParams(p);
-               else
+               if (p.filename.empty()) {
                        cur.noUpdate();
+                       break;
+               }
+
+               setParams(p);
+               // if the inset is part of a graphics group, all the
+               // other members should be updated too.
+               if (!params_.groupId.empty())
+                       graphics::unifyGraphicsGroups(buffer(), 
+                                                     to_utf8(cmd.argument()));
                break;
        }
 
        case LFUN_INSET_DIALOG_UPDATE:
-               cur.bv().updateDialog("graphics", params2string(params(),
-cur.bv().buffer()));
-               break;
-
-       case LFUN_MOUSE_RELEASE:
-               if (!cur.selection() && cmd.button() == mouse_button::button1)
-                       cur.bv().showDialog("graphics", params2string(params(),
-cur.bv().buffer()), this);
+               cur.bv().updateDialog("graphics", params2string(params(), buffer()));
                break;
 
        default:
@@ -242,10 +227,10 @@ bool InsetGraphics::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
        switch (cmd.action) {
-       case LFUN_GRAPHICS_EDIT:
+       case LFUN_INSET_EDIT:
        case LFUN_INSET_MODIFY:
        case LFUN_INSET_DIALOG_UPDATE:
-               flag.enabled(true);
+               flag.setEnabled(true);
                return true;
 
        default:
@@ -254,25 +239,14 @@ bool InsetGraphics::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetGraphics::registerEmbeddedFiles(EmbeddedFileList & files) const
-{
-       files.registerFile(params().filename, this, buffer());
-}
-
-
-void InsetGraphics::updateEmbeddedFile(EmbeddedFile const & file)
+bool InsetGraphics::showInsetDialog(BufferView * bv) const
 {
-       // only properties of an embedded file can be changed here.
-       params_.filename = file;
+       bv->showDialog("graphics", params2string(params(), bv->buffer()),
+               const_cast<InsetGraphics *>(this));
+       return true;
 }
 
 
-void InsetGraphics::edit(Cursor & cur, bool, EntryDirection)
-{
-       cur.bv().showDialog("graphics", params2string(params(),
-cur.bv().buffer()), this);
-}
-
 
 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
 {
@@ -286,12 +260,6 @@ void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-Inset::EDITABLE InsetGraphics::editable() const
-{
-       return IS_EDITABLE;
-}
-
-
 void InsetGraphics::write(ostream & os) const
 {
        os << "Graphics\n";
@@ -301,14 +269,9 @@ void InsetGraphics::write(ostream & os) const
 
 void InsetGraphics::read(Lexer & lex)
 {
-       string const token = lex.getString();
-
-       if (token == "Graphics")
-               readInsetGraphics(lex, buffer().filePath(), params_);
-       else
-               LYXERR(Debug::GRAPHICS, "Not a Graphics inset!");
-
-       params_.filename.enable(buffer().embedded(), &buffer(), false);
+       lex.setContext("InsetGraphics::read");
+       //lex >> "Graphics";
+       readInsetGraphics(lex, buffer().filePath(), params_);
        graphic_->update(params().as_grfxParams());
 }
 
@@ -573,7 +536,7 @@ string InsetGraphics::prepareFile(OutputParams const & runparams) const
        if (params().filename.empty())
                return string();
 
-       string const orig_file = params().filename.availableFile().absFilename();
+       string const orig_file = params().filename.absFilename();
        // this is for dryrun and display purposes, do not use latexFilename
        string const rel_file = params().filename.relFilename(buffer().filePath());
 
@@ -583,7 +546,7 @@ string InsetGraphics::prepareFile(OutputParams const & runparams) const
 
        // 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.availableFile();
+       FileName temp_file = params().filename;
 
        // The master buffer. This is useful when there are multiple levels
        // of include files
@@ -603,7 +566,7 @@ string InsetGraphics::prepareFile(OutputParams const & runparams) const
 
        GraphicsCopyStatus status;
        boost::tie(status, temp_file) =
-                       copyToDirIfNeeded(params().filename.availableFile(), temp_path);
+                       copyToDirIfNeeded(params().filename, temp_path);
 
        if (status == FAILURE)
                return orig_file;
@@ -637,7 +600,8 @@ string InsetGraphics::prepareFile(OutputParams const & runparams) const
                        // noUnzip parameter meaningless.
                        LYXERR(Debug::GRAPHICS, "\tpass zipped file to LaTeX.");
 
-                       FileName const bb_orig_file = FileName(changeExtension(orig_file, "bb"));
+                       FileName const bb_orig_file =
+                               FileName(changeExtension(orig_file, "bb"));
                        if (runparams.nice) {
                                runparams.exportdata->addExternalFile(tex_format,
                                                bb_orig_file,
@@ -645,7 +609,8 @@ string InsetGraphics::prepareFile(OutputParams const & runparams) const
                        } else {
                                // LaTeX needs the bounding box file in the
                                // tmp dir
-                               FileName bb_file = FileName(changeExtension(temp_file.absFilename(), "bb"));
+                               FileName bb_file =
+                                       FileName(changeExtension(temp_file.absFilename(), "bb"));
                                boost::tie(status, bb_file) =
                                        copyFileIfNeeded(bb_orig_file, bb_file);
                                if (status == FAILURE)
@@ -693,15 +658,17 @@ string InsetGraphics::prepareFile(OutputParams const & runparams) const
        LYXERR(Debug::GRAPHICS, "\tthe orig file is: " << orig_file);
 
        if (from == to) {
-               if (!runparams.nice && getExtension(temp_file.absFilename()) != ext) {
+               if (!runparams.nice && !FileName(temp_file).hasExtension(ext)) {
                        // The LaTeX compiler will not be able to determine
                        // the file format from the extension, so we must
                        // change it.
-                       FileName const new_file = FileName(changeExtension(temp_file.absFilename(), ext));
+                       FileName const new_file = 
+                               FileName(changeExtension(temp_file.absFilename(), ext));
                        if (temp_file.moveTo(new_file)) {
                                temp_file = new_file;
                                output_file = changeExtension(output_file, ext);
-                               source_file = FileName(changeExtension(source_file.absFilename(), ext));
+                               source_file = 
+                                       FileName(changeExtension(source_file.absFilename(), ext));
                        } else {
                                LYXERR(Debug::GRAPHICS, "Could not rename file `"
                                        << temp_file << "' to `" << new_file << "'.");
@@ -897,7 +864,8 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
 
        if (features.runparams().nice) {
                Buffer const * masterBuffer = features.buffer().masterBuffer();
-               string const rel_file = removeExtension(params().filename.relFilename(masterBuffer->filePath()));
+               string const rel_file = removeExtension(
+                       params().filename.relFilename(masterBuffer->filePath()));
                if (contains(rel_file, "."))
                        features.require("lyxdot");
        }
@@ -935,15 +903,12 @@ void InsetGraphics::editGraphics(InsetGraphicsParams const & p,
 }
 
 
-void InsetGraphics::addToToc(ParConstIterator const & cpit) const
+void InsetGraphics::addToToc(DocIterator const & cpit)
 {
        TocBackend & backend = buffer().tocBackend();
 
-       docstring str = params_.filename.displayName();
-       if (params_.filename.embedded()) {
-               backend.toc("embedded").push_back(TocItem(cpit, 0, str));
-               str += _(" (embedded)");
-       }
+       //FIXME UNICODE
+       docstring const str = from_utf8(params_.filename.onlyFileName());
        backend.toc("graphics").push_back(TocItem(cpit, 0, str));
 }
 
@@ -961,17 +926,10 @@ void InsetGraphics::string2params(string const & in, Buffer const & buffer,
                return;
 
        istringstream data(in);
-       Lexer lex(0,0);
+       Lexer lex;
        lex.setStream(data);
-
-       string name;
-       lex >> name;
-       if (!lex || name != "graphics") {
-               LYXERR0("InsetGraphics::string2params(" << in << ")\n"
-                                         "Expected arg 1 to be \"graphics\"\n");
-               return;
-       }
-
+       lex.setContext("InsetGraphics::string2params");
+       lex >> "graphics";
        params = InsetGraphicsParams();
        readInsetGraphics(lex, buffer.filePath(), params);
 }
@@ -987,5 +945,97 @@ string InsetGraphics::params2string(InsetGraphicsParams const & params,
        return data.str();
 }
 
+namespace graphics {
+
+void getGraphicsGroups(Buffer const & b, set<string> & ids)
+{
+       Inset & inset = b.inset();
+       InsetIterator it  = inset_iterator_begin(inset);
+       InsetIterator const end = inset_iterator_end(inset);
+       for (; it != end; ++it)
+               if (it->lyxCode() == GRAPHICS_CODE) {
+                       InsetGraphics & ins = static_cast<InsetGraphics &>(*it);
+                       InsetGraphicsParams inspar = ins.getParams();
+                       if (!inspar.groupId.empty())
+                               ids.insert(inspar.groupId);
+               }
+}
+
+
+int countGroupMembers(Buffer const & b, string const & groupId)
+{
+       int n = 0;
+       if (groupId.empty())
+               return n;
+       Inset & inset = b.inset();
+       InsetIterator it = inset_iterator_begin(inset);
+       InsetIterator const end = inset_iterator_end(inset);
+       for (; it != end; ++it)
+               if (it->lyxCode() == GRAPHICS_CODE) {
+                       InsetGraphics & ins = static_cast<InsetGraphics &>(*it);
+                       if (ins.getParams().groupId == groupId)
+                               ++n;
+               }
+       return n;
+}
+
+
+string getGroupParams(Buffer const & b, string const & groupId)
+{
+       if (groupId.empty())
+               return string();
+       Inset & inset = b.inset();
+       InsetIterator it  = inset_iterator_begin(inset);
+       InsetIterator const end = inset_iterator_end(inset);
+       for (; it != end; ++it)
+               if (it->lyxCode() == GRAPHICS_CODE) {
+                       InsetGraphics & ins = static_cast<InsetGraphics &>(*it);
+                       InsetGraphicsParams inspar = ins.getParams();
+                       if (inspar.groupId == groupId) {
+                               InsetGraphicsParams tmp = inspar;
+                               tmp.filename.erase();
+                               return InsetGraphics::params2string(tmp, b);
+                       }
+               }
+       return string();
+}
+
+
+void unifyGraphicsGroups(Buffer & b, string const & argument)
+{
+       InsetGraphicsParams params;
+       InsetGraphics::string2params(argument, b, params);
+
+       b.undo().beginUndoGroup();
+       Inset & inset = b.inset();
+       InsetIterator it  = inset_iterator_begin(inset);
+       InsetIterator const end = inset_iterator_end(inset);
+       for (; it != end; ++it) {
+               if (it->lyxCode() == GRAPHICS_CODE) {
+                       InsetGraphics & ins = static_cast<InsetGraphics &>(*it);
+                       InsetGraphicsParams inspar = ins.getParams();
+                       if (params.groupId == inspar.groupId) {
+                               b.undo().recordUndo(it);
+                               params.filename = inspar.filename;
+                               ins.setParams(params);
+                       }
+               }
+       }
+       b.undo().endUndoGroup();
+}
+
+
+InsetGraphics * getCurrentGraphicsInset(Cursor const & cur)
+{
+       Inset * instmp = &cur.inset();
+       if (instmp->lyxCode() != GRAPHICS_CODE)
+               instmp = cur.nextInset();
+       if (!instmp || instmp->lyxCode() != GRAPHICS_CODE)
+               return 0;
+
+       return static_cast<InsetGraphics *>(instmp);
+}
+
+} // namespace graphics
 
 } // namespace lyx