]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.C
more cursor dispatch
[lyx.git] / src / BufferView.C
index 97def2dfae54995202995168892613aceb88b3fd..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,8 +60,7 @@ 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)
+       : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
 {}
 
 
@@ -68,6 +70,12 @@ BufferView::~BufferView()
 }
 
 
+void BufferView::unsetXSel()
+{
+       pimpl_->xsel_cache_.set = false;
+}
+
+
 Buffer * BufferView::buffer() const
 {
        return pimpl_->buffer_;
@@ -253,7 +261,7 @@ bool BufferView::insertLyXFile(string const & filen)
 
        string const fname = MakeAbsPath(filen);
 
-       text()->clearSelection();
+       cursor().clearSelection();
        text()->breakParagraph(buffer()->paragraphs());
 
        bool res = buffer()->readFile(fname, text()->cursorPar());
@@ -293,7 +301,7 @@ void BufferView::setCursorFromRow(int row)
 }
 
 
-bool BufferView::insertInset(InsetOld * inset, string const & lout)
+bool BufferView::insertInset(InsetBase * inset, string const & lout)
 {
        return pimpl_->insertInset(inset, lout);
 }
@@ -306,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();
+                       cursor().clearSelection();
                        text()->setCursor(
-                               std::distance(text()->ownerParagraphs().begin(), it.getPar()),
+                               distance(text()->paragraphs().begin(), it.getPar()),
                                it.getPos());
-                       text()->selection.cursor = text()->cursor;
+                       cursor().resetAnchor();
                        update();
                        return;
                }
@@ -324,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();
@@ -338,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();
@@ -385,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();
@@ -410,10 +412,10 @@ Encoding const * BufferView::getEncoding() const
        LyXText * t = getLyXText();
        if (!t)
                return 0;
-       return t->cursorPar()->getFont(
-               buffer()->params(),
-               t->cursor.pos(),
-               outerFont(t->cursorPar(), t->ownerParagraphs())
+       CursorSlice const & cur = cursor().innerTextSlice();
+       return t->getPar(cur.par())->getFont(
+               buffer()->params(), cur.pos(),
+               outerFont(t->getPar(cur.par()), t->paragraphs())
        ).language()->encoding();
 }
 
@@ -430,37 +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);
 }
 
 
-int BufferView::x_target() const
+/*
+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)
 {
-       return x_target_;
+       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();
 }
 
 
-void BufferView::updateParagraphDialog()
+LCursor & BufferView::cursor()
 {
-       pimpl_->updateParagraphDialog();
+       return pimpl_->cursor_;
 }
 
 
-LyXText * BufferView::text() const
+LCursor const & BufferView::cursor() const
 {
-       return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
+       return pimpl_->cursor_;
 }