]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
More fixes to insettabular/text (and some missing features added).
[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
137 int MathMacro::GetColumns() const
138 {
139     return tmplate->getMacroPar(idx)->GetColumns();
140 }
141
142
143 void MathMacro::GetXY(int & x, int & y) const
144 {
145     x = args[idx].x;  y = args[idx].y;
146 }
147
148
149 bool MathMacro::Permit(short f)
150 {
151     return (nargs > 0) ?
152             tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f);
153 }
154
155
156 void MathMacro::SetFocus(int x, int y)
157 {
158     tmplate->update(this);
159     tmplate->SetMacroFocus(idx, x, y);
160 }
161
162
163 void MathMacro::Write(ostream & os, bool fragile)
164 {
165     if (tmplate->flags & MMF_Exp) {
166             lyxerr[Debug::MATHED] << "Expand " << tmplate->flags
167                                   << ' ' << MMF_Exp << endl; 
168         tmplate->update(this);
169         tmplate->Write(os, fragile);
170     } else {
171         if (tmplate->flags & MMF_Env) {
172                 os << "\\begin{"
173                    << name
174                    << "} ";
175         } else {
176                 os << '\\' << name;
177         }
178 //      if (options) { 
179 //        file += '[';
180 //        file += options;
181 //        file += ']';
182 //      }
183         
184         if (!(tmplate->flags & MMF_Env) && nargs > 0) 
185                 os << '{';
186         
187         for (int i = 0; i < nargs; ++i) {
188             array = args[i].array;
189             MathParInset::Write(os, fragile);
190             if (i < nargs - 1)  
191                     os << "}{";
192         }   
193         if (tmplate->flags & MMF_Env) {
194                 os << "\\end{"
195                    << name
196                    << '}';
197         } else {
198             if (nargs > 0) 
199                     os << '}';
200             else
201                     os << ' ';
202         }
203     }
204 }
205
206
207 /*---------------  Macro argument -----------------------------------*/
208
209 MathMacroArgument::MathMacroArgument(int n)
210 {
211     number = n;
212     expnd_mode = false;
213     SetType(LM_OT_MACRO_ARG);
214 }
215
216
217 void MathMacroArgument::draw(Painter & pain, int x, int baseline)
218 {
219     if (expnd_mode) {
220         MathParInset::draw(pain, x, baseline);
221     } else {
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     }
227 }
228
229
230 void MathMacroArgument::Metrics()
231 {
232     if (expnd_mode) {
233         MathParInset::Metrics();
234     } else {
235             std::ostringstream ost;
236             ost << '#' << number;
237             width = mathed_string_width(LM_TC_TEX, size, 
238                                         reinterpret_cast<byte const *>(ost.str().c_str()), 2);
239             mathed_string_height(LM_TC_TEX, size,
240                                  reinterpret_cast<byte const *>(ost.str().c_str()), 
241                                  2, ascent, descent);
242     }
243 }
244
245
246 void MathMacroArgument::Write(ostream & os, bool fragile)
247 {
248     if (expnd_mode) {
249         MathParInset::Write(os, fragile);
250     } else {
251             os << '#' << number << ' ';
252     }
253 }
254
255
256 /* --------------------- MathMacroTemplate ---------------------------*/
257
258 MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg):
259     MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), 
260     flags(flg), nargs(na)
261 {
262     if (nargs > 0) {
263         tcode = LM_TC_ACTIVE_INSET;
264         args = new MathMacroArgument[nargs];
265         for (int i = 0; i < nargs; ++i) {
266             args[i].setNumber(i + 1);
267         }
268     } else {
269         tcode = LM_TC_INSET;
270         args = 0;
271     }
272 }
273
274
275 MathMacroTemplate::~MathMacroTemplate()
276 {
277     // prevent to delete already deleted objects
278     for (int i = 0; i < nargs; ++i) {
279         args[i].SetData(0);
280     }
281     delete[] args;
282 }
283
284
285 void MathMacroTemplate::setEditMode(bool ed)
286 {
287     if (ed) {
288         flags |= MMF_Edit;
289         for (int i = 0; i < nargs; ++i) {
290             args[i].setExpand(false);
291         }
292     } else {
293         flags &= ~MMF_Edit;
294         for (int i = 0; i < nargs; ++i) {
295             args[i].setExpand(true);
296         }
297     }
298 }
299
300
301 void MathMacroTemplate::draw(Painter & pain, int x, int y)
302 {
303     int x2, y2;
304     bool expnd = (nargs > 0) ? args[0].getExpand(): false;
305     if (flags & MMF_Edit) {
306         for (int i = 0; i < nargs; ++i) {
307             args[i].setExpand(false);
308         }
309       x2 = x; y2 = y;
310     } else {
311         for (int i = 0; i < nargs; ++i) {
312             args[i].setExpand(true);
313         }
314       x2 = xo; y2 = yo;
315     }
316     MathParInset::draw(pain, x, y);
317     xo = x2; yo = y2;
318     
319     for (int i = 0; i < nargs; ++i) {
320         args[i].setExpand(expnd);
321     }
322 }
323
324
325 void MathMacroTemplate::Metrics()
326 {
327     bool expnd = (nargs > 0) ? args[0].getExpand(): false;
328     
329     if (flags & MMF_Edit) {
330         for (int i = 0; i < nargs; ++i) {
331             args[i].setExpand(false);
332         }
333     } else {
334         for (int i = 0; i < nargs; ++i) {
335             args[i].setExpand(true);
336         }
337     }
338     MathParInset::Metrics();
339     
340     for (int i = 0; i < nargs; ++i) {
341         args[i].setExpand(expnd);
342     }
343 }
344
345
346 void MathMacroTemplate::update(MathMacro * macro)
347 {
348     int idx = (macro) ? macro->getArgumentIdx() : 0;
349     for (int i = 0; i < nargs; ++i) {
350         if (macro) {
351             macro->setArgumentIdx(i);
352             args[i].SetData(macro->GetData());
353             MathedRowSt * row = macro->getRowSt();
354             args[i].setRowSt(row);
355         }
356     }   
357     if (macro)
358       macro->setArgumentIdx(idx);
359 }
360     
361
362 void MathMacroTemplate::WriteDef(ostream & os, bool fragile)
363 {
364         os << "\n\\newcommand{\\" << name << "}";
365       
366     if (nargs > 0 ) 
367             os << "[" << nargs << "]";
368     
369     os << "{";
370     
371     for (int i = 0; i < nargs; ++i) {
372         args[i].setExpand(false);
373     }    
374     Write(os, fragile); 
375     os << "}\n";
376 }
377
378
379 void MathMacroTemplate::setArgument(LyxArrayBase * a, int i)
380 {
381     args[i].SetData(a);
382 }
383
384
385 void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const
386 {
387     args[i].GetXY(x, y);
388 }
389
390
391 MathParInset * MathMacroTemplate::getMacroPar(int i) const
392 {
393     return (i >= 0 && i < nargs) ? static_cast<MathParInset*>(&args[i]) : 0;
394 }
395
396
397 void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y)
398 {
399     for (int i = 0; i < nargs; ++i) {
400         if (args[i].Inside(x, y)) {
401             idx = i;
402             break;
403         }
404     }
405 }
406
407
408 /* -------------------------- MathMacroTable -----------------------*/
409
410 MathMacroTable::MathMacroTable(int n) : max_macros(n)
411 {
412     macro_table = new MathMacroTemplateP[max_macros];
413     num_macros = 0;
414 }
415
416
417 MathMacroTable::~MathMacroTable()
418 {
419     delete[] macro_table;
420 }
421
422
423 // The search is currently linear but will be binary or hash, later.
424 MathMacroTemplate * MathMacroTable::getTemplate(string const & name) const
425 {
426     for (int i = 0; i < num_macros; ++i) {
427       if (name == macro_table[i]->GetName()) 
428         return macro_table[i];
429     }
430     
431     return 0;
432 }
433
434 void MathMacroTable::addTemplate(MathMacroTemplate * m)
435 {
436     if (num_macros < max_macros)
437       macro_table[num_macros++] = m;
438     else
439             lyxerr << "Error (MathMacroTable::addTemplate): "
440                     "Macro table exhausted!" << endl;
441 }
442
443
444 // All this stuff aparently leaks because it's created here and is not 
445 // deleted never, but it have to live all the LyX sesion. OK, would not
446 // so hard to do it in the MacroTable destructor, but this doesn't harm
447 // seriously, so don't bother me with purify results here.   ;-)
448
449 void MathMacroTable::builtinMacros()
450 {
451     MathedIter iter;
452     MathParInset * inset;// *arg;
453     LyxArrayBase * array2;
454     
455     built = true;
456     
457     lyxerr[Debug::MATHED] << "Building macros" << endl;
458     
459     // This macro doesn't have arguments
460     MathMacroTemplate * m = new MathMacroTemplate("notin");  // this leaks
461     addTemplate(m);
462     LyxArrayBase * array = new LyxArrayBase; // this leaks
463     iter.SetData(array);
464     iter.Insert(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not)); // this leaks
465     m->SetData(array);
466     
467     // These two are only while we are still with LyX 2.x
468     m = new MathMacroTemplate("emptyset"); // this leaks
469     addTemplate(m);
470     array = new LyxArrayBase; // this leaks
471     iter.SetData(array);
472     iter.Insert(new MathAccentInset('O', LM_TC_RM, LM_not)); // this leaks
473     m->SetData(array);
474     
475     m = new MathMacroTemplate("perp"); // this leaks
476     addTemplate(m);
477     array = new LyxArrayBase; // this leaks
478     iter.SetData(array);
479     iter.Insert(LM_bot, LM_TC_BOP);
480     m->SetData(array);
481
482     // binom has two arguments
483     m = new MathMacroTemplate("binom", 2);
484     addTemplate(m);
485     array = new LyxArrayBase; 
486     m->SetData(array);
487     iter.SetData(array);
488     inset = new MathDelimInset('(', ')');
489     iter.Insert(inset, LM_TC_ACTIVE_INSET);
490     array = new LyxArrayBase; 
491     iter.SetData(array);
492     MathFracInset *frac = new MathFracInset(LM_OT_ATOP);
493     iter.Insert(frac, LM_TC_ACTIVE_INSET);
494     inset->SetData(array);
495     array = new LyxArrayBase;
496     array2 = new LyxArrayBase;  
497     iter.SetData(array);
498     iter.Insert(m->getMacroPar(0));
499     iter.SetData(array2);
500     iter.Insert(m->getMacroPar(1));
501     frac->SetData(array, array2);
502
503 /*
504     // Cases has 1 argument
505     m = new MathMacroTemplate("cases", 1, MMF_Env); // this leaks
506     addTemplate(m);
507     array = new LyxArrayBase; // this leaks
508     iter.SetData(array);
509     arg = new MathMatrixInset(2, 1); // this leaks
510
511     m->setArgument(arg);
512     arg->SetAlign('c', "ll");
513     iter.Insert(arg, LM_TC_ACTIVE_INSET);
514     inset = new MathDelimInset('{', '.'); // this leaks
515     inset->SetData(array);
516     array = new LyxArrayBase; // this leaks
517     iter.SetData(array);
518     iter.Insert(inset, LM_TC_ACTIVE_INSET);
519     m->SetData(array);
520   
521
522     // the environment substack has 1 argument
523     m = new MathMacroTemplate("substack", 1, MMF_Env); // this leaks
524     addTemplate(m);     
525     arg = new MathMatrixInset(1, 1); // this leaks
526     m->setArgument(arg);
527     arg->SetType(LM_OT_MACRO);
528     array = new LyxArrayBase; // this leaks
529     iter.SetData(array);
530     iter.Insert(arg, LM_TC_ACTIVE_INSET);
531     m->SetData(array);*/
532 }
533
534
535 MathMacroTable MathMacroTable::mathMTable(255);
536 bool MathMacroTable::built = false;