]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
Transfer current_font and real_current_font from Text to Cursor.
[lyx.git] / src / BufferView.cpp
index 75566e54e03fdaead47b0a9dafc8eec55b3158bc..7fbd7187be614a96428e28cd8da0884345738d13 100644 (file)
@@ -21,6 +21,7 @@
 #include "BufferList.h"
 #include "BufferParams.h"
 #include "bufferview_funcs.h"
+#include "callback.h" // added for Dispatch functions
 #include "CoordCache.h"
 #include "CutAndPaste.h"
 #include "debug.h"
 #include "InsetIterator.h"
 #include "Language.h"
 #include "LaTeXFeatures.h"
-#include "callback.h" // added for Dispatch functions
 #include "LyX.h"
 #include "lyxfind.h"
 #include "LyXFunc.h"
 #include "Layout.h"
-#include "Text.h"
-#include "TextClass.h"
 #include "LyXRC.h"
-#include "Session.h"
+#include "MetricsInfo.h"
 #include "Paragraph.h"
 #include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "ParIterator.h"
+#include "Session.h"
 #include "TexRow.h"
+#include "Text.h"
+#include "TextClass.h"
 #include "toc.h"
 #include "Undo.h"
 #include "VSpace.h"
 #include "WordLangTuple.h"
-#include "MetricsInfo.h"
 
 #include "insets/InsetBibtex.h"
 #include "insets/InsetCommand.h" // ChangeRefs
@@ -63,6 +63,7 @@
 #include "frontends/alert.h"
 #include "frontends/FileDialog.h"
 #include "frontends/FontMetrics.h"
+#include "frontends/Painter.h"
 #include "frontends/Selection.h"
 
 #include "graphics/Previews.h"
@@ -126,14 +127,14 @@ BufferView::BufferView(Buffer & buf)
        : width_(0), height_(0), buffer_(buf), wh_(0),
          cursor_(*this),
          multiparsel_cache_(false), anchor_ref_(0), offset_ref_(0),
-         intl_(new Intl), last_inset_(0)
+         need_centering_(false), intl_(new Intl), last_inset_(0)
 {
        xsel_cache_.set = false;
        intl_->initKeyMapper(lyxrc.use_kbmap);
 
        cursor_.push(buffer_.inset());
        cursor_.resetAnchor();
-       buffer_.text().setCurrentFont(cursor_);
+       cursor_.setCurrentFont();
 
        if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
                graphics::Previews::get().generateBufferPreviews(buffer_);
@@ -142,18 +143,26 @@ BufferView::BufferView(Buffer & buf)
 
 BufferView::~BufferView()
 {
+       // current buffer is going to be switched-off, save cursor pos
+       // Ideally, the whole cursor stack should be saved, but session
+       // currently can only handle bottom (whole document) level pit and pos.
+       // That is to say, if a cursor is in a nested inset, it will be
+       // restore to the left of the top level inset.
+       LyX::ref().session().lastFilePos().save(
+               support::FileName(buffer_.fileName()),
+               boost::tie(cursor_.bottom().pit(), cursor_.bottom().pos()) );
 }
 
 
-Buffer * BufferView::buffer()
+Buffer & BufferView::buffer()
 {
-       return &buffer_;
+       return buffer_;
 }
 
 
-Buffer const * BufferView::buffer() const
+Buffer const & BufferView::buffer() const
 {
-       return &buffer_;
+       return buffer_;
 }
 
 
@@ -335,7 +344,7 @@ void BufferView::scrollDocView(int value)
 
 void BufferView::setCursorFromScrollbar()
 {
-       Text & t = buffer_.text();
+       TextMetrics & tm = text_metrics_[&buffer_.text()];
 
        int const height = 2 * defaultRowHeight();
        int const first = height;
@@ -349,14 +358,14 @@ void BufferView::setCursorFromScrollbar()
                // We reset the cursor because bv_funcs::status() does not
                // work when the cursor is within mathed.
                cur.reset(buffer_.inset());
-               t.setCursorFromCoordinates(cur, 0, first);
+               tm.setCursorFromCoordinates(cur, 0, first);
                cur.clearSelection();
                break;
        case bv_funcs::CUR_BELOW:
                // We reset the cursor because bv_funcs::status() does not
                // work when the cursor is within mathed.
                cur.reset(buffer_.inset());
-               t.setCursorFromCoordinates(cur, 0, last);
+               tm.setCursorFromCoordinates(cur, 0, last);
                cur.clearSelection();
                break;
        case bv_funcs::CUR_INSIDE:
@@ -364,7 +373,7 @@ void BufferView::setCursorFromScrollbar()
                int const newy = min(last, max(y, first));
                if (y != newy) {
                        cur.reset(buffer_.inset());
-                       t.setCursorFromCoordinates(cur, 0, newy);
+                       tm.setCursorFromCoordinates(cur, 0, newy);
                }
        }
 }
@@ -400,9 +409,12 @@ void BufferView::saveBookmark(unsigned int idx)
 }
 
 
-boost::tuple<pit_type, pos_type, int> BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
+bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
        int top_id, pos_type top_pos)
 {
+       bool success = false;
+       DocIterator doc_it;
+
        cursor_.clearSelection();
 
        // if a valid par_id is given, try it first
@@ -411,24 +423,23 @@ boost::tuple<pit_type, pos_type, int> BufferView::moveToPosition(pit_type bottom
        if (top_id > 0) {
                ParIterator par = buffer_.getParFromID(top_id);
                if (par != buffer_.par_iterator_end()) {
-                       DocIterator dit = makeDocIterator(par, min(par->size(), top_pos));
+                       doc_it = makeDocIterator(par, min(par->size(), top_pos));
                        // Some slices of the iterator may not be
                        // reachable (e.g. closed collapsable inset)
                        // so the dociterator may need to be
                        // shortened. Otherwise, setCursor may crash
                        // lyx when the cursor can not be set to these
                        // insets.
-                       size_t const n = dit.depth();
+                       size_t const n = doc_it.depth();
                        for (size_t i = 0; i < n; ++i)
-                               if (dit[i].inset().editable() != Inset::HIGHLY_EDITABLE) {
-                                       dit.resize(i);
+                               if (doc_it[i].inset().editable() != Inset::HIGHLY_EDITABLE) {
+                                       doc_it.resize(i);
                                        break;
                                }
-                       setCursor(dit);
-                       // Note: return bottom (document) level pit.
-                       return boost::make_tuple(cursor_.bottom().pit(), cursor_.bottom().pos(), top_id);
+                       success = true;
                }
        }
+
        // if top_id == 0, or searching through top_id failed
        // This is the case for a 'restored' bookmark when only bottom
        // (document level) pit was saved. Because of this, bookmark
@@ -436,22 +447,29 @@ boost::tuple<pit_type, pos_type, int> BufferView::moveToPosition(pit_type bottom
        // it will be restored to the left of the outmost inset that contains
        // the bookmark.
        if (static_cast<size_t>(bottom_pit) < buffer_.paragraphs().size()) {
-               DocIterator it = doc_iterator_begin(buffer_.inset());
-               it.pit() = bottom_pit;
-               it.pos() = min(bottom_pos, it.paragraph().size());
-               setCursor(it);
-               return boost::make_tuple(it.pit(), it.pos(),
-                                        it.paragraph().id());
+               doc_it = doc_iterator_begin(buffer_.inset());
+               doc_it.pit() = bottom_pit;
+               doc_it.pos() = min(bottom_pos, doc_it.paragraph().size());
+               success = true;
        }
-       // both methods fail
-       return boost::make_tuple(pit_type(0), pos_type(0), 0);
+
+       if (success) {
+               // Note: only bottom (document) level pit is set.
+               setCursor(doc_it);
+               // set the current font.
+               cursor_.setCurrentFont();
+               // center the screen on this new position.
+               center();
+       }
+
+       return success;
 }
 
 
 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 {
@@ -470,16 +488,31 @@ int BufferView::workWidth() const
 }
 
 
-void BufferView::center()
+void BufferView::updateOffsetRef()
 {
+       // No need to update offset_ref_ in this case.
+       if (!need_centering_)
+               return;
+
+       // We are not properly started yet, delay until resizing is
+       // done.
+       if (height_ == 0)
+               return;
+
        CursorSlice & bot = cursor_.bottom();
        TextMetrics & tm = text_metrics_[bot.text()];
-       pit_type const pit = bot.pit();
-       tm.redoParagraph(pit);
-       ParagraphMetrics const & pm = tm.parMetrics(pit);
-       anchor_ref_ = pit;
-       offset_ref_ = bv_funcs::coordOffset(*this, cursor_, cursor_.boundary()).y_
-               + pm.ascent() - height_ / 2;
+       ParagraphMetrics const & pm = tm.parMetrics(bot.pit());
+       Point p = bv_funcs::coordOffset(*this, cursor_, cursor_.boundary());
+       offset_ref_ = p.y_ + pm.ascent() - height_ / 2;
+
+       need_centering_ = false;
+}
+
+
+void BufferView::center()
+{
+       anchor_ref_ = cursor_.bottom().pit();
+       need_centering_ = true;
 }
 
 
@@ -942,21 +975,19 @@ void BufferView::clearSelection()
 }
 
 
-void BufferView::workAreaResize(int width, int height)
+void BufferView::resize(int width, int height)
 {
        // Update from work area
        width_ = width;
        height_ = height;
 
-       // The complete text metrics will be redone.
-       text_metrics_.clear();
        updateMetrics(false);
 }
 
 
 Inset const * BufferView::getCoveringInset(Text const & text, int x, int y)
 {
-       pit_type pit = text.getPitNearY(*this, y);
+       pit_type pit = text_metrics_[&text].getPitNearY(y);
        BOOST_ASSERT(pit != -1);
        Paragraph const & par = text.getPar(pit);
 
@@ -1053,7 +1084,7 @@ bool BufferView::workAreaDispatch(FuncRequest const & cmd0)
                        //metrics_info_.y1 = ymin of button;
                        //metrics_info_.y2 = ymax of button;
                        //
-                       // Unfortunately, rowpainter.cpp:paintText() does not distinguish
+                       // 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
@@ -1074,7 +1105,7 @@ bool BufferView::workAreaDispatch(FuncRequest const & cmd0)
        }
 
        // Build temporary cursor.
-       Inset * inset = buffer_.text().editXY(cur, cmd.x, cmd.y);
+       Inset * inset = text_metrics_[&buffer_.text()].editXY(cur, cmd.x, cmd.y);
 
        // Put anchor at the same position.
        cur.resetAnchor();
@@ -1306,7 +1337,6 @@ ViewMetricsInfo const & BufferView::viewMetricsInfo()
 void BufferView::updateMetrics(bool singlepar)
 {
        Text & buftext = buffer_.text();
-       TextMetrics & tm = textMetrics(&buftext);
        pit_type size = int(buftext.paragraphs().size());
 
        if (anchor_ref_ > int(buftext.paragraphs().size() - 1)) {
@@ -1320,9 +1350,12 @@ void BufferView::updateMetrics(bool singlepar)
        
                // Clear out paragraph metrics to avoid having invalid metrics
                // in the cache from paragraphs not relayouted below
-               tm.clear();
+               // The complete text metrics will be redone.
+               text_metrics_.clear();
        }
 
+       TextMetrics & tm = textMetrics(&buftext);
+
        // If the paragraph metrics has changed, we can not
        // use the singlepar optimisation.
        if (singlepar
@@ -1342,6 +1375,8 @@ void BufferView::updateMetrics(bool singlepar)
        if (!singlepar)
                tm.redoParagraph(pit);
 
+       updateOffsetRef();
+
        int y0 = tm.parMetrics(pit).ascent() - offset_ref_;
 
        // Redo paragraphs above anchor if necessary.
@@ -1482,7 +1517,7 @@ void BufferView::menuInsertLyXFile(string const & filenm)
                el = buf.errorList("Parse");
                recordUndo(cursor_);
                cap::pasteParagraphList(cursor_, buf.paragraphs(),
-                                            buf.params().textclass, el);
+                                            buf.params().getTextClass_ptr(), el);
                res = _("Document %1$s inserted.");
        } else
                res = _("Could not insert document %1$s");
@@ -1493,4 +1528,53 @@ void BufferView::menuInsertLyXFile(string const & filenm)
        updateMetrics(false);
 }
 
+
+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;
+
+       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);
+
+       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();
+       }
+       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 
+               && 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_ 
+               && metrics_info_.update_strategy == FullScreenUpdate)
+               pain.fillRectangle(0, metrics_info_.y2, width_,
+                       height_ - metrics_info_.y2, Color::bottomarea);
+}
+
 } // namespace lyx