]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
Fix Angus' drawing bug + 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 "LyXView.h"
36 #include "Painter.h"
37 #include "lyxrc.h"
38 #include "math_matrixinset.h"
39 #include "mathed/support.h"
40
41 using std::ostringstream;
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 // quite a hack i know. Should be done with return values...
54 int number_of_newlines = 0;
55
56
57 InsetFormula::InsetFormula()
58         : InsetFormulaBase(new MathMatrixInset)
59 {}
60
61
62 InsetFormula::InsetFormula(MathInsetTypes t)
63         : InsetFormulaBase(new MathMatrixInset(t))
64 {}
65
66
67 InsetFormula::InsetFormula(string const & s)
68         : InsetFormulaBase(0)
69 {
70         istringstream is(s.c_str());
71         par(mathed_parse(is));
72         Metrics();
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(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         MathInset::workwidth = bv->workWidth();
128         Painter & pain = bv->painter();
129
130         int w = par()->width();
131         int h = par()->height();
132         int a = par()->ascent();
133         pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
134
135         if (mathcursor) {
136                 Metrics();
137
138                 if (mathcursor->formula() == this) {
139                         if (mathcursor->Selection()) {
140                                 int xp[10];
141                                 int yp[10];
142                                 int n;
143                                 mathcursor->SelGetArea(xp, yp, n);
144                                 pain.fillPolygon(xp, yp, n, LColor::selection);
145                         }
146                         pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
147                 }
148         }
149
150         par()->draw(pain, int(x), y);
151         xx += par()->width();
152
153         setCursorVisible(false);
154 }
155
156
157 void InsetFormula::Metrics() const 
158 {
159         const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
160 }
161
162 vector<string> const InsetFormula::getLabelList() const
163 {
164         return par()->getLabelList();
165 }
166
167
168 UpdatableInset::RESULT
169 InsetFormula::localDispatch(BufferView * bv, kb_action action,
170          string const & arg)
171 {
172         RESULT result = DISPATCHED;
173
174         switch (action) {
175
176                 case LFUN_BREAKLINE: 
177                         bv->lockedInsetStoreUndo(Undo::INSERT);
178                         mathcursor->breakLine();
179                         updateLocal(bv);
180                         break;
181
182                 case LFUN_MATH_NUMBER:
183                 {
184                         //lyxerr << "toggling all numbers\n";
185                         if (display()) {
186                                 bv->lockedInsetStoreUndo(Undo::INSERT);
187                                 bool old = par()->numberedType();
188                                 for (int row = 0; row < par()->nrows(); ++row)
189                                         par()->numbered(row, !old);
190                                 bv->owner()->message(old ? _("No number") : _("Number"));
191                                 updateLocal(bv);
192                         }
193                         break;
194                 }
195
196                 case LFUN_MATH_NONUMBER:
197                 {
198                         //lyxerr << "toggling line number\n";
199                         if (display()) {
200                                 bv->lockedInsetStoreUndo(Undo::INSERT);
201                                 int row = mathcursor->row();
202                                 bool old = par()->numbered(row);
203                                 bv->owner()->message(old ? _("No number") : _("Number"));
204                                 par()->numbered(row, !old);
205                                 updateLocal(bv);
206                         }
207                         break;
208                 }
209
210                 case LFUN_INSERT_LABEL:
211                 {
212                         bv->lockedInsetStoreUndo(Undo::INSERT);
213
214                         int row = mathcursor->row();
215                         string old_label = par()->label(row);
216                         string new_label = arg;
217
218                         if (new_label.empty()) {
219                                 string const default_label =
220                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
221                                 pair<bool, string> const res = old_label.empty()
222                                         ? askForText(_("Enter new label to insert:"), default_label)
223                                         : askForText(_("Enter label:"), old_label);
224                                 
225                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
226                                 if (!res.first)
227                                         break;
228                                 new_label = frontStrip(strip(res.second));
229                         }
230
231                         //if (new_label == old_label)
232                         //      break;  // Nothing to do
233
234                         if (!new_label.empty()) {
235                                 lyxerr << "setting label to '" << new_label << "'\n";
236                                 par()->numbered(row, true);
237                         }
238
239                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
240                                 bv->redraw();
241
242                         par()->label(row, new_label);
243
244                         updateLocal(bv);
245                         break;
246                 }
247
248                 case LFUN_MATH_EXTERN:
249                         bv->lockedInsetStoreUndo(Undo::EDIT);
250                         handleExtern(arg, bv);
251                         updateLocal(bv);
252                         break;
253
254                 case LFUN_MATH_MUTATE:
255                 {
256                         bv->lockedInsetStoreUndo(Undo::EDIT);
257                         int x;
258                         int y;
259                         mathcursor->GetPos(x, y);
260                         par()->mutate(arg);
261                         mathcursor->SetPos(x, y);
262                         mathcursor->normalize();
263                         updateLocal(bv);
264                         break;
265                 }
266
267                 case LFUN_MATH_DISPLAY:
268                 {
269                         int x;
270                         int y;
271                         mathcursor->GetPos(x, y);
272                         if (par()->GetType() == LM_OT_SIMPLE) {
273                                 par()->mutate(LM_OT_EQUATION);
274                                 par()->numbered(0, false);
275                         }
276                         else
277                                 par()->mutate(LM_OT_SIMPLE);
278                         mathcursor->SetPos(x, y);
279                         mathcursor->normalize();
280                         updateLocal(bv);
281                         break;
282                 }
283
284                 default:
285                         result = InsetFormulaBase::localDispatch(bv, action, arg);
286         }
287
288         return result;
289 }
290
291
292 void InsetFormula::handleExtern(const string & arg, BufferView *)
293 {
294         //string outfile = lyx::tempName("maple.out");
295         string outfile = "/tmp/lyx2" + arg + ".out";
296         ostringstream os;
297         par()->WriteNormal(os); 
298         string code = os.str().c_str();
299         string script = "lyx2" + arg + " '" + code + "' " + outfile;
300         lyxerr << "calling: " << script << endl;
301         Systemcalls cmd(Systemcalls::System, script, 0);
302
303         ifstream is(outfile.c_str());
304         par(mathed_parse(is));
305         Metrics();
306 }
307
308 bool InsetFormula::display() const
309 {
310         return par_->GetType() != LM_OT_SIMPLE;
311 }
312
313
314 MathMatrixInset * InsetFormula::par() const
315 {
316         return static_cast<MathMatrixInset *>(par_);
317 }
318
319 void InsetFormula::par(MathInset * p)
320
321         delete par_;
322         par_ = p ? p : new MathMatrixInset;
323 }
324
325
326 Inset::Code InsetFormula::lyxCode() const
327 {
328         return Inset::MATH_CODE;
329 }
330
331
332 void InsetFormula::validate(LaTeXFeatures & features) const
333 {
334         par()->Validate(features);
335 }
336
337
338 int InsetFormula::ascent(BufferView *, LyXFont const &) const
339 {
340         return par()->ascent();
341 }
342
343
344 int InsetFormula::descent(BufferView *, LyXFont const &) const
345 {
346         return par()->descent();
347 }
348
349
350 int InsetFormula::width(BufferView *, LyXFont const &) const
351 {
352         return par()->width();
353 }