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
26 #include "math_macro.h"
28 #include "math_iter.h"
29 #include "math_inset.h"
30 #include "math_accentinset.h"
31 #include "math_deliminset.h"
32 #include "math_fracinset.h"
33 #include "math_rowst.h"
34 #include "support/lstrings.h"
40 ostream & operator<<(ostream & o, MathedTextCodes mtc)
45 enum MathedMacroFlag {
51 ostream & operator<<(ostream & o, MathedMacroFlag mmf)
56 extern int mathed_string_width(short type, int style, string const & s);
57 extern int mathed_string_height(short, int, string const &, int &, int &);
60 MathMacro::MathMacro(MathMacroTemplate* t):
61 MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
63 nargs = tmplate->getNoArgs();
64 tcode = tmplate->getTCode();
66 for (int i = 0; i < nargs; ++i) {
70 SetName(tmplate->GetName());
74 MathMacro::MathMacro(MathMacro * m):
75 MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
78 nargs = tmplate->getNoArgs();
79 tcode = tmplate->getTCode();
82 SetName(tmplate->GetName());
83 for (int i = 0; i < tmplate->nargs; ++i) {
85 MathedIter it(m->GetData());
86 args_[i].row = m->args_[i].row;
87 args_[i].array = it.Copy();
91 MathMacro::~MathMacro()
93 for (idx = 0; idx < nargs; ++idx) {
94 MathedIter it(args_[idx].array);
96 delete args_[idx].row;
101 MathedInset * MathMacro::Clone()
103 return new MathMacro(this);
107 void MathMacro::Metrics()
110 tmplate->update(this);
111 tmplate->SetStyle(size);
113 width = tmplate->Width();
114 ascent = tmplate->Ascent();
115 descent = tmplate->Descent();
119 void MathMacro::draw(Painter & pain, int x, int y)
123 tmplate->update(this);
124 tmplate->SetStyle(size);
125 tmplate->draw(pain, x, y);
126 for (int i = 0; i < nargs; ++i) {
127 tmplate->GetMacroXY(i, args_[i].x, args_[i].y);
132 int MathMacro::GetColumns() const
134 return tmplate->getMacroPar(idx)->GetColumns();
138 void MathMacro::GetXY(int & x, int & y) const
140 x = args_[idx].x; y = args_[idx].y;
144 bool MathMacro::Permit(short f) const
147 tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
151 void MathMacro::SetFocus(int x, int y)
153 tmplate->update(this);
154 tmplate->SetMacroFocus(idx, x, y);
158 void MathMacro::Write(ostream & os, bool fragile)
160 if (tmplate->flags & MMF_Exp) {
161 lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
162 << ' ' << MMF_Exp << endl;
163 tmplate->update(this);
164 tmplate->Write(os, fragile);
166 if (tmplate->flags & MMF_Env) {
179 if (!(tmplate->flags & MMF_Env) && nargs > 0)
182 for (int i = 0; i < nargs; ++i) {
183 array = args_[i].array;
184 MathParInset::Write(os, fragile);
188 if (tmplate->flags & MMF_Env) {
202 /*--------------- Macro argument -----------------------------------*/
204 MathMacroArgument::MathMacroArgument(int n)
208 SetType(LM_OT_MACRO_ARG);
212 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
215 MathParInset::draw(pain, x, baseline);
218 std::ostringstream ost;
219 ost << '#' << number;
220 drawStr(pain, LM_TC_TEX, size, x, baseline, ost.str().c_str());
225 void MathMacroArgument::Metrics()
228 MathParInset::Metrics();
231 std::ostringstream ost;
232 ost << '#' << number;
233 width = mathed_string_width(LM_TC_TEX, size, ost.str().c_str());
234 mathed_string_height(LM_TC_TEX, size, ost.str().c_str(),
240 void MathMacroArgument::Write(ostream & os, bool fragile)
243 MathParInset::Write(os, fragile);
245 os << '#' << number << ' ';
250 /* --------------------- MathMacroTemplate ---------------------------*/
252 MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg):
253 MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO),
254 flags(flg), nargs(na)
257 tcode = LM_TC_ACTIVE_INSET;
259 for (int i = 0; i < nargs; ++i) {
260 args_[i].setNumber(i + 1);
264 // Here is nargs != args_.size()
270 MathMacroTemplate::~MathMacroTemplate()
272 // prevent to delete already deleted objects
273 for (int i = 0; i < nargs; ++i) {
279 void MathMacroTemplate::setEditMode(bool ed)
283 for (int i = 0; i < nargs; ++i) {
284 args_[i].setExpand(false);
289 for (int i = 0; i < nargs; ++i) {
290 args_[i].setExpand(true);
296 void MathMacroTemplate::draw(Painter & pain, int x, int y)
299 bool expnd = (nargs > 0) ? args_[0].getExpand(): false;
300 if (flags & MMF_Edit) {
301 for (int i = 0; i < nargs; ++i) {
302 args_[i].setExpand(false);
306 for (int i = 0; i < nargs; ++i) {
307 args_[i].setExpand(true);
311 MathParInset::draw(pain, x, y);
314 for (int i = 0; i < nargs; ++i) {
315 args_[i].setExpand(expnd);
320 void MathMacroTemplate::Metrics()
322 bool expnd = (nargs > 0) ? args_[0].getExpand(): false;
324 if (flags & MMF_Edit) {
325 for (int i = 0; i < nargs; ++i) {
326 args_[i].setExpand(false);
329 for (int i = 0; i < nargs; ++i) {
330 args_[i].setExpand(true);
333 MathParInset::Metrics();
335 for (int i = 0; i < nargs; ++i) {
336 args_[i].setExpand(expnd);
341 void MathMacroTemplate::update(MathMacro * macro)
343 int idx = (macro) ? macro->getArgumentIdx() : 0;
344 for (int i = 0; i < nargs; ++i) {
346 macro->setArgumentIdx(i);
347 args_[i].SetData(macro->GetData());
348 MathedRowSt * row = macro->getRowSt();
349 args_[i].setRowSt(row);
353 macro->setArgumentIdx(idx);
357 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
359 os << "\n\\newcommand{\\" << name << "}";
362 os << "[" << nargs << "]";
366 for (int i = 0; i < nargs; ++i) {
367 args_[i].setExpand(false);
374 void MathMacroTemplate::setArgument(MathedArray * a, int i)
380 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
382 args_[i].GetXY(x, y);
386 MathParInset * MathMacroTemplate::getMacroPar(int i) const
388 if (i >= 0 && i < nargs)
389 return const_cast<MathParInset *>
390 (static_cast<MathParInset const *>(&args_[i]));
396 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
398 for (int i = 0; i < nargs; ++i) {
399 if (args_[i].Inside(x, y)) {
407 /* -------------------------- MathMacroTable -----------------------*/
409 MathMacroTable::MathMacroTable(int n) : max_macros(n)
411 macro_table = new MathMacroTemplateP[max_macros];
416 MathMacroTable::~MathMacroTable()
418 delete[] macro_table;
422 // The search is currently linear but will be binary or hash, later.
423 MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
425 for (int i = 0; i < num_macros; ++i) {
426 if (name == macro_table[i]->GetName())
427 return macro_table[i];
433 void MathMacroTable::addTemplate(MathMacroTemplate * m)
435 if (num_macros < max_macros)
436 macro_table[num_macros++] = m;
438 lyxerr << "Error (MathMacroTable::addTemplate): "
439 "Macro table exhausted!" << endl;
443 // All this stuff aparently leaks because it's created here and is not
444 // deleted never, but it have to live all the LyX sesion. OK, would not
445 // so hard to do it in the MacroTable destructor, but this doesn't harm
446 // seriously, so don't bother me with purify results here. ;-)
448 void MathMacroTable::builtinMacros()
451 MathParInset * inset;// *arg;
452 MathedArray * array2;
456 lyxerr[Debug::MATHED] << "Building macros" << endl;
458 // This macro doesn't have arguments
459 MathMacroTemplate * m = new MathMacroTemplate("notin"); // this leaks
461 MathedArray * array = new MathedArray; // this leaks
463 iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
466 // These two are only while we are still with LyX 2.x
467 m = new MathMacroTemplate("emptyset"); // this leaks
469 array = new MathedArray; // this leaks
471 iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
474 m = new MathMacroTemplate("perp"); // this leaks
476 array = new MathedArray; // this leaks
478 iter.Insert(LM_bot, LM_TC_BOP);
481 // binom has two arguments
482 m = new MathMacroTemplate("binom", 2);
484 array = new MathedArray;
487 inset = new MathDelimInset('(', ')');
488 iter.Insert(inset, LM_TC_ACTIVE_INSET);
489 array = new MathedArray;
491 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
492 iter.Insert(frac, LM_TC_ACTIVE_INSET);
493 inset->SetData(array);
494 array = new MathedArray;
495 array2 = new MathedArray;
497 iter.Insert(m->getMacroPar(0));
498 iter.SetData(array2);
499 iter.Insert(m->getMacroPar(1));
500 frac->SetData(array, array2);
503 // Cases has 1 argument
504 m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
506 array = new MathedArray; // this leaks
508 arg = new MathMatrixInset(2, 1); // this leaks
511 arg->SetAlign('c', "ll");
512 iter.Insert(arg, LM_TC_ACTIVE_INSET);
513 inset = new MathDelimInset('{', '.'); // this leaks
514 inset->SetData(array);
515 array = new MathedArray; // this leaks
517 iter.Insert(inset, LM_TC_ACTIVE_INSET);
521 // the environment substack has 1 argument
522 m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
524 arg = new MathMatrixInset(1, 1); // this leaks
526 arg->SetType(LM_OT_MACRO);
527 array = new MathedArray; // this leaks
529 iter.Insert(arg, LM_TC_ACTIVE_INSET);
534 MathMacroTable MathMacroTable::mathMTable(255);
535 bool MathMacroTable::built = false;