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...
217 lyxerr << "getCursorPos - should not happen";
221 mathcursor->getPos(x, y);
224 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
228 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
230 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
231 if (isCursorVisible())
238 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
240 if (isCursorVisible())
244 getCursorPos(bv, x, y);
245 math_font_max_dim(font_, asc, des);
246 bv->showLockedInsetCursor(x, y, asc, des);
247 setCursorVisible(true);
248 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
252 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
254 if (!isCursorVisible())
256 bv->hideLockedInsetCursor();
257 setCursorVisible(false);
258 //lyxerr << "hideInsetCursor: \n";
262 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
267 math_font_max_dim(font_, asc, des);
268 getCursorPos(bv, x, y);
270 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
271 bv->fitLockedInsetCursor(x, y, asc, des);
275 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
278 bv->updateInset(this, false);
282 vector<string> const InsetFormulaBase::getLabelList() const
284 return vector<string>();
288 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
290 bv->updateInset(this, dirty);
294 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
299 BufferView * bv = cmd.view();
302 bv->updateInset(this, false);
304 if (cmd.button() == mouse_button::button3) {
305 // try to dispatch to enclosed insets first
306 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
307 // launch math panel for right mouse button
308 bv->owner()->getDialogs().showMathPanel();
313 if (cmd.button() == mouse_button::button1) {
314 // try to dispatch to enclosed insets first
315 mathcursor->dispatch(cmd);
316 // try to set the cursor
318 //mathcursor = new MathCursor(this, x == 0);
320 //mathcursor->setPos(x + xo_, y + yo_);
327 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
329 BufferView * bv = cmd.view();
330 releaseMathCursor(bv);
331 mathcursor = new MathCursor(this, cmd.x == 0);
333 if (cmd.button() == mouse_button::button1) {
334 // just set the cursor here
335 //lyxerr << "setting cursor\n";
339 mathcursor->selClear();
340 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
341 mathcursor->dispatch(cmd);
344 if (cmd.button() == mouse_button::button3) {
345 mathcursor->dispatch(cmd);
349 bv->updateInset(this, false);
354 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
359 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
362 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
363 //lyxerr << "insetMotionNotify: ignored\n";
369 if (!mathcursor->selection())
370 mathcursor->selStart();
372 BufferView * bv = cmd.view();
374 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
376 bv->updateInset(this, false);
381 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
383 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
384 // << " arg: '" << cmd.argument
385 // << " x: '" << cmd.x
386 // << " y: '" << cmd.y
387 // << "' button: " << cmd.button() << "\n";
389 switch (cmd.action) {
390 case LFUN_MOUSE_PRESS:
391 return lfunMousePress(cmd);
392 case LFUN_MOUSE_MOTION:
393 return lfunMouseMotion(cmd);
394 case LFUN_MOUSE_RELEASE:
395 return lfunMouseRelease(cmd);
403 BufferView * bv = cmd.view();
404 string argument = cmd.argument;
405 RESULT result = DISPATCHED;
407 bool was_macro = mathcursor->inMacroMode();
408 bool was_selection = mathcursor->selection();
412 mathcursor->normalize();
415 switch (cmd.action) {
417 case LFUN_MATH_MUTATE:
418 case LFUN_MATH_DISPLAY:
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(cmd);
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(cmd.argument.c_str());
531 mathcursor->setPos(x, y);
532 updateLocal(bv, false);
537 case LFUN_PASTESELECTION: {
538 string const clip = bv->getClipboard();
540 mathed_parse_normal(par_, clip);
547 mathcursor->macroModeClose();
548 bv->lockedInsetStoreUndo(Undo::EDIT);
549 mathcursor->selPaste();
550 updateLocal(bv, true);
554 bv->lockedInsetStoreUndo(Undo::DELETE);
555 mathcursor->selCut();
556 updateLocal(bv, true);
560 mathcursor->selCopy();
564 // Special casing for superscript in case of LyX handling
566 case LFUN_CIRCUMFLEX:
567 if (cmd.argument.empty()) {
568 // do superscript if LyX handles
570 bv->lockedInsetStoreUndo(Undo::EDIT);
571 mathcursor->script(true);
572 updateLocal(bv, true);
589 case LFUN_HUNG_UMLAUT:
593 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
594 case LFUN_BOLD: handleFont(bv, cmd.argument, "textbf"); break;
595 case LFUN_SANS: handleFont(bv, cmd.argument, "textsf"); break;
596 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
597 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
598 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
599 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
600 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
601 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
602 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
603 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
606 handleFont(bv, cmd.argument, "lyxgreek1");
607 if (cmd.argument.size())
608 mathcursor->interpret(cmd.argument);
612 if (mathcursor->currentMode())
613 handleFont(bv, cmd.argument, "textrm");
615 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
616 updateLocal(bv, true);
618 //bv->owner()->message(_("math text mode toggled"));
621 case LFUN_MATH_LIMITS:
622 bv->lockedInsetStoreUndo(Undo::EDIT);
623 if (mathcursor->toggleLimits())
624 updateLocal(bv, true);
630 bv->lockedInsetStoreUndo(Undo::EDIT);
631 mathcursor->setSize(arg);
632 updateLocal(bv, true);
637 case LFUN_INSERT_MATRIX:
638 if (!cmd.argument.empty()) {
639 bv->lockedInsetStoreUndo(Undo::EDIT);
640 mathcursor->interpret("matrix " + cmd.argument);
641 updateLocal(bv, true);
645 case LFUN_SUPERSCRIPT:
648 bv->lockedInsetStoreUndo(Undo::EDIT);
649 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
650 updateLocal(bv, true);
654 case LFUN_MATH_DELIM:
656 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
658 string rs = split(cmd.argument, ls, ' ');
659 // Reasonable default values
665 bv->lockedInsetStoreUndo(Undo::EDIT);
666 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
667 updateLocal(bv, true);
671 case LFUN_PROTECTEDSPACE:
672 case LFUN_MATH_SPACE:
673 bv->lockedInsetStoreUndo(Undo::EDIT);
674 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
675 updateLocal(bv, true);
679 bv->owner()->message(_("Invalid action in math mode!"));
683 case LFUN_EXEC_COMMAND:
684 result = UNDISPATCHED;
688 // interpret this as if a backslash was typed
689 bv->lockedInsetStoreUndo(Undo::EDIT);
690 mathcursor->interpret('\\');
691 updateLocal(bv, true);
694 case LFUN_BREAKPARAGRAPH:
695 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
696 case LFUN_BREAKPARAGRAPH_SKIP:
701 case LFUN_INSERT_MATH:
702 case LFUN_SELFINSERT:
703 if (!argument.empty()) {
704 bv->lockedInsetStoreUndo(Undo::EDIT);
705 if (argument.size() == 1)
706 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
708 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
709 updateLocal(bv, true);
713 case LFUN_MATH_PANEL:
714 result = UNDISPATCHED;
718 if (mathcursor->selection())
719 mathcursor->selClear();
721 result = UNDISPATCHED;
724 case LFUN_INSET_TOGGLE:
725 mathcursor->insetToggle();
726 updateLocal(bv, true);
729 case LFUN_REF_INSERT:
730 //if (argument.empty()) {
731 // InsetCommandParams p("ref");
732 // owner_->getDialogs().createRef(p.getAsString());
734 // InsetCommandParams p;
735 // p.setFromString(argument);
737 // InsetRef * inset = new InsetRef(p, *buffer_);
738 // if (!insertInset(inset))
741 // updateInset(inset, true);
744 if (cmd.argument.empty()) {
745 InsetCommandParams p("ref");
746 bv->owner()->getDialogs().createRef(p.getAsString());
748 //mathcursor->handleNest(new InsetRef2);
749 //mathcursor->insert(arg);
750 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
752 updateLocal(bv, true);
756 result = UNDISPATCHED;
759 mathcursor->normalize();
762 lyx::Assert(mathcursor);
764 if (mathcursor->selection() || was_selection)
765 toggleInsetSelection(bv);
767 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
768 result == UNDISPATCHED) {
773 releaseMathCursor(bv);
774 bv->unlockInset(this);
777 return result; // original version
781 void InsetFormulaBase::revealCodes(BufferView * bv) const
785 bv->owner()->message(mathcursor->info());
788 // write something to the minibuffer
789 // translate to latex
790 mathcursor->markInsert();
793 string str = os.str();
794 mathcursor->markErase();
795 string::size_type pos = 0;
797 for (string::iterator it = str.begin(); it != str.end(); ++it) {
800 else if (*it == '\0') {
802 pos = it - str.begin();
808 res = res.substr(pos - 30);
810 res = res.substr(0, 60);
811 bv->owner()->message(res);
816 Inset::Code InsetFormulaBase::lyxCode() const
818 return Inset::MATH_CODE;
822 int InsetFormulaBase::ylow() const
824 return yo_ - ascent(view(), font_);
828 int InsetFormulaBase::yhigh() const
830 return yo_ + descent(view(), font_);
834 int InsetFormulaBase::xlow() const
840 int InsetFormulaBase::xhigh() const
842 return xo_ + width(view(), font_);
846 /////////////////////////////////////////////////////////////////////
849 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
855 static InsetFormulaBase * lastformula = 0;
856 static MathIterator current = MathIterator(ibegin(par().nucleus()));
858 static string laststr;
860 if (lastformula != this || laststr != str) {
861 //lyxerr << "reset lastformula to " << this << "\n";
864 current = ibegin(par().nucleus());
866 mathed_parse_cell(ar, str);
870 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
872 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
873 if (it.cell().matchpart(ar, it.back().pos_)) {
874 bv->unlockInset(bv->theLockingInset());
875 if (!bv->lockInset(this)) {
876 lyxerr << "Cannot lock inset" << endl;
880 mathcursor = new MathCursor(this, true);
882 mathcursor->setSelection(it, ar.size());
885 updateLocal(bv, false);
890 //lyxerr << "not found!\n";
896 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
899 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
900 return searchForward(bv, what, a, b);
904 bool InsetFormulaBase::display() const
906 return par()->asHullInset() && par()->asHullInset()->display();
910 /////////////////////////////////////////////////////////////////////
913 void mathDispatchCreation(FuncRequest const & cmd, bool display)
915 BufferView * bv = cmd.view();
916 // use selection if available..
918 //if (action == LFUN_MATH_IMPORT_SELECTION)
922 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
925 InsetFormula * f = new InsetFormula(bv);
926 if (openNewInset(bv, f)) {
927 bv->theLockingInset()->
928 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
929 // don't do that also for LFUN_MATH_MODE unless you want end up with
930 // always changing to mathrm when opening an inlined inset
931 // -- I really hate "LyXfunc overloading"...
933 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
934 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
937 // create a macro if we see "\\newcommand" somewhere, and an ordinary
939 InsetFormulaBase * f;
940 if (sel.find("\\newcommand") == string::npos &&
941 sel.find("\\def") == string::npos)
942 f = new InsetFormula(sel);
944 f = new InsetFormulaMacro(sel);
945 bv->getLyXText()->cutSelection(bv);
948 bv->owner()->getLyXFunc().setMessage(N_("Math editor mode"));
952 void mathDispatch(FuncRequest const & cmd)
954 BufferView * bv = cmd.view();
955 if (!bv->available())
958 switch (cmd.action) {
960 case LFUN_MATH_DISPLAY:
961 mathDispatchCreation(cmd, true);
965 mathDispatchCreation(cmd, false);
968 case LFUN_MATH_IMPORT_SELECTION:
969 mathDispatchCreation(cmd, false);
972 case LFUN_MATH_MACRO:
973 if (cmd.argument.empty())
974 bv->owner()->getLyXFunc().setErrorMessage(N_("Missing argument"));
976 string s = cmd.argument;
977 string const s1 = token(s, ' ', 1);
978 int const na = s1.empty() ? 0 : lyx::atoi(s1);
979 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
984 case LFUN_INSERT_MATH:
985 case LFUN_INSERT_MATRIX:
986 case LFUN_MATH_DELIM: {
987 InsetFormula * f = new InsetFormula(bv);
988 if (openNewInset(bv, f)) {
989 bv->theLockingInset()->
990 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
991 bv->theLockingInset()->localDispatch(cmd);