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"
61 MathCursor * mathcursor = 0;
72 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
74 if (!bv->insertInset(new_inset)) {
78 new_inset->edit(bv, 0, 0, mouse_button::none);
87 InsetFormulaBase::InsetFormulaBase()
88 : view_(0), font_(), xo_(0), yo_(0)
90 // This is needed as long the math parser is not re-entrant
91 MathMacroTable::builtinMacros();
92 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
93 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
94 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
95 //lyxerr << "sizeof(LyXFont): " << sizeof(LyXFont) << "\n";
99 // simply scrap this function if you want
100 void InsetFormulaBase::mutateToText()
103 // translate to latex
105 latex(NULL, os, false, false);
106 string str = os.str();
109 LyXText * lt = view_->getLyXText();
110 string::const_iterator cit = str.begin();
111 string::const_iterator end = str.end();
112 for (; cit != end; ++cit)
113 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
116 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
121 void InsetFormulaBase::handleFont
122 (BufferView * bv, string const & arg, string const & font)
124 bv->lockedInsetStoreUndo(Undo::EDIT);
125 bool sel = mathcursor->selection();
127 updateLocal(bv, true);
128 mathcursor->handleNest(new MathFontInset(font));
129 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
130 mathcursor->insert(*it);
132 updateLocal(bv, false);
136 // Check if uses AMS macros
137 void InsetFormulaBase::validate(LaTeXFeatures &) const
141 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
148 void InsetFormulaBase::metrics(BufferView * bv) const
154 mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
155 mi.base.font = font_;
156 mi.base.font.setColor(LColor::math);
161 string const InsetFormulaBase::editMessage() const
163 return _("Math editor mode");
167 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
169 if (!bv->lockInset(this))
170 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
172 mathcursor = new MathCursor(this, true);
174 mathcursor->setPos(x, y);
175 //lyxerr << "setting pos to " << x << "," << y << "\n";
177 // if that is removed, we won't get the magenta box when entering an
178 // inset for the first time
179 bv->updateInset(this, false);
183 void InsetFormulaBase::edit(BufferView * bv, bool front)
185 if (!bv->lockInset(this))
186 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
188 mathcursor = new MathCursor(this, front);
190 bv->updateInset(this, false);
194 void InsetFormulaBase::insetUnlock(BufferView * bv)
197 if (mathcursor->inMacroMode()) {
198 mathcursor->macroModeClose();
199 updateLocal(bv, true);
204 bv->updateInset(this, false);
208 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
211 mathcursor->getPos(x, y);
214 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
218 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
223 if (isCursorVisible())
224 bv->hideLockedInsetCursor();
229 mathcursor->getPos(x, y);
233 math_font_max_dim(font_, asc, des);
234 bv->showLockedInsetCursor(x, y, asc, des);
235 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
238 toggleCursorVisible();
242 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
244 if (isCursorVisible())
250 mathcursor->getPos(x, y);
254 math_font_max_dim(font_, asc, des);
255 bv->fitLockedInsetCursor(x, y, asc, des);
256 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
258 toggleInsetCursor(bv);
262 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
264 if (isCursorVisible())
265 toggleInsetCursor(bv);
269 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
274 int const asc = font_metrics::maxAscent(font_);
275 int const desc = font_metrics::maxDescent(font_);
278 getCursorPos(bv, x, y);
279 bv->fitLockedInsetCursor(x, y, asc, desc);
283 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
286 bv->updateInset(this, false);
290 vector<string> const InsetFormulaBase::getLabelList() const
292 return vector<string>();
296 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
301 bv->updateInset(this, dirty);
305 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
306 int /*x*/, int /*y*/, mouse_button::state button)
308 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
314 bv->updateInset(this, false);
316 if (button == mouse_button::button3) {
317 // launch math panel for right mouse button
318 bv->owner()->getDialogs()->showMathPanel();
325 void InsetFormulaBase::insetButtonPress(BufferView * bv,
326 int x, int y, mouse_button::state button)
328 //lyxerr << "insetButtonPress: "
329 // << x << " " << y << " but: " << button << "\n";
336 mathcursor = new MathCursor(this, x == 0);
340 mathcursor->selClear();
341 mathcursor->setPos(x + xo_, y + yo_);
345 lyxerr << "insetButtonPress: 2\n";
348 bv->lockedInsetStoreUndo(Undo::EDIT);
350 mathcursor->selGet(ar);
351 mathcursor->setPos(x + xo_, y + yo_);
353 bv->getLyXText()->selectionAsString(bv->buffer(), false);
354 mathed_parse_cell(ar, sel);
355 mathcursor->insert(ar);
360 // launch math panel for right mouse button
361 bv->owner()->getDialogs()->showMathPanel();
365 if (button == mouse_button::button1 || !mathcursor) {
367 mathcursor = new MathCursor(this, x == 0);
371 mathcursor->selClear();
372 mathcursor->setPos(x + xo_, y + yo_);
375 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
377 // launch math panel for right mouse button
378 bv->owner()->getDialogs()->showMathPanel();
382 bv->updateInset(this, false);
386 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
387 int x, int y, mouse_button::state)
392 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
393 //lyxerr << "insetMotionNotify: ignored\n";
399 if (!mathcursor->selection())
400 mathcursor->selStart();
402 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
403 // << ' ' << button << "\n";
405 mathcursor->setPos(x + xo_, y + yo_);
407 bv->updateInset(this, false);
411 UpdatableInset::RESULT
412 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
415 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
416 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
421 RESULT result = DISPATCHED;
423 bool was_macro = mathcursor->inMacroMode();
424 bool was_selection = mathcursor->selection();
428 mathcursor->normalize();
433 // --- Cursor Movements ---------------------------------------------
436 sel = true; // fall through...
439 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
440 //lyxerr << "calling scroll 20\n";
442 updateLocal(bv, false);
443 // write something to the minibuffer
444 //bv->owner()->message(mathcursor->info());
449 sel = true; // fall through
452 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
453 updateLocal(bv, false);
461 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
462 updateLocal(bv, false);
470 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
471 updateLocal(bv, false);
478 mathcursor->home(sel);
479 updateLocal(bv, false);
486 mathcursor->end(sel);
487 updateLocal(bv, false);
490 case LFUN_DELETE_LINE_FORWARD:
491 bv->lockedInsetStoreUndo(Undo::DELETE);
492 mathcursor->delLine();
493 updateLocal(bv, true);
497 mathcursor->idxNext();
498 updateLocal(bv, false);
502 mathcursor->idxPrev();
503 updateLocal(bv, false);
507 bv->lockedInsetStoreUndo(Undo::EDIT);
508 mathcursor->splitCell();
509 updateLocal(bv, true);
512 case LFUN_DELETE_WORD_BACKWARD:
514 bv->lockedInsetStoreUndo(Undo::DELETE);
515 mathcursor->backspace();
516 bv->updateInset(this, true);
519 case LFUN_DELETE_WORD_FORWARD:
521 bv->lockedInsetStoreUndo(Undo::DELETE);
523 bv->updateInset(this, true);
527 // sprintf(dispatch_buffer, "%d %d",);
528 // dispatch_result = dispatch_buffer;
531 lyxerr << "LFUN_SETXY broken!\n";
534 istringstream is(arg.c_str());
536 mathcursor->setPos(x, y);
537 updateLocal(bv, false);
543 mathcursor->macroModeClose();
544 bv->lockedInsetStoreUndo(Undo::EDIT);
545 mathcursor->selPaste();
546 updateLocal(bv, true);
550 bv->lockedInsetStoreUndo(Undo::DELETE);
551 mathcursor->selCut();
552 updateLocal(bv, true);
556 mathcursor->selCopy();
559 case LFUN_WORDRIGHTSEL:
560 case LFUN_WORDLEFTSEL:
563 // Special casing for superscript in case of LyX handling
565 case LFUN_CIRCUMFLEX:
567 // do superscript if LyX handles
569 bv->lockedInsetStoreUndo(Undo::EDIT);
570 mathcursor->script(true);
571 updateLocal(bv, true);
587 case LFUN_HUNG_UMLAUT:
591 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
592 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
593 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
594 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
595 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
596 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
597 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
598 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
599 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
600 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
601 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
604 handleFont(bv, arg, "lyxgreek1");
606 mathcursor->interpret(arg);
610 if (mathcursor->inMathMode()) {
611 handleFont(bv, arg, "textrm");
613 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
614 updateLocal(bv, true);
616 //bv->owner()->message(_("math text mode toggled"));
619 case LFUN_MATH_LIMITS:
620 bv->lockedInsetStoreUndo(Undo::EDIT);
621 if (mathcursor->toggleLimits())
622 updateLocal(bv, true);
628 bv->lockedInsetStoreUndo(Undo::EDIT);
629 mathcursor->setSize(arg);
630 updateLocal(bv, true);
635 case LFUN_INSERT_MATRIX:
637 bv->lockedInsetStoreUndo(Undo::EDIT);
638 mathcursor->interpret("matrix " + arg);
639 updateLocal(bv, true);
643 case LFUN_SUPERSCRIPT:
646 bv->lockedInsetStoreUndo(Undo::EDIT);
647 mathcursor->script(action == LFUN_SUPERSCRIPT);
648 updateLocal(bv, true);
652 case LFUN_MATH_DELIM:
654 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
656 string rs = split(arg, ls, ' ');
657 // Reasonable default values
663 bv->lockedInsetStoreUndo(Undo::EDIT);
664 mathcursor->handleDelim(ls, rs);
665 updateLocal(bv, true);
669 case LFUN_PROTECTEDSPACE:
670 case LFUN_MATH_SPACE:
671 bv->lockedInsetStoreUndo(Undo::EDIT);
672 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
673 updateLocal(bv, true);
677 bv->owner()->message(_("Invalid action in math mode!"));
681 case LFUN_MATH_HALIGN:
682 case LFUN_MATH_VALIGN:
683 case LFUN_MATH_ROW_INSERT:
684 case LFUN_MATH_ROW_DELETE:
685 case LFUN_MATH_COLUMN_INSERT:
686 case LFUN_MATH_COLUMN_DELETE:
688 MathInset::idx_type idx = 0;
689 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
691 mathcursor->popToEnclosingGrid();
692 bv->lockedInsetStoreUndo(Undo::EDIT);
693 char align = arg.size() ? arg[0] : 'c';
695 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
696 case LFUN_MATH_VALIGN: p->valign(align); break;
697 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
698 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
699 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
700 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
703 updateLocal(bv, true);
708 case LFUN_EXEC_COMMAND:
709 result = UNDISPATCHED;
712 case LFUN_BREAKPARAGRAPH:
713 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
714 //lyxerr << "LFUN ignored\n";
718 // interpret this as if a backslash was typed
719 bv->lockedInsetStoreUndo(Undo::EDIT);
720 mathcursor->interpret('\\');
721 updateLocal(bv, true);
725 case LFUN_INSERT_MATH:
726 case LFUN_SELFINSERT:
728 bv->lockedInsetStoreUndo(Undo::EDIT);
730 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
732 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
733 updateLocal(bv, true);
737 case LFUN_MATH_PANEL:
738 result = UNDISPATCHED;
742 if (mathcursor->selection())
743 mathcursor->selClear();
745 result = UNDISPATCHED;
748 case LFUN_INSET_TOGGLE:
749 mathcursor->insetToggle();
750 updateLocal(bv, true);
754 result = UNDISPATCHED;
757 mathcursor->normalize();
760 lyx::Assert(mathcursor);
762 if (mathcursor->selection() || was_selection)
763 toggleInsetSelection(bv);
765 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
766 result == UNDISPATCHED)
769 bv->unlockInset(this);
773 return result; // original version
777 void InsetFormulaBase::revealCodes(BufferView * bv) const
781 bv->owner()->message(mathcursor->info());
784 // write something to the minibuffer
785 // translate to latex
786 mathcursor->markInsert();
789 string str = os.str();
790 mathcursor->markErase();
791 string::size_type pos = 0;
793 for (string::iterator it = str.begin(); it != str.end(); ++it) {
796 else if (*it == '\0') {
798 pos = it - str.begin();
804 res = res.substr(pos - 30);
806 res = res.substr(0, 60);
807 bv->owner()->message(res);
812 Inset::Code InsetFormulaBase::lyxCode() const
814 return Inset::MATH_CODE;
818 int InsetFormulaBase::ylow() const
820 return yo_ - ascent(view_, font_);
824 int InsetFormulaBase::yhigh() const
826 return yo_ + descent(view_, font_);
830 int InsetFormulaBase::xlow() const
836 int InsetFormulaBase::xhigh() const
838 return xo_ + width(view_, font_);
842 /////////////////////////////////////////////////////////////////////
845 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
851 static InsetFormulaBase * lastformula = 0;
852 static MathIterator current = MathIterator(ibegin(par().nucleus()));
854 static string laststr;
856 if (lastformula != this || laststr != str) {
857 //lyxerr << "reset lastformula to " << this << "\n";
860 current = ibegin(par().nucleus());
862 mathed_parse_cell(ar, str);
866 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
868 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
869 if (it.cell().matchpart(ar, it.position().pos_)) {
870 bv->unlockInset(bv->theLockingInset());
871 if (!bv->lockInset(this)) {
872 lyxerr << "Cannot lock inset" << endl;
876 mathcursor = new MathCursor(this, true);
878 mathcursor->setSelection(it.cursor(), ar.size());
881 updateLocal(bv, false);
886 //lyxerr << "not found!\n";
892 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
895 lyxerr << "searching backward not implemented in mathed" << endl;
896 return searchForward(bv, what, a, b);
900 /////////////////////////////////////////////////////////////////////
903 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
905 if (bv->available()) {
906 // use selection if available..
908 //if (action == LFUN_MATH_IMPORT_SELECTION)
912 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
914 InsetFormulaBase * f;
916 f = new InsetFormula;
917 if (openNewInset(bv, f)) {
918 // don't do that also for LFUN_MATH_MODE unless you want end up with
919 // always changing to mathrm when opening an inlined inset
920 // -- I really hate "LyXfunc overloading"...
922 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
923 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
926 // create a macro if we see "\\newcommand" somewhere, and an ordinary
928 if (sel.find("\\newcommand") == string::npos &&
929 sel.find("\\def") == string::npos)
931 f = new InsetFormula(sel);
934 if (!mathed_parse_macro(name, sel))
936 f = new InsetFormulaMacro(sel);
938 bv->getLyXText()->cutSelection(bv);
942 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
946 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
948 mathDispatchCreation(bv, arg, true);
952 void mathDispatchMathMode(BufferView * bv, string const & arg)
954 mathDispatchCreation(bv, arg, false);
958 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
960 mathDispatchCreation(bv, arg, true);
964 void mathDispatchMathMacro(BufferView * bv, string const & arg)
966 if (bv->available()) {
968 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
971 string const s1 = token(s, ' ', 1);
972 int const na = s1.empty() ? 0 : lyx::atoi(s1);
973 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
979 void mathDispatchMathDelim(BufferView * bv, string const & arg)
981 if (bv->available()) {
982 if (openNewInset(bv, new InsetFormula))
983 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
988 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
990 if (bv->available()) {
991 if (openNewInset(bv, new InsetFormula))
992 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
997 void mathDispatchInsertMath(BufferView * bv, string const & arg)
999 if (bv->available()) {
1000 if (arg.size() && arg[0] == '\\') {
1001 InsetFormula * f = new InsetFormula(arg);
1002 if (!bv->insertInset(f))
1004 else if (!mathcursor) // hotfix
1005 bv->getLyXText()->cursorRight(bv);
1007 mathDispatchMathMode(bv, arg);
1013 void mathDispatchGreek(BufferView * bv, string const & arg)
1015 if (bv->available()) {
1016 InsetFormula * f = new InsetFormula;
1017 if (openNewInset(bv, f)) {
1018 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1025 void mathDispatch(BufferView *, kb_action, string const &)
1029 void mathDispatch(BufferView *, string const &)