]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
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::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                 case LFUN_PASTESELECTION:
276                 {
277                         string const clip = bv->getClipboard();
278                 if (!clip.empty())
279                                 par(mathed_parse(clip));
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 bool InsetFormula::insetAllowed(Inset::Code code) const
337 {
338         return code == Inset::LABEL_CODE; 
339 }
340
341
342 int InsetFormula::ascent(BufferView *, LyXFont const &) const
343 {
344         return par()->ascent() + 1;
345 }
346
347
348 int InsetFormula::descent(BufferView *, LyXFont const &) const
349 {
350         return par()->descent() + 1;
351 }
352
353
354 int InsetFormula::width(BufferView *, LyXFont const &) const
355 {
356         metrics();
357         return par()->width();
358 }