#include "bufferparams.h"
#include "BufferView.h"
#include "cursor.h"
+#include "coordcache.h"
#include "gettext.h"
#include "language.h"
#include "LColor.h"
#include "lyxrow.h"
#include "paragraph.h"
#include "ParagraphParameters.h"
-#include "iterators.h"
+#include "pariterator.h"
#include "frontends/Alert.h"
#include "frontends/LyXView.h"
+#include "insets/insetcommand.h"
#include "insets/insettext.h"
-#include "support/tostr.h"
-#include "support/std_sstream.h"
+#include "support/convert.h"
+
+#include <sstream>
using lyx::support::bformat;
using std::istringstream;
using std::ostringstream;
using std::string;
+using std::vector;
namespace bv_funcs {
// Set data using font and toggle
// If successful, returns true
-bool font2string(LyXFont const & font, bool toggle, string & data)
+bool font2string(LyXFont const & font, bool const toggle, string & data)
{
string lang = "ignore";
if (font.language())
<< "number " << font.number() << '\n'
<< "color " << font.color() << '\n'
<< "language " << lang << '\n'
- << "toggleall " << tostr(toggle);
+ << "toggleall " << convert<string>(toggle);
data = os.str();
return true;
}
return (nset > 0);
}
+
+// the next two should probably go elsewhere
+// this give the position relative to (0, baseline) of outermost
+// paragraph
+Point coordOffset(DocIterator const & dit)
+{
+ int x = 0;
+ int y = 0;
+
+ // Contribution of nested insets
+ for (size_t i = 1; i != dit.depth(); ++i) {
+ CursorSlice const & sl = dit[i];
+ int xx = 0;
+ int yy = 0;
+ sl.inset().getCursorPos(sl, xx, yy);
+ x += xx;
+ y += yy;
+ //lyxerr << "LCursor::getPos, i: " << i << " x: " << xx << " y: " << y << endl;
+ }
+
+ // Add contribution of initial rows of outermost paragraph
+ CursorSlice const & sl = dit[0];
+ Paragraph const & par = sl.text()->getPar(sl.pit());
+ y -= par.rows()[0].ascent();
+ for (size_t rit = 0, rend = par.pos2row(sl.pos()); rit != rend; ++rit)
+ y += par.rows()[rit].height();
+ y += par.rows()[par.pos2row(sl.pos())].ascent();
+ x += dit.bottom().text()->cursorX(dit.bottom());
+ // 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 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);
+ }
+ Point p = coordOffset(dit); // offset from outer paragraph
+ p.y_ += it->second.y_;
+ return p;
+}
+
+
+// this could be used elsewhere as well?
+CurStatus status(BufferView const * bv, DocIterator const & dit)
+{
+ CoordCache::InnerParPosCache const & cache = theCoords.getParPos().find(dit.bottom().text())->second;
+
+ if (cache.find(dit.bottom().pit()) != cache.end())
+ return CUR_INSIDE;
+ else if (dit.bottom().pit() < bv->anchor_ref())
+ return CUR_ABOVE;
+ else
+ 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