]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
Add empty InsetLayout for undefined cases. Should avoid possible bugs caused by empty...
[lyx.git] / src / BufferView.cpp
index 3518d0b7e8c5c18a9186ccc8d51790124849d287..9abb3d7d1f4a86f2f1d18dbfaa45a946d7149117 100644 (file)
@@ -69,7 +69,6 @@
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/ExceptionMessage.h"
-#include "support/FileFilterList.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
@@ -114,7 +113,7 @@ bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
        while (tmpdit) {
                Inset const * inset = tmpdit.nextInset();
                if (inset
-                   && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
+                   && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
                    && (contents.empty() ||
                    static_cast<InsetCommand const *>(inset)->getFirstNonOptParam() == contents)) {
                        dit = tmpdit;
@@ -140,7 +139,7 @@ bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
        if (same_content) {
                Inset const * inset = tmpdit.nextInset();
                if (inset
-                   && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
+                   && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
                        contents = static_cast<InsetCommand const *>(inset)->getFirstNonOptParam();
                }
        }
@@ -258,7 +257,7 @@ struct BufferView::Private
 
 
 BufferView::BufferView(Buffer & buf)
-       : width_(0), height_(0), buffer_(buf), d(new Private(*this))
+       : width_(0), height_(0), full_screen_(false), buffer_(buf), d(new Private(*this))
 {
        d->xsel_cache_.set = false;
        d->intl_.initKeyMapper(lyxrc.use_kbmap);
@@ -288,6 +287,24 @@ BufferView::~BufferView()
 }
 
 
+int BufferView::rightMargin() const
+{
+       // The additional test for the case the outliner is opened.
+       if (!full_screen_ ||
+               !lyxrc.full_screen_limit ||
+               width_ < lyxrc.full_screen_width + 20)
+                       return 10;
+
+       return (width_ - lyxrc.full_screen_width) / 2;
+}
+
+
+int BufferView::leftMargin() const
+{
+       return rightMargin();
+}
+
+
 Intl & BufferView::getIntl()
 {
        return d->intl_;
@@ -430,7 +447,7 @@ void BufferView::updateScrollbar()
                << " curr par: " << d->cursor_.bottom().pit()
                << " default height " << defaultRowHeight());
 
-       int const parsize = int(t.paragraphs().size());
+       size_t const parsize = t.paragraphs().size();
        if (d->par_height_.size() != parsize) {
                d->par_height_.clear();
                // FIXME: We assume a default paragraph height of 2 rows. This
@@ -450,7 +467,7 @@ void BufferView::updateScrollbar()
        int top_pos = first.second->position() - first.second->ascent();
        int bottom_pos = last.second->position() + last.second->descent();
        bool first_visible = first.first == 0 && top_pos >= 0;
-       bool last_visible = last.first == parsize  - 1 && bottom_pos <= height_;
+       bool last_visible = last.first + 1 == int(parsize) && bottom_pos <= height_;
        if (first_visible && last_visible) {
                d->scrollbarParameters_.min = 0;
                d->scrollbarParameters_.max = 0;
@@ -458,7 +475,7 @@ void BufferView::updateScrollbar()
        }
 
        d->scrollbarParameters_.min = top_pos;
-       for (size_t i = 0; i != first.first; ++i)
+       for (size_t i = 0; i != size_t(first.first); ++i)
                d->scrollbarParameters_.min -= d->par_height_[i];
        d->scrollbarParameters_.max = bottom_pos;
        for (size_t i = last.first + 1; i != parsize; ++i)
@@ -528,7 +545,7 @@ void BufferView::scrollDocView(int value)
        // find paragraph at target position
        int par_pos = d->scrollbarParameters_.min;
        pit_type i = 0;
-       for (; i != d->par_height_.size(); ++i) {
+       for (; i != int(d->par_height_.size()); ++i) {
                par_pos += d->par_height_[i];
                if (par_pos >= value)
                        break;
@@ -632,7 +649,7 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
        int top_id, pos_type top_pos)
 {
        bool success = false;
-       DocIterator doc_it;
+       DocIterator dit;
 
        d->cursor_.clearSelection();
 
@@ -640,19 +657,19 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
        // This is the case for a 'live' bookmark when unique paragraph ID
        // is used to track bookmarks.
        if (top_id > 0) {
-               ParIterator par = buffer_.getParFromID(top_id);
-               if (par != buffer_.par_iterator_end()) {
-                       doc_it = makeDocIterator(par, min(par->size(), top_pos));
+               dit = buffer_.getParFromID(top_id);
+               if (!dit.atEnd()) {
+                       dit.pos() = min(dit.paragraph().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 = doc_it.depth();
+                       size_t const n = dit.depth();
                        for (size_t i = 0; i < n; ++i)
-                               if (doc_it[i].inset().editable() != Inset::HIGHLY_EDITABLE) {
-                                       doc_it.resize(i);
+                               if (dit[i].inset().editable() != Inset::HIGHLY_EDITABLE) {
+                                       dit.resize(i);
                                        break;
                                }
                        success = true;
@@ -665,16 +682,17 @@ bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
        // restoration is inaccurate. If a bookmark was within an inset,
        // 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()) {
-               doc_it = doc_iterator_begin(buffer_.inset());
-               doc_it.pit() = bottom_pit;
-               doc_it.pos() = min(bottom_pos, doc_it.paragraph().size());
+       if (bottom_pit < int(buffer_.paragraphs().size())) {
+               dit = doc_iterator_begin(buffer_.inset());
+                               
+               dit.pit() = bottom_pit;
+               dit.pos() = min(bottom_pos, dit.paragraph().size());
                success = true;
        }
 
        if (success) {
                // Note: only bottom (document) level pit is set.
-               setCursor(doc_it);
+               setCursor(dit);
                // set the current font.
                d->cursor_.setCurrentFont();
                // To center the screen on this new position we need the
@@ -877,7 +895,7 @@ FuncStatus BufferView::getStatus(FuncRequest const & cmd)
 
        case LFUN_LAYOUT:
        case LFUN_LAYOUT_PARAGRAPH:
-               flag.enabled(cur.inset().forceDefaultParagraphs(cur.idx()));
+               flag.enabled(cur.inset().allowParagraphCustomization(cur.idx()));
                break;
 
        case LFUN_INSET_SETTINGS: {
@@ -998,27 +1016,27 @@ bool BufferView::dispatch(FuncRequest const & cmd)
                for (Buffer * b = &buffer_; i == 0 || b != &buffer_;
                        b = theBufferList().next(b)) {
 
-                       ParIterator par = b->getParFromID(id);
-                       if (par == b->par_iterator_end()) {
+                       DocIterator dit = b->getParFromID(id);
+                       if (dit.atEnd()) {
                                LYXERR(Debug::INFO, "No matching paragraph found! [" << id << "].");
+                               ++i;
+                               continue;
+                       }
+                       LYXERR(Debug::INFO, "Paragraph " << dit.paragraph().id()
+                               << " found in buffer `"
+                               << b->absFileName() << "'.");
+
+                       if (b == &buffer_) {
+                               // Set the cursor
+                               setCursor(dit);
+                               processUpdateFlags(Update::Force | Update::FitCursor);
                        } else {
-                               LYXERR(Debug::INFO, "Paragraph " << par->id()
-                                       << " found in buffer `"
-                                       << b->absFileName() << "'.");
-
-                               if (b == &buffer_) {
-                                       // Set the cursor
-                                       setCursor(makeDocIterator(par, 0));
-                                       showCursor();
-                               } else {
-                                       // Switch to other buffer view and resend cmd
-                                       theLyXFunc().dispatch(FuncRequest(
-                                               LFUN_BUFFER_SWITCH, b->absFileName()));
-                                       theLyXFunc().dispatch(cmd);
-                               }
-                               break;
+                               // Switch to other buffer view and resend cmd
+                               theLyXFunc().dispatch(FuncRequest(
+                                       LFUN_BUFFER_SWITCH, b->absFileName()));
+                               theLyXFunc().dispatch(cmd);
                        }
-                       ++i;
+                       break;
                }
                break;
        }
@@ -1091,7 +1109,10 @@ bool BufferView::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_WORD_FIND:
-               find(this, cmd);
+               if (find(this, cmd))
+                       showCursor();
+               else
+                       message(_("String not found!"));
                break;
 
        case LFUN_WORD_REPLACE: {
@@ -1336,23 +1357,14 @@ void BufferView::clearSelection()
 
 void BufferView::resize(int width, int height)
 {
-       bool initialResize = (height_ == 0);
-       
        // Update from work area
        width_ = width;
        height_ = height;
 
        // Clear the paragraph height cache.
        d->par_height_.clear();
-
+       // Redo the metrics.
        updateMetrics();
-
-       // view got his initial size, make sure that
-       // the cursor has a proper position
-       if (initialResize) {
-               updateScrollbar();
-               showCursor();
-       }
 }
 
 
@@ -1564,7 +1576,7 @@ void BufferView::gotoLabel(docstring const & label)
        for (InsetIterator it = inset_iterator_begin(buffer_.inset()); it; ++it) {
                vector<docstring> labels;
                it->getLabelList(buffer_, labels);
-               if (find(labels.begin(), labels.end(), label) != labels.end()) {
+               if (std::find(labels.begin(), labels.end(), label) != labels.end()) {
                        setCursor(it);
                        showCursor();
                        return;
@@ -1673,9 +1685,9 @@ bool BufferView::mouseSetCursor(Cursor & cur, bool select)
        // For an example, see bug 2933:
        // http://bugzilla.lyx.org/show_bug.cgi?id=2933
        // The code below could maybe be moved to a DocIterator method.
-       //lyxerr << "cur before " << cur <<endl;
-       DocIterator dit(cur.inset());
-       dit.push_back(cur.bottom());
+       //lyxerr << "cur before " << cur << endl;
+       DocIterator dit = doc_iterator_begin(cur.inset());
+       dit.bottom() = cur.bottom();
        size_t i = 1;
        while (i < cur.depth() && dit.nextInset() == &cur[i].inset()) {
                dit.push_back(cur[i]);
@@ -1710,6 +1722,9 @@ void BufferView::putSelectionAt(DocIterator const & cur,
                } else
                        d->cursor_.setSelection(d->cursor_, length);
        }
+       // Ensure a redraw happens in any case because the new selection could 
+       // possibly be on the same screen as the previous selection.
+       processUpdateFlags(Update::Force | Update::FitCursor);
 }