]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
BufferParams.cpp: change the package loading to:
[lyx.git] / src / BufferView.cpp
index 97e8d694cb39b6c6cf99054cd0ca0b23c627c3ab..eed6d0403d59b7654c77d5cc875e2105b84e9fe3 100644 (file)
@@ -134,7 +134,7 @@ BufferView::BufferView(Buffer & buf)
 
        cursor_.push(buffer_.inset());
        cursor_.resetAnchor();
-       buffer_.text().setCurrentFont(cursor_);
+       cursor_.setCurrentFont();
 
        if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
                graphics::Previews::get().generateBufferPreviews(buffer_);
@@ -339,6 +339,7 @@ void BufferView::scrollDocView(int value)
        int const h = tm.parMetrics(anchor_ref_).height();
        offset_ref_ = int((bar * t.paragraphs().size() - anchor_ref_) * h);
        updateMetrics(false);
+       buffer_.changed();
 }
 
 
@@ -457,7 +458,7 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
                // Note: only bottom (document) level pit is set.
                setCursor(doc_it);
                // set the current font.
-               buffer_.text().setCurrentFont(cursor_);
+               cursor_.setCurrentFont();
                // center the screen on this new position.
                center();
        }
@@ -469,7 +470,7 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
 void BufferView::translateAndInsert(char_type c, Text * t, Cursor & cur)
 {
        if (lyxrc.rtl_support) {
-               if (cursor_.innerText()->real_current_font.isRightToLeft()) {
+               if (cursor_.real_current_font.isRightToLeft()) {
                        if (intl_->keymap == Intl::PRIMARY)
                                intl_->keyMapSec();
                } else {
@@ -477,7 +478,7 @@ void BufferView::translateAndInsert(char_type c, Text * t, Cursor & cur)
                                intl_->keyMapPrim();
                }
        }
-       
+
        intl_->getTransManager().translateAndInsert(c, t, cur);
 }
 
@@ -592,6 +593,18 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd)
                break;
        }
 
+       case LFUN_SCREEN_UP:
+       case LFUN_SCREEN_DOWN:
+               flag.enabled(true);
+               break;
+
+       // FIXME: LFUN_SCREEN_DOWN_SELECT should be removed from
+       // everywhere else before this can enabled:
+       case LFUN_SCREEN_UP_SELECT:
+       case LFUN_SCREEN_DOWN_SELECT:
+               flag.enabled(false);
+               break;
+
        default:
                flag.enabled(false);
        }
@@ -913,12 +926,16 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                // if there is an inset at cursor, see whether it
                // wants to toggle.
                Inset * inset = cur.nextInset();
-               if (inset && inset->isActive()) {
-                       Cursor tmpcur = cur;
-                       tmpcur.pushLeft(*inset);
-                       inset->dispatch(tmpcur, tmpcmd);
-                       if (tmpcur.result().dispatched()) {
-                               cur.dispatched();
+               if (inset) {
+                       if (inset->isActive()) {
+                               Cursor tmpcur = cur;
+                               tmpcur.pushLeft(*inset);
+                               inset->dispatch(tmpcur, tmpcmd);
+                               if (tmpcur.result().dispatched()) {
+                                       cur.dispatched();
+                               }
+                       } else if (inset->editable() == Inset::IS_EDITABLE) {
+                               inset->edit(cur, true);
                        }
                }
                // if it did not work, try the underlying inset.
@@ -931,6 +948,47 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
                break;
        }
 
+       case LFUN_SCREEN_UP:
+       case LFUN_SCREEN_DOWN: {
+               Point p = bv_funcs::getPos(*this, cur, cur.boundary());
+               if (p.y_ < 0 || p.y_ > height_) {
+                       // The cursor is off-screen so recenter before proceeding.
+                       center();
+                       updateMetrics(false);
+                       //FIXME: updateMetrics() does not update paragraph position
+                       // This is done at draw() time. So we need a redraw!
+                       buffer_.changed();
+                       p = bv_funcs::getPos(*this, cur, cur.boundary());
+               }
+               scroll(cmd.action == LFUN_SCREEN_UP? - height_ : height_);
+               cur.reset(buffer_.inset());
+               text_metrics_[&buffer_.text()].editXY(cur, p.x_, p.y_);
+               //FIXME: what to do with cur.x_target()?
+               finishUndo();
+               // The metrics are already up to date. see scroll()
+               updateFlags = Update::None;
+               break;
+       }
+
+       case LFUN_SCREEN_UP_SELECT:
+       case LFUN_SCREEN_DOWN_SELECT: {
+               cur.selHandle(true);
+               size_t initial_depth = cur.depth();
+               Point const p = bv_funcs::getPos(*this, cur, cur.boundary());
+               scroll(cmd.action == LFUN_SCREEN_UP_SELECT? - height_ : height_);
+               // FIXME: We need to verify if the cursor stayed within an inset...
+               //cur.reset(buffer_.inset());
+               text_metrics_[&buffer_.text()].editXY(cur, p.x_, p.y_);
+               finishUndo();
+               while (cur.depth() > initial_depth) {
+                       cur.forwardInset();
+               }
+               // FIXME: we need to do a redraw again because of the selection
+               buffer_.changed();
+               updateFlags = Update::Force | Update::FitCursor;
+               break;
+       }
+
        default:
                updateFlags = Update::None;
        }
@@ -987,49 +1045,30 @@ void BufferView::resize(int width, int height)
 
 Inset const * BufferView::getCoveringInset(Text const & text, int x, int y)
 {
-       pit_type pit = text_metrics_[&text].getPitNearY(y);
-       BOOST_ASSERT(pit != -1);
-       Paragraph const & par = text.getPar(pit);
-
-       LYXERR(Debug::DEBUG)
-               << BOOST_CURRENT_FUNCTION
-               << ": x: " << x
-               << " y: " << y
-               << "  pit: " << pit
-               << endl;
-       InsetList::const_iterator iit = par.insetlist.begin();
-       InsetList::const_iterator iend = par.insetlist.end();
-       for (; iit != iend; ++iit) {
-               Inset * const inset = iit->inset;
-               if (inset->covers(*this, x, y)) {
-                       if (!inset->descendable())
-                               // No need to go further down if the inset is not
-                               // descendable.
-                               return inset;
-
-                       size_t cell_number = inset->nargs();
-                       // Check all the inner cell.
-                       for (size_t i = 0; i != cell_number; ++i) {
-                               Text const * inner_text = inset->getText(i);
-                               if (inner_text) {
-                                       // Try deeper.
-                                       Inset const * inset_deeper =
-                                               getCoveringInset(*inner_text, x, y);
-                                       if (inset_deeper)
-                                               return inset_deeper;
-                               }
-                       }
-
-                       LYXERR(Debug::DEBUG)
-                               << BOOST_CURRENT_FUNCTION
-                               << ": Hit inset: " << inset << endl;
-                       return inset;
+       TextMetrics & tm = text_metrics_[&text];
+       Inset * inset = tm.checkInsetHit(x, y);
+       if (!inset)
+               return 0;
+
+       if (!inset->descendable())
+               // No need to go further down if the inset is not
+               // descendable.
+               return inset;
+
+       size_t cell_number = inset->nargs();
+       // Check all the inner cell.
+       for (size_t i = 0; i != cell_number; ++i) {
+               Text const * inner_text = inset->getText(i);
+               if (inner_text) {
+                       // Try deeper.
+                       Inset const * inset_deeper =
+                               getCoveringInset(*inner_text, x, y);
+                       if (inset_deeper)
+                               return inset_deeper;
                }
        }
-       LYXERR(Debug::DEBUG)
-               << BOOST_CURRENT_FUNCTION
-               << ": No inset hit. " << endl;
-       return 0;
+
+       return inset;
 }
 
 
@@ -1070,38 +1109,30 @@ bool BufferView::workAreaDispatch(FuncRequest const & cmd0)
                        // Highlighted the newly hovered inset (if any).
                        need_redraw |= inset->setMouseHover(true);
                last_inset_ = inset;
+               if (!need_redraw)
+                       return false;
 
                // if last metrics update was in singlepar mode, WorkArea::redraw() will
                // not expose the button for redraw. We adjust here the metrics dimension
-               // to enable a full redraw.
-               // FIXME: It is possible to redraw only the area around the button!
-               if (need_redraw
-                       && metrics_info_.update_strategy == SingleParUpdate) {
-                       // FIXME: It should be possible to redraw only the area around
-                       // the button by doing this:
-                       //
-                       //metrics_info_.singlepar = false;
-                       //metrics_info_.y1 = ymin of button;
-                       //metrics_info_.y2 = ymax of button;
-                       //
-                       // Unfortunately, BufferView::draw() does not distinguish
-                       // between background updates and text updates. So we use the hammer
-                       // solution for now. We could also avoid the updateMetrics() below
-                       // by using the first and last pit of the CoordCache. Have a look
-                       // at Text::getPitNearY() to see what I mean.
-                       //
-                       //metrics_info_.pit1 = first pit of CoordCache;
-                       //metrics_info_.pit2 = last pit of CoordCache;
-                       //metrics_info_.singlepar = false;
-                       //metrics_info_.y1 = 0;
-                       //metrics_info_.y2 = height_;
-                       //
-                       updateMetrics(false);
-               }
+               // to enable a full redraw in any case as this is not costly.
+               TextMetrics & tm = text_metrics_[&buffer_.text()];
+               std::pair<pit_type, ParagraphMetrics const *> firstpm = tm.first();
+               std::pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
+               int y1 = firstpm.second->position() - firstpm.second->ascent();
+               int y2 = lastpm.second->position() + lastpm.second->descent();
+               metrics_info_ = ViewMetricsInfo(firstpm.first, lastpm.first, y1, y2,
+                       FullScreenUpdate, buffer_.text().paragraphs().size());
+               // Reinitialize anchor to first pit.
+               anchor_ref_ = firstpm.first;
+               offset_ref_ = -y1;
+               LYXERR(Debug::PAINTING)
+                       << "Mouse hover detected at: (" << cmd.x << ", " << cmd.y << ")"
+                       << "\nTriggering redraw: y1: " << y1 << " y2: " << y2
+                       << " pit1: " << firstpm.first << " pit2: " << lastpm.first << endl;
 
                // This event (moving without mouse click) is not passed further.
                // This should be changed if it is further utilized.
-               return need_redraw;
+               return true;
        }
 
        // Build temporary cursor.
@@ -1134,20 +1165,60 @@ bool BufferView::workAreaDispatch(FuncRequest const & cmd0)
 }
 
 
-void BufferView::scroll(int /*lines*/)
+void BufferView::scroll(int y)
+{
+       if (y > 0)
+               scrollDown(y);
+       else if (y < 0)
+               scrollUp(-y);
+}
+
+
+void BufferView::scrollDown(int offset)
+{
+       Text * text = &buffer_.text();
+       TextMetrics & tm = text_metrics_[text];
+       int ymax = height_ + offset;
+       while (true) {
+               std::pair<pit_type, ParagraphMetrics const *> last = tm.last();
+               int bottom_pos = last.second->position() + last.second->descent();
+               if (last.first == text->paragraphs().size() - 1) {
+                       if (bottom_pos <= height_)
+                               return;
+                       offset = min(offset, bottom_pos - height_);
+                       break;
+               }
+               if (bottom_pos > ymax)
+                       break;
+               tm.newParMetricsDown();
+       }
+       offset_ref_ += offset;
+       updateMetrics(false);
+       buffer_.changed();
+}
+
+
+void BufferView::scrollUp(int offset)
 {
-//     Text const * t = buffer_.text();
-//     int const line_height = defaultRowHeight();
-//
-//     // The new absolute coordinate
-//     int new_top_y = top_y() + lines * line_height;
-//
-//     // Restrict to a valid value
-//     new_top_y = std::min(t->height() - 4 * line_height, new_top_y);
-//     new_top_y = std::max(0, new_top_y);
-//
-//     scrollDocView(new_top_y);
-//
+       Text * text = &buffer_.text();
+       TextMetrics & tm = text_metrics_[text];
+       int ymin = - offset;
+       while (true) {
+               std::pair<pit_type, ParagraphMetrics const *> first = tm.first();
+               int top_pos = first.second->position() - first.second->ascent();
+               if (first.first == 0) {
+                       if (top_pos >= 0)
+                               return;
+                       offset = min(offset, - top_pos);
+                       break;
+               }
+               if (top_pos < ymin)
+                       break;
+               tm.newParMetricsUp();
+       }
+       offset_ref_ -= offset;
+       updateMetrics(false);
+       buffer_.changed();
 }
 
 
@@ -1249,7 +1320,7 @@ bool BufferView::mouseSetCursor(Cursor & cur)
 {
        BOOST_ASSERT(&cur.bv() == this);
 
-        // this event will clear selection so we save selection for
+       // this event will clear selection so we save selection for
        // persistent selection
        cap::saveSelection(cursor());
 
@@ -1337,17 +1408,17 @@ ViewMetricsInfo const & BufferView::viewMetricsInfo()
 void BufferView::updateMetrics(bool singlepar)
 {
        Text & buftext = buffer_.text();
-       pit_type size = int(buftext.paragraphs().size());
+       pit_type const npit = int(buftext.paragraphs().size());
 
-       if (anchor_ref_ > int(buftext.paragraphs().size() - 1)) {
-               anchor_ref_ = int(buftext.paragraphs().size() - 1);
+       if (anchor_ref_ > int(npit - 1)) {
+               anchor_ref_ = int(npit - 1);
                offset_ref_ = 0;
        }
 
        if (!singlepar) {
                // Clear out the position cache in case of full screen redraw,
                coord_cache_.clear();
-       
+
                // Clear out paragraph metrics to avoid having invalid metrics
                // in the cache from paragraphs not relayouted below
                // The complete text metrics will be redone.
@@ -1356,6 +1427,7 @@ void BufferView::updateMetrics(bool singlepar)
 
        TextMetrics & tm = textMetrics(&buftext);
 
+       pit_type const bottom_pit = cursor_.bottom().pit();
        // If the paragraph metrics has changed, we can not
        // use the singlepar optimisation.
        if (singlepar
@@ -1363,17 +1435,31 @@ void BufferView::updateMetrics(bool singlepar)
                // the (main text, not inset!) paragraph containing the cursor.
                // (if this paragraph contains insets etc., rebreaking will
                // recursively descend)
-               && tm.redoParagraph(cursor_.bottom().pit()))
-               singlepar = false;
+               && !tm.redoParagraph(bottom_pit)) {
+
+               updateOffsetRef();
+               // collect cursor paragraph iter bounds
+               ParagraphMetrics const & pm = tm.parMetrics(bottom_pit);
+               int y1 = pm.position() - pm.ascent();
+               int y2 = pm.position() + pm.descent();
+               metrics_info_ = ViewMetricsInfo(bottom_pit, bottom_pit, y1, y2,
+                       SingleParUpdate, npit);
+               LYXERR(Debug::PAINTING)
+                       << BOOST_CURRENT_FUNCTION
+                       << "\ny1: " << y1
+                       << " y2: " << y2
+                       << " pit: " << bottom_pit
+                       << " singlepar: " << singlepar
+                       << endl;
+               return;
+       }
 
        pit_type const pit = anchor_ref_;
        int pit1 = pit;
        int pit2 = pit;
-       size_t const npit = buftext.paragraphs().size();
 
        // Rebreak anchor paragraph.
-       if (!singlepar)
-               tm.redoParagraph(pit);
+       tm.redoParagraph(pit);
 
        updateOffsetRef();
 
@@ -1384,12 +1470,10 @@ void BufferView::updateMetrics(bool singlepar)
        while (y1 > 0 && pit1 > 0) {
                y1 -= tm.parMetrics(pit1).ascent();
                --pit1;
-               if (!singlepar)
-                       tm.redoParagraph(pit1);
+               tm.redoParagraph(pit1);
                y1 -= tm.parMetrics(pit1).descent();
        }
 
-
        // Take care of ascent of first line
        y1 -= tm.parMetrics(pit1).ascent();
 
@@ -1409,49 +1493,25 @@ void BufferView::updateMetrics(bool singlepar)
        while (y2 < height_ && pit2 < int(npit) - 1) {
                y2 += tm.parMetrics(pit2).descent();
                ++pit2;
-               if (!singlepar)
-                       tm.redoParagraph(pit2);
+               tm.redoParagraph(pit2);
                y2 += tm.parMetrics(pit2).ascent();
        }
 
        // Take care of descent of last line
        y2 += tm.parMetrics(pit2).descent();
 
-       // The coordinates of all these paragraphs are correct, cache them
-       int y = y1;
-       CoordCache::InnerParPosCache & parPos = coord_cache_.parPos()[&buftext];
-       for (pit_type pit = pit1; pit <= pit2; ++pit) {
-               ParagraphMetrics const & pm = tm.parMetrics(pit);
-               y += pm.ascent();
-               parPos[pit] = Point(0, y);
-               if (singlepar && pit == cursor_.bottom().pit()) {
-                       // In Single Paragraph mode, collect here the
-                       // y1 and y2 of the (one) paragraph the cursor is in
-                       y1 = y - pm.ascent();
-                       y2 = y + pm.descent();
-               }
-               y += pm.descent();
-       }
-
-       if (singlepar) {
-               // collect cursor paragraph iter bounds
-               pit1 = cursor_.bottom().pit();
-               pit2 = cursor_.bottom().pit();
-       }
-
-       LYXERR(Debug::DEBUG)
+       LYXERR(Debug::PAINTING)
                << BOOST_CURRENT_FUNCTION
-               << " y1: " << y1
+               << "\n y1: " << y1
                << " y2: " << y2
                << " pit1: " << pit1
                << " pit2: " << pit2
                << " npit: " << npit
                << " singlepar: " << singlepar
-               << "size: " << size
                << endl;
 
        metrics_info_ = ViewMetricsInfo(pit1, pit2, y1, y2,
-               singlepar? SingleParUpdate: FullScreenUpdate, size);
+               FullScreenUpdate, npit);
 
        if (lyxerr.debugging(Debug::WORKAREA)) {
                LYXERR(Debug::WORKAREA) << "BufferView::updateMetrics" << endl;
@@ -1517,7 +1577,7 @@ void BufferView::menuInsertLyXFile(string const & filenm)
                el = buf.errorList("Parse");
                recordUndo(cursor_);
                cap::pasteParagraphList(cursor_, buf.paragraphs(),
-                                            buf.params().getTextClass_ptr(), el);
+                                            buf.params().getTextClassPtr(), el);
                res = _("Document %1$s inserted.");
        } else
                res = _("Could not insert document %1$s");
@@ -1531,47 +1591,38 @@ void BufferView::menuInsertLyXFile(string const & filenm)
 
 void BufferView::draw(frontend::Painter & pain)
 {
-       Text & text = buffer_.text();
-       bool const select = cursor_.selection();
-
        PainterInfo pi(this, pain);
        // Should the whole screen, including insets, be refreshed?
        // FIXME: We should also distinguish DecorationUpdate to avoid text
        // drawing if possible. This is not possible to do easily right now
        // because of the single backing pixmap.
-       pi.full_repaint = select 
-               || metrics_info_.update_strategy != SingleParUpdate;
+       pi.full_repaint = metrics_info_.update_strategy != SingleParUpdate;
 
        if (pi.full_repaint)
                // Clear background (if not delegated to rows)
                pain.fillRectangle(0, metrics_info_.y1, width_,
-                       metrics_info_.y2 - metrics_info_.y1, text.backgroundColor());
-
-       TextMetrics const & tm = text_metrics_[&text];
-
-       if (select)
-               tm.drawSelection(pi, 0, 0);
+                       metrics_info_.y2 - metrics_info_.y1,
+                       buffer_.inset().backgroundColor());
 
-       int yy = metrics_info_.y1;
-       // draw contents
        LYXERR(Debug::PAINTING) << "\t\t*** START DRAWING ***" << endl;
-       for (pit_type pit = metrics_info_.p1; pit <= metrics_info_.p2; ++pit) {
-               ParagraphMetrics const & pm = tm.parMetrics(pit);
-               yy += pm.ascent();
-               tm.drawParagraph(pi, pit, 0, yy);
-               yy += pm.descent();
-       }
+       Text & text = buffer_.text();
+       TextMetrics const & tm = text_metrics_[&text];
+       int y = metrics_info_.y1 + tm.parMetrics(metrics_info_.p1).ascent();
+       if (!pi.full_repaint)
+               tm.drawParagraph(pi, metrics_info_.p1, 0, y);
+       else
+               tm.draw(pi, 0, y);
        LYXERR(Debug::PAINTING) << "\n\t\t*** END DRAWING  ***" << endl;
 
        // and grey out above (should not happen later)
 //     lyxerr << "par ascent: " << text.getPar(metrics_info_.p1).ascent() << endl;
-       if (metrics_info_.y1 > 0 
+       if (metrics_info_.y1 > 0
                && metrics_info_.update_strategy == FullScreenUpdate)
                pain.fillRectangle(0, 0, width_, metrics_info_.y1, Color::bottomarea);
 
        // and possibly grey out below
 //     lyxerr << "par descent: " << text.getPar(metrics_info_.p1).ascent() << endl;
-       if (metrics_info_.y2 < height_ 
+       if (metrics_info_.y2 < height_
                && metrics_info_.update_strategy == FullScreenUpdate)
                pain.fillRectangle(0, metrics_info_.y2, width_,
                        height_ - metrics_info_.y2, Color::bottomarea);