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