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