]> git.lyx.org Git - lyx.git/blobdiff - src/bufferview_funcs.C
fix crash when collapsing ert with cursor inside
[lyx.git] / src / bufferview_funcs.C
index d4dbfe353a9151bb0495da61d822b55da608c039..170ed0b80e0fb8cfbae8a527cdf52d48929b9940 100644 (file)
 #include "frontends/Alert.h"
 #include "frontends/LyXView.h"
 
+#include "insets/insetcommand.h"
 #include "insets/insettext.h"
 
-#include "support/tostr.h"
+#include "support/convert.h"
 
 #include <sstream>
 
@@ -44,6 +45,7 @@ using lyx::support::bformat;
 using std::istringstream;
 using std::ostringstream;
 using std::string;
+using std::vector;
 
 
 namespace bv_funcs {
@@ -156,9 +158,10 @@ Point coordOffset(DocIterator const & dit)
        int y = 0;
 
        // Contribution of nested insets
-       for (size_t i = 1; i != dit.size(); ++i) {
+       for (size_t i = 1; i != dit.depth(); ++i) {
                CursorSlice const & sl = dit[i];
-               int xx = 0, yy = 0;
+               int xx = 0;
+               int yy = 0;
                sl.inset().getCursorPos(sl, xx, yy);
                x += xx;
                y += yy;
@@ -173,18 +176,21 @@ Point coordOffset(DocIterator const & dit)
                y += par.rows()[rit].height();
        y += par.rows()[par.pos2row(sl.pos())].ascent();
        x += dit.bottom().text()->cursorX(dit.bottom());
-       return Point(x,y);
+       // The following correction should not be there at all.
+       // The cusor looks much better with the -1, though.
+       --x;
+       return Point(x, y);
 }
 
 
 Point getPos(DocIterator const & dit)
 {
        CursorSlice const & bot = dit.bottom();
-       CoordCache::InnerParPosCache & cache = theCoords.pars_[bot.text()];
-       CoordCache::InnerParPosCache::iterator it = cache.find(bot.pit());
+       CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(bot.text())->second;
+       CoordCache::InnerParPosCache::const_iterator it = cache.find(bot.pit());
        if (it == cache.end()) {
                //lyxerr << "cursor out of view" << std::endl;
-               return Point(-1,-1);
+               return Point(-1, -1);
        }
        Point p = coordOffset(dit); // offset from outer paragraph
        p.y_ += it->second.y_;
@@ -195,7 +201,7 @@ Point getPos(DocIterator const & dit)
 // this could be used elsewhere as well?
 CurStatus status(BufferView const * bv, DocIterator const & dit)
 {
-       CoordCache::InnerParPosCache & cache = theCoords.pars_[dit.bottom().text()];
+       CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
 
        if (cache.find(dit.bottom().pit()) != cache.end())
                return CUR_INSIDE;
@@ -205,5 +211,66 @@ CurStatus status(BufferView const * bv, DocIterator const & dit)
                return CUR_BELOW;
 }
 
+namespace {
+
+bool gotoNextInset(LCursor & cur,
+                  vector<InsetBase_code> const & codes,
+                  string const & contents)
+{
+       LCursor tmpcur = cur;
+
+       while (tmpcur) {
+               InsetBase const * inset = tmpcur.nextInset();
+               if (inset
+                   && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
+                   && (contents.empty() ||
+                       static_cast<InsetCommand const *>(inset)->getContents() == contents)) {
+                       cur = tmpcur;
+                       return true;
+               }
+               tmpcur.forwardInset();
+       }
+
+       return false;
+}
+
+}
+
+
+void gotoInset(BufferView * bv, vector<InsetBase_code> const & codes,
+              bool same_content)
+{
+       string contents;
+       LCursor tmpcur = bv->cursor();
+       tmpcur.forwardInset();
+
+       if (same_content) {
+               InsetBase const * inset = tmpcur.nextInset();
+               if (inset
+                   && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
+                       contents = static_cast<InsetCommand const *>(inset)->getContents();
+               }
+       }
+
+       if (!gotoNextInset(tmpcur, codes, contents)) {
+               if (tmpcur != doc_iterator_begin(tmpcur.inset())) {
+                       tmpcur.reset(tmpcur.bottom().inset());
+                       if (!gotoNextInset(tmpcur, codes, contents))
+                               bv->cursor().message(_("No more insets"));
+               } else {
+                       bv->cursor().message(_("No more insets"));
+               }
+       }
+
+       tmpcur.clearSelection();
+       bv->setCursor(tmpcur);
+}
+
+
+void gotoInset(BufferView * bv, InsetBase_code code, bool same_content)
+{
+       gotoInset(bv, vector<InsetBase_code>(1, code), same_content);
+}
+
 
 } // namespace bv_funcs