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