]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
preview cosmetics
[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 "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_charinset.h"
27 #include "math_arrayinset.h"
28 #include "math_deliminset.h"
29 #include "lyx_main.h"
30 #include "BufferView.h"
31 #include "gettext.h"
32 #include "debug.h"
33 #include "frontends/Alert.h"
34 #include "support/LOstream.h"
35 #include "support/LAssert.h"
36 #include "support/filetools.h" // LibFileSearch
37 #include "frontends/LyXView.h"
38 #include "frontends/Painter.h"
39 #include "lyxrc.h"
40 #include "math_hullinset.h"
41 #include "math_support.h"
42 #include "math_mathmlstream.h"
43 #include "textpainter.h"
44 #include "preview.h"
45
46 #include "graphics/GraphicsCacheItem.h"
47
48 using std::ostream;
49 using std::ifstream;
50 using std::istream;
51 using std::pair;
52 using std::endl;
53 using std::vector;
54 using std::getline;
55
56
57
58 InsetFormula::InsetFormula()
59         : par_(MathAtom(new MathHullInset))
60 {}
61
62
63 InsetFormula::InsetFormula(MathInsetTypes t)
64         : par_(MathAtom(new MathHullInset(t)))
65 {}
66
67
68 InsetFormula::InsetFormula(string const & s)
69 {
70         if (s.size()) {
71                 bool res = mathed_parse_normal(par_, s);
72
73                 if (!res)
74                         res = mathed_parse_normal(par_, "$" + s + "$");
75
76                 if (!res) {
77                         lyxerr << "cannot interpret '" << s << "' as math\n";
78                         par_ = MathAtom(new MathHullInset(LM_OT_SIMPLE));
79                 }
80         }
81         metrics();
82 }
83
84
85 Inset * InsetFormula::clone(Buffer const &, bool) const
86 {
87         return new InsetFormula(*this);
88 }
89
90
91 void InsetFormula::write(Buffer const *, ostream & os) const
92 {
93         os << "Formula ";
94         WriteStream wi(os, false, false);
95         par_->write(wi);
96 }
97
98
99 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
100 {
101         WriteStream wi(os, fragile, true);
102         par_->write(wi);
103         return wi.line();
104 }
105
106
107 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
108 {
109 #if 0
110         TextMetricsInfo mi;
111         par()->metricsT(mi);
112         TextPainter tpain(par()->width(), par()->height());
113         par()->drawT(tpain, 0, par()->ascent());
114         tpain.show(os);
115         // reset metrics cache to "real" values
116         metrics();
117         return tpain.textheight();
118 #else
119         WriteStream wi(os, false, true);
120         par_->write(wi);
121         return wi.line();
122 #endif
123 }
124
125
126 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
127 {
128         return docbook(buf, os, false);
129 }
130
131
132 int InsetFormula::docbook(Buffer const * buf, ostream & os, bool) const
133 {
134         MathMLStream ms(os);
135         ms << MTag("equation");
136         ms <<   MTag("alt");
137         ms <<    "<[CDATA[";
138         int res = ascii(buf, ms.os(), 0);
139         ms <<    "]]>";
140         ms <<   ETag("alt");
141         ms <<   MTag("math");
142         ms <<    par_.nucleus();
143         ms <<   ETag("math");
144         ms << ETag("equation");
145         return ms.line() + res;
146 }
147
148
149 void InsetFormula::read(Buffer const *, LyXLex & lex)
150 {
151         mathed_parse_normal(par_, lex);
152         metrics();
153 }
154
155
156 //ostream & operator<<(ostream & os, LyXCursor const & c)
157 //{
158 //      os << '[' << c.x() << ' ' << c.y() << ' ' << c.pos() << ']';
159 //      return os;
160 //}
161
162
163 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
164                         int y, float & xx, bool) const
165 {
166         metrics(bv, font);
167
168         int const x = int(xx);
169         int const w = par_->width();
170         int const h = par_->height();
171         int const a = par_->ascent();
172
173         MathPainterInfo pi(bv->painter());
174         pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
175         pi.base.font  = font;
176         pi.base.font.setColor(LColor::math);
177
178         if (lcolor.getX11Name(LColor::mathbg)!=lcolor.getX11Name(LColor::background))
179                 pi.pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
180
181         if (mathcursor &&
182                         const_cast<InsetFormulaBase const *>(mathcursor->formula()) == this)
183         {
184                 mathcursor->drawSelection(pi);
185                 pi.pain.rectangle(x, y - a, w, h, LColor::mathframe);
186         }
187
188         par_->draw(pi, x, y);
189
190         // preview stuff
191         if (lyxrc.preview) {
192                 ostringstream os;
193                 WriteStream wi(os, false, false);
194                 par_->write(wi);
195                 if (preview(os.str(), preview_)) {
196                         cerr << "image could be drawn\n";
197                         pi.pain.image(x + w + 2, y - a + 1, w - 2, h - 2, *(preview_->image()));
198                 } else {
199                         pi.pain.fillRectangle(x + w, y - a, w, h, LColor::white);
200                 }
201                 pi.pain.rectangle(x + w, y - a, w, h, LColor::mathframe);
202                 xx += w;
203         }
204
205         xx += w;
206         xo_ = x;
207         yo_ = y;
208
209         setCursorVisible(false);
210 }
211
212
213 vector<string> const InsetFormula::getLabelList() const
214 {
215         return hull()->getLabelList();
216 }
217
218
219 UpdatableInset::RESULT
220 InsetFormula::localDispatch(BufferView * bv, kb_action action,
221          string const & arg)
222 {
223         RESULT result = DISPATCHED;
224
225         switch (action) {
226
227                 case LFUN_BREAKLINE:
228                         bv->lockedInsetStoreUndo(Undo::INSERT);
229                         mathcursor->breakLine();
230                         mathcursor->normalize();
231                         updateLocal(bv, true);
232                         break;
233
234                 case LFUN_MATH_NUMBER:
235                 {
236                         //lyxerr << "toggling all numbers\n";
237                         if (display()) {
238                                 bv->lockedInsetStoreUndo(Undo::INSERT);
239                                 bool old = hull()->numberedType();
240                                 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
241                                         hull()->numbered(row, !old);
242                                 bv->owner()->message(old ? _("No number") : _("Number"));
243                                 updateLocal(bv, true);
244                         }
245                         break;
246                 }
247
248                 case LFUN_MATH_NONUMBER:
249                 {
250                         //lyxerr << "toggling line number\n";
251                         if (display()) {
252                                 bv->lockedInsetStoreUndo(Undo::INSERT);
253                                 MathCursor::row_type row = mathcursor->hullRow();
254                                 bool old = hull()->numbered(row);
255                                 bv->owner()->message(old ? _("No number") : _("Number"));
256                                 hull()->numbered(row, !old);
257                                 updateLocal(bv, true);
258                         }
259                         break;
260                 }
261
262                 case LFUN_INSERT_LABEL:
263                 {
264                         bv->lockedInsetStoreUndo(Undo::INSERT);
265
266                         MathCursor::row_type row = mathcursor->hullRow();
267                         string old_label = hull()->label(row);
268                         string new_label = arg;
269
270                         if (new_label.empty()) {
271                                 string const default_label =
272                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
273                                 pair<bool, string> const res = old_label.empty()
274                                         ? Alert::askForText(_("Enter new label to insert:"), default_label)
275                                         : Alert::askForText(_("Enter label:"), old_label);
276                                 if (!res.first)
277                                         break;
278                                 new_label = frontStrip(strip(res.second));
279                         }
280
281                         //if (new_label == old_label)
282                         //      break;  // Nothing to do
283
284                         if (!new_label.empty()) {
285                                 lyxerr << "setting label to '" << new_label << "'\n";
286                                 hull()->numbered(row, true);
287                         }
288
289 #warning FIXME: please check you really mean repaint() ... is it needed,
290 #warning and if so, should it be update() instead ? 
291                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
292                                 bv->repaint();
293
294                         hull()->label(row, new_label);
295
296                         updateLocal(bv, true);
297                         break;
298                 }
299
300                 case LFUN_MATH_MUTATE:
301                 {
302                         bv->lockedInsetStoreUndo(Undo::EDIT);
303                         int x;
304                         int y;
305                         mathcursor->getPos(x, y);
306                         hull()->mutate(arg);
307                         mathcursor->setPos(x, y);
308                         mathcursor->normalize();
309                         updateLocal(bv, true);
310                         break;
311                 }
312
313                 case LFUN_MATH_EXTERN:
314                 {
315                         bv->lockedInsetStoreUndo(Undo::EDIT);
316                         if (mathcursor)
317                                 mathcursor->handleExtern(arg);
318                         // re-compute inset dimension
319                         metrics(bv);
320                         updateLocal(bv, true);
321                         break;
322                 }
323
324                 case LFUN_MATH_DISPLAY:
325                 {
326                         int x = 0;
327                         int y = 0;
328                         mathcursor->getPos(x, y);
329                         if (getType() == LM_OT_SIMPLE)
330                                 hull()->mutate(LM_OT_EQUATION);
331                         else
332                                 hull()->mutate(LM_OT_SIMPLE);
333                         mathcursor->setPos(x, y);
334                         mathcursor->normalize();
335                         updateLocal(bv, true);
336                         break;
337                 }
338
339                 case LFUN_PASTESELECTION:
340                 {
341                         string const clip = bv->getClipboard();
342                 if (!clip.empty())
343                                 mathed_parse_normal(par_, clip);
344                         break;
345                 }
346
347                 default:
348                         result = InsetFormulaBase::localDispatch(bv, action, arg);
349         }
350
351         return result;
352 }
353
354
355 bool InsetFormula::display() const
356 {
357         return getType() != LM_OT_SIMPLE;
358 }
359
360
361 MathHullInset const * InsetFormula::hull() const
362 {
363         lyx::Assert(par_->asHullInset());
364         return par_->asHullInset();
365 }
366
367
368 MathHullInset * InsetFormula::hull()
369 {
370         lyx::Assert(par_->asHullInset());
371         return par_->asHullInset();
372 }
373
374
375 Inset::Code InsetFormula::lyxCode() const
376 {
377         return Inset::MATH_CODE;
378 }
379
380
381 void InsetFormula::validate(LaTeXFeatures & features) const
382 {
383         par_->validate(features);
384 }
385
386
387 bool InsetFormula::insetAllowed(Inset::Code code) const
388 {
389         return
390                 (code == Inset::LABEL_CODE && display())
391                 || code == Inset::REF_CODE      
392                 || code == Inset::ERT_CODE;
393 }
394
395
396 int InsetFormula::ascent(BufferView *, LyXFont const &) const
397 {
398         return par_->ascent() + 1;
399 }
400
401
402 int InsetFormula::descent(BufferView *, LyXFont const &) const
403 {
404         return par_->descent() + 1;
405 }
406
407
408 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
409 {
410         metrics(bv, font);
411         int const w = par_->width();
412         // double the space for the preview if needed
413         return lyxrc.preview ? 2 * w : w;
414 }
415
416
417 MathInsetTypes InsetFormula::getType() const
418 {
419         return hull()->getType();
420 }