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