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 MathMacro * MathMacro::Clone()
107 MathMacro *m = new MathMacro(this);
110 return new MathMacro(this);
114 void MathMacro::Metrics()
117 tmplate->update(this);
119 width = tmplate->Width();
120 ascent = tmplate->Ascent();
121 descent = tmplate->Descent();
125 void MathMacro::Draw(int x, int y)
129 tmplate->update(this);
130 tmplate->SetStyle(size);
133 for (int i= 0; i<nargs; i++)
134 tmplate->GetMacroXY(i, args[i].x, args[i].y);
138 int MathMacro::GetColumns()
140 return tmplate->getMacroPar(idx)->GetColumns();
144 void MathMacro::GetXY(int& x, int& y) const
146 x = args[idx].x; y = args[idx].y;
150 bool MathMacro::Permit(short f)
152 return ((nargs>0) ? tmplate->getMacroPar(idx)->Permit(f): MathParInset::Permit(f));
155 void MathMacro::SetFocus(int x, int y)
157 tmplate->update(this);
158 tmplate->SetMacroFocus(idx, x, y);
162 void MathMacro::Write(FILE *file)
165 MathMacro::Write(output);
166 fprintf(file, "%s", output.c_str());
170 void MathMacro::Write(string &file)
172 if (tmplate->flags & MMF_Exp) {
173 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
174 << ' ' << MMF_Exp << endl;
175 tmplate->update(this);
176 tmplate->Write(file);
178 if (tmplate->flags & MMF_Env) {
192 if (!(tmplate->flags & MMF_Env) && nargs>0)
195 for (int i= 0; i<nargs; i++) {
196 array = args[i].array;
197 MathParInset::Write(file);
201 if (tmplate->flags & MMF_Env) {
216 /*--------------- Macro argument -----------------------------------*/
218 MathMacroArgument::MathMacroArgument(int n)
222 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);
249 void MathMacroArgument::Write(FILE *file)
252 MathMacroArgument::Write(output);
253 fprintf(file, "%s", output.c_str());
256 void MathMacroArgument::Write(string &file)
259 MathParInset::Write(file);
262 file += tostr(number);
268 /* --------------------- MathMacroTemplate ---------------------------*/
270 MathMacroTemplate::MathMacroTemplate(char const *nm, int na, int flg):
271 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
272 flags(flg), nargs(na)
275 tcode = LM_TC_ACTIVE_INSET;
276 args = new MathMacroArgument[nargs];
277 for (int i= 0; i<nargs; i++) {
278 args[i].setNumber(i+1);
285 MathMacroTemplate::~MathMacroTemplate()
287 // prevent to delete already deleted objects
288 for (int i= 0; i<nargs; i++) {
295 void MathMacroTemplate::setEditMode(bool ed)
299 for (int i= 0; i<nargs; i++) {
300 args[i].setExpand(false);
304 for (int i= 0; i<nargs; i++) {
305 args[i].setExpand(true);
311 void MathMacroTemplate::Draw(int x, int y)
314 bool expnd = (nargs>0) ? args[0].getExpand(): false;
315 if (flags & MMF_Edit) {
316 for (int i= 0; i<nargs; i++) {
317 args[i].setExpand(false);
321 for (int i= 0; i<nargs; i++) {
322 args[i].setExpand(true);
326 MathParInset::Draw(x, y);
329 for (int i= 0; i<nargs; i++) {
330 args[i].setExpand(expnd);
334 void MathMacroTemplate::Metrics()
336 bool expnd = (nargs>0) ? args[0].getExpand(): false;
338 if (flags & MMF_Edit) {
339 for (int i= 0; i<nargs; i++) {
340 args[i].setExpand(false);
343 for (int i= 0; i<nargs; i++) {
344 args[i].setExpand(true);
347 MathParInset::Metrics();
349 for (int i= 0; i<nargs; i++) {
350 args[i].setExpand(expnd);
354 void MathMacroTemplate::update(MathMacro* macro)
356 int idx = (macro) ? macro->getArgumentIdx(): 0;
357 for (int i= 0; i<nargs; i++) {
359 macro->setArgumentIdx(i);
360 args[i].SetData(macro->GetData());
361 MathedRowSt *row = macro->getRowSt();
362 args[i].setRowSt(row);
366 macro->setArgumentIdx(idx);
370 void MathMacroTemplate::WriteDef(FILE *file)
372 fprintf(file, "\n\\newcommand{\\%s}", name);
375 fprintf(file, "[%d]", nargs);
379 for (int i= 0; i<nargs; i++) {
380 args[i].setExpand(false);
383 fprintf(file, "}\n");
386 void MathMacroTemplate::WriteDef(string &file)
388 file += "\n\\newcommand{\\";
394 file += tostr(nargs);
400 for (int i= 0; i<nargs; i++) {
401 args[i].setExpand(false);
407 void MathMacroTemplate::setArgument(LyxArrayBase *a, int i)
412 void MathMacroTemplate::GetMacroXY(int i, int& x, int& y) const
418 MathParInset *MathMacroTemplate::getMacroPar(int i) const
420 return ((i>= 0 && i<nargs) ? (MathParInset*)&args[i]: 0);
424 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
426 for (int i= 0; i<nargs; i++) {
427 if (args[i].Inside(x, y)) {
435 /* -------------------------- MathMacroTable -----------------------*/
437 MathMacroTable::MathMacroTable(int n): max_macros(n)
439 macro_table = new MathMacroTemplateP[max_macros];
444 MathMacroTable::~MathMacroTable()
446 delete[] macro_table;
450 // The search is currently linear but will be binary or hash, later.
451 MathMacroTemplate *MathMacroTable::getTemplate(char const* name) const
453 for (int i= 0; i<num_macros; i++) {
454 if (strcmp(name, macro_table[i]->GetName()) == 0)
455 return macro_table[i];
461 void MathMacroTable::addTemplate(MathMacroTemplate *m)
463 if (num_macros<max_macros)
464 macro_table[num_macros++] = m;
466 lyxerr << "Error (MathMacroTable::addTemplate): "
467 "Macro table exhausted!" << endl;
471 // All this stuff aparently leaks because it's created here and is not
472 // deleted never, but it have to live all the LyX sesion. OK, would not
473 // so hard to do it in the MacroTable destructor, but this doesn't harm
474 // seriously, so don't bother me with purify results here. ;-)
476 void MathMacroTable::builtinMacros()
478 MathMacroTemplate *m;
480 MathParInset* inset;// *arg;
481 LyxArrayBase *array, *array2;
485 lyxerr[Debug::MATHED] << "Building macros" << endl;
487 // This macro doesn't have arguments
488 m = new MathMacroTemplate("notin"); // this leaks
490 array = new LyxArrayBase; // this leaks
492 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
495 // These two are only while we are still with LyX 2.x
496 m = new MathMacroTemplate("emptyset"); // this leaks
498 array = new LyxArrayBase; // this leaks
500 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
503 m = new MathMacroTemplate("perp"); // this leaks
505 array = new LyxArrayBase; // this leaks
507 iter.Insert(LM_bot, LM_TC_BOP);
510 // binom has two arguments
511 m = new MathMacroTemplate("binom", 2);
513 array = new LyxArrayBase;
516 inset = new MathDelimInset('(', ')');
517 iter.Insert(inset, LM_TC_ACTIVE_INSET);
518 array = new LyxArrayBase;
520 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
521 iter.Insert(frac, LM_TC_ACTIVE_INSET);
522 inset->SetData(array);
523 array = new LyxArrayBase;
524 array2 = new LyxArrayBase;
526 iter.Insert(m->getMacroPar(0));
527 iter.SetData(array2);
528 iter.Insert(m->getMacroPar(1));
529 frac->SetData(array, array2);
532 // Cases has 1 argument
533 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
535 array = new LyxArrayBase; // this leaks
537 arg = new MathMatrixInset(2, 1); // this leaks
540 arg->SetAlign('c', "ll");
541 iter.Insert(arg, LM_TC_ACTIVE_INSET);
542 inset = new MathDelimInset('{', '.'); // this leaks
543 inset->SetData(array);
544 array = new LyxArrayBase; // this leaks
546 iter.Insert(inset, LM_TC_ACTIVE_INSET);
550 // the environment substack has 1 argument
551 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
553 arg = new MathMatrixInset(1, 1); // this leaks
555 arg->SetType(LM_OT_MACRO);
556 array = new LyxArrayBase; // this leaks
558 iter.Insert(arg, LM_TC_ACTIVE_INSET);
563 MathMacroTable MathMacroTable::mathMTable(255);
564 bool MathMacroTable::built = false;