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