]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
Andre's mathinset shrink patch ; default .lyx extension when loading files
[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 "support/lstrings.h"
36 #include "LyXView.h"
37 #include "Painter.h"
38 #include "lyxrc.h"
39 #include "math_matrixinset.h"
40 #include "mathed/support.h"
41
42 using std::ostream;
43 using std::ifstream;
44 using std::istream;
45 using std::pair;
46 using std::endl;
47 using std::vector;
48
49 extern char const * latex_mathenv[];
50 extern MathCursor * mathcursor;
51
52
53 // quite a hack i know. Should be done with return values...
54 int number_of_newlines = 0;
55
56
57 InsetFormula::InsetFormula()
58         : InsetFormulaBase(new MathMatrixInset)
59 {}
60
61
62 InsetFormula::InsetFormula(MathInsetTypes t)
63         : InsetFormulaBase(new MathMatrixInset(t))
64 {}
65
66
67 InsetFormula::InsetFormula(string const & s)
68         : InsetFormulaBase(mathed_parse(s))
69 {
70         metrics();
71 }
72
73
74 Inset * InsetFormula::clone(Buffer const &, bool) const
75 {
76         return new InsetFormula(*this);
77 }
78
79
80 void InsetFormula::write(ostream & os) const
81 {
82         os << "Formula ";
83         latex(os, false, false);
84 }
85
86
87 int InsetFormula::latex(ostream & os, bool fragile, bool) const
88 {
89         par()->write(os, fragile);
90         return 1;
91 }
92
93
94 int InsetFormula::ascii(ostream & os, int) const
95 {
96         par()->write(os, false);
97         return 1;
98 }
99
100
101 int InsetFormula::linuxdoc(ostream & os) const
102 {
103         return ascii(os, 0);
104 }
105
106
107 int InsetFormula::docBook(ostream & os) const
108 {
109         return ascii(os, 0);
110 }
111
112
113 void InsetFormula::read(LyXLex & lex)
114 {
115         par(mathed_parse(lex));
116         metrics();
117 }
118
119
120 void InsetFormula::draw(BufferView * bv, LyXFont const &,
121                         int y, float & xx, bool) const
122 {
123         int x = int(xx) - 1;
124         y -= 2;
125
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(x, y - a, w, h, LColor::mathbg);
134
135         if (mathcursor && mathcursor->formula() == this) {
136                 mathcursor->drawSelection(pain);
137                 pain.rectangle(x, y - a, w, h, LColor::mathframe);
138         }
139
140         par()->draw(pain, x, y);
141         xx += par()->width();
142
143         setCursorVisible(false);
144 }
145
146
147 void InsetFormula::metrics() const 
148 {
149         const_cast<MathInset *>(par_)->metrics(display() ? LM_ST_DISPLAY : LM_ST_TEXT);
150 }
151
152 vector<string> const InsetFormula::getLabelList() const
153 {
154         return par()->getLabelList();
155 }
156
157
158 UpdatableInset::RESULT
159 InsetFormula::localDispatch(BufferView * bv, kb_action action,
160          string const & arg)
161 {
162         RESULT result = DISPATCHED;
163
164         switch (action) {
165
166                 case LFUN_BREAKLINE: 
167                         bv->lockedInsetStoreUndo(Undo::INSERT);
168                         int x;
169                         int y;
170                         mathcursor->getPos(x, y);
171                         mathcursor->breakLine();
172                         mathcursor->normalize();
173                         updateLocal(bv, true);
174                         break;
175
176                 case LFUN_MATH_NUMBER:
177                 {
178                         //lyxerr << "toggling all numbers\n";
179                         if (display()) {
180                                 bv->lockedInsetStoreUndo(Undo::INSERT);
181                                 bool old = par()->numberedType();
182                                 for (int row = 0; row < par()->nrows(); ++row)
183                                         par()->numbered(row, !old);
184                                 bv->owner()->message(old ? _("No number") : _("Number"));
185                                 updateLocal(bv, true);
186                         }
187                         break;
188                 }
189
190                 case LFUN_MATH_NONUMBER:
191                 {
192                         //lyxerr << "toggling line number\n";
193                         if (display()) {
194                                 bv->lockedInsetStoreUndo(Undo::INSERT);
195                                 int row = mathcursor->row();
196                                 bool old = par()->numbered(row);
197                                 bv->owner()->message(old ? _("No number") : _("Number"));
198                                 par()->numbered(row, !old);
199                                 updateLocal(bv, true);
200                         }
201                         break;
202                 }
203
204                 case LFUN_INSERT_LABEL:
205                 {
206                         bv->lockedInsetStoreUndo(Undo::INSERT);
207
208                         int row = mathcursor->row();
209                         string old_label = par()->label(row);
210                         string new_label = arg;
211
212                         if (new_label.empty()) {
213                                 string const default_label =
214                                         (lyxrc.label_init_length >= 0) ? "eq:" : "";
215                                 pair<bool, string> const res = old_label.empty()
216                                         ? askForText(_("Enter new label to insert:"), default_label)
217                                         : askForText(_("Enter label:"), old_label);
218                                 
219                                 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
220                                 if (!res.first)
221                                         break;
222                                 new_label = frontStrip(strip(res.second));
223                         }
224
225                         //if (new_label == old_label)
226                         //      break;  // Nothing to do
227
228                         if (!new_label.empty()) {
229                                 lyxerr << "setting label to '" << new_label << "'\n";
230                                 par()->numbered(row, true);
231                         }
232
233                         if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
234                                 bv->redraw();
235
236                         par()->label(row, new_label);
237
238                         updateLocal(bv, true);
239                         break;
240                 }
241
242                 case LFUN_MATH_EXTERN:
243                         bv->lockedInsetStoreUndo(Undo::EDIT);
244                         handleExtern(arg, bv);
245                         updateLocal(bv, true);
246                         break;
247
248                 case LFUN_MATH_MUTATE:
249                 {
250                         bv->lockedInsetStoreUndo(Undo::EDIT);
251                         int x;
252                         int y;
253                         mathcursor->getPos(x, y);
254                         par()->mutate(arg);
255                         mathcursor->setPos(x, y);
256                         mathcursor->normalize();
257                         updateLocal(bv, true);
258                         break;
259                 }
260
261                 case LFUN_MATH_DISPLAY:
262                 {
263                         int x;
264                         int y;
265                         mathcursor->getPos(x, y);
266                         if (par()->getType() == LM_OT_SIMPLE)
267                                 par()->mutate(LM_OT_EQUATION);
268                         else
269                                 par()->mutate(LM_OT_SIMPLE);
270                         mathcursor->setPos(x, y);
271                         mathcursor->normalize();
272                         updateLocal(bv, true);
273                         break;
274                 }
275                 
276                 case LFUN_PASTESELECTION:
277                 {
278                         string const clip = bv->getClipboard();
279                 if (!clip.empty())
280                                 par(mathed_parse(clip));
281                         break;
282                 }
283
284                 default:
285                         result = InsetFormulaBase::localDispatch(bv, action, arg);
286         }
287
288         return result;
289 }
290
291
292 void InsetFormula::handleExtern(const string & arg, BufferView *)
293 {
294         //string outfile = lyx::tempName("maple.out");
295         string outfile = "/tmp/lyx2" + arg + ".out";
296         ostringstream os;
297         par()->writeNormal(os); 
298         string code = os.str().c_str();
299         string script = "lyx2" + arg + " '" + code + "' " + outfile;
300         lyxerr << "calling: " << script << endl;
301         Systemcalls cmd(Systemcalls::System, script, 0);
302
303         ifstream is(outfile.c_str());
304         par(mathed_parse(is));
305         metrics();
306 }
307
308 bool InsetFormula::display() const
309 {
310         return par()->getType() != LM_OT_SIMPLE;
311 }
312
313
314 MathMatrixInset * InsetFormula::par() const
315 {
316         return static_cast<MathMatrixInset *>(par_);
317 }
318
319 void InsetFormula::par(MathInset * p)
320
321         delete par_;
322         par_ = p ? p : new MathMatrixInset;
323 }
324
325
326 Inset::Code InsetFormula::lyxCode() const
327 {
328         return Inset::MATH_CODE;
329 }
330
331
332 void InsetFormula::validate(LaTeXFeatures & features) const
333 {
334         par()->validate(features);
335 }
336
337 bool InsetFormula::insetAllowed(Inset::Code code) const
338 {
339         return code == Inset::LABEL_CODE; 
340 }
341
342
343 int InsetFormula::ascent(BufferView *, LyXFont const &) const
344 {
345         return par()->ascent() + 1;
346 }
347
348
349 int InsetFormula::descent(BufferView *, LyXFont const &) const
350 {
351         return par()->descent() + 1;
352 }
353
354
355 int InsetFormula::width(BufferView *, LyXFont const &) const
356 {
357         metrics();
358         return par()->width();
359 }
360
361
362 MathInsetTypes InsetFormula::getType() const
363 {
364         return par()->getType();;
365 }