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