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: 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"
35 ostream & operator<<(ostream & o, MathedTextCodes mtc)
40 enum MathedMacroFlag {
46 ostream & operator<<(ostream & o, MathedMacroFlag mmf)
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();
122 void MathMacro::draw(Painter & pain, int x, int y)
126 tmplate->update(this);
127 tmplate->SetStyle(size);
128 tmplate->draw(pain, x, y);
129 for (int i = 0; i < nargs; ++i)
130 tmplate->GetMacroXY(i, args[i].x, args[i].y);
134 int MathMacro::GetColumns()
136 return tmplate->getMacroPar(idx)->GetColumns();
140 void MathMacro::GetXY(int & x, int & y) const
142 x = args[idx].x; y = args[idx].y;
146 bool MathMacro::Permit(short f)
149 tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
153 void MathMacro::SetFocus(int x, int y)
155 tmplate->update(this);
156 tmplate->SetMacroFocus(idx, x, y);
160 void MathMacro::Write(ostream & os)
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) {
204 /*--------------- Macro argument -----------------------------------*/
206 MathMacroArgument::MathMacroArgument(int n)
210 SetType(LM_OT_MACRO_ARG);
214 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
217 MathParInset::draw(pain, x, baseline);
221 ost << '#' << number;
222 drawStr(pain, LM_TC_TEX, size, x, baseline,
223 reinterpret_cast<byte const *>(ost.str().c_str()), 2);
226 ostrstream ost(s, 3);
227 ost << '#' << number << '\0';
228 drawStr(pain, LM_TC_TEX, size, x, baseline,
229 reinterpret_cast<byte *>(ost.str()), 2);
235 void MathMacroArgument::Metrics()
238 MathParInset::Metrics();
242 ost << '#' << number;
243 width = mathed_string_width(LM_TC_TEX, size,
244 reinterpret_cast<byte const *>(ost.str().c_str()), 2);
245 mathed_string_height(LM_TC_TEX, size,
246 reinterpret_cast<byte const *>(ost.str().c_str()),
250 ostrstream ost(s, 3);
251 ost << '#' << number << '\0';
252 width = mathed_string_width(LM_TC_TEX, size,
253 reinterpret_cast<byte *>
255 mathed_string_height(LM_TC_TEX, size,
256 reinterpret_cast<byte *>(ost.str()),
263 void MathMacroArgument::Write(ostream & os)
266 MathParInset::Write(os);
268 os << '#' << number << ' ';
273 /* --------------------- MathMacroTemplate ---------------------------*/
275 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
276 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
277 flags(flg), nargs(na)
280 tcode = LM_TC_ACTIVE_INSET;
281 args = new MathMacroArgument[nargs];
282 for (int i = 0; i < nargs; ++i) {
283 args[i].setNumber(i + 1);
290 MathMacroTemplate::~MathMacroTemplate()
292 // prevent to delete already deleted objects
293 for (int i = 0; i < nargs; ++i) {
300 void MathMacroTemplate::setEditMode(bool ed)
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);
316 void MathMacroTemplate::draw(Painter & pain, int x, int y)
319 bool expnd = (nargs > 0) ? args[0].getExpand(): false;
320 if (flags & MMF_Edit) {
321 for (int i = 0; i < nargs; ++i) {
322 args[i].setExpand(false);
326 for (int i = 0; i < nargs; ++i) {
327 args[i].setExpand(true);
331 MathParInset::draw(pain, x, y);
334 for (int i = 0; i < nargs; ++i) {
335 args[i].setExpand(expnd);
340 void MathMacroTemplate::Metrics()
342 bool expnd = (nargs>0) ? args[0].getExpand(): false;
344 if (flags & MMF_Edit) {
345 for (int i = 0; i < nargs; ++i) {
346 args[i].setExpand(false);
349 for (int i = 0; i < nargs; ++i) {
350 args[i].setExpand(true);
353 MathParInset::Metrics();
355 for (int i = 0; i < nargs; ++i) {
356 args[i].setExpand(expnd);
361 void MathMacroTemplate::update(MathMacro * macro)
363 int idx = (macro) ? macro->getArgumentIdx(): 0;
364 for (int i = 0; i < nargs; ++i) {
366 macro->setArgumentIdx(i);
367 args[i].SetData(macro->GetData());
368 MathedRowSt *row = macro->getRowSt();
369 args[i].setRowSt(row);
373 macro->setArgumentIdx(idx);
377 void MathMacroTemplate::WriteDef(ostream & os)
379 os << "\n\\newcommand{\\" << name << "}";
382 os << "[" << nargs << "]";
386 for (int i = 0; i < nargs; ++i) {
387 args[i].setExpand(false);
394 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) ? static_cast<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 << "Error (MathMacroTable::addTemplate): "
455 "Macro table exhausted!" << endl;
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()
467 MathParInset * inset;// *arg;
468 LyxArrayBase * array2;
472 lyxerr[Debug::MATHED] << "Building macros" << endl;
474 // This macro doesn't have arguments
475 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
477 LyxArrayBase * array = new LyxArrayBase; // this leaks
479 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
482 // These two are only while we are still with LyX 2.x
483 m = new MathMacroTemplate("emptyset"); // this leaks
485 array = new LyxArrayBase; // this leaks
487 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
490 m = new MathMacroTemplate("perp"); // this leaks
492 array = new LyxArrayBase; // this leaks
494 iter.Insert(LM_bot, LM_TC_BOP);
497 // binom has two arguments
498 m = new MathMacroTemplate("binom", 2);
500 array = new LyxArrayBase;
503 inset = new MathDelimInset('(', ')');
504 iter.Insert(inset, LM_TC_ACTIVE_INSET);
505 array = new LyxArrayBase;
507 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
508 iter.Insert(frac, LM_TC_ACTIVE_INSET);
509 inset->SetData(array);
510 array = new LyxArrayBase;
511 array2 = new LyxArrayBase;
513 iter.Insert(m->getMacroPar(0));
514 iter.SetData(array2);
515 iter.Insert(m->getMacroPar(1));
516 frac->SetData(array, array2);
519 // Cases has 1 argument
520 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
522 array = new LyxArrayBase; // this leaks
524 arg = new MathMatrixInset(2, 1); // this leaks
527 arg->SetAlign('c', "ll");
528 iter.Insert(arg, LM_TC_ACTIVE_INSET);
529 inset = new MathDelimInset('{', '.'); // this leaks
530 inset->SetData(array);
531 array = new LyxArrayBase; // this leaks
533 iter.Insert(inset, LM_TC_ACTIVE_INSET);
537 // the environment substack has 1 argument
538 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
540 arg = new MathMatrixInset(1, 1); // this leaks
542 arg->SetType(LM_OT_MACRO);
543 array = new LyxArrayBase; // this leaks
545 iter.Insert(arg, LM_TC_ACTIVE_INSET);
550 MathMacroTable MathMacroTable::mathMTable(255);
551 bool MathMacroTable::built = false;