]> git.lyx.org Git - features.git/blob - src/mathed/formula.C
first shot at preview. It crashes. Don't know why. To see it work a bit, 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 #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 x = int(xx);
169         int w = par_->width();
170         int h = par_->height();
171         int a = par_->ascent();
172
173         MathPainterInfo pi(bv->painter());
174         pi.base.font  = font;
175         pi.base.font.setColor(LColor::math);
176         pi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
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 0
192         ostringstream os;
193         WriteStream wi(os, false, false);
194         par_->write(wi);
195         if (preview(os.str(), preview_) && preview_.get()) {
196                 cerr << "image could be drawn\n";
197                 // LyX crashes if we try that. Why? Andre'
198                 //pi.pain.image(x, y, w, h, *(preview_->image()));
199         }
200 #endif
201
202         xx += par_->width();
203         xo_ = x;
204         yo_ = y;
205
206         setCursorVisible(false);
207 }
208
209
210 vector<string> const InsetFormula::getLabelList() const
211 {
212         return hull()->getLabelList();
213 }
214
215
216 UpdatableInset::RESULT
217 InsetFormula::localDispatch(BufferView * bv, kb_action action,
218          string const & arg)
219 {
220         RESULT result = DISPATCHED;
221
222         switch (action) {
223
224                 case LFUN_BREAKLINE:
225                         bv->lockedInsetStoreUndo(Undo::INSERT);
226                         mathcursor->breakLine();
227                         mathcursor->normalize();
228                         updateLocal(bv, true);
229                         break;
230
231                 case LFUN_MATH_NUMBER:
232                 {
233                         //lyxerr << "toggling all numbers\n";
234                         if (display()) {
235                                 bv->lockedInsetStoreUndo(Undo::INSERT);
236                                 bool old = hull()->numberedType();
237                                 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
238                                         hull()->numbered(row, !old);
239                                 bv->owner()->message(old ? _("No number") : _("Number"));
240                                 updateLocal(bv, true);
241                         }
242                         break;
243                 }
244
245                 case LFUN_MATH_NONUMBER:
246                 {
247                         //lyxerr << "toggling line number\n";
248                         if (display()) {
249                                 bv->lockedInsetStoreUndo(Undo::INSERT);
250                                 MathCursor::row_type row = mathcursor->hullRow();
251                                 bool old = hull()->numbered(row);
252                                 bv->owner()->message(old ? _("No number") : _("Number"));
253                                 hull()->numbered(row, !old);
254                                 updateLocal(bv, true);
255                         }
256                         break;
257                 }
258
259                 case LFUN_INSERT_LABEL:
260                 {
261                         bv->lockedInsetStoreUndo(Undo::INSERT);
262
263                         MathCursor::row_type row = mathcursor->hullRow();
264                         string old_label = hull()->label(row);
265                         string new_label = arg;
266
267                         if (new_label.empty()) {
268                                 string const default_label =
269                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
270                                 pair<bool, string> const res = old_label.empty()
271                                         ? Alert::askForText(_("Enter new label to insert:"), default_label)
272                                         : Alert::askForText(_("Enter label:"), old_label);
273                                 if (!res.first)
274                                         break;
275                                 new_label = frontStrip(strip(res.second));
276                         }
277
278                         //if (new_label == old_label)
279                         //      break;  // Nothing to do
280
281                         if (!new_label.empty()) {
282                                 lyxerr << "setting label to '" << new_label << "'\n";
283                                 hull()->numbered(row, true);
284                         }
285
286 #warning FIXME: please check you really mean repaint() ... is it needed,
287 #warning and if so, should it be update() instead ? 
288                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
289                                 bv->repaint();
290
291                         hull()->label(row, new_label);
292
293                         updateLocal(bv, true);
294                         break;
295                 }
296
297                 case LFUN_MATH_MUTATE:
298                 {
299                         bv->lockedInsetStoreUndo(Undo::EDIT);
300                         int x;
301                         int y;
302                         mathcursor->getPos(x, y);
303                         hull()->mutate(arg);
304                         mathcursor->setPos(x, y);
305                         mathcursor->normalize();
306                         updateLocal(bv, true);
307                         break;
308                 }
309
310                 case LFUN_MATH_EXTERN:
311                 {
312                         bv->lockedInsetStoreUndo(Undo::EDIT);
313                         if (mathcursor)
314                                 mathcursor->handleExtern(arg);
315                         // re-compute inset dimension
316                         metrics(bv);
317                         updateLocal(bv, true);
318                         break;
319                 }
320
321                 case LFUN_MATH_DISPLAY:
322                 {
323                         int x = 0;
324                         int y = 0;
325                         mathcursor->getPos(x, y);
326                         if (getType() == LM_OT_SIMPLE)
327                                 hull()->mutate(LM_OT_EQUATION);
328                         else
329                                 hull()->mutate(LM_OT_SIMPLE);
330                         mathcursor->setPos(x, y);
331                         mathcursor->normalize();
332                         updateLocal(bv, true);
333                         break;
334                 }
335
336                 case LFUN_PASTESELECTION:
337                 {
338                         string const clip = bv->getClipboard();
339                 if (!clip.empty())
340                                 mathed_parse_normal(par_, clip);
341                         break;
342                 }
343
344                 default:
345                         result = InsetFormulaBase::localDispatch(bv, action, arg);
346         }
347
348         return result;
349 }
350
351
352 bool InsetFormula::display() const
353 {
354         return getType() != LM_OT_SIMPLE;
355 }
356
357
358 MathHullInset const * InsetFormula::hull() const
359 {
360         lyx::Assert(par_->asHullInset());
361         return par_->asHullInset();
362 }
363
364
365 MathHullInset * InsetFormula::hull()
366 {
367         lyx::Assert(par_->asHullInset());
368         return par_->asHullInset();
369 }
370
371
372 Inset::Code InsetFormula::lyxCode() const
373 {
374         return Inset::MATH_CODE;
375 }
376
377
378 void InsetFormula::validate(LaTeXFeatures & features) const
379 {
380         par_->validate(features);
381 }
382
383
384 bool InsetFormula::insetAllowed(Inset::Code code) const
385 {
386         return
387                 (code == Inset::LABEL_CODE && display())
388                 || code == Inset::REF_CODE      
389                 || code == Inset::ERT_CODE;
390 }
391
392
393 int InsetFormula::ascent(BufferView *, LyXFont const &) const
394 {
395         return par_->ascent() + 1;
396 }
397
398
399 int InsetFormula::descent(BufferView *, LyXFont const &) const
400 {
401         return par_->descent() + 1;
402 }
403
404
405 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
406 {
407         metrics(bv, font);
408         return par_->width();
409 }
410
411
412 MathInsetTypes InsetFormula::getType() const
413 {
414         return hull()->getType();
415 }