]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
Point fix, earlier forgotten
[lyx.git] / src / BufferView_pimpl.C
index 9d830f999413d20242268c35fee24606ad4f0017..296b9d61d10f7db91aee1d783cdc15b43926d1bd 100644 (file)
@@ -1,10 +1,19 @@
 /**
  * \file BufferView_pimpl.C
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
+ * \author Asger Alstrup
+ * \author Alfredo Braustein
  * \author Lars Gullik Bjønnes
- * \author various
+ * \author Jean-Marc Lasgouttes
+ * \author Angus Leeming
+ * \author John Levon
+ * \author André Pönitz
+ * \author Dekel Tsur
+ * \author Jürgen Vigna
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
@@ -29,9 +38,9 @@
 #include "lyxfunc.h"
 #include "lyxtext.h"
 #include "lyxrc.h"
-#include "lyxrow.h"
 #include "lastfiles.h"
 #include "paragraph.h"
+#include "paragraph_funcs.h"
 #include "ParagraphParameters.h"
 #include "TextCache.h"
 #include "undo_funcs.h"
@@ -188,10 +197,10 @@ bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
        // get absolute path of file and add ".lyx" to the filename if
        // necessary
        string s = FileSearch(string(), filename, "lyx");
-       
+
        bool const found = !s.empty();
 
-       if (!found) 
+       if (!found)
                s = filename;
 
        // file already open?
@@ -264,6 +273,18 @@ Painter & BufferView::Pimpl::painter() const
 }
 
 
+void BufferView::Pimpl::top_y(int y)
+{
+       top_y_ = y;
+}
+
+
+int BufferView::Pimpl::top_y() const
+{
+       return top_y_;
+}
+
+
 void BufferView::Pimpl::buffer(Buffer * b)
 {
        lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
@@ -284,6 +305,8 @@ void BufferView::Pimpl::buffer(Buffer * b)
        // set current buffer
        buffer_ = b;
 
+       top_y_ = 0;
+
        // if we're quitting lyx, don't bother updating stuff
        if (quitting)
                return;
@@ -301,7 +324,7 @@ void BufferView::Pimpl::buffer(Buffer * b)
                        resizeCurrentBuffer();
 
                // FIXME: needed when ?
-               bv_->text->top_y(screen().topCursorVisible(bv_->text));
+               top_y(screen().topCursorVisible(bv_->text));
 
                // Buffer-dependent dialogs should be updated or
                // hidden. This should go here because some dialogs (eg ToC)
@@ -349,7 +372,7 @@ bool BufferView::Pimpl::fitCursor()
                ret = screen().fitCursor(bv_->text, bv_);
        }
 
-       dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
+       //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
 
        // We need to always update, in case we did a
        // paste and we stayed anchored to a row, but
@@ -371,7 +394,7 @@ void BufferView::Pimpl::redoCurrentBuffer()
 }
 
 
-int BufferView::Pimpl::resizeCurrentBuffer()
+void BufferView::Pimpl::resizeCurrentBuffer()
 {
        lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
 
@@ -400,30 +423,26 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                selection = bv_->text->selection.set();
                mark_set = bv_->text->selection.mark();
                the_locking_inset = bv_->theLockingInset();
-               resizeInsets(bv_);
                bv_->text->fullRebreak();
                update();
        } else {
-               lyxerr << "text not available!\n";
+               lyxerr << "text not available!" << endl;
                // See if we have a text in TextCache that fits
                // the new buffer_ with the correct width.
                bv_->text = textcache.findFit(buffer_, workarea().workWidth());
                if (bv_->text) {
-                       lyxerr << "text in cache!\n";
+                       lyxerr << "text in cache!" << endl;
                        if (lyxerr.debugging()) {
-                               lyxerr << "Found a LyXText that fits:\n";
+                               lyxerr << "Found a LyXText that fits:" << endl;
                                textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
                        }
                        // Set the owner of the newly found text
                        //      bv_->text->owner(bv_);
                        if (lyxerr.debugging())
                                textcache.show(lyxerr, "resizeCurrentBuffer");
-
-                       resizeInsets(bv_);
                } else {
-                       lyxerr << "no text in cache!\n";
-                       bv_->text = new LyXText(bv_);
-                       resizeInsets(bv_);
+                       lyxerr << "no text in cache!" << endl;
+                       bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs);
                        bv_->text->init(bv_);
                }
 
@@ -432,6 +451,10 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                selendpar = bv_->text->ownerParagraphs().end();
        }
 
+#warning does not help much
+       bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
+               bv_->text->ownerParagraphs().end());
+
        if (par != bv_->text->ownerParagraphs().end()) {
                bv_->text->selection.set(true);
                // At this point just to avoid the Delete-Empty-Paragraph-
@@ -452,7 +475,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                bv_->theLockingInset(the_locking_inset);
        }
 
-       bv_->text->top_y(screen().topCursorVisible(bv_->text));
+       top_y(screen().topCursorVisible(bv_->text));
 
        switchKeyMap();
        owner_->busy(false);
@@ -461,8 +484,6 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        owner_->clearMessage();
 
        updateScrollbar();
-
-       return 0;
 }
 
 
@@ -477,9 +498,9 @@ void BufferView::Pimpl::updateScrollbar()
        LyXText const & t = *bv_->text;
 
        lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
-               << t.top_y() << ", default height " << defaultRowHeight() << endl;
+               << top_y() << ", default height " << defaultRowHeight() << endl;
 
-       workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
+       workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
 }
 
 
@@ -492,21 +513,21 @@ void BufferView::Pimpl::scrollDocView(int value)
 
        screen().hideCursor();
 
-       screen().draw(bv_->text, bv_, value);
+       top_y(value);
+       screen().redraw(*bv_);
 
        if (!lyxrc.cursor_follows_scrollbar)
                return;
 
-       LyXText * vbt = bv_->text;
-
        int const height = defaultRowHeight();
-       int const first = static_cast<int>((bv_->text->top_y() + height));
-       int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
+       int const first = top_y() + height;
+       int const last = top_y() + workarea().workHeight() - height;
 
-       if (vbt->cursor.y() < first)
-               vbt->setCursorFromCoordinates(0, first);
-       else if (vbt->cursor.y() > last)
-               vbt->setCursorFromCoordinates(0, last);
+       LyXText * text = bv_->text;
+       if (text->cursor.y() < first)
+               text->setCursorFromCoordinates(0, first);
+       else if (text->cursor.y() > last)
+               text->setCursorFromCoordinates(0, last);
 
        owner_->updateLayoutChoice();
 }
@@ -522,7 +543,7 @@ void BufferView::Pimpl::scroll(int lines)
        int const line_height = defaultRowHeight();
 
        // The new absolute coordinate
-       int new_top_y = t->top_y() + lines * line_height;
+       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);
@@ -531,7 +552,7 @@ void BufferView::Pimpl::scroll(int lines)
        scrollDocView(new_top_y);
 
        // Update the scrollbar.
-       workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
+       workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
 }
 
 
@@ -569,7 +590,7 @@ void BufferView::Pimpl::selectionRequested()
                 text->selection.end != bv_->text->xsel_cache.end))
        {
                bv_->text->xsel_cache = text->selection;
-               sel = text->selectionAsString(bv_->buffer(), false);
+               sel = text->selectionAsString(*bv_->buffer(), false);
        } else if (!text->selection.set()) {
                sel = string();
                bv_->text->xsel_cache.set(false);
@@ -584,7 +605,6 @@ void BufferView::Pimpl::selectionLost()
 {
        if (available()) {
                screen().hideCursor();
-               toggleSelection();
                bv_->getLyXText()->clearSelection();
                bv_->text->xsel_cache.set(false);
        }
@@ -617,13 +637,6 @@ void BufferView::Pimpl::workAreaResize()
                        if (lyxerr.debugging())
                                textcache.show(lyxerr, "Expose delete all");
                        textcache.clear();
-                       // FIXME: this is already done in resizeCurrentBuffer() ??
-                       resizeInsets(bv_);
-               } else if (heightChange) {
-                       // fitCursor() ensures we don't jump back
-                       // to the start of the document on vertical
-                       // resize
-                       fitCursor();
                }
        }
 
@@ -633,50 +646,18 @@ void BufferView::Pimpl::workAreaResize()
        // always make sure that the scrollbar is sane.
        updateScrollbar();
        owner_->updateLayoutChoice();
-       return;
 }
 
 
 void BufferView::Pimpl::update()
 {
-       lyxerr << "BufferView::update()\n";
-       screen().redraw(*bv_);
-}
-
-
-void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
-{
-       if (!text->selection.set() && (f & SELECT)) {
-               text->selection.cursor = text->cursor;
-       }
-
-       text->partialRebreak();
-
-       if (text->inset_owner) {
-               text->inset_owner->setUpdateStatus(InsetText::NONE);
-               updateInset(text->inset_owner);
-       } else {
-               update();
-       }
-}
-
-
-void BufferView::Pimpl::update(BufferView::UpdateCodes f)
-{
-       LyXText * text = bv_->text;
-
-       if (!text->selection.set() && (f & SELECT)) {
-               text->selection.cursor = text->cursor;
-       }
-
-       text->partialRebreak();
-
-       if (text->inset_owner) {
-               text->inset_owner->setUpdateStatus(InsetText::NONE);
-               updateInset(text->inset_owner);
-       } else {
-               update();
+       lyxerr << "BufferView::update()" << endl;
+       // fix cursor coordinate cache in case something went wrong
+       if (bv_->getLyXText()) {
+               // check needed to survive LyX startup
+               bv_->getLyXText()->redoCursor();
        }
+       screen().redraw(*bv_);
 }
 
 
@@ -707,19 +688,18 @@ Change const BufferView::Pimpl::getCurrentChange()
        if (!bv_->buffer()->params.tracking_changes)
                return Change(Change::UNCHANGED);
 
-       LyXText * t(bv_->getLyXText());
+       LyXText * text = bv_->getLyXText();
 
-       if (!t->selection.set())
+       if (!text->selection.set())
                return Change(Change::UNCHANGED);
 
-       LyXCursor const & cur(t->selection.start);
+       LyXCursor const & cur = text->selection.start;
        return cur.par()->lookupChangeFull(cur.pos());
 }
 
 
 void BufferView::Pimpl::beforeChange(LyXText * text)
 {
-       toggleSelection();
        text->clearSelection();
 }
 
@@ -764,7 +744,7 @@ void BufferView::Pimpl::restorePosition(unsigned int i)
        bv_->text->setCursor(par.pit(),
                             min(par->size(), saved_positions[i].par_pos));
 
-       update(BufferView::SELECT);
+       update();
        if (i > 0)
                owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
 }
@@ -808,23 +788,13 @@ void BufferView::Pimpl::insetUnlock()
 }
 
 
-void BufferView::Pimpl::toggleSelection(bool b)
-{
-       screen().toggleSelection(bv_->text, bv_, b);
-}
-
-
 void BufferView::Pimpl::center()
 {
-       LyXText * t = bv_->text;
+       LyXText * text = bv_->text;
 
-       beforeChange(t);
+       beforeChange(text);
        int const half_height = workarea().workHeight() / 2;
-       int new_y = 0;
-
-       if (t->cursor.y() > half_height) {
-               new_y = t->cursor.y() - half_height;
-       }
+       int new_y = std::max(0, text->cursor.y() - half_height);
 
        // FIXME: look at this comment again ...
 
@@ -837,9 +807,9 @@ void BufferView::Pimpl::center()
        // and also might have moved top_y() must make sure to call
        // updateScrollbar() currently. Never mind that this is a
        // pretty obfuscated way of updating t->top_y()
-       screen().draw(t, bv_, new_y);
-
-       update(BufferView::SELECT);
+       top_y(new_y);
+       //screen().draw();
+       update();
 }
 
 
@@ -867,7 +837,7 @@ InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
        return it != buffer_->inset_iterator_end() ? (*it) : 0;
 #else
        // Ok, this is a little bit too brute force but it
-       // should work for now. Better infrastructure is comming. (Lgb)
+       // should work for now. Better infrastructure is coming. (Lgb)
 
        Buffer * b = bv_->buffer();
        LyXCursor cursor = bv_->getLyXText()->cursor;
@@ -971,7 +941,7 @@ void BufferView::Pimpl::trackChanges()
                // we cannot allow undos beyond the freeze point
                buf->undostack.clear();
        } else {
-               update(BufferView::SELECT);
+               update();
                bv_->text->setCursor(buf->paragraphs.begin(), 0);
 #warning changes FIXME
                //moveCursorUpdate(false);
@@ -992,37 +962,49 @@ void BufferView::Pimpl::trackChanges()
 }
 
 
-// Doesn't go through lyxfunc, so we need to update the
-// layout choice etc. ourselves
-bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
+bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
 {
-       // e.g. Qt mouse press when no buffer
-       if (!available())
-               return false;
+       switch (ev.action) {
+       case LFUN_MOUSE_PRESS:
+       case LFUN_MOUSE_MOTION:
+       case LFUN_MOUSE_RELEASE:
+       case LFUN_MOUSE_DOUBLE:
+       case LFUN_MOUSE_TRIPLE:
+       {
+               // We pass those directly to the Bufferview, since
+               // otherwise selection handling breaks down
 
-       screen().hideCursor();
+               // Doesn't go through lyxfunc, so we need to update
+               // the layout choice etc. ourselves
 
-       // Make sure that the cached BufferView is correct.
-       FuncRequest ev = ev_in;
-       ev.setView(bv_);
+               // e.g. Qt mouse press when no buffer
+               if (!available())
+                       return false;
 
-       bool const res = dispatch(ev);
+               screen().hideCursor();
 
-       // see workAreaKeyPress
-       cursor_timeout.restart();
-       screen().showCursor(*bv_);
+               bool const res = dispatch(ev);
 
-       // FIXME: we should skip these when selecting
-       bv_->owner()->updateLayoutChoice();
-       bv_->owner()->updateToolbar();
-       bv_->fitCursor();
+               // see workAreaKeyPress
+               cursor_timeout.restart();
+               screen().showCursor(*bv_);
+
+               // FIXME: we should skip these when selecting
+               owner_->updateLayoutChoice();
+               owner_->updateToolbar();
+               fitCursor();
 
-       // slight hack: this is only called currently when
-       // we clicked somewhere, so we force through the display
-       // of the new status here.
-       bv_->owner()->clearMessage();
+               // slight hack: this is only called currently when we
+               // clicked somewhere, so we force through the display
+               // of the new status here.
+               owner_->clearMessage();
 
-       return res;
+               return res;
+       }
+       default:
+               owner_->dispatch(ev);
+               return true;
+       }
 }
 
 
@@ -1177,7 +1159,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                        owner_->getLyXFunc().handleKeyFunc(ev.action);
                        owner_->getIntl().getTransManager()
                                .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
-                       update(bv_->getLyXText(), BufferView::SELECT);
+                       update();
                }
                break;
 
@@ -1219,7 +1201,6 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                                // InsetBibitem width could have changed
 #warning check whether the update() is needed at all
                                bv_->update();
-                               bv_->fitCursor();
                        }
                } else {
                        delete inset;
@@ -1239,12 +1220,8 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                break;
 
        case LFUN_LAYOUT_PARAGRAPH: {
-               Paragraph const * par = &*bv_->getLyXText()->cursor.par();
-               if (!par)
-                       break;
-
                string data;
-               params2string(*par, data);
+               params2string(*bv_->getLyXText()->cursor.par(), data);
 
                data = "show\n" + data;
                bv_->owner()->getDialogs().show("paragraph", data);
@@ -1254,15 +1231,13 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
        case LFUN_PARAGRAPH_UPDATE: {
                if (!bv_->owner()->getDialogs().visible("paragraph"))
                        break;
-               Paragraph const * par = &*bv_->getLyXText()->cursor.par();
-               if (!par)
-                       break;
+               Paragraph const & par = *bv_->getLyXText()->cursor.par();
 
                string data;
-               params2string(*par, data);
+               params2string(par, data);
 
                // Will the paragraph accept changes from the dialog?
-               InsetOld * const inset = par->inInset();
+               InsetOld * const inset = par.inInset();
                bool const accept =
                        !(inset && inset->forceDefaultParagraphs(inset));
 
@@ -1280,14 +1255,14 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                string arg = ev.argument;
 
                if (arg.empty()) {
-                       arg = bv_->getLyXText()->selectionAsString(buffer_,
+                       arg = bv_->getLyXText()->selectionAsString(*buffer_,
                                                                   false);
 
                        // FIXME
                        if (arg.size() > 100 || arg.empty()) {
                                // Get word or selection
                                bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
-                               arg = bv_->getLyXText()->selectionAsString(buffer_, false);
+                               arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
                                // FIXME: where is getLyXText()->unselect(bv_) ?
                        }
                }
@@ -1305,40 +1280,38 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                break;
 
        case LFUN_ACCEPT_ALL_CHANGES: {
-               update(BufferView::SELECT);
                bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
 #warning FIXME changes
                //moveCursorUpdate(false);
 
-               while (lyx::find::findNextChange(bv_)) {
+               while (lyx::find::findNextChange(bv_))
                        bv_->getLyXText()->acceptChange();
-               }
-               update(BufferView::SELECT);
+
+               update();
                break;
        }
 
        case LFUN_REJECT_ALL_CHANGES: {
-               update(BufferView::SELECT);
                bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
 #warning FIXME changes
                //moveCursorUpdate(false);
 
-               while (lyx::find::findNextChange(bv_)) {
+               while (lyx::find::findNextChange(bv_))
                        bv_->getLyXText()->rejectChange();
-               }
-               update(BufferView::SELECT);
+
+               update();
                break;
        }
 
        case LFUN_ACCEPT_CHANGE: {
                bv_->getLyXText()->acceptChange();
-               update(BufferView::SELECT);
+               update();
                break;
        }
 
        case LFUN_REJECT_CHANGE: {
                bv_->getLyXText()->rejectChange();
-               update(BufferView::SELECT);
+               update();
                break;
        }
 
@@ -1370,20 +1343,15 @@ bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
 
        beforeChange(bv_->text);
        if (!lout.empty()) {
-               update(BufferView::SELECT);
                bv_->text->breakParagraph(bv_->buffer()->paragraphs);
-               update(BufferView::SELECT);
 
                if (!bv_->text->cursor.par()->empty()) {
                        bv_->text->cursorLeft(bv_);
-
                        bv_->text->breakParagraph(bv_->buffer()->paragraphs);
-                       update(BufferView::SELECT);
                }
 
                string lres = lout;
-               LyXTextClass const & tclass =
-                       buffer_->params.getLyXTextClass();
+               LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
                bool hasLayout = tclass.hasLayout(lres);
                string lay = tclass.defaultLayoutName();
 
@@ -1404,57 +1372,27 @@ bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
                                   LYX_ALIGN_LAYOUT,
                                   string(),
                                   0);
-               update(BufferView::SELECT);
        }
 
        bv_->text->insertInset(inset);
-       update(BufferView::SELECT);
+       update();
 
        unFreezeUndo();
        return true;
 }
 
 
-void BufferView::Pimpl::updateInset(InsetOld * inset)
+void BufferView::Pimpl::updateInset(InsetOld const * inset)
 {
-       if (!inset || !available())
+       if (!available())
                return;
 
-       // first check for locking insets
-       if (bv_->theLockingInset()) {
-               if (bv_->theLockingInset() == inset) {
-                       if (bv_->text->updateInset(inset)) {
-                               update();
-                               updateScrollbar();
-                               return;
-                       }
-               } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
-                       if (bv_->text->updateInset(bv_->theLockingInset())) {
-                               update();
-                               updateScrollbar();
-                               return;
-                       }
-               }
-       }
+       bv_->text->redoParagraph(outerPar(*bv_->buffer(), inset));
 
-       // then check if the inset is a top_level inset (has no owner)
-       // if yes do the update as always otherwise we have to update the
-       // toplevel inset where this inset is inside
-       InsetOld * tl_inset = inset;
-       while (tl_inset->owner())
-               tl_inset = tl_inset->owner();
-       if (tl_inset == inset) {
-               update(BufferView::UPDATE);
-               if (bv_->text->updateInset(inset)) {
-                       update(BufferView::SELECT);
-                       return;
-               }
-       } else if (static_cast<UpdatableInset *>(tl_inset)
-                          ->updateInsetInInset(bv_, inset))
-       {
-               if (bv_->text->updateInset(tl_inset)) {
-                       update();
-                       updateScrollbar();
-               }
-       }
+       // this should not be needed, but it is...
+       // bv_->text->redoParagraph(bv_->text->cursor.par());
+       // bv_->text->fullRebreak();
+
+       update();
+       updateScrollbar();
 }