]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / math_macro.C
index dadbaef66703e14713794a52eeba1bd324275eee..5085e783bf864957aff6bea102d2e7df514b0865 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /*
  *  File:        math_macro.C
  *  Purpose:     Implementation of macro class for mathed 
  *  Created:     November 1996
  *  Description: WYSIWYG math macros
  *
- *  Dependencies: Mathed
+ *  Dependencies: Math
  *
- *  Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
+ *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
  *
- *  Version: 0.2, Mathed & Lyx project.
+ *  Version: 0.2, Math & Lyx project.
  *
  *  This code is under the GNU General Public Licence version 2 or later.
  */
 
-#include <config.h>
-#include FORMS_H_LOCATION
-
 #ifdef __GNUG__
-#pragma implementation "math_macro.h"
-#pragma implementation "math_defs.h"
+#pragma implementation
 #endif
 
-#include "LString.h"
 #include "math_macro.h"
-#include "math_iter.h"
-#include "math_inset.h"
+#include "array.h"
 #include "support/lstrings.h"
+#include "support/LAssert.h"
 #include "debug.h"
+#include "mathed/support.h"
+#include "mathed/math_cursor.h"
+#include "math_macrotable.h"
+#include "math_macrotemplate.h"
+#include "Painter.h"
+#include "LaTeXFeatures.h"
 
+using std::endl;
 
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
-       return o << int(mtc);
-}
-
-enum MathedMacroFlag {
-    MMF_Env= 1,
-    MMF_Exp= 2,
-    MMF_Edit= 4
-};
-
-ostream & operator<<(ostream & o, MathedMacroFlag mmf)
-{
-       return o << int(mmf);
-}
-
-extern GC mathGC, mathFrameGC, latexGC;
-extern int mathed_string_width(short type, int style, byte const* s, int ls);
-extern int mathed_string_height(short, int, byte const*, int, int&, int&);
-
-
-MathMacro::MathMacro(MathMacroTemplate* t): 
-    MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
-{
-    nargs = tmplate->getNoArgs();
-    tcode = tmplate->getTCode();
-    args = new MacroArgumentBase[nargs];
-    for (int i= 0; i<nargs; i++) {
-//     if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
-//       args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
-//     else 
-         args[i].row = 0;
-/*     int k = tmplate->getMacroPar(i)->GetColumns();
-       if (k>0) {
-           args[i].array = new LyxArrayBase;
-           for (int j= 0; j<k-1; j++) args[i].array->Insert(j, LM_TC_TAB);
-       }*/
-    }
-    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 = new MacroArgumentBase[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(MathMacroTemplate const & t)
+       : MathInset(t.numargs(), t.name(), LM_OT_MACRO), tmplate_(&t)
+{}
 
-MathMacro::~MathMacro()
-{
-    for (idx= 0; idx<nargs; idx++) {
-       MathedIter it(args[idx].array);
-       it. Clear();
-       delete args[idx].row;
-    }
-    delete[] args;
-}
 
+MathMacro::MathMacro(MathMacro const & t)
+       : MathInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
+{}
 
-MathedInset *MathMacro::Clone()
-{
-    MathMacro *m = new MathMacro(this);
-    return m;
-}
 
 
-void MathMacro::Metrics()
+MathInset * MathMacro::clone() const
 {
-    if (nargs>0)
-      tmplate->update(this);
-    tmplate->Metrics();
-    width = tmplate->Width();
-    ascent = tmplate->Ascent();
-    descent = tmplate->Descent();
+       return new MathMacro(*this);
 }
 
 
-void MathMacro::Draw(int x, int y)
+void MathMacro::Metrics(MathStyles st, int, int)
 {
-    xo = x;  yo = y;
-    Metrics();
-    tmplate->update(this);
-    tmplate->SetStyle(size);
-    mathGC = latexGC;
-    tmplate->Draw(x, y);
-    for (int i= 0; i<nargs; i++)
-      tmplate->GetMacroXY(i, args[i].x, args[i].y);
-}
-
+       if (mathcursor && mathcursor->isInside(this)) {
+               expanded_ = tmplate_->xcell(0);
+               expanded_.Metrics(st);
+               size_    = st;
+               width_   = expanded_.width()   + 4;
+               ascent_  = expanded_.ascent()  + 2;
+               descent_ = expanded_.descent() + 2;
 
-int MathMacro::GetColumns()
-{
-    return tmplate->getMacroPar(idx)->GetColumns();
-}
+               width_ +=  mathed_string_width(LM_TC_TEXTRM, size_, name_) + 10;
 
+               int lasc;
+               int ldes;
+               int lwid;
+               mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
 
-void MathMacro::GetXY(int& x, int& y) const
-{
-    x = args[idx].x;  y = args[idx].y;
-}
-
-
-bool MathMacro::Permit(short f)
-{
-    return ((nargs>0) ? tmplate->getMacroPar(idx)->Permit(f): MathParInset::Permit(f));
-}
-
-void MathMacro::SetFocus(int x, int y)
-{
-    tmplate->update(this);
-    tmplate->SetMacroFocus(idx, x, y);
-}
-
-
-void MathMacro::Write(FILE *file)
-{
-   string output;
-   MathMacro::Write(output);
-   fprintf(file, "%s", output.c_str());
-}
-
-
-void MathMacro::Write(string &file)
-{
-    if (tmplate->flags & MMF_Exp) {
-           lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
-                                 << ' ' << MMF_Exp << endl; 
-       tmplate->update(this);
-       tmplate->Write(file);
-    } else {
-       if (tmplate->flags & MMF_Env) {
-         file += "\\begin{";
-         file += name;
-         file += "} ";
+               for (int i = 0; i < nargs(); ++i) {
+                       MathXArray & c = xcell(i);
+                       c.Metrics(st);
+                       width_    = std::max(width_, c.width() + lwid);
+                       descent_ += std::max(c.ascent(),  lasc) + 5;
+                       descent_ += std::max(c.descent(), ldes) + 5;
+               }
        } else {
-         file += '\\';
-         file += name;
+               expanded_ = tmplate_->xcell(0);
+               expanded_.data_.substitute(*this);
+               expanded_.Metrics(st);
+               size_    = st;
+               width_   = expanded_.width()   + 6;
+               ascent_  = expanded_.ascent()  + 3;
+               descent_ = expanded_.descent() + 3;
        }
-//     if (options) { 
-//       file += '[';
-//       file += options;
-//       file += ']';
-//      }
-       
-       if (!(tmplate->flags & MMF_Env) && nargs>0) 
-         file += '{';
-       
-       for (int i= 0; i<nargs; i++) {
-           array = args[i].array;
-           MathParInset::Write(file);
-           if (i<nargs-1)  
-             file += "}{";
-       }   
-       if (tmplate->flags & MMF_Env) {
-           file += "\\end{";
-           file += name;
-           file += '}';
-       } else {
-           if (nargs>0) 
-               file += '}';
-           else
-               file += ' ';
-       }
-    }
 }
 
 
-
-/*---------------  Macro argument -----------------------------------*/
-
-MathMacroArgument::MathMacroArgument(int n)
+void MathMacro::draw(Painter & pain, int x, int y)
 {
-    number = n;
-    expnd_mode = false;
-    SetType(LM_OT_MACRO_ARG);
-}
+       xo(x);
+       yo(y);
 
-void MathMacroArgument::Draw(int x, int baseline)
-{
-    if (expnd_mode) {
-       MathParInset::Draw(x, baseline);
-    } else {
-       unsigned char s[3];
-       sprintf((char*)s, "#%d", number);
-       drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
-    }
-}
+       Metrics(size());
 
+       LColor::color col;
 
-void MathMacroArgument::Metrics()
-{
-    if (expnd_mode) {
-       MathParInset::Metrics();
-    } else {
-       unsigned char s[3];
-       sprintf((char*)s, "#%d", number);
-       width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
-       mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
-    }
-}
-
-void MathMacroArgument::Write(FILE *file)
-{
-   string output;
-   MathMacroArgument::Write(output);
-   fprintf(file, "%s", output.c_str());
-}
+       if (mathcursor && mathcursor->isInside(this)) {
 
-void MathMacroArgument::Write(string &file)
-{
-    if (expnd_mode) {
-       MathParInset::Write(file);
-    } else {
-       file += '#';
-       file += tostr(number);
-       file += ' ';
-    }
-}
+               int h = y - ascent() + 2 + expanded_.ascent();
+               drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
 
+               int const w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
+               expanded_.draw(pain, x + w + 12, h);
+               h += expanded_.descent();
 
-/* --------------------- MathMacroTemplate ---------------------------*/
+               int lasc;
+               int ldes;
+               int lwid;
+               mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
 
-MathMacroTemplate::MathMacroTemplate(char const *nm, int na, int flg):
-    MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
-    flags(flg), nargs(na)
-{
-    if (nargs>0) {
-       tcode = LM_TC_ACTIVE_INSET;
-       args = new MathMacroArgument[nargs];
-       for (int i= 0; i<nargs; i++) {
-           args[i].setNumber(i+1);
+               for (int i = 0; i < nargs(); ++i) {
+                       MathXArray & c = xcell(i);
+                       h += std::max(c.ascent(), lasc) + 5;
+                       c.draw(pain, x + lwid, h);
+                       char str[] = "#1:";
+                       str[1] += i;
+                       drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
+                       h += std::max(c.descent(), ldes) + 5;
+               }
+               col = LColor::red;
+       } else {
+               expanded_.draw(pain, x + 3, y);
+               col = LColor::black;
        }
-    } else 
-      tcode = LM_TC_INSET;
-}
 
-
-MathMacroTemplate::~MathMacroTemplate()
-{
-    // prevent to delete already deleted objects
-    for (int i= 0; i<nargs; i++) {
-       args[i].SetData(0);
-    }
-    delete[] args;
+       if (nargs() > 0)
+               pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
 }
 
 
-void MathMacroTemplate::setEditMode(bool ed)
+void MathMacro::dump(std::ostream & os) const
 {
-    if (ed) {
-       flags |= MMF_Edit;
-       for (int i= 0; i<nargs; i++) {
-           args[i].setExpand(false);
-       }
-    } else {
-       flags &= ~MMF_Edit;
-       for (int i= 0; i<nargs; i++) {
-           args[i].setExpand(true);
-       }
-    }
+       MathMacroTable::dump();
+       os << "\n macro: '" << this << "'\n";
+       os << " name: '" << name_ << "'\n";
+       os << " template: '" << tmplate_ << "'\n";
+       os << " template: '" << *tmplate_ << "'\n";
+       os << endl;
 }
 
-
-void MathMacroTemplate::Draw(int x, int y)
+void MathMacro::Write(std::ostream & os, bool fragile) const
 {
-    int x2, y2;
-    bool expnd = (nargs>0) ? args[0].getExpand(): false;
-    if (flags & MMF_Edit) {
-       for (int i= 0; i<nargs; i++) {
-           args[i].setExpand(false);
-       }
-      x2 = x; y2 = y;
-    } else {
-       for (int i= 0; i<nargs; i++) {
-           args[i].setExpand(true);
+       os << '\\' << name_;
+       for (int i = 0; i < nargs(); ++i) {
+               os << '{';
+               cell(i).Write(os, fragile);
+               os << '}';
        }
-      x2 = xo; y2 = yo;
-    }
-    MathParInset::Draw(x, y);
-    xo = x2; yo = y2;
-    
-    for (int i= 0; i<nargs; i++) {
-       args[i].setExpand(expnd);
-    }
+       if (nargs() == 0) 
+               os << ' ';
 }
 
-void MathMacroTemplate::Metrics()
-{
-    bool expnd = (nargs>0) ? args[0].getExpand(): false;
-    
-    if (flags & MMF_Edit) {
-       for (int i= 0; i<nargs; i++) {
-           args[i].setExpand(false);
-       }
-    } else {
-       for (int i= 0; i<nargs; i++) {
-           args[i].setExpand(true);
-       }
-    }
-    MathParInset::Metrics();
-    
-    for (int i= 0; i<nargs; i++) {
-       args[i].setExpand(expnd);
-    }
-}
 
-void MathMacroTemplate::update(MathMacro* macro)
+void MathMacro::WriteNormal(std::ostream & os) const
 {
-    int idx = (macro) ? macro->getArgumentIdx(): 0;
-    for (int i= 0; i<nargs; i++) {
-       if (macro) {
-           macro->setArgumentIdx(i);
-           args[i].SetData(macro->GetData());
-           MathedRowSt *row = macro->getRowSt();
-           args[i].setRowSt(row);
+       os << "[macro " << name_ << " ";
+       for (int i = 0; i < nargs(); ++i) {
+               cell(i).WriteNormal(os);
+               os << ' ';
        }
-    }  
-    if (macro)
-      macro->setArgumentIdx(idx);
-}
-    
-
-void MathMacroTemplate::WriteDef(FILE *file)
-{
-    fprintf(file, "\n\\newcommand{\\%s}", name);
-      
-    if (nargs > 0 ) 
-      fprintf(file, "[%d]", nargs);
-    
-    fprintf(file, "{");
-    
-    for (int i= 0; i<nargs; i++) {
-       args[i].setExpand(false);
-    }   
-    Write(file); 
-    fprintf(file, "}\n");
-}
-
-void MathMacroTemplate::WriteDef(string &file)
-{
-    file += "\n\\newcommand{\\";
-    file += name;
-    file += '}';
-      
-    if (nargs > 0 ) {
-      file += '[';
-      file += tostr(nargs);
-      file += ']';
-    }
-    
-    file += '{';
-    
-    for (int i= 0; i<nargs; i++) {
-       args[i].setExpand(false);
-    }   
-    Write(file); 
-    file += "}\n";
-}
-
-void MathMacroTemplate::setArgument(LyxArrayBase *a, int i)
-{
-    args[i].SetData(a);
-}
-
-void MathMacroTemplate::GetMacroXY(int i, int& x, int& y) const
-{
-    args[i].GetXY(x, y);
+       os << "] ";
 }
 
 
-MathParInset *MathMacroTemplate::getMacroPar(int i) const
+bool MathMacro::idxUp(int & idx, int & pos) const
 {
-    return ((i>= 0 && i<nargs) ? (MathParInset*)&args[i]: 0);
+       return MathInset::idxLeft(idx, pos);
 }
 
 
-void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
+bool MathMacro::idxDown(int & idx, int & pos) const
 {
-    for (int i= 0; i<nargs; i++) {
-       if (args[i].Inside(x, y)) {
-           idx = i;
-           break;
-       }
-    }
+       return MathInset::idxRight(idx, pos);
 }
 
 
-/* -------------------------- MathMacroTable -----------------------*/
-
-MathMacroTable::MathMacroTable(int n): max_macros(n)
+bool MathMacro::idxLeft(int &, int &) const
 {
-    macro_table = new MathMacroTemplateP[max_macros];
-    num_macros = 0;
+       return false;
 }
 
 
-MathMacroTable::~MathMacroTable()
+bool MathMacro::idxRight(int &, int &) const
 {
-    delete[] macro_table;
+       return false;
 }
 
 
-// The search is currently linear but will be binary or hash, later.
-MathMacroTemplate *MathMacroTable::getTemplate(char const* name) const
+void MathMacro::Validate(LaTeXFeatures & features) const
 {
-    for (int i= 0; i<num_macros; i++) {
-      if (strcmp(name, macro_table[i]->GetName()) == 0) 
-       return macro_table[i];
-    }
-    
-    return 0;
+       if (name_ == "binom")
+               features.binom = true;
+       MathInset::Validate(features);
 }
-
-void MathMacroTable::addTemplate(MathMacroTemplate *m)
-{
-    if (num_macros<max_macros)
-      macro_table[num_macros++] = m;
-    else
-           lyxerr << "Error (MathMacroTable::addTemplate): "
-                   "Macro table exhausted!" << endl;
-}
-
-
-// All this stuff aparently leaks because it's created here and is not 
-// deleted never, but it have to live all the LyX sesion. OK, would not
-// so hard to do it in the MacroTable destructor, but this doesn't harm
-// seriously, so don't bother me with purify results here.   ;-)
-
-void MathMacroTable::builtinMacros()
-{
-    MathMacroTemplate *m;
-    MathedIter iter;
-    MathParInset* inset;// *arg;
-    LyxArrayBase *array, *array2;
-    
-    built = true;
-    
-    lyxerr[Debug::MATHED] << "Building macros" << endl;
-    
-    // This macro doesn't have arguments
-    m = new MathMacroTemplate("notin");  // this leaks
-    addTemplate(m);
-    array = new LyxArrayBase; // this leaks
-    iter.SetData(array);
-    iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
-    m->SetData(array);
-    
-    // These two are only while we are still with LyX 2.x
-    m = new MathMacroTemplate("emptyset"); // this leaks
-    addTemplate(m);
-    array = new LyxArrayBase; // this leaks
-    iter.SetData(array);
-    iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
-    m->SetData(array);
-    
-    m = new MathMacroTemplate("perp"); // this leaks
-    addTemplate(m);
-    array = new LyxArrayBase; // this leaks
-    iter.SetData(array);
-    iter.Insert(LM_bot, LM_TC_BOP);
-    m->SetData(array);
-
-    // binom has two arguments
-    m = new MathMacroTemplate("binom", 2);
-    addTemplate(m);
-    array = new LyxArrayBase; 
-    m->SetData(array);
-    iter.SetData(array);
-    inset = new MathDelimInset('(', ')');
-    iter.Insert(inset, LM_TC_ACTIVE_INSET);
-    array = new LyxArrayBase; 
-    iter.SetData(array);
-    MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
-    iter.Insert(frac, LM_TC_ACTIVE_INSET);
-    inset->SetData(array);
-    array = new LyxArrayBase;
-    array2 = new LyxArrayBase;  
-    iter.SetData(array);
-    iter.Insert(m->getMacroPar(0));
-    iter.SetData(array2);
-    iter.Insert(m->getMacroPar(1));
-    frac->SetData(array, array2);
-
-/*
-    // Cases has 1 argument
-    m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
-    addTemplate(m);
-    array = new LyxArrayBase; // this leaks
-    iter.SetData(array);
-    arg = new MathMatrixInset(2, 1); // this leaks
-
-    m->setArgument(arg);
-    arg->SetAlign('c', "ll");
-    iter.Insert(arg, LM_TC_ACTIVE_INSET);
-    inset = new MathDelimInset('{', '.'); // this leaks
-    inset->SetData(array);
-    array = new LyxArrayBase; // this leaks
-    iter.SetData(array);
-    iter.Insert(inset, LM_TC_ACTIVE_INSET);
-    m->SetData(array);
-  
-
-    // the environment substack has 1 argument
-    m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
-    addTemplate(m);     
-    arg = new MathMatrixInset(1, 1); // this leaks
-    m->setArgument(arg);
-    arg->SetType(LM_OT_MACRO);
-    array = new LyxArrayBase; // this leaks
-    iter.SetData(array);
-    iter.Insert(arg, LM_TC_ACTIVE_INSET);
-    m->SetData(array);*/
-}
-
-
-MathMacroTable MathMacroTable::mathMTable(255);
-bool  MathMacroTable::built = false;
-