]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_nestinset.C
more IU
[lyx.git] / src / mathed / math_nestinset.C
index 16179f570bf87c9b336ad0b719b14c4585d3584b..36c90752345b9f5985e801c132b0b36cdbcdd3d5 100644 (file)
@@ -1,20 +1,29 @@
-#ifdef __GNUG__
-#pragma implementation
-#endif
+/**
+ * \file math_nestinset.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
 
 #include "math_nestinset.h"
 #include "math_cursor.h"
 #include "math_mathmlstream.h"
-#include "formulabase.h"
+#include "math_parser.h"
 #include "BufferView.h"
+#include "dispatchresult.h"
 #include "debug.h"
+#include "funcrequest.h"
+#include "LColor.h"
 #include "frontends/Painter.h"
-#include "graphics/PreviewLoader.h"
-#include "graphics/Previews.h"
 
 
 MathNestInset::MathNestInset(idx_type nargs)
-       : MathDimInset(), cells_(nargs), lock_(false)
+       : cells_(nargs), lock_(false)
 {}
 
 
@@ -24,27 +33,26 @@ MathInset::idx_type MathNestInset::nargs() const
 }
 
 
-MathXArray & MathNestInset::xcell(idx_type i)
+MathArray & MathNestInset::cell(idx_type i)
 {
        return cells_[i];
 }
 
 
-MathXArray const & MathNestInset::xcell(idx_type i) const
+MathArray const & MathNestInset::cell(idx_type i) const
 {
        return cells_[i];
 }
 
 
-MathArray & MathNestInset::cell(idx_type i)
-{
-       return cells_[i].data();
-}
-
-
-MathArray const & MathNestInset::cell(idx_type i) const
+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;
 }
 
 
@@ -55,26 +63,11 @@ void MathNestInset::substitute(MathMacro const & m)
 }
 
 
-void MathNestInset::metrics(MathMetricsInfo const & mi) const
+void MathNestInset::metrics(MetricsInfo const & mi) const
 {
-       MathMetricsInfo m = mi;
+       MetricsInfo m = mi;
        for (idx_type i = 0; i < nargs(); ++i)
-               xcell(i).metrics(m);
-}
-
-
-void MathNestInset::metricsMarkers(int frame) const
-{
-       dim_.d += frame;
-       dim_.w += 2 * frame;
-}
-
-
-void MathNestInset::metricsMarkers2(int frame) const
-{
-       dim_.a += frame;
-       dim_.d += frame;
-       dim_.w += 2 * frame;
+               cell(i).metrics(m);
 }
 
 
@@ -110,22 +103,22 @@ bool MathNestInset::idxLeft(idx_type & idx, pos_type & pos) const
 }
 
 
-bool MathNestInset::idxFirst(idx_type & i, pos_type & pos) const
+bool MathNestInset::idxFirst(idx_type & idx, pos_type & pos) const
 {
        if (nargs() == 0)
                return false;
-       i = 0;
+       idx = 0;
        pos = 0;
        return true;
 }
 
 
-bool MathNestInset::idxLast(idx_type & i, pos_type & pos) const
+bool MathNestInset::idxLast(idx_type & idx, pos_type & pos) const
 {
        if (nargs() == 0)
                return false;
-       i = nargs() - 1;
-       pos = cell(i).size();
+       idx = nargs() - 1;
+       pos = cell(idx).size();
        return true;
 }
 
@@ -161,8 +154,8 @@ void MathNestInset::dump() const
 }
 
 
-//void MathNestInset::draw(MathPainterInfo & pi, int x, int y) const
-void MathNestInset::draw(MathPainterInfo &, int, int) const
+//void MathNestInset::draw(PainterInfo & pi, int x, int y) const
+void MathNestInset::draw(PainterInfo &, int, int) const
 {
 #if 0
        if (lock_)
@@ -172,30 +165,28 @@ void MathNestInset::draw(MathPainterInfo &, int, int) const
 }
 
 
-void MathNestInset::drawMarkers(MathPainterInfo & pi, int x, int y) const
-{
-       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::drawMarkers2(MathPainterInfo & pi, int x, int y) const
+void MathNestInset::drawSelection(PainterInfo & pi,
+               idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
 {
-       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); 
+       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);
+                       }
+               }
+       }
 }
 
 
@@ -206,17 +197,6 @@ void MathNestInset::validate(LaTeXFeatures & features) const
 }
 
 
-bool MathNestInset::match(MathInset * p) const
-{
-       if (nargs() != p->nargs())
-               return false;
-       for (idx_type i = 0; i < nargs(); ++i)
-               if (!cell(i).match(p->cell(i)))
-                       return false;
-       return true;
-}
-
-
 void MathNestInset::replace(ReplaceData & rep)
 {
        for (idx_type i = 0; i < nargs(); ++i)
@@ -224,7 +204,7 @@ void MathNestInset::replace(ReplaceData & rep)
 }
 
 
-bool MathNestInset::contains(MathArray const & ar)
+bool MathNestInset::contains(MathArray const & ar) const
 {
        for (idx_type i = 0; i < nargs(); ++i)
                if (cell(i).contains(ar))
@@ -261,25 +241,108 @@ MathArray MathNestInset::glue() const
 {
        MathArray ar;
        for (unsigned i = 0; i < nargs(); ++i)
-               ar.push_back(cell(i));
+               ar.append(cell(i));
        return ar;
 }
 
 
-void MathNestInset::notifyCursorLeaves()
+void MathNestInset::write(WriteStream & os) const
 {
-       //lyxerr << "leaving " << *this << "\n";
-       if (!mathcursor || !grfx::Previews::activated())
-               return;
+       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();
+}
+
+
+DispatchResult
+MathNestInset::priv_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 DispatchResult(true, true);
+               }
+
+               case LFUN_PASTESELECTION:
+                       return
+                               dispatch(
+                                       FuncRequest(bv, LFUN_PASTE, bv->getClipboard()), idx, pos);
+
+               case LFUN_MOUSE_PRESS:
+                       if (cmd.button() == mouse_button::button2)
+                               return priv_dispatch(FuncRequest(bv, LFUN_PASTESELECTION), idx, pos);
+                       return DispatchResult(false);
+
+               default:
+                       return MathInset::priv_dispatch(cmd, idx, pos);
+       }
+}
+
+
+void MathNestInset::metricsMarkers(int) const
+{
+       dim_.wid += 2;
+       dim_.asc += 1;
+}
+
+
+void MathNestInset::metricsMarkers2(int) const
+{
+       dim_.wid += 2;
+       dim_.asc += 1;
+       dim_.des += 1;
+}
 
-       InsetFormulaBase * inset = mathcursor->formula();
-       BufferView * bufferview = inset->view();
-       if (!bufferview || !bufferview->buffer())
+void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
+{
+       if (!editing())
                return;
+       int t = x + dim_.width() - 1;
+       int d = y + dim_.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 - 3, d, t, d, LColor::mathframe);
+}
 
-       grfx::Previews & previews = grfx::Previews::get();
-       grfx::PreviewLoader & loader = previews.loader(bufferview->buffer());
 
-       inset->generatePreview(loader);
-       loader.startLoading();
+void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
+{
+       if (!editing())
+               return;
+       drawMarkers(pi, x, y);
+       int t = x + dim_.width() - 1;
+       int a = y - dim_.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 - 3, a, t, a, LColor::mathframe);
 }