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/insetcommand.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, 0, 0, mouse_button::none);
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.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;
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[Debug::MATHED] << "Cannot lock inset!!!" << endl;
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);
207 bv->updateInset(this, false);
211 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
214 mathcursor->getPos(x, y);
217 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
221 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
226 if (isCursorVisible())
227 bv->hideLockedInsetCursor();
232 mathcursor->getPos(x, y);
236 math_font_max_dim(font_, asc, des);
237 bv->showLockedInsetCursor(x, y, asc, des);
238 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
241 toggleCursorVisible();
245 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
247 if (isCursorVisible())
253 mathcursor->getPos(x, y);
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";
261 toggleInsetCursor(bv);
265 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
267 if (isCursorVisible())
268 toggleInsetCursor(bv);
272 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
277 int const asc = font_metrics::maxAscent(font_);
278 int const desc = font_metrics::maxDescent(font_);
281 getCursorPos(bv, x, y);
282 bv->fitLockedInsetCursor(x, y, asc, desc);
286 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
289 bv->updateInset(this, false);
293 vector<string> const InsetFormulaBase::getLabelList() const
295 return vector<string>();
299 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
304 bv->updateInset(this, dirty);
308 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
309 int /*x*/, int /*y*/, mouse_button::state button)
311 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
317 bv->updateInset(this, false);
319 if (button == mouse_button::button3) {
320 // try to dispatch to enclosed insets first
321 if (mathcursor->dispatch("mouse 3"))
324 // launch math panel for right mouse button
325 bv->owner()->getDialogs()->showMathPanel();
332 void InsetFormulaBase::insetButtonPress(BufferView * bv,
333 int x, int y, mouse_button::state button)
335 //lyxerr << "insetButtonPress: "
336 // << x << " " << y << " but: " << button << "\n";
343 mathcursor = new MathCursor(this, x == 0);
347 mathcursor->selClear();
348 mathcursor->setPos(x + xo_, y + yo_);
352 lyxerr << "insetButtonPress: 2\n";
355 bv->lockedInsetStoreUndo(Undo::EDIT);
357 mathcursor->selGet(ar);
358 mathcursor->setPos(x + xo_, y + yo_);
360 bv->getLyXText()->selectionAsString(bv->buffer(), false);
361 mathed_parse_cell(ar, sel);
362 mathcursor->insert(ar);
367 // launch math panel for right mouse button
368 bv->owner()->getDialogs()->showMathPanel();
372 if (button == mouse_button::button1 || !mathcursor) {
374 mathcursor = new MathCursor(this, x == 0);
378 mathcursor->selClear();
379 mathcursor->setPos(x + xo_, y + yo_);
382 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
384 // launch math panel for right mouse button
385 bv->owner()->getDialogs()->showMathPanel();
389 bv->updateInset(this, false);
393 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
394 int x, int y, mouse_button::state)
399 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
400 //lyxerr << "insetMotionNotify: ignored\n";
406 if (!mathcursor->selection())
407 mathcursor->selStart();
409 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
410 // << ' ' << button << "\n";
412 mathcursor->setPos(x + xo_, y + yo_);
414 bv->updateInset(this, false);
418 UpdatableInset::RESULT
419 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
422 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
423 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
428 RESULT result = DISPATCHED;
430 bool was_macro = mathcursor->inMacroMode();
431 bool was_selection = mathcursor->selection();
435 mathcursor->normalize();
440 // --- Cursor Movements ---------------------------------------------
443 sel = true; // fall through...
446 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
447 //lyxerr << "calling scroll 20\n";
449 updateLocal(bv, false);
450 // write something to the minibuffer
451 //bv->owner()->message(mathcursor->info());
456 sel = true; // fall through
459 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
460 updateLocal(bv, false);
468 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
469 updateLocal(bv, false);
477 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
478 updateLocal(bv, false);
485 mathcursor->home(sel);
486 updateLocal(bv, false);
493 mathcursor->end(sel);
494 updateLocal(bv, false);
497 case LFUN_DELETE_LINE_FORWARD:
498 bv->lockedInsetStoreUndo(Undo::DELETE);
499 mathcursor->delLine();
500 updateLocal(bv, true);
504 mathcursor->idxNext();
505 updateLocal(bv, false);
509 mathcursor->idxPrev();
510 updateLocal(bv, false);
514 bv->lockedInsetStoreUndo(Undo::EDIT);
515 mathcursor->splitCell();
516 updateLocal(bv, true);
519 case LFUN_DELETE_WORD_BACKWARD:
521 bv->lockedInsetStoreUndo(Undo::DELETE);
522 mathcursor->backspace();
523 updateLocal(bv, true);
526 case LFUN_DELETE_WORD_FORWARD:
528 bv->lockedInsetStoreUndo(Undo::DELETE);
530 bv->updateInset(this, true);
534 // sprintf(dispatch_buffer, "%d %d",);
535 // dispatch_result = dispatch_buffer;
538 lyxerr << "LFUN_SETXY broken!\n";
541 istringstream is(arg.c_str());
543 mathcursor->setPos(x, y);
544 updateLocal(bv, false);
550 mathcursor->macroModeClose();
551 bv->lockedInsetStoreUndo(Undo::EDIT);
552 mathcursor->selPaste();
553 updateLocal(bv, true);
557 bv->lockedInsetStoreUndo(Undo::DELETE);
558 mathcursor->selCut();
559 updateLocal(bv, true);
563 mathcursor->selCopy();
566 case LFUN_WORDRIGHTSEL:
567 case LFUN_WORDLEFTSEL:
570 // Special casing for superscript in case of LyX handling
572 case LFUN_CIRCUMFLEX:
574 // do superscript if LyX handles
576 bv->lockedInsetStoreUndo(Undo::EDIT);
577 mathcursor->script(true);
578 updateLocal(bv, true);
594 case LFUN_HUNG_UMLAUT:
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;
611 handleFont(bv, arg, "lyxgreek1");
613 mathcursor->interpret(arg);
617 if (mathcursor->inMathMode()) {
618 handleFont(bv, arg, "textrm");
620 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
621 updateLocal(bv, true);
623 //bv->owner()->message(_("math text mode toggled"));
626 case LFUN_MATH_LIMITS:
627 bv->lockedInsetStoreUndo(Undo::EDIT);
628 if (mathcursor->toggleLimits())
629 updateLocal(bv, true);
635 bv->lockedInsetStoreUndo(Undo::EDIT);
636 mathcursor->setSize(arg);
637 updateLocal(bv, true);
642 case LFUN_INSERT_MATRIX:
644 bv->lockedInsetStoreUndo(Undo::EDIT);
645 mathcursor->interpret("matrix " + arg);
646 updateLocal(bv, true);
650 case LFUN_SUPERSCRIPT:
653 bv->lockedInsetStoreUndo(Undo::EDIT);
654 mathcursor->script(action == LFUN_SUPERSCRIPT);
655 updateLocal(bv, true);
659 case LFUN_MATH_DELIM:
661 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
663 string rs = split(arg, ls, ' ');
664 // Reasonable default values
670 bv->lockedInsetStoreUndo(Undo::EDIT);
671 mathcursor->handleDelim(ls, rs);
672 updateLocal(bv, true);
676 case LFUN_PROTECTEDSPACE:
677 case LFUN_MATH_SPACE:
678 bv->lockedInsetStoreUndo(Undo::EDIT);
679 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
680 updateLocal(bv, true);
684 bv->owner()->message(_("Invalid action in math mode!"));
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:
695 MathInset::idx_type idx = 0;
696 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
698 mathcursor->popToEnclosingGrid();
699 bv->lockedInsetStoreUndo(Undo::EDIT);
700 char align = arg.size() ? arg[0] : 'c';
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;
710 updateLocal(bv, true);
715 case LFUN_EXEC_COMMAND:
716 result = UNDISPATCHED;
719 case LFUN_BREAKPARAGRAPH:
720 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
721 //lyxerr << "LFUN ignored\n";
725 // interpret this as if a backslash was typed
726 bv->lockedInsetStoreUndo(Undo::EDIT);
727 mathcursor->interpret('\\');
728 updateLocal(bv, true);
732 case LFUN_INSERT_MATH:
733 case LFUN_SELFINSERT:
735 bv->lockedInsetStoreUndo(Undo::EDIT);
737 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
739 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
740 updateLocal(bv, true);
744 case LFUN_MATH_PANEL:
745 result = UNDISPATCHED;
749 if (mathcursor->selection())
750 mathcursor->selClear();
752 result = UNDISPATCHED;
755 case LFUN_INSET_TOGGLE:
756 mathcursor->insetToggle();
757 updateLocal(bv, true);
760 case LFUN_REF_INSERT:
761 //if (argument.empty()) {
762 // InsetCommandParams p("ref");
763 // owner_->getDialogs()->createRef(p.getAsString());
765 // InsetCommandParams p;
766 // p.setFromString(argument);
768 // InsetRef * inset = new InsetRef(p, *buffer_);
769 // if (!insertInset(inset))
772 // updateInset(inset, true);
776 InsetCommandParams p("ref");
777 bv->owner()->getDialogs()->createRef(p.getAsString());
779 //mathcursor->handleNest(new RefInset);
780 //mathcursor->insert(arg);
781 mathcursor->insert(MathAtom(new RefInset(arg)));
783 updateLocal(bv, true);
787 result = UNDISPATCHED;
790 mathcursor->normalize();
793 lyx::Assert(mathcursor);
795 if (mathcursor->selection() || was_selection)
796 toggleInsetSelection(bv);
798 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
799 result == UNDISPATCHED)
802 bv->unlockInset(this);
806 return result; // original version
810 void InsetFormulaBase::revealCodes(BufferView * bv) const
814 bv->owner()->message(mathcursor->info());
817 // write something to the minibuffer
818 // translate to latex
819 mathcursor->markInsert();
822 string str = os.str();
823 mathcursor->markErase();
824 string::size_type pos = 0;
826 for (string::iterator it = str.begin(); it != str.end(); ++it) {
829 else if (*it == '\0') {
831 pos = it - str.begin();
837 res = res.substr(pos - 30);
839 res = res.substr(0, 60);
840 bv->owner()->message(res);
845 Inset::Code InsetFormulaBase::lyxCode() const
847 return Inset::MATH_CODE;
851 int InsetFormulaBase::ylow() const
853 return yo_ - ascent(view_, font_);
857 int InsetFormulaBase::yhigh() const
859 return yo_ + descent(view_, font_);
863 int InsetFormulaBase::xlow() const
869 int InsetFormulaBase::xhigh() const
871 return xo_ + width(view_, font_);
875 /////////////////////////////////////////////////////////////////////
878 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
884 static InsetFormulaBase * lastformula = 0;
885 static MathIterator current = MathIterator(ibegin(par().nucleus()));
887 static string laststr;
889 if (lastformula != this || laststr != str) {
890 //lyxerr << "reset lastformula to " << this << "\n";
893 current = ibegin(par().nucleus());
895 mathed_parse_cell(ar, str);
899 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
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;
909 mathcursor = new MathCursor(this, true);
911 mathcursor->setSelection(it.cursor(), ar.size());
914 updateLocal(bv, false);
919 //lyxerr << "not found!\n";
925 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
928 lyxerr << "searching backward not implemented in mathed" << endl;
929 return searchForward(bv, what, a, b);
933 /////////////////////////////////////////////////////////////////////
936 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
938 if (bv->available()) {
939 // use selection if available..
941 //if (action == LFUN_MATH_IMPORT_SELECTION)
945 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
948 InsetFormula * f = new InsetFormula;
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"...
955 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
956 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
959 // create a macro if we see "\\newcommand" somewhere, and an ordinary
961 if (sel.find("\\newcommand") == string::npos &&
962 sel.find("\\def") == string::npos)
964 InsetFormula * f = new InsetFormula;
967 bv->getLyXText()->cutSelection(bv);
971 if (!mathed_parse_macro(name, sel))
973 InsetFormulaMacro * f = new InsetFormulaMacro(sel);
974 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()) {
1005 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
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));
1016 void mathDispatchMathDelim(BufferView * bv, string const & arg)
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);
1027 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
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);
1038 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1040 if (bv->available()) {
1041 if (arg.size() && arg[0] == '\\') {
1042 InsetFormula * f = new InsetFormula;
1044 if (!bv->insertInset(f))
1046 else if (!mathcursor) // hotfix
1047 bv->getLyXText()->cursorRight(bv);
1049 mathDispatchMathMode(bv, arg);
1055 void mathDispatchGreek(BufferView * bv, string const & arg)
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);
1068 void mathDispatch(BufferView *, kb_action, string const &)
1072 void mathDispatch(BufferView *, string const &)