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()->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(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 3)))
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(FuncRequest(bv, LFUN_MOUSE_RELEASE, x, y, 1)))
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(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 1))) {
352 if (button == mouse_button::button3) {
353 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_PRESS, x, y, 3))) {
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(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 1)))
372 if (button == mouse_button::button3)
373 if (mathcursor->dispatch(FuncRequest(bv, LFUN_MOUSE_MOTION, x, y, 3)))
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(FuncRequest const & ev)
398 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
399 // << " arg: '" << arg
400 // << "' cursor: " << mathcursor << "\n";
405 BufferView * bv = ev.view();
406 string argument = ev.argument;
407 RESULT result = DISPATCHED;
409 bool was_macro = mathcursor->inMacroMode();
410 bool was_selection = mathcursor->selection();
414 mathcursor->normalize();
419 case LFUN_MATH_MUTATE:
420 case LFUN_MATH_DISPLAY:
421 case LFUN_MATH_HALIGN:
422 case LFUN_MATH_VALIGN:
423 case LFUN_MATH_ROW_INSERT:
424 case LFUN_MATH_ROW_DELETE:
425 case LFUN_MATH_COLUMN_INSERT:
426 case LFUN_MATH_COLUMN_DELETE:
427 case LFUN_MATH_NUMBER:
428 case LFUN_MATH_NONUMBER:
431 case LFUN_DELETE_LINE_FORWARD:
432 case LFUN_INSERT_LABEL:
433 case LFUN_MATH_EXTERN:
434 bv->lockedInsetStoreUndo(Undo::EDIT);
435 mathcursor->dispatch(ev);
436 updateLocal(bv, true);
439 case LFUN_WORDRIGHTSEL:
441 sel = true; // fall through...
443 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
444 //lyxerr << "calling scroll 20\n";
446 updateLocal(bv, false);
447 // write something to the minibuffer
448 //bv->owner()->message(mathcursor->info());
451 case LFUN_WORDLEFTSEL:
453 sel = true; // fall through
455 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
456 updateLocal(bv, false);
460 sel = true; // fall through
462 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
463 updateLocal(bv, false);
467 sel = true; // fall through
469 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
470 updateLocal(bv, false);
474 sel = true; // fall through
476 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
477 updateLocal(bv, false);
481 sel = true; // fall through
483 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
484 updateLocal(bv, false);
489 result = FINISHED_UP;
490 updateLocal(bv, false);
495 result = FINISHED_DOWN;
496 updateLocal(bv, false);
500 mathcursor->idxNext();
501 updateLocal(bv, false);
505 mathcursor->idxPrev();
506 updateLocal(bv, false);
509 case LFUN_DELETE_WORD_BACKWARD:
511 bv->lockedInsetStoreUndo(Undo::DELETE);
512 mathcursor->backspace();
513 updateLocal(bv, true);
516 case LFUN_DELETE_WORD_FORWARD:
518 bv->lockedInsetStoreUndo(Undo::DELETE);
520 bv->updateInset(this, true);
524 // sprintf(dispatch_buffer, "%d %d",);
525 // dispatch_result = dispatch_buffer;
528 lyxerr << "LFUN_SETXY broken!\n";
531 istringstream is(ev.argument.c_str());
533 mathcursor->setPos(x, y);
534 updateLocal(bv, false);
539 case LFUN_PASTESELECTION: {
540 string const clip = bv->getClipboard();
542 mathed_parse_normal(par_, clip);
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();
566 // Special casing for superscript in case of LyX handling
568 case LFUN_CIRCUMFLEX:
569 if (ev.argument.empty()) {
570 // do superscript if LyX handles
572 bv->lockedInsetStoreUndo(Undo::EDIT);
573 mathcursor->script(true);
574 updateLocal(bv, true);
591 case LFUN_HUNG_UMLAUT:
595 case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
596 case LFUN_BOLD: handleFont(bv, ev.argument, "textbf"); break;
597 case LFUN_SANS: handleFont(bv, ev.argument, "textsf"); break;
598 case LFUN_EMPH: handleFont(bv, ev.argument, "mathcal"); break;
599 case LFUN_ROMAN: handleFont(bv, ev.argument, "mathrm"); break;
600 case LFUN_CODE: handleFont(bv, ev.argument, "texttt"); break;
601 case LFUN_FRAK: handleFont(bv, ev.argument, "mathfrak"); break;
602 case LFUN_ITAL: handleFont(bv, ev.argument, "mathit"); break;
603 case LFUN_NOUN: handleFont(bv, ev.argument, "mathbb"); break;
604 case LFUN_DEFAULT: handleFont(bv, ev.argument, "textnormal"); break;
605 case LFUN_FREE: handleFont(bv, ev.argument, "textrm"); break;
608 handleFont(bv, ev.argument, "lyxgreek1");
609 if (ev.argument.size())
610 mathcursor->interpret(ev.argument);
614 if (mathcursor->currentMode()) {
615 handleFont(bv, ev.argument, "textrm");
617 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
618 updateLocal(bv, true);
620 //bv->owner()->message(_("math text mode toggled"));
623 case LFUN_MATH_LIMITS:
624 bv->lockedInsetStoreUndo(Undo::EDIT);
625 if (mathcursor->toggleLimits())
626 updateLocal(bv, true);
632 bv->lockedInsetStoreUndo(Undo::EDIT);
633 mathcursor->setSize(arg);
634 updateLocal(bv, true);
639 case LFUN_INSERT_MATRIX:
640 if (!ev.argument.empty()) {
641 bv->lockedInsetStoreUndo(Undo::EDIT);
642 mathcursor->interpret("matrix " + ev.argument);
643 updateLocal(bv, true);
647 case LFUN_SUPERSCRIPT:
650 bv->lockedInsetStoreUndo(Undo::EDIT);
651 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
652 updateLocal(bv, true);
656 case LFUN_MATH_DELIM:
658 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
660 string rs = split(ev.argument, ls, ' ');
661 // Reasonable default values
667 bv->lockedInsetStoreUndo(Undo::EDIT);
668 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
669 updateLocal(bv, true);
673 case LFUN_PROTECTEDSPACE:
674 case LFUN_MATH_SPACE:
675 bv->lockedInsetStoreUndo(Undo::EDIT);
676 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
677 updateLocal(bv, true);
681 bv->owner()->message(_("Invalid action in math mode!"));
685 case LFUN_EXEC_COMMAND:
686 result = UNDISPATCHED;
690 // interpret this as if a backslash was typed
691 bv->lockedInsetStoreUndo(Undo::EDIT);
692 mathcursor->interpret('\\');
693 updateLocal(bv, true);
696 case LFUN_BREAKPARAGRAPH:
697 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
698 case LFUN_BREAKPARAGRAPH_SKIP:
703 case LFUN_INSERT_MATH:
704 case LFUN_SELFINSERT:
705 if (!argument.empty()) {
706 bv->lockedInsetStoreUndo(Undo::EDIT);
707 if (argument.size() == 1)
708 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
710 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
711 updateLocal(bv, true);
715 case LFUN_MATH_PANEL:
716 result = UNDISPATCHED;
720 if (mathcursor->selection())
721 mathcursor->selClear();
723 result = UNDISPATCHED;
726 case LFUN_INSET_TOGGLE:
727 mathcursor->insetToggle();
728 updateLocal(bv, true);
731 case LFUN_REF_INSERT:
732 //if (argument.empty()) {
733 // InsetCommandParams p("ref");
734 // owner_->getDialogs().createRef(p.getAsString());
736 // InsetCommandParams p;
737 // p.setFromString(argument);
739 // InsetRef * inset = new InsetRef(p, *buffer_);
740 // if (!insertInset(inset))
743 // updateInset(inset, true);
746 if (ev.argument.empty()) {
747 InsetCommandParams p("ref");
748 bv->owner()->getDialogs().createRef(p.getAsString());
750 //mathcursor->handleNest(new InsetRef2);
751 //mathcursor->insert(arg);
752 mathcursor->insert(MathAtom(new RefInset(ev.argument)));
754 updateLocal(bv, true);
758 result = UNDISPATCHED;
761 mathcursor->normalize();
764 lyx::Assert(mathcursor);
766 if (mathcursor->selection() || was_selection)
767 toggleInsetSelection(bv);
769 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
770 result == UNDISPATCHED) {
775 releaseMathCursor(bv);
776 bv->unlockInset(this);
779 return result; // original version
783 void InsetFormulaBase::revealCodes(BufferView * bv) const
787 bv->owner()->message(mathcursor->info());
790 // write something to the minibuffer
791 // translate to latex
792 mathcursor->markInsert();
795 string str = os.str();
796 mathcursor->markErase();
797 string::size_type pos = 0;
799 for (string::iterator it = str.begin(); it != str.end(); ++it) {
802 else if (*it == '\0') {
804 pos = it - str.begin();
810 res = res.substr(pos - 30);
812 res = res.substr(0, 60);
813 bv->owner()->message(res);
818 Inset::Code InsetFormulaBase::lyxCode() const
820 return Inset::MATH_CODE;
824 int InsetFormulaBase::ylow() const
826 return yo_ - ascent(view(), font_);
830 int InsetFormulaBase::yhigh() const
832 return yo_ + descent(view(), font_);
836 int InsetFormulaBase::xlow() const
842 int InsetFormulaBase::xhigh() const
844 return xo_ + width(view(), font_);
848 /////////////////////////////////////////////////////////////////////
851 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
857 static InsetFormulaBase * lastformula = 0;
858 static MathIterator current = MathIterator(ibegin(par().nucleus()));
860 static string laststr;
862 if (lastformula != this || laststr != str) {
863 //lyxerr << "reset lastformula to " << this << "\n";
866 current = ibegin(par().nucleus());
868 mathed_parse_cell(ar, str);
872 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
874 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
875 if (it.cell().matchpart(ar, it.back().pos_)) {
876 bv->unlockInset(bv->theLockingInset());
877 if (!bv->lockInset(this)) {
878 lyxerr << "Cannot lock inset" << endl;
882 mathcursor = new MathCursor(this, true);
884 mathcursor->setSelection(it, ar.size());
887 updateLocal(bv, false);
892 //lyxerr << "not found!\n";
898 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
901 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
902 return searchForward(bv, what, a, b);
906 bool InsetFormulaBase::display() const
908 return par()->asHullInset() && par()->asHullInset()->display();
912 /////////////////////////////////////////////////////////////////////
915 void mathDispatchCreation(FuncRequest const & cmd, bool display)
917 BufferView * bv = cmd.view();
918 // use selection if available..
920 //if (action == LFUN_MATH_IMPORT_SELECTION)
924 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
927 InsetFormula * f = new InsetFormula(bv);
928 if (openNewInset(bv, f)) {
929 bv->theLockingInset()->
930 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
931 // don't do that also for LFUN_MATH_MODE unless you want end up with
932 // always changing to mathrm when opening an inlined inset
933 // -- I really hate "LyXfunc overloading"...
935 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
936 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
939 // create a macro if we see "\\newcommand" somewhere, and an ordinary
941 InsetFormulaBase * f;
942 if (sel.find("\\newcommand") == string::npos &&
943 sel.find("\\def") == string::npos)
944 f = new InsetFormula(sel);
946 f = new InsetFormulaMacro(sel);
947 bv->getLyXText()->cutSelection(bv);
950 bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
954 void mathDispatch(FuncRequest const & cmd)
956 BufferView * bv = cmd.view();
957 if (!bv->available())
960 switch (cmd.action) {
962 case LFUN_MATH_DISPLAY:
963 mathDispatchCreation(cmd, true);
967 mathDispatchCreation(cmd, false);
970 case LFUN_MATH_IMPORT_SELECTION:
971 mathDispatchCreation(cmd, false);
974 case LFUN_MATH_MACRO:
975 if (cmd.argument.empty())
976 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
978 string s = cmd.argument;
979 string const s1 = token(s, ' ', 1);
980 int const na = s1.empty() ? 0 : lyx::atoi(s1);
981 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
986 case LFUN_INSERT_MATH:
987 case LFUN_INSERT_MATRIX:
988 case LFUN_MATH_DELIM: {
989 InsetFormula * f = new InsetFormula(bv);
990 if (openNewInset(bv, f)) {
991 bv->theLockingInset()->
992 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
993 bv->theLockingInset()->localDispatch(cmd);