-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include <config.h>
#include "math_scriptinset.h"
-#include "support/LOstream.h"
+#include "math_support.h"
+#include "math_symbolinset.h"
+#include "math_mathmlstream.h"
+#include "funcrequest.h"
+#include "support/LAssert.h"
+#include "debug.h"
+
+using namespace lyx::support;
+
+using std::max;
+using std::auto_ptr;
+using std::endl;
MathScriptInset::MathScriptInset()
- : MathInset(2), up_(false), down_(false), limits_(0), symbol_(0)
-{}
+ : MathNestInset(3), limits_(0)
+{
+ script_[0] = false;
+ script_[1] = false;
+}
+
+MathScriptInset::MathScriptInset(bool up)
+ : MathNestInset(3), limits_(0)
+{
+ script_[0] = !up;
+ script_[1] = up;
+}
-MathScriptInset::MathScriptInset(bool up, bool down, MathInset * symbol)
- : MathInset(2), up_(up), down_(down), limits_(0), symbol_(symbol)
-{}
+MathScriptInset::MathScriptInset(MathAtom const & at, bool up)
+ : MathNestInset(3), limits_(0)
+{
+ script_[0] = !up;
+ script_[1] = up;
+ cell(2).push_back(at);
+}
-MathScriptInset::MathScriptInset(MathScriptInset const & p)
- : MathInset(p), up_(p.up_), down_(p.down_),
- limits_(p.limits_), symbol_(p.symbol_ ? p.symbol_->clone() : 0)
-{}
-MathScriptInset::~MathScriptInset()
+auto_ptr<InsetBase> MathScriptInset::clone() const
{
- delete symbol_;
+ return auto_ptr<InsetBase>(new MathScriptInset(*this));
}
-MathInset * MathScriptInset::clone() const
-{
- return new MathScriptInset(*this);
+MathScriptInset const * MathScriptInset::asScriptInset() const
+{
+ return this;
}
-bool MathScriptInset::up() const
+MathScriptInset * MathScriptInset::asScriptInset()
{
- return up_;
+ return this;
}
-bool MathScriptInset::down() const
+bool MathScriptInset::idxFirst(idx_type & idx, pos_type & pos) const
{
- return down_;
+ idx = 2;
+ pos = 0;
+ return true;
}
-void MathScriptInset::up(bool b)
+bool MathScriptInset::idxLast(idx_type & idx, pos_type & pos) const
{
- up_ = b;
+ idx = 2;
+ pos = nuc().size();
+ return true;
}
-void MathScriptInset::down(bool b)
+MathArray const & MathScriptInset::down() const
{
- down_ = b;
+ return cell(0);
}
-bool MathScriptInset::idxRight(int &, int &) const
+MathArray & MathScriptInset::down()
{
- return false;
+ return cell(0);
}
-bool MathScriptInset::idxLeft(int &, int &) const
+MathArray const & MathScriptInset::up() const
{
- return false;
+ return cell(1);
}
-bool MathScriptInset::idxUp(int & idx, int & pos) const
+MathArray & MathScriptInset::up()
{
- if (idx == 0 || !up())
- return false;
- idx = 0;
- pos = 0;
- return true;
+ return cell(1);
}
-bool MathScriptInset::idxDown(int & idx, int & pos) const
+
+void MathScriptInset::ensure(bool up)
{
- if (idx == 1 || !down())
- return false;
- idx = 1;
- pos = 0;
- return true;
+ script_[up] = true;
}
-bool MathScriptInset::idxFirst(int & idx, int & pos) const
+
+MathArray const & MathScriptInset::nuc() const
{
- idx = up() ? 0 : 1;
- pos = 0;
- return true;
+ return cell(2);
}
-bool MathScriptInset::idxLast(int & idx, int & pos) const
+
+MathArray & MathScriptInset::nuc()
{
- idx = down() ? 1 : 0;
- pos = cell(idx).size();
- return true;
+ return cell(2);
}
-bool MathScriptInset::idxFirstUp(int & idx, int & pos) const
+int MathScriptInset::dy0() const
{
- if (!up())
- return false;
- idx = 0;
- pos = 0;
- return true;
+ int nd = ndes();
+ if (!hasDown())
+ return nd;
+ int des = down().ascent();
+ if (hasLimits())
+ des += nd + 2;
+ else
+ des = max(des, nd);
+ return des;
}
-bool MathScriptInset::idxFirstDown(int & idx, int & pos) const
+int MathScriptInset::dy1() const
{
- if (!down())
- return false;
- idx = 1;
- pos = 0;
- return true;
+ int na = nasc();
+ if (!hasUp())
+ return na;
+ int asc = up().descent();
+ if (hasLimits())
+ asc += na + 2;
+ else
+ asc = max(asc, na);
+ asc = max(asc, 5);
+ return asc;
}
-bool MathScriptInset::idxLastUp(int & idx, int & pos) const
+int MathScriptInset::dx0() const
{
- if (!up())
- return false;
- idx = 0;
- pos = cell(idx).size();
- return true;
+ Assert(hasDown());
+ return hasLimits() ? (dim_.wid - down().width()) / 2 : nwid();
}
-bool MathScriptInset::idxLastDown(int & idx, int & pos) const
+int MathScriptInset::dx1() const
{
- if (!down())
+ Assert(hasUp());
+ return hasLimits() ? (dim_.wid - up().width()) / 2 : nwid();
+}
+
+
+int MathScriptInset::dxx() const
+{
+ return hasLimits() ? (dim_.wid - nwid()) / 2 : 0;
+}
+
+
+int MathScriptInset::nwid() const
+{
+ return nuc().size() ? nuc().width() : 2;
+}
+
+
+int MathScriptInset::nasc() const
+{
+ return nuc().size() ? nuc().ascent() : 5;
+}
+
+
+int MathScriptInset::ndes() const
+{
+ return nuc().size() ? nuc().descent() : 0;
+}
+
+
+void MathScriptInset::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+ cell(2).metrics(mi);
+ ScriptChanger dummy(mi.base);
+ cell(0).metrics(mi);
+ cell(1).metrics(mi);
+ dim_.wid = 0;
+ if (hasLimits()) {
+ dim_.wid = nwid();
+ if (hasUp())
+ dim_.wid = max(dim_.wid, up().width());
+ if (hasDown())
+ dim_.wid = max(dim_.wid, down().width());
+ } else {
+ if (hasUp())
+ dim_.wid = max(dim_.wid, up().width());
+ if (hasDown())
+ dim_.wid = max(dim_.wid, down().width());
+ dim_.wid += nwid();
+ }
+ dim_.asc = dy1() + (hasUp() ? up().ascent() : 0);
+ dim_.des = dy0() + (hasDown() ? down().descent() : 0);
+ metricsMarkers();
+ dim = dim_;
+}
+
+
+void MathScriptInset::draw(PainterInfo & pi, int x, int y) const
+{
+ if (nuc().size())
+ nuc().draw(pi, x + dxx(), y);
+ else {
+ nuc().setXY(x + dxx(), y);
+ if (editing())
+ drawStr(pi, pi.base.font, x + dxx(), y, ".");
+ }
+ ScriptChanger dummy(pi.base);
+ if (hasUp())
+ up().draw(pi, x + dx1(), y - dy1());
+ if (hasDown())
+ down().draw(pi, x + dx0(), y + dy0());
+ drawMarkers(pi, x, y);
+}
+
+
+void MathScriptInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
+{
+ if (hasUp())
+ up().metricsT(mi, dim);
+ if (hasDown())
+ down().metricsT(mi, dim);
+ nuc().metricsT(mi, dim);
+}
+
+
+void MathScriptInset::drawT(TextPainter & pain, int x, int y) const
+{
+ if (nuc().size())
+ nuc().drawT(pain, x + dxx(), y);
+ if (hasUp())
+ up().drawT(pain, x + dx1(), y - dy1());
+ if (hasDown())
+ down().drawT(pain, x + dx0(), y + dy0());
+}
+
+
+
+bool MathScriptInset::hasLimits() const
+{
+ // obvious cases
+ if (limits_ == 1)
+ return true;
+ if (limits_ == -1)
return false;
- idx = 1;
- pos = cell(idx).size();
+
+ // we can only display limits if the nucleus wants some
+ if (!nuc().size())
+ return false;
+ if (!nuc().back()->isScriptable())
+ return false;
+
+ // per default \int has limits beside the \int even in displayed formulas
+ if (nuc().back()->asSymbolInset())
+ if (nuc().back()->asSymbolInset()->name().find("int") != string::npos)
+ return false;
+
+ // assume "real" limits for everything else
return true;
}
-void MathScriptInset::Write(std::ostream & os, bool fragile) const
+void MathScriptInset::removeScript(bool up)
{
- if (symbol_) {
- symbol_->Write(os, fragile);
- if (limits())
- os << (limits() == 1 ? "\\limits" : "\\nolimits");
+ cell(up).clear();
+ script_[up] = false;
+}
+
+
+bool MathScriptInset::has(bool up) const
+{
+ return script_[up];
+}
+
+
+bool MathScriptInset::hasUp() const
+{
+ return script_[1];
+}
+
+
+bool MathScriptInset::hasDown() const
+{
+ return script_[0];
+}
+
+
+bool MathScriptInset::idxRight(idx_type &, pos_type &) const
+{
+ return false;
+}
+
+
+bool MathScriptInset::idxLeft(idx_type &, pos_type &) const
+{
+ return false;
+}
+
+
+bool MathScriptInset::idxUpDown(idx_type & idx, pos_type & pos, bool up,
+ int) const
+{
+ if (idx == 1) {
+ // if we are 'up' we can't go further up
+ if (up)
+ return false;
+ // otherwise go to last base position
+ idx = 2;
+ pos = cell(2).size();
}
- if (up()) {
- os << "^{";
- cell(0).Write(os, fragile);
- os << "}";
+
+ else if (idx == 0) {
+ // if we are 'down' we can't go further down
+ if (!up)
+ return false;
+ idx = 2;
+ pos = cell(2).size();
}
- if (down()) {
- os << "_{";
- cell(1).Write(os, fragile);
- os << "}";
+
+ else {
+ // in nucleus
+ // don't go up/down if there is no cell.
+ if (!has(up))
+ return false;
+ // go up/down only if in the last position
+ // or in the first position of something with displayed limits
+ if (pos == cell(2).size() || (pos == 0 && hasLimits())) {
+ idx = up;
+ pos = 0;
+ return true;
+ }
+ return false;
+ }
+ return true;
+}
+
+
+void MathScriptInset::write(WriteStream & os) const
+{
+ if (nuc().size()) {
+ os << nuc();
+ //if (nuc().back()->takesLimits()) {
+ if (limits_ == -1)
+ os << "\\nolimits ";
+ if (limits_ == 1)
+ os << "\\limits ";
+ //}
+ } else {
+ if (os.firstitem())
+ lyxerr[Debug::MATHED] << "suppressing {} when writing"
+ << endl;
+ else
+ os << "{}";
}
- os << " ";
+
+ if (hasDown() && down().size())
+ os << "_{" << down() << '}';
+
+ if (hasUp() && up().size())
+ os << "^{" << up() << '}';
+
+ if (lock_ && !os.latex())
+ os << "\\lyxlock ";
}
-void MathScriptInset::idxDelete(int & idx, bool & popit, bool & deleteit)
+void MathScriptInset::normalize(NormalStream & os) const
{
- if (idx == 0)
- up(false);
+ bool d = hasDown() && down().size();
+ bool u = hasUp() && up().size();
+
+ if (u && d)
+ os << "[subsup ";
+ else if (u)
+ os << "[sup ";
+ else if (d)
+ os << "[sub ";
+
+ if (nuc().size())
+ os << nuc() << ' ';
else
- down(false);
- popit = true;
- deleteit = !(up() || down());
+ os << "[par]";
+
+ if (u && d)
+ os << down() << ' ' << up() << ']';
+ else if (d)
+ os << down() << ']';
+ else if (u)
+ os << up() << ']';
}
-int MathScriptInset::limits() const
-{
- return limits_;
+void MathScriptInset::maple(MapleStream & os) const
+{
+ if (nuc().size())
+ os << nuc();
+ if (hasDown() && down().size())
+ os << '[' << down() << ']';
+ if (hasUp() && up().size())
+ os << "^(" << up() << ')';
+}
+
+
+void MathScriptInset::mathematica(MathematicaStream & os) const
+{
+ bool d = hasDown() && down().size();
+ bool u = hasUp() && up().size();
+
+ if (nuc().size()) {
+ if (d)
+ os << "Subscript[" << nuc();
+ else
+ os << nuc();
+ }
+
+ if (u)
+ os << "^(" << up() << ')';
+
+ if (nuc().size())
+ if (d)
+ os << ',' << down() << ']';
}
-void MathScriptInset::limits(int limits)
-{
- limits_ = limits;
+void MathScriptInset::mathmlize( MathMLStream & os) const
+{
+ bool d = hasDown() && down().size();
+ bool u = hasUp() && up().size();
+
+ if (u && d)
+ os << MTag("msubsup");
+ else if (u)
+ os << MTag("msup");
+ else if (d)
+ os << MTag("msub");
+
+ if (nuc().size())
+ os << nuc();
+ else
+ os << "<mrow/>";
+
+ if (u && d)
+ os << down() << up() << ETag("msubsup");
+ else if (u)
+ os << up() << ETag("msup");
+ else if (d)
+ os << down() << ETag("msub");
}
-bool MathScriptInset::hasLimits() const
+void MathScriptInset::octave(OctaveStream & os) const
{
- return
- symbol_ && (limits_ == 1 || (limits_ == 0 && size() == LM_ST_DISPLAY));
+ if (nuc().size())
+ os << nuc();
+ if (hasDown() && down().size())
+ os << '[' << down() << ']';
+ if (hasUp() && up().size())
+ os << "^(" << up() << ')';
}
-void MathScriptInset::WriteNormal(std::ostream & os) const
+void MathScriptInset::infoize(std::ostream & os) const
{
- if (limits() && symbol_)
- os << "[" << (limits() ? "limits" : "nolimits") << "]";
- if (up()) {
- os << "[superscript ";
- cell(0).WriteNormal(os);
- os << "] ";
- }
- if (down()) {
- os << "[subscript ";
- cell(1).WriteNormal(os);
- os << "] ";
- }
+ os << "Scripts";
}
-void MathScriptInset::Metrics(MathStyles st)
+void MathScriptInset::infoize2(std::ostream & os) const
{
- size_ = st;
- MathStyles tt = smallerStyleScript(st);
-
- xcell(0).Metrics(tt);
- xcell(1).Metrics(tt);
+ if (limits_)
+ os << (limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
+}
- width_ = std::max(xcell(0).width(), xcell(1).width());
- if (hasLimits()) {
- symbol_->Metrics(st);
- int wid = symbol_->width();
- ascent_ = symbol_->ascent();
- descent_ = symbol_->descent();
- width_ = std::max(width_, wid);
- if (up()) {
- ascent_ += xcell(0).height() + 2;
- dy0_ = - (ascent_ - xcell(0).ascent());
- }
- if (down()) {
- descent_ += xcell(1).height() + 2;
- dy1_ = descent_ - xcell(1).descent();
- }
- dxx_ = (width_ - wid) / 2;
- dx0_ = (width_ - xcell(0).width()) / 2;
- dx1_ = (width_ - xcell(1).width()) / 2;
- } else {
- int asc;
- int des;
- int wid = 0;
- mathed_char_height(LM_TC_VAR, st, 'I', asc, des);
- if (symbol_) {
- symbol_->Metrics(st);
- wid = symbol_->width();
- asc = symbol_->ascent();
- des = symbol_->descent();
- }
- ascent_ = up() ? xcell(0).height() + asc : 0;
- descent_ = down() ? xcell(1).height() + des : 0;
- width_ += wid;
- dy0_ = - asc - xcell(0).descent();
- dy1_ = des + xcell(1).ascent();
- dx0_ = wid;
- dx1_ = wid;
- dxx_ = 0;
+void MathScriptInset::notifyCursorLeaves(idx_type idx)
+{
+ MathNestInset::notifyCursorLeaves(idx);
+
+ // remove empty scripts if possible
+ if (idx != 2 && script_[idx] && cell(idx).empty()) {
+ cell(idx).clear();
+ script_[idx] = false;
}
}
-void MathScriptInset::draw(Painter & pain, int x, int y)
-{
- xo(x);
- yo(y);
+dispatch_result MathScriptInset::dispatch
+ (FuncRequest const & cmd, idx_type & idx, pos_type & pos)
+{
+ if (cmd.action == LFUN_MATH_LIMITS) {
+ if (!cmd.argument.empty()) {
+ if (cmd.argument == "limits")
+ limits_ = 1;
+ else if (cmd.argument == "nolimits")
+ limits_ = -1;
+ else
+ limits_ = 0;
+ } else if (limits_ == 0)
+ limits_ = (hasLimits()) ? -1 : 1;
+ else
+ limits_ = 0;
+ return DISPATCHED;
+ }
- if (symbol_)
- symbol_->draw(pain, x + dxx_, y);
- if (up())
- xcell(0).draw(pain, x + dx0_, y + dy0_);
- if (down())
- xcell(1).draw(pain, x + dx1_, y + dy1_);
+ return MathNestInset::dispatch(cmd, idx, pos);
}