]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macro.C
Initial revision
[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 "error.h"
31
32
33 enum MathedMacroFlag {
34     MMF_Env=1,
35     MMF_Exp=2,
36     MMF_Edit=4
37 };
38
39
40 extern GC mathGC, mathFrameGC, latexGC;
41 extern int mathed_string_width(short type, int style, byte const* s, int ls);
42 extern int mathed_string_height(short, int, byte const*, int, int&, int&);
43
44
45 MathMacro::MathMacro(MathMacroTemplate* t): 
46     MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t)
47 {
48     nargs = tmplate->getNoArgs();
49     tcode = tmplate->getTCode();
50     args = new MacroArgumentBase[nargs];
51     for (int i=0; i<nargs; i++) {
52 //      if (tmplate->getMacroPar(i)->Permit(LMPF_ALLOW_CR))
53 //        args[i].row = new MathedRowSt(tmplate->getMacroPar(i)->GetColumns());
54 //      else 
55           args[i].row = 0;
56 /*      int k = tmplate->getMacroPar(i)->GetColumns();
57         if (k>0) {
58             args[i].array = new LyxArrayBase;
59             for (int j=0; j<k-1; j++) args[i].array->Insert(j, LM_TC_TAB);
60         }*/
61     }
62     idx = 0;
63     SetName(tmplate->GetName());
64 }
65
66 MathMacro::MathMacro(MathMacro* m): 
67     MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO)
68 {
69     tmplate = m->tmplate;
70     nargs = tmplate->getNoArgs();
71     tcode = tmplate->getTCode();
72     args = new MacroArgumentBase[nargs];
73     idx = 0;
74     SetName(tmplate->GetName());
75     for (int i=0; i<tmplate->nargs; i++) {
76         m->setArgumentIdx(i);
77         MathedIter it(m->GetData());
78         args[i].row = m->args[i].row;
79         args[i].array = it.Copy();
80     }
81 }
82
83 MathMacro::~MathMacro()
84 {
85     for (idx=0; idx<nargs; idx++) {
86         MathedIter it(args[idx].array);
87         it. Clear();
88         delete args[idx].row;
89     }
90     delete[] args;
91 }
92
93
94 MathedInset *MathMacro::Clone()
95 {
96     MathMacro *m = new MathMacro(this);
97     return m;
98 }
99
100
101 void MathMacro::Metrics()
102 {
103     if (nargs>0)
104       tmplate->update(this);
105     tmplate->Metrics();
106     width = tmplate->Width();
107     ascent = tmplate->Ascent();
108     descent = tmplate->Descent();
109 }
110
111
112 void MathMacro::Draw(int x, int y)
113 {
114     xo = x;  yo = y;
115     Metrics();
116     tmplate->update(this);
117     tmplate->SetStyle(size);
118     mathGC = latexGC;
119     tmplate->Draw(x, y);
120     for (int i=0; i<nargs; i++)
121       tmplate->GetMacroXY(i, args[i].x, args[i].y);
122 }
123
124
125 int MathMacro::GetColumns()
126 {
127     return tmplate->getMacroPar(idx)->GetColumns();
128 }
129
130
131 void MathMacro::GetXY(int& x, int& y) const
132 {
133     x = args[idx].x;  y = args[idx].y;
134 }
135
136
137 bool MathMacro::Permit(short f)
138 {
139     return ((nargs>0) ? tmplate->getMacroPar(idx)->Permit(f): MathParInset::Permit(f));
140 }
141
142 void MathMacro::SetFocus(int x, int y)
143 {
144     tmplate->update(this);
145     tmplate->SetMacroFocus(idx, x, y);
146 }
147
148
149 void MathMacro::Write(FILE *file)
150 {
151    LString output;
152    MathMacro::Write(output);
153    fprintf(file, "%s", output.c_str());
154 }
155
156
157 void MathMacro::Write(LString &file)
158 {
159     if (tmplate->flags & MMF_Exp) {
160         lyxerr.debug(LString("Expand ")+ int(tmplate->flags)
161                      + ' ' + int(MMF_Exp), 
162                      Error::MATHED);
163         tmplate->update(this);
164         tmplate->Write(file);
165     } else {
166         if (tmplate->flags & MMF_Env) {
167           file += "\\begin{";
168           file += name;
169           file += "} ";
170         } else {
171           file += '\\';
172           file += name;
173         }
174 //      if (options) { 
175 //        file += '[';
176 //        file += options;
177 //        file += ']';
178 //      }
179         
180         if (!(tmplate->flags & MMF_Env) && nargs>0) 
181           file += '{';
182         
183         for (int i=0; i<nargs; i++) {
184             array = args[i].array;
185             MathParInset::Write(file);
186             if (i<nargs-1)  
187               file += "}{";
188         }   
189         if (tmplate->flags & MMF_Env) {
190             file += "\\end{";
191             file += name;
192             file += '}';
193         } else {
194             if (nargs>0) 
195                 file += '}';
196             else
197                 file += ' ';
198         }
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 void MathMacroArgument::Draw(int x, int baseline)
214 {
215     if (expnd_mode) {
216         MathParInset::Draw(x, baseline);
217     } else {
218         unsigned char s[3];
219         sprintf((char*)s, "#%d", number);
220         drawStr(LM_TC_TEX, size, x, baseline, &s[0], 2);
221     }
222 }
223
224
225 void MathMacroArgument::Metrics()
226 {
227     if (expnd_mode) {
228         MathParInset::Metrics();
229     } else {
230         unsigned char s[3];
231         sprintf((char*)s, "#%d", number);
232         width = mathed_string_width(LM_TC_TEX, size, &s[0], 2);
233         mathed_string_height(LM_TC_TEX, size, &s[0], 2, ascent, descent);
234     }
235 }
236
237 void MathMacroArgument::Write(FILE *file)
238 {
239    LString output;
240    MathMacroArgument::Write(output);
241    fprintf(file, "%s", output.c_str());
242 }
243
244 void MathMacroArgument::Write(LString &file)
245 {
246     if (expnd_mode) {
247         MathParInset::Write(file);
248     } else {
249         file += '#';
250         file += number;
251         file += ' ';
252     }
253 }
254
255
256 /* --------------------- MathMacroTemplate ---------------------------*/
257
258 MathMacroTemplate::MathMacroTemplate(char 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 }
271
272
273 MathMacroTemplate::~MathMacroTemplate()
274 {
275     // prevent to delete already deleted objects
276     for (int i=0; i<nargs; i++) {
277         args[i].SetData(0);
278     }
279     delete[] args;
280 }
281
282
283 void MathMacroTemplate::setEditMode(bool ed)
284 {
285     if (ed) {
286         flags |= MMF_Edit;
287         for (int i=0; i<nargs; i++) {
288             args[i].setExpand(false);
289         }
290     } else {
291         flags &= ~MMF_Edit;
292         for (int i=0; i<nargs; i++) {
293             args[i].setExpand(true);
294         }
295     }
296 }
297
298
299 void MathMacroTemplate::Draw(int x, int y)
300 {
301     int x2, y2;
302     bool expnd = (nargs>0) ? args[0].getExpand(): false;
303     if (flags & MMF_Edit) {
304         for (int i=0; i<nargs; i++) {
305             args[i].setExpand(false);
306         }
307       x2 = x; y2 = y;
308     } else {
309         for (int i=0; i<nargs; i++) {
310             args[i].setExpand(true);
311         }
312       x2 = xo; y2 = yo;
313     }
314     MathParInset::Draw(x, y);
315     xo = x2; yo = y2;
316     
317     for (int i=0; i<nargs; i++) {
318         args[i].setExpand(expnd);
319     }
320 }
321
322 void MathMacroTemplate::Metrics()
323 {
324     bool expnd = (nargs>0) ? args[0].getExpand(): false;
325     
326     if (flags & MMF_Edit) {
327         for (int i=0; i<nargs; i++) {
328             args[i].setExpand(false);
329         }
330     } else {
331         for (int i=0; i<nargs; i++) {
332             args[i].setExpand(true);
333         }
334     }
335     MathParInset::Metrics();
336     
337     for (int i=0; i<nargs; i++) {
338         args[i].setExpand(expnd);
339     }
340 }
341
342 void MathMacroTemplate::update(MathMacro* macro)
343 {
344     int idx = (macro) ? macro->getArgumentIdx(): 0;
345     for (int i=0; i<nargs; i++) {
346         if (macro) {
347             macro->setArgumentIdx(i);
348             args[i].SetData(macro->GetData());
349             MathedRowSt *row = macro->getRowSt();
350             args[i].setRowSt(row);
351         }
352     }   
353     if (macro)
354       macro->setArgumentIdx(idx);
355 }
356     
357
358 void MathMacroTemplate::WriteDef(FILE *file)
359 {
360     fprintf(file, "\n\\newcommand{\\%s}", name);
361       
362     if (nargs > 0 ) 
363       fprintf(file, "[%d]", nargs);
364     
365     fprintf(file, "{");
366     
367     for (int i=0; i<nargs; i++) {
368         args[i].setExpand(false);
369     }    
370     Write(file); 
371     fprintf(file, "}\n");
372 }
373
374 void MathMacroTemplate::WriteDef(LString &file)
375 {
376     file += "\n\\newcommand{\\";
377     file += name;
378     file += '}';
379       
380     if (nargs > 0 ) {
381       file += '[';
382       file += nargs;
383       file += ']';
384     }
385     
386     file += '{';
387     
388     for (int i=0; i<nargs; i++) {
389         args[i].setExpand(false);
390     }    
391     Write(file); 
392     file += "}\n";
393 }
394
395 void MathMacroTemplate::setArgument(LyxArrayBase *a, int i)
396 {
397     args[i].SetData(a);
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) ? (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.print("Error (MathMacroTable::addTemplate): "
455                    "Macro table exhausted!");
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     MathMacroTemplate *m;
467     MathedIter iter;
468     MathParInset* inset;// *arg;
469     LyxArrayBase *array, *array2;
470     
471     built = true;
472     
473     lyxerr.debug("Building macros", Error::MATHED);
474     
475     // This macro doesn't have arguments
476     m = new MathMacroTemplate("notin");  // this leaks
477     addTemplate(m);
478     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;
553