]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
Fix to formula macros from Dekel
[lyx.git] / src / mathed / math_macro.C
1 // -*- C++ -*-
2 /*
3  *  File:        math_macro.C
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
8  *
9  *  Dependencies: Mathed
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *  Version: 0.2, Mathed & Lyx project.
14  *
15  *  This code is under the GNU General Public Licence version 2 or later.
16  */
17
18 #include <config.h>
19 #include FORMS_H_LOCATION
20
21 #ifdef __GNUG__
22 #pragma implementation "math_macro.h"
23 #pragma implementation "math_defs.h"
24 #endif
25
26 #include "LString.h"
27 #include "math_macro.h"
28 #include "math_iter.h"
29 #include "math_inset.h"
30 #include "support/lstrings.h"
31 #include "debug.h"
32
33 using std::ostream;
34 using std::endl;
35
36 ostream & operator<<(ostream & o, MathedTextCodes mtc)
37 {
38         return o << int(mtc);
39 }
40
41 enum MathedMacroFlag {
42     MMF_Env= 1,
43     MMF_Exp= 2,
44     MMF_Edit= 4
45 };
46
47 ostream & operator<<(ostream & o, MathedMacroFlag mmf)
48 {
49         return o << int(mmf);
50 }
51
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&);
54
55
56 MathMacro::MathMacro(MathMacroTemplate* t): 
57     MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
58 {
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());
65 //      else 
66           args[i].row = 0;
67 /*      int k = tmplate->getMacroPar(i)->GetColumns();
68         if (k>0) {
69             args[i].array = new LyxArrayBase;
70             for (int j= 0; j<k-1; ++j) args[i].array->Insert(j, LM_TC_TAB);
71         }*/
72     }
73     idx = 0;
74     SetName(tmplate->GetName());
75 }
76
77
78 MathMacro::MathMacro(MathMacro * m): 
79     MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
80 {
81     tmplate = m->tmplate;
82     nargs = tmplate->getNoArgs();
83     tcode = tmplate->getTCode();
84     args = new MacroArgumentBase[nargs];
85     idx = 0;
86     SetName(tmplate->GetName());
87     for (int i = 0; i < tmplate->nargs; ++i) {
88         m->setArgumentIdx(i);
89         MathedIter it(m->GetData());
90         args[i].row = m->args[i].row;
91         args[i].array = it.Copy();
92     }
93 }
94
95 MathMacro::~MathMacro()
96 {
97     for (idx = 0; idx < nargs; ++idx) {
98         MathedIter it(args[idx].array);
99         it. Clear();
100         delete args[idx].row;
101     }
102     delete[] args;
103 }
104
105
106 MathedInset * MathMacro::Clone()
107 {
108     return new MathMacro(this);
109 }
110
111
112 void MathMacro::Metrics()
113 {
114     if (nargs > 0)
115       tmplate->update(this);
116     tmplate->SetStyle(size);
117     tmplate->Metrics();
118     width = tmplate->Width();
119     ascent = tmplate->Ascent();
120     descent = tmplate->Descent();
121 }
122
123
124 void MathMacro::draw(Painter & pain, int x, int y)
125 {
126     xo = x;  yo = y;
127     Metrics();
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);
133 }
134
135
136 int MathMacro::GetColumns()
137 {
138     return tmplate->getMacroPar(idx)->GetColumns();
139 }
140
141
142 void MathMacro::GetXY(int & x, int & y) const
143 {
144     x = args[idx].x;  y = args[idx].y;
145 }
146
147
148 bool MathMacro::Permit(short f)
149 {
150     return (nargs > 0) ?
151             tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
152 }
153
154
155 void MathMacro::SetFocus(int x, int y)
156 {
157     tmplate->update(this);
158     tmplate->SetMacroFocus(idx, x, y);
159 }
160
161
162 void MathMacro::Write(ostream & os, bool fragile)
163 {
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);
169     } else {
170         if (tmplate->flags & MMF_Env) {
171                 os << "\\begin{"
172                    << name
173                    << "} ";
174         } else {
175                 os << '\\' << name;
176         }
177 //      if (options) { 
178 //        file += '[';
179 //        file += options;
180 //        file += ']';
181 //      }
182         
183         if (!(tmplate->flags & MMF_Env) && nargs > 0) 
184                 os << '{';
185         
186         for (int i = 0; i < nargs; ++i) {
187             array = args[i].array;
188             MathParInset::Write(os, fragile);
189             if (i < nargs - 1)  
190                     os << "}{";
191         }   
192         if (tmplate->flags & MMF_Env) {
193                 os << "\\end{"
194                    << name
195                    << '}';
196         } else {
197             if (nargs > 0) 
198                     os << '}';
199             else
200                     os << ' ';
201         }
202     }
203 }
204
205
206 /*---------------  Macro argument -----------------------------------*/
207
208 MathMacroArgument::MathMacroArgument(int n)
209 {
210     number = n;
211     expnd_mode = false;
212     SetType(LM_OT_MACRO_ARG);
213 }
214
215
216 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
217 {
218     if (expnd_mode) {
219         MathParInset::draw(pain, x, baseline);
220     } else {
221 #ifdef HAVE_SSTREAM
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);
226 #else
227             char s[3];
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);
232 #endif
233     }
234 }
235
236
237 void MathMacroArgument::Metrics()
238 {
239     if (expnd_mode) {
240         MathParInset::Metrics();
241     } else {
242 #ifdef HAVE_SSTREAM
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()), 
249                                  2, ascent, descent);
250 #else
251         char s[3];
252         ostrstream ost(s, 3);
253         ost << '#' << number << '\0';
254         width = mathed_string_width(LM_TC_TEX, size,
255                                     reinterpret_cast<byte *>
256                                     (ost.str()), 2);
257         mathed_string_height(LM_TC_TEX, size,
258                              reinterpret_cast<byte *>(ost.str()),
259                              2, ascent, descent);
260 #endif
261     }
262 }
263
264
265 void MathMacroArgument::Write(ostream & os, bool fragile)
266 {
267     if (expnd_mode) {
268         MathParInset::Write(os, fragile);
269     } else {
270             os << '#' << number << ' ';
271     }
272 }
273
274
275 /* --------------------- MathMacroTemplate ---------------------------*/
276
277 MathMacroTemplate::MathMacroTemplate(char const * nm, int na, int flg):
278     MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
279     flags(flg), nargs(na)
280 {
281     if (nargs > 0) {
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);
286         }
287     } else 
288       tcode = LM_TC_INSET;
289 }
290
291
292 MathMacroTemplate::~MathMacroTemplate()
293 {
294     // prevent to delete already deleted objects
295     for (int i = 0; i < nargs; ++i) {
296         args[i].SetData(0);
297     }
298     delete[] args;
299 }
300
301
302 void MathMacroTemplate::setEditMode(bool ed)
303 {
304     if (ed) {
305         flags |= MMF_Edit;
306         for (int i = 0; i < nargs; ++i) {
307             args[i].setExpand(false);
308         }
309     } else {
310         flags &= ~MMF_Edit;
311         for (int i = 0; i < nargs; ++i) {
312             args[i].setExpand(true);
313         }
314     }
315 }
316
317
318 void MathMacroTemplate::draw(Painter & pain, int x, int y)
319 {
320     int x2, y2;
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);
325         }
326       x2 = x; y2 = y;
327     } else {
328         for (int i = 0; i < nargs; ++i) {
329             args[i].setExpand(true);
330         }
331       x2 = xo; y2 = yo;
332     }
333     MathParInset::draw(pain, x, y);
334     xo = x2; yo = y2;
335     
336     for (int i = 0; i < nargs; ++i) {
337         args[i].setExpand(expnd);
338     }
339 }
340
341
342 void MathMacroTemplate::Metrics()
343 {
344     bool expnd = (nargs>0) ? args[0].getExpand(): false;
345     
346     if (flags & MMF_Edit) {
347         for (int i = 0; i < nargs; ++i) {
348             args[i].setExpand(false);
349         }
350     } else {
351         for (int i = 0; i < nargs; ++i) {
352             args[i].setExpand(true);
353         }
354     }
355     MathParInset::Metrics();
356     
357     for (int i = 0; i < nargs; ++i) {
358         args[i].setExpand(expnd);
359     }
360 }
361
362
363 void MathMacroTemplate::update(MathMacro * macro)
364 {
365     int idx = (macro) ? macro->getArgumentIdx(): 0;
366     for (int i = 0; i < nargs; ++i) {
367         if (macro) {
368             macro->setArgumentIdx(i);
369             args[i].SetData(macro->GetData());
370             MathedRowSt *row = macro->getRowSt();
371             args[i].setRowSt(row);
372         }
373     }   
374     if (macro)
375       macro->setArgumentIdx(idx);
376 }
377     
378
379 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
380 {
381         os << "\n\\newcommand{\\" << name << "}";
382       
383     if (nargs > 0 ) 
384             os << "[" << nargs << "]";
385     
386     os << "{";
387     
388     for (int i = 0; i < nargs; ++i) {
389         args[i].setExpand(false);
390     }    
391     Write(os, fragile); 
392     os << "}\n";
393 }
394
395
396 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
397 {
398     args[i].SetData(a);
399 }
400
401
402 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
403 {
404     args[i].GetXY(x, y);
405 }
406
407
408 MathParInset * MathMacroTemplate::getMacroPar(int i) const
409 {
410     return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
411 }
412
413
414 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
415 {
416     for (int i = 0; i < nargs; ++i) {
417         if (args[i].Inside(x, y)) {
418             idx = i;
419             break;
420         }
421     }
422 }
423
424
425 /* -------------------------- MathMacroTable -----------------------*/
426
427 MathMacroTable::MathMacroTable(int n) : max_macros(n)
428 {
429     macro_table = new MathMacroTemplateP[max_macros];
430     num_macros = 0;
431 }
432
433
434 MathMacroTable::~MathMacroTable()
435 {
436     delete[] macro_table;
437 }
438
439
440 // The search is currently linear but will be binary or hash, later.
441 MathMacroTemplate * MathMacroTable::getTemplate(char const * name) const
442 {
443     for (int i = 0; i < num_macros; ++i) {
444       if (strcmp(name, macro_table[i]->GetName()) == 0) 
445         return macro_table[i];
446     }
447     
448     return 0;
449 }
450
451 void MathMacroTable::addTemplate(MathMacroTemplate * m)
452 {
453     if (num_macros < max_macros)
454       macro_table[num_macros++] = m;
455     else
456             lyxerr << "Error (MathMacroTable::addTemplate): "
457                     "Macro table exhausted!" << endl;
458 }
459
460
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.   ;-)
465
466 void MathMacroTable::builtinMacros()
467 {
468     MathedIter iter;
469     MathParInset * inset;// *arg;
470     LyxArrayBase * array2;
471     
472     built = true;
473     
474     lyxerr[Debug::MATHED] << "Building macros" << endl;
475     
476     // This macro doesn't have arguments
477     MathMacroTemplate * m = new MathMacroTemplate("notin");  // this leaks
478     addTemplate(m);
479     LyxArrayBase * array = new LyxArrayBase; // this leaks
480     iter.SetData(array);
481     iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
482     m->SetData(array);
483     
484     // These two are only while we are still with LyX 2.x
485     m = new MathMacroTemplate("emptyset"); // this leaks
486     addTemplate(m);
487     array = new LyxArrayBase; // this leaks
488     iter.SetData(array);
489     iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
490     m->SetData(array);
491     
492     m = new MathMacroTemplate("perp"); // this leaks
493     addTemplate(m);
494     array = new LyxArrayBase; // this leaks
495     iter.SetData(array);
496     iter.Insert(LM_bot, LM_TC_BOP);
497     m->SetData(array);
498
499     // binom has two arguments
500     m = new MathMacroTemplate("binom", 2);
501     addTemplate(m);
502     array = new LyxArrayBase; 
503     m->SetData(array);
504     iter.SetData(array);
505     inset = new MathDelimInset('(', ')');
506     iter.Insert(inset, LM_TC_ACTIVE_INSET);
507     array = new LyxArrayBase; 
508     iter.SetData(array);
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;  
514     iter.SetData(array);
515     iter.Insert(m->getMacroPar(0));
516     iter.SetData(array2);
517     iter.Insert(m->getMacroPar(1));
518     frac->SetData(array, array2);
519
520 /*
521     // Cases has 1 argument
522     m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
523     addTemplate(m);
524     array = new LyxArrayBase; // this leaks
525     iter.SetData(array);
526     arg = new MathMatrixInset(2, 1); // this leaks
527
528     m->setArgument(arg);
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
534     iter.SetData(array);
535     iter.Insert(inset, LM_TC_ACTIVE_INSET);
536     m->SetData(array);
537   
538
539     // the environment substack has 1 argument
540     m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
541     addTemplate(m);     
542     arg = new MathMatrixInset(1, 1); // this leaks
543     m->setArgument(arg);
544     arg->SetType(LM_OT_MACRO);
545     array = new LyxArrayBase; // this leaks
546     iter.SetData(array);
547     iter.Insert(arg, LM_TC_ACTIVE_INSET);
548     m->SetData(array);*/
549 }
550
551
552 MathMacroTable MathMacroTable::mathMTable(255);
553 bool MathMacroTable::built = false;