]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_macro.C
index 632c3d76763742c3da7d4da66cc00912bf5ce817..096acf8a58bf34f96723158a2eb29fe499d5ddd8 100644 (file)
-// -*- 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>
-#include FORMS_H_LOCATION
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
-#include "LString.h"
 #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 "math_support.h"
+#include "math_extern.h"
+#include "math_mathmlstream.h"
+
+#include "buffer.h"
+#include "cursor.h"
 #include "debug.h"
-#include "mathed/support.h"
-#include "math_macrotemplate.h"
-#include "macro_support.h"
+#include "BufferView.h"
+#include "LaTeXFeatures.h"
+#include "frontends/Painter.h"
 
-using std::ostream;
+using std::string;
+using std::max;
+using std::auto_ptr;
 using std::endl;
 
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
-       return o << int(mtc);
-}
-
-
-//extern int mathed_string_width(short type, int style, string const & s);
-//extern int mathed_string_height(short, int, string const &, int &, int &);
-
-MathMacro::MathMacro(MathMacroTemplate * t): 
-       MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
-{
-       nargs = tmplate->getNoArgs();
-       tcode = tmplate->getTCode();
-       args_.resize(nargs);
-       for (int i = 0; i < nargs; ++i) {
-               args_[i].row = 0;
-       }
-       idx = 0;
-       SetName(tmplate->GetName());
-}
-
 
-MathMacro::MathMacro(MathMacro * m): 
-       MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
-{
-       tmplate = m->tmplate;
-       nargs = tmplate->getNoArgs();
-       tcode = tmplate->getTCode();
-       args_.resize(nargs);
-       idx = 0;
-       SetName(tmplate->GetName());
-       for (int i = 0; i < tmplate->nargs; ++i) {
-               m->setArgumentIdx(i);
-               MathedIter it(m->GetData());
-               args_[i].row   = m->args_[i].row;
-               args_[i].array = it.Copy();
-       }
-}
-
-MathMacro::~MathMacro()
-{
-       for (idx = 0; idx < nargs; ++idx) {
-               MathedIter it(args_[idx].array);
-               it.Clear();
-               delete args_[idx].row;
-       }
-}
+MathMacro::MathMacro(string const & name, int numargs)
+       : MathNestInset(numargs), name_(name)
+{}
 
 
-MathedInset * MathMacro::Clone()
+auto_ptr<InsetBase> MathMacro::doClone() const
 {
-       return new MathMacro(this);
+       return auto_ptr<InsetBase>(new MathMacro(*this));
 }
 
 
-void MathMacro::Metrics()
+string MathMacro::name() const
 {
-       if (nargs > 0)
-               tmplate->update(this);
-       tmplate->SetStyle(size);
-       tmplate->Metrics();
-       width = tmplate->Width();
-       ascent = tmplate->Ascent();
-       descent = tmplate->Descent();
+       return name_;
 }
 
 
-void MathMacro::draw(Painter & pain, int x, int y)
+void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       xo = x;  yo = y;
-       Metrics();
-       tmplate->update(this);
-       tmplate->SetStyle(size);
-       tmplate->draw(pain, x, y);
-       for (int i = 0; i < nargs; ++i) {
-               tmplate->GetMacroXY(i, args_[i].x, args_[i].y);
+       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 {
+               MacroTable::globalMacros().get(name()).expand(cells_, expanded_);
+               expanded_.metrics(mi, dim);
        }
+       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] += static_cast<char>(i);
+                       pi.pain.text(x + 3, h, str, font);
+                       h += max(c.descent(), ldim.des) + 5;
+               }
+       } else {
+               expanded_.draw(pi, x, y);
+       }
+       drawMarkers2(pi, x, y);
 }
 
 
-bool MathMacro::setArgumentIdx(int i)
-{
-       if (i >= 0 && i < nargs) {
-               idx = i;
-               return true;
-       } else
-               return false;
-}
-
-
-int MathMacro::getArgumentIdx() const 
-{ 
-       return idx; 
-}
-
-
-int MathMacro::getMaxArgumentIdx() const 
-{ 
-       return nargs - 1; 
-} 
-
-
-MathedArray * MathMacro::GetData() 
-{ 
-       return args_[idx].array; 
-} 
-
-
-int MathMacro::GetColumns() const
+void MathMacro::validate(LaTeXFeatures & features) const
 {
-       return tmplate->getMacroPar(idx)->GetColumns();
+       if (name() == "binom" || name() == "mathcircumflex")
+               features.require(name());
 }
 
 
-void MathMacro::GetXY(int & x, int & y) const
+void MathMacro::maple(MapleStream & os) const
 {
-       x = args_[idx].x;  y = args_[idx].y;
+       updateExpansion();
+       ::maple(expanded_, os);
 }
 
 
-bool MathMacro::Permit(short f) const
+void MathMacro::mathmlize(MathMLStream & os) const
 {
-       return (nargs > 0) ?
-               tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
+       updateExpansion();
+       ::mathmlize(expanded_, os);
 }
 
 
-void MathMacro::SetFocus(int x, int y)
+void MathMacro::octave(OctaveStream & os) const
 {
-       tmplate->update(this);
-       tmplate->SetMacroFocus(idx, x, y);
+       updateExpansion();
+       ::octave(expanded_, os);
 }
 
 
-void MathMacro::SetData(MathedArray * a)
+void MathMacro::updateExpansion() const
 {
-       args_[idx].array = a;
+       //expanded_.substitute(*this);
 }
 
 
-
-
-MathedRowSt * MathMacro::getRowSt() const
+void MathMacro::infoize(std::ostream & os) const
 {
-       return args_[idx].row;
+       os << "Macro: " << name();
 }
 
 
-MathedTextCodes MathMacro::getTCode() const
+void MathMacro::infoize2(std::ostream & os) const
 {
-       return tcode;
-}
-       
+       os << "Macro: " << name();
 
-void MathMacro::Write(ostream & os, bool fragile)
-{
-       if (tmplate->flags & MMF_Exp) {
-               lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
-                                     << ' ' << MMF_Exp << endl; 
-               tmplate->update(this);
-               tmplate->Write(os, fragile);
-       } else {
-               if (tmplate->flags & MMF_Env) {
-                       os << "\\begin{"
-                          << name
-                          << "} ";
-               } else {
-                       os << '\\' << name;
-               }
-//     if (options) { 
-//       file += '[';
-//       file += options;
-//       file += ']';
-//      }
-       
-               if (!(tmplate->flags & MMF_Env) && nargs > 0) 
-                       os << '{';
-       
-               for (int i = 0; i < nargs; ++i) {
-                       array = args_[i].array;
-                       MathParInset::Write(os, fragile);
-                       if (i < nargs - 1)  
-                               os << "}{";
-               }   
-               if (tmplate->flags & MMF_Env) {
-                       os << "\\end{"
-                          << name
-                          << '}';
-               } else {
-                       if (nargs > 0) 
-                               os << '}';
-                       else
-                               os << ' ';
-               }
-       }
 }