]> git.lyx.org Git - lyx.git/blobdiff - src/Text2.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / Text2.cpp
index 64da0621630f513e8ea33111abe8c6f3855f65eb..ba0b09fe92822fc395465c483287b485cfada132 100644 (file)
@@ -11,6 +11,7 @@
  * \author John Levon
  * \author André Pönitz
  * \author Allan Rae
+ * \author Stefan Schimanski
  * \author Dekel Tsur
  * \author Jürgen Vigna
  *
@@ -149,9 +150,7 @@ Font Text::getFont(Buffer const & buffer, Paragraph const & par,
        BOOST_ASSERT(pos >= 0);
 
        Layout_ptr const & layout = par.layout();
-#ifdef WITH_WARNINGS
-#warning broken?
-#endif
+       // FIXME: broken?
        BufferParams const & params = buffer.params();
        pos_type const body_pos = par.beginOfBody();
 
@@ -307,6 +306,36 @@ void Text::setCharFont(Buffer const & buffer, pit_type pit,
 }
 
 
+void Text::setInsetFont(Buffer const & buffer, pit_type pit,
+               pos_type pos, Font const & font, bool toggleall)
+{
+       BOOST_ASSERT(pars_[pit].isInset(pos) &&
+                    pars_[pit].getInset(pos)->noFontChange());
+
+       Inset * const inset = pars_[pit].getInset(pos);
+       DocIterator dit = doc_iterator_begin(*inset);
+       // start of the last cell
+       DocIterator end = dit;
+       end.idx() = end.lastidx();
+
+       while (true) {
+               Text * text = dit.text();
+               Inset * cell = dit.realInset();
+               if (text && cell) {
+                       DocIterator cellbegin = doc_iterator_begin(*cell);
+                       // last position of the cell
+                       DocIterator cellend = cellbegin;
+                       cellend.pit() = cellend.lastpit();
+                       cellend.pos() = cellend.lastpos();
+                       text->setFont(buffer, cellbegin, cellend, font, toggleall);
+               }
+               if (dit == end)
+                       break;
+               dit.forwardIdx();
+       }
+}
+
+
 // return past-the-last paragraph influenced by a layout change on pit
 pit_type Text::undoSpan(pit_type pit)
 {
@@ -327,7 +356,7 @@ pit_type Text::undoSpan(pit_type pit)
 
 
 void Text::setLayout(Buffer const & buffer, pit_type start, pit_type end,
-               string const & layout)
+               docstring const & layout)
 {
        BOOST_ASSERT(start != end);
 
@@ -345,7 +374,7 @@ void Text::setLayout(Buffer const & buffer, pit_type start, pit_type end,
 
 
 // set layout over selection and make a total rebreak of those paragraphs
-void Text::setLayout(Cursor & cur, string const & layout)
+void Text::setLayout(Cursor & cur, docstring const & layout)
 {
        BOOST_ASSERT(this == cur.text());
        // special handling of new environment insets
@@ -354,7 +383,7 @@ void Text::setLayout(Cursor & cur, string const & layout)
        Layout_ptr const & lyxlayout = params.getTextClass()[layout];
        if (lyxlayout->is_environment) {
                // move everything in a new environment inset
-               LYXERR(Debug::DEBUG) << "setting layout " << layout << endl;
+               LYXERR(Debug::DEBUG) << "setting layout " << to_utf8(layout) << endl;
                lyx::dispatch(FuncRequest(LFUN_LINE_BEGIN));
                lyx::dispatch(FuncRequest(LFUN_LINE_END_SELECT));
                lyx::dispatch(FuncRequest(LFUN_CUT));
@@ -433,51 +462,64 @@ void Text::changeDepth(Cursor & cur, DEPTH_CHANGE type)
 }
 
 
-// set font over selection
 void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
 {
        BOOST_ASSERT(this == cur.text());
-       // if there is no selection just set the current_font
-       if (!cur.selection()) {
-               // Determine basis font
-               Font layoutfont;
-               pit_type pit = cur.pit();
-               if (cur.pos() < pars_[pit].beginOfBody())
-                       layoutfont = getLabelFont(cur.buffer(), pars_[pit]);
-               else
-                       layoutfont = getLayoutFont(cur.buffer(), pit);
+       // Set the current_font
+       // Determine basis font
+       Font layoutfont;
+       pit_type pit = cur.pit();
+       if (cur.pos() < pars_[pit].beginOfBody())
+               layoutfont = getLabelFont(cur.buffer(), pars_[pit]);
+       else
+               layoutfont = getLayoutFont(cur.buffer(), pit);
 
-               // Update current font
-               real_current_font.update(font,
-                                        cur.buffer().params().language,
-                                        toggleall);
+       // Update current font
+       real_current_font.update(font,
+                                       cur.buffer().params().language,
+                                       toggleall);
 
-               // Reduce to implicit settings
-               current_font = real_current_font;
-               current_font.reduce(layoutfont);
-               // And resolve it completely
-               real_current_font.realize(layoutfont);
+       // Reduce to implicit settings
+       current_font = real_current_font;
+       current_font.reduce(layoutfont);
+       // And resolve it completely
+       real_current_font.realize(layoutfont);
 
+       // if there is no selection that's all we need to do
+       if (!cur.selection())
                return;
-       }
 
        // Ok, we have a selection.
        recordUndoSelection(cur);
 
-       DocIterator dit = cur.selectionBegin();
-       DocIterator ditend = cur.selectionEnd();
+       setFont(cur.buffer(), cur.selectionBegin(), cur.selectionEnd(), font,
+               toggleall);
+}
 
-       BufferParams const & params = cur.buffer().params();
 
+void Text::setFont(Buffer const & buffer, DocIterator const & begin,
+               DocIterator const & end, Font const & font,
+               bool toggleall)
+{
        // Don't use forwardChar here as ditend might have
        // pos() == lastpos() and forwardChar would miss it.
        // Can't use forwardPos either as this descends into
        // nested insets.
-       for (; dit != ditend; dit.forwardPosNoDescend()) {
+       Language const * language = buffer.params().language;
+       for (DocIterator dit = begin; dit != end; dit.forwardPosNoDescend()) {
                if (dit.pos() != dit.lastpos()) {
-                       Font f = getFont(cur.buffer(), dit.paragraph(), dit.pos());
-                       f.update(font, params.language, toggleall);
-                       setCharFont(cur.buffer(), dit.pit(), dit.pos(), f);
+                       pit_type const pit = dit.pit();
+                       pos_type const pos = dit.pos();
+                       if (pars_[pit].isInset(pos) &&
+                           pars_[pit].getInset(pos)->noFontChange())
+                               // We need to propagate the font change to all
+                               // text cells of the inset (bug 1973).
+                               // FIXME: This should change, see documentation
+                               // of noFontChange in Inset.h
+                               setInsetFont(buffer, pit, pos, font, toggleall);
+                       Font f = getFont(buffer, dit.paragraph(), pos);
+                       f.update(font, language, toggleall);
+                       setCharFont(buffer, pit, pos, f);
                }
        }
 }
@@ -605,16 +647,8 @@ void Text::setParagraph(Cursor & cur,
                params.spacing(spacing);
 
                // does the layout allow the new alignment?
-               Layout_ptr const & layout = par.layout();
-
-               if (align == LYX_ALIGN_LAYOUT)
-                       align = layout->align;
-               if (align & layout->alignpossible) {
-                       if (align == layout->align)
-                               params.align(LYX_ALIGN_LAYOUT);
-                       else
-                               params.align(align);
-               }
+               if (align & par.layout()->alignpossible)
+                       params.align(align);
                par.setLabelWidthString(labelwidthstring);
                params.noindent(noindent);
        }
@@ -626,9 +660,9 @@ void Text::insertInset(Cursor & cur, Inset * inset)
 {
        BOOST_ASSERT(this == cur.text());
        BOOST_ASSERT(inset);
-       cur.paragraph().insertInset(cur.pos(), inset, 
+       cur.paragraph().insertInset(cur.pos(), inset, current_font,
                                    Change(cur.buffer().params().trackChanges ?
-                                          Change::INSERTED : Change::UNCHANGED));
+                                          Change::INSERTED : Change::UNCHANGED));
 }
 
 
@@ -717,29 +751,33 @@ void Text::setCurrentFont(Cursor & cur)
        pos_type pos = cur.pos();
        Paragraph & par = cur.paragraph();
 
-       if (cur.boundary() && pos > 0)
+       // are we behind previous char in fact? -> go to that char
+       if (pos > 0 && cur.boundary())
                --pos;
 
-       if (pos > 0) {
+       // find position to take the font from
+       if (pos != 0) {
+               // paragraph end? -> font of last char
                if (pos == cur.lastpos())
                        --pos;
-               else // potentional bug... BUG (Lgb)
-                       if (par.isSeparator(pos)) {
-                               if (pos > cur.textRow().pos() &&
-                                   bidi.level(pos) % 2 ==
-                                   bidi.level(pos - 1) % 2)
-                                       --pos;
-                               else if (pos + 1 < cur.lastpos())
-                                       ++pos;
-                       }
+               // on space? -> look at the words in front of space
+               else if (pos > 0 && par.isSeparator(pos))       {
+                       // abc| def -> font of c
+                       // abc |[WERBEH], i.e. boundary==true -> font of c
+                       // abc [WERBEH]| def, font of the space
+                       if (!isRTLBoundary(cur.buffer(), par, pos))
+                               --pos;
+               }
        }
 
+       // get font
        BufferParams const & bufparams = cur.buffer().params();
        current_font = par.getFontSettings(bufparams, pos);
        real_current_font = getFont(cur.buffer(), par, pos);
 
+       // special case for paragraph end
        if (cur.pos() == cur.lastpos()
-           && bidi.isBoundary(cur.buffer(), par, cur.pos())
+           && isRTLBoundary(cur.buffer(), par, cur.pos())
            && !cur.boundary()) {
                Language const * lang = par.getParLanguage(bufparams);
                current_font.setLanguage(lang);
@@ -864,7 +902,7 @@ Inset * Text::editXY(Cursor & cur, int x, int y)
        cur.pit() = pit;
        cur.pos() = pos;
        cur.boundary(bound);
-       cur.x_target() = x;
+       cur.setTargetX(x);
 
        // try to descend into nested insets
        Inset * inset = checkInsetHit(cur.bv(), x, y);
@@ -886,8 +924,10 @@ Inset * Text::editXY(Cursor & cur, int x, int y)
 
        // Make sure the cursor points to the position before
        // this inset.
-       if (inset == insetBefore)
+       if (inset == insetBefore) {
                --cur.pos();
+               cur.boundary(false);
+       }
 
        // Try to descend recursively inside the inset.
        inset = inset->editXY(cur, x, y);
@@ -902,11 +942,22 @@ bool Text::checkAndActivateInset(Cursor & cur, bool front)
 {
        if (cur.selection())
                return false;
-       if (cur.pos() == cur.lastpos())
+       if (front && cur.pos() == cur.lastpos())
+               return false;
+       if (!front && cur.pos() == 0)
                return false;
-       Inset * inset = cur.nextInset();
+       Inset * inset = front ? cur.nextInset() : cur.prevInset();
        if (!isHighlyEditableInset(inset))
                return false;
+       /*
+        * Apparently, when entering an inset we are expected to be positioned
+        * *before* it in the containing paragraph, regardless of the direction
+        * from which we are entering. Otherwise, cursor placement goes awry,
+        * and when we exit from the beginning, we'll be placed *after* the
+        * inset.
+        */
+       if (!front)
+               --cur.pos();
        inset->edit(cur, front);
        return true;
 }
@@ -917,30 +968,39 @@ bool Text::cursorLeft(Cursor & cur)
        // Tell BufferView to test for FitCursor in any case!
        cur.updateFlags(Update::FitCursor);
 
-       if (!cur.boundary() && cur.pos() > 0 &&
-           cur.textRow().pos() == cur.pos() &&
-           !cur.paragraph().isLineSeparator(cur.pos()-1) &&
-           !cur.paragraph().isNewline(cur.pos()-1)) {
-               return setCursor(cur, cur.pit(), cur.pos(), true, true);
-       }
-       if (cur.pos() != 0) {
-               bool updateNeeded = setCursor(cur, cur.pit(), cur.pos() - 1, true, false);
-               if (!checkAndActivateInset(cur, false)) {
-                       /** FIXME: What's this cause purpose???
-                       bool boundary = cur.boundary();
-                       if (false && !boundary &&
-                           bidi.isBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
-                               updateNeeded |=
-                                       setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
-                       */
+       // not at paragraph start?
+       if (cur.pos() > 0) {
+               // if on right side of boundary (i.e. not at paragraph end, but line end)
+               // -> skip it, i.e. set boundary to true, i.e. go only logically left
+               // there are some exceptions to ignore this: lineseps, newlines, spaces
+#if 0
+               // some effectless debug code to see the values in the debugger
+               bool bound = cur.boundary();
+               int rowpos = cur.textRow().pos();
+               int pos = cur.pos();
+               bool sep = cur.paragraph().isSeparator(cur.pos() - 1);
+               bool newline = cur.paragraph().isNewline(cur.pos() - 1);
+               bool linesep = cur.paragraph().isLineSeparator(cur.pos() - 1);
+#endif
+               if (!cur.boundary() &&
+                               cur.textRow().pos() == cur.pos() &&
+                               !cur.paragraph().isLineSeparator(cur.pos() - 1) &&
+                               !cur.paragraph().isNewline(cur.pos() - 1) &&
+                               !cur.paragraph().isSeparator(cur.pos() - 1)) {
+                       return setCursor(cur, cur.pit(), cur.pos(), true, true);
                }
-               return updateNeeded;
+               
+               // go left and try to enter inset
+               if (checkAndActivateInset(cur, false))
+                       return false;
+               
+               // normal character left
+               return setCursor(cur, cur.pit(), cur.pos() - 1, true, false);
        }
 
-       if (cur.pit() != 0) {
-               // Steps into the paragraph above
+       // move to the previous paragraph or do nothing
+       if (cur.pit() > 0)
                return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size());
-       }
        return false;
 }
 
@@ -950,169 +1010,58 @@ bool Text::cursorRight(Cursor & cur)
        // Tell BufferView to test for FitCursor in any case!
        cur.updateFlags(Update::FitCursor);
 
+       // not at paragraph end?
        if (cur.pos() != cur.lastpos()) {
-               if (cur.boundary())
-                       return setCursor(cur, cur.pit(), cur.pos(),
-                                        true, false);
-
-               bool updateNeeded = false;
-               if (!checkAndActivateInset(cur, true)) {
-                       if (cur.textRow().endpos() == cur.pos() + 1 &&
-                           cur.textRow().endpos() != cur.lastpos() &&
-                           !cur.paragraph().isLineSeparator(cur.pos()) &&
-                           !cur.paragraph().isNewline(cur.pos())) {
-                               cur.boundary(true);
-                       }
-                       updateNeeded |= setCursor(cur, cur.pit(), cur.pos() + 1, true, cur.boundary());
-                       if (false && bidi.isBoundary(cur.buffer(), cur.paragraph(),
-                                                    cur.pos()))
-                               updateNeeded |= setCursor(cur, cur.pit(), cur.pos(), true, true);
+               // in front of editable inset, i.e. jump into it?
+               if (checkAndActivateInset(cur, true))
+                       return false;
+
+               // if left of boundary -> just jump to right side
+         // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
+         if (cur.boundary() && 
+                               !isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos()))
+                       return setCursor(cur, cur.pit(), cur.pos(), true, false);
+
+               // next position is left of boundary, 
+               // but go to next line for special cases like space, newline, linesep
+#if 0
+               // some effectless debug code to see the values in the debugger
+               int endpos = cur.textRow().endpos();
+               int lastpos = cur.lastpos();
+               int pos = cur.pos();
+               bool linesep = cur.paragraph().isLineSeparator(cur.pos());
+               bool newline = cur.paragraph().isNewline(cur.pos());
+               bool sep = cur.paragraph().isSeparator(cur.pos());
+               if (cur.pos() != cur.lastpos()) {
+                       bool linesep2 = cur.paragraph().isLineSeparator(cur.pos()+1);
+                       bool newline2 = cur.paragraph().isNewline(cur.pos()+1);
+                       bool sep2 = cur.paragraph().isSeparator(cur.pos()+1);
+               }
+#endif
+               if (cur.textRow().endpos() == cur.pos() + 1 &&
+                   cur.textRow().endpos() != cur.lastpos() &&
+                               !cur.paragraph().isNewline(cur.pos()) &&
+                               !cur.paragraph().isLineSeparator(cur.pos()) &&
+                               !cur.paragraph().isSeparator(cur.pos())) {
+                       return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
                }
-               return updateNeeded;
+               
+               // in front of RTL boundary? Stay on this side of the boundary because:
+               //   ab|cDDEEFFghi -> abc|DDEEFFghi
+               if (isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
+                       return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
+               
+               // move right
+               return setCursor(cur, cur.pit(), cur.pos() + 1, true, false);
        }
 
+       // move to next paragraph
        if (cur.pit() != cur.lastpit())
                return setCursor(cur, cur.pit() + 1, 0);
        return false;
 }
 
 
-bool Text::cursorUp(Cursor & cur)
-{
-       // Tell BufferView to test for FitCursor in any case!
-       cur.updateFlags(Update::FitCursor);
-
-       TextMetrics const & tm = cur.bv().textMetrics(this);
-       ParagraphMetrics const & pm = tm.parMetrics(cur.pit());
-
-       int row;
-       if (cur.pos() && cur.boundary())
-               row = pm.pos2row(cur.pos()-1);
-       else
-               row = pm.pos2row(cur.pos());
-
-       int x = cur.targetX();
-       cur.setTargetX();
-       // We want to keep the x-target on subsequent up movements
-       // that cross beyond the end of short lines. Thus a special
-       // handling when the cursor is at the end of line: Use the new 
-       // x-target only if the old one was before the end of line.
-       if (cur.pos() != pm.rows()[row].endpos() 
-               || (!reverseDirectionNeeded(cur) && x < cur.targetX())
-               || (reverseDirectionNeeded(cur) && x > cur.targetX())) {
-
-               x = cur.targetX();
-       }
-
-       if (!cur.selection()) {
-               int const y = bv_funcs::getPos(cur.bv(), cur, cur.boundary()).y_;
-               Cursor old = cur;
-               // Go to middle of previous row. 16 found to work OK;
-               // 12 = top/bottom margin of display math
-               int const margin = 3 * InsetMathHull::displayMargin() / 2;
-               editXY(cur, x, y - pm.rows()[row].ascent() - margin);
-               cur.clearSelection();
-
-               // This happens when you move out of an inset.
-               // And to give the DEPM the possibility of doing
-               // something we must provide it with two different
-               // cursors. (Lgb)
-               Cursor dummy = cur;
-               if (dummy == old)
-                       ++dummy.pos();
-
-               cur.bv().checkDepm(dummy, old);
-               return false;
-       }
-
-       bool updateNeeded = false;
-
-       if (row > 0) {
-               updateNeeded |= setCursor(cur, cur.pit(),
-                       tm.x2pos(cur.pit(), row - 1, x));
-       } else if (cur.pit() > 0) {
-               --cur.pit();
-               //cannot use 'par' now
-               ParagraphMetrics const & pmcur = cur.bv().parMetrics(this, cur.pit());
-               updateNeeded |= setCursor(cur, cur.pit(),
-                       tm.x2pos(cur.pit(), pmcur.rows().size() - 1, x));
-       }
-
-       cur.x_target() = x;
-
-       return updateNeeded;
-}
-
-
-bool Text::cursorDown(Cursor & cur)
-{
-       // Tell BufferView to test for FitCursor in any case!
-       cur.updateFlags(Update::FitCursor);
-
-       TextMetrics const & tm = cur.bv().textMetrics(this);
-       ParagraphMetrics const & pm = tm.parMetrics(cur.pit());
-
-       int row;
-       if (cur.pos() && cur.boundary())
-               row = pm.pos2row(cur.pos()-1);
-       else
-               row = pm.pos2row(cur.pos());
-
-       int x = cur.targetX();
-       cur.setTargetX();
-       // We want to keep the x-target on subsequent down movements
-       // that cross beyond the end of short lines. Thus a special
-       // handling when the cursor is at the end of line: Use the new 
-       // x-target only if the old one was before the end of line.
-       if (cur.pos() != pm.rows()[row].endpos() 
-               || (!cur.isRTL() && x < cur.targetX())
-               || (cur.isRTL() && x > cur.targetX())) {
-
-               x = cur.targetX();
-       }
-
-       if (!cur.selection()) {
-               int const y = bv_funcs::getPos(cur.bv(), cur, cur.boundary()).y_;
-               Cursor old = cur;
-               // To middle of next row
-               int const margin = 3 * InsetMathHull::displayMargin() / 2;
-               editXY(cur, x, y + pm.rows()[row].descent() + margin);
-               cur.clearSelection();
-
-               // This happens when you move out of an inset.
-               // And to give the DEPM the possibility of doing
-               // something we must provide it with two different
-               // cursors. (Lgb)
-               Cursor dummy = cur;
-               if (dummy == old)
-                       ++dummy.pos();
-               
-               bool const changed = cur.bv().checkDepm(dummy, old);
-
-               // Make sure that cur gets back whatever happened to dummy(Lgb)
-               if (changed)
-                       cur = dummy;
-
-               return false;
-       }
-
-       bool updateNeeded = false;
-
-       if (row + 1 < int(pm.rows().size())) {
-               updateNeeded |= setCursor(cur, cur.pit(),
-                       tm.x2pos(cur.pit(), row + 1, x));
-       } else if (cur.pit() + 1 < int(paragraphs().size())) {
-               ++cur.pit();
-               updateNeeded |= setCursor(cur, cur.pit(),
-                       tm.x2pos(cur.pit(), 0, x));
-       }
-
-       cur.x_target() = x;
-
-       return updateNeeded;
-}
-
-
 bool Text::cursorUpParagraph(Cursor & cur)
 {
        bool updated = false;
@@ -1196,12 +1145,10 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                    && oldpar.isLineSeparator(old.pos() - 1)
                    && !oldpar.isDeleted(old.pos() - 1)) {
                        oldpar.eraseChar(old.pos() - 1, cur.buffer().params().trackChanges);
-#ifdef WITH_WARNINGS
-#warning This will not work anymore when we have multiple views of the same buffer
+// FIXME: This will not work anymore when we have multiple views of the same buffer
 // In this case, we will have to correct also the cursors held by
 // other bufferviews. It will probably be easier to do that in a more
 // automated way in CursorSlice code. (JMarc 26/09/2001)
-#endif
                        // correct all cursor parts
                        if (same_par) {
                                fixCursorAfterDelete(cur.top(), old.top());
@@ -1231,7 +1178,7 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                ParagraphList & plist = old.text()->paragraphs();
                plist.erase(boost::next(plist.begin(), old.pit()));
 
-               // see #warning above
+               // see #warning (FIXME?) above 
                if (cur.depth() >= old.depth()) {
                        CursorSlice & curslice = cur[old.depth() - 1];
                        if (&curslice.inset() == &old.inset()
@@ -1280,7 +1227,7 @@ void Text::deleteEmptyParagraphMechanism(pit_type first, pit_type last, bool tra
                }
 
                // don't delete anything if this is the only remaining paragraph within the given range
-               // note: Text::acceptOrRejectChanges() sets the cursor to 'first' after calling DEPM 
+               // note: Text::acceptOrRejectChanges() sets the cursor to 'first' after calling DEPM
                if (first == last)
                        continue;