]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
most of the so far unapplied stuff from porto including proper support for
[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 #warning FIXME: please check you really mean repaint() ... is it needed,
282 #warning and if so, should it be update() instead ? 
283                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
284                                 bv->repaint();
285
286                         hull()->label(row, new_label);
287
288                         updateLocal(bv, true);
289                         break;
290                 }
291
292                 case LFUN_MATH_MUTATE:
293                 {
294                         bv->lockedInsetStoreUndo(Undo::EDIT);
295                         int x;
296                         int y;
297                         mathcursor->getPos(x, y);
298                         hull()->mutate(arg);
299                         mathcursor->setPos(x, y);
300                         mathcursor->normalize();
301                         updateLocal(bv, true);
302                         break;
303                 }
304
305                 case LFUN_MATH_EXTERN:
306                 {
307                         bv->lockedInsetStoreUndo(Undo::EDIT);
308                         if (mathcursor)
309                                 mathcursor->handleExtern(arg);
310                         // re-compute inset dimension
311                         metrics(bv);
312                         updateLocal(bv, true);
313                         break;
314                 }
315
316                 case LFUN_MATH_DISPLAY:
317                 {
318                         int x = 0;
319                         int y = 0;
320                         mathcursor->getPos(x, y);
321                         if (getType() == LM_OT_SIMPLE)
322                                 hull()->mutate(LM_OT_EQUATION);
323                         else
324                                 hull()->mutate(LM_OT_SIMPLE);
325                         mathcursor->setPos(x, y);
326                         mathcursor->normalize();
327                         updateLocal(bv, true);
328                         break;
329                 }
330
331                 case LFUN_PASTESELECTION:
332                 {
333                         string const clip = bv->getClipboard();
334                 if (!clip.empty())
335                                 mathed_parse_normal(par_, clip);
336                         break;
337                 }
338
339                 default:
340                         result = InsetFormulaBase::localDispatch(bv, action, arg);
341         }
342
343         return result;
344 }
345
346
347 bool InsetFormula::display() const
348 {
349         return getType() != LM_OT_SIMPLE;
350 }
351
352
353 MathHullInset const * InsetFormula::hull() const
354 {
355         lyx::Assert(par_->asHullInset());
356         return par_->asHullInset();
357 }
358
359
360 MathHullInset * InsetFormula::hull()
361 {
362         lyx::Assert(par_->asHullInset());
363         return par_->asHullInset();
364 }
365
366
367 Inset::Code InsetFormula::lyxCode() const
368 {
369         return Inset::MATH_CODE;
370 }
371
372
373 void InsetFormula::validate(LaTeXFeatures & features) const
374 {
375         par_->validate(features);
376 }
377
378
379 bool InsetFormula::insetAllowed(Inset::Code code) const
380 {
381         return
382                 (code == Inset::LABEL_CODE && display())
383                 || code == Inset::REF_CODE      
384                 || code == Inset::ERT_CODE;
385 }
386
387
388 int InsetFormula::ascent(BufferView *, LyXFont const &) const
389 {
390         return par_->ascent() + 1;
391 }
392
393
394 int InsetFormula::descent(BufferView *, LyXFont const &) const
395 {
396         return par_->descent() + 1;
397 }
398
399
400 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
401 {
402         metrics(bv, font);
403         return par_->width();
404 }
405
406
407 MathInsetTypes InsetFormula::getType() const
408 {
409         return hull()->getType();
410 }