]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
dfbb59c606a554bb2384b746d2e8d31ad0749417
[lyx.git] / src / mathed / math_macrotable.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_macrotable.h"
8 #include "math_macro.h"
9 #include "math_macrotemplate.h"
10 #include "math_iter.h"
11 #include "array.h"
12 #include "math_accentinset.h"
13 #include "math_deliminset.h"
14 #include "math_fracinset.h"
15 #include "math_parinset.h"
16 #include "debug.h"
17 #include "support/LAssert.h"
18
19 using std::endl;
20 using std::make_pair;
21
22 MathMacroTable::table_type MathMacroTable::macro_table;
23
24 bool MathMacroTable::built = false;
25
26
27 void MathMacroTable::dump()
28 {
29         using std::cerr;
30
31         cerr << "\n------------------------------------------\n";
32         table_type::const_iterator it;
33         for (it = macro_table.begin(); it != macro_table.end(); ++it)
34                 cerr << it->first << ": " << it->second->GetData() << endl;
35         cerr << "------------------------------------------\n";
36 }
37
38
39 MathMacroTemplate &
40 MathMacroTable::provideTemplate(string const & name, int na)
41 {
42         if (!built)
43                 builtinMacros();
44         
45         if (macro_table.find(name) == macro_table.end())
46                 macro_table.insert(make_pair(name, new MathMacroTemplate(name, na)));
47         
48         return *(macro_table.find(name)->second);
49 }
50
51
52 MathMacroTemplate &
53 MathMacroTable::provideTemplate(string const & name)
54 {
55         if (!built)
56                 builtinMacros();
57         
58         return *macro_table[name];
59 }
60
61
62 bool MathMacroTable::hasTemplate(string const & name)
63 {
64         if (!built)
65                 builtinMacros();
66         
67         return macro_table.find(name) != macro_table.end();
68 }
69
70
71 MathMacro * MathMacroTable::cloneTemplate(string const & name)
72 {
73         return new MathMacro(provideTemplate(name));
74 }
75
76
77 void MathMacroTable::builtinMacros()
78 {
79         built = true;
80     
81         lyxerr[Debug::MATHED] << "Building macros" << endl;
82     
83         // This macro doesn't have arguments
84         {
85                 MathMacroTemplate & m = provideTemplate("notin", 0);
86                 MathedIter iter(&m.GetData());
87                 iter.insertInset(new MathAccentInset(LM_in, LM_TC_BOPS, LM_not),
88                                  LM_TC_INSET);
89         }
90
91         // These two are only while we are still with LyX 2.x
92         {
93                 MathMacroTemplate & m = provideTemplate("emptyset", 0);
94                 MathedIter iter(&m.GetData());
95                 iter.insertInset(new MathAccentInset('0', LM_TC_RM, LM_not),
96                                  LM_TC_INSET);
97         }
98
99         {
100                 MathMacroTemplate & m = provideTemplate("perp", 0);
101                 MathedIter iter(&m.GetData());
102                 iter.insert(LM_bot, LM_TC_BOP);
103         }
104
105         // binom has two arguments
106         {
107                 MathMacroTemplate & m = provideTemplate("binom", 2);
108                 MathedIter iter(&m.GetData());
109
110                 MathParInset * inset = new MathDelimInset('(', ')');
111                 iter.insertInset(inset, LM_TC_ACTIVE_INSET);
112
113                 MathedArray array2;
114                 MathedIter iter2(&array2);
115                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
116                 iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
117                 frac->setData(array2);
118
119                 MathedArray array3;
120                 MathedIter iter3(&array3);
121                 iter3.insertInset(new MathMacroArgument(1), LM_TC_INSET);
122
123                 MathedArray array4;
124                 MathedIter iter4(&array4);
125                 iter4.insertInset(new MathMacroArgument(2), LM_TC_INSET);
126
127                 frac->SetData(array3, array4);
128         }
129
130 /*    
131         {
132                 boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("perp", 0));
133                 addTemplate(m);
134                 MathedArray array;
135                 MathedIter iter(&array);
136                 iter.insert(LM_bot, LM_TC_BOP);
137                 m->setData(array);
138         }
139
140         // binom has two arguments
141         {
142                 boost::shared_ptr<MathMacroTemplate> m(new MathMacroTemplate("binom", 2));
143                 addTemplate(m);
144                 MathedArray array;
145                 m->setData(array);
146                 MathedIter iter(&array);
147                 MathParInset * inset = new MathDelimInset('(', ')');
148                 iter.insertInset(inset, LM_TC_ACTIVE_INSET);
149                 array = MathedArray();
150                 MathedIter iter2(&array);
151                 MathFracInset * frac = new MathFracInset(LM_OT_ATOP);
152                 iter2.insertInset(frac, LM_TC_ACTIVE_INSET);
153                 inset->setData(array);
154                 array = MathedArray();
155                 MathedArray array2;
156                 MathedIter iter3(&array);
157                 iter3.insertInset(m->getMacroPar(0), LM_TC_INSET);
158                 MathedIter iter4(&array2);
159                 iter4.insertInset(m->getMacroPar(1), LM_TC_INSET);
160                 frac->SetData(array, array2);
161         }
162 */
163 }