]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
John's Layout Tabular UI improvements and Martins fixes to clearing the
[lyx.git] / src / text2.C
index dab824fb143f6faae5da6f9ff077ffa4f3b2ab3d..19ad0bdc1d4517f45c1b2bef7c94a0bc73ffeeb1 100644 (file)
 #include "lyxtext.h"
 #include "LString.h"
 #include "paragraph.h"
-#include "insets/inseterror.h"
-#include "insets/insetbib.h"
-#include "insets/insetspecialchar.h"
-#include "insets/insettext.h"
-#include "insets/insetfloat.h"
-#include "layout.h"
+#include "lyxtextclasslist.h"
 #include "LyXView.h"
-#include "support/textutils.h"
-#include "support/lstrings.h"
 #include "undo_funcs.h"
 #include "buffer.h"
 #include "bufferparams.h"
 #include "font.h"
 #include "debug.h"
 #include "lyxrc.h"
+#include "lyxrow.h"
 #include "FloatList.h"
 #include "language.h"
 #include "ParagraphParameters.h"
+
+#include "insets/inseterror.h"
+#include "insets/insetbib.h"
+#include "insets/insetspecialchar.h"
+#include "insets/insettext.h"
+#include "insets/insetfloat.h"
+
 #include "support/LAssert.h"
+#include "support/textutils.h"
+#include "support/lstrings.h"
 
 using std::copy;
 using std::find;
 using std::endl;
 using std::find;
 using std::pair;
+using lyx::pos_type;
 
 
 LyXText::LyXText(BufferView * bv)
@@ -155,7 +159,7 @@ LyXFont const realizeFont(LyXFont const & font,
 // If position is -1, we get the layout font of the paragraph.
 // If position is -2, we get the font of the manual label of the paragraph.
 LyXFont const LyXText::getFont(Buffer const * buf, Paragraph * par,
-                               Paragraph::size_type pos) const
+                               pos_type pos) const
 {
        lyx::Assert(pos >= 0);
        
@@ -239,21 +243,19 @@ LyXFont const LyXText::getLabelFont(Buffer const * buf, Paragraph * par) const
 
 
 void LyXText::setCharFont(BufferView * bv, Paragraph * par,
-                          Paragraph::size_type pos, LyXFont const & fnt,
+                          pos_type pos, LyXFont const & fnt,
                           bool toggleall)
 {
        Buffer const * buf = bv->buffer();
        LyXFont font = getFont(buf, par, pos);
        font.update(fnt, buf->params.language, toggleall);
        // Let the insets convert their font
-       if (par->getChar(pos) == Paragraph::META_INSET) {
+       if (par->isInset(pos)) {
                Inset * inset = par->getInset(pos);
-               if (inset) {
-                       if (inset->editable()==Inset::IS_EDITABLE) {
-                               UpdatableInset * uinset =
-                                       static_cast<UpdatableInset *>(inset);
-                               uinset->setFont(bv, fnt, toggleall, true);
-                       }
+               if (isEditableInset(inset)) {
+                       UpdatableInset * uinset =
+                               static_cast<UpdatableInset *>(inset);
+                       uinset->setFont(bv, fnt, toggleall, true);
                }
        }
 
@@ -303,7 +305,7 @@ void LyXText::setCharFont(BufferView * bv, Paragraph * par,
 
 
 void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
-                          Paragraph::size_type pos, LyXFont const & fnt)
+                          pos_type pos, LyXFont const & fnt)
 {
        LyXFont font(fnt);
 
@@ -355,7 +357,7 @@ void LyXText::setCharFont(Buffer const * buf, Paragraph * par,
 // inserts a new row behind the specified row, increments
 // the touched counters
 void LyXText::insertRow(Row * row, Paragraph * par,
-                        Paragraph::size_type pos) const
+                        pos_type pos) const
 {
        Row * tmprow = new Row;
        if (!row) {
@@ -387,16 +389,11 @@ void LyXText::insertRow(Row * row, Paragraph * par,
 // removes the row and reset the touched counters
 void LyXText::removeRow(Row * row) const
 {
-       /* this must not happen before the currentrow for clear reasons.
-          so the trick is just to set the current row onto the previous
-          row of this row */
-       int unused_y;
-       getRow(row->par(), row->pos(), unused_y);
-   
        if (row->next())
                row->next()->previous(row->previous());
        if (!row->previous()) {
                firstrow = row->next();
+//             lyx::Assert(firstrow);
        } else  {
                row->previous()->next(row->next());
        }
@@ -451,7 +448,7 @@ Inset * LyXText::getInset() const
        if (cursor.pos() == 0 && cursor.par()->bibkey) {
                inset = cursor.par()->bibkey;
        } else if (cursor.pos() < cursor.par()->size() 
-                  && cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
+                  && cursor.par()->isInset(cursor.pos())) {
                inset = cursor.par()->getInset(cursor.pos());
        }
        return inset;
@@ -461,12 +458,12 @@ Inset * LyXText::getInset() const
 void LyXText::toggleInset(BufferView * bview)
 {
        Inset * inset = getInset();
-       if (!inset->editable())
+       if (!isEditableInset(inset))
                return;
        //bview->owner()->message(inset->editMessage());
 
        // do we want to keep this?? (JMarc)
-       if (inset->editable() != Inset::HIGHLY_EDITABLE)
+       if (!isHighlyEditableInset(inset))
                setCursorParUndo(bview);
 
        if (inset->isOpen()) {
@@ -489,7 +486,7 @@ void LyXText::makeFontEntriesLayoutSpecific(Buffer const * buf,
                textclasslist.Style(buf->params.textclass, par->getLayout());
 
        LyXFont layoutfont;
-       for (Paragraph::size_type pos = 0; pos < par->size(); ++pos) {
+       for (pos_type pos = 0; pos < par->size(); ++pos) {
                if (pos < beginningOfMainBody(buf, par))
                        layoutfont = layout.labelfont;
                else
@@ -505,7 +502,7 @@ void LyXText::makeFontEntriesLayoutSpecific(Buffer const * buf,
 Paragraph * LyXText::setLayout(BufferView * bview,
                               LyXCursor & cur, LyXCursor & sstart_cur,
                               LyXCursor & send_cur,
-                              LyXTextClass::size_type layout)
+                              lyx::layout_type layout)
 {
        Paragraph * endpar = send_cur.par()->next();
        Paragraph * undoendpar = endpar;
@@ -567,7 +564,7 @@ Paragraph * LyXText::setLayout(BufferView * bview,
 
 
 // set layout over selection and make a total rebreak of those paragraphs
-void LyXText::setLayout(BufferView * bview, LyXTextClass::size_type layout)
+void LyXText::setLayout(BufferView * bview, lyx::layout_type layout)
 {
        LyXCursor tmpcursor = cursor;  /* store the current cursor  */
 
@@ -793,11 +790,11 @@ void LyXText::setFont(BufferView * bview, LyXFont const & font, bool toggleall)
        redoParagraphs(bview, selection.start, selection.end.par()->next());
    
        // we have to reset the selection, because the
-       // geometry could have changed
+       // geometry could have changed, but we keep
+       // it for user convenience
        setCursor(bview, selection.start.par(), selection.start.pos());
        selection.cursor = cursor;
        setCursor(bview, selection.end.par(), selection.end.pos());
-       clearSelection();
        setSelection(bview);
        setCursor(bview, tmpcursor.par(), tmpcursor.pos(), true,
                  tmpcursor.boundary());
@@ -865,8 +862,9 @@ void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
 
        if (!tmprow->previous()) {
                // a trick/hack for UNDO
-               // Can somebody please tell me _why_ this solves
-               // anything. (Lgb)
+               // This is needed because in an UNDO/REDO we could have changed
+               // the firstParagrah() so the paragraph inside the row is NOT
+               // my really first par anymore. Got it Lars ;) (Jug 20011206)
                first_phys_par = firstParagraph();
        } else {
                first_phys_par = tmprow->par();
@@ -877,7 +875,7 @@ void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
                        y -= tmprow->height();
                }
        }
-       
+
        // we can set the refreshing parameters now
        status(bview, LyXText::NEED_MORE_REFRESH);
        refresh_y = y;
@@ -889,13 +887,14 @@ void LyXText::redoParagraphs(BufferView * bview, LyXCursor const & cur,
                tmppar = tmprow->next()->par();
        else
                tmppar = 0;
-       while (tmppar != endpar) {
+       while (tmprow->next() && tmppar != endpar) {
                removeRow(tmprow->next());
-               if (tmprow->next())
+               if (tmprow->next()) {
                        tmppar = tmprow->next()->par();
-               else
+               } else {
                        tmppar = 0;
-       }  
+               }
+       }
    
        // remove the first one
        tmprow2 = tmprow;     /* this is because tmprow->previous()
@@ -1014,7 +1013,7 @@ void LyXText::setSelection(BufferView * bview)
 
 
 string const LyXText::selectionAsString(Buffer const * buffer,
-                                       bool label) const
+                                        bool label) const
 {
        if (!selection.set()) return string();
        string result;
@@ -1059,7 +1058,7 @@ void LyXText::clearSelection() const
 {
        selection.set(false);
        selection.mark(false);
-       selection.end = selection.start = selection.cursor = cursor;
+       last_sel_cursor = selection.end = selection.start = selection.cursor = cursor;
 }
 
 
@@ -1137,8 +1136,7 @@ void LyXText::toggleFree(BufferView * bview,
 }
 
 
-string
-LyXText::getStringToIndex(BufferView * bview)
+string LyXText::getStringToIndex(BufferView * bview)
 {
        string idxstring;
        
@@ -1170,8 +1168,8 @@ LyXText::getStringToIndex(BufferView * bview)
        return idxstring;
 }
 
-Paragraph::size_type
-LyXText::beginningOfMainBody(Buffer const * buf,
+
+pos_type LyXText::beginningOfMainBody(Buffer const * buf,
                             Paragraph const * par) const
 {
        if (textclasslist.Style(buf->params.textclass,
@@ -1193,7 +1191,7 @@ void LyXText::setParagraph(BufferView * bview,
                           bool pagebreak_top, bool pagebreak_bottom,
                           VSpace const & space_top,
                           VSpace const & space_bottom,
-                           Spacing const & spacing,
+                          Spacing const & spacing,
                           LyXAlignment align, 
                           string labelwidthstring,
                           bool noindent) 
@@ -1686,8 +1684,8 @@ void LyXText::insertInset(BufferView * bview, Inset * inset)
 {
        if (!cursor.par()->insetAllowed(inset->lyxCode()))
                return;
-       setUndo(bview, Undo::INSERT,
-               cursor.par(), cursor.par()->next());
+       // I don't know if this is necessary here (Jug 20020102)
+       setUndo(bview, Undo::INSERT, cursor.par(), cursor.par()->next());
        cursor.par()->insertInset(cursor.pos(), inset);
        // Just to rebreak and refresh correctly.
        // The character will not be inserted a second time
@@ -1698,7 +1696,7 @@ void LyXText::insertInset(BufferView * bview, Inset * inset)
        // inset now after the Undo LyX tries to call inset->Edit(...) again
        // and cannot do this as the cursor is behind the inset and GetInset
        // does not return the inset!
-       if (inset->editable() == Inset::HIGHLY_EDITABLE) {
+       if (isHighlyEditableInset(inset)) {
                cursorLeft(bview, true);
        }
 #endif
@@ -1760,16 +1758,16 @@ void LyXText::cutSelection(BufferView * bview, bool doclear, bool realcut)
                int pos = selection.end.pos();
                CutAndPaste::cutSelection(selection.start.par(), &endpar,
                                          selection.start.pos(), pos,
-                                         bview->buffer()->params.textclass, doclear,
-                                         realcut);
+                                         bview->buffer()->params.textclass,
+                                         doclear, realcut);
                selection.end.pos(pos);
        } else {
                endpar = selection.end.par();
                int pos = selection.end.pos();
                CutAndPaste::cutSelection(selection.start.par(), &endpar,
                                          selection.start.pos(), pos,
-                                         bview->buffer()->params.textclass, doclear,
-                                                                 realcut);
+                                         bview->buffer()->params.textclass,
+                                         doclear, realcut);
                cursor.par(endpar);
                selection.end.par(endpar);
                selection.end.pos(pos);
@@ -1887,7 +1885,7 @@ void LyXText::replaceSelectionWithString(BufferView * bview,
        }
 
        // Get font setting before we cut
-       Paragraph::size_type pos = selection.end.pos();
+       pos_type pos = selection.end.pos();
        LyXFont const font = selection.start.par()
                ->getFontSettings(bview->buffer()->params,
                                  selection.start.pos());
@@ -1909,7 +1907,7 @@ void LyXText::replaceSelectionWithString(BufferView * bview,
 void LyXText::insertStringAsLines(BufferView * bview, string const & str)
 {
        Paragraph * par = cursor.par();
-       Paragraph::size_type pos = cursor.pos();
+       pos_type pos = cursor.pos();
        Paragraph * endpar = cursor.par()->next();
        
        setCursorParUndo(bview);
@@ -1968,7 +1966,7 @@ bool LyXText::gotoNextInset(BufferView * bview,
                }
       
        } while (res.par() && 
-                !(res.par()->getChar(res.pos()) == Paragraph::META_INSET
+                !(res.par()->isInset(res.pos())
                   && (inset = res.par()->getInset(res.pos())) != 0
                   && find(codes.begin(), codes.end(), inset->lyxCode())
                   != codes.end()
@@ -1977,7 +1975,7 @@ bool LyXText::gotoNextInset(BufferView * bview,
                       == contents)));
 
        if (res.par()) {
-               setCursor(bview, res.par(), res.pos());
+               setCursor(bview, res.par(), res.pos(), false);
                return true;
        }
        return false;
@@ -1985,12 +1983,12 @@ bool LyXText::gotoNextInset(BufferView * bview,
 
 
 void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
-                            Paragraph::size_type pos)
+                             pos_type pos)
 {
        LyXCursor tmpcursor;                    
 
        int y = 0;
-       Paragraph::size_type z;
+       pos_type z;
        Row * row = getRow(par, pos, y);
        
        // is there a break one row above
@@ -2015,7 +2013,7 @@ void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
        }
 
        int const tmpheight = row->height();
-       Paragraph::size_type const tmplast = rowLast(row);
+       pos_type const tmplast = rowLast(row);
        refresh_y = y;
        refresh_row = row;
        
@@ -2028,7 +2026,8 @@ void LyXText::checkParagraph(BufferView * bview, Paragraph * par,
        // check the special right address boxes
        if (textclasslist.Style(bview->buffer()->params.textclass,
                                par->getLayout()).margintype
-           == MARGIN_RIGHT_ADDRESS_BOX) {
+           == MARGIN_RIGHT_ADDRESS_BOX)
+       {
                tmpcursor.par(par);
                tmpcursor.row(row);
                tmpcursor.y(y);
@@ -2070,7 +2069,7 @@ bool LyXText::updateInset(BufferView * bview, Inset * inset)
 {
        // first check the current paragraph
        int pos = cursor.par()->getPositionOfInset(inset);
-       if (pos != -1){
+       if (pos != -1) {
                checkParagraph(bview, cursor.par(), pos);
                return true;
        }
@@ -2080,7 +2079,7 @@ bool LyXText::updateInset(BufferView * bview, Inset * inset)
        Paragraph * par = firstParagraph();
        do {
                pos = par->getPositionOfInset(inset);
-               if (pos != -1){
+               if (pos != -1) {
                        checkParagraph(bview, par, pos);
                        return true;
                }
@@ -2092,7 +2091,7 @@ bool LyXText::updateInset(BufferView * bview, Inset * inset)
 
 
 void LyXText::setCursor(BufferView * bview, Paragraph * par,
-                        Paragraph::size_type pos, 
+                        pos_type pos, 
                         bool setfont, bool boundary) const
 {
        LyXCursor old_cursor = cursor;
@@ -2101,9 +2100,12 @@ void LyXText::setCursor(BufferView * bview, Paragraph * par,
 }
 
 
-void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
-                       Paragraph::size_type pos, bool boundary) const
+void LyXText::setCursor(BufferView * bview, LyXCursor & cur, Paragraph * par,
+                       pos_type pos, bool boundary) const
 {
+       lyx::Assert(par);
+       lyx::Assert(bview);
+       
        cur.par(par);
        cur.pos(pos);
        cur.boundary(boundary);
@@ -2115,7 +2117,7 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
        y += row->baseline();
        // y is now the cursor baseline 
        cur.y(y);
-   
+
        // now get the cursors x position
        float x;
        float fill_separator;
@@ -2123,14 +2125,18 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
        float fill_label_hfill;
        prepareToPrint(bview, row, x, fill_separator, fill_hfill,
                       fill_label_hfill);
-       Paragraph::size_type cursor_vpos = 0;
-       Paragraph::size_type last = rowLastPrintable(row);
+       pos_type cursor_vpos = 0;
+       pos_type last = rowLastPrintable(row);
 
-       if (pos > last + 1)   // This shouldn't happen.
+       if (pos > last + 1) {
+               // This shouldn't happen.
                pos = last + 1;
-       else if (pos < row->pos())
+               cur.pos(pos);
+       } else if (pos < row->pos()) {
                pos = row->pos();
-
+               cur.pos(pos);
+       }
+       
        if (last < row->pos())
                 cursor_vpos = row->pos();
        else if (pos > last && !boundary)
@@ -2146,14 +2152,14 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
                cursor_vpos = (bidi_level(pos) % 2 == 0)
                        ? log2vis(pos) : log2vis(pos) + 1;
        
-       Paragraph::size_type main_body =
+       pos_type main_body =
                beginningOfMainBody(bview->buffer(), row->par());
        if ((main_body > 0) &&
            ((main_body-1 > last) || 
             !row->par()->isLineSeparator(main_body-1)))
                main_body = 0;
        
-       for (Paragraph::size_type vpos = row->pos();
+       for (pos_type vpos = row->pos();
             vpos < cursor_vpos; ++vpos) {
                pos = vis2log(vpos);
                if (main_body > 0 && pos == main_body - 1) {
@@ -2181,14 +2187,13 @@ void LyXText::setCursor(BufferView *bview, LyXCursor & cur, Paragraph * par,
        }
        
        cur.x(int(x));
-       cur.x_fix(cur.x());
+       cur.x_fix(cur.x());
        cur.row(row);
 }
 
 
 void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
-                             Paragraph::size_type pos,
-                             bool setfont, bool boundary) const
+                              pos_type pos, bool setfont, bool boundary) const
 {
        InsetText * it = static_cast<InsetText *>(par->inInset());
        if (it) {
@@ -2207,8 +2212,7 @@ void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
                        // I moved the lyxerr stuff in here so we can see if
                        // this is actually really needed and where!
                        // (Jug)
-                       it->getLyXText(bview)->setCursorIntern(bview, par, pos, setfont,
-                                                              boundary);
+                       // it->getLyXText(bview)->setCursorIntern(bview, par, pos, setfont, boundary);
                        return;
                }
        }
@@ -2221,7 +2225,7 @@ void LyXText::setCursorIntern(BufferView * bview, Paragraph * par,
 
 void LyXText::setCurrentFont(BufferView * bview) const
 {
-       Paragraph::size_type pos = cursor.pos();
+       pos_type pos = cursor.pos();
        if (cursor.boundary() && pos > 0)
                --pos;
 
@@ -2273,8 +2277,7 @@ void LyXText::setCursorFromCoordinates(BufferView * bview, LyXCursor & cur,
    
        Row * row = getRowNearY(y);
        bool bound = false;
-       int const column = getColumnNearX(bview, row, x, bound);
-   
+       pos_type const column = getColumnNearX(bview, row, x, bound);
        cur.par(row->par());
        cur.pos(row->pos() + column);
        cur.x(x);
@@ -2378,8 +2381,11 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
 
        // We allow all kinds of "mumbo-jumbo" when freespacing.
        if (textclasslist.Style(bview->buffer()->params.textclass,
-                               old_cursor.par()->getLayout()).free_spacing)
+                               old_cursor.par()->getLayout()).free_spacing
+           || old_cursor.par()->isFreeSpacing())
+       {
                return;
+       }
 
        bool deleted = false;
        
@@ -2407,7 +2413,8 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
        // MISSING
 
        // If the pos around the old_cursor were spaces, delete one of them.
-       if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) { 
+       if (old_cursor.par() != cursor.par()
+           || old_cursor.pos() != cursor.pos()) { 
                // Only if the cursor has really moved
                
                if (old_cursor.pos() > 0
@@ -2439,6 +2446,10 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                }
        }
 
+       // don't delete anything if this is the ONLY paragraph!
+       if (!old_cursor.par()->next() && !old_cursor.par()->previous())
+               return;
+       
        // Do not delete empty paragraphs with keepempty set.
        if ((textclasslist.Style(bview->buffer()->params.textclass,
                                 old_cursor.par()->getLayout())).keepempty)
@@ -2472,7 +2483,7 @@ void LyXText::deleteEmptyParagraphMechanism(BufferView * bview,
                        setUndo(bview, Undo::DELETE, old_cursor.par(), endpar);
                        cursor = tmpcursor;
 
-                               // delete old row
+                       // delete old row
                        removeRow(old_cursor.row());
                        if (ownerParagraph() == old_cursor.par()) {
                                ownerParagraph(ownerParagraph()->next());
@@ -2575,29 +2586,24 @@ Paragraph * LyXText::ownerParagraph() const
 }
 
 
-Paragraph * LyXText::ownerParagraph(Paragraph * p) const
+void LyXText::ownerParagraph(Paragraph * p) const
 {
        if (inset_owner) {
                inset_owner->paragraph(p);
        } else {
                bv_owner->buffer()->paragraph = p;
        }
-       return 0;
 }
 
-Paragraph * LyXText::ownerParagraph(int id, Paragraph * p) const
+
+void LyXText::ownerParagraph(int id, Paragraph * p) const
 {
        Paragraph * op = bv_owner->buffer()->getParFromID(id);
        if (op && op->inInset()) {
                static_cast<InsetText *>(op->inInset())->paragraph(p);
        } else {
-               if (inset_owner) {
-                       inset_owner->paragraph(p);
-               } else {
-                       bv_owner->buffer()->paragraph = p;
-               }
+               ownerParagraph(p);
        }
-       return 0;
 }