]> git.lyx.org Git - lyx.git/blobdiff - src/CutAndPaste.C
ws changes and some cruft removal, slight cleanup
[lyx.git] / src / CutAndPaste.C
index 2eab33a685ffd3c3e147d44ca979480ee9ffbce6..df12c4de8d81a241a7044b0a11df46c232bb1ba8 100644 (file)
 #endif
 
 #include "CutAndPaste.h"
+//#include "debug.h"
 #include "BufferView.h"
 #include "buffer.h"
 #include "paragraph.h"
+#include "ParagraphParameters.h"
 #include "lyxcursor.h"
 #include "gettext.h"
 #include "iterators.h"
@@ -26,7 +28,6 @@
 
 using std::pair;
 using lyx::pos_type;
-using lyx::layout_type;
 using lyx::textclass_type;
 
 extern BufferView * current_view;
@@ -91,8 +92,10 @@ bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
        
        if (!(*endpar) || startpar == (*endpar)) {
                // only within one paragraph
-               if (realcut)
+               if (realcut) {
                        buf = new Paragraph;
+                       buf->layout(startpar->layout());
+               }
                pos_type i = start;
                if (end > startpar->size())
                        end = startpar->size();
@@ -130,9 +133,12 @@ bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
                (*endpar)->previous(startpar);
                
                // the cut selection should begin with standard layout
-               if (realcut)
-                       buf->clear(); 
-               
+               if (realcut) {
+                       buf->params().clear();
+                       buf->bibkey = 0;
+                       buf->layout(textclasslist[current_view->buffer()->params.textclass].defaultLayoutName());
+               }
+
                // paste the paragraphs again, if possible
                if (doclear)
                        startpar->next()->stripLeadingSpaces(textclass);
@@ -153,7 +159,7 @@ bool CutAndPaste::cutSelection(Paragraph * startpar, Paragraph ** endpar,
 
 
 bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
-                               int start, int end, char tc)
+                                int start, int end, char tc)
 {
        if (!startpar || (start > startpar->size()))
                return false;
@@ -165,6 +171,7 @@ bool CutAndPaste::copySelection(Paragraph * startpar, Paragraph * endpar,
        if (!endpar || startpar == endpar) {
                // only within one paragraph
                buf = new Paragraph;
+               buf->layout(startpar->layout());
                pos_type i = start;
                if (end > startpar->size())
                        end = startpar->size();
@@ -271,7 +278,26 @@ bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
                // 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;
+               int depth_delta = (*par)->params().depth() - tmpbuf->params().depth();
+               // temporary set *par as previous of tmpbuf as we might have to realize
+               // the font.
+               tmpbuf->previous(*par);
+               Paragraph::depth_type max_depth = (*par)->getMaxDepthAfter(current_view->buffer());
                while(tmpbuf) {
+                       // if we have a negative jump so that the depth would go below
+                       // 0 depth then we have to redo the delta to this new max depth
+                       // level so that subsequent paragraphs are aligned correctly to
+                       // this paragraph at level 0.
+                       if ((static_cast<int>(tmpbuf->params().depth()) + depth_delta) < 0)
+                               depth_delta = 0;
+                       // set the right depth so that we are not too deep or shallow.
+                       tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
+                       if (tmpbuf->params().depth() > max_depth)
+                               tmpbuf->params().depth(max_depth);
+                       // only set this from the 2nd on as the 2nd depends for maxDepth
+                       // still on *par
+                       if (tmpbuf->previous() != (*par))
+                               max_depth = tmpbuf->getMaxDepthAfter(current_view->buffer());
                        // set the inset owner of this paragraph
                        tmpbuf->setInsetOwner((*par)->inInset());
                        for(pos_type i = 0; i < tmpbuf->size(); ++i) {
@@ -292,10 +318,12 @@ bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
                        }
                        tmpbuf = tmpbuf->next();
                }
+               // now reset it to 0
+               buf->previous(0);
                
                // make sure there is no class difference
                SwitchLayoutsBetweenClasses(textclass, tc, buf,
-                                           current_view->buffer()->params);
+                                           current_view->buffer()->params);
                
                // make the buf exactly the same layout than
                // the cursor paragraph
@@ -312,7 +340,7 @@ bool CutAndPaste::pasteSelection(Paragraph ** par, Paragraph ** endpar,
                // if necessary
                if (((*par)->size() > pos) || !(*par)->next()) {
                        (*par)->breakParagraphConservative(current_view->buffer()->params,
-                                                          pos);
+                                                          pos);
                        paste_the_end = true;
                }
                // set the end for redoing later
@@ -380,26 +408,28 @@ int CutAndPaste::SwitchLayoutsBetweenClasses(textclass_type c1,
        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, layout_type> pp =
-                       textclasslist.NumberOfLayout(c2, name);
-               if (pp.first) {
-                       lay = pp.second;
+               string const name = par->layout();
+               LyXTextClass const & tclass = textclasslist[c2];
+               
+               bool hasLayout = tclass.hasLayout(name);
+               
+               string lay = tclass.defaultLayoutName();
+               if (hasLayout) {
+                       lay = name;
                } else {
-                       // not found: use default layout "Standard" (0)
-                       lay = 0;
+                       // not found: use default layout
+                       lay = tclass.defaultLayoutName();
                }
-               par->layout = lay;
+               par->layout(lay);
                
-               if (name != textclasslist.NameOfLayout(c2, par->layout)) {
+               if (name != par->layout()) {
                        ++ret;
                        string const s = _("Layout had to be changed from\n")
                                + name + _(" to ")
-                               + textclasslist.NameOfLayout(c2, par->layout)
+                               + par->layout()
                                + _("\nbecause of class conversion from\n")
-                               + textclasslist.NameOfClass(c1) + _(" to ")
-                               + textclasslist.NameOfClass(c2);
+                               + textclasslist[c1].name() + _(" to ")
+                               + textclasslist[c2].name();
                        InsetError * new_inset = new InsetError(s);
                        par->insertInset(0, new_inset,
                                         LyXFont(LyXFont::ALL_INHERIT,