]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
some visual support for \lefteqn
[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 #include <fstream>
22
23 #include "formula.h"
24 #include "commandtags.h"
25 #include "math_cursor.h"
26 #include "math_parser.h"
27 #include "math_charinset.h"
28 #include "lyx_main.h"
29 #include "BufferView.h"
30 #include "gettext.h"
31 #include "debug.h"
32 #include "lyx_gui_misc.h"
33 #include "support/LOstream.h"
34 #include "support/LAssert.h"
35 #include "support/lyxlib.h"
36 #include "support/syscall.h"
37 #include "support/lstrings.h"
38 #include "LyXView.h"
39 #include "Painter.h"
40 #include "lyxrc.h"
41 #include "math_matrixinset.h"
42 #include "mathed/support.h"
43
44 using std::ostream;
45 using std::ifstream;
46 using std::istream;
47 using std::pair;
48 using std::endl;
49 using std::vector;
50
51
52
53 InsetFormula::InsetFormula()
54         : par_(MathAtom(new MathMatrixInset))
55 {}
56
57
58 InsetFormula::InsetFormula(MathInsetTypes t)
59         : par_(MathAtom(new MathMatrixInset(t)))
60 {}
61
62
63 InsetFormula::InsetFormula(string const & s) 
64 {
65         if (s.size()) {
66                 bool res = mathed_parse_normal(par_, s);
67
68                 if (!res)
69                         res = mathed_parse_normal(par_, "$" + s + "$");
70
71                 if (!res) {
72                         lyxerr << "cannot interpret '" << s << "' as math\n";
73                         par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
74                 }
75         }
76         metrics();
77 }
78
79
80 Inset * InsetFormula::clone(Buffer const &, bool) const
81 {
82         return new InsetFormula(*this);
83 }
84
85
86 void InsetFormula::write(Buffer const * buf, ostream & os) const
87 {
88         os << "Formula ";
89         latex(buf, os, false, false);
90 }
91
92
93 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
94 {
95         par_->write(os, fragile);
96         return 1;
97 }
98
99
100 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
101 {
102         par_->write(os, false);
103         return 1;
104 }
105
106
107 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
108 {
109         return ascii(buf, os, 0);
110 }
111
112
113 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
114 {
115         return ascii(buf, os, 0);
116 }
117
118
119 void InsetFormula::read(Buffer const *, LyXLex & lex)
120 {
121         mathed_parse_normal(par_, lex);
122         metrics();
123 }
124
125
126 void InsetFormula::draw(BufferView * bv, LyXFont const &,
127                         int y, float & xx, bool) const
128 {
129         int x = int(xx) - 1;
130         y -= 2;
131
132         MathInset::workwidth = bv->workWidth();
133         Painter & pain = bv->painter();
134
135         metrics();
136         int w = par_->width();
137         int h = par_->height();
138         int a = par_->ascent();
139         pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
140
141         if (mathcursor && mathcursor->formula() == this) {
142                 mathcursor->drawSelection(pain);
143                 pain.rectangle(x, y - a, w, h, LColor::mathframe);
144         }
145
146         par_->draw(pain, x, y);
147         xx += par_->width();
148
149         setCursorVisible(false);
150 }
151
152
153 void InsetFormula::metrics() const 
154 {
155         par_->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
156 }
157
158
159 vector<string> const InsetFormula::getLabelList() const
160 {
161         return mat()->getLabelList();
162 }
163
164
165 UpdatableInset::RESULT
166 InsetFormula::localDispatch(BufferView * bv, kb_action action,
167          string const & arg)
168 {
169         RESULT result = DISPATCHED;
170
171         switch (action) {
172
173                 case LFUN_BREAKLINE: 
174                         bv->lockedInsetStoreUndo(Undo::INSERT);
175                         mathcursor->breakLine();
176                         mathcursor->normalize();
177                         updateLocal(bv, true);
178                         break;
179
180                 case LFUN_MATH_NUMBER:
181                 {
182                         //lyxerr << "toggling all numbers\n";
183                         if (display()) {
184                                 bv->lockedInsetStoreUndo(Undo::INSERT);
185                                 bool old = mat()->numberedType();
186                                 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
187                                         mat()->numbered(row, !old);
188                                 bv->owner()->message(old ? _("No number") : _("Number"));
189                                 updateLocal(bv, true);
190                         }
191                         break;
192                 }
193
194                 case LFUN_MATH_NONUMBER:
195                 {
196                         //lyxerr << "toggling line number\n";
197                         if (display()) {
198                                 bv->lockedInsetStoreUndo(Undo::INSERT);
199                                 MathCursor::row_type row = mathcursor->row();
200                                 bool old = mat()->numbered(row);
201                                 bv->owner()->message(old ? _("No number") : _("Number"));
202                                 mat()->numbered(row, !old);
203                                 updateLocal(bv, true);
204                         }
205                         break;
206                 }
207
208                 case LFUN_INSERT_LABEL:
209                 {
210                         bv->lockedInsetStoreUndo(Undo::INSERT);
211
212                         MathCursor::row_type row = mathcursor->row();
213                         string old_label = mat()->label(row);
214                         string new_label = arg;
215
216                         if (new_label.empty()) {
217                                 string const default_label =
218                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
219                                 pair<bool, string> const res = old_label.empty()
220                                         ? askForText(_("Enter new label to insert:"), default_label)
221                                         : askForText(_("Enter label:"), old_label);
222                                 
223                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
224                                 if (!res.first)
225                                         break;
226                                 new_label = frontStrip(strip(res.second));
227                         }
228
229                         //if (new_label == old_label)
230                         //      break;  // Nothing to do
231
232                         if (!new_label.empty()) {
233                                 lyxerr << "setting label to '" << new_label << "'\n";
234                                 mat()->numbered(row, true);
235                         }
236
237                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
238                                 bv->redraw();
239
240                         mat()->label(row, new_label);
241
242                         updateLocal(bv, true);
243                         break;
244                 }
245
246                 case LFUN_MATH_EXTERN:
247                         bv->lockedInsetStoreUndo(Undo::EDIT);
248                         handleExtern(arg, bv);
249                         updateLocal(bv, true);
250                         break;
251
252                 case LFUN_MATH_MUTATE:
253                 {
254                         bv->lockedInsetStoreUndo(Undo::EDIT);
255                         int x;
256                         int y;
257                         mathcursor->getPos(x, y);
258                         mat()->mutate(arg);
259                         mathcursor->setPos(x, y);
260                         mathcursor->normalize();
261                         updateLocal(bv, true);
262                         break;
263                 }
264
265                 case LFUN_MATH_DISPLAY:
266                 {
267                         int x;
268                         int y;
269                         mathcursor->getPos(x, y);
270                         if (mat()->getType() == LM_OT_SIMPLE)
271                                 mat()->mutate(LM_OT_EQUATION);
272                         else
273                                 mat()->mutate(LM_OT_SIMPLE);
274                         mathcursor->setPos(x, y);
275                         mathcursor->normalize();
276                         updateLocal(bv, true);
277                         break;
278                 }
279                 
280                 case LFUN_PASTESELECTION:
281                 {
282                         string const clip = bv->getClipboard();
283                 if (!clip.empty())
284                                 mathed_parse_normal(par_, clip);
285                         break;
286                 }
287
288                 case LFUN_MATH_COLUMN_INSERT:
289                 {
290                         if (mat()->getType() == LM_OT_ALIGN)
291                                 mat()->mutate(LM_OT_ALIGNAT);
292                         mat()->addCol(mat()->ncols());
293                         mathcursor->normalize();
294                         updateLocal(bv, true);
295                 }
296
297                 default:
298                         result = InsetFormulaBase::localDispatch(bv, action, arg);
299         }
300
301         return result;
302 }
303
304
305 void InsetFormula::handleExtern(const string & arg, BufferView *)
306 {
307         // where are we?
308         MathArray & ar = mathcursor->cursor().cell();
309
310         // find position of last '=' in the array for handleExtern
311         MathArray::size_type pos = ar.size();
312         for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
313                 if ((*it)->getChar() == '=')
314                         pos = it - ar.begin();
315
316         // delete everything behind this position
317         ar.erase(pos, ar.size());
318
319         // create normalized expression
320         //string outfile = lyx::tempName("maple.out");
321         string outfile = "/tmp/lyx2" + arg + ".out";
322         ostringstream os;
323         ar.writeNormal(os); 
324         string code = os.str().c_str();
325
326         // run external sript
327         string script = "lyx2" + arg + " '" + code + "' " + outfile;
328         lyxerr << "calling: " << script << endl;
329         Systemcalls cmd(Systemcalls::System, script, 0);
330
331         // append a '='
332         ar.push_back(MathAtom(new MathCharInset('=')));
333         
334         // append result
335         ifstream is(outfile.c_str());
336         mathed_parse_cell(ar, is);
337         mathcursor->end();
338
339         // re-compute inset dimension
340         metrics();
341 }
342
343
344 bool InsetFormula::display() const
345 {
346         return mat()->getType() != LM_OT_SIMPLE;
347 }
348
349
350 MathMatrixInset const * InsetFormula::mat() const
351 {
352         lyx::Assert(par_->asMatrixInset());
353         return par_->asMatrixInset();
354 }
355
356
357 MathMatrixInset * InsetFormula::mat()
358 {
359         lyx::Assert(par_->asMatrixInset());
360         return par_->asMatrixInset();
361 }
362
363
364 Inset::Code InsetFormula::lyxCode() const
365 {
366         return Inset::MATH_CODE;
367 }
368
369
370 void InsetFormula::validate(LaTeXFeatures & features) const
371 {
372         par_->validate(features);
373 }
374
375
376 bool InsetFormula::insetAllowed(Inset::Code code) const
377 {
378         return code == Inset::LABEL_CODE && display(); 
379 }
380
381
382 int InsetFormula::ascent(BufferView *, LyXFont const &) const
383 {
384         return par_->ascent() + 2;
385 }
386
387
388 int InsetFormula::descent(BufferView *, LyXFont const &) const
389 {
390         return par_->descent() - 2;
391 }
392
393
394 int InsetFormula::width(BufferView *, LyXFont const &) const
395 {
396         metrics();
397         return par_->width();
398 }
399
400
401 MathInsetTypes InsetFormula::getType() const
402 {
403         return mat()->getType();
404 }