]> git.lyx.org Git - lyx.git/blobdiff - src/CutAndPaste.C
bug 183
[lyx.git] / src / CutAndPaste.C
index 36af28fbc7bd6a0022348748a1f05e780526113c..da7a4ae9bd2a05b49eec365185033b6ef72085be 100644 (file)
@@ -9,20 +9,26 @@
 
 #include <config.h>
 
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
 #include "CutAndPaste.h"
 #include "BufferView.h"
 #include "buffer.h"
 #include "paragraph.h"
-#include "insets/inseterror.h"
 #include "lyx_gui_misc.h"
 #include "lyxcursor.h"
 #include "gettext.h"
+#include "iterators.h"
+#include "lyxtextclasslist.h"
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include "insets/inseterror.h"
 
 using std::pair;
+using lyx::pos_type;
+using lyx::layout_type;
+using lyx::textclass_type;
 
 extern BufferView * current_view;
 
@@ -48,7 +54,7 @@ extern BufferView * current_view;
 namespace {
 
 Paragraph * buf = 0;
-LyXTextClassList::size_type textclass = 0;
+textclass_type textclass = 0;
 
 // for now here this should be in another Cut&Paste Class!
 // Jürgen, I moved this out of CutAndPaste since it does not operate on any
@@ -73,44 +79,50 @@ void DeleteBuffer()
 
 
 bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
-                              int start, int & end, char tc, bool doclear)
+                               int start, int & end, char tc, bool doclear,
+                                                          bool realcut)
 {
        if (!startpar || (start > startpar->size()))
                return false;
        
-       DeleteBuffer();
+       if (realcut)
+               DeleteBuffer();
        
        textclass = tc;
        
-       if (!(*endpar) ||
-           startpar == (*endpar)) {
+       if (!(*endpar) || startpar == (*endpar)) {
                // only within one paragraph
-               buf = new Paragraph;
-               Paragraph::size_type i = start;
+               if (realcut)
+                       buf = new Paragraph;
+               pos_type i = start;
                if (end > startpar->size())
                        end = startpar->size();
                for (; i < end; ++i) {
-                       startpar->copyIntoMinibuffer(*current_view->buffer(),
-                                                    start);
+                       if (realcut)
+                               startpar->copyIntoMinibuffer(*current_view->buffer(),
+                                                            start);
                        startpar->erase(start);
-                       
-                       buf->insertFromMinibuffer(buf->size());
+                       if (realcut)
+                               buf->insertFromMinibuffer(buf->size());
                }
                end = start - 1;
        } else {
                // more than one paragraph
                (*endpar)->breakParagraphConservative(current_view->buffer()->params,
-                                                     end);
+                                                     end);
                *endpar = (*endpar)->next();
                end = 0;
                
                startpar->breakParagraphConservative(current_view->buffer()->params,
-                                                    start);
+                                                    start);
                
                // store the selection
-               buf = startpar->next();
-               
-               buf->previous(0);
+               if (realcut) {
+                       buf = startpar->next();
+                       buf->previous(0);
+               } else {
+                       startpar->next()->previous(0);
+               }
                (*endpar)->previous()->next(0);
                
                // cut the selection
@@ -119,7 +131,8 @@ bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
                (*endpar)->previous(startpar);
                
                // the cut selection should begin with standard layout
-               buf->clear(); 
+               if (realcut)
+                       buf->clear(); 
                
                // paste the paragraphs again, if possible
                if (doclear)
@@ -129,6 +142,12 @@ bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
                        startpar->pasteParagraph(current_view->buffer()->params);
                        (*endpar) = startpar; // this because endpar gets deleted here!
                }
+               // this paragraph's are of noone's owner!
+               Paragraph * p = buf;
+               while (p) {
+                       p->setInsetOwner(0);
+                       p = p->next();
+               }
        }
        return true;
 }
@@ -144,11 +163,10 @@ bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
        
        textclass = tc;
        
-       if (!endpar ||
-           startpar == endpar) {
+       if (!endpar || startpar == endpar) {
                // only within one paragraph
                buf = new Paragraph;
-               Paragraph::size_type i = start;
+               pos_type i = start;
                if (end > startpar->size())
                        end = startpar->size();
                for (; i < end; ++i) {
@@ -159,20 +177,20 @@ bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
                // copy more than one paragraph
                // clone the paragraphs within the selection
                Paragraph * tmppar = startpar;
-               buf = new Paragraph(*tmppar);
+               buf = new Paragraph(*tmppar, false);
                Paragraph * tmppar2 = buf;
                
                while (tmppar != endpar
                       && tmppar->next()) {
                        tmppar = tmppar->next();
-                       tmppar2->next(new Paragraph(*tmppar));
+                       tmppar2->next(new Paragraph(*tmppar, false));
                        tmppar2->next()->previous(tmppar2);
                        tmppar2 = tmppar2->next();
                }
                tmppar2->next(0);
                
                // the buf paragraph is too big
-               Paragraph::size_type tmpi2 = start;
+               pos_type tmpi2 = start;
                for (; tmpi2; --tmpi2)
                        buf->erase(0);
                
@@ -181,13 +199,19 @@ bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
                while (tmppar2->size() > tmpi2) {
                        tmppar2->erase(tmppar2->size() - 1);
                }
+               // this paragraph's are of noone's owner!
+               tmppar = buf;
+               while (tmppar) {
+                       tmppar->setInsetOwner(0);
+                       tmppar = tmppar->next();
+               }
        }
        return true;
 }
 
 
 bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
-                                int & pos, char tc)
+                                 int & pos, char tc)
 {
        if (!checkPastePossible(*par))
                return false;
@@ -202,7 +226,7 @@ bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
        // There are two cases: cutbuffer only one paragraph or many
        if (!buf->next()) {
                // only within a paragraph
-               Paragraph * tmpbuf = new Paragraph(*buf);
+               Paragraph * tmpbuf = new Paragraph(*buf, false);
                
                // Some provisions should be done here for checking
                // if we are inserting at the beginning of a
@@ -232,18 +256,42 @@ bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
                
                // make a copy of the simple cut_buffer
                Paragraph * tmpbuf = buf;
-               Paragraph * simple_cut_clone = new Paragraph(*tmpbuf);
+               Paragraph * simple_cut_clone = new Paragraph(*tmpbuf, false);
                Paragraph * tmpbuf2 = simple_cut_clone;
                
                while (tmpbuf->next()) {
                        tmpbuf = tmpbuf->next();
-                       tmpbuf2->next(new Paragraph(*tmpbuf));
+                       tmpbuf2->next(new Paragraph(*tmpbuf, false));
                        tmpbuf2->next()->previous(tmpbuf2);
                        tmpbuf2 = tmpbuf2->next();
                }
+
+               // now remove all out of the buffer which is NOT allowed in the
+               // new environment and set also another font if that is required
+               tmpbuf = buf;
+               while(tmpbuf) {
+                       for(pos_type i = 0; i < tmpbuf->size(); ++i) {
+                               if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
+                                       if (!(*par)->insetAllowed(tmpbuf->getInset(i)->lyxCode()))
+                                       {
+                                               tmpbuf->erase(i--);
+                                       }
+                               } else {
+                                       LyXFont f1 = tmpbuf->getFont(current_view->buffer()->params,i);
+                                       LyXFont f2 = f1;
+                                       if (!(*par)->checkInsertChar(f1)) {
+                                               tmpbuf->erase(i--);
+                                       } else if (f1 != f2) {
+                                               tmpbuf->setFont(i, f1);
+                                       }
+                               }
+                       }
+                       tmpbuf = tmpbuf->next();
+               }
                
                // make sure there is no class difference
-               SwitchLayoutsBetweenClasses(textclass, tc, buf);
+               SwitchLayoutsBetweenClasses(textclass, tc, buf,
+                                           current_view->buffer()->params);
                
                // make the buf exactly the same layout than
                // the cursor paragraph
@@ -308,7 +356,7 @@ int CutAndPaste::nrOfParagraphs()
        
        int n = 1;
        Paragraph * tmppar = buf;
-       while(tmppar->next()) {
+       while (tmppar->next()) {
                ++n;
                tmppar = tmppar->next();
        }
@@ -316,24 +364,26 @@ int CutAndPaste::nrOfParagraphs()
 }
 
 
-int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
-                                            LyXTextClassList::size_type c2,
-                                            Paragraph * par)
+int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
+                                            textclass_type c2,
+                                            Paragraph * par,
+                                            BufferParams const & bparams)
 {
        int ret = 0;
        if (!par || c1 == c2)
                return ret;
-       
-       while (par) {
-               string const name = textclasslist.NameOfLayout(c1,
-                                                              par->layout);
+
+       ParIterator end = ParIterator();
+       for (ParIterator it = ParIterator(par); it != end; ++it) {
+               par = *it;
+               string const name = textclasslist.NameOfLayout(c1, par->layout);
                int lay = 0;
-               pair<bool, LyXTextClass::LayoutList::size_type> pp =
+               pair<bool, layout_type> pp =
                        textclasslist.NumberOfLayout(c2, name);
                if (pp.first) {
                        lay = pp.second;
-               } else { // layout not found
-                       // use default layout "Standard" (0)
+               } else {
+                       // not found: use default layout "Standard" (0)
                        lay = 0;
                }
                par->layout = lay;
@@ -347,9 +397,10 @@ int CutAndPaste::SwitchLayoutsBetweenClasses(LyXTextClassList::size_type c1,
                                + textclasslist.NameOfClass(c1) + _(" to ")
                                + textclasslist.NameOfClass(c2);
                        InsetError * new_inset = new InsetError(s);
-                       par->insertInset(0, new_inset);
+                       par->insertInset(0, new_inset,
+                                        LyXFont(LyXFont::ALL_INHERIT,
+                                                bparams.language));
                }
-               par = par->next();
        }
        return ret;
 }