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());
76 MathMacro::MathMacro(MathMacro* m):
77 MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
80 nargs = tmplate->getNoArgs();
81 tcode = tmplate->getTCode();
82 args = new MacroArgumentBase[nargs];
84 SetName(tmplate->GetName());
85 for (int i= 0; i<tmplate->nargs; i++) {
87 MathedIter it(m->GetData());
88 args[i].row = m->args[i].row;
89 args[i].array = it.Copy();
93 MathMacro::~MathMacro()
95 for (idx= 0; idx<nargs; idx++) {
96 MathedIter it(args[idx].array);
104 MathedInset *MathMacro::Clone()
106 MathMacro *m = 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)
149 return ((nargs>0) ? tmplate->getMacroPar(idx)->Permit(f): MathParInset::Permit(f));
152 void MathMacro::SetFocus(int x, int y)
154 tmplate->update(this);
155 tmplate->SetMacroFocus(idx, x, y);
159 void MathMacro::Write(FILE *file)
162 MathMacro::Write(output);
163 fprintf(file, "%s", output.c_str());
167 void MathMacro::Write(string &file)
169 if (tmplate->flags & MMF_Exp) {
170 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
171 << ' ' << MMF_Exp << endl;
172 tmplate->update(this);
173 tmplate->Write(file);
175 if (tmplate->flags & MMF_Env) {
189 if (!(tmplate->flags & MMF_Env) && nargs>0)
192 for (int i= 0; i<nargs; i++) {
193 array = args[i].array;
194 MathParInset::Write(file);
198 if (tmplate->flags & MMF_Env) {
213 /*--------------- Macro argument -----------------------------------*/
215 MathMacroArgument::MathMacroArgument(int n)
219 SetType(LM_OT_MACRO_ARG);
222 void MathMacroArgument::Draw(int x, int baseline)
225 MathParInset::Draw(x, baseline);
228 sprintf((char*)s, "#%d", number);
229 drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
234 void MathMacroArgument::Metrics()
237 MathParInset::Metrics();
240 sprintf((char*)s, "#%d", number);
241 width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
242 mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
246 void MathMacroArgument::Write(FILE *file)
249 MathMacroArgument::Write(output);
250 fprintf(file, "%s", output.c_str());
253 void MathMacroArgument::Write(string &file)
256 MathParInset::Write(file);
259 file += tostr(number);
265 /* --------------------- MathMacroTemplate ---------------------------*/
267 MathMacroTemplate::MathMacroTemplate(char const *nm, int na, int flg):
268 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
269 flags(flg), nargs(na)
272 tcode = LM_TC_ACTIVE_INSET;
273 args = new MathMacroArgument[nargs];
274 for (int i= 0; i<nargs; i++) {
275 args[i].setNumber(i+1);
282 MathMacroTemplate::~MathMacroTemplate()
284 // prevent to delete already deleted objects
285 for (int i= 0; i<nargs; i++) {
292 void MathMacroTemplate::setEditMode(bool ed)
296 for (int i= 0; i<nargs; i++) {
297 args[i].setExpand(false);
301 for (int i= 0; i<nargs; i++) {
302 args[i].setExpand(true);
308 void MathMacroTemplate::Draw(int x, int y)
311 bool expnd = (nargs>0) ? args[0].getExpand(): false;
312 if (flags & MMF_Edit) {
313 for (int i= 0; i<nargs; i++) {
314 args[i].setExpand(false);
318 for (int i= 0; i<nargs; i++) {
319 args[i].setExpand(true);
323 MathParInset::Draw(x, y);
326 for (int i= 0; i<nargs; i++) {
327 args[i].setExpand(expnd);
331 void MathMacroTemplate::Metrics()
333 bool expnd = (nargs>0) ? args[0].getExpand(): false;
335 if (flags & MMF_Edit) {
336 for (int i= 0; i<nargs; i++) {
337 args[i].setExpand(false);
340 for (int i= 0; i<nargs; i++) {
341 args[i].setExpand(true);
344 MathParInset::Metrics();
346 for (int i= 0; i<nargs; i++) {
347 args[i].setExpand(expnd);
351 void MathMacroTemplate::update(MathMacro* macro)
353 int idx = (macro) ? macro->getArgumentIdx(): 0;
354 for (int i= 0; i<nargs; i++) {
356 macro->setArgumentIdx(i);
357 args[i].SetData(macro->GetData());
358 MathedRowSt *row = macro->getRowSt();
359 args[i].setRowSt(row);
363 macro->setArgumentIdx(idx);
367 void MathMacroTemplate::WriteDef(FILE *file)
369 fprintf(file, "\n\\newcommand{\\%s}", name);
372 fprintf(file, "[%d]", nargs);
376 for (int i= 0; i<nargs; i++) {
377 args[i].setExpand(false);
380 fprintf(file, "}\n");
383 void MathMacroTemplate::WriteDef(string &file)
385 file += "\n\\newcommand{\\";
391 file += tostr(nargs);
397 for (int i= 0; i<nargs; i++) {
398 args[i].setExpand(false);
404 void MathMacroTemplate::setArgument(LyxArrayBase *a, int i)
409 void MathMacroTemplate::GetMacroXY(int i, int& x, int& y) const
415 MathParInset *MathMacroTemplate::getMacroPar(int i) const
417 return ((i>= 0 && i<nargs) ? (MathParInset*)&args[i]: 0);
421 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
423 for (int i= 0; i<nargs; i++) {
424 if (args[i].Inside(x, y)) {
432 /* -------------------------- MathMacroTable -----------------------*/
434 MathMacroTable::MathMacroTable(int n): max_macros(n)
436 macro_table = new MathMacroTemplateP[max_macros];
441 MathMacroTable::~MathMacroTable()
443 delete[] macro_table;
447 // The search is currently linear but will be binary or hash, later.
448 MathMacroTemplate *MathMacroTable::getTemplate(char const* name) const
450 for (int i= 0; i<num_macros; i++) {
451 if (strcmp(name, macro_table[i]->GetName()) == 0)
452 return macro_table[i];
458 void MathMacroTable::addTemplate(MathMacroTemplate *m)
460 if (num_macros<max_macros)
461 macro_table[num_macros++] = m;
463 lyxerr << "Error (MathMacroTable::addTemplate): "
464 "Macro table exhausted!" << endl;
468 // All this stuff aparently leaks because it's created here and is not
469 // deleted never, but it have to live all the LyX sesion. OK, would not
470 // so hard to do it in the MacroTable destructor, but this doesn't harm
471 // seriously, so don't bother me with purify results here. ;-)
473 void MathMacroTable::builtinMacros()
475 MathMacroTemplate *m;
477 MathParInset* inset;// *arg;
478 LyxArrayBase *array, *array2;
482 lyxerr[Debug::MATHED] << "Building macros" << endl;
484 // This macro doesn't have arguments
485 m = new MathMacroTemplate("notin"); // this leaks
487 array = new LyxArrayBase; // this leaks
489 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
492 // These two are only while we are still with LyX 2.x
493 m = new MathMacroTemplate("emptyset"); // this leaks
495 array = new LyxArrayBase; // this leaks
497 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
500 m = new MathMacroTemplate("perp"); // this leaks
502 array = new LyxArrayBase; // this leaks
504 iter.Insert(LM_bot, LM_TC_BOP);
507 // binom has two arguments
508 m = new MathMacroTemplate("binom", 2);
510 array = new LyxArrayBase;
513 inset = new MathDelimInset('(', ')');
514 iter.Insert(inset, LM_TC_ACTIVE_INSET);
515 array = new LyxArrayBase;
517 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
518 iter.Insert(frac, LM_TC_ACTIVE_INSET);
519 inset->SetData(array);
520 array = new LyxArrayBase;
521 array2 = new LyxArrayBase;
523 iter.Insert(m->getMacroPar(0));
524 iter.SetData(array2);
525 iter.Insert(m->getMacroPar(1));
526 frac->SetData(array, array2);
529 // Cases has 1 argument
530 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
532 array = new LyxArrayBase; // this leaks
534 arg = new MathMatrixInset(2, 1); // this leaks
537 arg->SetAlign('c', "ll");
538 iter.Insert(arg, LM_TC_ACTIVE_INSET);
539 inset = new MathDelimInset('{', '.'); // this leaks
540 inset->SetData(array);
541 array = new LyxArrayBase; // this leaks
543 iter.Insert(inset, LM_TC_ACTIVE_INSET);
547 // the environment substack has 1 argument
548 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
550 arg = new MathMatrixInset(1, 1); // this leaks
552 arg->SetType(LM_OT_MACRO);
553 array = new LyxArrayBase; // this leaks
555 iter.Insert(arg, LM_TC_ACTIVE_INSET);
560 MathMacroTable MathMacroTable::mathMTable(255);
561 bool MathMacroTable::built = false;