-// -*- C++ -*-
-/*
- * File: math_macro.C
- * Purpose: Implementation of macro class for mathed
- * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
- * Created: November 1996
- * Description: WYSIWYG math macros
+/**
+ * \file math_macro.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * Dependencies: Mathed
+ * \author Alejandro Aguilar Sierra
+ * \author André Pönitz
*
- * Copyright: 1996, 1997 Alejandro Aguilar Sierra
- *
- * Version: 0.2, Mathed & Lyx project.
- *
- * This code is under the GNU General Public Licence version 2 or later.
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
#include "math_macro.h"
-#include "array.h"
-#include "math_iter.h"
-#include "math_inset.h"
-#include "math_accentinset.h"
-#include "math_deliminset.h"
-#include "math_fracinset.h"
-#include "math_rowst.h"
-#include "support/lstrings.h"
-#include "debug.h"
-#include "mathed/support.h"
-#include "math_macrotemplate.h"
-#include "macro_support.h"
-#include "Painter.h"
-
-using namespace std;
-
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
- return o << int(mtc);
-}
+#include "math_support.h"
+#include "math_extern.h"
+#include "math_mathmlstream.h"
+#include "buffer.h"
+#include "cursor.h"
+#include "debug.h"
+#include "BufferView.h"
+#include "LaTeXFeatures.h"
+#include "frontends/Painter.h"
-MathMacro::MathMacro(MathMacroTemplate const & t)
- : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
- tmplate_(const_cast<MathMacroTemplate *>(&t)),
- args_(t.nargs()),
- idx_(-1)
-{
- array = tmplate_->GetData();
- for (int i = 0; i < nargs(); ++i)
- args_[i].reset(new MathParInset);
-}
+using std::string;
+using std::max;
+using std::auto_ptr;
+using std::endl;
-MathedInset * MathMacro::Clone()
-{
- return new MathMacro(*this);
-}
+MathMacro::MathMacro(string const & name, int numargs)
+ : MathNestInset(numargs), name_(name)
+{}
-void MathMacro::expand()
+auto_ptr<InsetBase> MathMacro::doClone() const
{
- expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
- expanded_->substitute(this);
+ return auto_ptr<InsetBase>(new MathMacro(*this));
}
-
-MathParInset const * MathMacro::arg(int i) const
+string MathMacro::name() const
{
- if (i < 0 || i >= nargs()) {
- lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
- lyx::Assert(0);
- return 0;
- }
-
- return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
-}
-
-MathParInset * MathMacro::arg(int i)
-{
- if (i < 0 || i >= nargs()) {
- lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
- lyx::Assert(0);
- return 0;
- }
-
- return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
+ return name_;
}
-MathMacroTemplate * MathMacro::tmplate() const
+void MathMacro::cursorPos(CursorSlice const & sl, bool boundary, int & x,
+ int & y) const
{
- return const_cast<MathMacroTemplate *>(tmplate_);
+ // We may have 0 arguments, but MathNestInset requires at least one.
+ if (nargs() > 0)
+ MathNestInset::cursorPos(sl, boundary, x, y);
}
-extern bool is_mathcursor_inside(MathParInset *);
-
-
-void MathMacro::Metrics()
+void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
{
-
- if (is_mathcursor_inside(this)) {
-
- tmplate_->Metrics();
- width = tmplate_->Width() + 4;
- ascent = tmplate_->Ascent() + 2;
- descent = tmplate_->Descent() + 2;
-
- width += mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
-
- for (int i = 0; i < nargs(); ++i) {
- MathParInset * p = arg(i);
- p->Metrics();
- if (p->Width() + 30 > width)
- width = p->Width() + 30;
- descent += p->Height() + 10;
+ if (!MacroTable::globalMacros().has(name())) {
+ mathed_string_dim(mi.base.font, "Unknown: " + name(), dim);
+ } else if (editing(mi.base.bv)) {
+ asArray(MacroTable::globalMacros().get(name()).def(), tmpl_);
+ LyXFont font = mi.base.font;
+ augmentFont(font, "lyxtex");
+ tmpl_.metrics(mi, dim);
+ dim.wid += mathed_string_width(font, name()) + 10;
+ int ww = mathed_string_width(font, "#1: ");
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathArray const & c = cell(i);
+ c.metrics(mi);
+ dim.wid = max(dim.wid, c.width() + ww);
+ dim.des += c.height() + 10;
}
} else {
- expand();
- expanded_->Metrics();
- width = expanded_->Width() + 4;
- ascent = expanded_->Ascent() + 2;
- descent = expanded_->Descent() + 2;
-
+ MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
+ expanded_.metrics(mi, dim);
}
-}
-
-
-void MathMacro::draw(Painter & pain, int x, int y)
-{
- LColor::color col;
-
- if (is_mathcursor_inside(this)) {
- int h = y + Descent() - 2;
- for (int i = nargs() - 1; i >= 0; --i) {
- MathParInset * p = arg(i);
- h -= p->Descent() + 5;
- p->draw(pain, x + 30, h);
+ metricsMarkers2(dim);
+ dim_ = dim;
+}
+
+
+void MathMacro::draw(PainterInfo & pi, int x, int y) const
+{
+ if (!MacroTable::globalMacros().has(name())) {
+ drawStrRed(pi, x, y, "Unknown: " + name());
+ } else if (editing(pi.base.bv)) {
+ LyXFont font = pi.base.font;
+ augmentFont(font, "lyxtex");
+ int h = y - dim_.ascent() + 2 + tmpl_.ascent();
+ pi.pain.text(x + 3, h, name(), font);
+ int const w = mathed_string_width(font, name());
+ tmpl_.draw(pi, x + w + 12, h);
+ h += tmpl_.descent();
+ Dimension ldim;
+ mathed_string_dim(font, "#1: ", ldim);
+ for (idx_type i = 0; i < nargs(); ++i) {
+ MathArray const & c = cell(i);
+ h += max(c.ascent(), ldim.asc) + 5;
+ c.draw(pi, x + ldim.wid, h);
char str[] = "#1:";
- str[1] += i;
- drawStr(pain, LM_TC_TEX, size(), x + 1, h, str);
- h -= p->Ascent() + 5;
+ str[1] += static_cast<char>(i);
+ pi.pain.text(x + 3, h, str, font);
+ h += max(c.descent(), ldim.des) + 5;
}
-
- h -= tmplate_->Descent();
- int w = mathed_string_width(LM_TC_TEXTRM, size(), GetName());
- drawStr(pain, LM_TC_TEXTRM, size(), x + 2, h, GetName());
- tmplate_->draw(pain, x + w + 12, h);
-
- col = LColor::red;
} else {
- expanded_->draw(pain, x + 2, y - 1);
- col = LColor::black;
+ expanded_.draw(pi, x, y);
}
-
- int w = Width();
- int a = Ascent();
- int h = Height();
- pain.rectangle(x, y - a, w, h, col);
+ drawMarkers2(pi, x, y);
}
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
{
- if (i >= 0 && 0 < (nargs() - i)) {
- idx_ = i;
- return true;
- } else
- return false;
- idx_ = i;
- return true;
+ // We may have 0 arguments, but MathNestInset requires at least one.
+ if (nargs() > 0)
+ MathNestInset::drawSelection(pi, x, y);
}
-int MathMacro::getArgumentIdx() const
-{
- //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
- return idx_;
-}
-
-
-int MathMacro::getMaxArgumentIdx() const
-{
- return nargs() - 1;
+void MathMacro::validate(LaTeXFeatures & features) const
+{
+ if (name() == "binom" || name() == "mathcircumflex")
+ features.require(name());
}
-
-int MathMacro::nargs() const
-{
- return args_.size();
-}
-
-
-MathedArray & MathMacro::GetData()
-{
- //lyxerr << "MathMacro::GetData: " << *this << endl;
- return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
-}
-
-
-MathedArray const & MathMacro::GetData() const
-{
- //lyxerr << "MathMacro::GetData: " << *this << endl;
- return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
-}
-
-
-int MathMacro::GetColumns() const
+InsetBase * MathMacro::editXY(LCursor & cur, int x, int y)
{
- return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
+ // We may have 0 arguments, but MathNestInset requires at least one.
+ if (nargs() > 0) {
+ // Prevent crash due to cold coordcache
+ // FIXME: This is only a workaround, the call of
+ // MathNestInset::editXY is correct. The correct fix would
+ // ensure that the coordcache of the arguments is valid.
+ if (!editing(&cur.bv())) {
+ edit(cur, true);
+ return this;
+ }
+ return MathNestInset::editXY(cur, x, y);
+ }
+ return this;
}
-void MathMacro::GetXY(int & x, int & y) const
+void MathMacro::maple(MapleStream & os) const
{
- if (idx_ >= 0)
- arg(idx_)->GetXY(x, y);
- else
- MathParInset::GetXY(x, y);
+ updateExpansion();
+ ::maple(expanded_, os);
}
-bool MathMacro::Permit(short f) const
+void MathMacro::mathmlize(MathMLStream & os) const
{
- return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
+ updateExpansion();
+ ::mathmlize(expanded_, os);
}
-void MathMacro::SetFocus(int x, int y)
+void MathMacro::octave(OctaveStream & os) const
{
- idx_ = -1;
- for (int i = 0; i < nargs(); ++i) {
- if (arg(i)->Inside(x, y)) {
- idx_ = i;
- break;
- }
- }
+ updateExpansion();
+ ::octave(expanded_, os);
}
-void MathMacro::setData(MathedArray const & a, int i)
+
+void MathMacro::updateExpansion() const
{
- arg(i)->setData(a);
+ //expanded_.substitute(*this);
}
-void MathMacro::setData(MathedArray const & a)
+void MathMacro::infoize(std::ostream & os) const
{
- if (idx_ >= 0)
- arg(idx_)->setData(a);
- else
- array = a;
+ os << "Macro: " << name();
}
-MathedTextCodes MathMacro::getTCode() const
-{
- return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
- //return LM_TC_INSET;
-}
-
-void MathMacro::dump(ostream & os) const
+void MathMacro::infoize2(std::ostream & os) const
{
- os << "\n macro: '" << this << "'\n";
- os << " name: '" << name << "'\n";
- os << " idx: '" << idx_ << "'\n";
- os << " data: '" << array << "'\n";
- os << " nargs: '" << nargs() << "'\n";
- for (int i = 0; i < nargs(); ++i)
- os << " " << arg(i) << ": " << arg(i)->GetData() << endl;
- os << endl;
-}
+ os << "Macro: " << name();
-void MathMacro::Write(ostream & os, bool fragile)
-{
- os << '\\' << name;
- for (int i = 0; i < nargs(); ++i) {
- os << '{';
- arg(i)->Write(os, fragile);
- os << '}';
- }
- if (nargs() == 0)
- os << ' ';
}