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"
36 ostream & operator<<(ostream & o, MathedTextCodes mtc)
41 enum MathedMacroFlag {
47 ostream & operator<<(ostream & o, MathedMacroFlag mmf)
52 extern int mathed_string_width(short type, int style, byte const* s, int ls);
53 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
56 MathMacro::MathMacro(MathMacroTemplate* t):
57 MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
59 nargs = tmplate->getNoArgs();
60 tcode = tmplate->getTCode();
61 args = new MacroArgumentBase[nargs];
62 for (int i = 0; i < nargs; ++i) {
63 // if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
64 // args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
67 /* int k = tmplate->getMacroPar(i)->GetColumns();
69 args[i].array = new LyxArrayBase;
70 for (int j= 0; j<k-1; ++j) args[i].array->Insert(j, LM_TC_TAB);
74 SetName(tmplate->GetName());
78 MathMacro::MathMacro(MathMacro * m):
79 MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
82 nargs = tmplate->getNoArgs();
83 tcode = tmplate->getTCode();
84 args = new MacroArgumentBase[nargs];
86 SetName(tmplate->GetName());
87 for (int i = 0; i < tmplate->nargs; ++i) {
89 MathedIter it(m->GetData());
90 args[i].row = m->args[i].row;
91 args[i].array = it.Copy();
95 MathMacro::~MathMacro()
97 for (idx = 0; idx < nargs; ++idx) {
98 MathedIter it(args[idx].array);
100 delete args[idx].row;
106 MathedInset * MathMacro::Clone()
108 return new MathMacro(this);
112 void MathMacro::Metrics()
115 tmplate->update(this);
116 tmplate->SetStyle(size);
118 width = tmplate->Width();
119 ascent = tmplate->Ascent();
120 descent = tmplate->Descent();
124 void MathMacro::draw(Painter & pain, int x, int y)
128 tmplate->update(this);
129 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);
136 int MathMacro::GetColumns()
138 return tmplate->getMacroPar(idx)->GetColumns();
142 void MathMacro::GetXY(int & x, int & y) const
144 x = args[idx].x; y = args[idx].y;
148 bool MathMacro::Permit(short f)
151 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(ostream & os, bool fragile)
164 if (tmplate->flags & MMF_Exp) {
165 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
166 << ' ' << MMF_Exp << endl;
167 tmplate->update(this);
168 tmplate->Write(os, fragile);
170 if (tmplate->flags & MMF_Env) {
183 if (!(tmplate->flags & MMF_Env) && nargs > 0)
186 for (int i = 0; i < nargs; ++i) {
187 array = args[i].array;
188 MathParInset::Write(os, fragile);
192 if (tmplate->flags & MMF_Env) {
206 /*--------------- Macro argument -----------------------------------*/
208 MathMacroArgument::MathMacroArgument(int n)
212 SetType(LM_OT_MACRO_ARG);
216 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
219 MathParInset::draw(pain, x, baseline);
222 std::ostringstream ost;
223 ost << '#' << number;
224 drawStr(pain, LM_TC_TEX, size, x, baseline,
225 reinterpret_cast<byte const *>(ost.str().c_str()), 2);
228 ostrstream ost(s, 3);
229 ost << '#' << number << '\0';
230 drawStr(pain, LM_TC_TEX, size, x, baseline,
231 reinterpret_cast<byte *>(ost.str()), 2);
237 void MathMacroArgument::Metrics()
240 MathParInset::Metrics();
243 std::ostringstream ost;
244 ost << '#' << number;
245 width = mathed_string_width(LM_TC_TEX, size,
246 reinterpret_cast<byte const *>(ost.str().c_str()), 2);
247 mathed_string_height(LM_TC_TEX, size,
248 reinterpret_cast<byte const *>(ost.str().c_str()),
252 ostrstream ost(s, 3);
253 ost << '#' << number << '\0';
254 width = mathed_string_width(LM_TC_TEX, size,
255 reinterpret_cast<byte *>
257 mathed_string_height(LM_TC_TEX, size,
258 reinterpret_cast<byte *>(ost.str()),
265 void MathMacroArgument::Write(ostream & os, bool fragile)
268 MathParInset::Write(os, fragile);
270 os << '#' << number << ' ';
275 /* --------------------- MathMacroTemplate ---------------------------*/
277 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
278 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
279 flags(flg), nargs(na)
282 tcode = LM_TC_ACTIVE_INSET;
283 args = new MathMacroArgument[nargs];
284 for (int i = 0; i < nargs; ++i) {
285 args[i].setNumber(i + 1);
292 MathMacroTemplate::~MathMacroTemplate()
294 // prevent to delete already deleted objects
295 for (int i = 0; i < nargs; ++i) {
302 void MathMacroTemplate::setEditMode(bool ed)
306 for (int i = 0; i < nargs; ++i) {
307 args[i].setExpand(false);
311 for (int i = 0; i < nargs; ++i) {
312 args[i].setExpand(true);
318 void MathMacroTemplate::draw(Painter & pain, int x, int y)
321 bool expnd = (nargs > 0) ? args[0].getExpand(): false;
322 if (flags & MMF_Edit) {
323 for (int i = 0; i < nargs; ++i) {
324 args[i].setExpand(false);
328 for (int i = 0; i < nargs; ++i) {
329 args[i].setExpand(true);
333 MathParInset::draw(pain, x, y);
336 for (int i = 0; i < nargs; ++i) {
337 args[i].setExpand(expnd);
342 void MathMacroTemplate::Metrics()
344 bool expnd = (nargs>0) ? args[0].getExpand(): false;
346 if (flags & MMF_Edit) {
347 for (int i = 0; i < nargs; ++i) {
348 args[i].setExpand(false);
351 for (int i = 0; i < nargs; ++i) {
352 args[i].setExpand(true);
355 MathParInset::Metrics();
357 for (int i = 0; i < nargs; ++i) {
358 args[i].setExpand(expnd);
363 void MathMacroTemplate::update(MathMacro * macro)
365 int idx = (macro) ? macro->getArgumentIdx(): 0;
366 for (int i = 0; i < nargs; ++i) {
368 macro->setArgumentIdx(i);
369 args[i].SetData(macro->GetData());
370 MathedRowSt *row = macro->getRowSt();
371 args[i].setRowSt(row);
375 macro->setArgumentIdx(idx);
379 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
381 os << "\n\\newcommand{\\" << name << "}";
384 os << "[" << nargs << "]";
388 for (int i = 0; i < nargs; ++i) {
389 args[i].setExpand(false);
396 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
402 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
408 MathParInset * MathMacroTemplate::getMacroPar(int i) const
410 return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
414 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
416 for (int i = 0; i < nargs; ++i) {
417 if (args[i].Inside(x, y)) {
425 /* -------------------------- MathMacroTable -----------------------*/
427 MathMacroTable::MathMacroTable(int n) : max_macros(n)
429 macro_table = new MathMacroTemplateP[max_macros];
434 MathMacroTable::~MathMacroTable()
436 delete[] macro_table;
440 // The search is currently linear but will be binary or hash, later.
441 MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
443 for (int i = 0; i < num_macros; ++i) {
444 if (strcmp(name, macro_table[i]->GetName()) == 0)
445 return macro_table[i];
451 void MathMacroTable::addTemplate(MathMacroTemplate * m)
453 if (num_macros < max_macros)
454 macro_table[num_macros++] = m;
456 lyxerr << "Error (MathMacroTable::addTemplate): "
457 "Macro table exhausted!" << endl;
461 // All this stuff aparently leaks because it's created here and is not
462 // deleted never, but it have to live all the LyX sesion. OK, would not
463 // so hard to do it in the MacroTable destructor, but this doesn't harm
464 // seriously, so don't bother me with purify results here. ;-)
466 void MathMacroTable::builtinMacros()
469 MathParInset * inset;// *arg;
470 LyxArrayBase * array2;
474 lyxerr[Debug::MATHED] << "Building macros" << endl;
476 // This macro doesn't have arguments
477 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
479 LyxArrayBase * array = new LyxArrayBase; // this leaks
481 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
484 // These two are only while we are still with LyX 2.x
485 m = new MathMacroTemplate("emptyset"); // this leaks
487 array = new LyxArrayBase; // this leaks
489 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
492 m = new MathMacroTemplate("perp"); // this leaks
494 array = new LyxArrayBase; // this leaks
496 iter.Insert(LM_bot, LM_TC_BOP);
499 // binom has two arguments
500 m = new MathMacroTemplate("binom", 2);
502 array = new LyxArrayBase;
505 inset = new MathDelimInset('(', ')');
506 iter.Insert(inset, LM_TC_ACTIVE_INSET);
507 array = new LyxArrayBase;
509 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
510 iter.Insert(frac, LM_TC_ACTIVE_INSET);
511 inset->SetData(array);
512 array = new LyxArrayBase;
513 array2 = new LyxArrayBase;
515 iter.Insert(m->getMacroPar(0));
516 iter.SetData(array2);
517 iter.Insert(m->getMacroPar(1));
518 frac->SetData(array, array2);
521 // Cases has 1 argument
522 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
524 array = new LyxArrayBase; // this leaks
526 arg = new MathMatrixInset(2, 1); // this leaks
529 arg->SetAlign('c', "ll");
530 iter.Insert(arg, LM_TC_ACTIVE_INSET);
531 inset = new MathDelimInset('{', '.'); // this leaks
532 inset->SetData(array);
533 array = new LyxArrayBase; // this leaks
535 iter.Insert(inset, LM_TC_ACTIVE_INSET);
539 // the environment substack has 1 argument
540 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
542 arg = new MathMatrixInset(1, 1); // this leaks
544 arg->SetType(LM_OT_MACRO);
545 array = new LyxArrayBase; // this leaks
547 iter.Insert(arg, LM_TC_ACTIVE_INSET);
552 MathMacroTable MathMacroTable::mathMTable(255);
553 bool MathMacroTable::built = false;