]> git.lyx.org Git - lyx.git/blob - src/mathed/formulabase.C
ws changes
[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         if (!mathcursor)
276                 return false;
277         hideInsetCursor(bv);
278         showInsetCursor(bv);
279         bv->updateInset(this, false);
280         return false;
281 }
282
283
284 void InsetFormulaBase::insetButtonPress(BufferView * bv,
285                                         int x, int y, int button)
286 {
287         //lyxerr << "insetButtonPress: "
288         //      << x << " " << y << " but: " << button << "\n";
289
290         switch (button) {
291                 default:
292                 case 1:
293                         // left click
294                         if (!mathcursor) {
295                                 mathcursor = new MathCursor(this, x == 0);
296                                 metrics(bv);
297                         } 
298                         first_x = x;
299                         first_y = y;
300                         mathcursor->selClear();
301                         mathcursor->setPos(x + xo_, y + yo_);
302                         break;
303 /*
304                 case 2:
305                         lyxerr << "insetButtonPress: 2\n";
306                         // insert stuff
307                         if (mathcursor) {
308                                 bv->lockedInsetStoreUndo(Undo::EDIT);
309                                 MathArray ar;
310                                 mathcursor->selGet(ar);
311                                 mathcursor->setPos(x + xo_, y + yo_);
312                                 string sel =
313                                         bv->getLyXText()->selectionAsString(bv->buffer(), false);
314                                 mathed_parse_cell(ar, sel);
315                                 mathcursor->insert(ar);
316                         }
317                         break;
318 */
319                 case 3:
320                         // launch math panel for right mouse button
321                         bv->owner()->getDialogs()->showMathPanel();
322                         break;
323         }
324         bv->updateInset(this, false);
325 }
326
327
328 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
329                                          int x, int y, int /*button*/)
330 {
331         if (!mathcursor)
332                 return;
333
334         if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
335                 //lyxerr << "insetMotionNotify: ignored\n";
336                 return;
337         }
338         first_x = x;
339         first_y = y;
340
341         if (!mathcursor->selection())
342                 mathcursor->selStart();
343
344         //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
345         //      << ' ' << button << "\n";
346         hideInsetCursor(bv);
347         mathcursor->setPos(x + xo_, y + yo_);
348         showInsetCursor(bv);
349         bv->updateInset(this, false);
350 }
351
352
353 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
354 {
355         lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
356 }
357
358
359 UpdatableInset::RESULT
360 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
361                             string const & arg)
362 {
363         //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
364         //      << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
365
366         if (!mathcursor)
367                 return UNDISPATCHED;
368
369         if (mathcursor->asHyperActiveInset()) {
370                 lyxerr << " uurr.... getting dificult now\n";
371                 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
372         }
373
374         RESULT result      = DISPATCHED;
375         bool sel           = false;
376         bool was_macro     = mathcursor->inMacroMode();
377         bool was_selection = mathcursor->selection();
378
379         hideInsetCursor(bv);
380
381         mathcursor->normalize();
382         mathcursor->touch();
383
384         switch (action) {
385
386                 // --- Cursor Movements ---------------------------------------------
387
388         case LFUN_RIGHTSEL:
389                 sel = true; // fall through...
390
391         case LFUN_RIGHT:
392                 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
393                 //lyxerr << "calling scroll 20\n";
394                 //scroll(bv, 20);
395                 updateLocal(bv, false);
396                 // write something to the minibuffer
397                 //bv->owner()->message(mathcursor->info());
398                 break;
399
400
401         case LFUN_LEFTSEL:
402                 sel = true; // fall through
403
404         case LFUN_LEFT:
405                 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
406                 updateLocal(bv, false);
407                 break;
408
409
410         case LFUN_UPSEL:
411                 sel = true;
412
413         case LFUN_UP:
414                 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
415                 updateLocal(bv, false);
416                 break;
417
418
419         case LFUN_DOWNSEL:
420                 sel = true;
421
422         case LFUN_DOWN:
423                 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
424                 updateLocal(bv, false);
425                 break;
426
427         case LFUN_HOMESEL:
428                 sel = true;
429
430         case LFUN_HOME:
431                 mathcursor->home(sel);
432                 updateLocal(bv, false);
433                 break;
434
435         case LFUN_ENDSEL:
436                 sel = true;
437
438         case LFUN_END:
439                 mathcursor->end(sel);
440                 updateLocal(bv, false);
441                 break;
442
443         case LFUN_DELETE_LINE_FORWARD:
444                 bv->lockedInsetStoreUndo(Undo::DELETE);
445                 mathcursor->delLine();
446                 updateLocal(bv, true);
447                 break;
448
449         case LFUN_TAB:
450                 mathcursor->idxNext();
451                 updateLocal(bv, false);
452                 break;
453
454         case LFUN_SHIFT_TAB:
455                 mathcursor->idxPrev();
456                 updateLocal(bv, false);
457                 break;
458
459         case LFUN_TABINSERT:
460                 bv->lockedInsetStoreUndo(Undo::EDIT);
461                 mathcursor->splitCell();
462                 updateLocal(bv, true);
463                 break;
464
465         case LFUN_DELETE_WORD_BACKWARD:
466         case LFUN_BACKSPACE:
467                 bv->lockedInsetStoreUndo(Undo::DELETE);
468                 mathcursor->backspace();
469                 bv->updateInset(this, true);
470                 break;
471
472         case LFUN_DELETE_WORD_FORWARD:
473         case LFUN_DELETE:
474                 bv->lockedInsetStoreUndo(Undo::DELETE);
475                 mathcursor->erase();
476                 bv->updateInset(this, true);
477                 break;
478
479         //    case LFUN_GETXY:
480         //      sprintf(dispatch_buffer, "%d %d",);
481         //      dispatch_result = dispatch_buffer;
482         //      break;
483         case LFUN_SETXY: {
484                 lyxerr << "LFUN_SETXY broken!\n";
485                 int x = 0;
486                 int y = 0;
487                 istringstream is(arg.c_str());
488                 is >> x >> y;
489                 mathcursor->setPos(x, y);
490                 updateLocal(bv, false);
491                 break;
492         }
493
494         case LFUN_PASTE:
495                 if (was_macro)
496                         mathcursor->macroModeClose();
497                 bv->lockedInsetStoreUndo(Undo::EDIT);
498                 mathcursor->selPaste();
499                 updateLocal(bv, true);
500                 break;
501
502         case LFUN_CUT:
503                 bv->lockedInsetStoreUndo(Undo::DELETE);
504                 mathcursor->selCut();
505                 updateLocal(bv, true);
506                 break;
507
508         case LFUN_COPY:
509                 mathcursor->selCopy();
510                 break;
511
512         case LFUN_WORDRIGHTSEL:
513         case LFUN_WORDLEFTSEL:
514                 break;
515
516         // Special casing for superscript in case of LyX handling
517         // dead-keys:
518         case LFUN_CIRCUMFLEX:
519                 if (arg.empty()) {
520                         // do superscript if LyX handles
521                         // deadkeys
522                         bv->lockedInsetStoreUndo(Undo::EDIT);
523                         mathcursor->script(true);
524                         updateLocal(bv, true);
525                 }
526                 break;
527         case LFUN_UMLAUT:
528         case LFUN_ACUTE:
529         case LFUN_GRAVE:
530         case LFUN_BREVE:
531         case LFUN_DOT:
532         case LFUN_MACRON:
533         case LFUN_CARON:
534         case LFUN_TILDE:
535         case LFUN_CEDILLA:
536         case LFUN_CIRCLE:
537         case LFUN_UNDERDOT:
538         case LFUN_TIE:
539         case LFUN_OGONEK:
540         case LFUN_HUNG_UMLAUT:
541                 break;
542
543         //  Math fonts
544         case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
545         case LFUN_BOLD:         handleFont(bv, arg, LM_TC_BF); break;
546         case LFUN_SANS:         handleFont(bv, arg, LM_TC_SF); break;
547         case LFUN_EMPH:         handleFont(bv, arg, LM_TC_CAL); break;
548         case LFUN_ROMAN:        handleFont(bv, arg, LM_TC_RM); break;
549         case LFUN_CODE:         handleFont(bv, arg, LM_TC_TT); break;
550         case LFUN_FRAK:         handleFont(bv, arg, LM_TC_EUFRAK); break;
551         case LFUN_ITAL:         handleFont(bv, arg, LM_TC_IT); break;
552         case LFUN_NOUN:         handleFont(bv, arg, LM_TC_BB); break;
553         case LFUN_DEFAULT:      handleFont(bv, arg, LM_TC_VAR); break;
554         case LFUN_FREE:         handleFont(bv, arg, LM_TC_TEXTRM); break;
555
556         case LFUN_GREEK:
557                 handleFont(bv, arg, LM_TC_GREEK1);
558                 if (arg.size())
559                         mathcursor->interpret(arg);
560                 break;
561
562         case LFUN_MATH_MODE:
563 #if 1
564                 handleFont(bv, arg, LM_TC_TEXTRM);
565 #endif
566
567 #if 0
568                 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
569                 updateLocal(bv, true);
570 #endif
571
572                 //bv->owner()->message(_("math text mode toggled"));
573                 break;
574
575         case LFUN_MATH_LIMITS:
576                 bv->lockedInsetStoreUndo(Undo::EDIT);
577                 if (mathcursor->toggleLimits())
578                         updateLocal(bv, true);
579                 break;
580
581         case LFUN_MATH_SIZE:
582 #if 0
583                 if (!arg.empty()) {
584                         bv->lockedInsetStoreUndo(Undo::EDIT);
585                         mathcursor->setSize(arg);
586                         updateLocal(bv, true);
587                 }
588 #endif
589                 break;
590
591         case LFUN_INSERT_MATRIX:
592                 if (!arg.empty()) {
593                         bv->lockedInsetStoreUndo(Undo::EDIT);
594                         mathcursor->interpret("matrix " + arg);
595                         updateLocal(bv, true);
596                 }
597                 break;
598
599         case LFUN_MATH_SPACE:
600         {
601                 bv->lockedInsetStoreUndo(Undo::EDIT);
602                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
603                 updateLocal(bv, true);
604                 break;
605         }
606
607         case LFUN_SUPERSCRIPT:
608         case LFUN_SUBSCRIPT:
609         {
610                 bv->lockedInsetStoreUndo(Undo::EDIT);
611                 mathcursor->script((action == LFUN_SUPERSCRIPT));
612                 updateLocal(bv, true);
613                 break;
614         }
615
616         case LFUN_MATH_DELIM:
617         {
618                 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
619                 string ls;
620                 string rs;
621                 istringstream is(arg.c_str());
622                 is >> ls >> rs;
623                 if (!is) {
624                         lyxerr << "can't parse delimeters from '" << arg << "'\n";
625                         break;
626                 }
627                 bv->lockedInsetStoreUndo(Undo::EDIT);
628                 mathcursor->handleDelim(ls, rs);
629                 updateLocal(bv, true);
630                 break;
631         }
632
633         case LFUN_PROTECTEDSPACE:
634                 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
635                 bv->lockedInsetStoreUndo(Undo::EDIT);
636                 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
637                 updateLocal(bv, true);
638                 break;
639
640         case LFUN_UNDO:
641                 bv->owner()->message(_("Invalid action in math mode!"));
642                 break;
643
644
645         case LFUN_MATH_HALIGN:
646         case LFUN_MATH_VALIGN:
647         case LFUN_MATH_ROW_INSERT:
648         case LFUN_MATH_ROW_DELETE:
649         case LFUN_MATH_COLUMN_INSERT:
650         case LFUN_MATH_COLUMN_DELETE:
651         {
652                 MathInset::idx_type idx = 0;
653                 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
654                 if (p) {
655                         mathcursor->popToEnclosingGrid();
656                         bv->lockedInsetStoreUndo(Undo::EDIT);
657                         char align = arg.size() ? arg[0] : 'c';
658                         switch (action) {
659                                 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
660                                 case LFUN_MATH_VALIGN: p->valign(align); break;
661                                 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
662                                 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
663                                 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
664                                 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
665                                 default: ;
666                         }
667                         updateLocal(bv, true);
668                 }
669                 break;
670         }
671
672         case LFUN_EXEC_COMMAND:
673                 result = UNDISPATCHED;
674                 break;
675
676         case LFUN_BREAKPARAGRAPH:
677         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
678                 //lyxerr << "LFUN ignored\n";
679                 break;
680
681         case LFUN_INSET_ERT:
682                 // interpret this as if a backslash was typed
683                 bv->lockedInsetStoreUndo(Undo::EDIT);
684                 mathcursor->interpret('\\');
685                 updateLocal(bv, true);
686                 break;
687
688         case -1:
689         case LFUN_INSERT_MATH:
690         case LFUN_SELFINSERT:
691                 if (!arg.empty()) {
692                         bv->lockedInsetStoreUndo(Undo::EDIT);
693                         if (arg.size() == 1)
694                                 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
695                         else
696                                 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
697                         updateLocal(bv, true);
698                 }
699                 break;
700
701         case LFUN_MATH_PANEL:
702                 result = UNDISPATCHED;
703                 break;
704
705         case LFUN_ESCAPE:
706                 if (mathcursor->selection())
707                         mathcursor->selClear();
708                 else
709                         result = UNDISPATCHED;
710                 break;
711
712         default:
713                 result = UNDISPATCHED;
714         }
715
716         mathcursor->normalize();
717         mathcursor->touch();
718
719         lyx::Assert(mathcursor);
720
721         if (mathcursor->selection() || was_selection)
722                 toggleInsetSelection(bv);
723
724         if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
725             result == UNDISPATCHED)
726                 showInsetCursor(bv);
727         else
728                 bv->unlockInset(this);
729
730         revealCodes(bv);
731
732         return result;  // original version
733 }
734
735
736 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
737 {
738         if (!mathcursor)
739                 return;
740 #if 0
741         // write something to the minibuffer
742         // translate to latex
743         mathcursor->markInsert();
744         ostringstream os;
745         write(NULL, os);
746         string str = os.str();
747         mathcursor->markErase();
748         string::size_type pos = 0;
749         string res;
750         for (string::iterator it = str.begin(); it != str.end(); ++it) {
751                 if (*it == '\n')
752                         res += ' ';
753                 else if (*it == '\0') {
754                         res += "  -X-  ";
755                         pos = it - str.begin();
756                 }
757                 else
758                         res += *it;
759         }
760         if (pos > 30)
761                 res = res.substr(pos - 30);
762         if (res.size() > 60)
763                 res = res.substr(0, 60);
764         bv->owner()->message(res);
765 #endif
766 }
767
768
769 Inset::Code InsetFormulaBase::lyxCode() const
770 {
771         return Inset::MATH_CODE;
772 }
773
774
775 int InsetFormulaBase::ylow() const
776 {
777         return yo_ - ascent(view_, font_);
778 }
779
780
781 int InsetFormulaBase::yhigh() const
782 {
783         return yo_ + descent(view_, font_);
784 }
785
786
787 int InsetFormulaBase::xlow() const
788 {
789         return xo_;
790 }
791
792
793 int InsetFormulaBase::xhigh() const
794 {
795         return xo_ + width(view_, font_);
796 }
797
798
799 /////////////////////////////////////////////////////////////////////
800
801
802 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
803                                      bool, bool)
804 {
805 #ifdef WITH_WARNINGS
806 #warning pretty ugly
807 #endif
808         static InsetFormulaBase * lastformula = 0;
809         static MathIterator current = MathIterator(ibegin(par().nucleus()));
810         static MathArray ar;
811         static string laststr;
812
813         if (lastformula != this || laststr != str) {
814                 //lyxerr << "reset lastformula to " << this << "\n";
815                 lastformula = this;
816                 laststr = str;
817                 current = ibegin(par().nucleus());
818                 ar.clear();
819                 mathed_parse_cell(ar, str);
820         } else {
821                 ++current;
822         }
823         //lyxerr << "searching '" << str << "' in " << this << ar << endl;
824
825         for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
826                 if (it.cell().matchpart(ar, it.position().pos_)) {
827                         mathcursor->setSelection(it.cursor(), ar.size());
828                         current = it;
829                         it.jump(ar.size());
830                         // I guess some of the following can go
831                         bv->toggleSelection(true);
832                         hideInsetCursor(bv);
833                         updateLocal(bv, true);
834                         showInsetCursor(bv);
835                         metrics(bv);
836                         return true;
837                 }
838         }
839
840         //lyxerr << "not found!\n";
841         lastformula = 0;
842         // we have to unlock ourself in this function by default!
843         // don't ask me why...
844         bv->unlockInset(this);
845         return false;
846 }
847
848
849 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
850                                       bool a, bool b)
851 {
852         lyxerr << "searching backward not implemented in mathed" << endl;
853         return searchForward(bv, what, a, b);
854 }
855
856
857 /////////////////////////////////////////////////////////////////////
858
859
860 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
861 {
862         if (bv->available()) {
863                 // use selection if available..
864                 //string sel;
865                 //if (action == LFUN_MATH_IMPORT_SELECTION)
866                 //      sel = "";
867                 //else
868
869                 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
870
871                 InsetFormulaBase * f;
872                 if (sel.empty()) {
873                         f = new InsetFormula;
874                         if (openNewInset(bv, f)) {
875                                 // don't do that also for LFUN_MATH_MODE unless you want end up with
876                                 // always changing to mathrm when opening an inlined inset
877                                 // -- I really hate "LyXfunc overloading"...
878                                 if (display)
879                                         f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
880                                 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
881                         }
882                 } else {
883                         // create a macro if we see "\\newcommand" somewhere, and an ordinary
884                         // formula otherwise
885                         if (sel.find("\\newcommand") == string::npos &&
886                                   sel.find("\\def") == string::npos)
887                                 f = new InsetFormula(sel);
888                         else {
889                                 string name;
890                                 if (!mathed_parse_macro(name, sel))
891                                         return;
892                                 f = new InsetFormulaMacro(sel);
893                         }
894                         bv->getLyXText()->cutSelection(bv);
895                         openNewInset(bv, f);
896                 }
897         }
898         bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
899 }
900
901
902 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
903 {
904         mathDispatchCreation(bv, arg, true);
905 }
906
907
908 void mathDispatchMathMode(BufferView * bv, string const & arg)
909 {
910         mathDispatchCreation(bv, arg, false);
911 }
912
913
914 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
915 {
916         mathDispatchCreation(bv, arg, true);
917 }
918
919
920 void mathDispatchMathMacro(BufferView * bv, string const & arg)
921 {
922         if (bv->available()) {
923                 if (arg.empty())
924                         bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
925                 else {
926                         string s = arg;
927                         string const s1 = token(s, ' ', 1);
928                         int const na = s1.empty() ? 0 : lyx::atoi(s1);
929                         openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
930                 }
931         }
932 }
933
934
935 void mathDispatchMathDelim(BufferView * bv, string const & arg)
936 {
937         if (bv->available()) {
938                 if (openNewInset(bv, new InsetFormula))
939                         bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
940         }
941 }
942
943
944 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
945 {
946         if (bv->available()) {
947                 if (openNewInset(bv, new InsetFormula))
948                         bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
949         }
950 }
951
952
953 void mathDispatchInsertMath(BufferView * bv, string const & arg)
954 {
955         if (bv->available()) {
956                 if (arg.size() && arg[0] == '\\') {
957                         InsetFormula * f = new InsetFormula(arg);
958                         if (!bv->insertInset(f))
959                                 delete f;
960                 } else
961                         mathDispatchMathMode(bv, arg);
962         }
963 }
964
965
966 void mathDispatchGreek(BufferView * bv, string const & arg)
967 {
968         if (bv->available()) {
969                 InsetFormula * f = new InsetFormula;
970                 if (openNewInset(bv, f)) {
971                         bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
972                         bv->unlockInset(f);
973                 }
974         }
975 }
976
977
978 void mathDispatch(BufferView *, kb_action, string const &)
979 {}