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