]> git.lyx.org Git - lyx.git/blobdiff - src/TextMetrics.cpp
Revert 23154.
[lyx.git] / src / TextMetrics.cpp
index 737024af1b7439cf8884bfe495398f0533d88005..a9ce0f04dc7321b9f232e0152e888f33f97b9eb9 100644 (file)
@@ -27,7 +27,6 @@
 #include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
-#include "support/debug.h"
 #include "FontIterator.h"
 #include "FuncRequest.h"
 #include "InsetList.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
-using std::make_pair;
-using std::max;
-using std::min;
-using std::pair;
+#include "support/debug.h"
+
+using namespace std;
 
 namespace lyx {
 
@@ -223,13 +221,13 @@ bool TextMetrics::metrics(MetricsInfo & mi, Dimension & dim, int min_width)
 
 int TextMetrics::rightMargin(ParagraphMetrics const & pm) const
 {
-       return main_text_? pm.rightMargin(bv_->buffer()) : 0;
+       return main_text_? pm.rightMargin(*bv_) : 0;
 }
 
 
 int TextMetrics::rightMargin(pit_type const pit) const
 {
-       return main_text_? par_metrics_[pit].rightMargin(bv_->buffer()) : 0;
+       return main_text_? par_metrics_[pit].rightMargin(*bv_) : 0;
 }
 
 
@@ -378,35 +376,37 @@ bool TextMetrics::redoParagraph(pit_type const pit)
        // so better to calculate that once here.
        int const right_margin = rightMargin(pm);
 
+       // iterator pointing to paragraph to resolve macros
+       DocIterator parPos = text_->macrocontextPosition();
+       if (!parPos.empty())
+               parPos.pit() = pit;
+       else {
+               LYXERR(Debug::INFO, "MacroContext not initialised!"
+                       << " Going through the buffer again and hope"
+                       << " the context is better then.");
+               updateLabels(bv_->buffer());
+               parPos = text_->macrocontextPosition();
+               BOOST_ASSERT(!parPos.empty());
+               parPos.pit() = pit;
+       }
+       
        // redo insets
        // FIXME: We should always use getFont(), see documentation of
        // noFontChange() in Inset.h.
        Font const bufferfont = buffer.params().getFont();
-       MacroContext mc(buffer, par);
        InsetList::const_iterator ii = par.insetList().begin();
        InsetList::const_iterator iend = par.insetList().end();
        for (; ii != iend; ++ii) {
-               // the macro must come here, _before_ the metric call, because
-               // the macro should see itself to detect recursions. To find out
-               // whether the macro definition is a redefinition it will look
-               // at the MacroData::redefinition_. So it doesn't get confused
-               // by the already existing macro definition of itself in the 
-               // macro context.
-               if (ii->inset->lyxCode() == MATHMACRO_CODE) {
-                       // get macro data
-                       MathMacroTemplate const & macroTemplate
-                       = static_cast<MathMacroTemplate const &>(*ii->inset);
-
-                       // valid?
-                       if (macroTemplate.validMacro()) {
-                               MacroData macro = macroTemplate.asMacroData();
-
-                               // redefinition?
-                               macro.setRedefinition(mc.has(macroTemplate.name()));
-
-                               // register macro (possibly overwrite the previous one of this paragraph)
-                               mc.insert(macroTemplate.name(), macro);
-                       }
+               // position already initialized?
+               if (!parPos.empty()) {
+                       parPos.pos() = ii->pos;
+               
+                       // A macro template would normally not be visible 
+                       // by itself. But the tex macro semantics allow 
+                       // recursion, so we artifically take the context
+                       // after the macro template to simulate this.
+                       if (ii->inset->lyxCode() == MATHMACRO_CODE)
+                               parPos.pos()++;
                }
 
                // do the metric calculation
@@ -415,6 +415,7 @@ bool TextMetrics::redoParagraph(pit_type const pit)
                        - right_margin;
                Font const & font = ii->inset->noFontChange() ?
                        bufferfont : getDisplayFont(pit, ii->pos);
+               MacroContext mc(buffer, parPos);
                MetricsInfo mi(bv_, font.fontInfo(), w, mc);
                ii->inset->metrics(mi, dim);
                Dimension const old_dim = pm.insetDimension(ii->inset);
@@ -476,7 +477,7 @@ bool TextMetrics::redoParagraph(pit_type const pit)
                first = end;
                ++row_index;
 
-               pm.dim().wid = std::max(pm.dim().wid, dim.wid);
+               pm.dim().wid = max(pm.dim().wid, dim.wid);
                pm.dim().des += dim.height();
        } while (first < par.size());
 
@@ -513,7 +514,6 @@ void TextMetrics::computeRowMetrics(pit_type const pit,
                Row & row, int width) const
 {
        row.label_hfill = 0;
-       row.hfill = 0;
        row.separator = 0;
 
        Buffer & buffer = bv_->buffer();
@@ -553,12 +553,11 @@ void TextMetrics::computeRowMetrics(pit_type const pit,
                        row.label_hfill = labelFill(pit, row) / double(nlh);
        }
 
+       double hfill = 0;
        // are there any hfills in the row?
-       int const nh = numberOfHfills(par, row);
-
-       if (nh) {
+       if (int const nh = numberOfHfills(par, row)) {
                if (w > 0)
-                       row.hfill = w / nh;
+                       hfill = w / double(nh);
        // we don't have to look at the alignment if it is ALIGN_LEFT and
        // if the row is already larger then the permitted width as then
        // we force the LEFT_ALIGN'edness!
@@ -572,11 +571,8 @@ void TextMetrics::computeRowMetrics(pit_type const pit,
                        align = par.params().align();
 
                // Display-style insets should always be on a centred row
-               // The test on par.size() is to catch zero-size pars, which
-               // would trigger the assert in Paragraph::getInset().
-               //inset = par.size() ? par.getInset(row.pos()) : 0;
-               if (row.pos() < par.size() && par.isInset(row.pos())) {
-                       switch(par.getInset(row.pos())->display()) {
+               if (Inset const * inset = par.getInset(row.pos())) {
+                       switch (inset->display()) {
                                case Inset::AlignLeft:
                                        align = LYX_ALIGN_BLOCK;
                                        break;
@@ -653,9 +649,10 @@ void TextMetrics::computeRowMetrics(pit_type const pit,
                        continue;
                Dimension dim = row.dimension();
                if (pm.hfillExpansion(row, ii->pos))
-                       dim.wid = int(ii->pos >= body_pos ? row.hfill : row.label_hfill);
+                       dim.wid = int(ii->pos >= body_pos ?
+                               max(hfill, 5.0) : row.label_hfill);
                else
-                       dim.wid = 3;
+                       dim.wid = 5;
                // Cache the inset dimension. 
                bv_->coordCache().insets().add(ii->inset, dim);
                pm.setInsetDimension(ii->inset, dim);
@@ -733,6 +730,15 @@ pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
 
        pos_type const body_pos = par.beginOfBody();
 
+       // check for possible inline completion
+       DocIterator const & inlineCompletionPos = bv_->inlineCompletionPos();
+       pos_type inlineCompletionLPos = -1;
+       if (inlineCompletionPos.inTexted()
+           && inlineCompletionPos.text() == text_
+           && inlineCompletionPos.pit() == pit) {
+               // draw logically behind the previous character
+               inlineCompletionLPos = inlineCompletionPos.pos() - 1;
+       }
 
        // Now we iterate through until we reach the right margin
        // or the end of the par, then choose the possible break
@@ -751,6 +757,13 @@ pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
        for ( ; i < end; ++i, ++fi) {
                int thiswidth = pm.singleWidth(i, *fi);
 
+               // add inline completion width
+               if (inlineCompletionLPos == i) {
+                       docstring const & completion = bv_->inlineCompletion();
+                       if (completion.length() > 0)
+                               thiswidth += theFontMetrics(*fi).width(completion);
+               }
+
                // add the auto-hfill from label end to the body
                if (body_pos && i == body_pos) {
                        FontMetrics const & fm = theFontMetrics(
@@ -759,7 +772,7 @@ pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
                        if (par.isLineSeparator(i - 1))
                                add -= singleWidth(pit, i - 1);
 
-                       add = std::max(add, label_end - x);
+                       add = max(add, label_end - x);
                        thiswidth += add;
                }
 
@@ -784,20 +797,22 @@ pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit,
                        point = i + 1;
                        break;
                }
+               Inset const * inset = 0;
                // Break before...
                if (i + 1 < end) {
-                       if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
+                       if ((inset = par.getInset(i + 1)) && inset->display()) {
                                point = i + 1;
                                break;
                        }
                        // ...and after.
-                       if (par.isInset(i) && par.getInset(i)->display()) {
+                       if ((inset = par.getInset(i)) && inset->display()) {
                                point = i + 1;
                                break;
                        }
                }
 
-               if (!par.isInset(i) || par.getInset(i)->isChar()) {
+               inset = par.getInset(i);
+               if (!inset || inset->isChar()) {
                        // some insets are line separators too
                        if (par.isLineSeparator(i)) {
                                // register breakpoint:
@@ -831,6 +846,16 @@ int TextMetrics::rowWidth(int right_margin, pit_type const pit,
        int w = leftMargin(max_width_, pit, first);
        int label_end = labelEnd(pit);
 
+       // check for possible inline completion
+       DocIterator const & inlineCompletionPos = bv_->inlineCompletionPos();
+       pos_type inlineCompletionLPos = -1;
+       if (inlineCompletionPos.inTexted()
+           && inlineCompletionPos.text() == text_
+           && inlineCompletionPos.pit() == pit) {
+               // draw logically behind the previous character
+               inlineCompletionLPos = inlineCompletionPos.pos() - 1;
+       }
+
        pos_type const body_pos = par.beginOfBody();
        pos_type i = first;
 
@@ -846,6 +871,13 @@ int TextMetrics::rowWidth(int right_margin, pit_type const pit,
                                w = max(w, label_end);
                        }
                        w += pm.singleWidth(i, *fi);
+
+                       // add inline completion width
+                       if (inlineCompletionLPos == i) {
+                               docstring const & completion = bv_->inlineCompletion();
+                               if (completion.length() > 0)
+                                       w += theFontMetrics(*fi).width(completion);
+                       }
                }
        }
 
@@ -863,7 +895,7 @@ int TextMetrics::rowWidth(int right_margin, pit_type const pit,
 
 
 Dimension TextMetrics::rowHeight(pit_type const pit, pos_type const first,
-               pos_type const end) const
+               pos_type const end, bool topBottomSpace) const
 {
        Paragraph const & par = text_->getPar(pit);
        // get the maximum ascent and the maximum descent
@@ -934,7 +966,7 @@ Dimension TextMetrics::rowHeight(pit_type const pit, pos_type const first,
        ParagraphList const & pars = text_->paragraphs();
 
        // is it a top line?
-       if (first == 0) {
+       if (first == 0 && topBottomSpace) {
                BufferParams const & bufparams = buffer.params();
                // some parskips VERY EASY IMPLEMENTATION
                if (bufparams.paragraph_separation
@@ -966,7 +998,7 @@ Dimension TextMetrics::rowHeight(pit_type const pit, pos_type const first,
                     || layout->labeltype == LABEL_BIBLIO
                     || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
                    && isFirstInSequence(pit, pars)
-                   && !par.getLabelstring().empty())
+                   && !par.labelString().empty())
                {
                        labeladdon = int(
                                  labelfont_metrics.maxHeight()
@@ -1005,7 +1037,7 @@ Dimension TextMetrics::rowHeight(pit_type const pit, pos_type const first,
        }
 
        // is it a bottom line?
-       if (end >= par.size()) {
+       if (end >= par.size() && topBottomSpace) {
                // add the layout spaces, for example before and after
                // a section, or between the items of a itemize or enumerate
                // environment
@@ -1040,7 +1072,7 @@ Dimension TextMetrics::rowHeight(pit_type const pit, pos_type const first,
        // following code in another method specially tailored for the
        // main Text. The following test is thus bogus.
        // Top and bottom margin of the document (only at top-level)
-       if (main_text_) {
+       if (main_text_ && topBottomSpace) {
                if (pit == 0 && first == 0)
                        maxasc += 20;
                if (pit + 1 == pit_type(pars.size()) &&
@@ -1126,9 +1158,10 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit,
        bool const rtl = lastrow ? text_->isRTL(buffer, par) : false;
        if (lastrow &&
            ((rtl  &&  left_side && vc == row.pos() && x < tmpx - 5) ||
-            (!rtl && !left_side && vc == end  && x > tmpx + 5)))
-               c = end;
-       else if (vc == row.pos()) {
+            (!rtl && !left_side && vc == end  && x > tmpx + 5))) {
+               if (!par.isNewline(end - 1))
+                       c = end;
+       } else if (vc == row.pos()) {
                c = bidi.vis2log(vc);
                if (bidi.level(c) % 2 == 1)
                        ++c;
@@ -1209,7 +1242,7 @@ void TextMetrics::newParMetricsDown()
        // do it and update its position.
        redoParagraph(pit);
        par_metrics_[pit].setPosition(last.second.position()
-               + last.second.descent());
+               + last.second.descent() + par_metrics_[pit].ascent());
 }
 
 
@@ -1223,7 +1256,7 @@ void TextMetrics::newParMetricsUp()
        // do it and update its position.
        redoParagraph(pit);
        par_metrics_[pit].setPosition(first.second.position()
-               - first.second.ascent());
+               - first.second.ascent() - par_metrics_[pit].descent());
 }
 
 // y is screen coordinate
@@ -1493,6 +1526,18 @@ int TextMetrics::cursorX(CursorSlice const & sl,
            (body_pos > end || !par.isLineSeparator(body_pos - 1)))
                body_pos = 0;
 
+       // check for possible inline completion in this row
+       DocIterator const & inlineCompletionPos = bv_->inlineCompletionPos();
+       pos_type inlineCompletionVPos = -1;
+       if (inlineCompletionPos.inTexted()
+           && inlineCompletionPos.text() == text_
+           && inlineCompletionPos.pit() == pit
+           && inlineCompletionPos.pos() >= row_pos
+           && inlineCompletionPos.pos() <= end) {
+               // draw logically behind the previous character
+               inlineCompletionVPos = bidi.log2vis(inlineCompletionPos.pos() - 1);
+       }
+
        // Use font span to speed things up, see below
        FontSpan font_span;
        Font font;
@@ -1502,6 +1547,15 @@ int TextMetrics::cursorX(CursorSlice const & sl,
        if (end > 0 && end < par.size() && par.isSeparator(end - 1))
                skipped_sep_vpos = bidi.log2vis(end - 1);
        
+       // Inline completion RTL special case row_pos == cursor_pos:
+       // "__|b" => cursor_pos is right of __
+       if (row_pos == inlineCompletionVPos && row_pos == cursor_vpos) {
+               font = getDisplayFont(pit, row_pos + 1);
+               docstring const & completion = bv_->inlineCompletion();
+               if (font.isRightToLeft() && completion.length() > 0)
+                       x += theFontMetrics(font.fontInfo()).width(completion);
+       }
+       
        for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
                // Skip the separator which is at the logical end of the row
                if (vpos == skipped_sep_vpos)
@@ -1523,6 +1577,26 @@ int TextMetrics::cursorX(CursorSlice const & sl,
 
                x += pm.singleWidth(pos, font);
 
+               // Inline completion RTL case:
+               // "a__|b", __ of b => non-boundary a-pos is right of __
+               if (vpos + 1 == inlineCompletionVPos 
+                   && (vpos + 1 < cursor_vpos || !boundary_correction)) {
+                       font = getDisplayFont(pit, vpos + 1);
+                       docstring const & completion = bv_->inlineCompletion();
+                       if (font.isRightToLeft() && completion.length() > 0)
+                               x += theFontMetrics(font.fontInfo()).width(completion);
+               }
+               
+               //  Inline completion LTR case:
+               // "b|__a", __ of b => non-boundary a-pos is in front of __
+               if (vpos == inlineCompletionVPos
+                   && (vpos + 1 < cursor_vpos || boundary_correction)) {
+                       font = getDisplayFont(pit, vpos);
+                       docstring const & completion = bv_->inlineCompletion();
+                       if (!font.isRightToLeft() && completion.length() > 0)
+                               x += theFontMetrics(font.fontInfo()).width(completion);
+               }
+               
                if (par.isSeparator(pos) && pos >= body_pos)
                        x += row.separator;
        }
@@ -1541,7 +1615,7 @@ int TextMetrics::cursorX(CursorSlice const & sl,
 
 int TextMetrics::cursorY(CursorSlice const & sl, bool boundary) const
 {
-       //lyxerr << "TextMetrics::cursorY: boundary: " << boundary << std::endl;
+       //lyxerr << "TextMetrics::cursorY: boundary: " << boundary << endl;
        ParagraphMetrics const & pm = par_metrics_[sl.pit()];
        if (pm.rows().empty())
                return 0;
@@ -1651,7 +1725,7 @@ void TextMetrics::deleteLineForward(Cursor & cur)
                        text_->deleteWordForward(cur);
                else
                        cap::cutSelection(cur, true, false);
-               checkBufferStructure(cur.buffer(), cur);
+               cur.checkBufferStructure();
        }
 }
 
@@ -1698,7 +1772,7 @@ int TextMetrics::leftMargin(int max_width,
        int l_margin = 0;
 
        if (text_->isMainText(buffer))
-               l_margin += changebarMargin();
+               l_margin += bv_->leftMargin();
 
        l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
                tclass.leftmargin());
@@ -1710,6 +1784,7 @@ int TextMetrics::leftMargin(int max_width,
                        if (pars[newpar].layout()->isEnvironment()) {
                                l_margin = leftMargin(max_width, newpar);
                        }
+                       //FIXME Should this check for emptyLayout() as well?
                        if (par.layout() == tclass.defaultLayout()) {
                                if (pars[newpar].params().noindent())
                                        parindent.erase();
@@ -1735,9 +1810,9 @@ int TextMetrics::leftMargin(int max_width,
                        l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
                                layout->leftmargin);
                }
-               if (!par.getLabelstring().empty()) {
+               if (!par.labelString().empty()) {
                        l_margin += labelfont_metrics.signedWidth(layout->labelindent);
-                       l_margin += labelfont_metrics.width(par.getLabelstring());
+                       l_margin += labelfont_metrics.width(par.labelString());
                        l_margin += labelfont_metrics.width(layout->labelsep);
                }
                break;
@@ -1781,7 +1856,7 @@ int TextMetrics::leftMargin(int max_width,
                           LABEL_CENTERED_TOP_ENVIRONMENT) {
                        l_margin += labelfont_metrics.signedWidth(layout->labelindent);
                        l_margin += labelfont_metrics.width(layout->labelsep);
-                       l_margin += labelfont_metrics.width(par.getLabelstring());
+                       l_margin += labelfont_metrics.width(par.labelString());
                }
                break;
 
@@ -1821,7 +1896,7 @@ int TextMetrics::leftMargin(int max_width,
               || layout->labeltype == LABEL_TOP_ENVIRONMENT
               || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
               || (layout->labeltype == LABEL_STATIC
-                  && layout->latextype == LATEX_ENVIRONMENT
+                        && layout->latextype == LATEX_ENVIRONMENT
                   && !isFirstInSequence(pit, pars)))
            && align == LYX_ALIGN_BLOCK
            && !par.params().noindent()
@@ -1831,7 +1906,7 @@ int TextMetrics::leftMargin(int max_width,
            && !(!par.empty()
                    && par.isInset(pos)
                    && par.getInset(pos)->display())
-           && (par.layout() != tclass.defaultLayout()
+           && (par.layout() != tclass.defaultLayout() //should this check emptyLayout()?
                || buffer.params().paragraph_separation ==
                   BufferParams::PARSEP_INDENT))
        {
@@ -1936,7 +2011,7 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) co
                // 12 lines lower):
                if (lyxerr.debugging(Debug::PAINTING) && inside
                        && (row_selection || pi.full_repaint || row_has_changed)) {
-                               std::string const foreword = text_->isMainText(bv_->buffer()) ?
+                               string const foreword = text_->isMainText(bv_->buffer()) ?
                                        "main text redraw " : "inset text redraw: ";
                        LYXERR(Debug::PAINTING, foreword << "pit=" << pit << " row=" << i
                                << " row_selection="    << row_selection