3 * Purpose: Implementation of common parts of the LyX math insets
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
7 * Copyright: 1996-1998 Alejandro Aguilar Sierra
9 * Version: 0.4, Lyx project.
11 * You are free to use and modify this code under the terms of
12 * the GNU General Public Licence version 2 or later.
19 #include "support/LAssert.h"
22 #pragma implementation
26 #include "formulamacro.h"
28 #include "funcrequest.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "math_support.h"
36 #include "math_metricsinfo.h"
37 #include "support/lstrings.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/mouse_state.h"
42 #include "math_arrayinset.h"
43 #include "math_charinset.h"
44 #include "math_deliminset.h"
45 #include "math_cursor.h"
46 #include "math_factory.h"
47 #include "math_fontinset.h"
48 #include "math_hullinset.h"
49 #include "math_iterator.h"
50 #include "math_macrotable.h"
51 #include "math_parser.h"
53 #include "math_spaceinset.h"
54 #include "undo_funcs.h"
55 #include "textpainter.h"
56 #include "frontends/Dialogs.h"
58 #include "insets/insetcommandparams.h"
59 #include "ref_inset.h"
66 MathCursor * mathcursor = 0;
77 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
79 if (!bv->insertInset(new_inset)) {
83 new_inset->edit(bv, true);
92 InsetFormulaBase::InsetFormulaBase()
93 : font_(), xo_(0), yo_(0)
95 // This is needed as long the math parser is not re-entrant
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";
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
108 // translate to latex
110 latex(NULL, os, false, false);
111 string str = os.str();
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);
121 //view_->owner()->dispatch(LFUN_ESCAPE);
126 void InsetFormulaBase::handleFont
127 (BufferView * bv, string const & arg, string const & font)
129 bv->lockedInsetStoreUndo(Undo::EDIT);
130 bool sel = mathcursor->selection();
132 updateLocal(bv, true);
133 mathcursor->handleNest(createMathInset(font));
134 mathcursor->insert(arg);
136 updateLocal(bv, false);
140 // Check if uses AMS macros
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
145 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
152 void InsetFormulaBase::metrics(BufferView * bv) const
155 view_ = bv->owner()->view();
157 mi.base.style = LM_ST_TEXT;
158 mi.base.font = font_;
159 mi.base.font.setColor(LColor::math);
164 string const InsetFormulaBase::editMessage() const
166 return _("Math editor mode");
170 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
172 if (!bv->lockInset(this))
173 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174 releaseMathCursor(bv);
175 mathcursor = new MathCursor(this, true);
177 mathcursor->setPos(x, y);
178 //lyxerr << "setting pos to " << x << "," << y << "\n";
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);
186 void InsetFormulaBase::edit(BufferView * bv, bool front)
188 if (!bv->lockInset(this))
189 lyxerr << "Cannot lock math inset in edit call!\n";
190 releaseMathCursor(bv);
191 mathcursor = new MathCursor(this, front);
193 bv->updateInset(this, false);
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
200 if (mathcursor->inMacroMode()) {
201 mathcursor->macroModeClose();
202 updateLocal(bv, true);
204 releaseMathCursor(bv);
207 bv->updateInset(this, false);
211 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
213 // calling metrics here destroys the cached xo,yo positions e.g. in
214 // MathParboxinset. And it would be too expensive anyway...
216 mathcursor->getPos(x, y);
219 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
226 if (isCursorVisible())
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
235 if (isCursorVisible())
239 getCursorPos(bv, x, y);
240 math_font_max_dim(font_, asc, des);
241 bv->showLockedInsetCursor(x, y, asc, des);
242 setCursorVisible(true);
243 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
247 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
249 if (!isCursorVisible())
251 bv->hideLockedInsetCursor();
252 setCursorVisible(false);
253 //lyxerr << "hideInsetCursor: \n";
257 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
262 math_font_max_dim(font_, asc, des);
263 getCursorPos(bv, x, y);
265 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
266 bv->fitLockedInsetCursor(x, y, asc, des);
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
273 bv->updateInset(this, false);
277 vector<string> const InsetFormulaBase::getLabelList() const
279 return vector<string>();
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
285 bv->updateInset(this, dirty);
289 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
290 int x, int y, mouse_button::state button)
295 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
298 bv->updateInset(this, false);
300 if (button == mouse_button::button3) {
301 // try to dispatch to enclosed insets first
302 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 3)))
305 // launch math panel for right mouse button
306 bv->owner()->getDialogs().showMathPanel();
310 if (button == mouse_button::button1) {
311 // try to dispatch to enclosed insets first
312 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 1)))
315 // try to set the cursor
317 //mathcursor = new MathCursor(this, x == 0);
319 //mathcursor->setPos(x + xo_, y + yo_);
326 void InsetFormulaBase::insetButtonPress(BufferView * bv,
327 int x, int y, mouse_button::state button)
329 //lyxerr << "insetButtonPress: "
330 // << x << " " << y << " but: " << button << "\n";
331 //lyxerr << "formula: ";
334 releaseMathCursor(bv);
335 mathcursor = new MathCursor(this, x == 0);
337 if (button == mouse_button::button1) {
338 // just set the cursor here
339 //lyxerr << "setting cursor\n";
343 mathcursor->selClear();
344 mathcursor->setPos(x + xo_, y + yo_);
346 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 1))) {
352 if (button == mouse_button::button3) {
353 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 3))) {
358 bv->updateInset(this, false);
362 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
363 int x, int y, mouse_button::state button)
368 if (button == mouse_button::button1)
369 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 1)))
372 if (button == mouse_button::button3)
373 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 3)))
376 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
377 //lyxerr << "insetMotionNotify: ignored\n";
383 if (!mathcursor->selection())
384 mathcursor->selStart();
386 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
387 // << ' ' << button << "\n";
389 mathcursor->setPos(x + xo_, y + yo_);
391 bv->updateInset(this, false);
395 UpdatableInset::RESULT
396 InsetFormulaBase::localDispatch(FuncRequest const & ev)
398 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
399 // << " arg: '" << arg
400 // << "' cursor: " << mathcursor << "\n";
405 BufferView * bv = ev.view();
406 string argument = ev.argument;
407 RESULT result = DISPATCHED;
409 bool was_macro = mathcursor->inMacroMode();
410 bool was_selection = mathcursor->selection();
414 mathcursor->normalize();
419 case LFUN_MATH_HALIGN:
420 case LFUN_MATH_VALIGN:
421 case LFUN_MATH_ROW_INSERT:
422 case LFUN_MATH_ROW_DELETE:
423 case LFUN_MATH_COLUMN_INSERT:
424 case LFUN_MATH_COLUMN_DELETE:
425 case LFUN_MATH_NUMBER:
426 case LFUN_MATH_NONUMBER:
429 case LFUN_DELETE_LINE_FORWARD:
430 case LFUN_INSERT_LABEL:
431 bv->lockedInsetStoreUndo(Undo::EDIT);
432 mathcursor->dispatch(ev);
433 updateLocal(bv, true);
436 case LFUN_WORDRIGHTSEL:
438 sel = true; // fall through...
440 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
441 //lyxerr << "calling scroll 20\n";
443 updateLocal(bv, false);
444 // write something to the minibuffer
445 //bv->owner()->message(mathcursor->info());
448 case LFUN_WORDLEFTSEL:
450 sel = true; // fall through
452 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
453 updateLocal(bv, false);
457 sel = true; // fall through
459 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
460 updateLocal(bv, false);
464 sel = true; // fall through
466 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
467 updateLocal(bv, false);
471 sel = true; // fall through
473 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
474 updateLocal(bv, false);
478 sel = true; // fall through
480 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
481 updateLocal(bv, false);
486 result = FINISHED_UP;
487 updateLocal(bv, false);
492 result = FINISHED_DOWN;
493 updateLocal(bv, false);
497 mathcursor->idxNext();
498 updateLocal(bv, false);
502 mathcursor->idxPrev();
503 updateLocal(bv, false);
506 case LFUN_DELETE_WORD_BACKWARD:
508 bv->lockedInsetStoreUndo(Undo::DELETE);
509 mathcursor->backspace();
510 updateLocal(bv, true);
513 case LFUN_DELETE_WORD_FORWARD:
515 bv->lockedInsetStoreUndo(Undo::DELETE);
517 bv->updateInset(this, true);
521 // sprintf(dispatch_buffer, "%d %d",);
522 // dispatch_result = dispatch_buffer;
525 lyxerr << "LFUN_SETXY broken!\n";
528 istringstream is(ev.argument.c_str());
530 mathcursor->setPos(x, y);
531 updateLocal(bv, false);
537 mathcursor->macroModeClose();
538 bv->lockedInsetStoreUndo(Undo::EDIT);
539 mathcursor->selPaste();
540 updateLocal(bv, true);
544 bv->lockedInsetStoreUndo(Undo::DELETE);
545 mathcursor->selCut();
546 updateLocal(bv, true);
550 mathcursor->selCopy();
554 // Special casing for superscript in case of LyX handling
556 case LFUN_CIRCUMFLEX:
557 if (ev.argument.empty()) {
558 // do superscript if LyX handles
560 bv->lockedInsetStoreUndo(Undo::EDIT);
561 mathcursor->script(true);
562 updateLocal(bv, true);
579 case LFUN_HUNG_UMLAUT:
583 case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
584 case LFUN_BOLD: handleFont(bv, ev.argument, "textbf"); break;
585 case LFUN_SANS: handleFont(bv, ev.argument, "textsf"); break;
586 case LFUN_EMPH: handleFont(bv, ev.argument, "mathcal"); break;
587 case LFUN_ROMAN: handleFont(bv, ev.argument, "mathrm"); break;
588 case LFUN_CODE: handleFont(bv, ev.argument, "texttt"); break;
589 case LFUN_FRAK: handleFont(bv, ev.argument, "mathfrak"); break;
590 case LFUN_ITAL: handleFont(bv, ev.argument, "mathit"); break;
591 case LFUN_NOUN: handleFont(bv, ev.argument, "mathbb"); break;
592 case LFUN_DEFAULT: handleFont(bv, ev.argument, "textnormal"); break;
593 case LFUN_FREE: handleFont(bv, ev.argument, "textrm"); break;
596 handleFont(bv, ev.argument, "lyxgreek1");
597 if (ev.argument.size())
598 mathcursor->interpret(ev.argument);
602 if (mathcursor->currentMode()) {
603 handleFont(bv, ev.argument, "textrm");
605 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
606 updateLocal(bv, true);
608 //bv->owner()->message(_("math text mode toggled"));
611 case LFUN_MATH_LIMITS:
612 bv->lockedInsetStoreUndo(Undo::EDIT);
613 if (mathcursor->toggleLimits())
614 updateLocal(bv, true);
620 bv->lockedInsetStoreUndo(Undo::EDIT);
621 mathcursor->setSize(arg);
622 updateLocal(bv, true);
627 case LFUN_INSERT_MATRIX:
628 if (!ev.argument.empty()) {
629 bv->lockedInsetStoreUndo(Undo::EDIT);
630 mathcursor->interpret("matrix " + ev.argument);
631 updateLocal(bv, true);
635 case LFUN_SUPERSCRIPT:
638 bv->lockedInsetStoreUndo(Undo::EDIT);
639 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
640 updateLocal(bv, true);
644 case LFUN_MATH_DELIM:
646 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
648 string rs = split(ev.argument, ls, ' ');
649 // Reasonable default values
655 bv->lockedInsetStoreUndo(Undo::EDIT);
656 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
657 updateLocal(bv, true);
661 case LFUN_PROTECTEDSPACE:
662 case LFUN_MATH_SPACE:
663 bv->lockedInsetStoreUndo(Undo::EDIT);
664 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
665 updateLocal(bv, true);
669 bv->owner()->message(_("Invalid action in math mode!"));
673 case LFUN_EXEC_COMMAND:
674 result = UNDISPATCHED;
678 // interpret this as if a backslash was typed
679 bv->lockedInsetStoreUndo(Undo::EDIT);
680 mathcursor->interpret('\\');
681 updateLocal(bv, true);
684 case LFUN_BREAKPARAGRAPH:
685 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
686 case LFUN_BREAKPARAGRAPH_SKIP:
691 case LFUN_INSERT_MATH:
692 case LFUN_SELFINSERT:
693 if (!argument.empty()) {
694 bv->lockedInsetStoreUndo(Undo::EDIT);
695 if (argument.size() == 1)
696 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
698 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
699 updateLocal(bv, true);
703 case LFUN_MATH_PANEL:
704 result = UNDISPATCHED;
708 if (mathcursor->selection())
709 mathcursor->selClear();
711 result = UNDISPATCHED;
714 case LFUN_INSET_TOGGLE:
715 mathcursor->insetToggle();
716 updateLocal(bv, true);
719 case LFUN_REF_INSERT:
720 //if (argument.empty()) {
721 // InsetCommandParams p("ref");
722 // owner_->getDialogs().createRef(p.getAsString());
724 // InsetCommandParams p;
725 // p.setFromString(argument);
727 // InsetRef * inset = new InsetRef(p, *buffer_);
728 // if (!insertInset(inset))
731 // updateInset(inset, true);
734 if (ev.argument.empty()) {
735 InsetCommandParams p("ref");
736 bv->owner()->getDialogs().createRef(p.getAsString());
738 //mathcursor->handleNest(new InsetRef2);
739 //mathcursor->insert(arg);
740 mathcursor->insert(MathAtom(new RefInset(ev.argument)));
742 updateLocal(bv, true);
746 result = UNDISPATCHED;
749 mathcursor->normalize();
752 lyx::Assert(mathcursor);
754 if (mathcursor->selection() || was_selection)
755 toggleInsetSelection(bv);
757 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
758 result == UNDISPATCHED) {
763 releaseMathCursor(bv);
764 bv->unlockInset(this);
767 return result; // original version
771 void InsetFormulaBase::revealCodes(BufferView * bv) const
775 bv->owner()->message(mathcursor->info());
778 // write something to the minibuffer
779 // translate to latex
780 mathcursor->markInsert();
783 string str = os.str();
784 mathcursor->markErase();
785 string::size_type pos = 0;
787 for (string::iterator it = str.begin(); it != str.end(); ++it) {
790 else if (*it == '\0') {
792 pos = it - str.begin();
798 res = res.substr(pos - 30);
800 res = res.substr(0, 60);
801 bv->owner()->message(res);
806 Inset::Code InsetFormulaBase::lyxCode() const
808 return Inset::MATH_CODE;
812 int InsetFormulaBase::ylow() const
814 return yo_ - ascent(view(), font_);
818 int InsetFormulaBase::yhigh() const
820 return yo_ + descent(view(), font_);
824 int InsetFormulaBase::xlow() const
830 int InsetFormulaBase::xhigh() const
832 return xo_ + width(view(), font_);
836 /////////////////////////////////////////////////////////////////////
839 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
845 static InsetFormulaBase * lastformula = 0;
846 static MathIterator current = MathIterator(ibegin(par().nucleus()));
848 static string laststr;
850 if (lastformula != this || laststr != str) {
851 //lyxerr << "reset lastformula to " << this << "\n";
854 current = ibegin(par().nucleus());
856 mathed_parse_cell(ar, str);
860 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
862 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
863 if (it.cell().matchpart(ar, it.back().pos_)) {
864 bv->unlockInset(bv->theLockingInset());
865 if (!bv->lockInset(this)) {
866 lyxerr << "Cannot lock inset" << endl;
870 mathcursor = new MathCursor(this, true);
872 mathcursor->setSelection(it, ar.size());
875 updateLocal(bv, false);
880 //lyxerr << "not found!\n";
886 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
889 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
890 return searchForward(bv, what, a, b);
894 /////////////////////////////////////////////////////////////////////
897 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
899 if (!bv->available())
902 // use selection if available..
904 //if (action == LFUN_MATH_IMPORT_SELECTION)
908 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
911 InsetFormula * f = new InsetFormula(bv);
912 if (openNewInset(bv, f)) {
914 // don't do that also for LFUN_MATH_MODE unless you want end up with
915 // always changing to mathrm when opening an inlined inset
916 // -- I really hate "LyXfunc overloading"...
918 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
919 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
922 // create a macro if we see "\\newcommand" somewhere, and an ordinary
924 InsetFormulaBase * f;
925 if (sel.find("\\newcommand") == string::npos &&
926 sel.find("\\def") == string::npos)
927 f = new InsetFormula(sel);
929 f = new InsetFormulaMacro(sel);
930 bv->getLyXText()->cutSelection(bv);
933 bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
937 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
939 mathDispatchCreation(bv, arg, true);
943 void mathDispatchMathMode(BufferView * bv, string const & arg)
945 mathDispatchCreation(bv, arg, false);
949 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
951 mathDispatchCreation(bv, arg, true);
955 void mathDispatchMathMacro(BufferView * bv, string const & arg)
957 if (!bv->available())
960 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
963 string const s1 = token(s, ' ', 1);
964 int const na = s1.empty() ? 0 : lyx::atoi(s1);
965 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
970 void mathDispatchMathDelim(BufferView * bv, string const & arg)
972 if (!bv->available())
974 InsetFormula * f = new InsetFormula(bv);
975 if (openNewInset(bv, f)) {
977 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_MATH_DELIM, arg));
982 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
984 if (!bv->available())
986 InsetFormula * f = new InsetFormula(bv);
987 if (openNewInset(bv, f)) {
989 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATRIX, arg));
994 void mathDispatchInsertMath(BufferView * bv, string const & arg)
996 if (!bv->available())
998 InsetFormula * f = new InsetFormula(bv);
999 if (openNewInset(bv, f)) {
1000 f->mutate("simple");
1001 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
1006 void mathDispatchGreek(BufferView * bv, string const & arg)
1008 if (!bv->available())
1010 InsetFormula * f = new InsetFormula(bv);
1011 if (openNewInset(bv, f)) {
1012 f->mutate("simple");
1013 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_GREEK, arg));
1019 void mathDispatch(BufferView *, kb_action, string const &)
1023 void mathDispatch(BufferView *, string const &)