From e809647c77ac3d9fb8ee50706c84b5c7688cf996 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Wed, 22 Aug 2007 14:25:51 +0000 Subject: [PATCH] simplify BufferView::moveToPosition() use for the external use (in LyXView and LyXFunc). * BufferView::moveToPosition() - now only return success boolean - now schedule a screen centering - now set the current font to the new position. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@19722 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 41 ++++++++++++++++++++++++--------------- src/BufferView.h | 6 +++--- src/LyXFunc.cpp | 16 ++++++++++----- src/frontends/LyXView.cpp | 7 +------ 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 40ee79c330..75324b8fe4 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -408,9 +408,12 @@ void BufferView::saveBookmark(unsigned int idx) } -boost::tuple 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 @@ -419,24 +422,23 @@ boost::tuple 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 @@ -444,15 +446,22 @@ boost::tuple BufferView::moveToPosition(pit_type bottom // it will be restored to the left of the outmost inset that contains // the bookmark. if (static_cast(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. + buffer_.text().setCurrentFont(cursor_); + // center the screen on this new position. + center(); + } + + return success; } diff --git a/src/BufferView.h b/src/BufferView.h index 8cb5725ee1..a063e62c01 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -107,9 +107,9 @@ public: /// Save the current position as bookmark. /// if idx == 0, save to temp_bookmark void saveBookmark(unsigned int idx); - /// goto a specified position, try top_id first, and then bottom_pit - /// return the bottom_pit and top_id of the new paragraph - boost::tuple moveToPosition( + /// goto a specified position, try top_id first, and then bottom_pit. + /// \return true if success + bool moveToPosition( pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid. pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid. int top_id, ///< Paragraph ID, \sa Paragraph diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index 3961a39c6d..f99febbadd 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -273,11 +273,17 @@ void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer) else return; } - // moveToPosition use par_id, and par_pit and return new par_id. - pit_type new_pit; - pos_type new_pos; - int new_id; - boost::tie(new_pit, new_pos, new_id) = view()->moveToPosition(bm.bottom_pit, bm.bottom_pos, bm.top_id, bm.top_pos); + // moveToPosition try paragraph id first and then paragraph (pit, pos). + if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos, + bm.top_id, bm.top_pos)) + return; + + // Cursor jump succeeded! + Cursor const & cur = view()->cursor(); + pit_type new_pit = cur.pit(); + pos_type new_pos = cur.pos(); + int new_id = cur.paragraph().id(); + // if bottom_pit, bottom_pos or top_id has been changed, update bookmark // see http://bugzilla.lyx.org/show_bug.cgi?id=3092 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos || bm.top_id != new_id ) diff --git a/src/frontends/LyXView.cpp b/src/frontends/LyXView.cpp index 14aae66a14..af322ba092 100644 --- a/src/frontends/LyXView.cpp +++ b/src/frontends/LyXView.cpp @@ -151,12 +151,7 @@ Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles) boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename); // if successfully move to pit (returned par_id is not zero), // update metrics and reset font - BufferView & bv = wa->bufferView(); - if (bv.moveToPosition(pit, pos, 0, 0).get<1>()) { - if (bv.fitCursor()) - bv.updateMetrics(false); - newBuffer->text().setCurrentFont(bv.cursor()); - } + wa->bufferView().moveToPosition(pit, pos, 0, 0); } if (tolastfiles)