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();
123 void MathMacro::draw(Painter & pain, int x, int y)
127 tmplate->update(this);
128 tmplate->SetStyle(size);
130 tmplate->draw(pain, x, y);
131 for (int i = 0; i < nargs; ++i)
132 tmplate->GetMacroXY(i, args[i].x, args[i].y);
135 void MathMacro::Draw(int x, int y)
139 tmplate->update(this);
140 tmplate->SetStyle(size);
143 for (int i = 0; i < nargs; ++i)
144 tmplate->GetMacroXY(i, args[i].x, args[i].y);
149 int MathMacro::GetColumns()
151 return tmplate->getMacroPar(idx)->GetColumns();
155 void MathMacro::GetXY(int & x, int & y) const
157 x = args[idx].x; y = args[idx].y;
161 bool MathMacro::Permit(short f)
164 tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
168 void MathMacro::SetFocus(int x, int y)
170 tmplate->update(this);
171 tmplate->SetMacroFocus(idx, x, y);
175 void MathMacro::Write(ostream & os)
178 MathMacro::Write(output);
183 void MathMacro::Write(string &file)
185 if (tmplate->flags & MMF_Exp) {
186 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
187 << ' ' << MMF_Exp << endl;
188 tmplate->update(this);
189 tmplate->Write(file);
191 if (tmplate->flags & MMF_Env) {
205 if (!(tmplate->flags & MMF_Env) && nargs > 0)
208 for (int i = 0; i < nargs; ++i) {
209 array = args[i].array;
210 MathParInset::Write(file);
214 if (tmplate->flags & MMF_Env) {
229 /*--------------- Macro argument -----------------------------------*/
231 MathMacroArgument::MathMacroArgument(int n)
235 SetType(LM_OT_MACRO_ARG);
240 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
243 MathParInset::draw(pain, x, baseline);
246 sprintf(reinterpret_cast<char*>(s), "#%d", number);
247 drawStr(pain, LM_TC_TEX, size, x, baseline, &s[0], 2);
251 void MathMacroArgument::Draw(int x, int baseline)
254 MathParInset::Draw(x, baseline);
257 sprintf(reinterpret_cast<char*>(s), "#%d", number);
258 drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
264 void MathMacroArgument::Metrics()
267 MathParInset::Metrics();
270 sprintf(reinterpret_cast<char*>(s), "#%d", number);
271 width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
272 mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
277 void MathMacroArgument::Write(ostream & os)
280 MathMacroArgument::Write(output);
285 void MathMacroArgument::Write(string & file)
288 MathParInset::Write(file);
291 file += tostr(number);
297 /* --------------------- MathMacroTemplate ---------------------------*/
299 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
300 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
301 flags(flg), nargs(na)
304 tcode = LM_TC_ACTIVE_INSET;
305 args = new MathMacroArgument[nargs];
306 for (int i = 0; i < nargs; ++i) {
307 args[i].setNumber(i + 1);
314 MathMacroTemplate::~MathMacroTemplate()
316 // prevent to delete already deleted objects
317 for (int i = 0; i < nargs; ++i) {
324 void MathMacroTemplate::setEditMode(bool ed)
328 for (int i = 0; i < nargs; ++i) {
329 args[i].setExpand(false);
333 for (int i = 0; i < nargs; ++i) {
334 args[i].setExpand(true);
341 void MathMacroTemplate::draw(Painter & pain, int x, int y)
344 bool expnd = (nargs > 0) ? args[0].getExpand(): false;
345 if (flags & MMF_Edit) {
346 for (int i = 0; i < nargs; ++i) {
347 args[i].setExpand(false);
351 for (int i = 0; i < nargs; ++i) {
352 args[i].setExpand(true);
356 MathParInset::draw(pain, x, y);
359 for (int i = 0; i < nargs; ++i) {
360 args[i].setExpand(expnd);
364 void MathMacroTemplate::Draw(int x, int y)
367 bool expnd = (nargs>0) ? args[0].getExpand(): false;
368 if (flags & MMF_Edit) {
369 for (int i = 0; i < nargs; ++i) {
370 args[i].setExpand(false);
374 for (int i = 0; i < nargs; ++i) {
375 args[i].setExpand(true);
379 MathParInset::Draw(x, y);
382 for (int i = 0; i < nargs; ++i) {
383 args[i].setExpand(expnd);
389 void MathMacroTemplate::Metrics()
391 bool expnd = (nargs>0) ? args[0].getExpand(): false;
393 if (flags & MMF_Edit) {
394 for (int i = 0; i < nargs; ++i) {
395 args[i].setExpand(false);
398 for (int i = 0; i < nargs; ++i) {
399 args[i].setExpand(true);
402 MathParInset::Metrics();
404 for (int i = 0; i < nargs; ++i) {
405 args[i].setExpand(expnd);
410 void MathMacroTemplate::update(MathMacro * macro)
412 int idx = (macro) ? macro->getArgumentIdx(): 0;
413 for (int i = 0; i < nargs; ++i) {
415 macro->setArgumentIdx(i);
416 args[i].SetData(macro->GetData());
417 MathedRowSt *row = macro->getRowSt();
418 args[i].setRowSt(row);
422 macro->setArgumentIdx(idx);
426 void MathMacroTemplate::WriteDef(ostream & os)
428 os << "\n\\newcommand{\\" << name << "}";
431 os << "[" << nargs << "]";
435 for (int i = 0; i < nargs; ++i) {
436 args[i].setExpand(false);
443 void MathMacroTemplate::WriteDef(string &file)
445 file += "\n\\newcommand{\\";
451 file += tostr(nargs);
457 for (int i = 0; i < nargs; ++i) {
458 args[i].setExpand(false);
465 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
471 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
477 MathParInset * MathMacroTemplate::getMacroPar(int i) const
479 return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
483 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
485 for (int i = 0; i < nargs; ++i) {
486 if (args[i].Inside(x, y)) {
494 /* -------------------------- MathMacroTable -----------------------*/
496 MathMacroTable::MathMacroTable(int n): max_macros(n)
498 macro_table = new MathMacroTemplateP[max_macros];
503 MathMacroTable::~MathMacroTable()
505 delete[] macro_table;
509 // The search is currently linear but will be binary or hash, later.
510 MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
512 for (int i = 0; i < num_macros; ++i) {
513 if (strcmp(name, macro_table[i]->GetName()) == 0)
514 return macro_table[i];
520 void MathMacroTable::addTemplate(MathMacroTemplate * m)
522 if (num_macros < max_macros)
523 macro_table[num_macros++] = m;
525 lyxerr << "Error (MathMacroTable::addTemplate): "
526 "Macro table exhausted!" << endl;
530 // All this stuff aparently leaks because it's created here and is not
531 // deleted never, but it have to live all the LyX sesion. OK, would not
532 // so hard to do it in the MacroTable destructor, but this doesn't harm
533 // seriously, so don't bother me with purify results here. ;-)
535 void MathMacroTable::builtinMacros()
538 MathParInset * inset;// *arg;
539 LyxArrayBase * array2;
543 lyxerr[Debug::MATHED] << "Building macros" << endl;
545 // This macro doesn't have arguments
546 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
548 LyxArrayBase * array = new LyxArrayBase; // this leaks
550 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
553 // These two are only while we are still with LyX 2.x
554 m = new MathMacroTemplate("emptyset"); // this leaks
556 array = new LyxArrayBase; // this leaks
558 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
561 m = new MathMacroTemplate("perp"); // this leaks
563 array = new LyxArrayBase; // this leaks
565 iter.Insert(LM_bot, LM_TC_BOP);
568 // binom has two arguments
569 m = new MathMacroTemplate("binom", 2);
571 array = new LyxArrayBase;
574 inset = new MathDelimInset('(', ')');
575 iter.Insert(inset, LM_TC_ACTIVE_INSET);
576 array = new LyxArrayBase;
578 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
579 iter.Insert(frac, LM_TC_ACTIVE_INSET);
580 inset->SetData(array);
581 array = new LyxArrayBase;
582 array2 = new LyxArrayBase;
584 iter.Insert(m->getMacroPar(0));
585 iter.SetData(array2);
586 iter.Insert(m->getMacroPar(1));
587 frac->SetData(array, array2);
590 // Cases has 1 argument
591 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
593 array = new LyxArrayBase; // this leaks
595 arg = new MathMatrixInset(2, 1); // this leaks
598 arg->SetAlign('c', "ll");
599 iter.Insert(arg, LM_TC_ACTIVE_INSET);
600 inset = new MathDelimInset('{', '.'); // this leaks
601 inset->SetData(array);
602 array = new LyxArrayBase; // this leaks
604 iter.Insert(inset, LM_TC_ACTIVE_INSET);
608 // the environment substack has 1 argument
609 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
611 arg = new MathMatrixInset(1, 1); // this leaks
613 arg->SetType(LM_OT_MACRO);
614 array = new LyxArrayBase; // this leaks
616 iter.Insert(arg, LM_TC_ACTIVE_INSET);
621 MathMacroTable MathMacroTable::mathMTable(255);
622 bool MathMacroTable::built = false;