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 int mathed_string_width(short type, int style, byte const* s, int ls);
51 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
54 MathMacro::MathMacro(MathMacroTemplate* t):
55 MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
57 nargs = tmplate->getNoArgs();
58 tcode = tmplate->getTCode();
59 args = new MacroArgumentBase[nargs];
60 for (int i = 0; i < nargs; ++i) {
61 // if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
62 // args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
65 /* int k = tmplate->getMacroPar(i)->GetColumns();
67 args[i].array = new LyxArrayBase;
68 for (int j= 0; j<k-1; ++j) args[i].array->Insert(j, LM_TC_TAB);
72 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 return new MathMacro(this);
110 void MathMacro::Metrics()
113 tmplate->update(this);
115 width = tmplate->Width();
116 ascent = tmplate->Ascent();
117 descent = tmplate->Descent();
121 void MathMacro::draw(Painter & pain, int x, int y)
125 tmplate->update(this);
126 tmplate->SetStyle(size);
127 tmplate->draw(pain, x, y);
128 for (int i = 0; i < nargs; ++i)
129 tmplate->GetMacroXY(i, args[i].x, args[i].y);
133 int MathMacro::GetColumns()
135 return tmplate->getMacroPar(idx)->GetColumns();
139 void MathMacro::GetXY(int & x, int & y) const
141 x = args[idx].x; y = args[idx].y;
145 bool MathMacro::Permit(short f)
148 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(ostream & os)
161 #ifdef USE_OSTREAM_ONLY
162 if (tmplate->flags & MMF_Exp) {
163 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
164 << ' ' << MMF_Exp << endl;
165 tmplate->update(this);
168 if (tmplate->flags & MMF_Env) {
181 if (!(tmplate->flags & MMF_Env) && nargs > 0)
184 for (int i = 0; i < nargs; ++i) {
185 array = args[i].array;
186 MathParInset::Write(os);
190 if (tmplate->flags & MMF_Env) {
203 MathMacro::Write(output);
209 #ifndef USE_OSTREAM_ONLY
210 void MathMacro::Write(string &file)
212 if (tmplate->flags & MMF_Exp) {
213 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
214 << ' ' << MMF_Exp << endl;
215 tmplate->update(this);
216 tmplate->Write(file);
218 if (tmplate->flags & MMF_Env) {
232 if (!(tmplate->flags & MMF_Env) && nargs > 0)
235 for (int i = 0; i < nargs; ++i) {
236 array = args[i].array;
237 MathParInset::Write(file);
241 if (tmplate->flags & MMF_Env) {
256 /*--------------- Macro argument -----------------------------------*/
258 MathMacroArgument::MathMacroArgument(int n)
262 SetType(LM_OT_MACRO_ARG);
266 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
269 MathParInset::draw(pain, x, baseline);
273 ost << '#' << number;
274 drawStr(pain, LM_TC_TEX, size, x, baseline, ost.str().c_str(), 2);
277 ostrstream ost(s, 3);
278 ost << '#' << number << '\0';
279 drawStr(pain, LM_TC_TEX, size, x, baseline,
280 reinterpret_cast<unsigned char*>(ost.str()), 2);
286 void MathMacroArgument::Metrics()
289 MathParInset::Metrics();
293 ost << '#' << number;
294 width = mathed_string_width(LM_TC_TEX, size, ost.str().c_str(), 2);
295 mathed_string_height(LM_TC_TEX, size,
296 ost.str().c_str(), 2, ascent, descent);
299 ostrstream ost(s, 3);
300 ost << '#' << number << '\0';
301 width = mathed_string_width(LM_TC_TEX, size,
302 reinterpret_cast<unsigned char*>
304 mathed_string_height(LM_TC_TEX, size,
305 reinterpret_cast<unsigned char*>(ost.str()),
312 void MathMacroArgument::Write(ostream & os)
314 #ifdef USE_OSTREAM_ONLY
316 MathParInset::Write(os);
318 os << '#' << number << ' ';
322 MathMacroArgument::Write(output);
328 #ifndef USE_OSTREAM_ONLY
329 void MathMacroArgument::Write(string & file)
332 MathParInset::Write(file);
335 file += tostr(number);
342 /* --------------------- MathMacroTemplate ---------------------------*/
344 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
345 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
346 flags(flg), nargs(na)
349 tcode = LM_TC_ACTIVE_INSET;
350 args = new MathMacroArgument[nargs];
351 for (int i = 0; i < nargs; ++i) {
352 args[i].setNumber(i + 1);
359 MathMacroTemplate::~MathMacroTemplate()
361 // prevent to delete already deleted objects
362 for (int i = 0; i < nargs; ++i) {
369 void MathMacroTemplate::setEditMode(bool ed)
373 for (int i = 0; i < nargs; ++i) {
374 args[i].setExpand(false);
378 for (int i = 0; i < nargs; ++i) {
379 args[i].setExpand(true);
385 void MathMacroTemplate::draw(Painter & pain, int x, int y)
388 bool expnd = (nargs > 0) ? args[0].getExpand(): false;
389 if (flags & MMF_Edit) {
390 for (int i = 0; i < nargs; ++i) {
391 args[i].setExpand(false);
395 for (int i = 0; i < nargs; ++i) {
396 args[i].setExpand(true);
400 MathParInset::draw(pain, x, y);
403 for (int i = 0; i < nargs; ++i) {
404 args[i].setExpand(expnd);
409 void MathMacroTemplate::Metrics()
411 bool expnd = (nargs>0) ? args[0].getExpand(): false;
413 if (flags & MMF_Edit) {
414 for (int i = 0; i < nargs; ++i) {
415 args[i].setExpand(false);
418 for (int i = 0; i < nargs; ++i) {
419 args[i].setExpand(true);
422 MathParInset::Metrics();
424 for (int i = 0; i < nargs; ++i) {
425 args[i].setExpand(expnd);
430 void MathMacroTemplate::update(MathMacro * macro)
432 int idx = (macro) ? macro->getArgumentIdx(): 0;
433 for (int i = 0; i < nargs; ++i) {
435 macro->setArgumentIdx(i);
436 args[i].SetData(macro->GetData());
437 MathedRowSt *row = macro->getRowSt();
438 args[i].setRowSt(row);
442 macro->setArgumentIdx(idx);
446 void MathMacroTemplate::WriteDef(ostream & os)
448 os << "\n\\newcommand{\\" << name << "}";
451 os << "[" << nargs << "]";
455 for (int i = 0; i < nargs; ++i) {
456 args[i].setExpand(false);
463 #ifndef USE_OSTREAM_ONLY
464 void MathMacroTemplate::WriteDef(string & file)
466 file += "\n\\newcommand{\\";
472 file += tostr(nargs);
478 for (int i = 0; i < nargs; ++i) {
479 args[i].setExpand(false);
487 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
493 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
499 MathParInset * MathMacroTemplate::getMacroPar(int i) const
501 return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
505 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
507 for (int i = 0; i < nargs; ++i) {
508 if (args[i].Inside(x, y)) {
516 /* -------------------------- MathMacroTable -----------------------*/
518 MathMacroTable::MathMacroTable(int n) : max_macros(n)
520 macro_table = new MathMacroTemplateP[max_macros];
525 MathMacroTable::~MathMacroTable()
527 delete[] macro_table;
531 // The search is currently linear but will be binary or hash, later.
532 MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
534 for (int i = 0; i < num_macros; ++i) {
535 if (strcmp(name, macro_table[i]->GetName()) == 0)
536 return macro_table[i];
542 void MathMacroTable::addTemplate(MathMacroTemplate * m)
544 if (num_macros < max_macros)
545 macro_table[num_macros++] = m;
547 lyxerr << "Error (MathMacroTable::addTemplate): "
548 "Macro table exhausted!" << endl;
552 // All this stuff aparently leaks because it's created here and is not
553 // deleted never, but it have to live all the LyX sesion. OK, would not
554 // so hard to do it in the MacroTable destructor, but this doesn't harm
555 // seriously, so don't bother me with purify results here. ;-)
557 void MathMacroTable::builtinMacros()
560 MathParInset * inset;// *arg;
561 LyxArrayBase * array2;
565 lyxerr[Debug::MATHED] << "Building macros" << endl;
567 // This macro doesn't have arguments
568 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
570 LyxArrayBase * array = new LyxArrayBase; // this leaks
572 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
575 // These two are only while we are still with LyX 2.x
576 m = new MathMacroTemplate("emptyset"); // this leaks
578 array = new LyxArrayBase; // this leaks
580 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
583 m = new MathMacroTemplate("perp"); // this leaks
585 array = new LyxArrayBase; // this leaks
587 iter.Insert(LM_bot, LM_TC_BOP);
590 // binom has two arguments
591 m = new MathMacroTemplate("binom", 2);
593 array = new LyxArrayBase;
596 inset = new MathDelimInset('(', ')');
597 iter.Insert(inset, LM_TC_ACTIVE_INSET);
598 array = new LyxArrayBase;
600 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
601 iter.Insert(frac, LM_TC_ACTIVE_INSET);
602 inset->SetData(array);
603 array = new LyxArrayBase;
604 array2 = new LyxArrayBase;
606 iter.Insert(m->getMacroPar(0));
607 iter.SetData(array2);
608 iter.Insert(m->getMacroPar(1));
609 frac->SetData(array, array2);
612 // Cases has 1 argument
613 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
615 array = new LyxArrayBase; // this leaks
617 arg = new MathMatrixInset(2, 1); // this leaks
620 arg->SetAlign('c', "ll");
621 iter.Insert(arg, LM_TC_ACTIVE_INSET);
622 inset = new MathDelimInset('{', '.'); // this leaks
623 inset->SetData(array);
624 array = new LyxArrayBase; // this leaks
626 iter.Insert(inset, LM_TC_ACTIVE_INSET);
630 // the environment substack has 1 argument
631 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
633 arg = new MathMatrixInset(1, 1); // this leaks
635 arg->SetType(LM_OT_MACRO);
636 array = new LyxArrayBase; // this leaks
638 iter.Insert(arg, LM_TC_ACTIVE_INSET);
643 MathMacroTable MathMacroTable::mathMTable(255);
644 bool MathMacroTable::built = false;