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)
51 extern GC mathGC, mathFrameGC, latexGC;
53 extern int mathed_string_width(short type, int style, byte const* s, int ls);
54 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
57 MathMacro::MathMacro(MathMacroTemplate* t):
58 MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
60 nargs = tmplate->getNoArgs();
61 tcode = tmplate->getTCode();
62 args = new MacroArgumentBase[nargs];
63 for (int i = 0; i < nargs; ++i) {
64 // if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
65 // args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
68 /* int k = tmplate->getMacroPar(i)->GetColumns();
70 args[i].array = new LyxArrayBase;
71 for (int j= 0; j<k-1; ++j) args[i].array->Insert(j, LM_TC_TAB);
75 SetName(tmplate->GetName());
79 MathMacro::MathMacro(MathMacro * m):
80 MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
83 nargs = tmplate->getNoArgs();
84 tcode = tmplate->getTCode();
85 args = new MacroArgumentBase[nargs];
87 SetName(tmplate->GetName());
88 for (int i = 0; i < tmplate->nargs; ++i) {
90 MathedIter it(m->GetData());
91 args[i].row = m->args[i].row;
92 args[i].array = it.Copy();
96 MathMacro::~MathMacro()
98 for (idx = 0; idx < nargs; ++idx) {
99 MathedIter it(args[idx].array);
101 delete args[idx].row;
107 MathedInset * MathMacro::Clone()
109 return new MathMacro(this);
113 void MathMacro::Metrics()
116 tmplate->update(this);
118 width = tmplate->Width();
119 ascent = tmplate->Ascent();
120 descent = tmplate->Descent();
125 void MathMacro::draw(Painter & pain, int x, int y)
129 tmplate->update(this);
130 tmplate->SetStyle(size);
132 tmplate->draw(pain, x, y);
133 for (int i = 0; i < nargs; ++i)
134 tmplate->GetMacroXY(i, args[i].x, args[i].y);
137 void MathMacro::Draw(int x, int y)
141 tmplate->update(this);
142 tmplate->SetStyle(size);
145 for (int i = 0; i < nargs; ++i)
146 tmplate->GetMacroXY(i, args[i].x, args[i].y);
151 int MathMacro::GetColumns()
153 return tmplate->getMacroPar(idx)->GetColumns();
157 void MathMacro::GetXY(int & x, int & y) const
159 x = args[idx].x; y = args[idx].y;
163 bool MathMacro::Permit(short f)
166 tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
170 void MathMacro::SetFocus(int x, int y)
172 tmplate->update(this);
173 tmplate->SetMacroFocus(idx, x, y);
177 void MathMacro::Write(ostream & os)
180 MathMacro::Write(output);
185 void MathMacro::Write(string &file)
187 if (tmplate->flags & MMF_Exp) {
188 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
189 << ' ' << MMF_Exp << endl;
190 tmplate->update(this);
191 tmplate->Write(file);
193 if (tmplate->flags & MMF_Env) {
207 if (!(tmplate->flags & MMF_Env) && nargs > 0)
210 for (int i = 0; i < nargs; ++i) {
211 array = args[i].array;
212 MathParInset::Write(file);
216 if (tmplate->flags & MMF_Env) {
231 /*--------------- Macro argument -----------------------------------*/
233 MathMacroArgument::MathMacroArgument(int n)
237 SetType(LM_OT_MACRO_ARG);
242 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
245 MathParInset::draw(pain, x, baseline);
248 sprintf(reinterpret_cast<char*>(s), "#%d", number);
249 drawStr(pain, LM_TC_TEX, size, x, baseline, &s[0], 2);
253 void MathMacroArgument::Draw(int x, int baseline)
256 MathParInset::Draw(x, baseline);
259 sprintf(reinterpret_cast<char*>(s), "#%d", number);
260 drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
266 void MathMacroArgument::Metrics()
269 MathParInset::Metrics();
272 sprintf(reinterpret_cast<char*>(s), "#%d", number);
273 width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
274 mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
279 void MathMacroArgument::Write(ostream & os)
282 MathMacroArgument::Write(output);
287 void MathMacroArgument::Write(string & file)
290 MathParInset::Write(file);
293 file += tostr(number);
299 /* --------------------- MathMacroTemplate ---------------------------*/
301 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
302 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
303 flags(flg), nargs(na)
306 tcode = LM_TC_ACTIVE_INSET;
307 args = new MathMacroArgument[nargs];
308 for (int i = 0; i < nargs; ++i) {
309 args[i].setNumber(i + 1);
316 MathMacroTemplate::~MathMacroTemplate()
318 // prevent to delete already deleted objects
319 for (int i = 0; i < nargs; ++i) {
326 void MathMacroTemplate::setEditMode(bool ed)
330 for (int i = 0; i < nargs; ++i) {
331 args[i].setExpand(false);
335 for (int i = 0; i < nargs; ++i) {
336 args[i].setExpand(true);
343 void MathMacroTemplate::draw(Painter & pain, int x, int y)
346 bool expnd = (nargs > 0) ? args[0].getExpand(): false;
347 if (flags & MMF_Edit) {
348 for (int i = 0; i < nargs; ++i) {
349 args[i].setExpand(false);
353 for (int i = 0; i < nargs; ++i) {
354 args[i].setExpand(true);
358 MathParInset::draw(pain, x, y);
361 for (int i = 0; i < nargs; ++i) {
362 args[i].setExpand(expnd);
366 void MathMacroTemplate::Draw(int x, int y)
369 bool expnd = (nargs>0) ? args[0].getExpand(): false;
370 if (flags & MMF_Edit) {
371 for (int i = 0; i < nargs; ++i) {
372 args[i].setExpand(false);
376 for (int i = 0; i < nargs; ++i) {
377 args[i].setExpand(true);
381 MathParInset::Draw(x, y);
384 for (int i = 0; i < nargs; ++i) {
385 args[i].setExpand(expnd);
391 void MathMacroTemplate::Metrics()
393 bool expnd = (nargs>0) ? args[0].getExpand(): false;
395 if (flags & MMF_Edit) {
396 for (int i = 0; i < nargs; ++i) {
397 args[i].setExpand(false);
400 for (int i = 0; i < nargs; ++i) {
401 args[i].setExpand(true);
404 MathParInset::Metrics();
406 for (int i = 0; i < nargs; ++i) {
407 args[i].setExpand(expnd);
412 void MathMacroTemplate::update(MathMacro * macro)
414 int idx = (macro) ? macro->getArgumentIdx(): 0;
415 for (int i = 0; i < nargs; ++i) {
417 macro->setArgumentIdx(i);
418 args[i].SetData(macro->GetData());
419 MathedRowSt *row = macro->getRowSt();
420 args[i].setRowSt(row);
424 macro->setArgumentIdx(idx);
428 void MathMacroTemplate::WriteDef(ostream & os)
430 os << "\n\\newcommand{\\" << name << "}";
433 os << "[" << nargs << "]";
437 for (int i = 0; i < nargs; ++i) {
438 args[i].setExpand(false);
445 void MathMacroTemplate::WriteDef(string &file)
447 file += "\n\\newcommand{\\";
453 file += tostr(nargs);
459 for (int i = 0; i < nargs; ++i) {
460 args[i].setExpand(false);
467 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
473 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
479 MathParInset * MathMacroTemplate::getMacroPar(int i) const
481 return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
485 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
487 for (int i = 0; i < nargs; ++i) {
488 if (args[i].Inside(x, y)) {
496 /* -------------------------- MathMacroTable -----------------------*/
498 MathMacroTable::MathMacroTable(int n): max_macros(n)
500 macro_table = new MathMacroTemplateP[max_macros];
505 MathMacroTable::~MathMacroTable()
507 delete[] macro_table;
511 // The search is currently linear but will be binary or hash, later.
512 MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
514 for (int i = 0; i < num_macros; ++i) {
515 if (strcmp(name, macro_table[i]->GetName()) == 0)
516 return macro_table[i];
522 void MathMacroTable::addTemplate(MathMacroTemplate * m)
524 if (num_macros < max_macros)
525 macro_table[num_macros++] = m;
527 lyxerr << "Error (MathMacroTable::addTemplate): "
528 "Macro table exhausted!" << endl;
532 // All this stuff aparently leaks because it's created here and is not
533 // deleted never, but it have to live all the LyX sesion. OK, would not
534 // so hard to do it in the MacroTable destructor, but this doesn't harm
535 // seriously, so don't bother me with purify results here. ;-)
537 void MathMacroTable::builtinMacros()
540 MathParInset * inset;// *arg;
541 LyxArrayBase * array2;
545 lyxerr[Debug::MATHED] << "Building macros" << endl;
547 // This macro doesn't have arguments
548 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
550 LyxArrayBase * array = new LyxArrayBase; // this leaks
552 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
555 // These two are only while we are still with LyX 2.x
556 m = new MathMacroTemplate("emptyset"); // this leaks
558 array = new LyxArrayBase; // this leaks
560 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
563 m = new MathMacroTemplate("perp"); // this leaks
565 array = new LyxArrayBase; // this leaks
567 iter.Insert(LM_bot, LM_TC_BOP);
570 // binom has two arguments
571 m = new MathMacroTemplate("binom", 2);
573 array = new LyxArrayBase;
576 inset = new MathDelimInset('(', ')');
577 iter.Insert(inset, LM_TC_ACTIVE_INSET);
578 array = new LyxArrayBase;
580 MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
581 iter.Insert(frac, LM_TC_ACTIVE_INSET);
582 inset->SetData(array);
583 array = new LyxArrayBase;
584 array2 = new LyxArrayBase;
586 iter.Insert(m->getMacroPar(0));
587 iter.SetData(array2);
588 iter.Insert(m->getMacroPar(1));
589 frac->SetData(array, array2);
592 // Cases has 1 argument
593 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
595 array = new LyxArrayBase; // this leaks
597 arg = new MathMatrixInset(2, 1); // this leaks
600 arg->SetAlign('c', "ll");
601 iter.Insert(arg, LM_TC_ACTIVE_INSET);
602 inset = new MathDelimInset('{', '.'); // this leaks
603 inset->SetData(array);
604 array = new LyxArrayBase; // this leaks
606 iter.Insert(inset, LM_TC_ACTIVE_INSET);
610 // the environment substack has 1 argument
611 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
613 arg = new MathMatrixInset(1, 1); // this leaks
615 arg->SetType(LM_OT_MACRO);
616 array = new LyxArrayBase; // this leaks
618 iter.Insert(arg, LM_TC_ACTIVE_INSET);
623 MathMacroTable MathMacroTable::mathMTable(255);
624 bool MathMacroTable::built = false;