]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
the lfun3 patches (overall cleanup and "localizing" dispatch() in mathed)
[lyx.git] / src / mathed / formulabase.C
1 /*
2 *  File:        formulabase.C
3 *  Purpose:     Implementation of common parts of the LyX  math insets
4 *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 *  Created:     January 1996
6 *
7 *  Copyright: 1996-1998 Alejandro Aguilar Sierra
8 *
9 *  Version: 0.4, Lyx project.
10 *
11 *   You are free to use and modify this code under the terms of
12 *   the GNU General Public Licence version 2 or later.
13 */
14
15 #include <config.h>
16 #include <fstream>
17
18 #include "Lsstream.h"
19 #include "support/LAssert.h"
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "formula.h"
26 #include "formulamacro.h"
27 #include "lyxrc.h"
28 #include "funcrequest.h"
29 #include "BufferView.h"
30 #include "lyxtext.h"
31 #include "lyxfunc.h"
32 #include "gettext.h"
33 #include "LaTeXFeatures.h"
34 #include "debug.h"
35 #include "math_support.h"
36 #include "math_metricsinfo.h"
37 #include "support/lstrings.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/mouse_state.h"
41 #include "Lsstream.h"
42 #include "math_arrayinset.h"
43 #include "math_charinset.h"
44 #include "math_deliminset.h"
45 #include "math_cursor.h"
46 #include "math_factory.h"
47 #include "math_fontinset.h"
48 #include "math_hullinset.h"
49 #include "math_iterator.h"
50 #include "math_macrotable.h"
51 #include "math_parser.h"
52 #include "math_pos.h"
53 #include "math_spaceinset.h"
54 #include "undo_funcs.h"
55 #include "textpainter.h"
56 #include "frontends/Dialogs.h"
57 #include "intl.h"
58 #include "insets/insetcommandparams.h"
59 #include "ref_inset.h"
60
61 using std::endl;
62 using std::ostream;
63 using std::vector;
64 using std::abs;
65
66 MathCursor * mathcursor = 0;
67
68
69 namespace {
70
71 // local global
72 int first_x;
73 int first_y;
74
75
76
77 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
78 {
79         if (!bv->insertInset(new_inset)) {
80                 delete new_inset;
81                 return false;
82         }
83         new_inset->edit(bv, true);
84         return true;
85 }
86
87
88 } // namespace anon
89
90
91
92 InsetFormulaBase::InsetFormulaBase()
93         : font_(), xo_(0), yo_(0)
94 {
95         // This is needed as long the math parser is not re-entrant
96         initMath();
97         //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
98         //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
99         //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
100         //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
101 }
102
103
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
106 {
107 #if 0
108         // translate to latex
109         ostringstream os;
110         latex(NULL, os, false, false);
111         string str = os.str();
112
113         // insert this text
114         LyXText * lt = view_->getLyXText();
115         string::const_iterator cit = str.begin();
116         string::const_iterator end = str.end();
117         for (; cit != end; ++cit)
118                 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
119
120         // remove ourselves
121         //view_->owner()->dispatch(LFUN_ESCAPE);
122 #endif
123 }
124
125
126 void InsetFormulaBase::handleFont
127         (BufferView * bv, string const & arg, string const & font)
128 {
129         bv->lockedInsetStoreUndo(Undo::EDIT);
130         bool sel = mathcursor->selection();
131         if (sel)
132                 updateLocal(bv, true);
133         mathcursor->handleNest(createMathInset(font));
134         mathcursor->insert(arg);
135         if (!sel)
136                 updateLocal(bv, false);
137 }
138
139
140 // Check if uses AMS macros
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
142 {}
143
144
145 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
146 {
147         font_ = f;
148         metrics(bv);
149 }
150
151
152 void InsetFormulaBase::metrics(BufferView * bv) const
153 {
154         if (bv)
155                 view_ = bv->owner()->view();
156         MathMetricsInfo mi;
157         mi.base.style = LM_ST_TEXT;
158         mi.base.font  = font_;
159         mi.base.font.setColor(LColor::math);
160         par()->metrics(mi);
161 }
162
163
164 string const InsetFormulaBase::editMessage() const
165 {
166         return _("Math editor mode");
167 }
168
169
170 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
171 {
172         if (!bv->lockInset(this))
173                 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174         releaseMathCursor(bv);
175         mathcursor = new MathCursor(this, true);
176         metrics(bv);
177         mathcursor->setPos(x, y);
178         //lyxerr << "setting pos to " << x << "," << y << "\n";
179
180         // if that is removed, we won't get the magenta box when entering an
181         // inset for the first time
182         bv->updateInset(this, false);
183 }
184
185
186 void InsetFormulaBase::edit(BufferView * bv, bool front)
187 {
188         if (!bv->lockInset(this))
189                 lyxerr << "Cannot lock math inset in edit call!\n";
190         releaseMathCursor(bv);
191         mathcursor = new MathCursor(this, front);
192         metrics(bv);
193         bv->updateInset(this, false);
194 }
195
196
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
198 {
199         if (mathcursor) {
200                 if (mathcursor->inMacroMode()) {
201                         mathcursor->macroModeClose();
202                         updateLocal(bv, true);
203                 }
204                 releaseMathCursor(bv);
205         }
206         generatePreview();
207         bv->updateInset(this, false);
208 }
209
210
211 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
212 {
213         // calling metrics here destroys the cached xo,yo positions e.g. in
214         // MathParboxinset. And it would be too expensive anyway...
215         //metrics(bv);
216         mathcursor->getPos(x, y);
217         //x -= xo_;
218         y -= yo_;
219         //lyxerr << "getCursorPos: " << x << " " << y << "\n";
220 }
221
222
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
224 {
225         //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
226         if (isCursorVisible())
227                 hideInsetCursor(bv);
228         else
229                 showInsetCursor(bv);
230 }
231
232
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
234 {
235         if (isCursorVisible())
236                 return;
237         fitInsetCursor(bv);
238         int x, y, asc, des;
239         getCursorPos(bv, x, y);
240         math_font_max_dim(font_, asc, des);
241         bv->showLockedInsetCursor(x, y, asc, des);
242         setCursorVisible(true);
243         //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
244 }
245
246
247 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
248 {
249         if (!isCursorVisible())
250                 return;
251         bv->hideLockedInsetCursor();
252         setCursorVisible(false);
253         //lyxerr << "hideInsetCursor: \n";
254 }
255
256
257 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
258 {
259         if (!mathcursor)
260                 return;
261         int x, y, asc, des;
262         math_font_max_dim(font_, asc, des);
263         getCursorPos(bv, x, y);
264         //y += yo_;
265         //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
266         bv->fitLockedInsetCursor(x, y, asc, des);
267 }
268
269
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
271 {
272         if (mathcursor)
273                 bv->updateInset(this, false);
274 }
275
276
277 vector<string> const InsetFormulaBase::getLabelList() const
278 {
279   return vector<string>();
280 }
281
282
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
284 {
285         bv->updateInset(this, dirty);
286 }
287
288
289 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
290         int x, int y, mouse_button::state button)
291 {
292         if (!mathcursor)
293                 return false;
294
295         //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
296         hideInsetCursor(bv);
297         showInsetCursor(bv);
298         bv->updateInset(this, false);
299
300         if (button == mouse_button::button3) {
301                 // try to dispatch to enclosed insets first
302                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 3)))
303                         return true;
304
305                 // launch math panel for right mouse button
306                 bv->owner()->getDialogs().showMathPanel();
307                 return true;
308         }
309
310         if (button == mouse_button::button1) {
311                 // try to dispatch to enclosed insets first
312                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 1)))
313                         return true;
314
315                 // try to set the cursor
316                 //delete mathcursor;
317                 //mathcursor = new MathCursor(this, x == 0);
318                 //metrics(bv);
319                 //mathcursor->setPos(x + xo_, y + yo_);
320                 return true;
321         }
322         return false;
323 }
324
325
326 void InsetFormulaBase::insetButtonPress(BufferView * bv,
327                                         int x, int y, mouse_button::state button)
328 {
329         //lyxerr << "insetButtonPress: "
330         //      << x << " " << y << " but: " << button << "\n";
331         //lyxerr << "formula: ";
332         //par()->dump();
333
334         releaseMathCursor(bv);
335         mathcursor = new MathCursor(this, x == 0);
336
337         if (button == mouse_button::button1) {
338                 // just set the cursor here
339                 //lyxerr << "setting cursor\n";
340                 metrics(bv);
341                 first_x = x;
342                 first_y = y;
343                 mathcursor->selClear();
344                 mathcursor->setPos(x + xo_, y + yo_);
345
346                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 1))) {
347                         //delete mathcursor;
348                         return;
349                 }
350
351         }
352         if (button == mouse_button::button3) {
353                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 3))) {
354                         //delete mathcursor;
355                         return;
356                 }
357         }
358         bv->updateInset(this, false);
359 }
360
361
362 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
363         int x, int y, mouse_button::state button)
364 {
365         if (!mathcursor)
366                 return;
367
368         if (button == mouse_button::button1)
369                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 1)))
370                         return;
371
372         if (button == mouse_button::button3)
373                 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 3)))
374                         return;
375
376         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
377                 //lyxerr << "insetMotionNotify: ignored\n";
378                 return;
379         }
380         first_x = x;
381         first_y = y;
382
383         if (!mathcursor->selection())
384                 mathcursor->selStart();
385
386         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
387         //      << ' ' << button << "\n";
388         hideInsetCursor(bv);
389         mathcursor->setPos(x + xo_, y + yo_);
390         showInsetCursor(bv);
391         bv->updateInset(this, false);
392 }
393
394
395 UpdatableInset::RESULT
396 InsetFormulaBase::localDispatch(FuncRequest const & ev)
397 {
398         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
399         //      << " arg: '" << arg
400         //      << "' cursor: " << mathcursor << "\n";
401
402         if (!mathcursor)
403                 return UNDISPATCHED;
404
405         BufferView * bv    = ev.view();
406         string argument    = ev.argument;
407         RESULT result      = DISPATCHED;
408         bool sel           = false;
409         bool was_macro     = mathcursor->inMacroMode();
410         bool was_selection = mathcursor->selection();
411
412         hideInsetCursor(bv);
413
414         mathcursor->normalize();
415         mathcursor->touch();
416
417         switch (ev.action) {
418
419         case LFUN_MATH_NUMBER:
420         case LFUN_MATH_NONUMBER:
421         case LFUN_TABINSERT:
422         case LFUN_BREAKLINE:
423         case LFUN_DELETE_LINE_FORWARD:
424                 bv->lockedInsetStoreUndo(Undo::EDIT);
425                 mathcursor->dispatch(ev);
426                 updateLocal(bv, true);
427                 break;
428
429         case LFUN_WORDRIGHTSEL:
430         case LFUN_RIGHTSEL:
431                 sel = true; // fall through...
432         case LFUN_RIGHT:
433                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
434                 //lyxerr << "calling scroll 20\n";
435                 //scroll(bv, 20);
436                 updateLocal(bv, false);
437                 // write something to the minibuffer
438                 //bv->owner()->message(mathcursor->info());
439                 break;
440
441         case LFUN_WORDLEFTSEL:
442         case LFUN_LEFTSEL:
443                 sel = true; // fall through
444         case LFUN_LEFT:
445                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
446                 updateLocal(bv, false);
447                 break;
448
449         case LFUN_UPSEL:
450                 sel = true; // fall through
451         case LFUN_UP:
452                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
453                 updateLocal(bv, false);
454                 break;
455
456         case LFUN_DOWNSEL:
457                 sel = true; // fall through
458         case LFUN_DOWN:
459                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
460                 updateLocal(bv, false);
461                 break;
462
463         case LFUN_HOMESEL:
464                 sel = true; // fall through
465         case LFUN_HOME:
466                 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
467                 updateLocal(bv, false);
468                 break;
469
470         case LFUN_ENDSEL:
471                 sel = true; // fall through
472         case LFUN_END:
473                 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
474                 updateLocal(bv, false);
475                 break;
476
477         case LFUN_PRIORSEL:
478         case LFUN_PRIOR:
479                 result = FINISHED_UP;
480                 updateLocal(bv, false);
481                 break;
482
483         case LFUN_NEXTSEL:
484         case LFUN_NEXT:
485                 result = FINISHED_DOWN;
486                 updateLocal(bv, false);
487                 break;
488
489         case LFUN_TAB:
490                 mathcursor->idxNext();
491                 updateLocal(bv, false);
492                 break;
493
494         case LFUN_SHIFT_TAB:
495                 mathcursor->idxPrev();
496                 updateLocal(bv, false);
497                 break;
498
499         case LFUN_DELETE_WORD_BACKWARD:
500         case LFUN_BACKSPACE:
501                 bv->lockedInsetStoreUndo(Undo::DELETE);
502                 mathcursor->backspace();
503                 updateLocal(bv, true);
504                 break;
505
506         case LFUN_DELETE_WORD_FORWARD:
507         case LFUN_DELETE:
508                 bv->lockedInsetStoreUndo(Undo::DELETE);
509                 mathcursor->erase();
510                 bv->updateInset(this, true);
511                 break;
512
513         //    case LFUN_GETXY:
514         //      sprintf(dispatch_buffer, "%d %d",);
515         //      dispatch_result = dispatch_buffer;
516         //      break;
517         case LFUN_SETXY: {
518                 lyxerr << "LFUN_SETXY broken!\n";
519                 int x = 0;
520                 int y = 0;
521                 istringstream is(ev.argument.c_str());
522                 is >> x >> y;
523                 mathcursor->setPos(x, y);
524                 updateLocal(bv, false);
525                 break;
526         }
527
528         case LFUN_PASTE:
529                 if (was_macro)
530                         mathcursor->macroModeClose();
531                 bv->lockedInsetStoreUndo(Undo::EDIT);
532                 mathcursor->selPaste();
533                 updateLocal(bv, true);
534                 break;
535
536         case LFUN_CUT:
537                 bv->lockedInsetStoreUndo(Undo::DELETE);
538                 mathcursor->selCut();
539                 updateLocal(bv, true);
540                 break;
541
542         case LFUN_COPY:
543                 mathcursor->selCopy();
544                 break;
545
546
547         // Special casing for superscript in case of LyX handling
548         // dead-keys:
549         case LFUN_CIRCUMFLEX:
550                 if (ev.argument.empty()) {
551                         // do superscript if LyX handles
552                         // deadkeys
553                         bv->lockedInsetStoreUndo(Undo::EDIT);
554                         mathcursor->script(true);
555                         updateLocal(bv, true);
556                 }
557                 break;
558
559         case LFUN_UMLAUT:
560         case LFUN_ACUTE:
561         case LFUN_GRAVE:
562         case LFUN_BREVE:
563         case LFUN_DOT:
564         case LFUN_MACRON:
565         case LFUN_CARON:
566         case LFUN_TILDE:
567         case LFUN_CEDILLA:
568         case LFUN_CIRCLE:
569         case LFUN_UNDERDOT:
570         case LFUN_TIE:
571         case LFUN_OGONEK:
572         case LFUN_HUNG_UMLAUT:
573                 break;
574
575         //  Math fonts
576         case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
577         case LFUN_BOLD:         handleFont(bv, ev.argument, "textbf"); break;
578         case LFUN_SANS:         handleFont(bv, ev.argument, "textsf"); break;
579         case LFUN_EMPH:         handleFont(bv, ev.argument, "mathcal"); break;
580         case LFUN_ROMAN:        handleFont(bv, ev.argument, "mathrm"); break;
581         case LFUN_CODE:         handleFont(bv, ev.argument, "texttt"); break;
582         case LFUN_FRAK:         handleFont(bv, ev.argument, "mathfrak"); break;
583         case LFUN_ITAL:         handleFont(bv, ev.argument, "mathit"); break;
584         case LFUN_NOUN:         handleFont(bv, ev.argument, "mathbb"); break;
585         case LFUN_DEFAULT:      handleFont(bv, ev.argument, "textnormal"); break;
586         case LFUN_FREE:         handleFont(bv, ev.argument, "textrm"); break;
587
588         case LFUN_GREEK:
589                 handleFont(bv, ev.argument, "lyxgreek1");
590                 if (ev.argument.size())
591                         mathcursor->interpret(ev.argument);
592                 break;
593
594         case LFUN_MATH_MODE:
595                 if (mathcursor->currentMode()) {
596                         handleFont(bv, ev.argument, "textrm");
597                 } else {
598                         mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
599                         updateLocal(bv, true);
600                 }
601                 //bv->owner()->message(_("math text mode toggled"));
602                 break;
603
604         case LFUN_MATH_LIMITS:
605                 bv->lockedInsetStoreUndo(Undo::EDIT);
606                 if (mathcursor->toggleLimits())
607                         updateLocal(bv, true);
608                 break;
609
610         case LFUN_MATH_SIZE:
611 #if 0
612                 if (!arg.empty()) {
613                         bv->lockedInsetStoreUndo(Undo::EDIT);
614                         mathcursor->setSize(arg);
615                         updateLocal(bv, true);
616                 }
617 #endif
618                 break;
619
620         case LFUN_INSERT_MATRIX:
621                 if (!ev.argument.empty()) {
622                         bv->lockedInsetStoreUndo(Undo::EDIT);
623                         mathcursor->interpret("matrix " + ev.argument);
624                         updateLocal(bv, true);
625                 }
626                 break;
627
628         case LFUN_SUPERSCRIPT:
629         case LFUN_SUBSCRIPT:
630         {
631                 bv->lockedInsetStoreUndo(Undo::EDIT);
632                 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
633                 updateLocal(bv, true);
634                 break;
635         }
636
637         case LFUN_MATH_DELIM:
638         {
639                 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
640                 string ls;
641                 string rs = split(ev.argument, ls, ' ');
642                 // Reasonable default values
643                 if (ls.empty())
644                         ls = '(';
645                 if (rs.empty())
646                         rs = ')';
647
648                 bv->lockedInsetStoreUndo(Undo::EDIT);
649                 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
650                 updateLocal(bv, true);
651                 break;
652         }
653
654         case LFUN_PROTECTEDSPACE:
655         case LFUN_MATH_SPACE:
656                 bv->lockedInsetStoreUndo(Undo::EDIT);
657                 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
658                 updateLocal(bv, true);
659                 break;
660
661         case LFUN_UNDO:
662                 bv->owner()->message(_("Invalid action in math mode!"));
663                 break;
664
665
666         case LFUN_MATH_HALIGN:
667         case LFUN_MATH_VALIGN:
668         case LFUN_MATH_ROW_INSERT:
669         case LFUN_MATH_ROW_DELETE:
670         case LFUN_MATH_COLUMN_INSERT:
671         case LFUN_MATH_COLUMN_DELETE:
672         {
673                 MathInset::idx_type idx = 0;
674                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
675                 if (p) {
676                         mathcursor->popToEnclosingGrid();
677                         bv->lockedInsetStoreUndo(Undo::EDIT);
678                         char align = ev.argument.size() ? ev.argument[0] : 'c';
679                         switch (ev.action) {
680                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
681                                 case LFUN_MATH_VALIGN: p->valign(align); break;
682                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
683                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
684                                 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
685                                 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
686                                 default: ;
687                         }
688                         updateLocal(bv, true);
689                 }
690                 break;
691         }
692
693         case LFUN_EXEC_COMMAND:
694                 result = UNDISPATCHED;
695                 break;
696
697         case LFUN_INSET_ERT:
698                 // interpret this as if a backslash was typed
699                 bv->lockedInsetStoreUndo(Undo::EDIT);
700                 mathcursor->interpret('\\');
701                 updateLocal(bv, true);
702                 break;
703
704         case LFUN_BREAKPARAGRAPH:
705         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
706         case LFUN_BREAKPARAGRAPH_SKIP:
707                 argument = "\n";
708                 // fall through
709
710         case -1:
711         case LFUN_INSERT_MATH:
712         case LFUN_SELFINSERT:
713                 if (!argument.empty()) {
714                         bv->lockedInsetStoreUndo(Undo::EDIT);
715                         if (argument.size() == 1)
716                                 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
717                         else
718                                 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
719                         updateLocal(bv, true);
720                 }
721                 break;
722
723         case LFUN_MATH_PANEL:
724                 result = UNDISPATCHED;
725                 break;
726
727         case LFUN_ESCAPE:
728                 if (mathcursor->selection())
729                         mathcursor->selClear();
730                 else
731                         result = UNDISPATCHED;
732                 break;
733
734         case LFUN_INSET_TOGGLE:
735                 mathcursor->insetToggle();
736                 updateLocal(bv, true);
737                 break;
738
739         case LFUN_REF_INSERT:
740                 //if (argument.empty()) {
741                 //      InsetCommandParams p("ref");
742                 //      owner_->getDialogs().createRef(p.getAsString());
743                 //} else {
744                 //      InsetCommandParams p;
745                 //      p.setFromString(argument);
746
747                 //      InsetRef * inset = new InsetRef(p, *buffer_);
748                 //      if (!insertInset(inset))
749                 //              delete inset;
750                 //      else
751                 //              updateInset(inset, true);
752                 //}
753                 //
754                 if (ev.argument.empty()) {
755                         InsetCommandParams p("ref");
756                         bv->owner()->getDialogs().createRef(p.getAsString());
757                 } else {
758                         //mathcursor->handleNest(new InsetRef2);
759                         //mathcursor->insert(arg);
760                         mathcursor->insert(MathAtom(new RefInset(ev.argument)));
761                 }
762                 updateLocal(bv, true);
763                 break;
764
765         default:
766                 result = UNDISPATCHED;
767         }
768
769         mathcursor->normalize();
770         mathcursor->touch();
771
772         lyx::Assert(mathcursor);
773
774         if (mathcursor->selection() || was_selection)
775                 toggleInsetSelection(bv);
776
777         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
778             result == UNDISPATCHED) {
779                 fitInsetCursor(bv);
780                 showInsetCursor(bv);
781                 revealCodes(bv);
782         } else {
783                 releaseMathCursor(bv);
784                 bv->unlockInset(this);
785         }
786
787         return result;  // original version
788 }
789
790
791 void InsetFormulaBase::revealCodes(BufferView * bv) const
792 {
793         if (!mathcursor)
794                 return;
795         bv->owner()->message(mathcursor->info());
796
797 #if 0
798         // write something to the minibuffer
799         // translate to latex
800         mathcursor->markInsert();
801         ostringstream os;
802         write(NULL, os);
803         string str = os.str();
804         mathcursor->markErase();
805         string::size_type pos = 0;
806         string res;
807         for (string::iterator it = str.begin(); it != str.end(); ++it) {
808                 if (*it == '\n')
809                         res += ' ';
810                 else if (*it == '\0') {
811                         res += "  -X-  ";
812                         pos = it - str.begin();
813                 }
814                 else
815                         res += *it;
816         }
817         if (pos > 30)
818                 res = res.substr(pos - 30);
819         if (res.size() > 60)
820                 res = res.substr(0, 60);
821         bv->owner()->message(res);
822 #endif
823 }
824
825
826 Inset::Code InsetFormulaBase::lyxCode() const
827 {
828         return Inset::MATH_CODE;
829 }
830
831
832 int InsetFormulaBase::ylow() const
833 {
834         return yo_ - ascent(view(), font_);
835 }
836
837
838 int InsetFormulaBase::yhigh() const
839 {
840         return yo_ + descent(view(), font_);
841 }
842
843
844 int InsetFormulaBase::xlow() const
845 {
846         return xo_;
847 }
848
849
850 int InsetFormulaBase::xhigh() const
851 {
852         return xo_ + width(view(), font_);
853 }
854
855
856 /////////////////////////////////////////////////////////////////////
857
858
859 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
860                                      bool, bool)
861 {
862 #ifdef WITH_WARNINGS
863 #warning pretty ugly
864 #endif
865         static InsetFormulaBase * lastformula = 0;
866         static MathIterator current = MathIterator(ibegin(par().nucleus()));
867         static MathArray ar;
868         static string laststr;
869
870         if (lastformula != this || laststr != str) {
871                 //lyxerr << "reset lastformula to " << this << "\n";
872                 lastformula = this;
873                 laststr = str;
874                 current = ibegin(par().nucleus());
875                 ar.clear();
876                 mathed_parse_cell(ar, str);
877         } else {
878                 ++current;
879         }
880         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
881
882         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
883                 if (it.cell().matchpart(ar, it.back().pos_)) {
884                         bv->unlockInset(bv->theLockingInset());
885                         if (!bv->lockInset(this)) {
886                                 lyxerr << "Cannot lock inset" << endl;
887                                 return false;
888                         }
889                         delete mathcursor;
890                         mathcursor = new MathCursor(this, true);
891                         metrics(bv);
892                         mathcursor->setSelection(it, ar.size());
893                         current = it;
894                         it.jump(ar.size());
895                         updateLocal(bv, false);
896                         return true;
897                 }
898         }
899
900         //lyxerr << "not found!\n";
901         lastformula = 0;
902         return false;
903 }
904
905
906 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
907                                       bool a, bool b)
908 {
909         lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
910         return searchForward(bv, what, a, b);
911 }
912
913
914 /////////////////////////////////////////////////////////////////////
915
916
917 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
918 {
919         if (!bv->available())
920                 return;
921
922         // use selection if available..
923         //string sel;
924         //if (action == LFUN_MATH_IMPORT_SELECTION)
925         //      sel = "";
926         //else
927
928         string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
929
930         if (sel.empty()) {
931                 InsetFormula * f = new InsetFormula(bv);
932                 if (openNewInset(bv, f)) {
933                         f->mutate("simple");
934                         // don't do that also for LFUN_MATH_MODE unless you want end up with
935                         // always changing to mathrm when opening an inlined inset
936                         // -- I really hate "LyXfunc overloading"...
937                         if (display)
938                                 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
939                         f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
940                 }
941         } else {
942                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
943                 // formula otherwise
944                 InsetFormulaBase * f;
945                 if (sel.find("\\newcommand") == string::npos &&
946                                 sel.find("\\def") == string::npos)
947                         f = new InsetFormula(sel);
948                 else
949                         f = new InsetFormulaMacro(sel);
950                 bv->getLyXText()->cutSelection(bv);
951                 openNewInset(bv, f);
952         }
953         bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
954 }
955
956
957 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
958 {
959         mathDispatchCreation(bv, arg, true);
960 }
961
962
963 void mathDispatchMathMode(BufferView * bv, string const & arg)
964 {
965         mathDispatchCreation(bv, arg, false);
966 }
967
968
969 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
970 {
971         mathDispatchCreation(bv, arg, true);
972 }
973
974
975 void mathDispatchMathMacro(BufferView * bv, string const & arg)
976 {
977         if (!bv->available())
978                 return;
979         if (arg.empty())
980                 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
981         else {
982                 string s = arg;
983                 string const s1 = token(s, ' ', 1);
984                 int const na = s1.empty() ? 0 : lyx::atoi(s1);
985                 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
986         }
987 }
988
989
990 void mathDispatchMathDelim(BufferView * bv, string const & arg)
991 {
992         if (!bv->available())
993                 return;
994         InsetFormula * f = new InsetFormula(bv);
995         if (openNewInset(bv, f)) {
996                 f->mutate("simple");
997                 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_MATH_DELIM, arg));
998         }
999 }
1000
1001
1002 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1003 {
1004         if (!bv->available())
1005                 return;
1006         InsetFormula * f = new InsetFormula(bv);
1007         if (openNewInset(bv, f)) {
1008                 f->mutate("simple");
1009                 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATRIX, arg));
1010         }
1011 }
1012
1013
1014 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1015 {
1016         if (!bv->available())
1017                 return;
1018         InsetFormula * f = new InsetFormula(bv);
1019         if (openNewInset(bv, f)) {
1020                 f->mutate("simple");
1021     bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
1022         }
1023 }
1024
1025
1026 void mathDispatchGreek(BufferView * bv, string const & arg)
1027 {
1028         if (!bv->available())
1029                 return;
1030         InsetFormula * f = new InsetFormula(bv);
1031         if (openNewInset(bv, f)) {
1032                 f->mutate("simple");
1033                 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_GREEK, arg));
1034                 bv->unlockInset(f);
1035         }
1036 }
1037
1038
1039 void mathDispatch(BufferView *, kb_action, string const &)
1040 {}
1041
1042
1043 void mathDispatch(BufferView *, string const &)
1044 {}