]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
LyX Drinkers Union: patch 1
[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 & x, bool) const
121 {
122         MathInset::workwidth = bv->workWidth();
123         Painter & pain = bv->painter();
124
125         if (mathcursor) {
126                 par()->Metrics(LM_ST_TEXT);
127                 int w = par()->width()  + 2;
128                 int a = par()->ascent() + 1;
129                 int h = par()->height() + 1;
130
131                 if (mathcursor->formula() == this) {
132                         if (mathcursor->Selection()) {
133                                 int xp[10];
134                                 int yp[10];
135                                 int n;
136                                 mathcursor->SelGetArea(xp, yp, n);
137                                 pain.fillPolygon(xp, yp, n, LColor::selection);
138                         }
139                         pain.rectangle(int(x - 1), y - a, w, h, LColor::green);
140                 }
141         }
142
143         par()->draw(pain, int(x), y);
144         x += par()->width();
145
146         setCursorVisible(false);
147 }
148
149
150 vector<string> const InsetFormula::getLabelList() const
151 {
152         return par()->getLabelList();
153 }
154
155
156 UpdatableInset::RESULT
157 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
158          string const & arg)
159 {
160         RESULT result = DISPATCHED;
161
162         switch (action) {
163
164                 case LFUN_BREAKLINE: 
165                         bv->lockedInsetStoreUndo(Undo::INSERT);
166                         par()->breakLine();
167                         UpdateLocal(bv);
168                         break;
169
170
171                 case LFUN_DELETE_LINE_FORWARD:
172                         bv->lockedInsetStoreUndo(Undo::DELETE);
173                         mathcursor->DelLine();
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 = par()->nrows() - 1;
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 = par()->nrows() - 1;
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                         HandleExtern(arg, bv);
245                         UpdateLocal(bv);
246                         break;
247
248                 case LFUN_MATH_MUTATE:
249                         par()->mutate(arg);
250                         UpdateLocal(bv);
251                         break;
252
253                 case LFUN_TABINSERT:
254                         lyxerr << "take index from cursor\n";
255                         par()->splitCell(0);
256                         UpdateLocal(bv);
257                         break;
258
259                 case LFUN_MATH_DISPLAY:
260                         if (par()->GetType() == LM_OT_SIMPLE)
261                                 par()->mutate(LM_OT_EQUATION);
262                         else
263                                 par()->mutate(LM_OT_SIMPLE);
264                         UpdateLocal(bv);
265                         break;
266
267                 default:
268                         result = InsetFormulaBase::LocalDispatch(bv, action, arg);
269         }
270
271         return result;
272 }
273
274
275 void InsetFormula::HandleExtern(const string & arg, BufferView *)
276 {
277         //string outfile = lyx::tempName("maple.out");
278         string outfile = "/tmp/lyx2" + arg + ".out";
279         ostringstream os;
280         par()->WriteNormal(os); 
281         string code = os.str().c_str();
282         string script = "lyx2" + arg + " '" + code + "' " + outfile;
283         lyxerr << "calling: " << script << endl;
284         Systemcalls cmd(Systemcalls::System, script, 0);
285
286         ifstream is(outfile.c_str());
287         par_ = mathed_parse(is);
288 }
289
290 bool InsetFormula::display() const
291 {
292         return par_->GetType() != LM_OT_SIMPLE;
293 }
294
295
296 MathMatrixInset * InsetFormula::par() const
297 {
298         return static_cast<MathMatrixInset *>(par_);
299 }
300
301
302 Inset::Code InsetFormula::LyxCode() const
303 {
304         return Inset::MATH_CODE;
305 }
306
307
308 void InsetFormula::Validate(LaTeXFeatures & features) const
309 {
310         par()->Validate(features);
311 }
312
313
314 int InsetFormula::ascent(BufferView *, LyXFont const &) const
315 {
316         return par()->ascent();
317 }
318
319
320 int InsetFormula::descent(BufferView *, LyXFont const &) const
321 {
322         return par()->descent();
323 }
324
325
326 int InsetFormula::width(BufferView *, LyXFont const &) const
327 {
328         par()->Metrics(LM_ST_TEXT);
329         return par()->width();
330 }
331
332 /*
333 LyXFont const InsetFormula::ConvertFont(LyXFont const & f) const
334 {
335         // We have already discussed what was here
336         LyXFont font(f);
337         font.setLatex(LyXFont::OFF);
338         return font;
339 }
340 */