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;
175 releaseMathCursor(bv);
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 << "Cannot lock math inset in edit call!\n";
191 releaseMathCursor(bv);
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);
205 releaseMathCursor(bv);
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)
223 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
224 if (isCursorVisible())
231 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
233 if (isCursorVisible())
237 getCursorPos(bv, x, y);
238 math_font_max_dim(font_, asc, des);
239 bv->showLockedInsetCursor(x, y, asc, des);
240 setCursorVisible(true);
241 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
245 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
247 if (!isCursorVisible())
249 bv->hideLockedInsetCursor();
250 setCursorVisible(false);
251 //lyxerr << "hideInsetCursor: \n";
255 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
260 math_font_max_dim(font_, asc, des);
261 getCursorPos(bv, x, y);
263 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
264 bv->fitLockedInsetCursor(x, y, asc, des);
268 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
271 bv->updateInset(this, false);
275 vector<string> const InsetFormulaBase::getLabelList() const
277 return vector<string>();
281 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
286 bv->updateInset(this, dirty);
290 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
291 int /*x*/, int /*y*/, mouse_button::state button)
296 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
299 bv->updateInset(this, false);
301 if (button == mouse_button::button3) {
302 // try to dispatch to enclosed insets first
303 if (mathcursor->dispatch("mouse-3-release"))
306 // launch math panel for right mouse button
307 bv->owner()->getDialogs()->showMathPanel();
311 if (button == mouse_button::button1) {
312 // try to dispatch to enclosed insets first
313 if (mathcursor->dispatch("mouse-1-release"))
316 // try to set the cursor
318 //mathcursor = new MathCursor(this, x == 0);
320 //mathcursor->setPos(x + xo_, y + yo_);
327 void InsetFormulaBase::insetButtonPress(BufferView * bv,
328 int x, int y, mouse_button::state button)
330 //lyxerr << "insetButtonPress: "
331 // << x << " " << y << " but: " << button << "\n";
332 //lyxerr << "formula: ";
335 releaseMathCursor(bv);
336 mathcursor = new MathCursor(this, x == 0);
338 if (button == mouse_button::button1) {
339 // just set the cursor here
340 lyxerr << "setting cursor\n";
344 mathcursor->selClear();
345 mathcursor->setPos(x + xo_, y + yo_);
347 if (mathcursor->dispatch("mouse-1-press")) {
353 if (button == mouse_button::button3) {
354 if (mathcursor->dispatch("mouse-3-press")) {
359 bv->updateInset(this, false);
363 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
364 int x, int y, mouse_button::state button)
369 if (button == mouse_button::button1)
370 if (mathcursor->dispatch("mouse-1-motion"))
373 if (button == mouse_button::button3)
374 if (mathcursor->dispatch("mouse-3-motion"))
377 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
378 //lyxerr << "insetMotionNotify: ignored\n";
384 if (!mathcursor->selection())
385 mathcursor->selStart();
387 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
388 // << ' ' << button << "\n";
390 mathcursor->setPos(x + xo_, y + yo_);
392 bv->updateInset(this, false);
396 UpdatableInset::RESULT
397 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
400 lyxerr << "InsetFormulaBase::localDispatch: act: " << action
402 << "' cursor: " << mathcursor << "\n";
407 RESULT result = DISPATCHED;
409 bool was_macro = mathcursor->inMacroMode();
410 bool was_selection = mathcursor->selection();
414 mathcursor->normalize();
420 sel = true; // fall through...
422 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
423 //lyxerr << "calling scroll 20\n";
425 updateLocal(bv, false);
426 // write something to the minibuffer
427 //bv->owner()->message(mathcursor->info());
431 sel = true; // fall through
433 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
434 updateLocal(bv, false);
438 sel = true; // fall through
440 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
441 updateLocal(bv, false);
445 sel = true; // fall through
447 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
448 updateLocal(bv, false);
452 sel = true; // fall through
454 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
455 updateLocal(bv, false);
459 sel = true; // fall through
461 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
462 updateLocal(bv, false);
467 result = FINISHED_UP;
468 updateLocal(bv, false);
473 result = FINISHED_DOWN;
474 updateLocal(bv, false);
477 case LFUN_DELETE_LINE_FORWARD:
478 bv->lockedInsetStoreUndo(Undo::DELETE);
479 mathcursor->delLine();
480 updateLocal(bv, true);
484 mathcursor->idxNext();
485 updateLocal(bv, false);
489 mathcursor->idxPrev();
490 updateLocal(bv, false);
494 bv->lockedInsetStoreUndo(Undo::EDIT);
495 mathcursor->splitCell();
496 updateLocal(bv, true);
499 case LFUN_DELETE_WORD_BACKWARD:
501 bv->lockedInsetStoreUndo(Undo::DELETE);
502 mathcursor->backspace();
503 updateLocal(bv, true);
506 case LFUN_DELETE_WORD_FORWARD:
508 bv->lockedInsetStoreUndo(Undo::DELETE);
510 bv->updateInset(this, true);
514 // sprintf(dispatch_buffer, "%d %d",);
515 // dispatch_result = dispatch_buffer;
518 lyxerr << "LFUN_SETXY broken!\n";
521 istringstream is(arg.c_str());
523 mathcursor->setPos(x, y);
524 updateLocal(bv, false);
530 mathcursor->macroModeClose();
531 bv->lockedInsetStoreUndo(Undo::EDIT);
532 mathcursor->selPaste();
533 updateLocal(bv, true);
537 bv->lockedInsetStoreUndo(Undo::DELETE);
538 mathcursor->selCut();
539 updateLocal(bv, true);
543 mathcursor->selCopy();
546 case LFUN_WORDRIGHTSEL:
547 case LFUN_WORDLEFTSEL:
550 // Special casing for superscript in case of LyX handling
552 case LFUN_CIRCUMFLEX:
554 // do superscript if LyX handles
556 bv->lockedInsetStoreUndo(Undo::EDIT);
557 mathcursor->script(true);
558 updateLocal(bv, true);
575 case LFUN_HUNG_UMLAUT:
579 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
580 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
581 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
582 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
583 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
584 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
585 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
586 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
587 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
588 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
589 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
592 handleFont(bv, arg, "lyxgreek1");
594 mathcursor->interpret(arg);
598 if (mathcursor->currentMode()) {
599 handleFont(bv, arg, "textrm");
601 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
602 updateLocal(bv, true);
604 //bv->owner()->message(_("math text mode toggled"));
607 case LFUN_MATH_LIMITS:
608 bv->lockedInsetStoreUndo(Undo::EDIT);
609 if (mathcursor->toggleLimits())
610 updateLocal(bv, true);
616 bv->lockedInsetStoreUndo(Undo::EDIT);
617 mathcursor->setSize(arg);
618 updateLocal(bv, true);
623 case LFUN_INSERT_MATRIX:
625 bv->lockedInsetStoreUndo(Undo::EDIT);
626 mathcursor->interpret("matrix " + arg);
627 updateLocal(bv, true);
631 case LFUN_SUPERSCRIPT:
634 bv->lockedInsetStoreUndo(Undo::EDIT);
635 mathcursor->script(action == LFUN_SUPERSCRIPT);
636 updateLocal(bv, true);
640 case LFUN_MATH_DELIM:
642 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
644 string rs = split(arg, ls, ' ');
645 // Reasonable default values
651 bv->lockedInsetStoreUndo(Undo::EDIT);
652 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
653 updateLocal(bv, true);
657 case LFUN_PROTECTEDSPACE:
658 case LFUN_MATH_SPACE:
659 bv->lockedInsetStoreUndo(Undo::EDIT);
660 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
661 updateLocal(bv, true);
665 bv->owner()->message(_("Invalid action in math mode!"));
669 case LFUN_MATH_HALIGN:
670 case LFUN_MATH_VALIGN:
671 case LFUN_MATH_ROW_INSERT:
672 case LFUN_MATH_ROW_DELETE:
673 case LFUN_MATH_COLUMN_INSERT:
674 case LFUN_MATH_COLUMN_DELETE:
676 MathInset::idx_type idx = 0;
677 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
679 mathcursor->popToEnclosingGrid();
680 bv->lockedInsetStoreUndo(Undo::EDIT);
681 char align = arg.size() ? arg[0] : 'c';
683 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
684 case LFUN_MATH_VALIGN: p->valign(align); break;
685 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
686 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
687 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
688 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
691 updateLocal(bv, true);
696 case LFUN_EXEC_COMMAND:
697 result = UNDISPATCHED;
700 case LFUN_BREAKPARAGRAPH:
701 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
702 //lyxerr << "LFUN ignored\n";
706 // interpret this as if a backslash was typed
707 bv->lockedInsetStoreUndo(Undo::EDIT);
708 mathcursor->interpret('\\');
709 updateLocal(bv, true);
713 case LFUN_INSERT_MATH:
714 case LFUN_SELFINSERT:
716 bv->lockedInsetStoreUndo(Undo::EDIT);
718 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
720 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
721 updateLocal(bv, true);
725 case LFUN_MATH_PANEL:
726 result = UNDISPATCHED;
730 if (mathcursor->selection())
731 mathcursor->selClear();
733 result = UNDISPATCHED;
736 case LFUN_INSET_TOGGLE:
737 mathcursor->insetToggle();
738 updateLocal(bv, true);
741 case LFUN_REF_INSERT:
742 //if (argument.empty()) {
743 // InsetCommandParams p("ref");
744 // owner_->getDialogs()->createRef(p.getAsString());
746 // InsetCommandParams p;
747 // p.setFromString(argument);
749 // InsetRef * inset = new InsetRef(p, *buffer_);
750 // if (!insertInset(inset))
753 // updateInset(inset, true);
757 InsetCommandParams p("ref");
758 bv->owner()->getDialogs()->createRef(p.getAsString());
760 //mathcursor->handleNest(new InsetRef2);
761 //mathcursor->insert(arg);
762 mathcursor->insert(MathAtom(new RefInset(arg)));
764 updateLocal(bv, true);
768 result = UNDISPATCHED;
771 mathcursor->normalize();
774 lyx::Assert(mathcursor);
776 if (mathcursor->selection() || was_selection)
777 toggleInsetSelection(bv);
779 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
780 result == UNDISPATCHED) {
785 releaseMathCursor(bv);
786 bv->unlockInset(this);
789 return result; // original version
793 void InsetFormulaBase::revealCodes(BufferView * bv) const
797 bv->owner()->message(mathcursor->info());
800 // write something to the minibuffer
801 // translate to latex
802 mathcursor->markInsert();
805 string str = os.str();
806 mathcursor->markErase();
807 string::size_type pos = 0;
809 for (string::iterator it = str.begin(); it != str.end(); ++it) {
812 else if (*it == '\0') {
814 pos = it - str.begin();
820 res = res.substr(pos - 30);
822 res = res.substr(0, 60);
823 bv->owner()->message(res);
828 Inset::Code InsetFormulaBase::lyxCode() const
830 return Inset::MATH_CODE;
834 int InsetFormulaBase::ylow() const
836 return yo_ - ascent(view_, font_);
840 int InsetFormulaBase::yhigh() const
842 return yo_ + descent(view_, font_);
846 int InsetFormulaBase::xlow() const
852 int InsetFormulaBase::xhigh() const
854 return xo_ + width(view_, font_);
858 /////////////////////////////////////////////////////////////////////
861 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
867 static InsetFormulaBase * lastformula = 0;
868 static MathIterator current = MathIterator(ibegin(par().nucleus()));
870 static string laststr;
872 if (lastformula != this || laststr != str) {
873 //lyxerr << "reset lastformula to " << this << "\n";
876 current = ibegin(par().nucleus());
878 mathed_parse_cell(ar, str);
882 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
884 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
885 if (it.cell().matchpart(ar, it.position().pos_)) {
886 bv->unlockInset(bv->theLockingInset());
887 if (!bv->lockInset(this)) {
888 lyxerr << "Cannot lock inset" << endl;
892 mathcursor = new MathCursor(this, true);
894 mathcursor->setSelection(it.cursor(), ar.size());
897 updateLocal(bv, false);
902 //lyxerr << "not found!\n";
908 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
911 lyxerr << "searching backward not implemented in mathed" << endl;
912 return searchForward(bv, what, a, b);
916 /////////////////////////////////////////////////////////////////////
919 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
921 if (!bv->available())
924 // use selection if available..
926 //if (action == LFUN_MATH_IMPORT_SELECTION)
930 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
933 InsetFormula * f = new InsetFormula(bv);
934 if (openNewInset(bv, f)) {
936 // don't do that also for LFUN_MATH_MODE unless you want end up with
937 // always changing to mathrm when opening an inlined inset
938 // -- I really hate "LyXfunc overloading"...
940 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
941 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
944 // create a macro if we see "\\newcommand" somewhere, and an ordinary
946 InsetFormulaBase * f;
947 if (sel.find("\\newcommand") == string::npos &&
948 sel.find("\\def") == string::npos)
949 f = new InsetFormula(sel);
951 f = new InsetFormulaMacro(sel);
952 bv->getLyXText()->cutSelection(bv);
955 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
959 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
961 mathDispatchCreation(bv, arg, true);
965 void mathDispatchMathMode(BufferView * bv, string const & arg)
967 mathDispatchCreation(bv, arg, false);
971 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
973 mathDispatchCreation(bv, arg, true);
977 void mathDispatchMathMacro(BufferView * bv, string const & arg)
979 if (!bv->available())
982 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
985 string const s1 = token(s, ' ', 1);
986 int const na = s1.empty() ? 0 : lyx::atoi(s1);
987 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
992 void mathDispatchMathDelim(BufferView * bv, string const & arg)
994 if (!bv->available())
996 InsetFormula * f = new InsetFormula(bv);
997 if (openNewInset(bv, f)) {
999 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1004 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1006 if (!bv->available())
1008 InsetFormula * f = new InsetFormula(bv);
1009 if (openNewInset(bv, f)) {
1010 f->mutate("simple");
1011 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1016 void mathDispatchInsertMath(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_INSERT_MATH, arg);
1028 void mathDispatchGreek(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_GREEK, arg);
1041 void mathDispatch(BufferView *, kb_action, string const &)
1045 void mathDispatch(BufferView *, string const &)