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