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