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