]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
bugfix (invalid cursor position)
[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 #include <config.h>
17 #include <fstream>
18
19 #include "support/LOstream.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
29 #include "lyx_main.h"
30 #include "BufferView.h"
31 #include "lyxtext.h"
32 #include "gettext.h"
33 #include "debug.h"
34 #include "lyx_gui_misc.h"
35 #include "support/LOstream.h"
36 #include "support/lyxlib.h"
37 #include "support/syscall.h"
38 #include "LyXView.h"
39 #include "Painter.h"
40 #include "font.h"
41 #include "lyxrc.h"
42 #include "math_matrixinset.h"
43 #include "mathed/support.h"
44
45 using std::ostringstream;
46 using std::ostream;
47 using std::ifstream;
48 using std::istream;
49 using std::pair;
50 using std::endl;
51 using std::vector;
52
53 extern char const * latex_mathenv[];
54 extern MathCursor * mathcursor;
55
56
57 // quite a hack i know. Should be done with return values...
58 int number_of_newlines = 0;
59
60
61 InsetFormula::InsetFormula()
62         : InsetFormulaBase(new MathMatrixInset)
63 {}
64
65
66 InsetFormula::InsetFormula(MathInsetTypes t)
67         : InsetFormulaBase(new MathMatrixInset(t))
68 {}
69
70
71 InsetFormula::InsetFormula(string const & s)
72         : InsetFormulaBase(0)
73 {
74         istringstream is(s.c_str());
75         par(mathed_parse(is));
76 }
77
78
79 Inset * InsetFormula::clone(Buffer const &, bool) const
80 {
81         return new InsetFormula(*this);
82 }
83
84
85 void InsetFormula::write(ostream & os) const
86 {
87         os << "Formula ";
88         latex(os, false, false);
89 }
90
91
92 int InsetFormula::latex(ostream & os, bool fragile, bool) const
93 {
94         par()->Write(os, fragile);
95         return 1;
96 }
97
98
99 int InsetFormula::ascii(ostream & os, int) const
100 {
101         par()->Write(os, false);
102         return 1;
103 }
104
105
106 int InsetFormula::linuxdoc(ostream & os) const
107 {
108         return ascii(os, 0);
109 }
110
111
112 int InsetFormula::docBook(ostream & os) const
113 {
114         return ascii(os, 0);
115 }
116
117
118 void InsetFormula::read(LyXLex & lex)
119 {
120         par(mathed_parse(lex));
121 }
122
123
124 void InsetFormula::draw(BufferView * bv, LyXFont const &,
125                         int y, float & xx, bool) const
126 {
127         int x = int(xx) - 1;
128         y -= 2;
129         MathInset::workwidth = bv->workWidth();
130         Painter & pain = bv->painter();
131
132         int w = par()->width();
133         int h = par()->height();
134         int a = par()->ascent();
135         pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
136
137         if (mathcursor) {
138                 par()->Metrics(LM_ST_TEXT);
139
140                 if (mathcursor->formula() == this) {
141                         if (mathcursor->Selection()) {
142                                 int xp[10];
143                                 int yp[10];
144                                 int n;
145                                 mathcursor->SelGetArea(xp, yp, n);
146                                 pain.fillPolygon(xp, yp, n, LColor::selection);
147                         }
148                         pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
149                 }
150         }
151
152         par()->draw(pain, int(x), y);
153         xx += par()->width();
154
155         setCursorVisible(false);
156 }
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                         mathcursor->breakLine();
176                         updateLocal(bv);
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);
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);
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);
242                         break;
243                 }
244
245                 case LFUN_MATH_EXTERN:
246                         bv->lockedInsetStoreUndo(Undo::EDIT);
247                         handleExtern(arg, bv);
248                         updateLocal(bv);
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);
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                                 par()->numbered(0, false);
272                         }
273                         else
274                                 par()->mutate(LM_OT_SIMPLE);
275                         mathcursor->SetPos(x, y);
276                         mathcursor->normalize();
277                         updateLocal(bv);
278                         break;
279                 }
280
281                 default:
282                         result = InsetFormulaBase::localDispatch(bv, action, arg);
283         }
284
285         return result;
286 }
287
288
289 void InsetFormula::handleExtern(const string & arg, BufferView *)
290 {
291         //string outfile = lyx::tempName("maple.out");
292         string outfile = "/tmp/lyx2" + arg + ".out";
293         ostringstream os;
294         par()->WriteNormal(os); 
295         string code = os.str().c_str();
296         string script = "lyx2" + arg + " '" + code + "' " + outfile;
297         lyxerr << "calling: " << script << endl;
298         Systemcalls cmd(Systemcalls::System, script, 0);
299
300         ifstream is(outfile.c_str());
301         par(mathed_parse(is));
302 }
303
304 bool InsetFormula::display() const
305 {
306         return par_->GetType() != LM_OT_SIMPLE;
307 }
308
309
310 MathMatrixInset * InsetFormula::par() const
311 {
312         return static_cast<MathMatrixInset *>(par_);
313 }
314
315 void InsetFormula::par(MathInset * p)
316
317         delete par_;
318         par_ = p ? p : new MathMatrixInset;
319 }
320
321
322 Inset::Code InsetFormula::lyxCode() const
323 {
324         return Inset::MATH_CODE;
325 }
326
327
328 void InsetFormula::validate(LaTeXFeatures & features) const
329 {
330         par()->Validate(features);
331 }
332
333
334 int InsetFormula::ascent(BufferView *, LyXFont const &) const
335 {
336         return par()->ascent() + 4;
337 }
338
339
340 int InsetFormula::descent(BufferView *, LyXFont const &) const
341 {
342         return par()->descent();
343 }
344
345
346 int InsetFormula::width(BufferView *, LyXFont const &) const
347 {
348         par()->Metrics(LM_ST_TEXT);
349         return par()->width();
350 }