]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
small up/down tweaking
[lyx.git] / src / mathed / formula.C
1 /*
2 *  File:        formula.C
3 *  Purpose:     Implementation of formula inset
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *  Description: Allows the edition of math paragraphs inside Lyx.
7 *
8 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
9 *
10 *  Version: 0.4, Lyx project.
11 *
12 *   You are free to use and modify this code under the terms of
13 *   the GNU General Public Licence version 2 or later.
14 */
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include <config.h>
21
22 #include "formula.h"
23 #include "math_cursor.h"
24 #include "math_parser.h"
25 #include "math_charinset.h"
26 #include "math_arrayinset.h"
27 #include "math_metricsinfo.h"
28 #include "math_deliminset.h"
29 #include "math_hullinset.h"
30 #include "math_support.h"
31 #include "math_mathmlstream.h"
32 #include "textpainter.h"
33
34 #include "BufferView.h"
35 #include "gettext.h"
36 #include "debug.h"
37 #include "lyxrc.h"
38 #include "funcrequest.h"
39
40 #include "support/LOstream.h"
41 #include "support/LAssert.h"
42 #include "support/lyxlib.h"
43 #include "support/systemcall.h"
44 #include "support/filetools.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/Painter.h"
49
50 #include "graphics/PreviewedInset.h"
51 #include "graphics/PreviewImage.h"
52
53 #include <fstream>
54
55
56 using std::ostream;
57 using std::ifstream;
58 using std::istream;
59 using std::pair;
60 using std::endl;
61 using std::vector;
62 using std::getline;
63
64
65 class InsetFormula::PreviewImpl : public grfx::PreviewedInset {
66 public:
67         ///
68         PreviewImpl(InsetFormula & p) : PreviewedInset(p) {}
69
70 private:
71         ///
72         bool previewWanted() const;
73         ///
74         string const latexString() const;
75         ///
76         InsetFormula & parent() const
77         {
78                 return *static_cast<InsetFormula*>(inset());
79         }
80 };
81
82
83
84 InsetFormula::InsetFormula()
85         : par_(MathAtom(new MathHullInset)),
86           preview_(new PreviewImpl(*this))
87 {}
88
89
90 InsetFormula::InsetFormula(InsetFormula const & other)
91         : InsetFormulaBase(other),
92           par_(other.par_),
93           preview_(new PreviewImpl(*this))
94 {}
95
96
97 InsetFormula::InsetFormula(BufferView * bv)
98         : par_(MathAtom(new MathHullInset)),
99           preview_(new PreviewImpl(*this))
100 {
101         view_ = bv->owner()->view();
102 }
103
104
105 InsetFormula::InsetFormula(string const & data)
106         : par_(MathAtom(new MathHullInset)),
107           preview_(new PreviewImpl(*this))
108 {
109         if (!data.size())
110                 return;
111         if (!mathed_parse_normal(par_, data))
112                 lyxerr << "cannot interpret '" << data << "' as math\n";
113 }
114
115
116
117 InsetFormula::~InsetFormula()
118 {}
119
120
121 Inset * InsetFormula::clone(Buffer const &, bool) const
122 {
123         return new InsetFormula(*this);
124 }
125
126
127 void InsetFormula::write(Buffer const *, ostream & os) const
128 {
129         WriteStream wi(os, false, false);
130         os << par_->fileInsetLabel() << ' ';
131         par_->write(wi);
132 }
133
134
135 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
136 {
137         WriteStream wi(os, fragile, true);
138         par_->write(wi);
139         return wi.line();
140 }
141
142
143 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
144 {
145         if (0 && display()) {
146                 TextMetricsInfo mi;
147                 par()->metricsT(mi);
148                 TextPainter tpain(par()->width(), par()->height());
149                 par()->drawT(tpain, 0, par()->ascent());
150                 tpain.show(os, 3);
151                 // reset metrics cache to "real" values
152                 metrics();
153                 return tpain.textheight();
154         } else {
155                 WriteStream wi(os, false, true);
156                 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
157                 return wi.line();
158         }
159 }
160
161
162 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
163 {
164         return docbook(buf, os, false);
165 }
166
167
168 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
169 {
170         MathMLStream ms(os);
171         ms << MTag("equation");
172         ms <<   MTag("alt");
173         ms <<    "<[CDATA[";
174         int res = ascii(buf, ms.os(), 0);
175         ms <<    "]]>";
176         ms <<   ETag("alt");
177         ms <<   MTag("math");
178         ms <<    par_;
179         ms <<   ETag("math");
180         ms << ETag("equation");
181         return ms.line() + res;
182 }
183
184
185 void InsetFormula::read(Buffer const *, LyXLex & lex)
186 {
187         mathed_parse_normal(par_, lex);
188         metrics();
189 }
190
191
192 //ostream & operator<<(ostream & os, LyXCursor const & c)
193 //{
194 //      os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
195 //      return os;
196 //}
197
198
199 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
200                         int y, float & xx, bool) const
201 {
202         // This initiates the loading of the preview, so should come
203         // before the metrics are computed.
204         preview_->setView(bv);
205         bool const use_preview = preview_->previewReady();
206
207         int const x = int(xx);
208         int const w = width(bv, font);
209         int const d = descent(bv, font);
210         int const a = ascent(bv, font);
211         int const h = a + d;
212
213         MathPainterInfo pi(bv->painter());
214
215         if (use_preview) {
216                 pi.pain.image(x + 1, y - a, w, h,   // one pixel gap in front
217                               *(preview_->pimage()->image(*this, *bv)));
218         } else {
219                 pi.base.style = LM_ST_TEXT;
220                 pi.base.font  = font;
221                 pi.base.font.setColor(LColor::math);
222                 if (lcolor.getX11Name(LColor::mathbg)
223                             != lcolor.getX11Name(LColor::background))
224                         pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
225
226                 if (mathcursor &&
227                                 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
228                 {
229                         mathcursor->drawSelection(pi);
230                         //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
231                 }
232
233                 par_->draw(pi, x, y);
234         }
235
236         xx += w;
237         xo_ = x;
238         yo_ = y;
239
240         setCursorVisible(false);
241 }
242
243
244 vector<string> const InsetFormula::getLabelList() const
245 {
246         vector<string> res;
247         par()->getLabelList(res);
248         return res;
249 }
250
251
252 Inset::Code InsetFormula::lyxCode() const
253 {
254         return Inset::MATH_CODE;
255 }
256
257
258 void InsetFormula::validate(LaTeXFeatures & features) const
259 {
260         par_->validate(features);
261 }
262
263
264 bool InsetFormula::insetAllowed(Inset::Code code) const
265 {
266         return
267                 (code == Inset::LABEL_CODE)
268                 || code == Inset::REF_CODE
269                 || code == Inset::ERT_CODE;
270 }
271
272
273 int InsetFormula::ascent(BufferView *, LyXFont const &) const
274 {
275         return preview_->previewReady() ?
276                 preview_->pimage()->ascent() : 1 + par_->ascent();
277 }
278
279
280 int InsetFormula::descent(BufferView *, LyXFont const &) const
281 {
282         if (!preview_->previewReady())
283                 return 1 + par_->descent();
284
285         int const descent = preview_->pimage()->descent();
286         return display() ? descent + 12 : descent;
287 }
288
289
290 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
291 {
292         metrics(bv, font);
293         return preview_->previewReady() ?
294                 1 + preview_->pimage()->width() : par_->width();
295                 // insert a one pixel gap in front of the formula
296 }
297
298
299 /*
300 void InsetFormula::mutate(string const & type)
301 {
302         par_.nucleus()->mutate(type);
303 }
304 */
305
306
307 //
308 // preview stuff
309 //
310
311 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
312 {
313         preview_->addPreview(ploader);
314 }
315
316
317 void InsetFormula::generatePreview() const
318 {
319         preview_->generatePreview();
320 }
321
322
323 bool InsetFormula::PreviewImpl::previewWanted() const
324 {
325         return !parent().par_->asNestInset()->editing();
326 }
327
328
329 string const InsetFormula::PreviewImpl::latexString() const
330 {
331         ostringstream ls;
332         WriteStream wi(ls, false, false);
333         parent().par_->write(wi);
334         return STRCONV(ls.str());
335 }