]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTemplate.cpp
* gcc does not like missing characters in keywords
[lyx.git] / src / mathed / MathMacroTemplate.cpp
1 /**
2  * \file math_macrotemplate.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathMacroTemplate.h"
14 #include "MathStream.h"
15 #include "MathParser.h"
16 #include "MathSupport.h"
17
18 #include "Buffer.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "Lexer.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "support/lstrings.h"
28
29
30 namespace lyx {
31
32 using support::bformat;
33
34 using std::ostream;
35 using std::endl;
36
37
38 MathMacroTemplate::MathMacroTemplate()
39         : InsetMathNest(2), numargs_(0), name_(), type_(from_ascii("newcommand"))
40 {
41         initMath();
42 }
43
44
45 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
46                 docstring const & type, MathData const & ar1, MathData const & ar2)
47         : InsetMathNest(2), numargs_(numargs), name_(name), type_(type)
48 {
49         initMath();
50
51         if (numargs_ > 9)
52                 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
53                         << numargs_ << std::endl;
54         cell(0) = ar1;
55         cell(1) = ar2;
56 }
57
58
59 MathMacroTemplate::MathMacroTemplate(docstring const & str)
60         : InsetMathNest(2), numargs_(0), name_()
61 {
62         initMath();
63
64         MathData ar;
65         mathed_parse_cell(ar, str);
66         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
67                 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
68                 return;
69         }
70         operator=( *(ar[0]->asMacroTemplate()) );
71 }
72
73
74 Inset * MathMacroTemplate::clone() const
75 {
76         return new MathMacroTemplate(*this);
77 }
78
79
80 void MathMacroTemplate::edit(Cursor & cur, bool)
81 {
82         //lyxerr << "MathMacroTemplate: edit left/right" << endl;
83         cur.push(*this);
84 }
85
86
87 int MathMacroTemplate::numargs() const
88 {
89         return numargs_;
90 }
91
92
93 void MathMacroTemplate::numargs(int numargs)
94 {
95         numargs_ = numargs;
96 }
97
98
99 docstring MathMacroTemplate::name() const
100 {
101         return name_;
102 }
103
104
105 docstring MathMacroTemplate::prefix() const
106 {
107         return bformat(_(" Macro: %1$s: "), name_);
108 }
109
110
111 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
112 {
113         bool lockMacro = MacroTable::globalMacros().has(name_);
114         if (lockMacro)
115                 MacroTable::globalMacros().get(name_).lock();
116
117         Dimension dim0;
118         cell(0).metrics(mi, dim0);
119         Dimension dim1;
120         cell(1).metrics(mi, dim1);
121         docstring dp = prefix();
122         dim.wid = dim0.width() + dim1.width() + 20
123                 + theFontMetrics(mi.base.font).width(dp);
124         dim.asc = std::max(dim0.ascent(),  dim1.ascent())  + 7;
125         dim.des = std::max(dim0.descent(), dim1.descent()) + 7;
126
127         if (lockMacro)
128                 MacroTable::globalMacros().get(name_).unlock();
129
130         // Cache the inset dimension. 
131         setDimCache(mi, dim);
132 }
133
134
135 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
136 {
137         bool lockMacro = MacroTable::globalMacros().has(name_);
138         if (lockMacro)
139                 MacroTable::globalMacros().get(name_).lock();
140
141         setPosCache(p, x, y);
142
143         Dimension const dim = dimension(*p.base.bv);
144
145         // label
146         FontInfo font = p.base.font;
147         font.setColor(Color_math);
148
149         PainterInfo pi(p.base.bv, p.pain);
150         pi.base.style = LM_ST_TEXT;
151         pi.base.font  = font;
152
153         int const a = y - dim.asc + 1;
154         int const w = dim.wid - 2;
155         int const h = dim.height() - 2;
156
157         // Color_mathbg used to be "AntiqueWhite" but is "linen" now, too
158         // the next line would overwrite the selection!
159         //pi.pain.fillRectangle(x, a, w, h, Color_mathmacrobg);
160         pi.pain.rectangle(x, a, w, h, Color_mathframe);
161
162         // FIXME:
163 #if 0
164         Cursor & cur = p.base.bv->cursor();
165         if (cur.isInside(this))
166                 cur.drawSelection(pi);
167 #endif
168         docstring dp = prefix();
169         pi.pain.text(x + 2, y, dp, font);
170         // FIXME: Painter text should retain the drawn text width
171         x += theFontMetrics(font).width(dp) + 6;
172
173         int const w0 = cell(0).dimension(*pi.base.bv).width();
174         int const w1 = cell(1).dimension(*pi.base.bv).width();
175         cell(0).draw(pi, x + 2, y + 1);
176         pi.pain.rectangle(x, y - dim.ascent() + 3,
177                 w0 + 4, dim.height() - 6, Color_mathline);
178         cell(1).draw(pi, x + 8 + w0, y + 1);
179         pi.pain.rectangle(x + w0 + 6, y - dim.ascent() + 3,
180                 w1 + 4, dim.height() - 6, Color_mathline);
181
182         if (lockMacro)
183                 MacroTable::globalMacros().get(name_).unlock();
184 }
185
186
187 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
188 {
189         MathData ar;
190         mathed_parse_cell(ar, lex.getStream());
191         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
192                 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
193                 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
194                 return;
195         }
196         operator=( *(ar[0]->asMacroTemplate()) );
197 }
198
199
200 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
201 {
202         odocstringstream oss;
203         WriteStream wi(oss, false, false);
204         oss << "FormulaMacro\n";
205         write(wi);
206         os << to_utf8(oss.str());
207 }
208
209
210 void MathMacroTemplate::write(WriteStream & os) const
211 {
212         if (type_ == "def") {
213                 os << "\\def\\" << name_.c_str();
214                 for (int i = 1; i <= numargs_; ++i)
215                         os << '#' << i;
216         } else {
217                 // newcommand or renewcommand
218                 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
219                 if (numargs_ > 0)
220                         os << '[' << numargs_ << ']';
221         }
222
223         os << '{' << cell(0) << "}";
224
225         if (os.latex()) {
226                 // writing .tex. done.
227                 os << "\n";
228         } else {
229                 // writing .lyx, write special .tex export only if necessary
230                 if (!cell(1).empty())
231                         os << "\n{" << cell(1) << '}';
232         }
233 }
234
235
236 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
237                                  OutputParams const &) const
238 {
239         static docstring const str = '[' + buf.B_("math macro") + ']';
240
241         os << str;
242         return str.size();
243 }
244
245
246 MacroData MathMacroTemplate::asMacroData() const
247 {
248         return MacroData(asString(cell(0)), numargs(), asString(cell(1)), std::string());
249 }
250
251
252 } // namespace lyx