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_HALIGN:
420 case LFUN_MATH_VALIGN:
421 case LFUN_MATH_ROW_INSERT:
422 case LFUN_MATH_ROW_DELETE:
423 case LFUN_MATH_COLUMN_INSERT:
424 case LFUN_MATH_COLUMN_DELETE:
425 case LFUN_MATH_NUMBER:
426 case LFUN_MATH_NONUMBER:
429 case LFUN_DELETE_LINE_FORWARD:
430 case LFUN_INSERT_LABEL:
431 case LFUN_MATH_EXTERN:
432 bv->lockedInsetStoreUndo(Undo::EDIT);
433 mathcursor->dispatch(ev);
434 updateLocal(bv, true);
437 case LFUN_WORDRIGHTSEL:
439 sel = true; // fall through...
441 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
442 //lyxerr << "calling scroll 20\n";
444 updateLocal(bv, false);
445 // write something to the minibuffer
446 //bv->owner()->message(mathcursor->info());
449 case LFUN_WORDLEFTSEL:
451 sel = true; // fall through
453 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
454 updateLocal(bv, false);
458 sel = true; // fall through
460 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
461 updateLocal(bv, false);
465 sel = true; // fall through
467 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
468 updateLocal(bv, false);
472 sel = true; // fall through
474 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
475 updateLocal(bv, false);
479 sel = true; // fall through
481 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
482 updateLocal(bv, false);
487 result = FINISHED_UP;
488 updateLocal(bv, false);
493 result = FINISHED_DOWN;
494 updateLocal(bv, false);
498 mathcursor->idxNext();
499 updateLocal(bv, false);
503 mathcursor->idxPrev();
504 updateLocal(bv, false);
507 case LFUN_DELETE_WORD_BACKWARD:
509 bv->lockedInsetStoreUndo(Undo::DELETE);
510 mathcursor->backspace();
511 updateLocal(bv, true);
514 case LFUN_DELETE_WORD_FORWARD:
516 bv->lockedInsetStoreUndo(Undo::DELETE);
518 bv->updateInset(this, true);
522 // sprintf(dispatch_buffer, "%d %d",);
523 // dispatch_result = dispatch_buffer;
526 lyxerr << "LFUN_SETXY broken!\n";
529 istringstream is(ev.argument.c_str());
531 mathcursor->setPos(x, y);
532 updateLocal(bv, false);
538 mathcursor->macroModeClose();
539 bv->lockedInsetStoreUndo(Undo::EDIT);
540 mathcursor->selPaste();
541 updateLocal(bv, true);
545 bv->lockedInsetStoreUndo(Undo::DELETE);
546 mathcursor->selCut();
547 updateLocal(bv, true);
551 mathcursor->selCopy();
555 // Special casing for superscript in case of LyX handling
557 case LFUN_CIRCUMFLEX:
558 if (ev.argument.empty()) {
559 // do superscript if LyX handles
561 bv->lockedInsetStoreUndo(Undo::EDIT);
562 mathcursor->script(true);
563 updateLocal(bv, true);
580 case LFUN_HUNG_UMLAUT:
584 case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
585 case LFUN_BOLD: handleFont(bv, ev.argument, "textbf"); break;
586 case LFUN_SANS: handleFont(bv, ev.argument, "textsf"); break;
587 case LFUN_EMPH: handleFont(bv, ev.argument, "mathcal"); break;
588 case LFUN_ROMAN: handleFont(bv, ev.argument, "mathrm"); break;
589 case LFUN_CODE: handleFont(bv, ev.argument, "texttt"); break;
590 case LFUN_FRAK: handleFont(bv, ev.argument, "mathfrak"); break;
591 case LFUN_ITAL: handleFont(bv, ev.argument, "mathit"); break;
592 case LFUN_NOUN: handleFont(bv, ev.argument, "mathbb"); break;
593 case LFUN_DEFAULT: handleFont(bv, ev.argument, "textnormal"); break;
594 case LFUN_FREE: handleFont(bv, ev.argument, "textrm"); break;
597 handleFont(bv, ev.argument, "lyxgreek1");
598 if (ev.argument.size())
599 mathcursor->interpret(ev.argument);
603 if (mathcursor->currentMode()) {
604 handleFont(bv, ev.argument, "textrm");
606 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
607 updateLocal(bv, true);
609 //bv->owner()->message(_("math text mode toggled"));
612 case LFUN_MATH_LIMITS:
613 bv->lockedInsetStoreUndo(Undo::EDIT);
614 if (mathcursor->toggleLimits())
615 updateLocal(bv, true);
621 bv->lockedInsetStoreUndo(Undo::EDIT);
622 mathcursor->setSize(arg);
623 updateLocal(bv, true);
628 case LFUN_INSERT_MATRIX:
629 if (!ev.argument.empty()) {
630 bv->lockedInsetStoreUndo(Undo::EDIT);
631 mathcursor->interpret("matrix " + ev.argument);
632 updateLocal(bv, true);
636 case LFUN_SUPERSCRIPT:
639 bv->lockedInsetStoreUndo(Undo::EDIT);
640 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
641 updateLocal(bv, true);
645 case LFUN_MATH_DELIM:
647 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
649 string rs = split(ev.argument, ls, ' ');
650 // Reasonable default values
656 bv->lockedInsetStoreUndo(Undo::EDIT);
657 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
658 updateLocal(bv, true);
662 case LFUN_PROTECTEDSPACE:
663 case LFUN_MATH_SPACE:
664 bv->lockedInsetStoreUndo(Undo::EDIT);
665 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
666 updateLocal(bv, true);
670 bv->owner()->message(_("Invalid action in math mode!"));
674 case LFUN_EXEC_COMMAND:
675 result = UNDISPATCHED;
679 // interpret this as if a backslash was typed
680 bv->lockedInsetStoreUndo(Undo::EDIT);
681 mathcursor->interpret('\\');
682 updateLocal(bv, true);
685 case LFUN_BREAKPARAGRAPH:
686 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
687 case LFUN_BREAKPARAGRAPH_SKIP:
692 case LFUN_INSERT_MATH:
693 case LFUN_SELFINSERT:
694 if (!argument.empty()) {
695 bv->lockedInsetStoreUndo(Undo::EDIT);
696 if (argument.size() == 1)
697 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
699 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
700 updateLocal(bv, true);
704 case LFUN_MATH_PANEL:
705 result = UNDISPATCHED;
709 if (mathcursor->selection())
710 mathcursor->selClear();
712 result = UNDISPATCHED;
715 case LFUN_INSET_TOGGLE:
716 mathcursor->insetToggle();
717 updateLocal(bv, true);
720 case LFUN_REF_INSERT:
721 //if (argument.empty()) {
722 // InsetCommandParams p("ref");
723 // owner_->getDialogs().createRef(p.getAsString());
725 // InsetCommandParams p;
726 // p.setFromString(argument);
728 // InsetRef * inset = new InsetRef(p, *buffer_);
729 // if (!insertInset(inset))
732 // updateInset(inset, true);
735 if (ev.argument.empty()) {
736 InsetCommandParams p("ref");
737 bv->owner()->getDialogs().createRef(p.getAsString());
739 //mathcursor->handleNest(new InsetRef2);
740 //mathcursor->insert(arg);
741 mathcursor->insert(MathAtom(new RefInset(ev.argument)));
743 updateLocal(bv, true);
747 result = UNDISPATCHED;
750 mathcursor->normalize();
753 lyx::Assert(mathcursor);
755 if (mathcursor->selection() || was_selection)
756 toggleInsetSelection(bv);
758 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
759 result == UNDISPATCHED) {
764 releaseMathCursor(bv);
765 bv->unlockInset(this);
768 return result; // original version
772 void InsetFormulaBase::revealCodes(BufferView * bv) const
776 bv->owner()->message(mathcursor->info());
779 // write something to the minibuffer
780 // translate to latex
781 mathcursor->markInsert();
784 string str = os.str();
785 mathcursor->markErase();
786 string::size_type pos = 0;
788 for (string::iterator it = str.begin(); it != str.end(); ++it) {
791 else if (*it == '\0') {
793 pos = it - str.begin();
799 res = res.substr(pos - 30);
801 res = res.substr(0, 60);
802 bv->owner()->message(res);
807 Inset::Code InsetFormulaBase::lyxCode() const
809 return Inset::MATH_CODE;
813 int InsetFormulaBase::ylow() const
815 return yo_ - ascent(view(), font_);
819 int InsetFormulaBase::yhigh() const
821 return yo_ + descent(view(), font_);
825 int InsetFormulaBase::xlow() const
831 int InsetFormulaBase::xhigh() const
833 return xo_ + width(view(), font_);
837 /////////////////////////////////////////////////////////////////////
840 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
846 static InsetFormulaBase * lastformula = 0;
847 static MathIterator current = MathIterator(ibegin(par().nucleus()));
849 static string laststr;
851 if (lastformula != this || laststr != str) {
852 //lyxerr << "reset lastformula to " << this << "\n";
855 current = ibegin(par().nucleus());
857 mathed_parse_cell(ar, str);
861 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
863 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
864 if (it.cell().matchpart(ar, it.back().pos_)) {
865 bv->unlockInset(bv->theLockingInset());
866 if (!bv->lockInset(this)) {
867 lyxerr << "Cannot lock inset" << endl;
871 mathcursor = new MathCursor(this, true);
873 mathcursor->setSelection(it, ar.size());
876 updateLocal(bv, false);
881 //lyxerr << "not found!\n";
887 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
890 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
891 return searchForward(bv, what, a, b);
895 /////////////////////////////////////////////////////////////////////
898 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
900 if (!bv->available())
903 // use selection if available..
905 //if (action == LFUN_MATH_IMPORT_SELECTION)
909 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
912 InsetFormula * f = new InsetFormula(bv);
913 if (openNewInset(bv, f)) {
915 // don't do that also for LFUN_MATH_MODE unless you want end up with
916 // always changing to mathrm when opening an inlined inset
917 // -- I really hate "LyXfunc overloading"...
919 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
920 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, arg));
923 // create a macro if we see "\\newcommand" somewhere, and an ordinary
925 InsetFormulaBase * f;
926 if (sel.find("\\newcommand") == string::npos &&
927 sel.find("\\def") == string::npos)
928 f = new InsetFormula(sel);
930 f = new InsetFormulaMacro(sel);
931 bv->getLyXText()->cutSelection(bv);
934 bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
938 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
940 mathDispatchCreation(bv, arg, true);
944 void mathDispatchMathMode(BufferView * bv, string const & arg)
946 mathDispatchCreation(bv, arg, false);
950 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
952 mathDispatchCreation(bv, arg, true);
956 void mathDispatchMathMacro(BufferView * bv, string const & arg)
958 if (!bv->available())
961 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
964 string const s1 = token(s, ' ', 1);
965 int const na = s1.empty() ? 0 : lyx::atoi(s1);
966 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
971 void mathDispatchMathDelim(BufferView * bv, string const & arg)
973 if (!bv->available())
975 InsetFormula * f = new InsetFormula(bv);
976 if (openNewInset(bv, f)) {
978 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_MATH_DELIM, arg));
983 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
985 if (!bv->available())
987 InsetFormula * f = new InsetFormula(bv);
988 if (openNewInset(bv, f)) {
990 bv->theLockingInset()->localDispatch(FuncRequest(bv, LFUN_INSERT_MATRIX, arg));
995 void mathDispatchInsertMath(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(FuncRequest(bv, LFUN_INSERT_MATH, arg));
1007 void mathDispatchGreek(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(FuncRequest(bv, LFUN_GREEK, arg));
1020 void mathDispatch(BufferView *, kb_action, string const &)
1024 void mathDispatch(BufferView *, string const &)