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