]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
Remove the inset and view member functions from PreviewedInset.
[lyx.git] / src / BufferView_pimpl.C
index a2f74e21a1a8148baacdd20a54010cc0e0d176cd..b7eafa9d9663223a7f89083ecc79dc97c681f35f 100644 (file)
@@ -1,17 +1,29 @@
 /**
  * \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 various
+ * \author Asger Alstrup
+ * \author Alfredo Braustein
+ * \author Lars Gullik Bjønnes
+ * \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>
 
 #include "BufferView_pimpl.h"
+#include "buffer.h"
+#include "buffer_funcs.h"
 #include "bufferlist.h"
-#include "bufferview_funcs.h"
-#include "lfuns.h"
+#include "bufferparams.h"
+#include "cursor.h"
 #include "debug.h"
 #include "factory.h"
 #include "FloatList.h"
 #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"
+#include "vspace.h"
 
 #include "insets/insetfloatlist.h"
-#include "insets/insetgraphics.h"
-#include "insets/insetinclude.h"
 #include "insets/insetref.h"
-#include "insets/insettext.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
 #include "frontends/FileDialog.h"
 #include "frontends/LyXView.h"
 #include "frontends/LyXScreenFactory.h"
-#include "frontends/mouse_state.h"
 #include "frontends/screen.h"
 #include "frontends/WorkArea.h"
 #include "frontends/WorkAreaFactory.h"
 
-#include "mathed/formulabase.h"
-
 #include "graphics/Previews.h"
 
-#include "support/LAssert.h"
-#include "support/lstrings.h"
+#include "mathed/formulabase.h"
+
 #include "support/filetools.h"
+#include "support/path_defines.h"
+#include "support/tostr.h"
 
 #include <boost/bind.hpp>
-#include <boost/signals/connection.hpp>
-#include "support/BoostFormat.h"
 
-#include <unistd.h>
-#include <sys/wait.h>
+using bv_funcs::bold;
+using bv_funcs::code;
+using bv_funcs::currentState;
+using bv_funcs::emph;
+using bv_funcs::fontSize;
+using bv_funcs::lang;
+using bv_funcs::noun;
+using bv_funcs::roman;
+using bv_funcs::sans;
+using bv_funcs::styleReset;
+using bv_funcs::underline;
+
+using lyx::pos_type;
 
+using lyx::support::AddPath;
+using lyx::support::bformat;
+using lyx::support::FileSearch;
+using lyx::support::IsDirWriteable;
+using lyx::support::MakeDisplayPath;
+using lyx::support::strToUnsignedInt;
+using lyx::support::system_lyxdir;
 
-using std::vector;
-using std::find_if;
-using std::find;
-using std::pair;
 using std::endl;
 using std::make_pair;
 using std::min;
+using std::string;
 
-using lyx::pos_type;
-using namespace bv_funcs;
 
 extern BufferList bufferlist;
 
@@ -127,6 +148,119 @@ BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
 }
 
 
+void BufferView::Pimpl::addError(ErrorItem const & ei)
+{
+       errorlist_.push_back(ei);
+}
+
+
+void BufferView::Pimpl::showReadonly(bool)
+{
+       owner_->updateWindowTitle();
+       owner_->getDialogs().updateBufferDependent(false);
+}
+
+
+void BufferView::Pimpl::connectBuffer(Buffer & buf)
+{
+       if (errorConnection_.connected())
+               disconnectBuffer();
+
+       errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
+       messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
+       busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
+       titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
+       timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
+       readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
+       closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
+}
+
+
+void BufferView::Pimpl::disconnectBuffer()
+{
+       errorConnection_.disconnect();
+       messageConnection_.disconnect();
+       busyConnection_.disconnect();
+       titleConnection_.disconnect();
+       timerConnection_.disconnect();
+       readonlyConnection_.disconnect();
+       closingConnection_.disconnect();
+}
+
+
+bool BufferView::Pimpl::newFile(string const & filename,
+                               string const & tname,
+                               bool isNamed)
+{
+       Buffer * b = ::newFile(filename, tname, isNamed);
+       buffer(b);
+       return true;
+}
+
+
+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)
+               s = filename;
+
+       // file already open?
+       if (bufferlist.exists(s)) {
+               string const file = MakeDisplayPath(s, 20);
+               string text = bformat(_("The document %1$s is already "
+                                       "loaded.\n\nDo you want to revert "
+                                       "to the saved version?"), file);
+               int const ret = Alert::prompt(_("Revert to saved document?"),
+                       text, 0, 1,  _("&Revert"), _("&Switch to document"));
+
+               if (ret != 0) {
+                       buffer(bufferlist.getBuffer(s));
+                       return true;
+               } else {
+                       // FIXME: should be LFUN_REVERT
+                       if (!bufferlist.close(bufferlist.getBuffer(s), false))
+                               return false;
+                       // Fall through to new load. (Asger)
+               }
+       }
+
+       Buffer * b;
+
+       if (found) {
+               b = bufferlist.newBuffer(s);
+               connectBuffer(*b);
+               if (!::loadLyXFile(b, s)) {
+                       bufferlist.release(b);
+                       return false;
+               }
+       } else {
+               string text = bformat(_("The document %1$s does not yet "
+                                       "exist.\n\nDo you want to create "
+                                       "a new document?"), s);
+               int const ret = Alert::prompt(_("Create new document?"),
+                        text, 0, 1, _("&Create"), _("Cancel"));
+
+               if (ret == 0)
+                       b = ::newFile(s, string(), true);
+               else
+                       return false;
+       }
+
+       buffer(b);
+       bv_->showErrorList(_("Parse"));
+
+       if (tolastfiles)
+               lastfiles->newFile(b->fileName());
+
+       return true;
+}
+
+
 WorkArea & BufferView::Pimpl::workarea() const
 {
        return *workarea_.get();
@@ -145,13 +279,24 @@ 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 ("
                            << b << ')' << endl;
        if (buffer_) {
-               buffer_->delUser(bv_);
-
+               disconnectBuffer();
                // Put the old text into the TextCache, but
                // only if the buffer is still loaded.
                // Also set the owner of the test to 0
@@ -166,26 +311,26 @@ 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;
 
        // if we are closing the buffer, use the first buffer as current
-       if (!buffer_) {
+       if (!buffer_)
                buffer_ = bufferlist.first();
-       }
 
        if (buffer_) {
                lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
-               buffer_->addUser(bv_);
+               connectBuffer(*buffer_);
 
                // If we don't have a text object for this, we make one
-               if (bv_->text == 0) {
+               if (bv_->text == 0)
                        resizeCurrentBuffer();
-               }
 
                // FIXME: needed when ?
-               bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
+               top_y(screen().topCursorVisible(bv_->text));
 
                // Buffer-dependent dialogs should be updated or
                // hidden. This should go here because some dialogs (eg ToC)
@@ -203,7 +348,7 @@ void BufferView::Pimpl::buffer(Buffer * b)
                textcache.clear();
        }
 
-       repaint();
+       update();
        updateScrollbar();
        owner_->updateMenubar();
        owner_->updateToolbar();
@@ -213,12 +358,12 @@ void BufferView::Pimpl::buffer(Buffer * b)
        if (buffer_) {
                // Don't forget to update the Layout
                string const layoutname =
-                       bv_->text->cursor.par()->layout()->name();
+                       bv_->text->cursorPar()->layout()->name();
                owner_->setLayout(layoutname);
        }
 
-       if (grfx::Previews::activated() && buffer_)
-               grfx::Previews::get().generateBufferPreviews(*buffer_);
+       if (lyx::graphics::Previews::activated() && buffer_)
+               lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
 }
 
 
@@ -233,7 +378,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
@@ -250,18 +395,18 @@ void BufferView::Pimpl::redoCurrentBuffer()
                resizeCurrentBuffer();
                updateScrollbar();
                owner_->updateLayoutChoice();
-               repaint();
+               update();
        }
 }
 
 
-int BufferView::Pimpl::resizeCurrentBuffer()
+void BufferView::Pimpl::resizeCurrentBuffer()
 {
        lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
 
-       Paragraph * par = 0;
-       Paragraph * selstartpar = 0;
-       Paragraph * selendpar = 0;
+       int par = -1;
+       int selstartpar = -1;
+       int selendpar = -1;
        UpdatableInset * the_locking_inset = 0;
 
        pos_type pos = 0;
@@ -275,44 +420,44 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        owner_->message(_("Formatting document..."));
 
        if (bv_->text) {
-               par = &*bv_->text->cursor.par();
+               par = bv_->text->cursor.par();
                pos = bv_->text->cursor.pos();
-               selstartpar = &*bv_->text->selection.start.par();
+               selstartpar = bv_->text->selection.start.par();
                selstartpos = bv_->text->selection.start.pos();
-               selendpar = &*bv_->text->selection.end.par();
+               selendpar = bv_->text->selection.end.par();
                selendpos = bv_->text->selection.end.pos();
                selection = bv_->text->selection.set();
                mark_set = bv_->text->selection.mark();
                the_locking_inset = bv_->theLockingInset();
-               buffer_->resizeInsets(bv_);
-               // I don't think the delete and new are necessary here we just could
-               // call only init! (Jug 20020419)
-               delete bv_->text;
-               bv_->text = new LyXText(bv_);
-               bv_->text->init(bv_);
+               bv_->text->fullRebreak();
+               update();
        } else {
+               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!" << 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");
-
-                       buffer_->resizeInsets(bv_);
                } else {
-                       bv_->text = new LyXText(bv_);
+                       lyxerr << "no text in cache!" << endl;
+                       bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
                        bv_->text->init(bv_);
-                       //buffer_->resizeInsets(bv_);
                }
        }
 
-       if (par) {
+#warning does not help much
+       //bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
+       //      bv_->text->ownerParagraphs().end());
+
+       if (par != -1) {
                bv_->text->selection.set(true);
                // At this point just to avoid the Delete-Empty-Paragraph-
                // Mechanism when setting the cursor.
@@ -332,7 +477,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
                bv_->theLockingInset(the_locking_inset);
        }
 
-       bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
+       top_y(screen().topCursorVisible(bv_->text));
 
        switchKeyMap();
        owner_->busy(false);
@@ -341,15 +486,6 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        owner_->clearMessage();
 
        updateScrollbar();
-
-       return 0;
-}
-
-
-void BufferView::Pimpl::repaint()
-{
-       // Regenerate the screen.
-       screen().redraw(bv_->text, bv_);
 }
 
 
@@ -364,9 +500,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());
 }
 
 
@@ -377,21 +513,23 @@ void BufferView::Pimpl::scrollDocView(int value)
        if (!buffer_)
                return;
 
-       screen().draw(bv_->text, bv_, value);
+       screen().hideCursor();
+
+       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();
 }
@@ -407,7 +545,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);
@@ -416,7 +554,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());
 }
 
 
@@ -424,6 +562,18 @@ void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
                                         key_modifier::state state)
 {
        bv_->owner()->getLyXFunc().processKeySym(key, state);
+
+       /* This is perhaps a bit of a hack. When we move
+        * around, or type, it's nice to be able to see
+        * the cursor immediately after the keypress. So
+        * we reset the toggle timeout and force the visibility
+        * of the cursor. Note we cannot do this inside
+        * dispatch() itself, because that's called recursively.
+        */
+       if (available()) {
+               cursor_timeout.restart();
+               screen().showCursor(*bv_);
+       }
 }
 
 
@@ -442,7 +592,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);
@@ -456,10 +606,8 @@ void BufferView::Pimpl::selectionRequested()
 void BufferView::Pimpl::selectionLost()
 {
        if (available()) {
-               hideCursor();
-               toggleSelection();
+               screen().hideCursor();
                bv_->getLyXText()->clearSelection();
-               showCursor();
                bv_->text->xsel_cache.set(false);
        }
 }
@@ -491,69 +639,27 @@ void BufferView::Pimpl::workAreaResize()
                        if (lyxerr.debugging())
                                textcache.show(lyxerr, "Expose delete all");
                        textcache.clear();
-                       // FIXME: this is already done in resizeCurrentBuffer() ??
-                       buffer_->resizeInsets(bv_);
-               } else if (heightChange) {
-                       // fitCursor() ensures we don't jump back
-                       // to the start of the document on vertical
-                       // resize
-                       fitCursor();
                }
        }
 
-       if (widthChange || heightChange) {
-               repaint();
-       }
+       if (widthChange || heightChange)
+               update();
 
        // always make sure that the scrollbar is sane.
        updateScrollbar();
        owner_->updateLayoutChoice();
-       return;
 }
 
 
 void BufferView::Pimpl::update()
 {
-       if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
-               screen().update(*bv_);
-               bv_->text->clearPaint();
-       }
-}
-
-
-void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
-{
-       if (!text->selection.set() && (f & SELECT)) {
-               text->selection.cursor = text->cursor;
-       }
-
-       text->fullRebreak();
-
-       if (text->inset_owner) {
-               text->inset_owner->setUpdateStatus(bv_, 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->fullRebreak();
-
-       if (text->inset_owner) {
-               text->inset_owner->setUpdateStatus(bv_, 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_);
 }
 
 
@@ -565,11 +671,7 @@ void BufferView::Pimpl::cursorToggle()
                return;
        }
 
-       if (!bv_->theLockingInset()) {
-               screen().cursorToggle(bv_);
-       } else {
-               bv_->theLockingInset()->toggleInsetCursor(bv_);
-       }
+       screen().toggleCursor(*bv_);
 
        cursor_timeout.restart();
 }
@@ -585,22 +687,21 @@ bool BufferView::Pimpl::available() const
 
 Change const BufferView::Pimpl::getCurrentChange()
 {
-       if (!bv_->buffer()->params.tracking_changes)
+       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);
-       return cur.par()->lookupChangeFull(cur.pos());
+       return text->getPar(text->selection.start)
+               ->lookupChangeFull(text->selection.start.pos());
 }
 
 
 void BufferView::Pimpl::beforeChange(LyXText * text)
 {
-       toggleSelection();
        text->clearSelection();
 }
 
@@ -610,17 +711,10 @@ void BufferView::Pimpl::savePosition(unsigned int i)
        if (i >= saved_positions_num)
                return;
        saved_positions[i] = Position(buffer_->fileName(),
-                                     bv_->text->cursor.par()->id(),
+                                     bv_->text->cursorPar()->id(),
                                      bv_->text->cursor.pos());
-       if (i > 0) {
-               ostringstream str;
-#if USE_BOOST_FORMAT
-               str << boost::format(_("Saved bookmark %1$d")) % i;
-#else
-               str << _("Saved bookmark ") << i;
-#endif
-               owner_->message(STRCONV(str.str()));
-       }
+       if (i > 0)
+               owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
 }
 
 
@@ -634,29 +728,27 @@ void BufferView::Pimpl::restorePosition(unsigned int i)
        beforeChange(bv_->text);
 
        if (fname != buffer_->fileName()) {
-               Buffer * b = bufferlist.exists(fname) ?
-                       bufferlist.getBuffer(fname) :
-                       bufferlist.loadLyXFile(fname); // don't ask, just load it
-               if (b != 0) buffer(b);
+               Buffer * b = 0;
+               if (bufferlist.exists(fname))
+                       b = bufferlist.getBuffer(fname);
+               else {
+                       b = bufferlist.newBuffer(fname);
+                       ::loadLyXFile(b, fname); // don't ask, just load it
+               }
+               if (b)
+                       buffer(b);
        }
 
-       Paragraph * par = &*buffer_->getParFromID(saved_positions[i].par_id);
-       if (!par)
+       ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
+       if (par == buffer_->par_iterator_end())
                return;
 
-       bv_->text->setCursor(par,
+       bv_->text->setCursor(par.pit(),
                             min(par->size(), saved_positions[i].par_pos));
 
-       update(BufferView::SELECT);
-       if (i > 0) {
-               ostringstream str;
-#if USE_BOOST_FORMAT
-               str << boost::format(_("Moved to bookmark %1$d")) % i;
-#else
-               str << _("Moved to bookmark ") << i;
-#endif
-               owner_->message(STRCONV(str.str()));
-       }
+       update();
+       if (i > 0)
+               owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
 }
 
 
@@ -677,7 +769,7 @@ void BufferView::Pimpl::switchKeyMap()
        LyXText * text = bv_->getLyXText();
        if (text->real_current_font.isRightToLeft()
            && !(bv_->theLockingInset()
-                && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
+                && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
        {
                if (owner_->getIntl().keymap == Intl::PRIMARY)
                        owner_->getIntl().KeyMapSec();
@@ -698,47 +790,13 @@ void BufferView::Pimpl::insetUnlock()
 }
 
 
-void BufferView::Pimpl::showCursor()
-{
-       if (bv_->theLockingInset())
-               bv_->theLockingInset()->showInsetCursor(bv_);
-       else
-               screen().showCursor(bv_->text, bv_);
-}
-
-
-void BufferView::Pimpl::hideCursor()
-{
-       if (!bv_->theLockingInset())
-               screen().hideCursor();
-}
-
-
-void BufferView::Pimpl::toggleSelection(bool b)
-{
-       if (bv_->theLockingInset())
-               bv_->theLockingInset()->toggleSelection(bv_, b);
-       screen().toggleSelection(bv_->text, bv_, b);
-}
-
-
-void BufferView::Pimpl::toggleToggle()
-{
-       screen().toggleToggle(bv_->text, bv_);
-}
-
-
 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 ...
 
@@ -751,9 +809,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();
 }
 
 
@@ -769,37 +827,37 @@ void BufferView::Pimpl::stuffClipboard(string const & stuff) const
  */
 
 
-Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
+InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
 {
 #if 0
        LyXCursor cursor = bv_->getLyXText()->cursor;
        Buffer::inset_iterator it =
                find_if(Buffer::inset_iterator(
-                       cursor.par(), cursor.pos()),
+                       cursorPar(), cursor.pos()),
                        buffer_->inset_iterator_end(),
                        lyx::compare_memfun(&Inset::lyxCode, 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;
+       LyXText * text =  bv_->getLyXText();
 
        Buffer::inset_iterator beg = b->inset_iterator_begin();
        Buffer::inset_iterator end = b->inset_iterator_end();
 
-       bool cursor_par_seen = false;
+       bool cursorPar_seen = false;
 
        for (; beg != end; ++beg) {
-               if (beg.getPar() == cursor.par()) {
-                       cursor_par_seen = true;
+               if (beg.getPar() == text->cursorPar()) {
+                       cursorPar_seen = true;
                }
-               if (cursor_par_seen) {
-                       if (beg.getPar() == cursor.par()
-                           && beg.getPos() >= cursor.pos()) {
+               if (cursorPar_seen) {
+                       if (beg.getPar() == text->cursorPar()
+                           && beg.getPos() >= text->cursor.pos()) {
                                break;
-                       } else if (beg.getPar() != cursor.par()) {
+                       } else if (beg.getPar() != text->cursorPar()) {
                                break;
                        }
                }
@@ -838,7 +896,7 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
                        make_pair(string(_("Documents|#o#O")),
                                  string(lyxrc.document_path)),
                        make_pair(string(_("Examples|#E#e")),
-                                 string(AddPath(system_lyxdir, "examples"))));
+                                 string(AddPath(system_lyxdir(), "examples"))));
 
                FileDialog::Result result =
                        fileDlg.open(initpath,
@@ -860,94 +918,95 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
        // necessary
        filename = FileSearch(string(), filename, "lyx");
 
-       string const disp_fn(MakeDisplayPath(filename));
-
-       ostringstream s1;
-#if USE_BOOST_FORMAT
-       s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
-#else
-       s1 << _("Inserting document ") << disp_fn << _("...");
-#endif
-       owner_->message(STRCONV(s1.str()));
-       bool const res = bv_->insertLyXFile(filename);
-       if (res) {
-               ostringstream str;
-#if USE_BOOST_FORMAT
-               str << boost::format(_("Document %1$s inserted.")) % disp_fn;
-#else
-               str << _("Document ") << disp_fn << _(" inserted.");
-#endif
-               owner_->message(STRCONV(str.str()));
-       } else {
-               ostringstream str;
-#if USE_BOOST_FORMAT
-               str << boost::format(_("Could not insert document %1$s")) % disp_fn;
-#else
-               str << _("Could not insert document ") << disp_fn;
-#endif
-               owner_->message(STRCONV(str.str()));
-       }
+       string const disp_fn = MakeDisplayPath(filename);
+       owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
+       if (bv_->insertLyXFile(filename))
+               owner_->message(bformat(_("Document %1$s inserted."),
+                                       disp_fn));
+       else
+               owner_->message(bformat(_("Could not insert document %1$s"),
+                                       disp_fn));
 }
 
 
 void BufferView::Pimpl::trackChanges()
 {
        Buffer * buf(bv_->buffer());
-       bool const tracking(buf->params.tracking_changes);
+       bool const tracking(buf->params().tracking_changes);
 
        if (!tracking) {
                ParIterator const end = buf->par_iterator_end();
-               for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
-                       (*it)->trackChanges();
-               }
-               buf->params.tracking_changes = true;
+               for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
+                       it->trackChanges();
+               buf->params().tracking_changes = true;
 
                // we cannot allow undos beyond the freeze point
-               buf->undostack.clear();
+               buf->undostack().clear();
        } else {
-               update(BufferView::SELECT);
-               bv_->text->setCursor(&(*buf->paragraphs.begin()), 0);
+               update();
+               bv_->text->setCursor(0, 0);
 #warning changes FIXME
                //moveCursorUpdate(false);
 
-               bool found = lyxfind::findNextChange(bv_);
+               bool found = lyx::find::findNextChange(bv_);
                if (found) {
                        owner_->getDialogs().show("changes");
                        return;
                }
 
                ParIterator const end = buf->par_iterator_end();
-               for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
-                       (*it)->untrackChanges();
-               }
-               buf->params.tracking_changes = false;
+               for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
+                       it->untrackChanges();
+               buf->params().tracking_changes = false;
        }
 
-       buf->redostack.clear();
+       buf->redostack().clear();
 }
 
 
-// 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
+
+               // Doesn't go through lyxfunc, so we need to update
+               // the layout choice etc. ourselves
+
+               // e.g. Qt mouse press when no buffer
+               if (!available())
+                       return false;
 
-       bool const res = dispatch(ev_in);
+               screen().hideCursor();
 
-       // FIXME: we should skip these when selecting
-       bv_->owner()->updateLayoutChoice();
-       bv_->owner()->updateToolbar();
-       bv_->fitCursor();
+               bool const res = dispatch(ev);
 
-       // 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();
+               // see workAreaKeyPress
+               cursor_timeout.restart();
+               screen().showCursor(*bv_);
 
-       return res;
+               // 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.
+               owner_->clearMessage();
+
+               return res;
+       }
+       default:
+               owner_->dispatch(ev);
+               return true;
+       }
 }
 
 
@@ -965,7 +1024,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                << " button[" << ev.button() << ']'
                << endl;
 
-       LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
+       LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
 
        switch (ev.action) {
 
@@ -1064,7 +1123,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                string label = ev.argument;
                if (label.empty()) {
                        InsetRef * inset =
-                               static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
+                               static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
                        if (inset) {
                                label = inset->getContents();
                                savePosition(0);
@@ -1102,7 +1161,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;
 
@@ -1134,7 +1193,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
        break;
 
        case LFUN_INSET_INSERT: {
-               Inset * inset = createInset(ev);
+               InsetOld * inset = createInset(ev);
                if (inset && insertInset(inset)) {
                        updateInset(inset);
 
@@ -1142,10 +1201,8 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                        if (name == "bibitem") {
                                // We need to do a redraw because the maximum
                                // InsetBibitem width could have changed
-#warning please check you mean repaint() not update(),
-#warning and whether the repaint() is needed at all
-                               bv_->repaint();
-                               bv_->fitCursor();
+#warning check whether the update() is needed at all
+                               bv_->update();
                        }
                } else {
                        delete inset;
@@ -1155,7 +1212,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
 
        case LFUN_FLOAT_LIST:
                if (tclass.floats().typeExist(ev.argument)) {
-                       Inset * inset = new InsetFloatList(ev.argument);
+                       InsetOld * inset = new InsetFloatList(ev.argument);
                        if (!insertInset(inset, tclass.defaultLayoutName()))
                                delete inset;
                } else {
@@ -1165,12 +1222,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()->cursorPar(), data);
 
                data = "show\n" + data;
                bv_->owner()->getDialogs().show("paragraph", data);
@@ -1178,15 +1231,15 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
        }
 
        case LFUN_PARAGRAPH_UPDATE: {
-               Paragraph const * par = &*bv_->getLyXText()->cursor.par();
-               if (!par)
+               if (!bv_->owner()->getDialogs().visible("paragraph"))
                        break;
+               Paragraph const & par = *bv_->getLyXText()->cursorPar();
 
                string data;
-               params2string(*par, data);
+               params2string(par, data);
 
                // Will the paragraph accept changes from the dialog?
-               Inset * const inset = par->inInset();
+               InsetOld * const inset = par.inInset();
                bool const accept =
                        !(inset && inset->forceDefaultParagraphs(inset));
 
@@ -1204,14 +1257,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(LyXText::WHOLE_WORD);
-                               arg = bv_->getLyXText()->selectionAsString(buffer_, false);
+                               bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
+                               arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
                                // FIXME: where is getLyXText()->unselect(bv_) ?
                        }
                }
@@ -1229,40 +1282,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);
+               bv_->text->setCursor(0, 0);
 #warning FIXME changes
                //moveCursorUpdate(false);
 
-               while (lyxfind::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);
+               bv_->text->setCursor(0, 0);
 #warning FIXME changes
                //moveCursorUpdate(false);
 
-               while (lyxfind::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;
        }
 
@@ -1278,7 +1329,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
 }
 
 
-bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
+bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
 {
        // if we are in a locking inset we should try to insert the
        // inset there otherwise this is a illegal function now
@@ -1289,25 +1340,20 @@ bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
        }
 
        // not quite sure if we want this...
-       setCursorParUndo(bv_);
+       recordUndo(bv_, Undo::ATOMIC);
        freezeUndo();
 
        beforeChange(bv_->text);
        if (!lout.empty()) {
-               update(BufferView::SELECT);
-               bv_->text->breakParagraph(bv_->buffer()->paragraphs);
-               update(BufferView::SELECT);
+               bv_->text->breakParagraph(bv_->buffer()->paragraphs());
 
-               if (!bv_->text->cursor.par()->empty()) {
+               if (!bv_->text->cursorPar()->empty()) {
                        bv_->text->cursorLeft(bv_);
-
-                       bv_->text->breakParagraph(bv_->buffer()->paragraphs);
-                       update(BufferView::SELECT);
+                       bv_->text->breakParagraph(bv_->buffer()->paragraphs());
                }
 
                string lres = lout;
-               LyXTextClass const & tclass =
-                       buffer_->params.getLyXTextClass();
+               LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
                bool hasLayout = tclass.hasLayout(lres);
                string lay = tclass.defaultLayoutName();
 
@@ -1328,58 +1374,68 @@ bool BufferView::Pimpl::insertInset(Inset * 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(Inset * 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));
+
+       // this should not be needed, but it is...
+       // bv_->text->redoParagraph(bv_->text->cursorPar());
+       // bv_->text->fullRebreak();
+
+       update();
+       updateScrollbar();
+}
+
+
+bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
+                                    string const & from, string const & to)
+{
+       bool need_update = false;
+       LyXCursor cursor = bv_->text->cursor;
+       LyXCursor tmpcursor = cursor;
+       cursor.par(tmpcursor.par());
+       cursor.pos(tmpcursor.pos());
+
+       ParIterator end = bv_->buffer()->par_iterator_end();
+       for (ParIterator it = bv_->buffer()->par_iterator_begin();
+            it != end; ++it) {
+               bool changed_inset = false;
+               for (InsetList::iterator it2 = it->insetlist.begin();
+                    it2 != it->insetlist.end(); ++it2) {
+                       if (it2->inset->lyxCode() == code) {
+                               InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
+                               if (inset->getContents() == from) {
+                                       inset->setContents(to);
+                                       changed_inset = true;
+                               }
                        }
                }
-       }
+               if (changed_inset) {
+                       need_update = true;
 
-       // 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
-       Inset * tl_inset = inset;
-       while (tl_inset->owner())
-               tl_inset = tl_inset->owner();
-       hideCursor();
-       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();
+                       // FIXME
+
+                       // The test it.size()==1 was needed to prevent crashes.
+                       // How to set the cursor correctly when it.size()>1 ??
+                       if (it.size() == 1) {
+                               bv_->text->setCursorIntern(bv_->text->parOffset(it.pit()), 0);
+                               bv_->text->redoParagraph(bv_->text->cursorPar());
+                       }
                }
        }
+       bv_->text->setCursorIntern(cursor.par(), cursor.pos());
+       return need_update;
 }