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