]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
change a lot of methods to begin with small char
[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 #include <config.h>
17 #include <fstream>
18
19 #include "support/LOstream.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
29 #include "lyx_main.h"
30 #include "BufferView.h"
31 #include "lyxtext.h"
32 #include "gettext.h"
33 #include "debug.h"
34 #include "lyx_gui_misc.h"
35 #include "support/LOstream.h"
36 #include "support/lyxlib.h"
37 #include "support/syscall.h"
38 #include "LyXView.h"
39 #include "Painter.h"
40 #include "font.h"
41 #include "lyxrc.h"
42 #include "math_matrixinset.h"
43 #include "mathed/support.h"
44
45 using std::ostringstream;
46 using std::ostream;
47 using std::ifstream;
48 using std::istream;
49 using std::pair;
50 using std::endl;
51 using std::vector;
52
53 extern char const * latex_mathenv[];
54 extern MathCursor * mathcursor;
55 extern LyXFont WhichFont(short type, int size);
56
57
58
59 // quite a hack i know. Should be done with return values...
60 int number_of_newlines = 0;
61
62
63 InsetFormula::InsetFormula()
64         : InsetFormulaBase(new MathMatrixInset)
65 {}
66
67
68 InsetFormula::InsetFormula(MathInsetTypes t)
69         : InsetFormulaBase(new MathMatrixInset(t))
70 {}
71
72
73
74 Inset * InsetFormula::clone(Buffer const &) const
75 {
76         return new InsetFormula(*this);
77 }
78
79
80 void InsetFormula::write(Buffer const * buf, ostream & os) const
81 {
82         os << "Formula ";
83         latex(buf, os, false, false);
84 }
85
86
87 int InsetFormula::latex(Buffer const *, ostream & os, bool fragile, bool) const
88 {
89         par()->Write(os, fragile);
90         return 1;
91 }
92
93
94 int InsetFormula::ascii(Buffer const *, ostream & os, int) const
95 {
96         par()->Write(os, false);
97         return 1;
98 }
99
100
101 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
102 {
103         return ascii(buf, os, 0);
104 }
105
106
107 int InsetFormula::docBook(Buffer const * buf, ostream & os) const
108 {
109         return ascii(buf, os, 0);
110 }
111
112
113 void InsetFormula::read(Buffer const *, LyXLex & lex)
114 {
115         par_ = mathed_parse(lex);
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         MathInset::workwidth = bv->workWidth();
125         Painter & pain = bv->painter();
126
127         int w = par()->width();
128         int h = par()->height();
129         int a = par()->ascent();
130         pain.fillRectangle(int(x), y - a, w, h, LColor::mathbg);
131
132         if (mathcursor) {
133                 par()->Metrics(LM_ST_TEXT);
134
135                 if (mathcursor->formula() == this) {
136                         if (mathcursor->Selection()) {
137                                 int xp[10];
138                                 int yp[10];
139                                 int n;
140                                 mathcursor->SelGetArea(xp, yp, n);
141                                 pain.fillPolygon(xp, yp, n, LColor::selection);
142                         }
143                         pain.rectangle(int(x), y - a, w, h, LColor::mathframe);
144                 }
145         }
146
147         par()->draw(pain, int(x), y);
148         xx += par()->width();
149
150         setCursorVisible(false);
151 }
152
153
154 vector<string> const InsetFormula::getLabelList() const
155 {
156         return par()->getLabelList();
157 }
158
159
160 UpdatableInset::RESULT
161 InsetFormula::localDispatch(BufferView * bv, kb_action action,
162          string const & arg)
163 {
164         RESULT result = DISPATCHED;
165
166         switch (action) {
167
168                 case LFUN_BREAKLINE: 
169                         bv->lockedInsetStoreUndo(Undo::INSERT);
170                         par()->breakLine();
171                         updateLocal(bv);
172                         break;
173
174
175                 case LFUN_DELETE_LINE_FORWARD:
176                         bv->lockedInsetStoreUndo(Undo::DELETE);
177                         mathcursor->DelLine();
178                         updateLocal(bv);
179                         break;
180
181                 case LFUN_MATH_NUMBER:
182                 {
183                         //lyxerr << "toggling all numbers\n";
184                         if (display()) {
185                                 bv->lockedInsetStoreUndo(Undo::INSERT);
186                                 bool old = par()->numberedType();
187                                 for (int row = 0; row < par()->nrows(); ++row)
188                                         par()->numbered(row, !old);
189                                 bv->owner()->message(old ? _("No number") : _("Number"));
190                                 updateLocal(bv);
191                         }
192                         break;
193                 }
194
195                 case LFUN_MATH_NONUMBER:
196                 {
197                         //lyxerr << "toggling line number\n";
198                         if (display()) {
199                                 bv->lockedInsetStoreUndo(Undo::INSERT);
200                                 int row = mathcursor->row();
201                                 bool old = par()->numbered(row);
202                                 bv->owner()->message(old ? _("No number") : _("Number"));
203                                 par()->numbered(row, !old);
204                                 updateLocal(bv);
205                         }
206                         break;
207                 }
208
209                 case LFUN_INSERT_LABEL:
210                 {
211                         bv->lockedInsetStoreUndo(Undo::INSERT);
212
213                         int row = mathcursor->row();
214                         string old_label = par()->label(row);
215                         string new_label = arg;
216
217                         if (new_label.empty()) {
218                                 string const default_label =
219                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
220                                 pair<bool, string> const res = old_label.empty()
221                                         ? askForText(_("Enter new label to insert:"), default_label)
222                                         : askForText(_("Enter label:"), old_label);
223                                 
224                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
225                                 if (!res.first)
226                                         break;
227                                 new_label = frontStrip(strip(res.second));
228                         }
229
230                         //if (new_label == old_label)
231                         //      break;  // Nothing to do
232
233                         if (!new_label.empty()) {
234                                 lyxerr << "setting label to '" << new_label << "'\n";
235                                 par()->numbered(row, true);
236                         }
237
238                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
239                                 bv->redraw();
240
241                         par()->label(row, new_label);
242
243                         updateLocal(bv);
244                         break;
245                 }
246
247                 case LFUN_MATH_EXTERN:
248                         handleExtern(arg, bv);
249                         updateLocal(bv);
250                         break;
251
252                 case LFUN_MATH_MUTATE:
253                         par()->mutate(arg);
254                         updateLocal(bv);
255                         break;
256
257                 case LFUN_TABINSERT:
258                         lyxerr << "take index from cursor\n";
259                         par()->splitCell(0);
260                         updateLocal(bv);
261                         break;
262
263                 case LFUN_MATH_DISPLAY:
264                         if (par()->GetType() == LM_OT_SIMPLE)
265                                 par()->mutate(LM_OT_EQUATION);
266                         else
267                                 par()->mutate(LM_OT_SIMPLE);
268                         updateLocal(bv);
269                         break;
270
271                 default:
272                         result = InsetFormulaBase::localDispatch(bv, action, arg);
273         }
274
275         return result;
276 }
277
278
279 void InsetFormula::handleExtern(const string & arg, BufferView *)
280 {
281         //string outfile = lyx::tempName("maple.out");
282         string outfile = "/tmp/lyx2" + arg + ".out";
283         ostringstream os;
284         par()->WriteNormal(os); 
285         string code = os.str().c_str();
286         string script = "lyx2" + arg + " '" + code + "' " + outfile;
287         lyxerr << "calling: " << script << endl;
288         Systemcalls cmd(Systemcalls::System, script, 0);
289
290         ifstream is(outfile.c_str());
291         par_ = mathed_parse(is);
292 }
293
294 bool InsetFormula::display() const
295 {
296         return par_->GetType() != LM_OT_SIMPLE;
297 }
298
299
300 MathMatrixInset * InsetFormula::par() const
301 {
302         return static_cast<MathMatrixInset *>(par_);
303 }
304
305
306 Inset::Code InsetFormula::lyxCode() const
307 {
308         return Inset::MATH_CODE;
309 }
310
311
312 void InsetFormula::validate(LaTeXFeatures & features) const
313 {
314         par()->Validate(features);
315 }
316
317
318 int InsetFormula::ascent(BufferView *, LyXFont const &) const
319 {
320         return par()->ascent() + 4;
321 }
322
323
324 int InsetFormula::descent(BufferView *, LyXFont const &) const
325 {
326         return par()->descent();
327 }
328
329
330 int InsetFormula::width(BufferView *, LyXFont const &) const
331 {
332         par()->Metrics(LM_ST_TEXT);
333         return par()->width();
334 }
335
336 /*
337 LyXFont const InsetFormula::ConvertFont(LyXFont const & f) const
338 {
339         // We have already discussed what was here
340         LyXFont font(f);
341         font.setLatex(LyXFont::OFF);
342         return font;
343 }
344 */