]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_nestinset.C
fix #1073
[lyx.git] / src / mathed / math_nestinset.C
index aed935a19fa9d88f99c600eb95d4e661796405c5..44649e2cdcb29defba2526ed0afea3d0e1345744 100644 (file)
@@ -1,73 +1,79 @@
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
 #include "math_nestinset.h"
+#include "math_cursor.h"
+#include "math_mathmlstream.h"
+#include "math_parser.h"
+#include "funcrequest.h"
 #include "debug.h"
+#include "BufferView.h"
+#include "frontends/Painter.h"
 
 
-MathNestInset::MathNestInset(int nargs, string const & name)
-       : MathInset(name), cells_(nargs)
+MathNestInset::MathNestInset(idx_type nargs)
+       : MathDimInset(), cells_(nargs), lock_(false)
 {}
 
 
-int MathNestInset::nargs() const
+MathInset::idx_type MathNestInset::nargs() const
 {
        return cells_.size();
 }
 
 
-MathXArray & MathNestInset::xcell(int i)
+MathArray & MathNestInset::cell(idx_type i)
 {
        return cells_[i];
 }
 
 
-MathXArray const & MathNestInset::xcell(int i) const
+MathArray const & MathNestInset::cell(idx_type i) const
 {
        return cells_[i];
 }
 
 
-MathArray & MathNestInset::cell(int i)
+void MathNestInset::getPos(idx_type idx, pos_type pos, int & x, int & y) const
 {
-       return cells_[i].data_;
+       MathArray const & ar = cell(idx);
+       x = ar.xo() + ar.pos2x(pos);
+       y = ar.yo();
+       // move cursor visually into empty cells ("blue rectangles");
+       if (cell(idx).empty())
+               x += 2;
 }
 
 
-MathArray const & MathNestInset::cell(int i) const
+void MathNestInset::substitute(MathMacro const & m)
 {
-       return cells_[i].data_;
+       for (idx_type i = 0; i < nargs(); ++i)
+               cell(i).substitute(m);
 }
 
 
-void MathNestInset::substitute(MathArray & array, MathMacro const & m) const
+void MathNestInset::metrics(MetricsInfo const & mi) const
 {
-       MathNestInset * p = static_cast<MathNestInset *>(clone());
-       for (int i = 0; i < nargs(); ++i)
-               p->cell(i).substitute(m);
-       array.push_back(p);
+       MetricsInfo m = mi;
+       for (idx_type i = 0; i < nargs(); ++i)
+               cell(i).metrics(m);
 }
 
 
-void MathNestInset::metrics(MathStyles st)
+void MathNestInset::metricsMarkers(int frame) const
 {
-       size_ = st;
-       for (int i = 0; i < nargs(); ++i)
-               xcell(i).metrics(st);
+       dim_.d += frame;
+       dim_.w += 2 * frame;
 }
 
 
-void MathNestInset::draw(Painter & pain, int x, int y)
+void MathNestInset::metricsMarkers2(int frame) const
 {
-       xo(x);
-       yo(y);
-       for (int i = 0; i < nargs(); ++i)
-               xcell(i).draw(pain, x + xcell(i).xo(), y + xcell(i).yo());
+       dim_.a += frame;
+       dim_.d += frame;
+       dim_.w += 2 * frame;
 }
 
 
-bool MathNestInset::idxNext(int & idx, int & pos) const
+bool MathNestInset::idxNext(idx_type & idx, pos_type & pos) const
 {
        if (idx + 1 >= nargs())
                return false;
@@ -77,13 +83,13 @@ bool MathNestInset::idxNext(int & idx, int & pos) const
 }
 
 
-bool MathNestInset::idxRight(int & idx, int & pos) const
+bool MathNestInset::idxRight(idx_type & idx, pos_type & pos) const
 {
        return idxNext(idx, pos);
 }
 
 
-bool MathNestInset::idxPrev(int & idx, int & pos) const
+bool MathNestInset::idxPrev(idx_type & idx, pos_type & pos) const
 {
        if (idx == 0)
                return false;
@@ -93,13 +99,13 @@ bool MathNestInset::idxPrev(int & idx, int & pos) const
 }
 
 
-bool MathNestInset::idxLeft(int & idx, int & pos) const
+bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
 {
        return idxPrev(idx, pos);
 }
 
 
-bool MathNestInset::idxFirst(int & i, int & pos) const
+bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
 {
        if (nargs() == 0)
                return false;
@@ -109,7 +115,7 @@ bool MathNestInset::idxFirst(int & i, int & pos) const
 }
 
 
-bool MathNestInset::idxLast(int & i, int & pos) const
+bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
 {
        if (nargs() == 0)
                return false;
@@ -119,7 +125,7 @@ bool MathNestInset::idxLast(int & i, int & pos) const
 }
 
 
-bool MathNestInset::idxHome(int & /* idx */, int & pos) const
+bool MathNestInset::idxHome(idx_type & /* idx */, pos_type & pos) const
 {
        if (pos == 0)
                return false;
@@ -128,47 +134,214 @@ bool MathNestInset::idxHome(int & /* idx */, int & pos) const
 }
 
 
-bool MathNestInset::idxEnd(int & idx, int & pos) const
+bool MathNestInset::idxEnd(idx_type & idx, pos_type & pos) const
 {
-       if (pos == cell(idx).size())
+       pos_type n = cell(idx).size();
+       if (pos == n)
                return false;
-
-       pos = cell(idx).size();
+       pos = n;
        return true;
 }
 
 
 void MathNestInset::dump() const
 {
-       lyxerr << "---------------------------------------------\n";
-       write(lyxerr, false);
-       lyxerr << "\n";
-       for (int i = 0; i < nargs(); ++i)
-               lyxerr << cell(i) << "\n";
-       lyxerr << "---------------------------------------------\n";
+       WriteStream os(lyxerr);
+       os << "---------------------------------------------\n";
+       write(os);
+       os << "\n";
+       for (idx_type i = 0; i < nargs(); ++i)
+               os << cell(i) << "\n";
+       os << "---------------------------------------------\n";
+}
+
+
+//void MathNestInset::draw(PainterInfo & pi, int x, int y) const
+void MathNestInset::draw(PainterInfo &, int, int) const
+{
+#if 0
+       if (lock_)
+               pi.pain.fillRectangle(x, y - ascent(), width(), height(),
+                                       LColor::mathlockbg);
+#endif
+}
+
+
+void MathNestInset::drawSelection(PainterInfo & pi,
+               idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
+{
+       if (idx1 == idx2) {
+               MathArray const & c = cell(idx1);
+               int x1 = c.xo() + c.pos2x(pos1);
+               int y1 = c.yo() - c.ascent();
+               int x2 = c.xo() + c.pos2x(pos2);
+               int y2 = c.yo() + c.descent();
+               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
+       } else {
+               for (idx_type i = 0; i < nargs(); ++i) {
+                       if (idxBetween(i, idx1, idx2)) {
+                               MathArray const & c = cell(i);
+                               int x1 = c.xo();
+                               int y1 = c.yo() - c.ascent();
+                               int x2 = c.xo() + c.width();
+                               int y2 = c.yo() + c.descent();
+                               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
+                       }
+               }
+       }
 }
 
 
-void MathNestInset::push_back(unsigned char ch, MathTextCodes fcode)
+void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
 {
-       if (nargs())
-               cells_.back().data_.push_back(ch, fcode);
-       else
-               lyxerr << "can't push without a cell\n";
+       if (!editing())
+               return;
+       int t = x + width() - 1;
+       int d = y + descent();
+       pi.pain.line(x, d - 3, x, d, LColor::mathframe);
+       pi.pain.line(t, d - 3, t, d, LColor::mathframe);
+       pi.pain.line(x, d, x + 3, d, LColor::mathframe);
+       pi.pain.line(t - 2, d, t, d, LColor::mathframe);
 }
 
 
-void MathNestInset::push_back(MathInset * p)
+void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
 {
-       if (nargs())
-               cells_.back().data_.push_back(p);
-       else
-               lyxerr << "can't push without a cell\n";
+       if (!editing())
+               return;
+       drawMarkers(pi, x, y);
+       int t = x + width() - 1;
+       int a = y - ascent();
+       pi.pain.line(x, a + 3, x, a, LColor::mathframe);
+       pi.pain.line(t, a + 3, t, a, LColor::mathframe);
+       pi.pain.line(x, a, x + 3, a, LColor::mathframe);
+       pi.pain.line(t - 2, a, t, a, LColor::mathframe);
 }
 
 
 void MathNestInset::validate(LaTeXFeatures & features) const
 {
-       for (int i = 0; i < nargs(); ++i)
+       for (idx_type i = 0; i < nargs(); ++i)
                cell(i).validate(features);
 }
+
+
+bool MathNestInset::match(MathAtom const & at) const
+{
+       if (nargs() != at->nargs())
+               return false;
+       for (idx_type i = 0; i < nargs(); ++i)
+               if (!cell(i).match(at->cell(i)))
+                       return false;
+       return true;
+}
+
+
+void MathNestInset::replace(ReplaceData & rep)
+{
+       for (idx_type i = 0; i < nargs(); ++i)
+               cell(i).replace(rep);
+}
+
+
+bool MathNestInset::contains(MathArray const & ar) const
+{
+       for (idx_type i = 0; i < nargs(); ++i)
+               if (cell(i).contains(ar))
+                       return true;
+       return false;
+}
+
+
+bool MathNestInset::editing() const
+{
+       return mathcursor && mathcursor->isInside(this);
+}
+
+
+bool MathNestInset::lock() const
+{
+       return lock_;
+}
+
+
+void MathNestInset::lock(bool l)
+{
+       lock_ = l;
+}
+
+
+bool MathNestInset::isActive() const
+{
+       return nargs() > 0;
+}
+
+
+MathArray MathNestInset::glue() const
+{
+       MathArray ar;
+       for (unsigned i = 0; i < nargs(); ++i)
+               ar.append(cell(i));
+       return ar;
+}
+
+
+void MathNestInset::write(WriteStream & os) const
+{
+       os << '\\' << name().c_str();
+       for (unsigned i = 0; i < nargs(); ++i)
+               os << '{' << cell(i) << '}';
+       if (nargs() == 0)
+               os.pendingSpace(true);
+       if (lock_ && !os.latex()) {
+               os << "\\lyxlock";
+               os.pendingSpace(true);
+       }
+}
+
+
+void MathNestInset::normalize(NormalStream & os) const
+{
+       os << '[' << name().c_str();
+       for (unsigned i = 0; i < nargs(); ++i)
+               os << ' ' << cell(i);
+       os << ']';
+}
+
+
+void MathNestInset::notifyCursorLeaves(idx_type idx)
+{
+       cell(idx).notifyCursorLeaves();
+}
+
+
+dispatch_result MathNestInset::dispatch
+       (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
+{
+       BufferView * bv = cmd.view();
+
+       switch (cmd.action) {
+
+               case LFUN_PASTE: {
+                       MathArray ar;
+                       mathed_parse_cell(ar, cmd.argument);
+                       cell(idx).insert(pos, ar);
+                       pos += ar.size();
+                       return DISPATCHED;
+               }
+
+               case LFUN_PASTESELECTION:
+                       return
+                               dispatch(
+                                       FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
+
+               case LFUN_MOUSE_PRESS:
+                       if (cmd.button() == mouse_button::button2)
+                               return dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
+                       return UNDISPATCHED;
+
+               default:
+                       return MathInset::dispatch(cmd, idx, pos);
+       }
+       return UNDISPATCHED;
+}