]> git.lyx.org Git - lyx.git/blobdiff - src/rowpainter.C
Point fix, earlier forgotten
[lyx.git] / src / rowpainter.C
index 4c14111611fc7129a9e066b87defa9d990db749d..a59e8385bbda8be267fc7b6cb4aaada930b43ce6 100644 (file)
@@ -6,7 +6,7 @@
  * \author various
  * \author John Levon
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
@@ -38,6 +38,7 @@
 using namespace lyx::support;
 
 using std::max;
+using std::endl;
 using lyx::pos_type;
 
 extern int PAPER_MARGIN;
@@ -61,6 +62,7 @@ class RowPainter {
 public:
        /// initialise painter
        RowPainter(BufferView const & bv, LyXText const & text,
+               ParagraphList::iterator pit,
                RowList::iterator rit, int y_offset, int x_offset, int y);
 
        /// do the painting
@@ -128,11 +130,10 @@ private:
 
 
 RowPainter::RowPainter(BufferView const & bv, LyXText const & text,
-     RowList::iterator rit,
+     ParagraphList::iterator pit, RowList::iterator rit,
      int y_offset, int x_offset, int y)
        : bv_(bv), pain_(bv_.painter()), text_(text), row_(rit),
-         pit_(text.getPar(rit)),
-               xo_(x_offset), yo_(y_offset), y_(y)
+         pit_(pit), xo_(x_offset), yo_(y_offset), y_(y)
 {}
 
 
@@ -151,7 +152,8 @@ int RowPainter::singleWidth(lyx::pos_type pos) const
 
 int RowPainter::singleWidth(lyx::pos_type pos, char c) const
 {
-       return text_.singleWidth(pit_, pos, c);
+       LyXFont const & font = text_.getFont(pit_, pos);
+       return text_.singleWidth(pit_, pos, c, font);
 }
 
 
@@ -255,7 +257,7 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos)
 void RowPainter::paintChars(pos_type & vpos, bool hebrew, bool arabic)
 {
        pos_type pos = text_.vis2log(vpos);
-       pos_type const last = lastPrintablePos(text_, pit_, row_);
+       pos_type const last = lastPrintablePos(*pit_, row_);
        LyXFont orig_font = getFont(pos);
 
        // first character
@@ -432,7 +434,7 @@ void RowPainter::paintSelection()
                        int(x_), row_->height(), LColor::selection);
 
        pos_type const body_pos = pit_->beginningOfBody();
-       pos_type const last = lastPrintablePos(text_, pit_, row_);
+       pos_type const last = lastPrintablePos(*pit_, row_);
        double tmpx = x_;
 
        for (pos_type vpos = row_->pos(); vpos <= last; ++vpos)  {
@@ -448,7 +450,7 @@ void RowPainter::paintSelection()
                                tmpx -= singleWidth(body_pos - 1);
                }
 
-               if (hfillExpansion(text_, pit_, row_, pos)) {
+               if (hfillExpansion(*pit_, row_, pos)) {
                        tmpx += singleWidth(pos);
                        if (pos >= body_pos)
                                tmpx += hfill_;
@@ -484,14 +486,14 @@ void RowPainter::paintSelection()
 void RowPainter::paintChangeBar()
 {
        pos_type const start = row_->pos();
-       pos_type const end = lastPrintablePos(text_, pit_, row_);
+       pos_type const end = lastPrintablePos(*pit_, row_);
 
        if (!pit_->isChanged(start, end))
                return;
 
-       int const height = (boost::next(row_) != text_.rows().end()
-               ? row_->height() + boost::next(row_)->top_of_text()
-               : row_->baseline());
+       int const height = (row_ == text_.lastRow())
+               ? row_->baseline()
+               : row_->height() + boost::next(row_)->top_of_text();
 
        pain_.fillRectangle(4, yo_, 5, height, LColor::changebar);
 }
@@ -523,13 +525,20 @@ void RowPainter::paintDepthBar()
                return;
 
        Paragraph::depth_type prev_depth = 0;
-       if (row_ != text_.rows().begin())
-               prev_depth = text_.getPar(boost::prior(row_))->getDepth();
-       Paragraph::depth_type next_depth = 0;
+       if (row_ != text_.firstRow()) {
+               ParagraphList::iterator pit2 = pit_;
+               if (row_ == pit2->rows.begin())
+                       --pit2;
+               prev_depth = pit2->getDepth();
+       }
 
-       RowList::iterator next_row = boost::next(row_);
-       if (next_row != text_.rows().end())
-               next_depth = text_.getPar(next_row)->getDepth();
+       Paragraph::depth_type next_depth = 0;
+       if (row_ != text_.lastRow()) {
+               ParagraphList::iterator pit2 = pit_;
+               if (boost::next(row_) == pit2->rows.end())
+                       ++pit2;
+               next_depth = pit2->getDepth();
+       }
 
        for (Paragraph::depth_type i = 1; i <= depth; ++i) {
                int const w = PAPER_MARGIN / 5;
@@ -670,7 +679,7 @@ void RowPainter::paintFirst()
                y_top += paintAppendixStart(yo_ + y_top + 2 * defaultRowHeight());
 
        // the top margin
-       if (row_ == text_.rows().begin() && !text_.isInInset())
+       if (row_ == text_.firstRow() && !text_.isInInset())
                y_top += PAPER_MARGIN;
 
        // draw a top pagebreak
@@ -682,22 +691,22 @@ void RowPainter::paintFirst()
        y_top += paintLengthMarker(_("Space above"), parparams.spaceTop(),
                        yo_ + y_top);
 
-       Buffer const * buffer = bv_.buffer();
+       Buffer const & buffer = *bv_.buffer();
 
        LyXLayout_ptr const & layout = pit_->layout();
 
-       if (buffer->params.paragraph_separation == BufferParams::PARSEP_SKIP) {
+       if (buffer.params.paragraph_separation == BufferParams::PARSEP_SKIP) {
                if (pit_ != text_.ownerParagraphs().begin()) {
                        if (layout->latextype == LATEX_PARAGRAPH
                                && !pit_->getDepth()) {
-                               y_top += buffer->params.getDefSkip().inPixels(bv_);
+                               y_top += buffer.params.getDefSkip().inPixels(bv_);
                        } else {
                                LyXLayout_ptr const & playout =
                                        boost::prior(pit_)->layout();
                                if (playout->latextype == LATEX_PARAGRAPH
                                        && !boost::prior(pit_)->getDepth()) {
                                        // is it right to use defskip here, too? (AS)
-                                       y_top += buffer->params.getDefSkip().inPixels(bv_);
+                                       y_top += buffer.params.getDefSkip().inPixels(bv_);
                                }
                        }
                }
@@ -737,12 +746,12 @@ void RowPainter::paintFirst()
                        // printed in an extra row and has a pagebreak at
                        // the top.
                        if (layout->labeltype == LABEL_COUNTER_CHAPTER) {
-                               if (buffer->params.secnumdepth >= 0) {
+                               if (buffer.params.secnumdepth >= 0) {
                                        float spacing_val = 1.0;
                                        if (!parparams.spacing().isDefault()) {
                                                spacing_val = parparams.spacing().getValue();
                                        } else {
-                                               spacing_val = buffer->params.spacing.getValue();
+                                               spacing_val = buffer.params.spacing.getValue();
                                        }
 
                                        int const maxdesc =
@@ -785,7 +794,7 @@ void RowPainter::paintFirst()
                        if (!parparams.spacing().isDefault()) {
                                spacing_val = parparams.spacing().getValue();
                        } else {
-                               spacing_val = buffer->params.spacing.getValue();
+                               spacing_val = buffer.params.spacing.getValue();
                        }
 
                        int maxdesc =
@@ -815,7 +824,7 @@ void RowPainter::paintLast()
        int y_bottom = row_->height() - 1;
 
        // the bottom margin
-       if (boost::next(row_) == text_.rows().end() && !text_.isInInset())
+       if (row_ == text_.lastRow() && !text_.isInInset())
                y_bottom -= PAPER_MARGIN;
 
        int const ww = bv_.workWidth();
@@ -887,7 +896,7 @@ void RowPainter::paintLast()
 
 void RowPainter::paintText()
 {
-       pos_type const last = lastPrintablePos(text_, pit_, row_);
+       pos_type const last = lastPrintablePos(*pit_, row_);
        pos_type body_pos = pit_->beginningOfBody();
        if (body_pos > 0 &&
                (body_pos - 1 > last || !pit_->isLineSeparator(body_pos - 1))) {
@@ -952,7 +961,7 @@ void RowPainter::paintText()
 
                        pain_.line(int(x_), y1, int(x_), y0, LColor::added_space);
 
-                       if (hfillExpansion(text_, pit_, row_, pos)) {
+                       if (hfillExpansion(*pit_, row_, pos)) {
                                int const y2 = (y0 + y1) / 2;
 
                                if (pos >= body_pos) {
@@ -993,11 +1002,11 @@ void RowPainter::paintText()
 
 void RowPainter::paint()
 {
-       width_ = text_.workWidth();
-
-       // FIXME: must be a cleaner way here. Aren't these calculations
-       // belonging to row metrics ?
-       text_.prepareToPrint(pit_, row_, x_, separator_, hfill_, label_hfill_);
+       width_       = text_.workWidth();
+       x_           = row_->x();
+       separator_   = row_->fill_separator();
+       hfill_       = row_->fill_hfill();
+       label_hfill_ = row_->fill_label_hfill();
 
        // FIXME: what is this fixing ?
        if (text_.isInInset() && x_ < 0)
@@ -1025,7 +1034,7 @@ void RowPainter::paint()
        if (row_->isParStart())
                paintFirst();
 
-       if (isParEnd(text_, pit_, row_))
+       if (isParEnd(*pit_, row_))
                paintLast();
 
        // paint text
@@ -1033,9 +1042,72 @@ void RowPainter::paint()
 }
 
 
+int paintRows(BufferView const & bv, LyXText const & text,
+       ParagraphList::iterator pit, RowList::iterator rit,
+       int xo, int y, int yf, int yo)
+{
+       //lyxerr << "  paintRows: rit: " << &*rit << endl;
+       //const_cast<LyXText&>(text).updateRowPositions();
+       int const yy = yf - y;
+       int const y2 = bv.painter().paperHeight();
+
+       ParagraphList::iterator end = text.ownerParagraphs().end();
+       bool active = false;
+
+       for ( ; pit != end; ++pit) {
+               RowList::iterator row = pit->rows.begin();
+               RowList::iterator rend = pit->rows.end();
+
+               for ( ; row != rend; ++row) {
+                       if (row == rit)
+                               active = true;
+                       if (active) {
+                               RowPainter painter(bv, text, pit, row, y + yo, xo, y + bv.top_y());
+                               painter.paint();
+                               y += row->height();
+                               if (yy + y >= y2)
+                                       return y;
+                       } else {
+                               //lyxerr << "   paintRows: row: " << &*row << " ignored" << endl;
+                       }
+               }
+       }
+
+       return y;
+}
+
 } // namespace anon
 
 
+int paintText(BufferView & bv)
+{
+       int const topy = bv.top_y();
+       ParagraphList::iterator pit;
+       RowList::iterator rit = bv.text->getRowNearY(topy, pit);
+       int y = rit->y() - topy;
+       return paintRows(bv, *bv.text, pit, rit, 0, y, y, 0);
+}
+
+
+void paintTextInset(BufferView & bv, LyXText & text, int x, int baseline)
+{
+       RowList::iterator rit = text.firstRow();
+       RowList::iterator end = text.endRow();
+       ParagraphList::iterator pit = text.ownerParagraphs().begin();
+
+       int y_offset = baseline - rit->ascent_of_text();
+       int y = y_offset;
+       while (rit != end && y + rit->height() <= 0) {
+               y += rit->height();
+               text.nextRow(pit, rit);
+       }
+       if (y_offset < 0)
+               paintRows(bv, text, pit, rit, x, 0, y, y);
+       else
+               paintRows(bv, text, pit, rit, x, 0, y_offset, y_offset);
+}
+
+
 int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
 {
        if (vsp.kind() == VSpace::NONE)
@@ -1055,55 +1127,3 @@ int getLengthMarkerHeight(BufferView const & bv, VSpace const & vsp)
        else
                return max(min_size, space_size);
 }
-
-
-void paintRowsHelper(BufferView const & bv, LyXText const & text,
-       RowList::iterator rit, int y_offset, int x_offset, int y)
-{
-       // fix up missing metrics() call for main LyXText
-       // calling metrics() directly is (a) slow and (b) crashs
-       if (&text == bv.text) {
-#if 1
-               // make sure all insets are updated
-               ParagraphList::iterator pit = text.ownerParagraphs().begin();
-               ParagraphList::iterator end = text.ownerParagraphs().end();
-
-               // compute inset metrics
-               for (; pit != end; ++pit) {
-                       InsetList & insetList = pit->insetlist;
-                       InsetList::iterator ii = insetList.begin();
-                       InsetList::iterator iend = insetList.end();
-                       for (; ii != iend; ++ii) {
-                               Dimension dim;
-                               LyXFont font;
-                               MetricsInfo mi(perv(bv), font, text.workWidth());
-                               ii->inset->metrics(mi, dim);
-                       }
-               }
-#else
-               LyXFont font;
-               Dimension dim;
-               MetricsInfo mi(perv(bv), font, text.workWidth());
-               const_cast<LyXText&>(text).metrics(mi, dim);
-#endif
-       }
-
-       RowPainter painter(bv, text, rit, y_offset, x_offset, y);
-       painter.paint();
-}
-
-
-int paintRows(BufferView const & bv, LyXText const & text,
-       RowList::iterator rit, int xo, int y, int yf, int y2, int yo)
-{
-       RowList::iterator end = text.rows().end();
-       while (rit != end && yf < y2) {
-               //const_cast<LyXText &>(text).setHeightOfRow(rit);
-               paintRowsHelper(bv, text, rit, y + yo, xo, y + text.top_y());
-               y += rit->height();
-               yf += rit->height();
-               ++rit;
-       }
-       return y;
-}
-