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"
33 enum MathedMacroFlag {
40 extern GC mathGC, mathFrameGC, latexGC;
41 extern int mathed_string_width(short type, int style, byte const* s, int ls);
42 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
45 MathMacro::MathMacro(MathMacroTemplate* t):
46 MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
48 nargs = tmplate->getNoArgs();
49 tcode = tmplate->getTCode();
50 args = new MacroArgumentBase[nargs];
51 for (int i=0; i<nargs; i++) {
52 // if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
53 // args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
56 /* int k = tmplate->getMacroPar(i)->GetColumns();
58 args[i].array = new LyxArrayBase;
59 for (int j=0; j<k-1; j++) args[i].array->Insert(j, LM_TC_TAB);
63 SetName(tmplate->GetName());
66 MathMacro::MathMacro(MathMacro* m):
67 MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
70 nargs = tmplate->getNoArgs();
71 tcode = tmplate->getTCode();
72 args = new MacroArgumentBase[nargs];
74 SetName(tmplate->GetName());
75 for (int i=0; i<tmplate->nargs; i++) {
77 MathedIter it(m->GetData());
78 args[i].row = m->args[i].row;
79 args[i].array = it.Copy();
83 MathMacro::~MathMacro()
85 for (idx=0; idx<nargs; idx++) {
86 MathedIter it(args[idx].array);
94 MathedInset *MathMacro::Clone()
96 MathMacro *m = new MathMacro(this);
101 void MathMacro::Metrics()
104 tmplate->update(this);
106 width = tmplate->Width();
107 ascent = tmplate->Ascent();
108 descent = tmplate->Descent();
112 void MathMacro::Draw(int x, int y)
116 tmplate->update(this);
117 tmplate->SetStyle(size);
120 for (int i=0; i<nargs; i++)
121 tmplate->GetMacroXY(i, args[i].x, args[i].y);
125 int MathMacro::GetColumns()
127 return tmplate->getMacroPar(idx)->GetColumns();
131 void MathMacro::GetXY(int& x, int& y) const
133 x = args[idx].x; y = args[idx].y;
137 bool MathMacro::Permit(short f)
139 return ((nargs>0) ? tmplate->getMacroPar(idx)->Permit(f): MathParInset::Permit(f));
142 void MathMacro::SetFocus(int x, int y)
144 tmplate->update(this);
145 tmplate->SetMacroFocus(idx, x, y);
149 void MathMacro::Write(FILE *file)
152 MathMacro::Write(output);
153 fprintf(file, "%s", output.c_str());
157 void MathMacro::Write(LString &file)
159 if (tmplate->flags & MMF_Exp) {
160 lyxerr.debug(LString("Expand ")+ int(tmplate->flags)
161 + ' ' + int(MMF_Exp),
163 tmplate->update(this);
164 tmplate->Write(file);
166 if (tmplate->flags & MMF_Env) {
180 if (!(tmplate->flags & MMF_Env) && nargs>0)
183 for (int i=0; i<nargs; i++) {
184 array = args[i].array;
185 MathParInset::Write(file);
189 if (tmplate->flags & MMF_Env) {
204 /*--------------- Macro argument -----------------------------------*/
206 MathMacroArgument::MathMacroArgument(int n)
210 SetType(LM_OT_MACRO_ARG);
213 void MathMacroArgument::Draw(int x, int baseline)
216 MathParInset::Draw(x, baseline);
219 sprintf((char*)s, "#%d", number);
220 drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
225 void MathMacroArgument::Metrics()
228 MathParInset::Metrics();
231 sprintf((char*)s, "#%d", number);
232 width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
233 mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
237 void MathMacroArgument::Write(FILE *file)
240 MathMacroArgument::Write(output);
241 fprintf(file, "%s", output.c_str());
244 void MathMacroArgument::Write(LString &file)
247 MathParInset::Write(file);
256 /* --------------------- MathMacroTemplate ---------------------------*/
258 MathMacroTemplate::MathMacroTemplate(char const *nm, int na, int flg):
259 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
260 flags(flg), nargs(na)
263 tcode = LM_TC_ACTIVE_INSET;
264 args = new MathMacroArgument[nargs];
265 for (int i=0; i<nargs; i++) {
266 args[i].setNumber(i+1);
273 MathMacroTemplate::~MathMacroTemplate()
275 // prevent to delete already deleted objects
276 for (int i=0; i<nargs; i++) {
283 void MathMacroTemplate::setEditMode(bool ed)
287 for (int i=0; i<nargs; i++) {
288 args[i].setExpand(false);
292 for (int i=0; i<nargs; i++) {
293 args[i].setExpand(true);
299 void MathMacroTemplate::Draw(int x, int y)
302 bool expnd = (nargs>0) ? args[0].getExpand(): false;
303 if (flags & MMF_Edit) {
304 for (int i=0; i<nargs; i++) {
305 args[i].setExpand(false);
309 for (int i=0; i<nargs; i++) {
310 args[i].setExpand(true);
314 MathParInset::Draw(x, y);
317 for (int i=0; i<nargs; i++) {
318 args[i].setExpand(expnd);
322 void MathMacroTemplate::Metrics()
324 bool expnd = (nargs>0) ? args[0].getExpand(): false;
326 if (flags & MMF_Edit) {
327 for (int i=0; i<nargs; i++) {
328 args[i].setExpand(false);
331 for (int i=0; i<nargs; i++) {
332 args[i].setExpand(true);
335 MathParInset::Metrics();
337 for (int i=0; i<nargs; i++) {
338 args[i].setExpand(expnd);
342 void MathMacroTemplate::update(MathMacro* macro)
344 int idx = (macro) ? macro->getArgumentIdx(): 0;
345 for (int i=0; i<nargs; i++) {
347 macro->setArgumentIdx(i);
348 args[i].SetData(macro->GetData());
349 MathedRowSt *row = macro->getRowSt();
350 args[i].setRowSt(row);
354 macro->setArgumentIdx(idx);
358 void MathMacroTemplate::WriteDef(FILE *file)
360 fprintf(file, "\n\\newcommand{\\%s}", name);
363 fprintf(file, "[%d]", nargs);
367 for (int i=0; i<nargs; i++) {
368 args[i].setExpand(false);
371 fprintf(file, "}\n");
374 void MathMacroTemplate::WriteDef(LString &file)
376 file += "\n\\newcommand{\\";
388 for (int i=0; i<nargs; i++) {
389 args[i].setExpand(false);
395 void MathMacroTemplate::setArgument(LyxArrayBase *a, int i)
400 void MathMacroTemplate::GetMacroXY(int i, int& x, int& y) const
406 MathParInset *MathMacroTemplate::getMacroPar(int i) const
408 return ((i>=0 && i<nargs) ? (MathParInset*)&args[i]: 0);
412 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
414 for (int i=0; i<nargs; i++) {
415 if (args[i].Inside(x, y)) {
423 /* -------------------------- MathMacroTable -----------------------*/
425 MathMacroTable::MathMacroTable(int n): max_macros(n)
427 macro_table = new MathMacroTemplateP[max_macros];
432 MathMacroTable::~MathMacroTable()
434 delete[] macro_table;
438 // The search is currently linear but will be binary or hash, later.
439 MathMacroTemplate *MathMacroTable::getTemplate(char const* name) const
441 for (int i=0; i<num_macros; i++) {
442 if (strcmp(name, macro_table[i]->GetName())==0)
443 return macro_table[i];
449 void MathMacroTable::addTemplate(MathMacroTemplate *m)
451 if (num_macros<max_macros)
452 macro_table[num_macros++] = m;
454 lyxerr.print("Error (MathMacroTable::addTemplate): "
455 "Macro table exhausted!");
459 // All this stuff aparently leaks because it's created here and is not
460 // deleted never, but it have to live all the LyX sesion. OK, would not
461 // so hard to do it in the MacroTable destructor, but this doesn't harm
462 // seriously, so don't bother me with purify results here. ;-)
464 void MathMacroTable::builtinMacros()
466 MathMacroTemplate *m;
468 MathParInset* inset;// *arg;
469 LyxArrayBase *array, *array2;
473 lyxerr.debug("Building macros", Error::MATHED);
475 // This macro doesn't have arguments
476 m = new MathMacroTemplate("notin"); // this leaks
478 array = new LyxArrayBase; // this leaks
480 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
483 // These two are only while we are still with LyX 2.x
484 m = new MathMacroTemplate("emptyset"); // this leaks
486 array = new LyxArrayBase; // this leaks
488 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
491 m = new MathMacroTemplate("perp"); // this leaks
493 array = new LyxArrayBase; // this leaks
495 iter.Insert(LM_bot, LM_TC_BOP);
498 // binom has two arguments
499 m = new MathMacroTemplate("binom", 2);
501 array = new LyxArrayBase;
504 inset = new MathDelimInset('(', ')');
505 iter.Insert(inset, LM_TC_ACTIVE_INSET);
506 array = new LyxArrayBase;
508 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
509 iter.Insert(frac, LM_TC_ACTIVE_INSET);
510 inset->SetData(array);
511 array = new LyxArrayBase;
512 array2 = new LyxArrayBase;
514 iter.Insert(m->getMacroPar(0));
515 iter.SetData(array2);
516 iter.Insert(m->getMacroPar(1));
517 frac->SetData(array, array2);
520 // Cases has 1 argument
521 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
523 array = new LyxArrayBase; // this leaks
525 arg = new MathMatrixInset(2,1); // this leaks
528 arg->SetAlign('c',"ll");
529 iter.Insert(arg, LM_TC_ACTIVE_INSET);
530 inset = new MathDelimInset('{', '.'); // this leaks
531 inset->SetData(array);
532 array = new LyxArrayBase; // this leaks
534 iter.Insert(inset, LM_TC_ACTIVE_INSET);
538 // the environment substack has 1 argument
539 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
541 arg = new MathMatrixInset(1,1); // this leaks
543 arg->SetType(LM_OT_MACRO);
544 array = new LyxArrayBase; // this leaks
546 iter.Insert(arg, LM_TC_ACTIVE_INSET);
551 MathMacroTable MathMacroTable::mathMTable(255);
552 bool MathMacroTable::built = false;