4 * Purpose: Implementation of macro class for mathed
5 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6 * Created: November 1996
7 * Description: WYSIWYG math macros
11 * Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
13 * Version: 0.2, Mathed & Lyx project.
15 * This code is under the GNU General Public Licence version 2 or later.
19 #include FORMS_H_LOCATION
22 #pragma implementation "math_macro.h"
23 #pragma implementation "math_defs.h"
27 #include "math_macro.h"
28 #include "math_iter.h"
29 #include "math_inset.h"
30 #include "support/lstrings.h"
34 ostream & operator<<(ostream & o, MathedTextCodes mtc)
39 enum MathedMacroFlag {
45 ostream & operator<<(ostream & o, MathedMacroFlag mmf)
50 extern GC mathGC, mathFrameGC, latexGC;
51 extern int mathed_string_width(short type, int style, byte const* s, int ls);
52 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
55 MathMacro::MathMacro(MathMacroTemplate* t):
56 MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
58 nargs = tmplate->getNoArgs();
59 tcode = tmplate->getTCode();
60 args = new MacroArgumentBase[nargs];
61 for (int i = 0; i < nargs; ++i) {
62 // if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
63 // args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
66 /* int k = tmplate->getMacroPar(i)->GetColumns();
68 args[i].array = new LyxArrayBase;
69 for (int j= 0; j<k-1; j++) args[i].array->Insert(j, LM_TC_TAB);
73 SetName(tmplate->GetName());
77 MathMacro::MathMacro(MathMacro * m):
78 MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
81 nargs = tmplate->getNoArgs();
82 tcode = tmplate->getTCode();
83 args = new MacroArgumentBase[nargs];
85 SetName(tmplate->GetName());
86 for (int i = 0; i < tmplate->nargs; ++i) {
88 MathedIter it(m->GetData());
89 args[i].row = m->args[i].row;
90 args[i].array = it.Copy();
94 MathMacro::~MathMacro()
96 for (idx = 0; idx < nargs; ++idx) {
97 MathedIter it(args[idx].array);
105 MathedInset * MathMacro::Clone()
107 return new MathMacro(this);
111 void MathMacro::Metrics()
114 tmplate->update(this);
116 width = tmplate->Width();
117 ascent = tmplate->Ascent();
118 descent = tmplate->Descent();
122 void MathMacro::Draw(int x, int y)
126 tmplate->update(this);
127 tmplate->SetStyle(size);
130 for (int i = 0; i < nargs; ++i)
131 tmplate->GetMacroXY(i, args[i].x, args[i].y);
135 int MathMacro::GetColumns()
137 return tmplate->getMacroPar(idx)->GetColumns();
141 void MathMacro::GetXY(int & x, int & y) const
143 x = args[idx].x; y = args[idx].y;
147 bool MathMacro::Permit(short f)
150 tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
154 void MathMacro::SetFocus(int x, int y)
156 tmplate->update(this);
157 tmplate->SetMacroFocus(idx, x, y);
161 void MathMacro::Write(ostream & os)
164 MathMacro::Write(output);
169 void MathMacro::Write(string &file)
171 if (tmplate->flags & MMF_Exp) {
172 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
173 << ' ' << MMF_Exp << endl;
174 tmplate->update(this);
175 tmplate->Write(file);
177 if (tmplate->flags & MMF_Env) {
191 if (!(tmplate->flags & MMF_Env) && nargs > 0)
194 for (int i = 0; i < nargs; ++i) {
195 array = args[i].array;
196 MathParInset::Write(file);
200 if (tmplate->flags & MMF_Env) {
215 /*--------------- Macro argument -----------------------------------*/
217 MathMacroArgument::MathMacroArgument(int n)
221 SetType(LM_OT_MACRO_ARG);
225 void MathMacroArgument::Draw(int x, int baseline)
228 MathParInset::Draw(x, baseline);
231 sprintf((char*)s, "#%d", number);
232 drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
237 void MathMacroArgument::Metrics()
240 MathParInset::Metrics();
243 sprintf((char*)s, "#%d", number);
244 width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
245 mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
250 void MathMacroArgument::Write(ostream & os)
253 MathMacroArgument::Write(output);
258 void MathMacroArgument::Write(string & file)
261 MathParInset::Write(file);
264 file += tostr(number);
270 /* --------------------- MathMacroTemplate ---------------------------*/
272 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
273 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
274 flags(flg), nargs(na)
277 tcode = LM_TC_ACTIVE_INSET;
278 args = new MathMacroArgument[nargs];
279 for (int i= 0; i<nargs; i++) {
280 args[i].setNumber(i+1);
287 MathMacroTemplate::~MathMacroTemplate()
289 // prevent to delete already deleted objects
290 for (int i= 0; i<nargs; i++) {
297 void MathMacroTemplate::setEditMode(bool ed)
301 for (int i = 0; i < nargs; ++i) {
302 args[i].setExpand(false);
306 for (int i = 0; i < nargs; ++i) {
307 args[i].setExpand(true);
313 void MathMacroTemplate::Draw(int x, int y)
316 bool expnd = (nargs>0) ? args[0].getExpand(): false;
317 if (flags & MMF_Edit) {
318 for (int i = 0; i < nargs; ++i) {
319 args[i].setExpand(false);
323 for (int i = 0; i < nargs; ++i) {
324 args[i].setExpand(true);
328 MathParInset::Draw(x, y);
331 for (int i = 0; i < nargs; ++i) {
332 args[i].setExpand(expnd);
337 void MathMacroTemplate::Metrics()
339 bool expnd = (nargs>0) ? args[0].getExpand(): false;
341 if (flags & MMF_Edit) {
342 for (int i= 0; i<nargs; i++) {
343 args[i].setExpand(false);
346 for (int i= 0; i<nargs; i++) {
347 args[i].setExpand(true);
350 MathParInset::Metrics();
352 for (int i= 0; i<nargs; i++) {
353 args[i].setExpand(expnd);
358 void MathMacroTemplate::update(MathMacro * macro)
360 int idx = (macro) ? macro->getArgumentIdx(): 0;
361 for (int i= 0; i<nargs; i++) {
363 macro->setArgumentIdx(i);
364 args[i].SetData(macro->GetData());
365 MathedRowSt *row = macro->getRowSt();
366 args[i].setRowSt(row);
370 macro->setArgumentIdx(idx);
374 void MathMacroTemplate::WriteDef(ostream & os)
376 os << "\n\\newcommand{\\" << name << "}";
379 os << "[" << nargs << "]";
383 for (int i = 0; i < nargs; ++i) {
384 args[i].setExpand(false);
391 void MathMacroTemplate::WriteDef(string &file)
393 file += "\n\\newcommand{\\";
399 file += tostr(nargs);
405 for (int i = 0; i < nargs; ++i) {
406 args[i].setExpand(false);
413 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
419 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
425 MathParInset * MathMacroTemplate::getMacroPar(int i) const
427 return (i >= 0 && i < nargs) ? (MathParInset*)&args[i] : 0;
431 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
433 for (int i = 0; i < nargs; ++i) {
434 if (args[i].Inside(x, y)) {
442 /* -------------------------- MathMacroTable -----------------------*/
444 MathMacroTable::MathMacroTable(int n): max_macros(n)
446 macro_table = new MathMacroTemplateP[max_macros];
451 MathMacroTable::~MathMacroTable()
453 delete[] macro_table;
457 // The search is currently linear but will be binary or hash, later.
458 MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
460 for (int i = 0; i < num_macros; ++i) {
461 if (strcmp(name, macro_table[i]->GetName()) == 0)
462 return macro_table[i];
468 void MathMacroTable::addTemplate(MathMacroTemplate * m)
470 if (num_macros < max_macros)
471 macro_table[num_macros++] = m;
473 lyxerr << "Error (MathMacroTable::addTemplate): "
474 "Macro table exhausted!" << endl;
478 // All this stuff aparently leaks because it's created here and is not
479 // deleted never, but it have to live all the LyX sesion. OK, would not
480 // so hard to do it in the MacroTable destructor, but this doesn't harm
481 // seriously, so don't bother me with purify results here. ;-)
483 void MathMacroTable::builtinMacros()
486 MathParInset * inset;// *arg;
487 LyxArrayBase * array2;
491 lyxerr[Debug::MATHED] << "Building macros" << endl;
493 // This macro doesn't have arguments
494 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
496 LyxArrayBase * array = new LyxArrayBase; // this leaks
498 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
501 // These two are only while we are still with LyX 2.x
502 m = new MathMacroTemplate("emptyset"); // this leaks
504 array = new LyxArrayBase; // this leaks
506 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
509 m = new MathMacroTemplate("perp"); // this leaks
511 array = new LyxArrayBase; // this leaks
513 iter.Insert(LM_bot, LM_TC_BOP);
516 // binom has two arguments
517 m = new MathMacroTemplate("binom", 2);
519 array = new LyxArrayBase;
522 inset = new MathDelimInset('(', ')');
523 iter.Insert(inset, LM_TC_ACTIVE_INSET);
524 array = new LyxArrayBase;
526 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
527 iter.Insert(frac, LM_TC_ACTIVE_INSET);
528 inset->SetData(array);
529 array = new LyxArrayBase;
530 array2 = new LyxArrayBase;
532 iter.Insert(m->getMacroPar(0));
533 iter.SetData(array2);
534 iter.Insert(m->getMacroPar(1));
535 frac->SetData(array, array2);
538 // Cases has 1 argument
539 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
541 array = new LyxArrayBase; // this leaks
543 arg = new MathMatrixInset(2, 1); // this leaks
546 arg->SetAlign('c', "ll");
547 iter.Insert(arg, LM_TC_ACTIVE_INSET);
548 inset = new MathDelimInset('{', '.'); // this leaks
549 inset->SetData(array);
550 array = new LyxArrayBase; // this leaks
552 iter.Insert(inset, LM_TC_ACTIVE_INSET);
556 // the environment substack has 1 argument
557 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
559 arg = new MathMatrixInset(1, 1); // this leaks
561 arg->SetType(LM_OT_MACRO);
562 array = new LyxArrayBase; // this leaks
564 iter.Insert(arg, LM_TC_ACTIVE_INSET);
569 MathMacroTable MathMacroTable::mathMTable(255);
570 bool MathMacroTable::built = false;