]> git.lyx.org Git - features.git/commitdiff
add label/refs validation on pasting.
authorJürgen Spitzmüller <spitz@lyx.org>
Mon, 3 Mar 2008 09:35:26 +0000 (09:35 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Mon, 3 Mar 2008 09:35:26 +0000 (09:35 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23407 a592a061-630c-0410-9148-cb99ea01b6c8

src/CutAndPaste.cpp
src/insets/InsetCommand.h
src/insets/InsetLabel.cpp
src/insets/InsetLabel.h

index 77652d611d8aed9ed05f41dffa65f5d4ecc7bb41..7e980ddd6ab62aeb4f824e332263807e9b09b699 100644 (file)
@@ -37,6 +37,7 @@
 #include "Undo.h"
 
 #include "insets/InsetFlex.h"
+#include "insets/InsetCommand.h"
 #include "insets/InsetGraphics.h"
 #include "insets/InsetGraphicsParams.h"
 #include "insets/InsetTabular.h"
@@ -212,14 +213,36 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist,
        // A couple of insets store buffer references so need updating.
        insertion.swap(in.paragraphs());
 
-       ParIterator fpit = par_iterator_begin(in);
-       ParIterator fend = par_iterator_end(in);
+       InsetIterator const i_end = inset_iterator_end(in);
+
+       for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
+
+               it->setBuffer(const_cast<Buffer &>(buffer));
+
+               switch (it->lyxCode()) {
+               case LABEL_CODE: {
+                       // check for duplicates
+                       InsetCommand & lab = static_cast<InsetCommand &>(*it);
+                       docstring const oldname = lab.getParam("name");
+                       lab.update(oldname, false);
+                       docstring const newname = lab.getParam("name");
+                       if (oldname != newname) {
+                               // adapt the references
+                               for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) {
+                                       if (itt->lyxCode() == REF_CODE) {
+                                               InsetCommand & ref = dynamic_cast<InsetCommand &>(*itt);
+                                               if (ref.getParam("reference") == oldname)
+                                                       ref.setParam("reference", newname);
+                                       }
+                               }
+                       }
+                       break;
+               }
 
-       for (; fpit != fend; ++fpit) {
-               InsetList::const_iterator it = fpit->insetList().begin();
-               InsetList::const_iterator et = fpit->insetList().end();
-               for (; it != et; ++it)
-                               it->inset->setBuffer(const_cast<Buffer &>(buffer));
+               default:
+                       break; // nothing
+               }
        }
        insertion.swap(in.paragraphs());
 
index 96090bacb57b024a30a6d75c4a8a992304313c2c..96e523a3ce4118e1f1521adf4b37e1d5839620f7 100644 (file)
@@ -85,6 +85,8 @@ public:
        /// Whether this is a command this inset can represent.
        /// Not implemented here. Must be implemented in derived class.
        static bool isCompatibleCommand(std::string const & cmd);
+       /// update label and references. Currently used by InsetLabel.
+       virtual void update(docstring const &, bool) {};
 
 protected:
        ///
index 606474b5d586c4453900fe9c4ab7798389b6beb5..2f3f13d60e37b7a46be111b11523ab062c0c2053 100644 (file)
@@ -49,7 +49,7 @@ void InsetLabel::validate()
 }
 
 
-void InsetLabel::update(docstring const & new_label)
+void InsetLabel::update(docstring const & new_label, bool updaterefs)
 {
        docstring const old_label = getParam("name");
        docstring label = new_label;
@@ -68,11 +68,13 @@ void InsetLabel::update(docstring const & new_label)
 
        setParam("name", label);
 
-       Buffer::References const & refs = buffer().references(old_label);
-       Buffer::References::const_iterator it = refs.begin();
-       Buffer::References::const_iterator end = refs.end();
-       for (; it != end; ++it)
-               it->first->setParam("reference", label);
+       if (updaterefs) {
+               Buffer::References const & refs = buffer().references(old_label);
+               Buffer::References::const_iterator it = refs.begin();
+               Buffer::References::const_iterator end = refs.end();
+               for (; it != end; ++it)
+                       it->first->setParam("reference", label);
+       }
 
        // We need an update of the Buffer reference cache. This is achieved by
        // updateLabel().
index f051a38addc9c9eca300cf7bcdfce81fa26d88ce..7fe8a0c290134a29a8fdfed290df1629b8275d44 100644 (file)
@@ -53,14 +53,14 @@ public:
        void updateLabels(ParIterator const & it);
        ///
        void addToToc(ParConstIterator const &) const;
+       ///
+       void update(docstring const & new_label, bool updaterefs = true);
 protected:
        ///
        void doDispatch(Cursor & cur, FuncRequest & cmd);
 private:
        ///
        Inset * clone() const { return new InsetLabel(*this); }
-       ///
-       void update(docstring const & new_label);
 };