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_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"
60 MathCursor * mathcursor = 0;
71 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
73 if (!bv->insertInset(new_inset)) {
77 new_inset->edit(bv, 0, 0, mouse_button::none);
86 InsetFormulaBase::InsetFormulaBase()
87 : view_(0), font_(), xo_(0), yo_(0)
89 // This is needed as long the math parser is not re-entrant
90 MathMacroTable::builtinMacros();
91 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
92 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
93 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
97 // simply scrap this function if you want
98 void InsetFormulaBase::mutateToText()
101 // translate to latex
103 latex(NULL, os, false, false);
104 string str = os.str();
107 LyXText * lt = view_->getLyXText();
108 string::const_iterator cit = str.begin();
109 string::const_iterator end = str.end();
110 for (; cit != end; ++cit)
111 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
114 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
119 // Check if uses AMS macros
120 void InsetFormulaBase::validate(LaTeXFeatures &) const
124 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
131 void InsetFormulaBase::metrics(BufferView * bv) const
135 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
140 string const InsetFormulaBase::editMessage() const
142 return _("Math editor mode");
146 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
148 if (!bv->lockInset(this))
149 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
151 mathcursor = new MathCursor(this, true);
153 mathcursor->setPos(x, y);
154 //lyxerr << "setting pos to " << x << "," << y << "\n";
156 // if that is removed, we won't get the magenta box when entering an
157 // inset for the first time
158 bv->updateInset(this, false);
162 void InsetFormulaBase::edit(BufferView * bv, bool front)
164 if (!bv->lockInset(this))
165 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
167 mathcursor = new MathCursor(this, front);
169 bv->updateInset(this, false);
173 void InsetFormulaBase::handleFont
174 (BufferView * bv, string const & arg, MathTextCodes t)
176 if (mathcursor->selection()) {
177 bv->lockedInsetStoreUndo(Undo::EDIT);
178 updateLocal(bv, true);
180 mathcursor->handleFont(t);
181 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
182 mathcursor->insert(*it);
183 updateLocal(bv, false);
187 void InsetFormulaBase::insetUnlock(BufferView * bv)
190 if (mathcursor->inMacroMode()) {
191 mathcursor->macroModeClose();
192 updateLocal(bv, true);
197 bv->updateInset(this, false);
201 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
203 mathcursor->getPos(x, y);
206 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
210 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
215 if (isCursorVisible())
216 bv->hideLockedInsetCursor();
221 mathcursor->getPos(x, y);
225 math_font_max_dim(font_, asc, des);
226 bv->showLockedInsetCursor(x, y, asc, des);
227 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
230 toggleCursorVisible();
234 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
236 if (isCursorVisible())
242 mathcursor->getPos(x, y);
246 math_font_max_dim(font_, asc, des);
247 bv->fitLockedInsetCursor(x, y, asc, des);
248 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
250 toggleInsetCursor(bv);
254 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
256 if (isCursorVisible())
257 toggleInsetCursor(bv);
261 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
264 bv->updateInset(this, false);
268 vector<string> const InsetFormulaBase::getLabelList() const
270 return vector<string>();
274 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
277 bv->updateInset(this, dirty);
281 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
282 int /*x*/, int /*y*/, mouse_button::state button)
284 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
290 bv->updateInset(this, false);
292 if (button == mouse_button::button3) {
293 // launch math panel for right mouse button
294 bv->owner()->getDialogs()->showMathPanel();
301 void InsetFormulaBase::insetButtonPress(BufferView * bv,
302 int x, int y, mouse_button::state button)
304 //lyxerr << "insetButtonPress: "
305 // << x << " " << y << " but: " << button << "\n";
312 mathcursor = new MathCursor(this, x == 0);
316 mathcursor->selClear();
317 mathcursor->setPos(x + xo_, y + yo_);
321 lyxerr << "insetButtonPress: 2\n";
324 bv->lockedInsetStoreUndo(Undo::EDIT);
326 mathcursor->selGet(ar);
327 mathcursor->setPos(x + xo_, y + yo_);
329 bv->getLyXText()->selectionAsString(bv->buffer(), false);
330 mathed_parse_cell(ar, sel);
331 mathcursor->insert(ar);
336 // launch math panel for right mouse button
337 bv->owner()->getDialogs()->showMathPanel();
341 if (button == mouse_button::button1 || !mathcursor) {
343 mathcursor = new MathCursor(this, x == 0);
347 mathcursor->selClear();
348 mathcursor->setPos(x + xo_, y + yo_);
351 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
353 // launch math panel for right mouse button
354 bv->owner()->getDialogs()->showMathPanel();
358 bv->updateInset(this, false);
362 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
363 int x, int y, mouse_button::state)
368 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
369 //lyxerr << "insetMotionNotify: ignored\n";
375 if (!mathcursor->selection())
376 mathcursor->selStart();
378 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
379 // << ' ' << button << "\n";
381 mathcursor->setPos(x + xo_, y + yo_);
383 bv->updateInset(this, false);
387 UpdatableInset::RESULT
388 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
391 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
392 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
397 RESULT result = DISPATCHED;
399 bool was_macro = mathcursor->inMacroMode();
400 bool was_selection = mathcursor->selection();
404 mathcursor->normalize();
409 // --- Cursor Movements ---------------------------------------------
412 sel = true; // fall through...
415 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
416 //lyxerr << "calling scroll 20\n";
418 updateLocal(bv, false);
419 // write something to the minibuffer
420 //bv->owner()->message(mathcursor->info());
425 sel = true; // fall through
428 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
429 updateLocal(bv, false);
437 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
438 updateLocal(bv, false);
446 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
447 updateLocal(bv, false);
454 mathcursor->home(sel);
455 updateLocal(bv, false);
462 mathcursor->end(sel);
463 updateLocal(bv, false);
466 case LFUN_DELETE_LINE_FORWARD:
467 bv->lockedInsetStoreUndo(Undo::DELETE);
468 mathcursor->delLine();
469 updateLocal(bv, true);
473 mathcursor->idxNext();
474 updateLocal(bv, false);
478 mathcursor->idxPrev();
479 updateLocal(bv, false);
483 bv->lockedInsetStoreUndo(Undo::EDIT);
484 mathcursor->splitCell();
485 updateLocal(bv, true);
488 case LFUN_DELETE_WORD_BACKWARD:
490 bv->lockedInsetStoreUndo(Undo::DELETE);
491 mathcursor->backspace();
492 bv->updateInset(this, true);
495 case LFUN_DELETE_WORD_FORWARD:
497 bv->lockedInsetStoreUndo(Undo::DELETE);
499 bv->updateInset(this, true);
503 // sprintf(dispatch_buffer, "%d %d",);
504 // dispatch_result = dispatch_buffer;
507 lyxerr << "LFUN_SETXY broken!\n";
510 istringstream is(arg.c_str());
512 mathcursor->setPos(x, y);
513 updateLocal(bv, false);
519 mathcursor->macroModeClose();
520 bv->lockedInsetStoreUndo(Undo::EDIT);
521 mathcursor->selPaste();
522 updateLocal(bv, true);
526 bv->lockedInsetStoreUndo(Undo::DELETE);
527 mathcursor->selCut();
528 updateLocal(bv, true);
532 mathcursor->selCopy();
535 case LFUN_WORDRIGHTSEL:
536 case LFUN_WORDLEFTSEL:
539 // Special casing for superscript in case of LyX handling
541 case LFUN_CIRCUMFLEX:
543 // do superscript if LyX handles
545 bv->lockedInsetStoreUndo(Undo::EDIT);
546 mathcursor->script(true);
547 updateLocal(bv, true);
563 case LFUN_HUNG_UMLAUT:
567 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
568 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
569 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
570 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
571 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
572 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
573 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
574 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
575 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
576 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
577 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
580 handleFont(bv, arg, LM_TC_GREEK1);
582 mathcursor->interpret(arg);
587 handleFont(bv, arg, LM_TC_TEXTRM);
591 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
592 updateLocal(bv, true);
595 //bv->owner()->message(_("math text mode toggled"));
598 case LFUN_MATH_LIMITS:
599 bv->lockedInsetStoreUndo(Undo::EDIT);
600 if (mathcursor->toggleLimits())
601 updateLocal(bv, true);
607 bv->lockedInsetStoreUndo(Undo::EDIT);
608 mathcursor->setSize(arg);
609 updateLocal(bv, true);
614 case LFUN_INSERT_MATRIX:
616 bv->lockedInsetStoreUndo(Undo::EDIT);
617 mathcursor->interpret("matrix " + arg);
618 updateLocal(bv, true);
622 case LFUN_SUPERSCRIPT:
625 bv->lockedInsetStoreUndo(Undo::EDIT);
626 mathcursor->script(action == LFUN_SUPERSCRIPT);
627 updateLocal(bv, true);
631 case LFUN_MATH_DELIM:
633 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
635 string rs = split(arg, ls, ' ');
636 // Reasonable default values
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->handleDelim(ls, rs);
644 updateLocal(bv, true);
648 case LFUN_PROTECTEDSPACE:
649 case LFUN_MATH_SPACE:
650 bv->lockedInsetStoreUndo(Undo::EDIT);
651 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
652 updateLocal(bv, true);
656 bv->owner()->message(_("Invalid action in math mode!"));
660 case LFUN_MATH_HALIGN:
661 case LFUN_MATH_VALIGN:
662 case LFUN_MATH_ROW_INSERT:
663 case LFUN_MATH_ROW_DELETE:
664 case LFUN_MATH_COLUMN_INSERT:
665 case LFUN_MATH_COLUMN_DELETE:
667 MathInset::idx_type idx = 0;
668 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
670 mathcursor->popToEnclosingGrid();
671 bv->lockedInsetStoreUndo(Undo::EDIT);
672 char align = arg.size() ? arg[0] : 'c';
674 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
675 case LFUN_MATH_VALIGN: p->valign(align); break;
676 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
677 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
678 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
679 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
682 updateLocal(bv, true);
687 case LFUN_EXEC_COMMAND:
688 result = UNDISPATCHED;
691 case LFUN_BREAKPARAGRAPH:
692 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
693 //lyxerr << "LFUN ignored\n";
697 // interpret this as if a backslash was typed
698 bv->lockedInsetStoreUndo(Undo::EDIT);
699 mathcursor->interpret('\\');
700 updateLocal(bv, true);
704 case LFUN_INSERT_MATH:
705 case LFUN_SELFINSERT:
707 bv->lockedInsetStoreUndo(Undo::EDIT);
709 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
711 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
712 updateLocal(bv, true);
716 case LFUN_MATH_PANEL:
717 result = UNDISPATCHED;
721 if (mathcursor->selection())
722 mathcursor->selClear();
724 result = UNDISPATCHED;
728 result = UNDISPATCHED;
731 mathcursor->normalize();
734 lyx::Assert(mathcursor);
736 if (mathcursor->selection() || was_selection)
737 toggleInsetSelection(bv);
739 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
740 result == UNDISPATCHED)
743 bv->unlockInset(this);
747 return result; // original version
751 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
756 // write something to the minibuffer
757 // translate to latex
758 mathcursor->markInsert();
761 string str = os.str();
762 mathcursor->markErase();
763 string::size_type pos = 0;
765 for (string::iterator it = str.begin(); it != str.end(); ++it) {
768 else if (*it == '\0') {
770 pos = it - str.begin();
776 res = res.substr(pos - 30);
778 res = res.substr(0, 60);
779 bv->owner()->message(res);
784 Inset::Code InsetFormulaBase::lyxCode() const
786 return Inset::MATH_CODE;
790 int InsetFormulaBase::ylow() const
792 return yo_ - ascent(view_, font_);
796 int InsetFormulaBase::yhigh() const
798 return yo_ + descent(view_, font_);
802 int InsetFormulaBase::xlow() const
808 int InsetFormulaBase::xhigh() const
810 return xo_ + width(view_, font_);
814 /////////////////////////////////////////////////////////////////////
817 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
823 static InsetFormulaBase * lastformula = 0;
824 static MathIterator current = MathIterator(ibegin(par().nucleus()));
826 static string laststr;
828 if (lastformula != this || laststr != str) {
829 //lyxerr << "reset lastformula to " << this << "\n";
832 current = ibegin(par().nucleus());
834 mathed_parse_cell(ar, str);
838 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
840 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
841 if (it.cell().matchpart(ar, it.position().pos_)) {
842 mathcursor->setSelection(it.cursor(), ar.size());
845 // I guess some of the following can go
846 bv->toggleSelection(true);
848 updateLocal(bv, true);
855 //lyxerr << "not found!\n";
857 // we have to unlock ourself in this function by default!
858 // don't ask me why...
859 bv->unlockInset(this);
864 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
867 lyxerr << "searching backward not implemented in mathed" << endl;
868 return searchForward(bv, what, a, b);
872 /////////////////////////////////////////////////////////////////////
875 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
877 if (bv->available()) {
878 // use selection if available..
880 //if (action == LFUN_MATH_IMPORT_SELECTION)
884 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
886 InsetFormulaBase * f;
888 f = new InsetFormula;
889 if (openNewInset(bv, f)) {
890 // don't do that also for LFUN_MATH_MODE unless you want end up with
891 // always changing to mathrm when opening an inlined inset
892 // -- I really hate "LyXfunc overloading"...
894 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
895 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
898 // create a macro if we see "\\newcommand" somewhere, and an ordinary
900 if (sel.find("\\newcommand") == string::npos &&
901 sel.find("\\def") == string::npos)
902 f = new InsetFormula(sel);
905 if (!mathed_parse_macro(name, sel))
907 f = new InsetFormulaMacro(sel);
909 bv->getLyXText()->cutSelection(bv);
913 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
917 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
919 mathDispatchCreation(bv, arg, true);
923 void mathDispatchMathMode(BufferView * bv, string const & arg)
925 mathDispatchCreation(bv, arg, false);
929 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
931 mathDispatchCreation(bv, arg, true);
935 void mathDispatchMathMacro(BufferView * bv, string const & arg)
937 if (bv->available()) {
939 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
942 string const s1 = token(s, ' ', 1);
943 int const na = s1.empty() ? 0 : lyx::atoi(s1);
944 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
950 void mathDispatchMathDelim(BufferView * bv, string const & arg)
952 if (bv->available()) {
953 if (openNewInset(bv, new InsetFormula))
954 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
959 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
961 if (bv->available()) {
962 if (openNewInset(bv, new InsetFormula))
963 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
968 void mathDispatchInsertMath(BufferView * bv, string const & arg)
970 if (bv->available()) {
971 if (arg.size() && arg[0] == '\\') {
972 InsetFormula * f = new InsetFormula(arg);
973 if (!bv->insertInset(f))
975 else if (!mathcursor) // hotfix
976 bv->getLyXText()->cursorRight(bv);
978 mathDispatchMathMode(bv, arg);
984 void mathDispatchGreek(BufferView * bv, string const & arg)
986 if (bv->available()) {
987 InsetFormula * f = new InsetFormula;
988 if (openNewInset(bv, f)) {
989 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
996 void mathDispatch(BufferView *, kb_action, string const &)