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"
27 #include "commandtags.h"
28 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/mouse_state.h"
40 #include "math_arrayinset.h"
41 #include "math_charinset.h"
42 #include "math_cursor.h"
43 #include "math_factory.h"
44 #include "math_fontinset.h"
45 #include "math_hullinset.h"
46 #include "math_iterator.h"
47 #include "math_macrotable.h"
48 #include "math_parser.h"
50 #include "math_spaceinset.h"
51 #include "undo_funcs.h"
52 #include "textpainter.h"
53 #include "frontends/Dialogs.h"
55 #include "../insets/insetcommand.h"
57 #include "ref_inset.h"
64 MathCursor * mathcursor = 0;
75 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
77 if (!bv->insertInset(new_inset)) {
81 new_inset->edit(bv, 0, 0, mouse_button::none);
90 InsetFormulaBase::InsetFormulaBase()
91 : view_(0), font_(), xo_(0), yo_(0)
93 // This is needed as long the math parser is not re-entrant
94 MathMacroTable::builtinMacros();
95 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
96 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
97 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
98 //lyxerr << "sizeof(LyXFont): " << sizeof(LyXFont) << "\n";
102 // simply scrap this function if you want
103 void InsetFormulaBase::mutateToText()
106 // translate to latex
108 latex(NULL, os, false, false);
109 string str = os.str();
112 LyXText * lt = view_->getLyXText();
113 string::const_iterator cit = str.begin();
114 string::const_iterator end = str.end();
115 for (; cit != end; ++cit)
116 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
119 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
124 void InsetFormulaBase::handleFont
125 (BufferView * bv, string const & arg, string const & font)
127 bv->lockedInsetStoreUndo(Undo::EDIT);
128 bool sel = mathcursor->selection();
130 updateLocal(bv, true);
131 mathcursor->handleNest(new MathFontInset(font));
132 mathcursor->insert(arg);
134 updateLocal(bv, false);
138 // Check if uses AMS macros
139 void InsetFormulaBase::validate(LaTeXFeatures &) const
143 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
150 void InsetFormulaBase::metrics(BufferView * bv) const
156 mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
157 mi.base.font = font_;
158 mi.base.font.setColor(LColor::math);
163 string const InsetFormulaBase::editMessage() const
165 return _("Math editor mode");
169 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
171 if (!bv->lockInset(this))
172 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174 mathcursor = new MathCursor(this, true);
176 mathcursor->setPos(x, y);
177 //lyxerr << "setting pos to " << x << "," << y << "\n";
179 // if that is removed, we won't get the magenta box when entering an
180 // inset for the first time
181 bv->updateInset(this, false);
185 void InsetFormulaBase::edit(BufferView * bv, bool front)
187 if (!bv->lockInset(this))
188 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
190 mathcursor = new MathCursor(this, front);
192 bv->updateInset(this, false);
196 void InsetFormulaBase::insetUnlock(BufferView * bv)
199 if (mathcursor->inMacroMode()) {
200 mathcursor->macroModeClose();
201 updateLocal(bv, true);
206 bv->updateInset(this, false);
210 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
213 mathcursor->getPos(x, y);
216 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
220 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 if (isCursorVisible())
226 bv->hideLockedInsetCursor();
231 mathcursor->getPos(x, y);
235 math_font_max_dim(font_, asc, des);
236 bv->showLockedInsetCursor(x, y, asc, des);
237 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
240 toggleCursorVisible();
244 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
246 if (isCursorVisible())
252 mathcursor->getPos(x, y);
256 math_font_max_dim(font_, asc, des);
257 bv->fitLockedInsetCursor(x, y, asc, des);
258 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
260 toggleInsetCursor(bv);
264 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
266 if (isCursorVisible())
267 toggleInsetCursor(bv);
271 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
276 int const asc = font_metrics::maxAscent(font_);
277 int const desc = font_metrics::maxDescent(font_);
280 getCursorPos(bv, x, y);
281 bv->fitLockedInsetCursor(x, y, asc, desc);
285 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
288 bv->updateInset(this, false);
292 vector<string> const InsetFormulaBase::getLabelList() const
294 return vector<string>();
298 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
303 bv->updateInset(this, dirty);
307 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
308 int /*x*/, int /*y*/, mouse_button::state button)
310 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
316 bv->updateInset(this, false);
318 if (button == mouse_button::button3) {
319 // try to dispatch to enclosed insets first
320 if (mathcursor->dispatch("mouse 3"))
323 // launch math panel for right mouse button
324 bv->owner()->getDialogs()->showMathPanel();
331 void InsetFormulaBase::insetButtonPress(BufferView * bv,
332 int x, int y, mouse_button::state button)
334 //lyxerr << "insetButtonPress: "
335 // << x << " " << y << " but: " << button << "\n";
342 mathcursor = new MathCursor(this, x == 0);
346 mathcursor->selClear();
347 mathcursor->setPos(x + xo_, y + yo_);
351 lyxerr << "insetButtonPress: 2\n";
354 bv->lockedInsetStoreUndo(Undo::EDIT);
356 mathcursor->selGet(ar);
357 mathcursor->setPos(x + xo_, y + yo_);
359 bv->getLyXText()->selectionAsString(bv->buffer(), false);
360 mathed_parse_cell(ar, sel);
361 mathcursor->insert(ar);
366 // launch math panel for right mouse button
367 bv->owner()->getDialogs()->showMathPanel();
371 if (button == mouse_button::button1 || !mathcursor) {
373 mathcursor = new MathCursor(this, x == 0);
377 mathcursor->selClear();
378 mathcursor->setPos(x + xo_, y + yo_);
381 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
383 // launch math panel for right mouse button
384 bv->owner()->getDialogs()->showMathPanel();
388 bv->updateInset(this, false);
392 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
393 int x, int y, mouse_button::state)
398 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
399 //lyxerr << "insetMotionNotify: ignored\n";
405 if (!mathcursor->selection())
406 mathcursor->selStart();
408 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
409 // << ' ' << button << "\n";
411 mathcursor->setPos(x + xo_, y + yo_);
413 bv->updateInset(this, false);
417 UpdatableInset::RESULT
418 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
421 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
422 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
427 RESULT result = DISPATCHED;
429 bool was_macro = mathcursor->inMacroMode();
430 bool was_selection = mathcursor->selection();
434 mathcursor->normalize();
439 // --- Cursor Movements ---------------------------------------------
442 sel = true; // fall through...
445 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
446 //lyxerr << "calling scroll 20\n";
448 updateLocal(bv, false);
449 // write something to the minibuffer
450 //bv->owner()->message(mathcursor->info());
455 sel = true; // fall through
458 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
459 updateLocal(bv, false);
467 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
468 updateLocal(bv, false);
476 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
477 updateLocal(bv, false);
484 mathcursor->home(sel);
485 updateLocal(bv, false);
492 mathcursor->end(sel);
493 updateLocal(bv, false);
496 case LFUN_DELETE_LINE_FORWARD:
497 bv->lockedInsetStoreUndo(Undo::DELETE);
498 mathcursor->delLine();
499 updateLocal(bv, true);
503 mathcursor->idxNext();
504 updateLocal(bv, false);
508 mathcursor->idxPrev();
509 updateLocal(bv, false);
513 bv->lockedInsetStoreUndo(Undo::EDIT);
514 mathcursor->splitCell();
515 updateLocal(bv, true);
518 case LFUN_DELETE_WORD_BACKWARD:
520 bv->lockedInsetStoreUndo(Undo::DELETE);
521 mathcursor->backspace();
522 bv->updateInset(this, true);
525 case LFUN_DELETE_WORD_FORWARD:
527 bv->lockedInsetStoreUndo(Undo::DELETE);
529 bv->updateInset(this, true);
533 // sprintf(dispatch_buffer, "%d %d",);
534 // dispatch_result = dispatch_buffer;
537 lyxerr << "LFUN_SETXY broken!\n";
540 istringstream is(arg.c_str());
542 mathcursor->setPos(x, y);
543 updateLocal(bv, false);
549 mathcursor->macroModeClose();
550 bv->lockedInsetStoreUndo(Undo::EDIT);
551 mathcursor->selPaste();
552 updateLocal(bv, true);
556 bv->lockedInsetStoreUndo(Undo::DELETE);
557 mathcursor->selCut();
558 updateLocal(bv, true);
562 mathcursor->selCopy();
565 case LFUN_WORDRIGHTSEL:
566 case LFUN_WORDLEFTSEL:
569 // Special casing for superscript in case of LyX handling
571 case LFUN_CIRCUMFLEX:
573 // do superscript if LyX handles
575 bv->lockedInsetStoreUndo(Undo::EDIT);
576 mathcursor->script(true);
577 updateLocal(bv, true);
593 case LFUN_HUNG_UMLAUT:
597 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
598 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
599 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
600 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
601 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
602 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
603 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
604 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
605 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
606 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
607 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
610 handleFont(bv, arg, "lyxgreek1");
612 mathcursor->interpret(arg);
616 if (mathcursor->inMathMode()) {
617 handleFont(bv, arg, "textrm");
619 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
620 updateLocal(bv, true);
622 //bv->owner()->message(_("math text mode toggled"));
625 case LFUN_MATH_LIMITS:
626 bv->lockedInsetStoreUndo(Undo::EDIT);
627 if (mathcursor->toggleLimits())
628 updateLocal(bv, true);
634 bv->lockedInsetStoreUndo(Undo::EDIT);
635 mathcursor->setSize(arg);
636 updateLocal(bv, true);
641 case LFUN_INSERT_MATRIX:
643 bv->lockedInsetStoreUndo(Undo::EDIT);
644 mathcursor->interpret("matrix " + arg);
645 updateLocal(bv, true);
649 case LFUN_SUPERSCRIPT:
652 bv->lockedInsetStoreUndo(Undo::EDIT);
653 mathcursor->script(action == LFUN_SUPERSCRIPT);
654 updateLocal(bv, true);
658 case LFUN_MATH_DELIM:
660 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
662 string rs = split(arg, ls, ' ');
663 // Reasonable default values
669 bv->lockedInsetStoreUndo(Undo::EDIT);
670 mathcursor->handleDelim(ls, rs);
671 updateLocal(bv, true);
675 case LFUN_PROTECTEDSPACE:
676 case LFUN_MATH_SPACE:
677 bv->lockedInsetStoreUndo(Undo::EDIT);
678 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
679 updateLocal(bv, true);
683 bv->owner()->message(_("Invalid action in math mode!"));
687 case LFUN_MATH_HALIGN:
688 case LFUN_MATH_VALIGN:
689 case LFUN_MATH_ROW_INSERT:
690 case LFUN_MATH_ROW_DELETE:
691 case LFUN_MATH_COLUMN_INSERT:
692 case LFUN_MATH_COLUMN_DELETE:
694 MathInset::idx_type idx = 0;
695 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
697 mathcursor->popToEnclosingGrid();
698 bv->lockedInsetStoreUndo(Undo::EDIT);
699 char align = arg.size() ? arg[0] : 'c';
701 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
702 case LFUN_MATH_VALIGN: p->valign(align); break;
703 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
704 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
705 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
706 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
709 updateLocal(bv, true);
714 case LFUN_EXEC_COMMAND:
715 result = UNDISPATCHED;
718 case LFUN_BREAKPARAGRAPH:
719 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
720 //lyxerr << "LFUN ignored\n";
724 // interpret this as if a backslash was typed
725 bv->lockedInsetStoreUndo(Undo::EDIT);
726 mathcursor->interpret('\\');
727 updateLocal(bv, true);
731 case LFUN_INSERT_MATH:
732 case LFUN_SELFINSERT:
734 bv->lockedInsetStoreUndo(Undo::EDIT);
736 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
738 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
739 updateLocal(bv, true);
743 case LFUN_MATH_PANEL:
744 result = UNDISPATCHED;
748 if (mathcursor->selection())
749 mathcursor->selClear();
751 result = UNDISPATCHED;
754 case LFUN_INSET_TOGGLE:
755 mathcursor->insetToggle();
756 updateLocal(bv, true);
759 case LFUN_REF_INSERT:
760 //if (argument.empty()) {
761 // InsetCommandParams p("ref");
762 // owner_->getDialogs()->createRef(p.getAsString());
764 // InsetCommandParams p;
765 // p.setFromString(argument);
767 // InsetRef * inset = new InsetRef(p, *buffer_);
768 // if (!insertInset(inset))
771 // updateInset(inset, true);
775 InsetCommandParams p("ref");
776 bv->owner()->getDialogs()->createRef(p.getAsString());
778 //mathcursor->handleNest(new RefInset);
779 //mathcursor->insert(arg);
780 mathcursor->insert(MathAtom(new RefInset(arg)));
782 updateLocal(bv, true);
786 result = UNDISPATCHED;
789 mathcursor->normalize();
792 lyx::Assert(mathcursor);
794 if (mathcursor->selection() || was_selection)
795 toggleInsetSelection(bv);
797 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
798 result == UNDISPATCHED)
801 bv->unlockInset(this);
805 return result; // original version
809 void InsetFormulaBase::revealCodes(BufferView * bv) const
813 bv->owner()->message(mathcursor->info());
816 // write something to the minibuffer
817 // translate to latex
818 mathcursor->markInsert();
821 string str = os.str();
822 mathcursor->markErase();
823 string::size_type pos = 0;
825 for (string::iterator it = str.begin(); it != str.end(); ++it) {
828 else if (*it == '\0') {
830 pos = it - str.begin();
836 res = res.substr(pos - 30);
838 res = res.substr(0, 60);
839 bv->owner()->message(res);
844 Inset::Code InsetFormulaBase::lyxCode() const
846 return Inset::MATH_CODE;
850 int InsetFormulaBase::ylow() const
852 return yo_ - ascent(view_, font_);
856 int InsetFormulaBase::yhigh() const
858 return yo_ + descent(view_, font_);
862 int InsetFormulaBase::xlow() const
868 int InsetFormulaBase::xhigh() const
870 return xo_ + width(view_, font_);
874 /////////////////////////////////////////////////////////////////////
877 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
883 static InsetFormulaBase * lastformula = 0;
884 static MathIterator current = MathIterator(ibegin(par().nucleus()));
886 static string laststr;
888 if (lastformula != this || laststr != str) {
889 //lyxerr << "reset lastformula to " << this << "\n";
892 current = ibegin(par().nucleus());
894 mathed_parse_cell(ar, str);
898 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
900 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
901 if (it.cell().matchpart(ar, it.position().pos_)) {
902 bv->unlockInset(bv->theLockingInset());
903 if (!bv->lockInset(this)) {
904 lyxerr << "Cannot lock inset" << endl;
908 mathcursor = new MathCursor(this, true);
910 mathcursor->setSelection(it.cursor(), ar.size());
913 updateLocal(bv, false);
918 //lyxerr << "not found!\n";
924 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
927 lyxerr << "searching backward not implemented in mathed" << endl;
928 return searchForward(bv, what, a, b);
932 /////////////////////////////////////////////////////////////////////
935 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
937 if (bv->available()) {
938 // use selection if available..
940 //if (action == LFUN_MATH_IMPORT_SELECTION)
944 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
946 InsetFormulaBase * f;
948 f = new InsetFormula(LM_OT_SIMPLE);
949 if (openNewInset(bv, f)) {
950 // don't do that also for LFUN_MATH_MODE unless you want end up with
951 // always changing to mathrm when opening an inlined inset
952 // -- I really hate "LyXfunc overloading"...
954 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
955 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
958 // create a macro if we see "\\newcommand" somewhere, and an ordinary
960 if (sel.find("\\newcommand") == string::npos &&
961 sel.find("\\def") == string::npos)
963 f = new InsetFormula(sel);
966 if (!mathed_parse_macro(name, sel))
968 f = new InsetFormulaMacro(sel);
970 bv->getLyXText()->cutSelection(bv);
974 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
978 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
980 mathDispatchCreation(bv, arg, true);
984 void mathDispatchMathMode(BufferView * bv, string const & arg)
986 mathDispatchCreation(bv, arg, false);
990 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
992 mathDispatchCreation(bv, arg, true);
996 void mathDispatchMathMacro(BufferView * bv, string const & arg)
998 if (bv->available()) {
1000 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1003 string const s1 = token(s, ' ', 1);
1004 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1005 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1011 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1013 if (bv->available()) {
1014 if (openNewInset(bv, new InsetFormula(LM_OT_SIMPLE)))
1015 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1020 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1022 if (bv->available()) {
1023 if (openNewInset(bv, new InsetFormula(LM_OT_SIMPLE)))
1024 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1029 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1031 if (bv->available()) {
1032 if (arg.size() && arg[0] == '\\') {
1033 InsetFormula * f = new InsetFormula(arg);
1034 if (!bv->insertInset(f))
1036 else if (!mathcursor) // hotfix
1037 bv->getLyXText()->cursorRight(bv);
1039 mathDispatchMathMode(bv, arg);
1045 void mathDispatchGreek(BufferView * bv, string const & arg)
1047 if (bv->available()) {
1048 InsetFormula * f = new InsetFormula(LM_OT_SIMPLE);
1049 if (openNewInset(bv, f)) {
1050 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1057 void mathDispatch(BufferView *, kb_action, string const &)
1061 void mathDispatch(BufferView *, string const &)