]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotemplate.C
This fixes the crash when PageDown scrolling UserGuide.lyx
[lyx.git] / src / mathed / math_macrotemplate.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 "math_macrotemplate.h"
14 #include "math_mathmlstream.h"
15 #include "math_parser.h"
16 #include "math_support.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/Painter.h"
25 #include "frontends/font_metrics.h"
26
27 #include "support/lstrings.h"
28
29 using lyx::docstring;
30 using lyx::support::bformat;
31
32 using std::string;
33 using std::auto_ptr;
34 using std::ostream;
35 using std::endl;
36
37
38 MathMacroTemplate::MathMacroTemplate()
39         : MathNestInset(2), numargs_(0), name_(), type_("newcommand")
40 {
41         initMath();
42 }
43
44
45 MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs,
46                 string const & type, MathArray const & ar1, MathArray const & ar2)
47         : MathNestInset(2), numargs_(numargs), name_(nm), 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(std::istream & is)
60         : MathNestInset(2), numargs_(0), name_()
61 {
62         initMath();
63
64         MathArray ar;
65         mathed_parse_cell(ar, is);
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 auto_ptr<InsetBase> MathMacroTemplate::doClone() const
75 {
76         return auto_ptr<InsetBase>(new MathMacroTemplate(*this));
77 }
78
79
80 void MathMacroTemplate::edit(LCursor & 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 string MathMacroTemplate::name() const
100 {
101         return name_;
102 }
103
104
105 docstring MathMacroTemplate::prefix() const
106 {
107         // FIXME: delete the conversion when bformat() will return a docstring.
108         return lyx::from_utf8(bformat(_(" Macro: %1$s: "), name_));
109 }
110
111
112 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
113 {
114         cell(0).metrics(mi);
115         cell(1).metrics(mi);
116         docstring dp = prefix();
117         dim.wid = cell(0).width() + cell(1).width() + 20
118                 + font_metrics::width(dp, mi.base.font);
119         dim.asc = std::max(cell(0).ascent(),  cell(1).ascent())  + 7;
120         dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7;
121         dim_ = dim;
122 }
123
124
125 void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const
126 {
127         setPosCache(p, x, y);
128
129         // label
130         LyXFont font = p.base.font;
131         font.setColor(LColor::math);
132
133         PainterInfo pi(p.base.bv, p.pain);
134         pi.base.style = LM_ST_TEXT;
135         pi.base.font  = font;
136
137         int const a = y - dim_.asc + 1;
138         int const w = dim_.wid - 2;
139         int const h = dim_.height() - 2;
140
141         // LColor::mathbg used to be "AntiqueWhite" but is "linen" now, too
142         // the next line would overwrite the selection!
143         //pi.pain.fillRectangle(x, a, w, h, LColor::mathmacrobg);
144         pi.pain.rectangle(x, a, w, h, LColor::mathframe);
145
146 #ifdef WITH_WARNINGS
147 #warning FIXME
148 #endif
149 #if 0
150         LCursor & cur = p.base.bv->cursor();
151         if (cur.isInside(this))
152                 cur.drawSelection(pi);
153 #endif
154         docstring dp = prefix();
155         pi.pain.text(x + 2, y, dp, font);
156         x += font_metrics::width(dp, pi.base.font) + 6;
157
158         int const w0 = cell(0).width();
159         int const w1 = cell(1).width();
160         cell(0).draw(pi, x + 2, y + 1);
161         pi.pain.rectangle(x, y - dim_.ascent() + 3,
162                 w0 + 4, dim_.height() - 6, LColor::mathline);
163         cell(1).draw(pi, x + 8 + w0, y + 1);
164         pi.pain.rectangle(x + w0 + 6, y - dim_.ascent() + 3,
165                 w1 + 4, dim_.height() - 6, LColor::mathline);
166 }
167
168
169 void MathMacroTemplate::read(Buffer const &, LyXLex & lex)
170 {
171         MathArray ar;
172         mathed_parse_cell(ar, lex.getStream());
173         if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
174                 lyxerr << "cannot read macro from '" << ar << "'" << endl;
175                 return;
176         }
177         operator=( *(ar[0]->asMacroTemplate()) );
178 }
179
180
181 void MathMacroTemplate::write(Buffer const &, std::ostream & os) const
182 {
183         WriteStream wi(os, false, false);
184         os << "FormulaMacro\n";
185         write(wi);
186 }
187
188
189 void MathMacroTemplate::write(WriteStream & os) const
190 {
191         if (type_ == "def") {
192                 os << "\\def\\" << name_.c_str();
193                 for (int i = 1; i <= numargs_; ++i)
194                         os << '#' << i;
195         } else {
196                 // newcommand or renewcommand
197                 os << "\\" << type_.c_str() << "{\\" << name_.c_str() << '}';
198                 if (numargs_ > 0)
199                         os << '[' << numargs_ << ']';
200         }
201
202         os << '{' << cell(0) << "}";
203
204         if (os.latex()) {
205                 // writing .tex. done.
206                 os << "\n";
207         } else {
208                 // writing .lyx, write special .tex export only if necessary
209                 if (!cell(1).empty())
210                         os << "\n{" << cell(1) << '}';
211         }
212 }
213
214
215 MacroData MathMacroTemplate::asMacroData() const
216 {
217         return MacroData(asString(cell(0)), numargs(), asString(cell(1)));
218 }