]> git.lyx.org Git - features.git/blob - src/mathed/MathMacroTemplate.C
32e7bc885bb2192d8f397f9a258335d93f5cff4d
[features.git] / src / mathed / MathMacroTemplate.C
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 "MathMLStream.h"
15 #include "MathParser.h"
16 #include "MathSupport.h"
17
18 #include "cursor.h"
19 #include "debug.h"
20 #include "gettext.h"
21 #include "lyxlex.h"
22 #include "LColor.h"
23
24 #include "frontends/Application.h"
25 #include "frontends/FontLoader.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/lstrings.h"
30
31 using lyx::docstring;
32 using lyx::support::bformat;
33
34 using std::string;
35 using std::auto_ptr;
36 using std::ostream;
37 using std::endl;
38
39
40 MathMacroTemplate::MathMacroTemplate()
41         : InsetMathNest(2), numargs_(0), name_(), type_("newcommand")
42 {
43         initMath();
44 }
45
46
47 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
48                 string const & type, MathArray const & ar1, MathArray const & ar2)
49         : InsetMathNest(2), numargs_(numargs), name_(nm), type_(type)
50 {
51         initMath();
52
53         if (numargs_ > 9)
54                 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
55                         << numargs_ << std::endl;
56         cell(0) = ar1;
57         cell(1) = ar2;
58 }
59
60
61 MathMacroTemplate::MathMacroTemplate(std::istream & is)
62         : InsetMathNest(2), numargs_(0), name_()
63 {
64         initMath();
65
66         MathArray ar;
67         mathed_parse_cell(ar, is);
68         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
69                 lyxerr << "cannot read macro from '" << ar << "'" << endl;
70                 return;
71         }
72         operator=( *(ar[0]->asMacroTemplate()) );
73 }
74
75
76 auto_ptr<InsetBase> MathMacroTemplate::doClone() const
77 {
78         return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
79 }
80
81
82 void MathMacroTemplate::edit(LCursor & cur, bool)
83 {
84         lyxerr << "MathMacroTemplate: edit left/right" << endl;
85         cur.push(*this);
86 }
87
88
89 int MathMacroTemplate::numargs() const
90 {
91         return numargs_;
92 }
93
94
95 void MathMacroTemplate::numargs(int numargs)
96 {
97         numargs_ = numargs;
98 }
99
100
101 string MathMacroTemplate::name() const
102 {
103         return name_;
104 }
105
106
107 docstring MathMacroTemplate::prefix() const
108 {
109         // FIXME UNICODE
110         // delete the conversion when bformat() will return a docstring.
111         // delete the conversion when bformat() takes a docstring arg.
112         return bformat(_(" Macro: %1$s: "), lyx::from_utf8(name_));
113 }
114
115
116 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
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                 + theApp->fontLoader().metrics(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         dim_ = dim;
126 }
127
128
129 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
130 {
131         setPosCache(p, x, y);
132
133         // label
134         LyXFont font = p.base.font;
135         font.setColor(LColor::math);
136
137         PainterInfo pi(p.base.bv, p.pain);
138         pi.base.style = LM_ST_TEXT;
139         pi.base.font  = font;
140
141         int const a = y - dim_.asc + 1;
142         int const w = dim_.wid - 2;
143         int const h = dim_.height() - 2;
144
145         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
146         // the next line would overwrite the selection!
147         //pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
148         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
149
150 #ifdef WITH_WARNINGS
151 #warning FIXME
152 #endif
153 #if 0
154         LCursor & cur = p.base.bv->cursor();
155         if (cur.isInside(this))
156                 cur.drawSelection(pi);
157 #endif
158         docstring dp = prefix();
159         pi.pain.text(x + 2, y, dp, font);
160         // FIXME: Painter text should retain the drawn text width
161         x += theApp->fontLoader().metrics(font).width(dp) + 6;
162
163         int const w0 = cell(0).width();
164         int const w1 = cell(1).width();
165         cell(0).draw(pi, x + 2, y + 1);
166         pi.pain.rectangle(x, y - dim_.ascent() + 3,
167                 w0 + 4, dim_.height() - 6, LColor::mathline);
168         cell(1).draw(pi, x + 8 + w0, y + 1);
169         pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
170                 w1 + 4, dim_.height() - 6, LColor::mathline);
171 }
172
173
174 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
175 {
176         MathArray ar;
177         mathed_parse_cell(ar, lex.getStream());
178         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
179                 lyxerr << "cannot read macro from '" << ar << "'" << endl;
180                 return;
181         }
182         operator=( *(ar[0]->asMacroTemplate()) );
183 }
184
185
186 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
187 {
188         WriteStream wi(os, false, false);
189         os << "FormulaMacro\n";
190         write(wi);
191 }
192
193
194 void MathMacroTemplate::write(WriteStream & os) const
195 {
196         if (type_ == "def") {
197                 os << "\\def\\" << name_.c_str();
198                 for (int i = 1; i <= numargs_; ++i)
199                         os << '#' << i;
200         } else {
201                 // newcommand or renewcommand
202                 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
203                 if (numargs_ > 0)
204                         os << '[' << numargs_ << ']';
205         }
206
207         os << '{' << cell(0) << "}";
208
209         if (os.latex()) {
210                 // writing .tex. done.
211                 os << "\n";
212         } else {
213                 // writing .lyx, write special .tex export only if necessary
214                 if (!cell(1).empty())
215                         os << "\n{" << cell(1) << '}';
216         }
217 }
218
219
220 MacroData MathMacroTemplate::asMacroData() const
221 {
222         return MacroData(asString(cell(0)), numargs(), asString(cell(1)));
223 }