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