#include "insets/insettext.h"
#include "mathed/math_data.h"
-#include "mathed/math_support.h"
#include "mathed/math_inset.h"
+#include "mathed/math_macrotable.h"
#include "support/limited_stack.h"
#include "support/std_sstream.h"
using std::swap;
-
-// our own cut buffer
-limited_stack<string> theCutBuffer;
-
-
-namespace {
-
-void region(CursorSlice const & i1, CursorSlice const & i2,
- LCursor::row_type & r1, LCursor::row_type & r2,
- LCursor::col_type & c1, LCursor::col_type & c2)
-{
- InsetBase & p = i1.inset();
- c1 = p.col(i1.idx());
- c2 = p.col(i2.idx());
- if (c1 > c2)
- swap(c1, c2);
- r1 = p.row(i1.idx());
- r2 = p.row(i2.idx());
- if (r1 > r2)
- swap(r1, r2);
-}
-
-}
-
-
LCursor::LCursor(BufferView & bv)
- : DocIterator(), bv_(&bv),
- anchor_(), cached_y_(0), x_target_(-1),
+ : DocIterator(), bv_(&bv), anchor_(), x_target_(-1),
selection_(false), mark_(false)
{}
clear();
push_back(CursorSlice(inset));
anchor_ = DocIterator(inset);
- cached_y_ = 0;
clearTargetX();
selection_ = false;
mark_ = false;
if (!disp_.dispatched()) {
lyxerr << "RESTORING OLD CURSOR!" << endl;
operator=(safe);
+ disp_.dispatched(false);
}
return disp_;
}
BufferView & LCursor::bv() const
{
+ BOOST_ASSERT(bv_);
return *bv_;
}
+Buffer & LCursor::buffer() const
+{
+ BOOST_ASSERT(bv_);
+ BOOST_ASSERT(bv_->buffer());
+ return *bv_->buffer();
+}
+
+
void LCursor::pop()
{
BOOST_ASSERT(size() >= 1);
}
-void LCursor::updatePos()
-{
- BOOST_ASSERT(!empty());
- if (size() > 1)
- cached_y_ = bv().top_y() + back().inset().yo();
- //cached_y_ = back().inset().yo();
-}
-
-
void LCursor::getDim(int & asc, int & des) const
{
if (inMathed()) {
x = 0;
y = 0;
if (!empty())
- inset().getCursorPos(back(), x, y);
- // getCursorPos gives _screen_ coordinates. We need to add
- // top_y to get document coordinates. This is hidden in cached_y_.
- //y += cached_y_ - inset().yo();
- // The rest is non-obvious. The reason we have to have these
- // extra computation is that the getCursorPos() calls rely
- // on the inset's own knowledge of its screen position.
- // If we scroll up or down in a big enough increment,
- // inset->draw() is not called: this doesn't update
- // inset.yo_, so getCursor() returns an old value.
- // Ugly as you like.
+ inset().getCursorPos(*this, x, y);
}
}
-CursorSlice & LCursor::anchor()
-{
- if (anchor_.size() < size()) {
- lyxerr << "anchor_.size() < cursor_.size() "
- "should not happen when accessing the anchor" << endl;
- BOOST_ASSERT(false);
- }
- BOOST_ASSERT(!anchor_.empty());
- // this size is cursor_.size()
- return anchor_[size() - 1];
-}
-
-
-CursorSlice const & LCursor::anchor() const
+CursorSlice LCursor::anchor() const
{
- if (anchor_.size() < size()) {
- lyxerr << "anchor_.size() < cursor_.size() "
- "should not happen when accessing the anchor" << endl;
- BOOST_ASSERT(false);
+ BOOST_ASSERT(anchor_.size() >= size());
+ CursorSlice normal = anchor_[size() - 1];
+ if (size() < anchor_.size() && back() <= normal) {
+ // anchor is behind cursor -> move anchor behind the inset
+ ++normal.pos();
}
- // this size is cursor_.size()
- BOOST_ASSERT(!anchor_.empty());
- return anchor_[size() - 1];
+ return normal;
}
-CursorSlice const & LCursor::selBegin() const
+CursorSlice LCursor::selBegin() const
{
if (!selection())
return back();
}
-CursorSlice const & LCursor::selEnd() const
+CursorSlice LCursor::selEnd() const
{
if (!selection())
return back();
{
selection() = true;
// a selection with no contents is not a selection
+#ifdef WITH_WARNINGS
#warning doesnt look ok
+#endif
if (par() == anchor().par() && pos() == anchor().pos())
selection() = false;
}
}
-string LCursor::grabSelection()
-{
- if (!selection())
- return string();
-
- CursorSlice i1 = selBegin();
- CursorSlice i2 = selEnd();
-
- if (i1.idx() == i2.idx()) {
- if (i1.inset().asMathInset()) {
- MathArray::const_iterator it = i1.cell().begin();
- return asString(MathArray(it + i1.pos(), it + i2.pos()));
- } else {
- return "unknown selection 1";
- }
- }
-
- row_type r1, r2;
- col_type c1, c2;
- region(i1, i2, r1, r2, c1, c2);
-
- string data;
- if (i1.inset().asMathInset()) {
- for (row_type row = r1; row <= r2; ++row) {
- if (row > r1)
- data += "\\\\";
- for (col_type col = c1; col <= c2; ++col) {
- if (col > c1)
- data += '&';
- data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
- }
- }
- } else {
- data = "unknown selection 2";
- }
- return data;
-}
-
-
-void LCursor::eraseSelection()
-{
- //lyxerr << "LCursor::eraseSelection" << endl;
- CursorSlice const & i1 = selBegin();
- CursorSlice const & i2 = selEnd();
-#ifdef WITH_WARNINGS
-#warning FIXME
-#endif
- if (i1.inset().asMathInset()) {
- if (i1.idx() == i2.idx()) {
- i1.cell().erase(i1.pos(), i2.pos());
- } else {
- MathInset * p = i1.asMathInset();
- row_type r1, r2;
- col_type c1, c2;
- region(i1, i2, r1, r2, c1, c2);
- for (row_type row = r1; row <= r2; ++row)
- for (col_type col = c1; col <= c2; ++col)
- p->cell(p->index(row, col)).clear();
- }
- back() = i1;
- } else {
- lyxerr << "can't erase this selection 1" << endl;
- }
- //lyxerr << "LCursor::eraseSelection end" << endl;
-}
-
-
-string LCursor::grabAndEraseSelection()
-{
- if (!selection())
- return string();
- string res = grabSelection();
- eraseSelection();
- selection() = false;
- return res;
-}
-
-
-void LCursor::selCopy()
-{
- if (selection()) {
- theCutBuffer.push(grabSelection());
- selection() = false;
- } else {
- //theCutBuffer.erase();
- }
-}
-
-
-void LCursor::selCut()
-{
- theCutBuffer.push(grabAndEraseSelection());
-}
-
-
-void LCursor::selDel()
-{
- //lyxerr << "LCursor::selDel" << endl;
- if (selection()) {
- eraseSelection();
- selection() = false;
- }
-}
-
-
-void LCursor::selPaste(size_t n)
-{
- selClearOrDel();
- if (n < theCutBuffer.size())
- paste(theCutBuffer[n]);
- //grabSelection();
- selection() = false;
-}
-
-
void LCursor::selHandle(bool sel)
{
//lyxerr << "LCursor::selHandle" << endl;
if (sel == selection())
return;
+
resetAnchor();
selection() = sel;
}
-void LCursor::selClearOrDel()
-{
- //lyxerr << "LCursor::selClearOrDel" << endl;
- if (lyxrc.auto_region_delete)
- selDel();
- else
- selection() = false;
-}
-
-
std::ostream & operator<<(std::ostream & os, LCursor const & cur)
{
for (size_t i = 0, n = cur.size(); i != n; ++i) {
//lyxerr << "LCursor::insert char '" << c << "'" << endl;
BOOST_ASSERT(!empty());
if (inMathed()) {
- selClearOrDel();
+ lyx::cap::selClearOrDel(*this);
insert(new MathCharInset(c));
} else {
text()->insertChar(*this, c);
{
//lyxerr << "LCursor::insert MathAtom: " << endl;
macroModeClose();
- selClearOrDel();
+ lyx::cap::selClearOrDel(*this);
plainInsert(t);
lyxerr << "LCursor::insert MathAtom: cur:\n" << *this << endl;
}
void LCursor::niceInsert(MathAtom const & t)
{
macroModeClose();
- string safe = grabAndEraseSelection();
+ string safe = lyx::cap::grabAndEraseSelection(*this);
plainInsert(t);
// enter the new inset and move the contents of the selection if possible
if (t->isActive()) {
{
macroModeClose();
if (selection())
- eraseSelection();
+ lyx::cap::eraseSelection(*this);
cell().insert(pos(), ar);
pos() += ar.size();
}
autocorrect() = false;
if (selection()) {
- selDel();
+ lyx::cap::selDel(*this);
return true;
}
return true;
if (selection()) {
- selDel();
+ lyx::cap::selDel(*this);
return true;
}
{
//lyxerr << "LCursor::handleNest: " << c << endl;
MathAtom t = a;
- asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
+ asArray(lyx::cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
insert(t);
posLeft();
pushLeft(*nextInset());
}
-void LCursor::adjust(pos_type from, int diff)
-{
- if (pos() > from)
- pos() += diff;
- if (anchor().pos() > from)
- anchor().pos() += diff;
- // just to be on the safe side
- // theoretically unecessary
- normalize();
-}
-
-
bool LCursor::inMacroMode() const
{
if (!pos() != 0)
// avoid invalid nesting when selecting
if (!selection() || positionable(it, anchor_)) {
int xo = 0, yo = 0;
- CursorSlice & cur = it.back();
+ LCursor cur = *this;
+ cur.setCursor(it, false);
cur.inset().getCursorPos(cur, xo, yo);
if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
et.back().pos() = et.back().asMathInset()->cell(et.back().idx()).size();
for (int i = 0; ; ++i) {
int xo, yo;
- CursorSlice & cur = it.back();
+ LCursor cur = *this;
+ cur.setCursor(it, false);
cur.inset().getCursorPos(cur, xo, yo);
double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
// '<=' in order to take the last possible position
}
-CursorSlice LCursor::normalAnchor()
-{
- if (anchor_.size() < depth()) {
- resetAnchor();
- lyxerr << "unusual Anchor size" << endl;
- }
- //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
- // use Anchor on the same level as Cursor
- CursorSlice normal = anchor_[size() - 1];
-#if 0
- if (depth() < anchor_.size() && !(normal < xx())) {
- // anchor is behind cursor -> move anchor behind the inset
- ++normal.pos_;
- }
-#endif
- return normal;
-}
-
-
void LCursor::handleFont(string const & font)
{
lyxerr << "LCursor::handleFont: " << font << endl;
string safe;
if (selection()) {
macroModeClose();
- safe = grabAndEraseSelection();
+ safe = lyx::cap::grabAndEraseSelection(*this);
}
if (lastpos() != 0) {