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_deliminset.h"
44 #include "math_cursor.h"
45 #include "math_factory.h"
46 #include "math_fontinset.h"
47 #include "math_hullinset.h"
48 #include "math_iterator.h"
49 #include "math_macrotable.h"
50 #include "math_parser.h"
52 #include "math_spaceinset.h"
53 #include "undo_funcs.h"
54 #include "textpainter.h"
55 #include "frontends/Dialogs.h"
57 #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
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(createMathInset(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)
315 //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-release"))
325 // launch math panel for right mouse button
326 bv->owner()->getDialogs()->showMathPanel();
330 if (button == mouse_button::button1) {
331 // try to dispatch to enclosed insets first
332 if (mathcursor->dispatch("mouse-1-release"))
335 // try to set the cursor
337 //mathcursor = new MathCursor(this, x == 0);
339 //mathcursor->setPos(x + xo_, y + yo_);
346 void InsetFormulaBase::insetButtonPress(BufferView * bv,
347 int x, int y, mouse_button::state button)
349 lyxerr << "insetButtonPress: "
350 << x << " " << y << " but: " << button << "\n";
351 lyxerr << "formula: ";
355 mathcursor = new MathCursor(this, x == 0);
357 if (button == mouse_button::button1) {
358 // just set the cursor here
359 lyxerr << "setting cursor\n";
363 mathcursor->selClear();
364 mathcursor->setPos(x + xo_, y + yo_);
366 if (mathcursor->dispatch("mouse-1-press")) {
372 if (button == mouse_button::button3) {
373 if (mathcursor->dispatch("mouse-3-press")) {
378 bv->updateInset(this, false);
382 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
383 int x, int y, mouse_button::state button)
388 if (button == mouse_button::button1)
389 if (mathcursor->dispatch("mouse-1-motion"))
392 if (button == mouse_button::button3)
393 if (mathcursor->dispatch("mouse-3-motion"))
396 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
397 //lyxerr << "insetMotionNotify: ignored\n";
403 if (!mathcursor->selection())
404 mathcursor->selStart();
406 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
407 // << ' ' << button << "\n";
409 mathcursor->setPos(x + xo_, y + yo_);
411 bv->updateInset(this, false);
415 UpdatableInset::RESULT
416 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
419 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
420 // << " arg: '" << arg
421 // << "' cursor: " << mathcursor
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 updateLocal(bv, 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("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->handleNest(MathAtom(new MathDelimInset(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(",")));
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 InsetRef2);
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())
940 // use selection if available..
942 //if (action == LFUN_MATH_IMPORT_SELECTION)
946 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
949 InsetFormula * f = new InsetFormula(bv);
950 if (openNewInset(bv, f)) {
952 // don't do that also for LFUN_MATH_MODE unless you want end up with
953 // always changing to mathrm when opening an inlined inset
954 // -- I really hate "LyXfunc overloading"...
956 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
957 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
960 // create a macro if we see "\\newcommand" somewhere, and an ordinary
962 InsetFormulaBase * f;
963 if (sel.find("\\newcommand") == string::npos &&
964 sel.find("\\def") == string::npos)
965 f = new InsetFormula(sel);
967 f = new InsetFormulaMacro(sel);
968 bv->getLyXText()->cutSelection(bv);
971 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
975 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
977 mathDispatchCreation(bv, arg, true);
981 void mathDispatchMathMode(BufferView * bv, string const & arg)
983 mathDispatchCreation(bv, arg, false);
987 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
989 mathDispatchCreation(bv, arg, true);
993 void mathDispatchMathMacro(BufferView * bv, string const & arg)
995 if (!bv->available())
998 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1001 string const s1 = token(s, ' ', 1);
1002 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1003 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1008 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1010 if (!bv->available())
1012 InsetFormula * f = new InsetFormula(bv);
1013 if (openNewInset(bv, f)) {
1014 f->mutate("simple");
1015 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1020 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1022 if (!bv->available())
1024 InsetFormula * f = new InsetFormula(bv);
1025 if (openNewInset(bv, f)) {
1026 f->mutate("simple");
1027 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1032 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1034 if (!bv->available())
1036 InsetFormula * f = new InsetFormula(bv);
1037 if (openNewInset(bv, f)) {
1038 f->mutate("simple");
1039 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1044 void mathDispatchGreek(BufferView * bv, string const & arg)
1046 if (!bv->available())
1048 InsetFormula * f = new InsetFormula(bv);
1049 if (openNewInset(bv, f)) {
1050 f->mutate("simple");
1051 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1057 void mathDispatch(BufferView *, kb_action, string const &)
1061 void mathDispatch(BufferView *, string const &)