]> git.lyx.org Git - lyx.git/blobdiff - src/cursor.C
Fix bug 886 and others not reported related with the document paper size.
[lyx.git] / src / cursor.C
index 6aeaec8884d0d8f47d0cf33045fe79c611b77a8e..e54cfe904f94bfe2fd1b9e524060c36de9987b47 100644 (file)
@@ -32,7 +32,6 @@
 #include "paragraph_funcs.h"
 #include "pariterator.h"
 
-#include "insets/updatableinset.h"
 #include "insets/insettabular.h"
 #include "insets/insettext.h"
 
@@ -97,9 +96,7 @@ namespace {
                for (int i = 0; ; ++i) {
                        int xo;
                        int yo;
-                       LCursor cur = c;
-                       cur.setCursor(it);
-                       cur.inset().getCursorPos(cur.top(), xo, yo);
+                       it.inset().cursorPos(it.top(), c.boundary() && ((i+1) == it.depth()), xo, yo);
                        double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
                        // '<=' in order to take the last possible position
                        // this is important for clicking behind \sum in e.g. '\sum_i a'
@@ -123,8 +120,6 @@ namespace {
        {
                BOOST_ASSERT(!cursor.empty());
                CursorSlice bottom = cursor[0];
-               LyXText * text = bottom.text();
-               BOOST_ASSERT(text);
 
                DocIterator it = doc_iterator_begin(bottom.inset());
                DocIterator const et = doc_iterator_end(bottom.inset());
@@ -136,7 +131,7 @@ namespace {
                        // avoid invalid nesting when selecting
                        if (bv_funcs::status(&cursor.bv(), it) == bv_funcs::CUR_INSIDE
                            && (!cursor.selection() || positionable(it, cursor.anchor_))) {
-                               Point p = bv_funcs::getPos(it);
+                               Point p = bv_funcs::getPos(it, false);
                                int xo = p.x_;
                                int yo = p.y_;
                                if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
@@ -169,7 +164,7 @@ namespace {
 // bv functions are not yet available!
 LCursor::LCursor(BufferView & bv)
        : DocIterator(), bv_(&bv), anchor_(), x_target_(-1),
-         selection_(false), mark_(false)
+         selection_(false), mark_(false), logicalpos_(false)
 {}
 
 
@@ -199,6 +194,7 @@ void LCursor::dispatch(FuncRequest const & cmd0)
        if (empty())
                return;
 
+       fixIfBroken();
        FuncRequest cmd = cmd0;
        LCursor safe = *this;
 
@@ -310,7 +306,7 @@ int LCursor::currentMode()
 
 void LCursor::getPos(int & x, int & y) const
 {
-       Point p = bv_funcs::getPos(*this);
+       Point p = bv_funcs::getPos(*this, boundary());
        x = p.x_;
        y = p.y_;
 }
@@ -394,7 +390,7 @@ DocIterator LCursor::selectionEnd() const
 void LCursor::setSelection()
 {
        selection() = true;
-       // a selection with no contents is not a selection
+       // A selection with no contents is not a selection
 #ifdef WITH_WARNINGS
 #warning doesnt look ok
 #endif
@@ -409,6 +405,9 @@ void LCursor::setSelection(DocIterator const & where, size_t n)
        selection() = true;
        anchor_ = where;
        pos() += n;
+       // Open all collapsed insets
+       for (int i = depth() - 1; i >= 0; --i)
+               operator[](i).inset().setStatus(*this, InsetBase::Open);
 }
 
 
@@ -508,13 +507,24 @@ std::ostream & operator<<(std::ostream & os, LCursor const & cur)
 
 bool LCursor::isInside(InsetBase const * p)
 {
-       for (unsigned i = 0; i < depth(); ++i)
+       for (size_t i = 0; i != depth(); ++i)
                if (&operator[](i).inset() == p)
                        return true;
        return false;
 }
 
 
+void LCursor::leaveInset(InsetBase const & inset)
+{
+       for (size_t i = 0; i != depth(); ++i) {
+               if (&operator[](i).inset() == &inset) {
+                       resize(i);
+                       return;
+               }
+       }
+}
+
+
 bool LCursor::openable(MathAtom const & t) const
 {
        if (!t->isActive())
@@ -1139,3 +1149,41 @@ LyXFont LCursor::getFont() const
 
        return font;
 }
+
+
+void LCursor::fixIfBroken()
+{
+       // find out last good level
+       LCursor copy = *this;
+       size_t newdepth = depth();
+       while (!copy.empty()) {
+               if (copy.idx() > copy.lastidx()) {
+                       lyxerr << "wrong idx " << copy.idx()
+                              << ", max is " << copy.lastidx()
+                              << " at level " << copy.depth()
+                              << ". Trying to correct this."  << endl;
+                       newdepth = copy.depth() - 1;
+               }
+               else if (copy.pit() > copy.lastpit()) {
+                       lyxerr << "wrong pit " << copy.pit()
+                              << ", max is " << copy.lastpit()
+                              << " at level " << copy.depth()
+                              << ". Trying to correct this."  << endl;
+                       newdepth = copy.depth() - 1;
+               }
+               else if (copy.pos() > copy.lastpos()) {
+                       lyxerr << "wrong pos " << copy.pos()
+                              << ", max is " << copy.lastpos()
+                              << " at level " << copy.depth()
+                              << ". Trying to correct this."  << endl;
+                       newdepth = copy.depth() - 1;
+               }
+               copy.pop();
+       }
+       // shrink cursor to a size where everything is valid, possibly
+       // leaving insets
+       while (depth() > newdepth) {
+               pop();
+               lyxerr << "correcting cursor to level " << depth() << endl;
+       }
+}