]> git.lyx.org Git - lyx.git/blobdiff - src/Text2.cpp
* do not lookup the same macro all the time
[lyx.git] / src / Text2.cpp
index e893889a299eefd7fd87a38daa3952d65746ada5..026600e79e03aa91f584012a38688e51785cfedd 100644 (file)
@@ -270,6 +270,9 @@ Font Text::getLabelFont(Buffer const & buffer, Paragraph const & par) const
 void Text::setCharFont(Buffer const & buffer, pit_type pit,
                pos_type pos, Font const & fnt)
 {
+       BOOST_ASSERT(!pars_[pit].isInset(pos) ||
+                    !pars_[pit].getInset(pos)->noFontChange());
+
        Font font = fnt;
        Layout_ptr const & layout = pars_[pit].layout();
 
@@ -307,6 +310,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)
 {
@@ -433,7 +466,6 @@ 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());
@@ -464,20 +496,36 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
        // 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 insetbase.h
+                               setInsetFont(buffer, pit, pos, font, toggleall);
+                       else {
+                               Font f = getFont(buffer, dit.paragraph(), pos);
+                               f.update(font, language, toggleall);
+                               setCharFont(buffer, pit, pos, f);
+                       }
                }
        }
 }
@@ -717,8 +765,12 @@ void Text::setCurrentFont(Cursor & cur)
        pos_type pos = cur.pos();
        Paragraph & par = cur.paragraph();
 
-       if (cur.boundary() && pos > 0)
+       if (cur.boundary() && pos > 0 && pos < cur.lastpos()) {
                --pos;
+               // We may have just moved to the previous row --- 
+               // we're going to be needing its bidi tables!
+               bidi.computeTables(par, cur.buffer(), cur.textRow());
+       }
 
        if (pos > 0) {
                if (pos == cur.lastpos())
@@ -904,9 +956,18 @@ bool Text::checkAndActivateInset(Cursor & cur, bool front)
                return false;
        if (cur.pos() == cur.lastpos())
                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,27 +978,35 @@ 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 (cur.pos() > 0) {
+               if (cur.boundary())
+                       return setCursor(cur, cur.pit(), cur.pos(), true, false);
+
+               bool updateNeeded = false;
+               // If checkAndActivateInset returns true, that means that
+               // the cursor was placed inside it, so we're done
                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);
-                       */
+                       if (!cur.boundary() && 
+                           cur.textRow().pos() == cur.pos() 
+                           // FIXME: the following two conditions are copied
+                           // from cursorRight; however, isLineSeparator()
+                           // is definitely wrong here, isNewline I'm not sure
+                           // about. I'm leaving them as comments for now,
+                           // until we understand why they should or shouldn't
+                           // be here.
+                           /*&& 
+                           !cur.paragraph().isLineSeparator(cur.pos()-1) &&
+                           !cur.paragraph().isNewline(cur.pos() - 1)*/) {
+                               updateNeeded |= setCursor(cur, cur.pit(), cur.pos(), 
+                                                                                 true, true);
+                       }
+                       updateNeeded |= setCursor(cur, cur.pit(),cur.pos() - 1, 
+                                                                         true, false);
                }
                return updateNeeded;
        }
 
-       if (cur.pit() != 0) {
+       if (cur.pit() > 0) {
                // Steps into the paragraph above
                return setCursor(cur, cur.pit() - 1, getPar(cur.pit() - 1).size());
        }
@@ -956,6 +1025,8 @@ bool Text::cursorRight(Cursor & cur)
                                         true, false);
 
                bool updateNeeded = false;
+               // If checkAndActivateInset returns true, that means that 
+               // the cursor was placed inside it, so we're done
                if (!checkAndActivateInset(cur, true)) {
                        if (cur.textRow().endpos() == cur.pos() + 1 &&
                            cur.textRow().endpos() != cur.lastpos() &&
@@ -964,9 +1035,6 @@ bool Text::cursorRight(Cursor & cur)
                                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);
                }
                return updateNeeded;
        }