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