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 //return lfunMouseMotion(cmd);
422 case LFUN_MOUSE_RELEASE:
423 //lyxerr << "Mouse single release\n";
424 return lfunMouseRelease(cmd);
425 case LFUN_MOUSE_DOUBLE:
426 //lyxerr << "Mouse double\n";
427 return localDispatch(FuncRequest(LFUN_WORDSEL));
435 BufferView * bv = cmd.view();
436 string argument = cmd.argument;
437 RESULT result = DISPATCHED;
439 bool was_macro = mathcursor->inMacroMode();
440 bool was_selection = mathcursor->selection();
444 mathcursor->normalize();
447 switch (cmd.action) {
449 case LFUN_MATH_MUTATE:
450 case LFUN_MATH_DISPLAY:
451 case LFUN_MATH_NUMBER:
452 case LFUN_MATH_NONUMBER:
455 case LFUN_DELETE_LINE_FORWARD:
456 case LFUN_INSERT_LABEL:
457 case LFUN_MATH_EXTERN:
458 case LFUN_TABULAR_FEATURE:
459 case LFUN_PASTESELECTION:
460 case LFUN_MATH_LIMITS:
461 bv->lockedInsetStoreUndo(Undo::EDIT);
462 mathcursor->dispatch(cmd);
466 sel = true; // fall through...
468 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
469 //lyxerr << "calling scroll 20\n";
471 // write something to the minibuffer
472 //bv->owner()->message(mathcursor->info());
476 sel = true; // fall through
478 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
482 sel = true; // fall through
484 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
488 sel = true; // fall through
490 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
494 mathcursor->home(false);
495 mathcursor->end(true);
498 case LFUN_UP_PARAGRAPHSEL:
499 case LFUN_UP_PARAGRAPH:
500 case LFUN_DOWN_PARAGRAPHSEL:
501 case LFUN_DOWN_PARAGRAPH:
506 case LFUN_WORDLEFTSEL:
507 sel = true; // fall through
510 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
514 case LFUN_WORDRIGHTSEL:
515 sel = true; // fall through
518 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
523 case LFUN_BEGINNINGBUFSEL:
524 case LFUN_BEGINNINGBUF:
532 result = FINISHED_RIGHT;
536 mathcursor->idxNext();
540 mathcursor->idxPrev();
543 case LFUN_DELETE_WORD_BACKWARD:
545 bv->lockedInsetStoreUndo(Undo::EDIT);
546 if (!mathcursor->backspace()) {
552 case LFUN_DELETE_WORD_FORWARD:
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 if (!mathcursor->erase()) {
562 // sprintf(dispatch_buffer, "%d %d",);
563 // dispatch_result = dispatch_buffer;
566 lyxerr << "LFUN_SETXY broken!\n";
569 istringstream is(cmd.argument.c_str());
571 mathcursor->setPos(x, y);
577 mathcursor->macroModeClose();
578 bv->lockedInsetStoreUndo(Undo::EDIT);
579 mathcursor->selPaste();
583 bv->lockedInsetStoreUndo(Undo::DELETE);
584 mathcursor->selCut();
588 mathcursor->selCopy();
592 // Special casing for superscript in case of LyX handling
594 case LFUN_CIRCUMFLEX:
595 if (cmd.argument.empty()) {
596 // do superscript if LyX handles
598 bv->lockedInsetStoreUndo(Undo::EDIT);
599 mathcursor->script(true);
616 case LFUN_HUNG_UMLAUT:
620 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
621 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
622 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
623 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
624 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
625 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
626 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
627 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
628 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
629 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
632 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
633 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
635 handleFont(bv, cmd.argument, "textrm");
636 //bv->owner()->message(_("math text mode toggled"));
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->setSize(arg);
648 case LFUN_INSERT_MATRIX: {
649 bv->lockedInsetStoreUndo(Undo::EDIT);
654 istringstream is(STRCONV(argument));
655 is >> m >> n >> v_align >> h_align;
659 mathcursor->niceInsert(
660 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
664 case LFUN_SUPERSCRIPT:
667 bv->lockedInsetStoreUndo(Undo::EDIT);
668 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
672 case LFUN_MATH_DELIM:
674 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
676 string rs = split(cmd.argument, ls, ' ');
677 // Reasonable default values
683 bv->lockedInsetStoreUndo(Undo::EDIT);
684 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
688 case LFUN_PROTECTEDSPACE:
689 case LFUN_MATH_SPACE:
690 bv->lockedInsetStoreUndo(Undo::EDIT);
691 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
695 bv->owner()->message(_("Invalid action in math mode!"));
699 case LFUN_EXEC_COMMAND:
700 result = UNDISPATCHED;
704 // interpret this as if a backslash was typed
705 bv->lockedInsetStoreUndo(Undo::EDIT);
706 mathcursor->interpret('\\');
709 case LFUN_BREAKPARAGRAPH:
710 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
711 case LFUN_BREAKPARAGRAPH_SKIP:
715 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
716 // handling such that "self-insert" works on "arbitrary stuff" too, and
717 // math-insert only handles special math things like "matrix".
718 case LFUN_INSERT_MATH:
719 bv->lockedInsetStoreUndo(Undo::EDIT);
720 mathcursor->niceInsert(argument);
724 case LFUN_SELFINSERT:
725 if (!argument.empty()) {
726 bv->lockedInsetStoreUndo(Undo::EDIT);
727 if (argument.size() == 1)
728 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
730 mathcursor->insert(asArray(argument));
734 case LFUN_MATH_PANEL:
735 result = UNDISPATCHED;
739 if (mathcursor->selection())
740 mathcursor->selClear();
742 result = UNDISPATCHED;
745 case LFUN_INSET_TOGGLE:
746 mathcursor->insetToggle();
749 case LFUN_DIALOG_SHOW_NEW_INSET: {
750 string const & name = argument;
754 data = tmp.createDialogStr(name);
758 result = UNDISPATCHED;
760 bv->owner()->getDialogs().show(name, data, 0);
765 case LFUN_INSET_APPLY: {
766 string const name = cmd.getArg(0);
768 bv->owner()->getDialogs().getOpenInset(name);
771 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
772 result = base->localDispatch(fr);
775 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
776 mathcursor->insert(ar);
779 result = UNDISPATCHED;
786 result = UNDISPATCHED;
789 if (result == DISPATCHED)
790 bv->updateInset(this);
792 mathcursor->normalize();
795 lyx::Assert(mathcursor);
797 if (mathcursor->selection() || was_selection)
798 toggleInsetSelection(bv);
800 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
801 result == UNDISPATCHED) {
805 cmd.view()->stuffClipboard(mathcursor->grabSelection());
807 releaseMathCursor(bv);
808 bv->unlockInset(this);
810 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
813 return result; // original version
817 void InsetFormulaBase::revealCodes(BufferView * bv) const
821 bv->owner()->message(mathcursor->info());
824 // write something to the minibuffer
825 // translate to latex
826 mathcursor->markInsert();
829 string str = os.str();
830 mathcursor->markErase();
831 string::size_type pos = 0;
833 for (string::iterator it = str.begin(); it != str.end(); ++it) {
836 else if (*it == '\0') {
838 pos = it - str.begin();
844 res = res.substr(pos - 30);
846 res = res.substr(0, 60);
847 bv->owner()->message(res);
852 Inset::Code InsetFormulaBase::lyxCode() const
854 return Inset::MATH_CODE;
858 int InsetFormulaBase::ylow() const
860 return yo_ - ascent(view(), font_);
864 int InsetFormulaBase::yhigh() const
866 return yo_ + descent(view(), font_);
870 int InsetFormulaBase::xlow() const
876 int InsetFormulaBase::xhigh() const
878 return xo_ + width(view(), font_);
882 /////////////////////////////////////////////////////////////////////
885 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
891 static InsetFormulaBase * lastformula = 0;
892 static MathIterator current = MathIterator(ibegin(par().nucleus()));
894 static string laststr;
896 if (lastformula != this || laststr != str) {
897 //lyxerr << "reset lastformula to " << this << "\n";
900 current = ibegin(par().nucleus());
902 mathed_parse_cell(ar, str);
906 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
908 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
909 if (it.cell().matchpart(ar, it.back().pos_)) {
910 bv->unlockInset(bv->theLockingInset());
911 if (!bv->lockInset(this)) {
912 lyxerr << "Cannot lock inset" << endl;
916 mathcursor = new MathCursor(this, true);
918 mathcursor->setSelection(it, ar.size());
921 bv->updateInset(this);
926 //lyxerr << "not found!\n";
932 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
935 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
936 return searchForward(bv, what, a, b);
940 bool InsetFormulaBase::display() const
942 return par()->asHullInset() && par()->asHullInset()->display();
946 string InsetFormulaBase::selectionAsString() const
948 return mathcursor ? mathcursor->grabSelection() : string();
951 /////////////////////////////////////////////////////////////////////
954 void mathDispatchCreation(FuncRequest const & cmd, bool display)
956 BufferView * bv = cmd.view();
957 // use selection if available..
959 //if (action == LFUN_MATH_IMPORT_SELECTION)
963 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
966 InsetFormula * f = new InsetFormula(bv);
967 if (openNewInset(bv, f)) {
968 bv->theLockingInset()->
969 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
970 // don't do that also for LFUN_MATH_MODE unless you want end up with
971 // always changing to mathrm when opening an inlined inset
972 // -- I really hate "LyXfunc overloading"...
974 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
975 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
978 // create a macro if we see "\\newcommand" somewhere, and an ordinary
980 InsetFormulaBase * f;
981 if (sel.find("\\newcommand") == string::npos &&
982 sel.find("\\def") == string::npos)
983 f = new InsetFormula(sel);
985 f = new InsetFormulaMacro(sel);
986 bv->getLyXText()->cutSelection(bv);
989 cmd.message(N_("Math editor mode"));
993 void mathDispatch(FuncRequest const & cmd)
995 BufferView * bv = cmd.view();
996 if (!bv->available())
999 switch (cmd.action) {
1001 case LFUN_MATH_DISPLAY:
1002 mathDispatchCreation(cmd, true);
1005 case LFUN_MATH_MODE:
1006 mathDispatchCreation(cmd, false);
1009 case LFUN_MATH_IMPORT_SELECTION:
1010 mathDispatchCreation(cmd, false);
1013 case LFUN_MATH_MACRO:
1014 if (cmd.argument.empty())
1015 cmd.errorMessage(N_("Missing argument"));
1017 string s = cmd.argument;
1018 string const s1 = token(s, ' ', 1);
1019 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1020 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1025 case LFUN_INSERT_MATH:
1026 case LFUN_INSERT_MATRIX:
1027 case LFUN_MATH_DELIM: {
1028 InsetFormula * f = new InsetFormula(bv);
1029 if (openNewInset(bv, f)) {
1030 bv->theLockingInset()->
1031 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
1032 bv->theLockingInset()->localDispatch(cmd);