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