]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
EmbeddedObjects.lyx: add hint how to force a rotation direction for rotated floats
[lyx.git] / src / Text.cpp
index 8a168e8140fa589b31dbfbc7b3486a10c48cad70..3c21222839045e6e60d0a50f524920676260f61e 100644 (file)
@@ -5,9 +5,11 @@
  *
  * \author Asger Alstrup
  * \author Lars Gullik Bjønnes
+ * \author Dov Feldstern
  * \author Jean-Marc Lasgouttes
  * \author John Levon
  * \author André Pönitz
+ * \author Stefan Schimanski
  * \author Dekel Tsur
  * \author Jürgen Vigna
  *
@@ -367,7 +369,8 @@ int Text::singleWidth(Paragraph const & par,
        if (isPrintable(c)) {
                Language const * language = font.language();
                if (language->rightToLeft()) {
-                       if (language->lang() == "arabic") {
+                       if (language->lang() == "arabic" ||
+                           language->lang() == "farsi") {
                                if (Encodings::isComposeChar_arabic(c))
                                        return 0;
                                c = par.transformChar(c, pos);
@@ -711,7 +714,51 @@ void Text::insertChar(Cursor & cur, char_type c)
                }
        }
 
-       // First check, if there will be two blanks together or a blank at
+       // In Bidi text, we want spaces to be treated in a special way: spaces
+       // which are between words in different languages should get the 
+       // paragraph's language; otherwise, spaces should keep the language 
+       // they were originally typed in. This is only in effect while typing;
+       // after the text is already typed in, the user can always go back and
+       // explicitly set the language of a space as desired. But 99.9% of the
+       // time, what we're doing here is what the user actually meant.
+       // 
+       // The following cases are the ones in which the language of the space
+       // should be changed to match that of the containing paragraph. In the
+       // depictions, lowercase is LTR, uppercase is RTL, underscore (_) 
+       // represents a space, pipe (|) represents the cursor position (so the
+       // character before it is the one just typed in). The different cases
+       // are depicted logically (not visually), from left to right:
+       // 
+       // 1. A_a|
+       // 2. a_A|
+       //
+       // Theoretically, there are other situations that we should, perhaps, deal
+       // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any 
+       // point (to understand why, just try to create this situation...).
+
+       if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
+               // get font in front and behind the space in question. But do NOT 
+               // use getFont(cur.pos()) because the character c is not inserted yet
+               Font const & pre_space_font  = getFont(buffer, par, cur.pos() - 2);
+               Font const & post_space_font = real_current_font;
+               bool pre_space_rtl  = pre_space_font.isVisibleRightToLeft();
+               bool post_space_rtl = post_space_font.isVisibleRightToLeft();
+               
+               if (pre_space_rtl != post_space_rtl) {
+                       // Set the space's language to match the language of the 
+                       // adjacent character whose direction is the paragraph's
+                       // direction; don't touch other properties of the font
+                       Language const * lang = 
+                               (pre_space_rtl == par.isRightToLeftPar(buffer.params())) ?
+                               pre_space_font.language() : post_space_font.language();
+
+                       Font space_font = getFont(buffer, par, cur.pos() - 1);
+                       space_font.setLanguage(lang);
+                       par.setFont(cur.pos() - 1, space_font);
+               }
+       }
+       
+       // Next check, if there will be two blanks together or a blank at
        // the beginning of a paragraph.
        // I decided to handle blanks like normal characters, the main
        // difference are the special checks when calculating the row.fill
@@ -858,7 +905,7 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
        bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
 
        // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
-       
+
        for (pit_type pit = begPit; pit <= endPit; ++pit) {
                pos_type parSize = pars_[pit].size();
 
@@ -872,7 +919,7 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
                        continue;
 
                // do not consider last paragraph if the cursor ends at pos 0
-               if (pit == endPit && endPos == 0) 
+               if (pit == endPit && endPos == 0)
                        break; // last iteration anyway
 
                pos_type left  = (pit == begPit ? begPos : 0);
@@ -884,9 +931,9 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
                        pars_[pit].rejectChanges(cur.buffer().params(), left, right);
                }
        }
-       
+
        // next, accept/reject imaginary end-of-par characters
+
        for (pit_type pit = begPit; pit <= endPit; ++pit) {
                pos_type pos = pars_[pit].size();
 
@@ -933,7 +980,7 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 
        deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer().params().trackChanges);
 
-       // 
+       //
 
        finishUndo();
        cur.clearSelection();
@@ -945,12 +992,12 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 
 void Text::acceptChanges(BufferParams const & bparams)
 {
-       pit_type pars_size = static_cast<pit_type>(pars_.size());
+       pit_type pars_size = static_cast<pit_type>(pars_.size());
 
        // first, accept changes within each individual paragraph
        // (do not consider end-of-par)
        for (pit_type pit = 0; pit < pars_size; ++pit) {
-               if (!pars_[pit].empty())   // prevent assertion failure 
+               if (!pars_[pit].empty())   // prevent assertion failure
                        pars_[pit].acceptChanges(bparams, 0, pars_[pit].size());
        }
 
@@ -983,9 +1030,9 @@ void Text::rejectChanges(BufferParams const & bparams)
        pit_type pars_size = static_cast<pit_type>(pars_.size());
 
        // first, reject changes within each individual paragraph
-       // (do not consider end-of-par)         
+       // (do not consider end-of-par)
        for (pit_type pit = 0; pit < pars_size; ++pit) {
-               if (!pars_[pit].empty())   // prevent assertion failure 
+               if (!pars_[pit].empty())   // prevent assertion failure
                        pars_[pit].rejectChanges(bparams, 0, pars_[pit].size());
        }
 
@@ -1102,7 +1149,7 @@ void Text::changeCase(Cursor & cur, Text::TextCase action)
                pos_type pos = (pit == begPit ? begPos : 0);
                right = (pit == endPit ? endPos : parSize);
 
-               // process sequences of modified characters; in change 
+               // process sequences of modified characters; in change
                // tracking mode, this approach results in much better
                // usability than changing case on a char-by-char basis
                docstring changes;
@@ -1170,6 +1217,38 @@ void Text::changeCase(Cursor & cur, Text::TextCase action)
 }
 
 
+bool Text::handleBibitems(Cursor & cur)
+{
+       if (cur.paragraph().layout()->labeltype != LABEL_BIBLIO)
+               return false;
+       // 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()) {
+                       recordUndo(cur, Undo::ATOMIC, 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());
+               return true;
+       }
+       return false;
+}
+
+
 bool Text::erase(Cursor & cur)
 {
        BOOST_ASSERT(this == cur.text());
@@ -1200,13 +1279,15 @@ bool Text::erase(Cursor & cur)
                }
        }
 
+       needsUpdate |= handleBibitems(cur);
+
        if (needsUpdate) {
                // 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);
        }
-       
+
        return needsUpdate;
 }
 
@@ -1229,14 +1310,14 @@ bool Text::backspacePos0(Cursor & cur)
        Paragraph const & prevpar = prevcur.paragraph();
 
        // is it an empty paragraph?
-       if (cur.lastpos() == 0 
+       if (cur.lastpos() == 0
            || (cur.lastpos() == 1 && par.isSeparator(0))) {
                recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
                plist.erase(boost::next(plist.begin(), cur.pit()));
                needsUpdate = true;
        }
        // is previous par empty?
-       else if (prevcur.lastpos() == 0 
+       else if (prevcur.lastpos() == 0
                 || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
                recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
                plist.erase(boost::next(plist.begin(), prevcur.pit()));
@@ -1298,6 +1379,8 @@ bool Text::backspace(Cursor & cur)
        if (cur.pos() == cur.lastpos())
                setCurrentFont(cur);
 
+       needsUpdate |= handleBibitems(cur);
+
        // A singlePar update is not enough in this case.
 //             cur.updateFlags(Update::Force);
        setCursor(cur.top(), cur.pit(), cur.pos());
@@ -1375,91 +1458,151 @@ void Text::drawSelection(PainterInfo & pi, int x, int) const
        DocIterator end = cur.selectionEnd();
 
        BufferView & bv = *pi.base.bv;
-       Buffer const & buffer = *bv.buffer();
 
-       // the selection doesn't touch the visible screen
+       // the selection doesn't touch the visible screen?
        if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
            || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
                return;
 
-       Paragraph const & par1 = pars_[beg.pit()];
-       Paragraph const & par2 = pars_[end.pit()];
        TextMetrics const & tm = bv.textMetrics(this);
        ParagraphMetrics const & pm1 = tm.parMetrics(beg.pit());
        ParagraphMetrics const & pm2 = tm.parMetrics(end.pit());
+       Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
+       Row const & row2 = pm2.getRow(end.pos(), end.boundary());
+
+       // clip above
+       int middleTop;
+       bool const clipAbove = 
+               (bv_funcs::status(&bv, beg) == bv_funcs::CUR_ABOVE);
+       if (clipAbove)
+               middleTop = 0;
+       else
+               middleTop = bv_funcs::getPos(bv, beg, beg.boundary()).y_ + row1.descent();
+       
+       // clip below
+       int middleBottom;
+       bool const clipBelow = 
+               (bv_funcs::status(&bv, end)     == bv_funcs::CUR_BELOW);
+       if (clipBelow)
+               middleBottom = bv.workHeight();
+       else
+               middleBottom = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
 
-       bool const above = (bv_funcs::status(&bv, beg)
-                           == bv_funcs::CUR_ABOVE);
-       bool const below = (bv_funcs::status(&bv, end)
-                           == bv_funcs::CUR_BELOW);
-       int y1,y2,x1,x2;
-       if (above) {
-               y1 = 0;
-               y2 = 0;
-               x1 = 0;
-               x2 = tm.width();
-       } else {
-               Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
-               y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
-               y2 = y1 + row1.height();
-               int const startx = cursorX(bv, beg.top(), beg.boundary());
-               if (!isRTL(buffer, par1)) {
-                       x1 = startx;
-                       x2 = 0 + tm.width();
+       // start and end in the same line?
+       if (!(clipAbove || clipBelow) && &row1 == &row2)
+               // then only draw this row's selection
+               drawRowSelection(pi, x, row1, beg, end, false, false);
+       else {
+               if (!clipAbove) {
+                       // get row end
+                       DocIterator begRowEnd = beg;
+                       begRowEnd.pos() = row1.endpos();
+                       begRowEnd.boundary(true);
+                       
+                       // draw upper rectangle
+                       drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
                }
-               else {
-                       x1 = 0;
-                       x2 = startx;
+                       
+               if (middleTop < middleBottom) {
+                       // draw middle rectangle
+                       pi.pain.fillRectangle(x, middleTop, 
+                                                                                                               tm.width(), middleBottom - middleTop, 
+                                                                                                               Color::selection);
                }
-       }
 
-       int Y1,Y2,X1,X2;
-       if (below) {
-               Y1 = bv.workHeight();
-               Y2 = bv.workHeight();
-               X1 = 0;
-               X2 = tm.width();
-       } else {
-               Row const & row2 = pm2.getRow(end.pos(), end.boundary());
-               Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
-               Y2 = Y1 + row2.height();
-               int const endx = cursorX(bv, end.top(), end.boundary());
-               if (!isRTL(buffer, par2)) {
-                       X1 = 0;
-                       X2 = endx;
-               }
-               else {
-                       X1 = endx;
-                       X2 = 0 + tm.width();
+               if (!clipBelow) {
+                       // get row begin
+                       DocIterator endRowBeg = end;
+                       endRowBeg.pos() = row2.pos();
+                       endRowBeg.boundary(false);
+                       
+                       // draw low rectangle
+                       drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
                }
        }
+}
 
-       if (!above && !below && &pm1.getRow(beg.pos(), beg.boundary())
-           == &pm2.getRow(end.pos(), end.boundary()))
-       {
-               // paint only one rectangle
-               int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
-               int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
-               pi.pain.fillRectangle(b, y1, w, y2 - y1, Color::selection);
-               return;
-       }
-
-       LYXERR(Debug::DEBUG) << " y1: " << y1 << " y2: " << y2
-               << "X1:" << X1 << " x2: " << X2 << " wid: " << tm.width()
-               << endl;
 
-       // paint upper rectangle
-       pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
-                                     Color::selection);
-       // paint bottom rectangle
-       pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
-                                     Color::selection);
-       // paint center rectangle
-       pi.pain.fillRectangle(x, y2, tm.width(),
-                             Y1 - y2, Color::selection);
+void Text::drawRowSelection(PainterInfo & pi, int x, Row const & row,
+                                                                                                               DocIterator const & beg, DocIterator const & end, 
+                                                                                                               bool drawOnBegMargin, bool drawOnEndMargin) const
+{
+       BufferView & bv = *pi.base.bv;
+       Buffer & buffer = *bv.buffer();
+       TextMetrics const & tm = bv.textMetrics(this);
+       DocIterator cur = beg;
+       int x1 = cursorX(bv, beg.top(), beg.boundary());
+       int x2 = cursorX(bv, end.top(), end.boundary());
+       int y1 = bv_funcs::getPos(bv, cur, cur.boundary()).y_ - row.ascent();
+       int y2 = y1 + row.height();
+       
+       // draw the margins
+       if (drawOnBegMargin) {
+               if (isRTL(buffer, beg.paragraph()))
+                       pi.pain.fillRectangle(x + x1, y1, tm.width() - x1, y2 - y1, Color::selection);
+               else
+                       pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color::selection);
+       }
+       
+       if (drawOnEndMargin) {
+               if (isRTL(buffer, beg.paragraph()))
+                       pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color::selection);
+               else
+                       pi.pain.fillRectangle(x + x2, y1, tm.width() - x2, y2 - y1, Color::selection);
+       }
+       
+       // if we are on a boundary from the beginning, it's probably
+       // a RTL boundary and we jump to the other side directly as this
+       // segement is 0-size and confuses the logic below
+       if (cur.boundary())
+               cur.boundary(false);
+       
+       // go through row and draw from RTL boundary to RTL boundary
+       while (cur < end) {
+               bool drawNow = false;
+               
+               // simplified cursorRight code below which does not
+               // descend into insets and which does not go into the
+               // next line. Compare the logic with the original cursorRight
+               
+               // if left of boundary -> just jump to right side
+               // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
+               if (cur.boundary()) {
+                       cur.boundary(false);
+               }       else if (isRTLBoundary(buffer, cur.paragraph(), cur.pos() + 1)) {
+                       // in front of RTL boundary -> Stay on this side of the boundary because:
+                       //   ab|cDDEEFFghi -> abc|DDEEFFghi
+                       ++cur.pos();
+                       cur.boundary(true);
+                       drawNow = true;
+               } else {
+                       // move right
+                       ++cur.pos();
+                       
+                       // line end?
+                       if (cur.pos() == row.endpos())
+                               cur.boundary(true);
+               }
+                       
+               if (x1 == -1) {
+                       // the previous segment was just drawn, now the next starts
+                       x1 = cursorX(bv, cur.top(), cur.boundary());
+               }
+               
+               if (!(cur < end) || drawNow) {
+                       x2 = cursorX(bv, cur.top(), cur.boundary());
+                       pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
+                                                                                                               Color::selection);
+                       
+                       // reset x1, so it is set again next round (which will be on the 
+                       // right side of a boundary or at the selection end)
+                       x1 = -1;
+               }
+       }
 }
 
 
+
 bool Text::isLastRow(pit_type pit, Row const & row) const
 {
        return row.endpos() >= pars_[pit].size()
@@ -1597,6 +1740,8 @@ int Text::cursorX(BufferView const & bv, CursorSlice const & sl,
        Buffer const & buffer = *bv.buffer();
        RowMetrics const m = tm.computeRowMetrics(pit, row);
        double x = m.x;
+       Bidi bidi;
+       bidi.computeTables(par, buffer, row);
 
        pos_type const row_pos  = row.pos();
        pos_type const end      = row.endpos();
@@ -1650,20 +1795,13 @@ int Text::cursorX(BufferView const & bv, CursorSlice const & sl,
        }
 
        // see correction above
-       if (boundary_correction)
-               if (getFont(buffer, par, ppos).isVisibleRightToLeft())
+       if (boundary_correction) {
+               if (isRTL(buffer, sl, boundary))
                        x -= singleWidth(buffer, par, ppos);
                else
                        x += singleWidth(buffer, par, ppos);
-
-       // Make sure inside an inset we always count from the left
-       // edge (bidi!) -- MV
-       if (sl.pos() < par.size()) {
-               font = getFont(buffer, par, sl.pos());
-               if (!boundary && font.isVisibleRightToLeft()
-                 && par.isInset(sl.pos()))
-                       x -= par.getInset(sl.pos())->width();
        }
+
        return int(x);
 }
 
@@ -1813,19 +1951,30 @@ docstring Text::getPossibleLabel(Cursor & cur) const
        if (caption_inset)
                name = from_ascii(static_cast<InsetCaption *>(caption_inset)->type());
 
-       // Inside floats or wraps, if none of the above worked
-       // we want by default the abbreviation of the float type.
+       // If none of the above worked, we'll see if we're inside various
+       // types of insets and take our abbreviation from them.
        if (name.empty()) {
-               Inset * float_inset = cur.innerInsetOfType(Inset::FLOAT_CODE);
-               if (!float_inset)
-                       float_inset = cur.innerInsetOfType(Inset::WRAP_CODE);
-               if (float_inset)
-                       name = float_inset->name();
+               Inset::Code const codes[] = {
+                       Inset::FLOAT_CODE,
+                       Inset::WRAP_CODE,
+                       Inset::FOOT_CODE
+               };
+               for (unsigned int i = 0; i < (sizeof codes / sizeof codes[0]); ++i) {
+                       Inset * float_inset = cur.innerInsetOfType(codes[i]);
+                       if (float_inset) {
+                               name = float_inset->name();
+                               break;
+                       }
+               }
        }
 
        // Create a correct prefix for prettyref
        if (name == "theorem")
                name = from_ascii("thm");
+       else if (name == "Foot")
+               name = from_ascii("fn");
+       else if (name == "listing")
+               name = from_ascii("lst");
 
        if (!name.empty())
                text = name.substr(0, 3) + ':' + text;
@@ -1869,7 +2018,7 @@ void Text::setCursorFromCoordinates(Cursor & cur, int const x, int const y)
 
        bool bound = false;
        int xx = x;
-       pos_type const pos = row.pos() 
+       pos_type const pos = row.pos()
                + tm.getColumnNearX(pit, row, xx, bound);
 
        LYXERR(Debug::DEBUG)
@@ -1896,7 +2045,7 @@ void Text::charsTranspose(Cursor & cur)
 
        Paragraph & par = cur.paragraph();
 
-       // Get the positions of the characters to be transposed. 
+       // Get the positions of the characters to be transposed.
        pos_type pos1 = pos - 1;
        pos_type pos2 = pos;
 
@@ -1919,7 +2068,7 @@ void Text::charsTranspose(Cursor & cur)
        char_type char1 = par.getChar(pos1);
        Font const font1 =
                par.getFontSettings(cur.buffer().params(), pos1);
-       
+
        char_type char2 = par.getChar(pos2);
        Font const font2 =
                par.getFontSettings(cur.buffer().params(), pos2);