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 "funcrequest.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "math_support.h"
36 #include "math_metricsinfo.h"
37 #include "support/lstrings.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/mouse_state.h"
42 #include "math_arrayinset.h"
43 #include "math_charinset.h"
44 #include "math_deliminset.h"
45 #include "math_cursor.h"
46 #include "math_factory.h"
47 #include "math_fontinset.h"
48 #include "math_hullinset.h"
49 #include "math_iterator.h"
50 #include "math_macrotable.h"
51 #include "math_parser.h"
53 #include "math_spaceinset.h"
54 #include "undo_funcs.h"
55 #include "textpainter.h"
56 #include "frontends/Dialogs.h"
58 #include "insets/insetcommandparams.h"
59 #include "ref_inset.h"
66 MathCursor * mathcursor = 0;
77 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
79 if (!bv->insertInset(new_inset)) {
83 new_inset->edit(bv, true);
92 InsetFormulaBase::InsetFormulaBase()
93 : font_(), xo_(0), yo_(0)
95 // This is needed as long the math parser is not re-entrant
97 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
98 //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
99 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
100 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
108 // translate to latex
110 latex(NULL, os, false, false);
111 string str = os.str();
114 LyXText * lt = view_->getLyXText();
115 string::const_iterator cit = str.begin();
116 string::const_iterator end = str.end();
117 for (; cit != end; ++cit)
118 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
121 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
126 void InsetFormulaBase::handleFont
127 (BufferView * bv, string const & arg, string const & font)
129 bv->lockedInsetStoreUndo(Undo::EDIT);
130 bool sel = mathcursor->selection();
132 updateLocal(bv, true);
133 mathcursor->handleNest(createMathInset(font));
134 mathcursor->insert(arg);
136 updateLocal(bv, false);
140 // Check if uses AMS macros
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
145 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
152 void InsetFormulaBase::metrics(BufferView * bv) const
155 view_ = bv->owner()->view();
157 mi.base.style = 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;
174 releaseMathCursor(bv);
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 << "Cannot lock math inset in edit call!\n";
190 releaseMathCursor(bv);
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);
204 releaseMathCursor(bv);
207 bv->updateInset(this, false);
211 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
213 // calling metrics here destroys the cached xo,yo positions e.g. in
214 // MathParboxinset. And it would be too expensive anyway...
216 mathcursor->getPos(x, y);
219 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
226 if (isCursorVisible())
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
235 if (isCursorVisible())
239 getCursorPos(bv, x, y);
240 math_font_max_dim(font_, asc, des);
241 bv->showLockedInsetCursor(x, y, asc, des);
242 setCursorVisible(true);
243 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
247 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
249 if (!isCursorVisible())
251 bv->hideLockedInsetCursor();
252 setCursorVisible(false);
253 //lyxerr << "hideInsetCursor: \n";
257 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
262 math_font_max_dim(font_, asc, des);
263 getCursorPos(bv, x, y);
265 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
266 bv->fitLockedInsetCursor(x, y, asc, des);
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
273 bv->updateInset(this, false);
277 vector<string> const InsetFormulaBase::getLabelList() const
279 return vector<string>();
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
285 bv->updateInset(this, dirty);
289 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
290 int /*x*/, int /*y*/, mouse_button::state button)
295 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
298 bv->updateInset(this, false);
300 if (button == mouse_button::button3) {
301 // try to dispatch to enclosed insets first
302 if (mathcursor->dispatch("mouse-3-release"))
305 // launch math panel for right mouse button
306 bv->owner()->getDialogs()->showMathPanel();
310 if (button == mouse_button::button1) {
311 // try to dispatch to enclosed insets first
312 if (mathcursor->dispatch("mouse-1-release"))
315 // try to set the cursor
317 //mathcursor = new MathCursor(this, x == 0);
319 //mathcursor->setPos(x + xo_, y + yo_);
326 void InsetFormulaBase::insetButtonPress(BufferView * bv,
327 int x, int y, mouse_button::state button)
329 //lyxerr << "insetButtonPress: "
330 // << x << " " << y << " but: " << button << "\n";
331 //lyxerr << "formula: ";
334 releaseMathCursor(bv);
335 mathcursor = new MathCursor(this, x == 0);
337 if (button == mouse_button::button1) {
338 // just set the cursor here
339 //lyxerr << "setting cursor\n";
343 mathcursor->selClear();
344 mathcursor->setPos(x + xo_, y + yo_);
346 if (mathcursor->dispatch("mouse-1-press")) {
352 if (button == mouse_button::button3) {
353 if (mathcursor->dispatch("mouse-3-press")) {
358 bv->updateInset(this, false);
362 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
363 int x, int y, mouse_button::state button)
368 if (button == mouse_button::button1)
369 if (mathcursor->dispatch("mouse-1-motion"))
372 if (button == mouse_button::button3)
373 if (mathcursor->dispatch("mouse-3-motion"))
376 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
377 //lyxerr << "insetMotionNotify: ignored\n";
383 if (!mathcursor->selection())
384 mathcursor->selStart();
386 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
387 // << ' ' << button << "\n";
389 mathcursor->setPos(x + xo_, y + yo_);
391 bv->updateInset(this, false);
395 UpdatableInset::RESULT
396 InsetFormulaBase::localDispatch(BufferView * bv, FuncRequest const & ev)
398 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
399 // << " arg: '" << arg
400 // << "' cursor: " << mathcursor << "\n";
405 string argument = ev.argument;
406 RESULT result = DISPATCHED;
408 bool was_macro = mathcursor->inMacroMode();
409 bool was_selection = mathcursor->selection();
413 mathcursor->normalize();
418 case LFUN_WORDRIGHTSEL:
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());
430 case LFUN_WORDLEFTSEL:
432 sel = true; // fall through
434 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
435 updateLocal(bv, false);
439 sel = true; // fall through
441 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
442 updateLocal(bv, false);
446 sel = true; // fall through
448 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
449 updateLocal(bv, false);
453 sel = true; // fall through
455 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
456 updateLocal(bv, false);
460 sel = true; // fall through
462 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
463 updateLocal(bv, false);
468 result = FINISHED_UP;
469 updateLocal(bv, false);
474 result = FINISHED_DOWN;
475 updateLocal(bv, false);
478 case LFUN_DELETE_LINE_FORWARD:
479 bv->lockedInsetStoreUndo(Undo::DELETE);
480 mathcursor->delLine();
481 updateLocal(bv, true);
485 mathcursor->idxNext();
486 updateLocal(bv, false);
490 mathcursor->idxPrev();
491 updateLocal(bv, false);
495 bv->lockedInsetStoreUndo(Undo::EDIT);
496 mathcursor->splitCell();
497 updateLocal(bv, true);
500 case LFUN_DELETE_WORD_BACKWARD:
502 bv->lockedInsetStoreUndo(Undo::DELETE);
503 mathcursor->backspace();
504 updateLocal(bv, true);
507 case LFUN_DELETE_WORD_FORWARD:
509 bv->lockedInsetStoreUndo(Undo::DELETE);
511 bv->updateInset(this, true);
515 // sprintf(dispatch_buffer, "%d %d",);
516 // dispatch_result = dispatch_buffer;
519 lyxerr << "LFUN_SETXY broken!\n";
522 istringstream is(ev.argument.c_str());
524 mathcursor->setPos(x, y);
525 updateLocal(bv, false);
531 mathcursor->macroModeClose();
532 bv->lockedInsetStoreUndo(Undo::EDIT);
533 mathcursor->selPaste();
534 updateLocal(bv, true);
538 bv->lockedInsetStoreUndo(Undo::DELETE);
539 mathcursor->selCut();
540 updateLocal(bv, true);
544 mathcursor->selCopy();
548 // Special casing for superscript in case of LyX handling
550 case LFUN_CIRCUMFLEX:
551 if (ev.argument.empty()) {
552 // do superscript if LyX handles
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 mathcursor->script(true);
556 updateLocal(bv, true);
573 case LFUN_HUNG_UMLAUT:
577 case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
578 case LFUN_BOLD: handleFont(bv, ev.argument, "textbf"); break;
579 case LFUN_SANS: handleFont(bv, ev.argument, "textsf"); break;
580 case LFUN_EMPH: handleFont(bv, ev.argument, "mathcal"); break;
581 case LFUN_ROMAN: handleFont(bv, ev.argument, "mathrm"); break;
582 case LFUN_CODE: handleFont(bv, ev.argument, "texttt"); break;
583 case LFUN_FRAK: handleFont(bv, ev.argument, "mathfrak"); break;
584 case LFUN_ITAL: handleFont(bv, ev.argument, "mathit"); break;
585 case LFUN_NOUN: handleFont(bv, ev.argument, "mathbb"); break;
586 case LFUN_DEFAULT: handleFont(bv, ev.argument, "textnormal"); break;
587 case LFUN_FREE: handleFont(bv, ev.argument, "textrm"); break;
590 handleFont(bv, ev.argument, "lyxgreek1");
591 if (ev.argument.size())
592 mathcursor->interpret(ev.argument);
596 if (mathcursor->currentMode()) {
597 handleFont(bv, ev.argument, "textrm");
599 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
600 updateLocal(bv, true);
602 //bv->owner()->message(_("math text mode toggled"));
605 case LFUN_MATH_LIMITS:
606 bv->lockedInsetStoreUndo(Undo::EDIT);
607 if (mathcursor->toggleLimits())
608 updateLocal(bv, true);
614 bv->lockedInsetStoreUndo(Undo::EDIT);
615 mathcursor->setSize(arg);
616 updateLocal(bv, true);
621 case LFUN_INSERT_MATRIX:
622 if (!ev.argument.empty()) {
623 bv->lockedInsetStoreUndo(Undo::EDIT);
624 mathcursor->interpret("matrix " + ev.argument);
625 updateLocal(bv, true);
629 case LFUN_SUPERSCRIPT:
632 bv->lockedInsetStoreUndo(Undo::EDIT);
633 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
634 updateLocal(bv, true);
638 case LFUN_MATH_DELIM:
640 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
642 string rs = split(ev.argument, ls, ' ');
643 // Reasonable default values
649 bv->lockedInsetStoreUndo(Undo::EDIT);
650 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
651 updateLocal(bv, true);
655 case LFUN_PROTECTEDSPACE:
656 case LFUN_MATH_SPACE:
657 bv->lockedInsetStoreUndo(Undo::EDIT);
658 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
659 updateLocal(bv, true);
663 bv->owner()->message(_("Invalid action in math mode!"));
667 case LFUN_MATH_HALIGN:
668 case LFUN_MATH_VALIGN:
669 case LFUN_MATH_ROW_INSERT:
670 case LFUN_MATH_ROW_DELETE:
671 case LFUN_MATH_COLUMN_INSERT:
672 case LFUN_MATH_COLUMN_DELETE:
674 MathInset::idx_type idx = 0;
675 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
677 mathcursor->popToEnclosingGrid();
678 bv->lockedInsetStoreUndo(Undo::EDIT);
679 char align = ev.argument.size() ? ev.argument[0] : 'c';
681 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
682 case LFUN_MATH_VALIGN: p->valign(align); break;
683 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
684 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
685 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
686 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
689 updateLocal(bv, true);
694 case LFUN_EXEC_COMMAND:
695 result = UNDISPATCHED;
699 // interpret this as if a backslash was typed
700 bv->lockedInsetStoreUndo(Undo::EDIT);
701 mathcursor->interpret('\\');
702 updateLocal(bv, true);
705 case LFUN_BREAKPARAGRAPH:
706 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
707 case LFUN_BREAKPARAGRAPH_SKIP:
712 case LFUN_INSERT_MATH:
713 case LFUN_SELFINSERT:
714 if (!argument.empty()) {
715 bv->lockedInsetStoreUndo(Undo::EDIT);
716 if (argument.size() == 1)
717 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
719 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
720 updateLocal(bv, true);
724 case LFUN_MATH_PANEL:
725 result = UNDISPATCHED;
729 if (mathcursor->selection())
730 mathcursor->selClear();
732 result = UNDISPATCHED;
735 case LFUN_INSET_TOGGLE:
736 mathcursor->insetToggle();
737 updateLocal(bv, true);
740 case LFUN_REF_INSERT:
741 //if (argument.empty()) {
742 // InsetCommandParams p("ref");
743 // owner_->getDialogs()->createRef(p.getAsString());
745 // InsetCommandParams p;
746 // p.setFromString(argument);
748 // InsetRef * inset = new InsetRef(p, *buffer_);
749 // if (!insertInset(inset))
752 // updateInset(inset, true);
755 if (ev.argument.empty()) {
756 InsetCommandParams p("ref");
757 bv->owner()->getDialogs()->createRef(p.getAsString());
759 //mathcursor->handleNest(new InsetRef2);
760 //mathcursor->insert(arg);
761 mathcursor->insert(MathAtom(new RefInset(ev.argument)));
763 updateLocal(bv, true);
767 result = UNDISPATCHED;
770 mathcursor->normalize();
773 lyx::Assert(mathcursor);
775 if (mathcursor->selection() || was_selection)
776 toggleInsetSelection(bv);
778 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
779 result == UNDISPATCHED) {
784 releaseMathCursor(bv);
785 bv->unlockInset(this);
788 return result; // original version
792 void InsetFormulaBase::revealCodes(BufferView * bv) const
796 bv->owner()->message(mathcursor->info());
799 // write something to the minibuffer
800 // translate to latex
801 mathcursor->markInsert();
804 string str = os.str();
805 mathcursor->markErase();
806 string::size_type pos = 0;
808 for (string::iterator it = str.begin(); it != str.end(); ++it) {
811 else if (*it == '\0') {
813 pos = it - str.begin();
819 res = res.substr(pos - 30);
821 res = res.substr(0, 60);
822 bv->owner()->message(res);
827 Inset::Code InsetFormulaBase::lyxCode() const
829 return Inset::MATH_CODE;
833 int InsetFormulaBase::ylow() const
835 return yo_ - ascent(view(), font_);
839 int InsetFormulaBase::yhigh() const
841 return yo_ + descent(view(), font_);
845 int InsetFormulaBase::xlow() const
851 int InsetFormulaBase::xhigh() const
853 return xo_ + width(view(), font_);
857 /////////////////////////////////////////////////////////////////////
860 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
866 static InsetFormulaBase * lastformula = 0;
867 static MathIterator current = MathIterator(ibegin(par().nucleus()));
869 static string laststr;
871 if (lastformula != this || laststr != str) {
872 //lyxerr << "reset lastformula to " << this << "\n";
875 current = ibegin(par().nucleus());
877 mathed_parse_cell(ar, str);
881 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
883 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
884 if (it.cell().matchpart(ar, it.back().pos_)) {
885 bv->unlockInset(bv->theLockingInset());
886 if (!bv->lockInset(this)) {
887 lyxerr << "Cannot lock inset" << endl;
891 mathcursor = new MathCursor(this, true);
893 mathcursor->setSelection(it, ar.size());
896 updateLocal(bv, false);
901 //lyxerr << "not found!\n";
907 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
910 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
911 return searchForward(bv, what, a, b);
915 /////////////////////////////////////////////////////////////////////
918 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
920 if (!bv->available())
923 // use selection if available..
925 //if (action == LFUN_MATH_IMPORT_SELECTION)
929 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
932 InsetFormula * f = new InsetFormula(bv);
933 if (openNewInset(bv, f)) {
935 // don't do that also for LFUN_MATH_MODE unless you want end up with
936 // always changing to mathrm when opening an inlined inset
937 // -- I really hate "LyXfunc overloading"...
939 f->localDispatch(bv, FuncRequest(LFUN_MATH_DISPLAY));
940 f->localDispatch(bv, FuncRequest(LFUN_INSERT_MATH, arg));
943 // create a macro if we see "\\newcommand" somewhere, and an ordinary
945 InsetFormulaBase * f;
946 if (sel.find("\\newcommand") == string::npos &&
947 sel.find("\\def") == string::npos)
948 f = new InsetFormula(sel);
950 f = new InsetFormulaMacro(sel);
951 bv->getLyXText()->cutSelection(bv);
954 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
958 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
960 mathDispatchCreation(bv, arg, true);
964 void mathDispatchMathMode(BufferView * bv, string const & arg)
966 mathDispatchCreation(bv, arg, false);
970 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
972 mathDispatchCreation(bv, arg, true);
976 void mathDispatchMathMacro(BufferView * bv, string const & arg)
978 if (!bv->available())
981 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
984 string const s1 = token(s, ' ', 1);
985 int const na = s1.empty() ? 0 : lyx::atoi(s1);
986 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
991 void mathDispatchMathDelim(BufferView * bv, string const & arg)
993 if (!bv->available())
995 InsetFormula * f = new InsetFormula(bv);
996 if (openNewInset(bv, f)) {
998 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_MATH_DELIM, arg));
1003 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1005 if (!bv->available())
1007 InsetFormula * f = new InsetFormula(bv);
1008 if (openNewInset(bv, f)) {
1009 f->mutate("simple");
1010 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_INSERT_MATRIX, arg));
1015 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1017 if (!bv->available())
1019 InsetFormula * f = new InsetFormula(bv);
1020 if (openNewInset(bv, f)) {
1021 f->mutate("simple");
1022 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_INSERT_MATH, arg));
1027 void mathDispatchGreek(BufferView * bv, string const & arg)
1029 if (!bv->available())
1031 InsetFormula * f = new InsetFormula(bv);
1032 if (openNewInset(bv, f)) {
1033 f->mutate("simple");
1034 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_GREEK, arg));
1040 void mathDispatch(BufferView *, kb_action, string const &)
1044 void mathDispatch(BufferView *, string const &)