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 "commandtags.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.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"
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"
51 #include "math_spaceinset.h"
52 #include "undo_funcs.h"
53 #include "textpainter.h"
54 #include "frontends/Dialogs.h"
56 #include "insets/insetcommandparams.h"
58 #include "ref_inset.h"
65 MathCursor * mathcursor = 0;
76 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
78 if (!bv->insertInset(new_inset)) {
82 new_inset->edit(bv, true);
91 InsetFormulaBase::InsetFormulaBase()
92 : view_(0), font_(), xo_(0), yo_(0)
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";
103 // simply scrap this function if you want
104 void InsetFormulaBase::mutateToText()
107 // translate to latex
109 latex(NULL, os, false, false);
110 string str = os.str();
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);
120 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
125 void InsetFormulaBase::handleFont
126 (BufferView * bv, string const & arg, string const & font)
128 bv->lockedInsetStoreUndo(Undo::EDIT);
129 bool sel = mathcursor->selection();
131 updateLocal(bv, true);
132 mathcursor->handleNest(new MathFontInset(font));
133 mathcursor->insert(arg);
135 updateLocal(bv, false);
139 // Check if uses AMS macros
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
144 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
151 void InsetFormulaBase::metrics(BufferView * bv) const
157 //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
158 mi.base.style = LM_ST_TEXT;
159 mi.base.font = font_;
160 mi.base.font.setColor(LColor::math);
165 string const InsetFormulaBase::editMessage() const
167 return _("Math editor mode");
171 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
173 if (!bv->lockInset(this))
174 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
176 mathcursor = new MathCursor(this, true);
178 mathcursor->setPos(x, y);
179 //lyxerr << "setting pos to " << x << "," << y << "\n";
181 // if that is removed, we won't get the magenta box when entering an
182 // inset for the first time
183 bv->updateInset(this, false);
187 void InsetFormulaBase::edit(BufferView * bv, bool front)
189 if (!bv->lockInset(this))
190 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
192 mathcursor = new MathCursor(this, front);
194 bv->updateInset(this, false);
198 void InsetFormulaBase::insetUnlock(BufferView * bv)
201 if (mathcursor->inMacroMode()) {
202 mathcursor->macroModeClose();
203 updateLocal(bv, true);
208 bv->updateInset(this, false);
212 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
215 mathcursor->getPos(x, y);
218 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
222 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
227 if (isCursorVisible())
228 bv->hideLockedInsetCursor();
233 mathcursor->getPos(x, y);
237 math_font_max_dim(font_, asc, des);
238 bv->showLockedInsetCursor(x, y, asc, des);
239 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
242 toggleCursorVisible();
246 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
248 if (isCursorVisible())
254 mathcursor->getPos(x, y);
258 math_font_max_dim(font_, asc, des);
259 bv->fitLockedInsetCursor(x, y, asc, des);
260 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
262 toggleInsetCursor(bv);
266 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
268 if (isCursorVisible())
269 toggleInsetCursor(bv);
273 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
278 int const asc = font_metrics::maxAscent(font_);
279 int const desc = font_metrics::maxDescent(font_);
282 getCursorPos(bv, x, y);
283 bv->fitLockedInsetCursor(x, y, asc, desc);
287 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
290 bv->updateInset(this, false);
294 vector<string> const InsetFormulaBase::getLabelList() const
296 return vector<string>();
300 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
305 bv->updateInset(this, dirty);
309 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
310 int /*x*/, int /*y*/, mouse_button::state button)
312 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
318 bv->updateInset(this, false);
320 if (button == mouse_button::button3) {
321 // try to dispatch to enclosed insets first
322 if (mathcursor->dispatch("mouse 3"))
325 // launch math panel for right mouse button
326 bv->owner()->getDialogs()->showMathPanel();
333 void InsetFormulaBase::insetButtonPress(BufferView * bv,
334 int x, int y, mouse_button::state button)
336 //lyxerr << "insetButtonPress: "
337 // << x << " " << y << " but: " << button << "\n";
344 mathcursor = new MathCursor(this, x == 0);
348 mathcursor->selClear();
349 mathcursor->setPos(x + xo_, y + yo_);
353 lyxerr << "insetButtonPress: 2\n";
356 bv->lockedInsetStoreUndo(Undo::EDIT);
358 mathcursor->selGet(ar);
359 mathcursor->setPos(x + xo_, y + yo_);
361 bv->getLyXText()->selectionAsString(bv->buffer(), false);
362 mathed_parse_cell(ar, sel);
363 mathcursor->insert(ar);
368 // launch math panel for right mouse button
369 bv->owner()->getDialogs()->showMathPanel();
373 if (button == mouse_button::button1 || !mathcursor) {
375 mathcursor = new MathCursor(this, x == 0);
379 mathcursor->selClear();
380 mathcursor->setPos(x + xo_, y + yo_);
383 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
385 // launch math panel for right mouse button
386 bv->owner()->getDialogs()->showMathPanel();
390 bv->updateInset(this, false);
394 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
395 int x, int y, mouse_button::state)
400 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
401 //lyxerr << "insetMotionNotify: ignored\n";
407 if (!mathcursor->selection())
408 mathcursor->selStart();
410 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
411 // << ' ' << button << "\n";
413 mathcursor->setPos(x + xo_, y + yo_);
415 bv->updateInset(this, false);
419 UpdatableInset::RESULT
420 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
423 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
424 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
429 RESULT result = DISPATCHED;
431 bool was_macro = mathcursor->inMacroMode();
432 bool was_selection = mathcursor->selection();
436 mathcursor->normalize();
441 // --- Cursor Movements ---------------------------------------------
444 sel = true; // fall through...
447 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
448 //lyxerr << "calling scroll 20\n";
450 updateLocal(bv, false);
451 // write something to the minibuffer
452 //bv->owner()->message(mathcursor->info());
457 sel = true; // fall through
460 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
461 updateLocal(bv, false);
469 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
470 updateLocal(bv, false);
478 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
479 updateLocal(bv, false);
486 mathcursor->home(sel);
487 updateLocal(bv, false);
494 mathcursor->end(sel);
495 updateLocal(bv, false);
498 case LFUN_DELETE_LINE_FORWARD:
499 bv->lockedInsetStoreUndo(Undo::DELETE);
500 mathcursor->delLine();
501 updateLocal(bv, true);
505 mathcursor->idxNext();
506 updateLocal(bv, false);
510 mathcursor->idxPrev();
511 updateLocal(bv, false);
515 bv->lockedInsetStoreUndo(Undo::EDIT);
516 mathcursor->splitCell();
517 updateLocal(bv, true);
520 case LFUN_DELETE_WORD_BACKWARD:
522 bv->lockedInsetStoreUndo(Undo::DELETE);
523 mathcursor->backspace();
524 updateLocal(bv, true);
527 case LFUN_DELETE_WORD_FORWARD:
529 bv->lockedInsetStoreUndo(Undo::DELETE);
531 bv->updateInset(this, true);
535 // sprintf(dispatch_buffer, "%d %d",);
536 // dispatch_result = dispatch_buffer;
539 lyxerr << "LFUN_SETXY broken!\n";
542 istringstream is(arg.c_str());
544 mathcursor->setPos(x, y);
545 updateLocal(bv, false);
551 mathcursor->macroModeClose();
552 bv->lockedInsetStoreUndo(Undo::EDIT);
553 mathcursor->selPaste();
554 updateLocal(bv, true);
558 bv->lockedInsetStoreUndo(Undo::DELETE);
559 mathcursor->selCut();
560 updateLocal(bv, true);
564 mathcursor->selCopy();
567 case LFUN_WORDRIGHTSEL:
568 case LFUN_WORDLEFTSEL:
571 // Special casing for superscript in case of LyX handling
573 case LFUN_CIRCUMFLEX:
575 // do superscript if LyX handles
577 bv->lockedInsetStoreUndo(Undo::EDIT);
578 mathcursor->script(true);
579 updateLocal(bv, true);
595 case LFUN_HUNG_UMLAUT:
599 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
600 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
601 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
602 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
603 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
604 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
605 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
606 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
607 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
608 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
609 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
612 handleFont(bv, arg, "lyxgreek1");
614 mathcursor->interpret(arg);
618 if (mathcursor->inMathMode()) {
619 handleFont(bv, arg, "textrm");
621 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
622 updateLocal(bv, true);
624 //bv->owner()->message(_("math text mode toggled"));
627 case LFUN_MATH_LIMITS:
628 bv->lockedInsetStoreUndo(Undo::EDIT);
629 if (mathcursor->toggleLimits())
630 updateLocal(bv, true);
636 bv->lockedInsetStoreUndo(Undo::EDIT);
637 mathcursor->setSize(arg);
638 updateLocal(bv, true);
643 case LFUN_INSERT_MATRIX:
645 bv->lockedInsetStoreUndo(Undo::EDIT);
646 mathcursor->interpret("matrix " + arg);
647 updateLocal(bv, true);
651 case LFUN_SUPERSCRIPT:
654 bv->lockedInsetStoreUndo(Undo::EDIT);
655 mathcursor->script(action == LFUN_SUPERSCRIPT);
656 updateLocal(bv, true);
660 case LFUN_MATH_DELIM:
662 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
664 string rs = split(arg, ls, ' ');
665 // Reasonable default values
671 bv->lockedInsetStoreUndo(Undo::EDIT);
672 mathcursor->handleDelim(ls, rs);
673 updateLocal(bv, true);
677 case LFUN_PROTECTEDSPACE:
678 case LFUN_MATH_SPACE:
679 bv->lockedInsetStoreUndo(Undo::EDIT);
680 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
681 updateLocal(bv, true);
685 bv->owner()->message(_("Invalid action in math mode!"));
689 case LFUN_MATH_HALIGN:
690 case LFUN_MATH_VALIGN:
691 case LFUN_MATH_ROW_INSERT:
692 case LFUN_MATH_ROW_DELETE:
693 case LFUN_MATH_COLUMN_INSERT:
694 case LFUN_MATH_COLUMN_DELETE:
696 MathInset::idx_type idx = 0;
697 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
699 mathcursor->popToEnclosingGrid();
700 bv->lockedInsetStoreUndo(Undo::EDIT);
701 char align = arg.size() ? arg[0] : 'c';
703 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
704 case LFUN_MATH_VALIGN: p->valign(align); break;
705 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
706 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
707 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
708 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
711 updateLocal(bv, true);
716 case LFUN_EXEC_COMMAND:
717 result = UNDISPATCHED;
720 case LFUN_BREAKPARAGRAPH:
721 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
722 //lyxerr << "LFUN ignored\n";
726 // interpret this as if a backslash was typed
727 bv->lockedInsetStoreUndo(Undo::EDIT);
728 mathcursor->interpret('\\');
729 updateLocal(bv, true);
733 case LFUN_INSERT_MATH:
734 case LFUN_SELFINSERT:
736 bv->lockedInsetStoreUndo(Undo::EDIT);
738 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
740 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
741 updateLocal(bv, true);
745 case LFUN_MATH_PANEL:
746 result = UNDISPATCHED;
750 if (mathcursor->selection())
751 mathcursor->selClear();
753 result = UNDISPATCHED;
756 case LFUN_INSET_TOGGLE:
757 mathcursor->insetToggle();
758 updateLocal(bv, true);
761 case LFUN_REF_INSERT:
762 //if (argument.empty()) {
763 // InsetCommandParams p("ref");
764 // owner_->getDialogs()->createRef(p.getAsString());
766 // InsetCommandParams p;
767 // p.setFromString(argument);
769 // InsetRef * inset = new InsetRef(p, *buffer_);
770 // if (!insertInset(inset))
773 // updateInset(inset, true);
777 InsetCommandParams p("ref");
778 bv->owner()->getDialogs()->createRef(p.getAsString());
780 //mathcursor->handleNest(new InsetRef2);
781 //mathcursor->insert(arg);
782 mathcursor->insert(MathAtom(new RefInset(arg)));
784 updateLocal(bv, true);
788 result = UNDISPATCHED;
791 mathcursor->normalize();
794 lyx::Assert(mathcursor);
796 if (mathcursor->selection() || was_selection)
797 toggleInsetSelection(bv);
799 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
800 result == UNDISPATCHED)
803 bv->unlockInset(this);
807 return result; // original version
811 void InsetFormulaBase::revealCodes(BufferView * bv) const
815 bv->owner()->message(mathcursor->info());
818 // write something to the minibuffer
819 // translate to latex
820 mathcursor->markInsert();
823 string str = os.str();
824 mathcursor->markErase();
825 string::size_type pos = 0;
827 for (string::iterator it = str.begin(); it != str.end(); ++it) {
830 else if (*it == '\0') {
832 pos = it - str.begin();
838 res = res.substr(pos - 30);
840 res = res.substr(0, 60);
841 bv->owner()->message(res);
846 Inset::Code InsetFormulaBase::lyxCode() const
848 return Inset::MATH_CODE;
852 int InsetFormulaBase::ylow() const
854 return yo_ - ascent(view_, font_);
858 int InsetFormulaBase::yhigh() const
860 return yo_ + descent(view_, font_);
864 int InsetFormulaBase::xlow() const
870 int InsetFormulaBase::xhigh() const
872 return xo_ + width(view_, font_);
876 /////////////////////////////////////////////////////////////////////
879 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
885 static InsetFormulaBase * lastformula = 0;
886 static MathIterator current = MathIterator(ibegin(par().nucleus()));
888 static string laststr;
890 if (lastformula != this || laststr != str) {
891 //lyxerr << "reset lastformula to " << this << "\n";
894 current = ibegin(par().nucleus());
896 mathed_parse_cell(ar, str);
900 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
902 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
903 if (it.cell().matchpart(ar, it.position().pos_)) {
904 bv->unlockInset(bv->theLockingInset());
905 if (!bv->lockInset(this)) {
906 lyxerr << "Cannot lock inset" << endl;
910 mathcursor = new MathCursor(this, true);
912 mathcursor->setSelection(it.cursor(), ar.size());
915 updateLocal(bv, false);
920 //lyxerr << "not found!\n";
926 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
929 lyxerr << "searching backward not implemented in mathed" << endl;
930 return searchForward(bv, what, a, b);
934 /////////////////////////////////////////////////////////////////////
937 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
939 if (!bv->available())
942 // use selection if available..
944 //if (action == LFUN_MATH_IMPORT_SELECTION)
948 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
951 InsetFormula * f = new InsetFormula(bv);
952 if (openNewInset(bv, f)) {
954 // don't do that also for LFUN_MATH_MODE unless you want end up with
955 // always changing to mathrm when opening an inlined inset
956 // -- I really hate "LyXfunc overloading"...
958 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
959 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
962 // create a macro if we see "\\newcommand" somewhere, and an ordinary
964 if (sel.find("\\newcommand") == string::npos &&
965 sel.find("\\def") == string::npos)
967 InsetFormula * f = new InsetFormula(sel);
968 bv->getLyXText()->cutSelection(bv);
972 if (!mathed_parse_macro(name, sel))
974 InsetFormulaMacro * f = new InsetFormulaMacro(sel);
975 bv->getLyXText()->cutSelection(bv);
979 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
983 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
985 mathDispatchCreation(bv, arg, true);
989 void mathDispatchMathMode(BufferView * bv, string const & arg)
991 mathDispatchCreation(bv, arg, false);
995 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
997 mathDispatchCreation(bv, arg, true);
1001 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1003 if (!bv->available())
1006 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1009 string const s1 = token(s, ' ', 1);
1010 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1011 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1016 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1018 if (!bv->available())
1020 InsetFormula * f = new InsetFormula(bv);
1021 if (openNewInset(bv, f)) {
1022 f->mutate("simple");
1023 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1028 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1030 if (!bv->available())
1032 InsetFormula * f = new InsetFormula(bv);
1033 if (openNewInset(bv, f)) {
1034 f->mutate("simple");
1035 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1040 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1042 if (!bv->available())
1044 InsetFormula * f = new InsetFormula(bv);
1045 if (openNewInset(bv, f)) {
1046 f->mutate("simple");
1047 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1052 void mathDispatchGreek(BufferView * bv, string const & arg)
1054 if (!bv->available())
1056 InsetFormula * f = new InsetFormula(bv);
1057 if (openNewInset(bv, f)) {
1058 f->mutate("simple");
1059 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1065 void mathDispatch(BufferView *, kb_action, string const &)
1069 void mathDispatch(BufferView *, string const &)