]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.C
more cursor dispatch
[lyx.git] / src / BufferView.C
index 747768eb149364c85eb5c98d8e68983754cbc913..d1ff326a342b1ed739c172f98612c333a29d6fc8 100644 (file)
@@ -28,6 +28,7 @@
 #include "lyxtext.h"
 #include "paragraph.h"
 #include "paragraph_funcs.h"
+#include "PosIterator.h"
 #include "texrow.h"
 #include "undo.h"
 #include "WordLangTuple.h"
 using lyx::support::bformat;
 using lyx::support::MakeAbsPath;
 
+using std::distance;
 using std::find;
 using std::string;
+using std::swap;
 using std::vector;
 
 
@@ -57,20 +60,22 @@ extern BufferList bufferlist;
 
 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
                       int width, int height)
-       : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height)),
-         x_target_(0)
-{
-       text = 0;
-}
+       : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
+{}
 
 
 BufferView::~BufferView()
 {
-       delete text;
        delete pimpl_;
 }
 
 
+void BufferView::unsetXSel()
+{
+       pimpl_->xsel_cache_.set = false;
+}
+
+
 Buffer * BufferView::buffer() const
 {
        return pimpl_->buffer_;
@@ -256,10 +261,10 @@ bool BufferView::insertLyXFile(string const & filen)
 
        string const fname = MakeAbsPath(filen);
 
-       text->clearSelection();
-       text->breakParagraph(buffer()->paragraphs());
+       cursor().clearSelection();
+       text()->breakParagraph(buffer()->paragraphs());
 
-       bool res = buffer()->readFile(fname, text->cursorPar());
+       bool res = buffer()->readFile(fname, text()->cursorPar());
        resize();
        return res;
 }
@@ -290,13 +295,13 @@ void BufferView::setCursorFromRow(int row)
        buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
 
        if (tmpid == -1)
-               text->setCursor(0, 0);
+               text()->setCursor(0, 0);
        else
-               text->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
+               text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
 }
 
 
-bool BufferView::insertInset(InsetOld * inset, string const & lout)
+bool BufferView::insertInset(InsetBase * inset, string const & lout)
 {
        return pimpl_->insertInset(inset, lout);
 }
@@ -309,11 +314,11 @@ void BufferView::gotoLabel(string const & label)
                vector<string> labels;
                it->getLabelList(*buffer(), labels);
                if (find(labels.begin(),labels.end(),label) != labels.end()) {
-                       text->clearSelection();
-                       text->setCursor(
-                               std::distance(text->ownerParagraphs().begin(), it.getPar()),
+                       cursor().clearSelection();
+                       text()->setCursor(
+                               distance(text()->paragraphs().begin(), it.getPar()),
                                it.getPos());
-                       text->selection.cursor = text->cursor;
+                       cursor().resetAnchor();
                        update();
                        return;
                }
@@ -327,8 +332,8 @@ void BufferView::undo()
                return;
 
        owner()->message(_("Undo"));
-       text->clearSelection();
-       if (!textUndo(this))
+       cursor().clearSelection();
+       if (!textUndo(*this))
                owner()->message(_("No further undo information"));
        update();
        switchKeyMap();
@@ -341,8 +346,8 @@ void BufferView::redo()
                return;
 
        owner()->message(_("Redo"));
-       text->clearSelection();
-       if (!textRedo(this))
+       cursor().clearSelection();
+       if (!textRedo(*this))
                owner()->message(_("No further redo information"));
        update();
        switchKeyMap();
@@ -354,14 +359,14 @@ void BufferView::replaceWord(string const & replacestring)
        if (!available())
                return;
 
-       LyXText * text = getLyXText();
+       LyXText * t = getLyXText();
 
-       text->replaceSelectionWithString(replacestring);
-       text->setSelectionRange(replacestring.length());
+       t->replaceSelectionWithString(replacestring);
+       t->setSelectionRange(replacestring.length());
 
        // Go back so that replacement string is also spellchecked
        for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
-               text->cursorLeft(this);
+               t->cursorLeft(this);
 
        // FIXME: should be done through LFUN
        buffer()->markDirty();
@@ -369,37 +374,12 @@ void BufferView::replaceWord(string const & replacestring)
 }
 
 
-bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
-{
-       lyxerr << "BufferView::fitLockedInsetCursor x: " << x
-               << " y: " << y << std::endl;
-       UpdatableInset * tli =
-               static_cast<UpdatableInset *>(cursor().innerInset());
-       if (tli && available()) {
-               lyxerr << "    text->cursor.y: " << text->cursor.y() << std::endl;
-               lyxerr << "    insetInInsetY: " << tli->insetInInsetY() << std::endl;
-               y += text->cursor.y() + tli->insetInInsetY();
-               if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
-                       updateScrollbar();
-                       return true;
-               }
-       }
-       return false;
-}
-
-
 void BufferView::hideCursor()
 {
        screen().hideCursor();
 }
 
 
-void BufferView::updateInset(InsetOld const * inset)
-{
-       pimpl_->updateInset(inset);
-}
-
-
 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
 {
        // Check if the label 'from' appears more than once
@@ -413,12 +393,6 @@ bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
 }
 
 
-UpdatableInset * BufferView::innerInset() const
-{
-       return static_cast<UpdatableInset*>(cursor().innerInset());
-}
-
-
 LyXText * BufferView::getLyXText() const
 {
        return cursor().innerText();
@@ -435,11 +409,13 @@ Language const * BufferView::getParentLanguage(InsetOld * inset) const
 
 Encoding const * BufferView::getEncoding() const
 {
-       LyXText * text = getLyXText();
-       return text->cursorPar()->getFont(
-               buffer()->params(),
-               text->cursor.pos(),
-               outerFont(text->cursorPar(), text->ownerParagraphs())
+       LyXText * t = getLyXText();
+       if (!t)
+               return 0;
+       CursorSlice const & cur = cursor().innerTextSlice();
+       return t->getPar(cur.par())->getFont(
+               buffer()->params(), cur.pos(),
+               outerFont(t->getPar(cur.par()), t->paragraphs())
        ).language()->encoding();
 }
 
@@ -456,31 +432,83 @@ int BufferView::workHeight() const
 }
 
 
-LCursor & BufferView::cursor()
+void BufferView::updateParagraphDialog()
 {
-       return pimpl_->cursor_;
+       pimpl_->updateParagraphDialog();
 }
 
 
-LCursor const & BufferView::cursor() const
+LyXText * BufferView::text() const
 {
-       return pimpl_->cursor_;
+       return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
 }
 
 
-void BufferView::x_target(int x)
+void BufferView::setCursor(ParIterator const & par,
+                          lyx::pos_type pos)
 {
-       x_target_ = x;
+       LCursor & cur = cursor();
+       cur.reset();
+       ParIterator::PosHolder const & positions = par.positions();
+       int const last = par.size() - 1;
+       for (int i = 0; i < last; ++i)
+               (*positions[i].it)->inset->edit(cur, true);
+       cur.resetAnchor();
+       LyXText * lt = par.text(*buffer());
+       lt->setCursor(par.pit(), pos);
+}
+
+
+/*
+if the fitCursor call refers to some point in never-explored-land, then we
+don't have y information in insets there, then we cannot even do an update
+to get it (because we need the y infomation for setting top_y first). So
+this is solved in putSelectionAt with:
+
+- setting top_y to the y of the outerPar (that has good info)
+- calling update
+- calling cursor().updatePos()
+- then call fitCursor()
+
+Ab.
+*/
+
+void BufferView::putSelectionAt(PosIterator const & cur,
+                     int length, bool backwards)
+{
+       ParIterator par(cur);
+
+       cursor().clearSelection();
+
+       LyXText * text = par.text(*buffer());
+       setCursor(par, cur.pos());
+       
+       // hack for the chicken and egg problem
+       if (par.inset())
+               top_y(par.outerPar()->y);
+       update();
+       text->setCursor(cur.pit(), cur.pos());
+       cursor().updatePos();
+
+       if (length) {
+               text->setSelectionRange(length);
+               cursor().setSelection();
+               if (backwards)
+                       swap(cursor().cursor_, cursor().anchor_);
+       }
+
+       fitCursor();
+       update();
 }
 
 
-int BufferView::x_target() const
+LCursor & BufferView::cursor()
 {
-       return x_target_;
+       return pimpl_->cursor_;
 }
 
 
-void BufferView::updateParagraphDialog()
+LCursor const & BufferView::cursor() const
 {
-       pimpl_->updateParagraphDialog();
+       return pimpl_->cursor_;
 }