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"
23 #include "formulamacro.h"
25 #include "funcrequest.h"
26 #include "BufferView.h"
29 #include "LaTeXFeatures.h"
31 #include "math_support.h"
32 #include "metricsinfo.h"
33 #include "support/lstrings.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/mouse_state.h"
38 #include "math_arrayinset.h"
39 #include "math_charinset.h"
40 #include "math_deliminset.h"
41 #include "math_cursor.h"
42 #include "math_factory.h"
43 #include "math_fontinset.h"
44 #include "math_hullinset.h"
45 #include "math_iterator.h"
46 #include "math_macrotable.h"
47 #include "math_parser.h"
49 #include "math_spaceinset.h"
50 #include "undo_funcs.h"
51 #include "textpainter.h"
52 #include "frontends/Dialogs.h"
54 #include "ref_inset.h"
62 MathCursor * mathcursor = 0;
73 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
75 if (!bv->insertInset(new_inset)) {
79 new_inset->edit(bv, true);
88 InsetFormulaBase::InsetFormulaBase()
89 : font_(), xo_(0), yo_(0)
91 // This is needed as long the math parser is not re-entrant
93 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
94 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << "\n";
95 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
96 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
100 // simply scrap this function if you want
101 void InsetFormulaBase::mutateToText()
104 // translate to latex
106 latex(NULL, os, false, false);
107 string str = os.str();
110 LyXText * lt = view_->getLyXText();
111 string::const_iterator cit = str.begin();
112 string::const_iterator end = str.end();
113 for (; cit != end; ++cit)
114 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
117 //view_->owner()->dispatch(LFUN_ESCAPE);
122 void InsetFormulaBase::handleFont
123 (BufferView * bv, string const & arg, string const & font)
125 // this whole function is a hack and won't work for incremental font
127 bv->lockedInsetStoreUndo(Undo::EDIT);
128 if (mathcursor->par()->name() == font)
129 mathcursor->handleFont(font);
131 mathcursor->handleNest(createMathInset(font));
132 mathcursor->insert(arg);
137 BufferView * InsetFormulaBase::view() const
139 return view_.lock().get();
143 void InsetFormulaBase::validate(LaTeXFeatures &) const
147 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
154 void InsetFormulaBase::metrics(BufferView * bv) const
157 view_ = bv->owner()->view();
159 mi.base.style = LM_ST_TEXT;
160 mi.base.font = font_;
161 mi.base.font.setColor(LColor::math);
166 string const InsetFormulaBase::editMessage() const
168 return _("Math editor mode");
172 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
174 if (!bv->lockInset(this))
175 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
176 releaseMathCursor(bv);
177 mathcursor = new MathCursor(this, true);
179 mathcursor->setPos(x + xo_, y + yo_);
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);
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);
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
200 if (mathcursor->inMacroMode()) {
201 mathcursor->macroModeClose();
202 bv->updateInset(this);
204 releaseMathCursor(bv);
207 bv->updateInset(this);
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);
222 x = mathcursor->targetX();
225 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
229 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
232 lyxerr[Debug::MATHED] << "toggleInsetCursor impossible" << endl;
235 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << endl;
236 if (isCursorVisible())
243 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
246 lyxerr << "showInsetCursor impossible" << endl;
249 if (isCursorVisible())
252 mathcursor->getPos(x, y);
253 math_font_max_dim(font_, asc, des);
254 bv->showLockedInsetCursor(x, y - yo_, asc, des);
255 setCursorVisible(true);
256 //lyxerr << "showInsetCursor: " << x << ' ' << y << endl;
260 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
264 if (!isCursorVisible())
266 bv->hideLockedInsetCursor();
267 setCursorVisible(false);
268 //lyxerr << "hideInsetCursor: " << endl;
272 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
277 math_font_max_dim(font_, asc, des);
278 getCursorPos(bv, x, y);
280 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
281 bv->fitLockedInsetCursor(x, y, asc, des);
285 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
288 bv->updateInset(this);
292 vector<string> const InsetFormulaBase::getLabelList() const
294 return vector<string>();
298 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
303 BufferView * bv = cmd.view();
306 bv->updateInset(this);
307 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
309 if (cmd.button() == mouse_button::button3) {
310 // try to dispatch to enclosed insets first
311 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
312 // launch math panel for right mouse button
313 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
314 bv->owner()->getDialogs().showMathPanel();
319 if (cmd.button() == mouse_button::button2) {
320 mathcursor->selClear();
321 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
322 mathcursor->insert(asArray(bv->getClipboard()));
323 bv->updateInset(this);
327 if (cmd.button() == mouse_button::button1) {
328 // try to dispatch to enclosed insets first
329 mathcursor->dispatch(cmd);
330 cmd.view()->stuffClipboard(mathcursor->grabSelection());
331 // try to set the cursor
333 //mathcursor = new MathCursor(this, x == 0);
335 //mathcursor->setPos(x + xo_, y + yo_);
343 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
345 BufferView * bv = cmd.view();
346 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
348 if (!mathcursor || mathcursor->formula() != this) {
349 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
350 releaseMathCursor(bv);
351 mathcursor = new MathCursor(this, cmd.x == 0);
353 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
356 if (cmd.button() == mouse_button::button3) {
357 mathcursor->dispatch(cmd);
361 if (cmd.button() == mouse_button::button1) {
364 mathcursor->selClear();
365 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
366 mathcursor->dispatch(cmd);
370 bv->updateInset(this);
375 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
380 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
383 // only select with button 1
384 if (cmd.button() != mouse_button::button1)
387 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
393 if (!mathcursor->selection())
394 mathcursor->selStart();
396 BufferView * bv = cmd.view();
398 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
400 bv->updateInset(this);
405 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
407 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
408 // << " arg: '" << cmd.argument
409 // << " x: '" << cmd.x
410 // << " y: '" << cmd.y
411 // << "' button: " << cmd.button() << endl;
413 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
414 bool remove_inset = false;
416 switch (cmd.action) {
417 case LFUN_MOUSE_PRESS:
418 //lyxerr << "Mouse single press\n";
419 return lfunMousePress(cmd);
420 case LFUN_MOUSE_MOTION:
421 //lyxerr << "Mouse motion\n";
422 return lfunMouseMotion(cmd);
423 case LFUN_MOUSE_RELEASE:
424 //lyxerr << "Mouse single release\n";
425 return lfunMouseRelease(cmd);
426 case LFUN_MOUSE_DOUBLE:
427 //lyxerr << "Mouse double\n";
428 return localDispatch(FuncRequest(LFUN_WORDSEL));
436 BufferView * bv = cmd.view();
437 string argument = cmd.argument;
438 RESULT result = DISPATCHED;
440 bool was_macro = mathcursor->inMacroMode();
441 bool was_selection = mathcursor->selection();
445 mathcursor->normalize();
448 switch (cmd.action) {
450 case LFUN_MATH_MUTATE:
451 case LFUN_MATH_DISPLAY:
452 case LFUN_MATH_NUMBER:
453 case LFUN_MATH_NONUMBER:
454 case LFUN_CELL_SPLIT:
456 case LFUN_DELETE_LINE_FORWARD:
457 case LFUN_INSERT_LABEL:
458 case LFUN_MATH_EXTERN:
459 case LFUN_TABULAR_FEATURE:
460 case LFUN_PASTESELECTION:
461 case LFUN_MATH_LIMITS:
462 bv->lockedInsetStoreUndo(Undo::EDIT);
463 mathcursor->dispatch(cmd);
467 sel = true; // fall through...
469 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
470 //lyxerr << "calling scroll 20\n";
472 // write something to the minibuffer
473 //bv->owner()->message(mathcursor->info());
477 sel = true; // fall through
479 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
483 sel = true; // fall through
485 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
489 sel = true; // fall through
491 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
495 mathcursor->home(false);
496 mathcursor->end(true);
499 case LFUN_UP_PARAGRAPHSEL:
500 case LFUN_UP_PARAGRAPH:
501 case LFUN_DOWN_PARAGRAPHSEL:
502 case LFUN_DOWN_PARAGRAPH:
507 case LFUN_WORDLEFTSEL:
508 sel = true; // fall through
511 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
515 case LFUN_WORDRIGHTSEL:
516 sel = true; // fall through
519 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
524 case LFUN_BEGINNINGBUFSEL:
525 case LFUN_BEGINNINGBUF:
533 result = FINISHED_RIGHT;
536 case LFUN_CELL_FORWARD:
537 mathcursor->idxNext();
540 case LFUN_CELL_BACKWARD:
541 mathcursor->idxPrev();
544 case LFUN_DELETE_WORD_BACKWARD:
546 bv->lockedInsetStoreUndo(Undo::EDIT);
547 if (!mathcursor->backspace()) {
553 case LFUN_DELETE_WORD_FORWARD:
555 bv->lockedInsetStoreUndo(Undo::EDIT);
556 if (!mathcursor->erase()) {
563 // sprintf(dispatch_buffer, "%d %d",);
564 // dispatch_result = dispatch_buffer;
567 lyxerr << "LFUN_SETXY broken!\n";
570 istringstream is(cmd.argument.c_str());
572 mathcursor->setPos(x, y);
578 istringstream is(cmd.argument.c_str());
581 mathcursor->macroModeClose();
582 bv->lockedInsetStoreUndo(Undo::EDIT);
583 mathcursor->selPaste(n);
588 bv->lockedInsetStoreUndo(Undo::DELETE);
589 mathcursor->selCut();
593 mathcursor->selCopy();
597 // Special casing for superscript in case of LyX handling
599 case LFUN_CIRCUMFLEX:
600 if (cmd.argument.empty()) {
601 // do superscript if LyX handles
603 bv->lockedInsetStoreUndo(Undo::EDIT);
604 mathcursor->script(true);
621 case LFUN_HUNG_UMLAUT:
625 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
626 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
627 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
628 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
629 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
630 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
631 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
632 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
633 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
634 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
637 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
638 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
640 handleFont(bv, cmd.argument, "textrm");
641 //bv->owner()->message(_("math text mode toggled"));
647 bv->lockedInsetStoreUndo(Undo::EDIT);
648 mathcursor->setSize(arg);
653 case LFUN_INSERT_MATRIX: {
654 bv->lockedInsetStoreUndo(Undo::EDIT);
659 istringstream is(STRCONV(argument));
660 is >> m >> n >> v_align >> h_align;
664 mathcursor->niceInsert(
665 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
669 case LFUN_SUPERSCRIPT:
672 bv->lockedInsetStoreUndo(Undo::EDIT);
673 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
677 case LFUN_MATH_DELIM:
679 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
681 string rs = split(cmd.argument, ls, ' ');
682 // Reasonable default values
688 bv->lockedInsetStoreUndo(Undo::EDIT);
689 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
693 case LFUN_PROTECTEDSPACE:
694 case LFUN_MATH_SPACE:
695 bv->lockedInsetStoreUndo(Undo::EDIT);
696 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
700 bv->owner()->message(_("Invalid action in math mode!"));
704 case LFUN_EXEC_COMMAND:
705 result = UNDISPATCHED;
709 // interpret this as if a backslash was typed
710 bv->lockedInsetStoreUndo(Undo::EDIT);
711 mathcursor->interpret('\\');
714 case LFUN_BREAKPARAGRAPH:
715 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
716 case LFUN_BREAKPARAGRAPH_SKIP:
720 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
721 // handling such that "self-insert" works on "arbitrary stuff" too, and
722 // math-insert only handles special math things like "matrix".
723 case LFUN_INSERT_MATH:
724 bv->lockedInsetStoreUndo(Undo::EDIT);
725 mathcursor->niceInsert(argument);
729 case LFUN_SELFINSERT:
730 if (!argument.empty()) {
731 bv->lockedInsetStoreUndo(Undo::EDIT);
732 if (argument.size() == 1)
733 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
735 mathcursor->insert(asArray(argument));
739 case LFUN_MATH_PANEL:
740 result = UNDISPATCHED;
744 if (mathcursor->selection())
745 mathcursor->selClear();
747 result = UNDISPATCHED;
750 case LFUN_INSET_TOGGLE:
751 mathcursor->insetToggle();
754 case LFUN_DIALOG_SHOW_NEW_INSET: {
755 string const & name = argument;
759 data = tmp.createDialogStr(name);
763 result = UNDISPATCHED;
765 bv->owner()->getDialogs().show(name, data, 0);
770 case LFUN_INSET_APPLY: {
771 string const name = cmd.getArg(0);
773 bv->owner()->getDialogs().getOpenInset(name);
776 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
777 result = base->localDispatch(fr);
780 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
781 mathcursor->insert(ar);
784 result = UNDISPATCHED;
791 result = UNDISPATCHED;
794 if (result == DISPATCHED)
795 bv->updateInset(this);
797 mathcursor->normalize();
800 lyx::Assert(mathcursor);
802 if (mathcursor->selection() || was_selection)
803 toggleInsetSelection(bv);
805 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
806 result == UNDISPATCHED) {
810 cmd.view()->stuffClipboard(mathcursor->grabSelection());
812 releaseMathCursor(bv);
813 bv->unlockInset(this);
815 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
818 return result; // original version
822 void InsetFormulaBase::revealCodes(BufferView * bv) const
826 bv->owner()->message(mathcursor->info());
829 // write something to the minibuffer
830 // translate to latex
831 mathcursor->markInsert();
834 string str = os.str();
835 mathcursor->markErase();
836 string::size_type pos = 0;
838 for (string::iterator it = str.begin(); it != str.end(); ++it) {
841 else if (*it == '\0') {
843 pos = it - str.begin();
849 res = res.substr(pos - 30);
851 res = res.substr(0, 60);
852 bv->owner()->message(res);
857 Inset::Code InsetFormulaBase::lyxCode() const
859 return Inset::MATH_CODE;
863 int InsetFormulaBase::ylow() const
865 return yo_ - ascent(view(), font_);
869 int InsetFormulaBase::yhigh() const
871 return yo_ + descent(view(), font_);
875 int InsetFormulaBase::xlow() const
881 int InsetFormulaBase::xhigh() const
883 return xo_ + width(view(), font_);
887 /////////////////////////////////////////////////////////////////////
890 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
896 static InsetFormulaBase * lastformula = 0;
897 static MathIterator current = MathIterator(ibegin(par().nucleus()));
899 static string laststr;
901 if (lastformula != this || laststr != str) {
902 //lyxerr << "reset lastformula to " << this << "\n";
905 current = ibegin(par().nucleus());
907 mathed_parse_cell(ar, str);
911 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
913 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
914 if (it.cell().matchpart(ar, it.back().pos_)) {
915 bv->unlockInset(bv->theLockingInset());
916 if (!bv->lockInset(this)) {
917 lyxerr << "Cannot lock inset" << endl;
921 mathcursor = new MathCursor(this, true);
923 mathcursor->setSelection(it, ar.size());
926 bv->updateInset(this);
931 //lyxerr << "not found!\n";
937 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
940 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
941 return searchForward(bv, what, a, b);
945 bool InsetFormulaBase::display() const
947 return par()->asHullInset() && par()->asHullInset()->display();
951 string InsetFormulaBase::selectionAsString() const
953 return mathcursor ? mathcursor->grabSelection() : string();
956 /////////////////////////////////////////////////////////////////////
959 void mathDispatchCreation(FuncRequest const & cmd, bool display)
961 BufferView * bv = cmd.view();
962 // use selection if available..
964 //if (action == LFUN_MATH_IMPORT_SELECTION)
968 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
971 InsetFormula * f = new InsetFormula(bv);
972 if (openNewInset(bv, f)) {
973 bv->theLockingInset()->
974 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
975 // don't do that also for LFUN_MATH_MODE unless you want end up with
976 // always changing to mathrm when opening an inlined inset
977 // -- I really hate "LyXfunc overloading"...
979 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
980 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
983 // create a macro if we see "\\newcommand" somewhere, and an ordinary
985 InsetFormulaBase * f;
986 if (sel.find("\\newcommand") == string::npos &&
987 sel.find("\\def") == string::npos)
988 f = new InsetFormula(sel);
990 f = new InsetFormulaMacro(sel);
991 bv->getLyXText()->cutSelection(bv);
994 cmd.message(N_("Math editor mode"));
998 void mathDispatch(FuncRequest const & cmd)
1000 BufferView * bv = cmd.view();
1001 if (!bv->available())
1004 switch (cmd.action) {
1006 case LFUN_MATH_DISPLAY:
1007 mathDispatchCreation(cmd, true);
1010 case LFUN_MATH_MODE:
1011 mathDispatchCreation(cmd, false);
1014 case LFUN_MATH_IMPORT_SELECTION:
1015 mathDispatchCreation(cmd, false);
1018 case LFUN_MATH_MACRO:
1019 if (cmd.argument.empty())
1020 cmd.errorMessage(N_("Missing argument"));
1022 string s = cmd.argument;
1023 string const s1 = token(s, ' ', 1);
1024 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1025 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1029 case LFUN_INSERT_MATH:
1030 case LFUN_INSERT_MATRIX:
1031 case LFUN_MATH_DELIM: {
1032 InsetFormula * f = new InsetFormula(bv);
1033 if (openNewInset(bv, f)) {
1034 bv->theLockingInset()->
1035 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
1036 bv->theLockingInset()->localDispatch(cmd);