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"
27 #include "commandtags.h"
28 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/mouse_state.h"
40 #include "math_arrayinset.h"
41 #include "math_charinset.h"
42 #include "math_cursor.h"
43 #include "math_factory.h"
44 #include "math_fontinset.h"
45 #include "math_hullinset.h"
46 #include "math_iterator.h"
47 #include "math_macrotable.h"
48 #include "math_parser.h"
50 #include "math_spaceinset.h"
51 #include "undo_funcs.h"
52 #include "textpainter.h"
53 #include "frontends/Dialogs.h"
62 MathCursor * mathcursor = 0;
73 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
75 if (!bv->insertInset(new_inset)) {
79 new_inset->edit(bv, 0, 0, mouse_button::none);
88 InsetFormulaBase::InsetFormulaBase()
89 : view_(0), font_(), xo_(0), yo_(0)
91 // This is needed as long the math parser is not re-entrant
92 MathMacroTable::builtinMacros();
93 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
94 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\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()->getTrans().TranslateAndInsert(*cit, lt);
117 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
122 void InsetFormulaBase::handleFont
123 (BufferView * bv, string const & arg, string const & font)
125 bv->lockedInsetStoreUndo(Undo::EDIT);
126 bool sel = mathcursor->selection();
128 updateLocal(bv, true);
129 mathcursor->handleNest(new MathFontInset(font));
130 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
131 mathcursor->insert(*it);
133 updateLocal(bv, false);
137 // Check if uses AMS macros
138 void InsetFormulaBase::validate(LaTeXFeatures &) const
142 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
149 void InsetFormulaBase::metrics(BufferView * bv) const
155 mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
156 mi.base.font = font_;
157 mi.base.font.setColor(LColor::math);
158 //whichFont(mi.font, LM_TC_MIN, mi);
163 string const InsetFormulaBase::editMessage() const
165 return _("Math editor mode");
169 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
171 if (!bv->lockInset(this))
172 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174 mathcursor = new MathCursor(this, true);
176 mathcursor->setPos(x, y);
177 //lyxerr << "setting pos to " << x << "," << y << "\n";
179 // if that is removed, we won't get the magenta box when entering an
180 // inset for the first time
181 bv->updateInset(this, false);
185 void InsetFormulaBase::edit(BufferView * bv, bool front)
187 if (!bv->lockInset(this))
188 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
190 mathcursor = new MathCursor(this, front);
192 bv->updateInset(this, false);
196 void InsetFormulaBase::insetUnlock(BufferView * bv)
199 if (mathcursor->inMacroMode()) {
200 mathcursor->macroModeClose();
201 updateLocal(bv, true);
206 bv->updateInset(this, false);
210 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
212 mathcursor->getPos(x, y);
215 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
219 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
224 if (isCursorVisible())
225 bv->hideLockedInsetCursor();
230 mathcursor->getPos(x, y);
234 math_font_max_dim(font_, asc, des);
235 bv->showLockedInsetCursor(x, y, asc, des);
236 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
239 toggleCursorVisible();
243 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
245 if (isCursorVisible())
251 mathcursor->getPos(x, y);
255 math_font_max_dim(font_, asc, des);
256 bv->fitLockedInsetCursor(x, y, asc, des);
257 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
259 toggleInsetCursor(bv);
263 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
265 if (isCursorVisible())
266 toggleInsetCursor(bv);
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)
286 bv->updateInset(this, dirty);
290 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
291 int /*x*/, int /*y*/, mouse_button::state button)
293 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
299 bv->updateInset(this, false);
301 if (button == mouse_button::button3) {
302 // launch math panel for right mouse button
303 bv->owner()->getDialogs()->showMathPanel();
310 void InsetFormulaBase::insetButtonPress(BufferView * bv,
311 int x, int y, mouse_button::state button)
313 //lyxerr << "insetButtonPress: "
314 // << x << " " << y << " but: " << button << "\n";
321 mathcursor = new MathCursor(this, x == 0);
325 mathcursor->selClear();
326 mathcursor->setPos(x + xo_, y + yo_);
330 lyxerr << "insetButtonPress: 2\n";
333 bv->lockedInsetStoreUndo(Undo::EDIT);
335 mathcursor->selGet(ar);
336 mathcursor->setPos(x + xo_, y + yo_);
338 bv->getLyXText()->selectionAsString(bv->buffer(), false);
339 mathed_parse_cell(ar, sel);
340 mathcursor->insert(ar);
345 // launch math panel for right mouse button
346 bv->owner()->getDialogs()->showMathPanel();
350 if (button == mouse_button::button1 || !mathcursor) {
352 mathcursor = new MathCursor(this, x == 0);
356 mathcursor->selClear();
357 mathcursor->setPos(x + xo_, y + yo_);
360 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
362 // launch math panel for right mouse button
363 bv->owner()->getDialogs()->showMathPanel();
367 bv->updateInset(this, false);
371 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
372 int x, int y, mouse_button::state)
377 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
378 //lyxerr << "insetMotionNotify: ignored\n";
384 if (!mathcursor->selection())
385 mathcursor->selStart();
387 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
388 // << ' ' << button << "\n";
390 mathcursor->setPos(x + xo_, y + yo_);
392 bv->updateInset(this, false);
396 UpdatableInset::RESULT
397 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
400 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
401 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
406 RESULT result = DISPATCHED;
408 bool was_macro = mathcursor->inMacroMode();
409 bool was_selection = mathcursor->selection();
413 mathcursor->normalize();
418 // --- Cursor Movements ---------------------------------------------
421 sel = true; // fall through...
424 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
425 //lyxerr << "calling scroll 20\n";
427 updateLocal(bv, false);
428 // write something to the minibuffer
429 //bv->owner()->message(mathcursor->info());
434 sel = true; // fall through
437 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
438 updateLocal(bv, false);
446 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
447 updateLocal(bv, false);
455 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
456 updateLocal(bv, false);
463 mathcursor->home(sel);
464 updateLocal(bv, false);
471 mathcursor->end(sel);
472 updateLocal(bv, false);
475 case LFUN_DELETE_LINE_FORWARD:
476 bv->lockedInsetStoreUndo(Undo::DELETE);
477 mathcursor->delLine();
478 updateLocal(bv, true);
482 mathcursor->idxNext();
483 updateLocal(bv, false);
487 mathcursor->idxPrev();
488 updateLocal(bv, false);
492 bv->lockedInsetStoreUndo(Undo::EDIT);
493 mathcursor->splitCell();
494 updateLocal(bv, true);
497 case LFUN_DELETE_WORD_BACKWARD:
499 bv->lockedInsetStoreUndo(Undo::DELETE);
500 mathcursor->backspace();
501 bv->updateInset(this, true);
504 case LFUN_DELETE_WORD_FORWARD:
506 bv->lockedInsetStoreUndo(Undo::DELETE);
508 bv->updateInset(this, true);
512 // sprintf(dispatch_buffer, "%d %d",);
513 // dispatch_result = dispatch_buffer;
516 lyxerr << "LFUN_SETXY broken!\n";
519 istringstream is(arg.c_str());
521 mathcursor->setPos(x, y);
522 updateLocal(bv, false);
528 mathcursor->macroModeClose();
529 bv->lockedInsetStoreUndo(Undo::EDIT);
530 mathcursor->selPaste();
531 updateLocal(bv, true);
535 bv->lockedInsetStoreUndo(Undo::DELETE);
536 mathcursor->selCut();
537 updateLocal(bv, true);
541 mathcursor->selCopy();
544 case LFUN_WORDRIGHTSEL:
545 case LFUN_WORDLEFTSEL:
548 // Special casing for superscript in case of LyX handling
550 case LFUN_CIRCUMFLEX:
552 // do superscript if LyX handles
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 mathcursor->script(true);
556 updateLocal(bv, true);
572 case LFUN_HUNG_UMLAUT:
576 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
577 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
578 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
579 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
580 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
581 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
582 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
583 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
584 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
585 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
586 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
589 handleFont(bv, arg, "lyxgreek1");
591 mathcursor->interpret(arg);
596 handleFont(bv, arg, "textrm");
600 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
601 updateLocal(bv, true);
604 //bv->owner()->message(_("math text mode toggled"));
607 case LFUN_MATH_LIMITS:
608 bv->lockedInsetStoreUndo(Undo::EDIT);
609 if (mathcursor->toggleLimits())
610 updateLocal(bv, true);
616 bv->lockedInsetStoreUndo(Undo::EDIT);
617 mathcursor->setSize(arg);
618 updateLocal(bv, true);
623 case LFUN_INSERT_MATRIX:
625 bv->lockedInsetStoreUndo(Undo::EDIT);
626 mathcursor->interpret("matrix " + arg);
627 updateLocal(bv, true);
631 case LFUN_SUPERSCRIPT:
634 bv->lockedInsetStoreUndo(Undo::EDIT);
635 mathcursor->script(action == LFUN_SUPERSCRIPT);
636 updateLocal(bv, true);
640 case LFUN_MATH_DELIM:
642 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
644 string rs = split(arg, ls, ' ');
645 // Reasonable default values
651 bv->lockedInsetStoreUndo(Undo::EDIT);
652 mathcursor->handleDelim(ls, rs);
653 updateLocal(bv, true);
657 case LFUN_PROTECTEDSPACE:
658 case LFUN_MATH_SPACE:
659 bv->lockedInsetStoreUndo(Undo::EDIT);
660 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
661 updateLocal(bv, true);
665 bv->owner()->message(_("Invalid action in math mode!"));
669 case LFUN_MATH_HALIGN:
670 case LFUN_MATH_VALIGN:
671 case LFUN_MATH_ROW_INSERT:
672 case LFUN_MATH_ROW_DELETE:
673 case LFUN_MATH_COLUMN_INSERT:
674 case LFUN_MATH_COLUMN_DELETE:
676 MathInset::idx_type idx = 0;
677 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
679 mathcursor->popToEnclosingGrid();
680 bv->lockedInsetStoreUndo(Undo::EDIT);
681 char align = arg.size() ? arg[0] : 'c';
683 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
684 case LFUN_MATH_VALIGN: p->valign(align); break;
685 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
686 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
687 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
688 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
691 updateLocal(bv, true);
696 case LFUN_EXEC_COMMAND:
697 result = UNDISPATCHED;
700 case LFUN_BREAKPARAGRAPH:
701 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
702 //lyxerr << "LFUN ignored\n";
706 // interpret this as if a backslash was typed
707 bv->lockedInsetStoreUndo(Undo::EDIT);
708 mathcursor->interpret('\\');
709 updateLocal(bv, true);
713 case LFUN_INSERT_MATH:
714 case LFUN_SELFINSERT:
716 bv->lockedInsetStoreUndo(Undo::EDIT);
718 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
720 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
721 updateLocal(bv, true);
725 case LFUN_MATH_PANEL:
726 result = UNDISPATCHED;
730 if (mathcursor->selection())
731 mathcursor->selClear();
733 result = UNDISPATCHED;
736 case LFUN_INSET_TOGGLE:
737 mathcursor->insetToggle();
741 result = UNDISPATCHED;
744 mathcursor->normalize();
747 lyx::Assert(mathcursor);
749 if (mathcursor->selection() || was_selection)
750 toggleInsetSelection(bv);
752 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
753 result == UNDISPATCHED)
756 bv->unlockInset(this);
760 return result; // original version
764 void InsetFormulaBase::revealCodes(BufferView * bv) const
768 bv->owner()->message(mathcursor->info());
771 // write something to the minibuffer
772 // translate to latex
773 mathcursor->markInsert();
776 string str = os.str();
777 mathcursor->markErase();
778 string::size_type pos = 0;
780 for (string::iterator it = str.begin(); it != str.end(); ++it) {
783 else if (*it == '\0') {
785 pos = it - str.begin();
791 res = res.substr(pos - 30);
793 res = res.substr(0, 60);
794 bv->owner()->message(res);
799 Inset::Code InsetFormulaBase::lyxCode() const
801 return Inset::MATH_CODE;
805 int InsetFormulaBase::ylow() const
807 return yo_ - ascent(view_, font_);
811 int InsetFormulaBase::yhigh() const
813 return yo_ + descent(view_, font_);
817 int InsetFormulaBase::xlow() const
823 int InsetFormulaBase::xhigh() const
825 return xo_ + width(view_, font_);
829 /////////////////////////////////////////////////////////////////////
832 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
838 static InsetFormulaBase * lastformula = 0;
839 static MathIterator current = MathIterator(ibegin(par().nucleus()));
841 static string laststr;
843 if (lastformula != this || laststr != str) {
844 //lyxerr << "reset lastformula to " << this << "\n";
847 current = ibegin(par().nucleus());
849 mathed_parse_cell(ar, str);
853 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
855 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
856 if (it.cell().matchpart(ar, it.position().pos_)) {
857 mathcursor->setSelection(it.cursor(), ar.size());
860 // I guess some of the following can go
861 bv->toggleSelection(true);
863 updateLocal(bv, true);
870 //lyxerr << "not found!\n";
872 // we have to unlock ourself in this function by default!
873 // don't ask me why...
874 bv->unlockInset(this);
879 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
882 lyxerr << "searching backward not implemented in mathed" << endl;
883 return searchForward(bv, what, a, b);
887 /////////////////////////////////////////////////////////////////////
890 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
892 if (bv->available()) {
893 // use selection if available..
895 //if (action == LFUN_MATH_IMPORT_SELECTION)
899 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
901 InsetFormulaBase * f;
903 f = new InsetFormula;
904 if (openNewInset(bv, f)) {
905 // don't do that also for LFUN_MATH_MODE unless you want end up with
906 // always changing to mathrm when opening an inlined inset
907 // -- I really hate "LyXfunc overloading"...
909 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
910 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
913 // create a macro if we see "\\newcommand" somewhere, and an ordinary
915 if (sel.find("\\newcommand") == string::npos &&
916 sel.find("\\def") == string::npos)
918 f = new InsetFormula(sel);
921 if (!mathed_parse_macro(name, sel))
923 f = new InsetFormulaMacro(sel);
925 bv->getLyXText()->cutSelection(bv);
929 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
933 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
935 mathDispatchCreation(bv, arg, true);
939 void mathDispatchMathMode(BufferView * bv, string const & arg)
941 mathDispatchCreation(bv, arg, false);
945 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
947 mathDispatchCreation(bv, arg, true);
951 void mathDispatchMathMacro(BufferView * bv, string const & arg)
953 if (bv->available()) {
955 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
958 string const s1 = token(s, ' ', 1);
959 int const na = s1.empty() ? 0 : lyx::atoi(s1);
960 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
966 void mathDispatchMathDelim(BufferView * bv, string const & arg)
968 if (bv->available()) {
969 if (openNewInset(bv, new InsetFormula))
970 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
975 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
977 if (bv->available()) {
978 if (openNewInset(bv, new InsetFormula))
979 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
984 void mathDispatchInsertMath(BufferView * bv, string const & arg)
986 if (bv->available()) {
987 if (arg.size() && arg[0] == '\\') {
988 InsetFormula * f = new InsetFormula(arg);
989 if (!bv->insertInset(f))
991 else if (!mathcursor) // hotfix
992 bv->getLyXText()->cursorRight(bv);
994 mathDispatchMathMode(bv, arg);
1000 void mathDispatchGreek(BufferView * bv, string const & arg)
1002 if (bv->available()) {
1003 InsetFormula * f = new InsetFormula;
1004 if (openNewInset(bv, f)) {
1005 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1012 void mathDispatch(BufferView *, kb_action, string const &)