]> git.lyx.org Git - lyx.git/blobdiff - src/bufferview_funcs.C
reduce number of calls to LyXText::getFont
[lyx.git] / src / bufferview_funcs.C
index e83ce1ae85ae08074e1c7eaf534c43eb0a03c7c2..3135a75461e8840ff99790c423f703b3ca6526e8 100644 (file)
@@ -33,6 +33,7 @@
 #include "frontends/Alert.h"
 #include "frontends/LyXView.h"
 
+#include "insets/insetcommand.h"
 #include "insets/insettext.h"
 
 #include "support/convert.h"
@@ -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,7 +176,10 @@ 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);
 }
 
 
@@ -184,7 +190,7 @@ Point getPos(DocIterator const & dit)
        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_;
@@ -205,5 +211,69 @@ 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 (bv->cursor() != doc_iterator_begin(bv->buffer()->inset())) {
+                       tmpcur.reset(tmpcur.bottom().inset());
+                       if (!gotoNextInset(tmpcur, codes, contents)) {
+                               bv->cursor().message(_("No more insets"));
+                               return;
+                       }
+               } else {
+                       bv->cursor().message(_("No more insets"));
+                       return;
+               }
+       }
+
+       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