]> 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 c5281728fd81239b1d44b8762347b9ccd524b9ed..b7eafa9d9663223a7f89083ecc79dc97c681f35f 100644 (file)
@@ -1,20 +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 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>
 
 #include "BufferView_pimpl.h"
-#include "bufferlist.h"
 #include "buffer.h"
 #include "buffer_funcs.h"
-#include "bufferview_funcs.h"
-#include "lfuns.h"
+#include "bufferlist.h"
+#include "bufferparams.h"
+#include "cursor.h"
 #include "debug.h"
 #include "factory.h"
 #include "FloatList.h"
@@ -22,7 +31,6 @@
 #include "gettext.h"
 #include "intl.h"
 #include "iterators.h"
-#include "Lsstream.h"
 #include "lyx_cb.h" // added for Dispatch functions
 #include "lyx_main.h"
 #include "lyxfind.h"
 #include "lyxrc.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/tostr.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 <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 lyx::support;
-using namespace bv_funcs;
 
 extern BufferList bufferlist;
 
@@ -263,6 +279,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 ("
@@ -283,6 +311,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;
@@ -300,7 +330,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)
@@ -328,7 +358,7 @@ 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);
        }
 
@@ -374,9 +404,9 @@ void BufferView::Pimpl::resizeCurrentBuffer()
 {
        lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
 
-       ParagraphList::iterator par;
-       ParagraphList::iterator selstartpar;
-       ParagraphList::iterator selendpar;
+       int par = -1;
+       int selstartpar = -1;
+       int selendpar = -1;
        UpdatableInset * the_locking_inset = 0;
 
        pos_type pos = 0;
@@ -418,20 +448,16 @@ void BufferView::Pimpl::resizeCurrentBuffer()
                                textcache.show(lyxerr, "resizeCurrentBuffer");
                } else {
                        lyxerr << "no text in cache!" << endl;
-                       bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs);
+                       bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
                        bv_->text->init(bv_);
                }
-
-               par = bv_->text->ownerParagraphs().end();
-               selstartpar = bv_->text->ownerParagraphs().end();
-               selendpar = bv_->text->ownerParagraphs().end();
        }
 
-#warning doesn't help much
-       bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
-               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()) {
+       if (par != -1) {
                bv_->text->selection.set(true);
                // At this point just to avoid the Delete-Empty-Paragraph-
                // Mechanism when setting the cursor.
@@ -451,7 +477,7 @@ void 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);
@@ -474,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());
 }
 
 
@@ -489,21 +515,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();
 }
@@ -519,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);
@@ -528,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());
 }
 
 
@@ -566,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);
@@ -627,12 +653,11 @@ void BufferView::Pimpl::workAreaResize()
 
 void BufferView::Pimpl::update()
 {
-       lyxerr << "BufferView::update()" << endl;
+       //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();
-               fitCursor();
        }
        screen().redraw(*bv_);
 }
@@ -662,7 +687,7 @@ 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 * text = bv_->getLyXText();
@@ -670,8 +695,8 @@ Change const BufferView::Pimpl::getCurrentChange()
        if (!text->selection.set())
                return Change(Change::UNCHANGED);
 
-       LyXCursor const & cur = text->selection.start;
-       return cur.par()->lookupChangeFull(cur.pos());
+       return text->getPar(text->selection.start)
+               ->lookupChangeFull(text->selection.start.pos());
 }
 
 
@@ -686,7 +711,7 @@ 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)
                owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
@@ -703,14 +728,14 @@ void BufferView::Pimpl::restorePosition(unsigned int i)
        beforeChange(bv_->text);
 
        if (fname != buffer_->fileName()) {
-               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 != 0)
+               if (b)
                        buffer(b);
        }
 
@@ -767,14 +792,11 @@ void BufferView::Pimpl::insetUnlock()
 
 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 ...
 
@@ -787,8 +809,8 @@ 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);
-
+       top_y(new_y);
+       //screen().draw();
        update();
 }
 
@@ -811,31 +833,31 @@ InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
        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;
                        }
                }
@@ -910,19 +932,19 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
 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;
+               buf->params().tracking_changes = true;
 
                // we cannot allow undos beyond the freeze point
-               buf->undostack.clear();
+               buf->undostack().clear();
        } else {
                update();
-               bv_->text->setCursor(buf->paragraphs.begin(), 0);
+               bv_->text->setCursor(0, 0);
 #warning changes FIXME
                //moveCursorUpdate(false);
 
@@ -935,10 +957,10 @@ void BufferView::Pimpl::trackChanges()
                ParIterator const end = buf->par_iterator_end();
                for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
                        it->untrackChanges();
-               buf->params.tracking_changes = false;
+               buf->params().tracking_changes = false;
        }
 
-       buf->redostack.clear();
+       buf->redostack().clear();
 }
 
 
@@ -964,7 +986,7 @@ bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
                screen().hideCursor();
 
                bool const res = dispatch(ev);
-               
+
                // see workAreaKeyPress
                cursor_timeout.restart();
                screen().showCursor(*bv_);
@@ -1002,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) {
 
@@ -1173,13 +1195,12 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
        case LFUN_INSET_INSERT: {
                InsetOld * inset = createInset(ev);
                if (inset && insertInset(inset)) {
-                       updateInset();
+                       updateInset(inset);
 
                        string const name = ev.getArg(0);
                        if (name == "bibitem") {
                                // We need to do a redraw because the maximum
                                // InsetBibitem width could have changed
-                               bv_->fitCursor();
 #warning check whether the update() is needed at all
                                bv_->update();
                        }
@@ -1201,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);
@@ -1216,7 +1233,7 @@ 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();
+               Paragraph const & par = *bv_->getLyXText()->cursorPar();
 
                string data;
                params2string(par, data);
@@ -1240,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(lyx::WHOLE_WORD);
-                               arg = bv_->getLyXText()->selectionAsString(buffer_, false);
+                               arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
                                // FIXME: where is getLyXText()->unselect(bv_) ?
                        }
                }
@@ -1265,7 +1282,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
                break;
 
        case LFUN_ACCEPT_ALL_CHANGES: {
-               bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
+               bv_->text->setCursor(0, 0);
 #warning FIXME changes
                //moveCursorUpdate(false);
 
@@ -1277,7 +1294,7 @@ bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
        }
 
        case LFUN_REJECT_ALL_CHANGES: {
-               bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
+               bv_->text->setCursor(0, 0);
 #warning FIXME changes
                //moveCursorUpdate(false);
 
@@ -1328,15 +1345,15 @@ bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
 
        beforeChange(bv_->text);
        if (!lout.empty()) {
-               bv_->text->breakParagraph(bv_->buffer()->paragraphs);
+               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);
+                       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();
 
@@ -1367,14 +1384,58 @@ bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
 }
 
 
-void BufferView::Pimpl::updateInset()
+void BufferView::Pimpl::updateInset(InsetOld const * inset)
 {
        if (!available())
                return;
 
+       bv_->text->redoParagraph(outerPar(*bv_->buffer(), inset));
+
        // this should not be needed, but it is...
-       bv_->text->redoParagraph(bv_->text->cursor.par());
+       // 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;
+
+                       // 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;
+}