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