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