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 = 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);
208 bv->updateInset(this, false);
212 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
214 // calling metrics here destroys the cached xo,yo positions e.g. in
215 // MathParboxinset. And it would be too expensive anyway...
217 mathcursor->getPos(x, y);
220 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
224 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
226 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
227 if (isCursorVisible())
234 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
236 if (isCursorVisible())
240 getCursorPos(bv, x, y);
241 math_font_max_dim(font_, asc, des);
242 bv->showLockedInsetCursor(x, y, asc, des);
243 setCursorVisible(true);
244 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
248 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
250 if (!isCursorVisible())
252 bv->hideLockedInsetCursor();
253 setCursorVisible(false);
254 //lyxerr << "hideInsetCursor: \n";
258 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
263 math_font_max_dim(font_, asc, des);
264 getCursorPos(bv, x, y);
266 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
267 bv->fitLockedInsetCursor(x, y, asc, des);
271 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
274 bv->updateInset(this, false);
278 vector<string> const InsetFormulaBase::getLabelList() const
280 return vector<string>();
284 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
289 bv->updateInset(this, dirty);
293 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
294 int /*x*/, int /*y*/, mouse_button::state button)
299 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
302 bv->updateInset(this, false);
304 if (button == mouse_button::button3) {
305 // try to dispatch to enclosed insets first
306 if (mathcursor->dispatch("mouse-3-release"))
309 // launch math panel for right mouse button
310 bv->owner()->getDialogs()->showMathPanel();
314 if (button == mouse_button::button1) {
315 // try to dispatch to enclosed insets first
316 if (mathcursor->dispatch("mouse-1-release"))
319 // try to set the cursor
321 //mathcursor = new MathCursor(this, x == 0);
323 //mathcursor->setPos(x + xo_, y + yo_);
330 void InsetFormulaBase::insetButtonPress(BufferView * bv,
331 int x, int y, mouse_button::state button)
333 //lyxerr << "insetButtonPress: "
334 // << x << " " << y << " but: " << button << "\n";
335 //lyxerr << "formula: ";
338 releaseMathCursor(bv);
339 mathcursor = new MathCursor(this, x == 0);
341 if (button == mouse_button::button1) {
342 // just set the cursor here
343 lyxerr << "setting cursor\n";
347 mathcursor->selClear();
348 mathcursor->setPos(x + xo_, y + yo_);
350 if (mathcursor->dispatch("mouse-1-press")) {
356 if (button == mouse_button::button3) {
357 if (mathcursor->dispatch("mouse-3-press")) {
362 bv->updateInset(this, false);
366 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
367 int x, int y, mouse_button::state button)
372 if (button == mouse_button::button1)
373 if (mathcursor->dispatch("mouse-1-motion"))
376 if (button == mouse_button::button3)
377 if (mathcursor->dispatch("mouse-3-motion"))
380 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
381 //lyxerr << "insetMotionNotify: ignored\n";
387 if (!mathcursor->selection())
388 mathcursor->selStart();
390 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
391 // << ' ' << button << "\n";
393 mathcursor->setPos(x + xo_, y + yo_);
395 bv->updateInset(this, false);
399 UpdatableInset::RESULT
400 InsetFormulaBase::localDispatch(BufferView * bv, FuncRequest const & ev)
402 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
403 // << " arg: '" << arg
404 // << "' cursor: " << mathcursor << "\n";
409 string argument = ev.argument;
410 RESULT result = DISPATCHED;
412 bool was_macro = mathcursor->inMacroMode();
413 bool was_selection = mathcursor->selection();
417 mathcursor->normalize();
422 case LFUN_WORDRIGHTSEL:
424 sel = true; // fall through...
426 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
427 //lyxerr << "calling scroll 20\n";
429 updateLocal(bv, false);
430 // write something to the minibuffer
431 //bv->owner()->message(mathcursor->info());
434 case LFUN_WORDLEFTSEL:
436 sel = true; // fall through
438 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
439 updateLocal(bv, false);
443 sel = true; // fall through
445 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
446 updateLocal(bv, false);
450 sel = true; // fall through
452 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
453 updateLocal(bv, false);
457 sel = true; // fall through
459 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
460 updateLocal(bv, false);
464 sel = true; // fall through
466 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
467 updateLocal(bv, false);
472 result = FINISHED_UP;
473 updateLocal(bv, false);
478 result = FINISHED_DOWN;
479 updateLocal(bv, false);
482 case LFUN_DELETE_LINE_FORWARD:
483 bv->lockedInsetStoreUndo(Undo::DELETE);
484 mathcursor->delLine();
485 updateLocal(bv, true);
489 mathcursor->idxNext();
490 updateLocal(bv, false);
494 mathcursor->idxPrev();
495 updateLocal(bv, false);
499 bv->lockedInsetStoreUndo(Undo::EDIT);
500 mathcursor->splitCell();
501 updateLocal(bv, true);
504 case LFUN_DELETE_WORD_BACKWARD:
506 bv->lockedInsetStoreUndo(Undo::DELETE);
507 mathcursor->backspace();
508 updateLocal(bv, true);
511 case LFUN_DELETE_WORD_FORWARD:
513 bv->lockedInsetStoreUndo(Undo::DELETE);
515 bv->updateInset(this, true);
519 // sprintf(dispatch_buffer, "%d %d",);
520 // dispatch_result = dispatch_buffer;
523 lyxerr << "LFUN_SETXY broken!\n";
526 istringstream is(ev.argument.c_str());
528 mathcursor->setPos(x, y);
529 updateLocal(bv, false);
535 mathcursor->macroModeClose();
536 bv->lockedInsetStoreUndo(Undo::EDIT);
537 mathcursor->selPaste();
538 updateLocal(bv, true);
542 bv->lockedInsetStoreUndo(Undo::DELETE);
543 mathcursor->selCut();
544 updateLocal(bv, true);
548 mathcursor->selCopy();
552 // Special casing for superscript in case of LyX handling
554 case LFUN_CIRCUMFLEX:
555 if (ev.argument.empty()) {
556 // do superscript if LyX handles
558 bv->lockedInsetStoreUndo(Undo::EDIT);
559 mathcursor->script(true);
560 updateLocal(bv, true);
577 case LFUN_HUNG_UMLAUT:
581 case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
582 case LFUN_BOLD: handleFont(bv, ev.argument, "textbf"); break;
583 case LFUN_SANS: handleFont(bv, ev.argument, "textsf"); break;
584 case LFUN_EMPH: handleFont(bv, ev.argument, "mathcal"); break;
585 case LFUN_ROMAN: handleFont(bv, ev.argument, "mathrm"); break;
586 case LFUN_CODE: handleFont(bv, ev.argument, "texttt"); break;
587 case LFUN_FRAK: handleFont(bv, ev.argument, "mathfrak"); break;
588 case LFUN_ITAL: handleFont(bv, ev.argument, "mathit"); break;
589 case LFUN_NOUN: handleFont(bv, ev.argument, "mathbb"); break;
590 case LFUN_DEFAULT: handleFont(bv, ev.argument, "textnormal"); break;
591 case LFUN_FREE: handleFont(bv, ev.argument, "textrm"); break;
594 handleFont(bv, ev.argument, "lyxgreek1");
595 if (ev.argument.size())
596 mathcursor->interpret(ev.argument);
600 if (mathcursor->currentMode()) {
601 handleFont(bv, ev.argument, "textrm");
603 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
604 updateLocal(bv, true);
606 //bv->owner()->message(_("math text mode toggled"));
609 case LFUN_MATH_LIMITS:
610 bv->lockedInsetStoreUndo(Undo::EDIT);
611 if (mathcursor->toggleLimits())
612 updateLocal(bv, true);
618 bv->lockedInsetStoreUndo(Undo::EDIT);
619 mathcursor->setSize(arg);
620 updateLocal(bv, true);
625 case LFUN_INSERT_MATRIX:
626 if (!ev.argument.empty()) {
627 bv->lockedInsetStoreUndo(Undo::EDIT);
628 mathcursor->interpret("matrix " + ev.argument);
629 updateLocal(bv, true);
633 case LFUN_SUPERSCRIPT:
636 bv->lockedInsetStoreUndo(Undo::EDIT);
637 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
638 updateLocal(bv, true);
642 case LFUN_MATH_DELIM:
644 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
646 string rs = split(ev.argument, ls, ' ');
647 // Reasonable default values
653 bv->lockedInsetStoreUndo(Undo::EDIT);
654 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
655 updateLocal(bv, true);
659 case LFUN_PROTECTEDSPACE:
660 case LFUN_MATH_SPACE:
661 bv->lockedInsetStoreUndo(Undo::EDIT);
662 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
663 updateLocal(bv, true);
667 bv->owner()->message(_("Invalid action in math mode!"));
671 case LFUN_MATH_HALIGN:
672 case LFUN_MATH_VALIGN:
673 case LFUN_MATH_ROW_INSERT:
674 case LFUN_MATH_ROW_DELETE:
675 case LFUN_MATH_COLUMN_INSERT:
676 case LFUN_MATH_COLUMN_DELETE:
678 MathInset::idx_type idx = 0;
679 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
681 mathcursor->popToEnclosingGrid();
682 bv->lockedInsetStoreUndo(Undo::EDIT);
683 char align = ev.argument.size() ? ev.argument[0] : 'c';
685 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
686 case LFUN_MATH_VALIGN: p->valign(align); break;
687 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
688 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
689 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
690 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
693 updateLocal(bv, true);
698 case LFUN_EXEC_COMMAND:
699 result = UNDISPATCHED;
703 // interpret this as if a backslash was typed
704 bv->lockedInsetStoreUndo(Undo::EDIT);
705 mathcursor->interpret('\\');
706 updateLocal(bv, true);
709 case LFUN_BREAKPARAGRAPH:
710 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
711 case LFUN_BREAKPARAGRAPH_SKIP:
716 case LFUN_INSERT_MATH:
717 case LFUN_SELFINSERT:
718 if (!argument.empty()) {
719 bv->lockedInsetStoreUndo(Undo::EDIT);
720 if (argument.size() == 1)
721 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
723 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
724 updateLocal(bv, true);
728 case LFUN_MATH_PANEL:
729 result = UNDISPATCHED;
733 if (mathcursor->selection())
734 mathcursor->selClear();
736 result = UNDISPATCHED;
739 case LFUN_INSET_TOGGLE:
740 mathcursor->insetToggle();
741 updateLocal(bv, true);
744 case LFUN_REF_INSERT:
745 //if (argument.empty()) {
746 // InsetCommandParams p("ref");
747 // owner_->getDialogs()->createRef(p.getAsString());
749 // InsetCommandParams p;
750 // p.setFromString(argument);
752 // InsetRef * inset = new InsetRef(p, *buffer_);
753 // if (!insertInset(inset))
756 // updateInset(inset, true);
759 if (ev.argument.empty()) {
760 InsetCommandParams p("ref");
761 bv->owner()->getDialogs()->createRef(p.getAsString());
763 //mathcursor->handleNest(new InsetRef2);
764 //mathcursor->insert(arg);
765 mathcursor->insert(MathAtom(new RefInset(ev.argument)));
767 updateLocal(bv, true);
771 result = UNDISPATCHED;
774 mathcursor->normalize();
777 lyx::Assert(mathcursor);
779 if (mathcursor->selection() || was_selection)
780 toggleInsetSelection(bv);
782 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
783 result == UNDISPATCHED) {
788 releaseMathCursor(bv);
789 bv->unlockInset(this);
792 return result; // original version
796 void InsetFormulaBase::revealCodes(BufferView * bv) const
800 bv->owner()->message(mathcursor->info());
803 // write something to the minibuffer
804 // translate to latex
805 mathcursor->markInsert();
808 string str = os.str();
809 mathcursor->markErase();
810 string::size_type pos = 0;
812 for (string::iterator it = str.begin(); it != str.end(); ++it) {
815 else if (*it == '\0') {
817 pos = it - str.begin();
823 res = res.substr(pos - 30);
825 res = res.substr(0, 60);
826 bv->owner()->message(res);
831 Inset::Code InsetFormulaBase::lyxCode() const
833 return Inset::MATH_CODE;
837 int InsetFormulaBase::ylow() const
839 return yo_ - ascent(view(), font_);
843 int InsetFormulaBase::yhigh() const
845 return yo_ + descent(view(), font_);
849 int InsetFormulaBase::xlow() const
855 int InsetFormulaBase::xhigh() const
857 return xo_ + width(view(), font_);
861 /////////////////////////////////////////////////////////////////////
864 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
870 static InsetFormulaBase * lastformula = 0;
871 static MathIterator current = MathIterator(ibegin(par().nucleus()));
873 static string laststr;
875 if (lastformula != this || laststr != str) {
876 //lyxerr << "reset lastformula to " << this << "\n";
879 current = ibegin(par().nucleus());
881 mathed_parse_cell(ar, str);
885 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
887 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
888 if (it.cell().matchpart(ar, it.back().pos_)) {
889 bv->unlockInset(bv->theLockingInset());
890 if (!bv->lockInset(this)) {
891 lyxerr << "Cannot lock inset" << endl;
895 mathcursor = new MathCursor(this, true);
897 mathcursor->setSelection(it, ar.size());
900 updateLocal(bv, false);
905 //lyxerr << "not found!\n";
911 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
914 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
915 return searchForward(bv, what, a, b);
919 /////////////////////////////////////////////////////////////////////
922 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
924 if (!bv->available())
927 // use selection if available..
929 //if (action == LFUN_MATH_IMPORT_SELECTION)
933 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
936 InsetFormula * f = new InsetFormula(bv);
937 if (openNewInset(bv, f)) {
939 // don't do that also for LFUN_MATH_MODE unless you want end up with
940 // always changing to mathrm when opening an inlined inset
941 // -- I really hate "LyXfunc overloading"...
943 f->localDispatch(bv, FuncRequest(LFUN_MATH_DISPLAY));
944 f->localDispatch(bv, FuncRequest(LFUN_INSERT_MATH, arg));
947 // create a macro if we see "\\newcommand" somewhere, and an ordinary
949 InsetFormulaBase * f;
950 if (sel.find("\\newcommand") == string::npos &&
951 sel.find("\\def") == string::npos)
952 f = new InsetFormula(sel);
954 f = new InsetFormulaMacro(sel);
955 bv->getLyXText()->cutSelection(bv);
958 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
962 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
964 mathDispatchCreation(bv, arg, true);
968 void mathDispatchMathMode(BufferView * bv, string const & arg)
970 mathDispatchCreation(bv, arg, false);
974 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
976 mathDispatchCreation(bv, arg, true);
980 void mathDispatchMathMacro(BufferView * bv, string const & arg)
982 if (!bv->available())
985 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
988 string const s1 = token(s, ' ', 1);
989 int const na = s1.empty() ? 0 : lyx::atoi(s1);
990 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
995 void mathDispatchMathDelim(BufferView * bv, string const & arg)
997 if (!bv->available())
999 InsetFormula * f = new InsetFormula(bv);
1000 if (openNewInset(bv, f)) {
1001 f->mutate("simple");
1002 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_MATH_DELIM, arg));
1007 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1009 if (!bv->available())
1011 InsetFormula * f = new InsetFormula(bv);
1012 if (openNewInset(bv, f)) {
1013 f->mutate("simple");
1014 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_INSERT_MATRIX, arg));
1019 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1021 if (!bv->available())
1023 InsetFormula * f = new InsetFormula(bv);
1024 if (openNewInset(bv, f)) {
1025 f->mutate("simple");
1026 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_INSERT_MATH, arg));
1031 void mathDispatchGreek(BufferView * bv, string const & arg)
1033 if (!bv->available())
1035 InsetFormula * f = new InsetFormula(bv);
1036 if (openNewInset(bv, f)) {
1037 f->mutate("simple");
1038 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_GREEK, arg));
1044 void mathDispatch(BufferView *, kb_action, string const &)
1048 void mathDispatch(BufferView *, string const &)