]> git.lyx.org Git - features.git/blob - src/mathed/formula.C
the ascent/descent/width -> dimensions() change
[features.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 "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 #include "Lsstream.h"
31
32 #include "BufferView.h"
33 #include "gettext.h"
34 #include "debug.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 * bv)
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 &, bool) const
117 {
118         return new InsetFormula(*this);
119 }
120
121
122 void InsetFormula::write(Buffer const *, ostream & os) const
123 {
124         WriteStream wi(os, false, false);
125         os << par_->fileInsetLabel() << ' ';
126         par_->write(wi);
127 }
128
129
130 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
131 {
132         WriteStream wi(os, fragile, true);
133         par_->write(wi);
134         return wi.line();
135 }
136
137
138 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
139 {
140         if (0 && display()) {
141                 TextMetricsInfo mi;
142                 par()->metricsT(mi);
143                 TextPainter tpain(par()->width(), par()->height());
144                 par()->drawT(tpain, 0, par()->ascent());
145                 tpain.show(os, 3);
146                 // reset metrics cache to "real" values
147                 metrics();
148                 return tpain.textheight();
149         } else {
150                 WriteStream wi(os, false, true);
151                 wi << ' ' << (par_->asNestInset()->cell(0)) << ' ';
152                 return wi.line();
153         }
154 }
155
156
157 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
158 {
159         return docbook(buf, os, false);
160 }
161
162
163 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
164 {
165         MathMLStream ms(os);
166         ms << MTag("equation");
167         ms <<   MTag("alt");
168         ms <<    "<[CDATA[";
169         int res = ascii(buf, ms.os(), 0);
170         ms <<    "]]>";
171         ms <<   ETag("alt");
172         ms <<   MTag("math");
173         ms <<    par_;
174         ms <<   ETag("math");
175         ms << ETag("equation");
176         return ms.line() + res;
177 }
178
179
180 void InsetFormula::read(Buffer const *, LyXLex & lex)
181 {
182         mathed_parse_normal(par_, lex);
183         // remove extra 'mathrm' for chemistry stuff.
184         // will be re-added on write
185         if (par_->asHullInset()->getType() =="chemistry")  {
186                 lyxerr << "this is chemistry\n";
187                 if (par_->cell(0).size() == 1) {
188                         lyxerr << "this is size 1\n";
189             if (par_->cell(0)[0]->asFontInset()) {
190                                 lyxerr << "this is a font inset \n";
191                                 lyxerr << "replacing " << par_.nucleus()->cell(0) << 
192                                         " with " << par_->cell(0)[0]->cell(0) << "\n";
193                         }
194                 }
195         }
196         metrics();
197 }
198
199
200 //ostream & operator<<(ostream & os, LyXCursor const & c)
201 //{
202 //      os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
203 //      return os;
204 //}
205
206
207 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
208                         int y, float & xx) const
209 {
210         cache(bv);
211         // This initiates the loading of the preview, so should come
212         // before the metrics are computed.
213         bool const use_preview = preview_->previewReady();
214
215         int const x = int(xx);
216         int const w = width(bv, font);
217         int const d = descent(bv, font);
218         int const a = ascent(bv, font);
219         int const h = a + d;
220
221         PainterInfo pi(bv->painter());
222
223         if (use_preview) {
224                 pi.pain.image(x + 1, y - a, w, h,   // one pixel gap in front
225                               *(preview_->pimage()->image()));
226         } else {
227                 pi.base.style = LM_ST_TEXT;
228                 pi.base.font  = font;
229                 pi.base.font.setColor(LColor::math);
230                 if (lcolor.getX11Name(LColor::mathbg)
231                             != lcolor.getX11Name(LColor::background))
232                         pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
233
234                 if (mathcursor &&
235                                 const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
236                 {
237                         mathcursor->drawSelection(pi);
238                         //pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
239                 }
240
241                 par_->draw(pi, x, y);
242         }
243
244         xx += w;
245         xo_ = x;
246         yo_ = y;
247 }
248
249
250 vector<string> const InsetFormula::getLabelList() const
251 {
252         vector<string> res;
253         par()->getLabelList(res);
254         return res;
255 }
256
257
258 Inset::Code InsetFormula::lyxCode() const
259 {
260         return Inset::MATH_CODE;
261 }
262
263
264 void InsetFormula::validate(LaTeXFeatures & features) const
265 {
266         par_->validate(features);
267 }
268
269
270 bool InsetFormula::insetAllowed(Inset::Code code) const
271 {
272         return
273                    code == Inset::LABEL_CODE
274                 || code == Inset::REF_CODE
275                 || code == Inset::ERT_CODE;
276 }
277
278
279 void InsetFormula::dimension(BufferView * bv, LyXFont const & font,
280         Dimension & dim) const
281 {
282         metrics(bv, font);
283         if (preview_->previewReady()) {
284                 dim.a = preview_->pimage()->ascent();
285                 int const descent = preview_->pimage()->descent();
286                 dim.d = display() ? descent + 12 : descent;
287                 // insert a one pixel gap in front of the formula
288                 dim.w = 1 + preview_->pimage()->width();
289         } else {
290                 dim = par_->dimensions();
291                 dim.a += 1;
292                 dim.d += 1;
293         }
294 }
295
296
297 void InsetFormula::mutate(string const & type)
298 {
299         par_.nucleus()->mutate(type);
300 }
301
302
303 //
304 // preview stuff
305 //
306
307 void InsetFormula::addPreview(grfx::PreviewLoader & ploader) const
308 {
309         preview_->addPreview(ploader);
310 }
311
312
313 void InsetFormula::generatePreview() const
314 {
315         preview_->generatePreview();
316 }
317
318
319 bool InsetFormula::PreviewImpl::previewWanted() const
320 {
321         return !parent().par_->asNestInset()->editing();
322 }
323
324
325 string const InsetFormula::PreviewImpl::latexString() const
326 {
327         ostringstream ls;
328         WriteStream wi(ls, false, false);
329         parent().par_->write(wi);
330         return STRCONV(ls.str());
331 }
332