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