]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
prepare infrastructure for multicell selection
[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
127         MathInset::workwidth = bv->workWidth();
128         Painter & pain = bv->painter();
129
130         Metrics();
131         int w = par()->width();
132         int h = par()->height();
133         int a = par()->ascent();
134         pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
135
136         if (mathcursor && 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(x, y - a, w, h, LColor::mathframe);
145         }
146
147         par()->draw(pain, x, y);
148         xx += par()->width();
149
150         setCursorVisible(false);
151 }
152
153
154 void InsetFormula::Metrics() const 
155 {
156         const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
157 }
158
159 vector<string> const InsetFormula::getLabelList() const
160 {
161         return par()->getLabelList();
162 }
163
164
165 UpdatableInset::RESULT
166 InsetFormula::localDispatch(BufferView * bv, kb_action action,
167          string const & arg)
168 {
169         RESULT result = DISPATCHED;
170
171         switch (action) {
172
173                 case LFUN_BREAKLINE: 
174                         bv->lockedInsetStoreUndo(Undo::INSERT);
175                         mathcursor->breakLine();
176                         updateLocal(bv);
177                         break;
178
179                 case LFUN_MATH_NUMBER:
180                 {
181                         //lyxerr << "toggling all numbers\n";
182                         if (display()) {
183                                 bv->lockedInsetStoreUndo(Undo::INSERT);
184                                 bool old = par()->numberedType();
185                                 for (int row = 0; row < par()->nrows(); ++row)
186                                         par()->numbered(row, !old);
187                                 bv->owner()->message(old ? _("No number") : _("Number"));
188                                 updateLocal(bv);
189                         }
190                         break;
191                 }
192
193                 case LFUN_MATH_NONUMBER:
194                 {
195                         //lyxerr << "toggling line number\n";
196                         if (display()) {
197                                 bv->lockedInsetStoreUndo(Undo::INSERT);
198                                 int row = mathcursor->row();
199                                 bool old = par()->numbered(row);
200                                 bv->owner()->message(old ? _("No number") : _("Number"));
201                                 par()->numbered(row, !old);
202                                 updateLocal(bv);
203                         }
204                         break;
205                 }
206
207                 case LFUN_INSERT_LABEL:
208                 {
209                         bv->lockedInsetStoreUndo(Undo::INSERT);
210
211                         int row = mathcursor->row();
212                         string old_label = par()->label(row);
213                         string new_label = arg;
214
215                         if (new_label.empty()) {
216                                 string const default_label =
217                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
218                                 pair<bool, string> const res = old_label.empty()
219                                         ? askForText(_("Enter new label to insert:"), default_label)
220                                         : askForText(_("Enter label:"), old_label);
221                                 
222                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
223                                 if (!res.first)
224                                         break;
225                                 new_label = frontStrip(strip(res.second));
226                         }
227
228                         //if (new_label == old_label)
229                         //      break;  // Nothing to do
230
231                         if (!new_label.empty()) {
232                                 lyxerr << "setting label to '" << new_label << "'\n";
233                                 par()->numbered(row, true);
234                         }
235
236                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
237                                 bv->redraw();
238
239                         par()->label(row, new_label);
240
241                         updateLocal(bv);
242                         break;
243                 }
244
245                 case LFUN_MATH_EXTERN:
246                         bv->lockedInsetStoreUndo(Undo::EDIT);
247                         handleExtern(arg, bv);
248                         updateLocal(bv);
249                         break;
250
251                 case LFUN_MATH_MUTATE:
252                 {
253                         bv->lockedInsetStoreUndo(Undo::EDIT);
254                         int x;
255                         int y;
256                         mathcursor->GetPos(x, y);
257                         par()->mutate(arg);
258                         mathcursor->SetPos(x, y);
259                         mathcursor->normalize();
260                         updateLocal(bv);
261                         break;
262                 }
263
264                 case LFUN_MATH_DISPLAY:
265                 {
266                         int x;
267                         int y;
268                         mathcursor->GetPos(x, y);
269                         if (par()->GetType() == LM_OT_SIMPLE)
270                                 par()->mutate(LM_OT_EQUATION);
271                         else
272                                 par()->mutate(LM_OT_SIMPLE);
273                         mathcursor->SetPos(x, y);
274                         mathcursor->normalize();
275                         updateLocal(bv);
276                         break;
277                 }
278
279                 default:
280                         result = InsetFormulaBase::localDispatch(bv, action, arg);
281         }
282
283         return result;
284 }
285
286
287 void InsetFormula::handleExtern(const string & arg, BufferView *)
288 {
289         //string outfile = lyx::tempName("maple.out");
290         string outfile = "/tmp/lyx2" + arg + ".out";
291         ostringstream os;
292         par()->WriteNormal(os); 
293         string code = os.str().c_str();
294         string script = "lyx2" + arg + " '" + code + "' " + outfile;
295         lyxerr << "calling: " << script << endl;
296         Systemcalls cmd(Systemcalls::System, script, 0);
297
298         ifstream is(outfile.c_str());
299         par(mathed_parse(is));
300         Metrics();
301 }
302
303 bool InsetFormula::display() const
304 {
305         return par_->GetType() != LM_OT_SIMPLE;
306 }
307
308
309 MathMatrixInset * InsetFormula::par() const
310 {
311         return static_cast<MathMatrixInset *>(par_);
312 }
313
314 void InsetFormula::par(MathInset * p)
315
316         delete par_;
317         par_ = p ? p : new MathMatrixInset;
318 }
319
320
321 Inset::Code InsetFormula::lyxCode() const
322 {
323         return Inset::MATH_CODE;
324 }
325
326
327 void InsetFormula::validate(LaTeXFeatures & features) const
328 {
329         par()->Validate(features);
330 }
331
332
333 int InsetFormula::ascent(BufferView *, LyXFont const &) const
334 {
335         return par()->ascent();
336 }
337
338
339 int InsetFormula::descent(BufferView *, LyXFont const &) const
340 {
341         return par()->descent();
342 }
343
344
345 int InsetFormula::width(BufferView *, LyXFont const &) const
346 {
347         return par()->width();
348 }