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