]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
reverse test move operators out of struct
[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                 mathcursor->drawSelection(pain);
138                 pain.rectangle(x, y - a, w, h, LColor::mathframe);
139         }
140
141         par()->draw(pain, x, y);
142         xx += par()->width();
143
144         setCursorVisible(false);
145 }
146
147
148 void InsetFormula::Metrics() const 
149 {
150         const_cast<MathInset *>(par_)->Metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
151 }
152
153 vector<string> const InsetFormula::getLabelList() const
154 {
155         return par()->getLabelList();
156 }
157
158
159 UpdatableInset::RESULT
160 InsetFormula::localDispatch(BufferView * bv, kb_action action,
161          string const & arg)
162 {
163         RESULT result = DISPATCHED;
164
165         switch (action) {
166
167                 case LFUN_BREAKLINE: 
168                         bv->lockedInsetStoreUndo(Undo::INSERT);
169                         int x;
170                         int y;
171                         mathcursor->GetPos(x, y);
172                         mathcursor->breakLine();
173                         mathcursor->normalize();
174                         updateLocal(bv);
175                         break;
176
177                 case LFUN_MATH_NUMBER:
178                 {
179                         //lyxerr << "toggling all numbers\n";
180                         if (display()) {
181                                 bv->lockedInsetStoreUndo(Undo::INSERT);
182                                 bool old = par()->numberedType();
183                                 for (int row = 0; row < par()->nrows(); ++row)
184                                         par()->numbered(row, !old);
185                                 bv->owner()->message(old ? _("No number") : _("Number"));
186                                 updateLocal(bv);
187                         }
188                         break;
189                 }
190
191                 case LFUN_MATH_NONUMBER:
192                 {
193                         //lyxerr << "toggling line number\n";
194                         if (display()) {
195                                 bv->lockedInsetStoreUndo(Undo::INSERT);
196                                 int row = mathcursor->row();
197                                 bool old = par()->numbered(row);
198                                 bv->owner()->message(old ? _("No number") : _("Number"));
199                                 par()->numbered(row, !old);
200                                 updateLocal(bv);
201                         }
202                         break;
203                 }
204
205                 case LFUN_INSERT_LABEL:
206                 {
207                         bv->lockedInsetStoreUndo(Undo::INSERT);
208
209                         int row = mathcursor->row();
210                         string old_label = par()->label(row);
211                         string new_label = arg;
212
213                         if (new_label.empty()) {
214                                 string const default_label =
215                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
216                                 pair<bool, string> const res = old_label.empty()
217                                         ? askForText(_("Enter new label to insert:"), default_label)
218                                         : askForText(_("Enter label:"), old_label);
219                                 
220                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
221                                 if (!res.first)
222                                         break;
223                                 new_label = frontStrip(strip(res.second));
224                         }
225
226                         //if (new_label == old_label)
227                         //      break;  // Nothing to do
228
229                         if (!new_label.empty()) {
230                                 lyxerr << "setting label to '" << new_label << "'\n";
231                                 par()->numbered(row, true);
232                         }
233
234                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
235                                 bv->redraw();
236
237                         par()->label(row, new_label);
238
239                         updateLocal(bv);
240                         break;
241                 }
242
243                 case LFUN_MATH_EXTERN:
244                         bv->lockedInsetStoreUndo(Undo::EDIT);
245                         handleExtern(arg, bv);
246                         updateLocal(bv);
247                         break;
248
249                 case LFUN_MATH_MUTATE:
250                 {
251                         bv->lockedInsetStoreUndo(Undo::EDIT);
252                         int x;
253                         int y;
254                         mathcursor->GetPos(x, y);
255                         par()->mutate(arg);
256                         mathcursor->SetPos(x, y);
257                         mathcursor->normalize();
258                         updateLocal(bv);
259                         break;
260                 }
261
262                 case LFUN_MATH_DISPLAY:
263                 {
264                         int x;
265                         int y;
266                         mathcursor->GetPos(x, y);
267                         if (par()->GetType() == LM_OT_SIMPLE)
268                                 par()->mutate(LM_OT_EQUATION);
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         Metrics();
299 }
300
301 bool InsetFormula::display() const
302 {
303         return par_->GetType() != LM_OT_SIMPLE;
304 }
305
306
307 MathMatrixInset * InsetFormula::par() const
308 {
309         return static_cast<MathMatrixInset *>(par_);
310 }
311
312 void InsetFormula::par(MathInset * p)
313
314         delete par_;
315         par_ = p ? p : new MathMatrixInset;
316 }
317
318
319 Inset::Code InsetFormula::lyxCode() const
320 {
321         return Inset::MATH_CODE;
322 }
323
324
325 void InsetFormula::validate(LaTeXFeatures & features) const
326 {
327         par()->Validate(features);
328 }
329
330
331 int InsetFormula::ascent(BufferView *, LyXFont const &) const
332 {
333         return par()->ascent() + 1;
334 }
335
336
337 int InsetFormula::descent(BufferView *, LyXFont const &) const
338 {
339         return par()->descent() + 1;
340 }
341
342
343 int InsetFormula::width(BufferView *, LyXFont const &) const
344 {
345         Metrics();
346         return par()->width() + 2;
347 }