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