]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
Add empty InsetLayout for undefined cases. Should avoid possible bugs caused by empty...
[lyx.git] / src / Text.cpp
index 12ed1b85d9ee12c8d6de915f50faaea6cb7af602..110bbe612fc9a49840edfcceb662d11c2ddd1fff 100644 (file)
@@ -47,9 +47,6 @@
 #include "VSpace.h"
 #include "WordLangTuple.h"
 
-#include "frontends/FontMetrics.h"
-#include "frontends/Painter.h"
-
 #include "insets/InsetText.h"
 #include "insets/InsetBibitem.h"
 #include "insets/InsetCaption.h"
@@ -81,8 +78,6 @@ namespace lyx {
 using cap::cutSelection;
 using cap::pasteParagraphList;
 
-using frontend::FontMetrics;
-
 namespace {
 
 void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
@@ -103,8 +98,20 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
 
                TextClass const & tclass = bp.getTextClass();
 
-               if (layoutname.empty()) {
+               if (layoutname.empty())
                        layoutname = tclass.defaultLayoutName();
+
+               if (par.forceEmptyLayout()) {
+                       // in this case only the empty layout is allowed
+                       layoutname = tclass.emptyLayoutName();
+               } else if (par.useEmptyLayout()) {
+                       // in this case, default layout maps to empty layout 
+                       if (layoutname == tclass.defaultLayoutName())
+                               layoutname = tclass.emptyLayoutName();
+               } else { 
+                       // otherwise, the empty layout maps to the default
+                       if (layoutname == tclass.emptyLayoutName())
+                               layoutname = tclass.defaultLayoutName();
                }
 
                bool hasLayout = tclass.hasLayout(layoutname);
@@ -113,7 +120,9 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                        errorList.push_back(ErrorItem(_("Unknown layout"),
                        bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
                        layoutname, from_utf8(tclass.name())), par.id(), 0, par.size()));
-                       layoutname = tclass.defaultLayoutName();
+                       layoutname = par.useEmptyLayout() ? 
+                                       tclass.emptyLayoutName() :
+                                       tclass.defaultLayoutName();
                }
 
                par.layout(bp.getTextClass()[layoutname]);
@@ -388,9 +397,11 @@ void Text::breakParagraph(Cursor & cur, bool inverse_logic)
        if (sensitive) {
                if (cur.pos() == 0)
                        // set to standard-layout
+               //FIXME Check if this should be emptyLayout() in some cases
                        pars_[cpit].applyLayout(tclass.defaultLayout());
                else
                        // set to standard-layout
+                       //FIXME Check if this should be emptyLayout() in some cases
                        pars_[next_par].applyLayout(tclass.defaultLayout());
        }
 
@@ -543,7 +554,7 @@ void Text::insertChar(Cursor & cur, char_type c)
        }
 
        par.insertChar(cur.pos(), c, cur.current_font, cur.buffer().params().trackChanges);
-       checkBufferStructure(cur.buffer(), cur);
+       cur.checkBufferStructure();
 
 //             cur.updateFlags(Update::Force);
        setCursor(cur.top(), cur.pit(), cur.pos() + 1);
@@ -795,7 +806,7 @@ void Text::deleteWordForward(Cursor & cur)
                cursorForwardOneWord(cur);
                cur.setSelection();
                cutSelection(cur, true, false);
-               checkBufferStructure(cur.buffer(), cur);
+               cur.checkBufferStructure();
        }
 }
 
@@ -811,7 +822,7 @@ void Text::deleteWordBackward(Cursor & cur)
                cursorBackwardOneWord(cur);
                cur.setSelection();
                cutSelection(cur, true, false);
-               checkBufferStructure(cur.buffer(), cur);
+               cur.checkBufferStructure();
        }
 }
 
@@ -823,9 +834,11 @@ void Text::changeCase(Cursor & cur, TextCase action)
        CursorSlice from;
        CursorSlice to;
 
+       bool gotsel = false;
        if (cur.selection()) {
                from = cur.selBegin();
                to = cur.selEnd();
+               gotsel = true;
        } else {
                from = cur.top();
                getWord(from, to, PARTIAL_WORD);
@@ -850,12 +863,15 @@ void Text::changeCase(Cursor & cur, TextCase action)
        }
 
        // the selection may have changed due to logically-only deleted chars
-       setCursor(cur, begPit, begPos);
-       cur.resetAnchor();
-       setCursor(cur, endPit, right);
-       cur.setSelection();
+       if (gotsel) {
+               setCursor(cur, begPit, begPos);
+               cur.resetAnchor();
+               setCursor(cur, endPit, right);
+               cur.setSelection();
+       } else
+               setCursor(cur, endPit, right);
 
-       checkBufferStructure(cur.buffer(), cur);
+       cur.checkBufferStructure();
 }
 
 
@@ -863,31 +879,37 @@ bool Text::handleBibitems(Cursor & cur)
 {
        if (cur.paragraph().layout()->labeltype != LABEL_BIBLIO)
                return false;
+
+       if (cur.pos() != 0)
+               return false;
+
+       BufferParams const & bufparams = cur.buffer().params();
+       Paragraph const & par = cur.paragraph();
+       Cursor prevcur = cur;
+       if (cur.pit() > 0) {
+               --prevcur.pit();
+               prevcur.pos() = prevcur.lastpos();
+       }
+       Paragraph const & prevpar = prevcur.paragraph();
+
        // if a bibitem is deleted, merge with previous paragraph
        // if this is a bibliography item as well
-       if (cur.pos() == 0) {
-               BufferParams const & bufparams = cur.buffer().params();
-               Paragraph const & par = cur.paragraph();
-               Cursor prevcur = cur;
-               if (cur.pit() > 0) {
-                       --prevcur.pit();
-                       prevcur.pos() = prevcur.lastpos();
-               }
-               Paragraph const & prevpar = prevcur.paragraph();
-               if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
-                       cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
-                       mergeParagraph(bufparams, cur.text()->paragraphs(),
-                                      prevcur.pit());
-                       updateLabels(cur.buffer());
-                       setCursorIntern(cur, prevcur.pit(), prevcur.pos());
-                       cur.updateFlags(Update::Force);
-               // if not, reset the paragraph to default
-               } else
-                       cur.paragraph().layout(
-                               bufparams.getTextClass().defaultLayout());
+       if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
+               cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
+               mergeParagraph(bufparams, cur.text()->paragraphs(),
+                                                       prevcur.pit());
+               updateLabels(cur.buffer());
+               setCursorIntern(cur, prevcur.pit(), prevcur.pos());
+               cur.updateFlags(Update::Force);
                return true;
-       }
-       return false;
+       } 
+
+       // otherwise reset to default
+       if (par.useEmptyLayout())
+               cur.paragraph().layout(bufparams.getTextClass().emptyLayout());
+       else
+               cur.paragraph().layout(bufparams.getTextClass().defaultLayout());
+       return true;
 }
 
 
@@ -901,11 +923,15 @@ bool Text::erase(Cursor & cur)
                // this is the code for a normal delete, not pasting
                // any paragraphs
                cur.recordUndo(DELETE_UNDO);
-               if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges)) {
+               bool const was_inset = cur.paragraph().isInset(cur.pos());
+               if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges))
                        // the character has been logically deleted only => skip it
                        cur.top().forwardPos();
-               }
-               checkBufferStructure(cur.buffer(), cur);
+
+               if (was_inset)
+                       updateLabels(cur.buffer());
+               else
+                       cur.checkBufferStructure();
                needsUpdate = true;
        } else {
                if (cur.pit() == cur.lastpit())
@@ -927,7 +953,7 @@ bool Text::erase(Cursor & cur)
                // Make sure the cursor is correct. Is this really needed?
                // No, not really... at least not here!
                cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
-               checkBufferStructure(cur.buffer(), cur);
+               cur.checkBufferStructure();
        }
 
        return needsUpdate;
@@ -969,8 +995,10 @@ bool Text::backspacePos0(Cursor & cur)
        // layouts. I think it is a real bug of all other
        // word processors to allow it. It confuses the user.
        // Correction: Pasting is always allowed with standard-layout
+       // or the empty layout.
        else if (par.layout() == prevpar.layout()
-                || par.layout() == tclass.defaultLayout()) {
+                || par.layout() == tclass.defaultLayout()
+                || par.layout() == tclass.emptyLayout()) {
                cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
                mergeParagraph(bufparams, plist, prevcur.pit());
                needsUpdate = true;
@@ -1014,8 +1042,12 @@ bool Text::backspace(Cursor & cur)
                // without the dreaded mechanism. (JMarc)
                setCursorIntern(cur, cur.pit(), cur.pos() - 1,
                                false, cur.boundary());
+               bool const was_inset = cur.paragraph().isInset(cur.pos());
                cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
-               checkBufferStructure(cur.buffer(), cur);
+               if (was_inset)
+                       updateLabels(cur.buffer());
+               else
+                       cur.checkBufferStructure();
        }
 
        if (cur.pos() == cur.lastpos())
@@ -1127,7 +1159,8 @@ void Text::write(Buffer const & buf, ostream & os) const
 }
 
 
-bool Text::read(Buffer const & buf, Lexer & lex, ErrorList & errorList)
+bool Text::read(Buffer const & buf, Lexer & lex, 
+               ErrorList & errorList, InsetText * insetPtr)
 {
        depth_type depth = 0;
 
@@ -1156,6 +1189,7 @@ bool Text::read(Buffer const & buf, Lexer & lex, ErrorList & errorList)
                        Paragraph par;
                        par.params().depth(depth);
                        par.setFont(0, Font(inherit_font, buf.params().language));
+                       par.setInsetOwner(insetPtr);
                        pars_.push_back(par);
 
                        // FIXME: goddamn InsetTabular makes us pass a Buffer
@@ -1384,7 +1418,7 @@ void Text::charsTranspose(Cursor & cur)
        par.insertChar(pos1, char2, font2, trackChanges);
        par.insertChar(pos2, char1, font1, trackChanges);
 
-       checkBufferStructure(cur.buffer(), cur);
+       cur.checkBufferStructure();
 
        // After the transposition, move cursor to after the transposition.
        setCursor(cur, cur.pit(), pos2);