+
+char MathCursor::valign() const
+{
+ int idx;
+ MathGridInset * p =
+ static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
+ return p ? p->valign() : 0;
+}
+
+
+char MathCursor::halign() const
+{
+ int idx;
+ MathGridInset * p =
+ static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
+ return p ? p->halign(idx % p->ncols()) : 0;
+}
+
+
+MathCursorPos MathCursor::firstSelectionPos() const
+{
+ MathCursorPos anc = normalAnchor();
+ return anc < cursor() ? anc : cursor();
+}
+
+
+MathCursorPos MathCursor::lastSelectionPos() const
+{
+ MathCursorPos anc = normalAnchor();
+ return anc < cursor() ? cursor() : anc;
+}
+
+
+void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
+{
+ MathCursorPos anc = normalAnchor();
+ if (anc < cursor()) {
+ i1 = anc;
+ i2 = cursor();
+ } else {
+ i1 = cursor();
+ i2 = anc;
+ }
+}
+
+
+MathCursorPos & MathCursor::cursor()
+{
+ return Cursor_.back();
+}
+
+
+MathCursorPos const & MathCursor::cursor() const
+{
+ return Cursor_.back();
+}
+
+
+
+////////////////////////////////////////////////////////////////////////
+
+
+bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
+{
+ return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
+}
+
+
+bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
+{
+ if (ti.par_ != it.par_) {
+ lyxerr << "can't compare cursor and anchor in different insets\n";
+ return true;
+ }
+ if (ti.idx_ != it.idx_)
+ return ti.idx_ < it.idx_;
+ return ti.pos_ < it.pos_;
+}
+
+
+MathArray & MathCursorPos::cell(int idx) const
+{
+ return par_->cell(idx);
+}
+
+MathArray & MathCursorPos::cell() const
+{
+ return par_->cell(idx_);
+}
+
+
+MathXArray & MathCursorPos::xcell(int idx) const
+{
+ return par_->xcell(idx);
+}
+
+
+MathXArray & MathCursorPos::xcell() const
+{
+ return par_->xcell(idx_);
+}
+
+
+MathCursorPos MathCursor::normalAnchor() const
+{
+ // use Anchor on the same level as Cursor
+ MathCursorPos normal = Anchor_[Cursor_.size() - 1];
+ if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
+ // anchor is behind cursor -> move anchor behind the inset
+ normal.cell().next(normal.pos_);
+ }
+ //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
+ // << normal << "\n";
+ return normal;
+}
+
+
+bool MathCursorPos::idxUp()
+{
+ return par_->idxUp(idx_, pos_);
+}
+
+
+bool MathCursorPos::idxDown()
+{
+ return par_->idxDown(idx_, pos_);
+}
+
+
+bool MathCursorPos::idxLeft()
+{
+ return par_->idxLeft(idx_, pos_);
+}
+
+
+bool MathCursorPos::idxRight()
+{
+ return par_->idxRight(idx_, pos_);
+}