]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
Edwin's "about" patch + consistent use of Lsstream.h
[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(0)
68 {
69         istringstream is(s.c_str());
70         par(mathed_parse(is));
71         Metrics();
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         Metrics();
118 }
119
120
121 void InsetFormula::draw(BufferView * bv, LyXFont const &,
122                         int y, float & xx, bool) const
123 {
124         int x = int(xx) - 1;
125         y -= 2;
126         MathInset::workwidth = bv->workWidth();
127         Painter & pain = bv->painter();
128
129         Metrics();
130         int w = par()->width();
131         int h = par()->height();
132         int a = par()->ascent();
133         pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
134
135         if (mathcursor) {
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 void InsetFormula::Metrics() const 
156 {
157         const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
158 }
159
160 vector<string> const InsetFormula::getLabelList() const
161 {
162         return par()->getLabelList();
163 }
164
165
166 UpdatableInset::RESULT
167 InsetFormula::localDispatch(BufferView * bv, kb_action action,
168          string const & arg)
169 {
170         RESULT result = DISPATCHED;
171
172         switch (action) {
173
174                 case LFUN_BREAKLINE: 
175                         bv->lockedInsetStoreUndo(Undo::INSERT);
176                         mathcursor->breakLine();
177                         updateLocal(bv);
178                         break;
179
180                 case LFUN_MATH_NUMBER:
181                 {
182                         //lyxerr << "toggling all numbers\n";
183                         if (display()) {
184                                 bv->lockedInsetStoreUndo(Undo::INSERT);
185                                 bool old = par()->numberedType();
186                                 for (int row = 0; row < par()->nrows(); ++row)
187                                         par()->numbered(row, !old);
188                                 bv->owner()->message(old ? _("No number") : _("Number"));
189                                 updateLocal(bv);
190                         }
191                         break;
192                 }
193
194                 case LFUN_MATH_NONUMBER:
195                 {
196                         //lyxerr << "toggling line number\n";
197                         if (display()) {
198                                 bv->lockedInsetStoreUndo(Undo::INSERT);
199                                 int row = mathcursor->row();
200                                 bool old = par()->numbered(row);
201                                 bv->owner()->message(old ? _("No number") : _("Number"));
202                                 par()->numbered(row, !old);
203                                 updateLocal(bv);
204                         }
205                         break;
206                 }
207
208                 case LFUN_INSERT_LABEL:
209                 {
210                         bv->lockedInsetStoreUndo(Undo::INSERT);
211
212                         int row = mathcursor->row();
213                         string old_label = par()->label(row);
214                         string new_label = arg;
215
216                         if (new_label.empty()) {
217                                 string const default_label =
218                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
219                                 pair<bool, string> const res = old_label.empty()
220                                         ? askForText(_("Enter new label to insert:"), default_label)
221                                         : askForText(_("Enter label:"), old_label);
222                                 
223                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
224                                 if (!res.first)
225                                         break;
226                                 new_label = frontStrip(strip(res.second));
227                         }
228
229                         //if (new_label == old_label)
230                         //      break;  // Nothing to do
231
232                         if (!new_label.empty()) {
233                                 lyxerr << "setting label to '" << new_label << "'\n";
234                                 par()->numbered(row, true);
235                         }
236
237                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
238                                 bv->redraw();
239
240                         par()->label(row, new_label);
241
242                         updateLocal(bv);
243                         break;
244                 }
245
246                 case LFUN_MATH_EXTERN:
247                         bv->lockedInsetStoreUndo(Undo::EDIT);
248                         handleExtern(arg, bv);
249                         updateLocal(bv);
250                         break;
251
252                 case LFUN_MATH_MUTATE:
253                 {
254                         bv->lockedInsetStoreUndo(Undo::EDIT);
255                         int x;
256                         int y;
257                         mathcursor->GetPos(x, y);
258                         par()->mutate(arg);
259                         mathcursor->SetPos(x, y);
260                         mathcursor->normalize();
261                         updateLocal(bv);
262                         break;
263                 }
264
265                 case LFUN_MATH_DISPLAY:
266                 {
267                         int x;
268                         int y;
269                         mathcursor->GetPos(x, y);
270                         if (par()->GetType() == LM_OT_SIMPLE)
271                                 par()->mutate(LM_OT_EQUATION);
272                         else
273                                 par()->mutate(LM_OT_SIMPLE);
274                         mathcursor->SetPos(x, y);
275                         mathcursor->normalize();
276                         updateLocal(bv);
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 void InsetFormula::par(MathInset * p)
316
317         delete par_;
318         par_ = p ? p : new MathMatrixInset;
319 }
320
321
322 Inset::Code InsetFormula::lyxCode() const
323 {
324         return Inset::MATH_CODE;
325 }
326
327
328 void InsetFormula::validate(LaTeXFeatures & features) const
329 {
330         par()->Validate(features);
331 }
332
333
334 int InsetFormula::ascent(BufferView *, LyXFont const &) const
335 {
336         return par()->ascent();
337 }
338
339
340 int InsetFormula::descent(BufferView *, LyXFont const &) const
341 {
342         return par()->descent();
343 }
344
345
346 int InsetFormula::width(BufferView *, LyXFont const &) const
347 {
348         return par()->width();
349 }