]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
remove unneeded #includes, 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::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 }
73
74
75 Inset * InsetFormula::clone(Buffer const &, bool) const
76 {
77         return new InsetFormula(*this);
78 }
79
80
81 void InsetFormula::write(ostream & os) const
82 {
83         os << "Formula ";
84         latex(os, false, false);
85 }
86
87
88 int InsetFormula::latex(ostream & os, bool fragile, bool) const
89 {
90         par()->Write(os, fragile);
91         return 1;
92 }
93
94
95 int InsetFormula::ascii(ostream & os, int) const
96 {
97         par()->Write(os, false);
98         return 1;
99 }
100
101
102 int InsetFormula::linuxdoc(ostream & os) const
103 {
104         return ascii(os, 0);
105 }
106
107
108 int InsetFormula::docBook(ostream & os) const
109 {
110         return ascii(os, 0);
111 }
112
113
114 void InsetFormula::read(LyXLex & lex)
115 {
116         par(mathed_parse(lex));
117 }
118
119
120 void InsetFormula::draw(BufferView * bv, LyXFont const &,
121                         int y, float & xx, bool) const
122 {
123         int x = int(xx) - 1;
124         y -= 2;
125         MathInset::workwidth = bv->workWidth();
126         Painter & pain = bv->painter();
127
128         int w = par()->width();
129         int h = par()->height();
130         int a = par()->ascent();
131         pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
132
133         if (mathcursor) {
134                 par()->Metrics(LM_ST_TEXT);
135
136                 if (mathcursor->formula() == this) {
137                         if (mathcursor->Selection()) {
138                                 int xp[10];
139                                 int yp[10];
140                                 int n;
141                                 mathcursor->SelGetArea(xp, yp, n);
142                                 pain.fillPolygon(xp, yp, n, LColor::selection);
143                         }
144                         pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
145                 }
146         }
147
148         par()->draw(pain, int(x), y);
149         xx += par()->width();
150
151         setCursorVisible(false);
152 }
153
154
155 vector<string> const InsetFormula::getLabelList() const
156 {
157         return par()->getLabelList();
158 }
159
160
161 UpdatableInset::RESULT
162 InsetFormula::localDispatch(BufferView * bv, kb_action action,
163          string const & arg)
164 {
165         RESULT result = DISPATCHED;
166
167         switch (action) {
168
169                 case LFUN_BREAKLINE: 
170                         bv->lockedInsetStoreUndo(Undo::INSERT);
171                         mathcursor->breakLine();
172                         updateLocal(bv);
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);
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);
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);
238                         break;
239                 }
240
241                 case LFUN_MATH_EXTERN:
242                         bv->lockedInsetStoreUndo(Undo::EDIT);
243                         handleExtern(arg, bv);
244                         updateLocal(bv);
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);
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                                 par()->numbered(0, false);
268                         }
269                         else
270                                 par()->mutate(LM_OT_SIMPLE);
271                         mathcursor->SetPos(x, y);
272                         mathcursor->normalize();
273                         updateLocal(bv);
274                         break;
275                 }
276
277                 default:
278                         result = InsetFormulaBase::localDispatch(bv, action, arg);
279         }
280
281         return result;
282 }
283
284
285 void InsetFormula::handleExtern(const string & arg, BufferView *)
286 {
287         //string outfile = lyx::tempName("maple.out");
288         string outfile = "/tmp/lyx2" + arg + ".out";
289         ostringstream os;
290         par()->WriteNormal(os); 
291         string code = os.str().c_str();
292         string script = "lyx2" + arg + " '" + code + "' " + outfile;
293         lyxerr << "calling: " << script << endl;
294         Systemcalls cmd(Systemcalls::System, script, 0);
295
296         ifstream is(outfile.c_str());
297         par(mathed_parse(is));
298 }
299
300 bool InsetFormula::display() const
301 {
302         return par_->GetType() != LM_OT_SIMPLE;
303 }
304
305
306 MathMatrixInset * InsetFormula::par() const
307 {
308         return static_cast<MathMatrixInset *>(par_);
309 }
310
311 void InsetFormula::par(MathInset * p)
312
313         delete par_;
314         par_ = p ? p : new MathMatrixInset;
315 }
316
317
318 Inset::Code InsetFormula::lyxCode() const
319 {
320         return Inset::MATH_CODE;
321 }
322
323
324 void InsetFormula::validate(LaTeXFeatures & features) const
325 {
326         par()->Validate(features);
327 }
328
329
330 int InsetFormula::ascent(BufferView *, LyXFont const &) const
331 {
332         return par()->ascent() + 4;
333 }
334
335
336 int InsetFormula::descent(BufferView *, LyXFont const &) const
337 {
338         return par()->descent();
339 }
340
341
342 int InsetFormula::width(BufferView *, LyXFont const &) const
343 {
344         par()->Metrics(LM_ST_TEXT);
345         return par()->width();
346 }