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