]> git.lyx.org Git - features.git/blob - src/mathed/formula.C
remove position cache from insets - these were the last data item stored in
[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 #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         xo_ = x;
167         yo_ = y;
168
169         setCursorVisible(false);
170 }
171
172
173 vector<string> const InsetFormula::getLabelList() const
174 {
175         return mat()->getLabelList();
176 }
177
178
179 UpdatableInset::RESULT
180 InsetFormula::localDispatch(BufferView * bv, kb_action action,
181          string const & arg)
182 {
183         RESULT result = DISPATCHED;
184
185         switch (action) {
186
187                 case LFUN_BREAKLINE: 
188                         bv->lockedInsetStoreUndo(Undo::INSERT);
189                         mathcursor->breakLine();
190                         mathcursor->normalize();
191                         updateLocal(bv, true);
192                         break;
193
194                 case LFUN_MATH_NUMBER:
195                 {
196                         //lyxerr << "toggling all numbers\n";
197                         if (display()) {
198                                 bv->lockedInsetStoreUndo(Undo::INSERT);
199                                 bool old = mat()->numberedType();
200                                 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
201                                         mat()->numbered(row, !old);
202                                 bv->owner()->message(old ? _("No number") : _("Number"));
203                                 updateLocal(bv, true);
204                         }
205                         break;
206                 }
207
208                 case LFUN_MATH_NONUMBER:
209                 {
210                         //lyxerr << "toggling line number\n";
211                         if (display()) {
212                                 bv->lockedInsetStoreUndo(Undo::INSERT);
213                                 MathCursor::row_type row = mathcursor->row();
214                                 bool old = mat()->numbered(row);
215                                 bv->owner()->message(old ? _("No number") : _("Number"));
216                                 mat()->numbered(row, !old);
217                                 updateLocal(bv, true);
218                         }
219                         break;
220                 }
221
222                 case LFUN_INSERT_LABEL:
223                 {
224                         bv->lockedInsetStoreUndo(Undo::INSERT);
225
226                         MathCursor::row_type row = mathcursor->row();
227                         string old_label = mat()->label(row);
228                         string new_label = arg;
229
230                         if (new_label.empty()) {
231                                 string const default_label =
232                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
233                                 pair<bool, string> const res = old_label.empty()
234                                         ? askForText(_("Enter new label to insert:"), default_label)
235                                         : askForText(_("Enter label:"), old_label);
236                                 
237                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
238                                 if (!res.first)
239                                         break;
240                                 new_label = frontStrip(strip(res.second));
241                         }
242
243                         //if (new_label == old_label)
244                         //      break;  // Nothing to do
245
246                         if (!new_label.empty()) {
247                                 lyxerr << "setting label to '" << new_label << "'\n";
248                                 mat()->numbered(row, true);
249                         }
250
251                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
252                                 bv->redraw();
253
254                         mat()->label(row, new_label);
255
256                         updateLocal(bv, true);
257                         break;
258                 }
259
260                 case LFUN_MATH_EXTERN:
261                         bv->lockedInsetStoreUndo(Undo::EDIT);
262                         handleExtern(arg);
263                         // re-compute inset dimension
264                         metrics(bv);
265                         updateLocal(bv, true);
266                         break;
267
268                 case LFUN_MATH_MUTATE:
269                 {
270                         bv->lockedInsetStoreUndo(Undo::EDIT);
271                         int x;
272                         int y;
273                         mathcursor->getPos(x, y);
274                         mat()->mutate(arg);
275                         mathcursor->setPos(x, y);
276                         mathcursor->normalize();
277                         updateLocal(bv, true);
278                         break;
279                 }
280
281                 case LFUN_MATH_DISPLAY:
282                 {
283                         int x;
284                         int y;
285                         mathcursor->getPos(x, y);
286                         if (mat()->getType() == LM_OT_SIMPLE)
287                                 mat()->mutate(LM_OT_EQUATION);
288                         else
289                                 mat()->mutate(LM_OT_SIMPLE);
290                         mathcursor->setPos(x, y);
291                         mathcursor->normalize();
292                         updateLocal(bv, true);
293                         break;
294                 }
295                 
296                 case LFUN_PASTESELECTION:
297                 {
298                         string const clip = bv->getClipboard();
299                 if (!clip.empty())
300                                 mathed_parse_normal(par_, clip);
301                         break;
302                 }
303
304                 case LFUN_MATH_COLUMN_INSERT:
305                 {
306                         if (mat()->getType() == LM_OT_ALIGN)
307                                 mat()->mutate(LM_OT_ALIGNAT);
308                         mat()->addCol(mat()->ncols());
309                         mathcursor->normalize();
310                         updateLocal(bv, true);
311                 }
312
313                 default:
314                         result = InsetFormulaBase::localDispatch(bv, action, arg);
315         }
316
317         return result;
318 }
319
320 #if 0
321 void InsetFormula::handleExtern(const string & arg)
322 {
323         // where are we?
324         if (!mathcursor)
325                 return; 
326
327         MathArray & ar = mathcursor->cursor().cell();
328
329         // parse args
330         string lang;
331         string extra;
332         istringstream iss(arg.c_str());
333         iss >> lang >> extra;
334         if (extra.empty())
335                 extra = "noextra";      
336
337         // strip last '=' and everything behind
338         stripFromLastEqualSign(ar);
339
340         // create normalized expression
341         //string outfile = lyx::tempName("maple.out");
342         string outfile = "/tmp/lyx2" + lang + ".out";
343         ostringstream os;
344         os << "[" << extra << ' ';
345         ar.writeNormal(os); 
346         os << "]";
347         string code = os.str().c_str();
348
349         // run external sript
350         string script = "lyx2" + arg + " '" + code + "' " + outfile;
351         lyxerr << "calling: " << script << endl;
352         Systemcalls cmd(Systemcalls::System, script, 0);
353
354         // append a '='
355         ar.push_back(MathAtom(new MathCharInset('=')));
356         
357         // append result
358         ifstream is(outfile.c_str());
359         mathed_parse_cell(ar, is);
360         mathcursor->end();
361 }
362 #endif
363
364 void InsetFormula::handleExtern(const string & arg)
365 {
366         // where are we?
367         if (!mathcursor)
368                 return; 
369
370         bool selected = mathcursor->selection();
371
372         MathArray ar;
373         if (selected) {
374                 mathcursor->selGet(ar);
375                 lyxerr << "use selection: " << ar << "\n";
376         } else {
377                 ar = mathcursor->cursor().cell();
378                 lyxerr << "use whole cell: " << ar << "\n";
379         }
380
381
382         // parse args
383         string lang;
384         string extra;
385         istringstream iss(arg.c_str());
386         iss >> lang >> extra;
387         if (extra.empty())
388                 extra = "noextra";      
389
390         // strip last '=' and everything behind
391         stripFromLastEqualSign(ar);
392
393         // create normalized expression
394         //string outfile = lyx::tempName("maple.out");
395         string outfile = "/tmp/lyx2" + lang + ".out";
396         ostringstream os;
397         os << "[" << extra << ' ';
398         ar.writeNormal(os); 
399         os << "]";
400         string code = os.str().c_str();
401
402         // run external sript
403         string script = "lyx2" + lang + " '" + code + "' " + outfile;
404         lyxerr << "calling: " << script << endl;
405         Systemcalls cmd(Systemcalls::System, script, 0);
406
407         // append result
408         MathArray br;
409         if (selected)
410                 br.push_back(MathAtom(new MathCharInset('=', LM_TC_VAR)));
411         ifstream is(outfile.c_str());
412         mathed_parse_cell(br, is);
413         mathcursor->insert(br);
414 }
415
416 bool InsetFormula::display() const
417 {
418         return mat()->getType() != LM_OT_SIMPLE;
419 }
420
421
422 MathMatrixInset const * InsetFormula::mat() const
423 {
424         lyx::Assert(par_->asMatrixInset());
425         return par_->asMatrixInset();
426 }
427
428
429 MathMatrixInset * InsetFormula::mat()
430 {
431         lyx::Assert(par_->asMatrixInset());
432         return par_->asMatrixInset();
433 }
434
435
436 Inset::Code InsetFormula::lyxCode() const
437 {
438         return Inset::MATH_CODE;
439 }
440
441
442 void InsetFormula::validate(LaTeXFeatures & features) const
443 {
444         par_->validate(features);
445 }
446
447
448 bool InsetFormula::insetAllowed(Inset::Code code) const
449 {
450         return code == Inset::LABEL_CODE && display(); 
451 }
452
453
454 int InsetFormula::ascent(BufferView *, LyXFont const &) const
455 {
456         return par_->ascent() + 2;
457 }
458
459
460 int InsetFormula::descent(BufferView *, LyXFont const &) const
461 {
462         return par_->descent() - 2;
463 }
464
465
466 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
467 {
468         metrics(bv, font);
469         return par_->width();
470 }
471
472
473 MathInsetTypes InsetFormula::getType() const
474 {
475         return mat()->getType();
476 }