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"
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"
59 MathCursor * mathcursor = 0;
70 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
72 if (mathcursor->selection())
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->handleFont(t);
75 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
76 mathcursor->insert(*it);
80 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
82 if (!bv->insertInset(new_inset)) {
86 new_inset->edit(bv, 0, 0, 0);
95 InsetFormulaBase::InsetFormulaBase()
96 : view_(0), font_(), xo_(0), yo_(0)
98 // This is needed as long the math parser is not re-entrant
99 MathMacroTable::builtinMacros();
100 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
101 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
102 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
106 // simply scrap this function if you want
107 void InsetFormulaBase::mutateToText()
110 // translate to latex
112 latex(NULL, os, false, false);
113 string str = os.str();
116 LyXText * lt = view_->getLyXText();
117 string::const_iterator cit = str.begin();
118 string::const_iterator end = str.end();
119 for (; cit != end; ++cit)
120 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
123 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
128 // Check if uses AMS macros
129 void InsetFormulaBase::validate(LaTeXFeatures &) const
133 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
140 void InsetFormulaBase::metrics(BufferView * bv) const
144 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
149 string const InsetFormulaBase::editMessage() const
151 return _("Math editor mode");
155 void InsetFormulaBase::edit(BufferView * bv, int x, int y, unsigned int)
157 if (!bv->lockInset(this))
158 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
160 mathcursor = new MathCursor(this, true);
162 mathcursor->setPos(x, y);
163 //lyxerr << "setting pos to " << x << "," << y << "\n";
165 // if that is removed, we won't get the magenta box when entering an
166 // inset for the first time
167 bv->updateInset(this, false);
171 void InsetFormulaBase::edit(BufferView * bv, bool front)
173 if (!bv->lockInset(this))
174 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
176 mathcursor = new MathCursor(this, front);
178 bv->updateInset(this, false);
182 void InsetFormulaBase::insetUnlock(BufferView * bv)
185 if (mathcursor->inMacroMode()) {
186 mathcursor->macroModeClose();
187 updateLocal(bv, true);
192 bv->updateInset(this, false);
196 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
198 mathcursor->getPos(x, y);
201 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
205 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
210 if (isCursorVisible())
211 bv->hideLockedInsetCursor();
216 mathcursor->getPos(x, y);
220 math_font_max_dim(font_, asc, des);
221 bv->showLockedInsetCursor(x, y, asc, des);
222 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
225 toggleCursorVisible();
229 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
231 if (isCursorVisible())
237 mathcursor->getPos(x, y);
241 math_font_max_dim(font_, asc, des);
242 bv->fitLockedInsetCursor(x, y, asc, des);
243 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
245 toggleInsetCursor(bv);
249 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
251 if (isCursorVisible())
252 toggleInsetCursor(bv);
256 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
259 bv->updateInset(this, false);
263 vector<string> const InsetFormulaBase::getLabelList() const
265 return vector<string>();
269 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
272 bv->updateInset(this, dirty);
276 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
277 int /*x*/, int /*y*/, int button)
279 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
285 bv->updateInset(this, false);
288 // launch math panel for right mouse button
289 bv->owner()->getDialogs()->showMathPanel();
296 void InsetFormulaBase::insetButtonPress(BufferView * bv,
297 int x, int y, int button)
299 //lyxerr << "insetButtonPress: "
300 // << x << " " << y << " but: " << button << "\n";
307 mathcursor = new MathCursor(this, x == 0);
311 mathcursor->selClear();
312 mathcursor->setPos(x + xo_, y + yo_);
316 lyxerr << "insetButtonPress: 2\n";
319 bv->lockedInsetStoreUndo(Undo::EDIT);
321 mathcursor->selGet(ar);
322 mathcursor->setPos(x + xo_, y + yo_);
324 bv->getLyXText()->selectionAsString(bv->buffer(), false);
325 mathed_parse_cell(ar, sel);
326 mathcursor->insert(ar);
331 // launch math panel for right mouse button
332 bv->owner()->getDialogs()->showMathPanel();
336 if (button == 1 || !mathcursor) {
338 mathcursor = new MathCursor(this, x == 0);
342 mathcursor->selClear();
343 mathcursor->setPos(x + xo_, y + yo_);
346 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
348 // launch math panel for right mouse button
349 bv->owner()->getDialogs()->showMathPanel();
353 bv->updateInset(this, false);
357 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
358 int x, int y, int /*button*/)
363 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
364 //lyxerr << "insetMotionNotify: ignored\n";
370 if (!mathcursor->selection())
371 mathcursor->selStart();
373 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
374 // << ' ' << button << "\n";
376 mathcursor->setPos(x + xo_, y + yo_);
378 bv->updateInset(this, false);
382 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
384 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
388 UpdatableInset::RESULT
389 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
392 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
393 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
398 RESULT result = DISPATCHED;
400 bool was_macro = mathcursor->inMacroMode();
401 bool was_selection = mathcursor->selection();
405 mathcursor->normalize();
410 // --- Cursor Movements ---------------------------------------------
413 sel = true; // fall through...
416 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
417 //lyxerr << "calling scroll 20\n";
419 updateLocal(bv, false);
420 // write something to the minibuffer
421 //bv->owner()->message(mathcursor->info());
426 sel = true; // fall through
429 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
430 updateLocal(bv, false);
438 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
439 updateLocal(bv, false);
447 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
448 updateLocal(bv, false);
455 mathcursor->home(sel);
456 updateLocal(bv, false);
463 mathcursor->end(sel);
464 updateLocal(bv, false);
467 case LFUN_DELETE_LINE_FORWARD:
468 bv->lockedInsetStoreUndo(Undo::DELETE);
469 mathcursor->delLine();
470 updateLocal(bv, true);
474 mathcursor->idxNext();
475 updateLocal(bv, false);
479 mathcursor->idxPrev();
480 updateLocal(bv, false);
484 bv->lockedInsetStoreUndo(Undo::EDIT);
485 mathcursor->splitCell();
486 updateLocal(bv, true);
489 case LFUN_DELETE_WORD_BACKWARD:
491 bv->lockedInsetStoreUndo(Undo::DELETE);
492 mathcursor->backspace();
493 bv->updateInset(this, true);
496 case LFUN_DELETE_WORD_FORWARD:
498 bv->lockedInsetStoreUndo(Undo::DELETE);
500 bv->updateInset(this, true);
504 // sprintf(dispatch_buffer, "%d %d",);
505 // dispatch_result = dispatch_buffer;
508 lyxerr << "LFUN_SETXY broken!\n";
511 istringstream is(arg.c_str());
513 mathcursor->setPos(x, y);
514 updateLocal(bv, false);
520 mathcursor->macroModeClose();
521 bv->lockedInsetStoreUndo(Undo::EDIT);
522 mathcursor->selPaste();
523 updateLocal(bv, true);
527 bv->lockedInsetStoreUndo(Undo::DELETE);
528 mathcursor->selCut();
529 updateLocal(bv, true);
533 mathcursor->selCopy();
536 case LFUN_WORDRIGHTSEL:
537 case LFUN_WORDLEFTSEL:
540 // Special casing for superscript in case of LyX handling
542 case LFUN_CIRCUMFLEX:
544 // do superscript if LyX handles
546 bv->lockedInsetStoreUndo(Undo::EDIT);
547 mathcursor->script(true);
548 updateLocal(bv, true);
564 case LFUN_HUNG_UMLAUT:
568 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
569 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
570 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
571 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
572 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
573 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
574 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
575 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
576 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
577 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
578 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
581 handleFont(bv, arg, LM_TC_GREEK1);
583 mathcursor->interpret(arg);
588 handleFont(bv, arg, LM_TC_TEXTRM);
592 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
593 updateLocal(bv, true);
596 //bv->owner()->message(_("math text mode toggled"));
599 case LFUN_MATH_LIMITS:
600 bv->lockedInsetStoreUndo(Undo::EDIT);
601 if (mathcursor->toggleLimits())
602 updateLocal(bv, true);
608 bv->lockedInsetStoreUndo(Undo::EDIT);
609 mathcursor->setSize(arg);
610 updateLocal(bv, true);
615 case LFUN_INSERT_MATRIX:
617 bv->lockedInsetStoreUndo(Undo::EDIT);
618 mathcursor->interpret("matrix " + arg);
619 updateLocal(bv, true);
623 case LFUN_SUPERSCRIPT:
626 bv->lockedInsetStoreUndo(Undo::EDIT);
627 mathcursor->script(action == LFUN_SUPERSCRIPT);
628 updateLocal(bv, true);
632 case LFUN_MATH_DELIM:
634 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
636 string rs = split(arg, ls, ' ');
637 // Reasonable default values
643 bv->lockedInsetStoreUndo(Undo::EDIT);
644 mathcursor->handleDelim(ls, rs);
645 updateLocal(bv, true);
649 case LFUN_PROTECTEDSPACE:
650 case LFUN_MATH_SPACE:
651 bv->lockedInsetStoreUndo(Undo::EDIT);
652 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
653 updateLocal(bv, true);
657 bv->owner()->message(_("Invalid action in math mode!"));
661 case LFUN_MATH_HALIGN:
662 case LFUN_MATH_VALIGN:
663 case LFUN_MATH_ROW_INSERT:
664 case LFUN_MATH_ROW_DELETE:
665 case LFUN_MATH_COLUMN_INSERT:
666 case LFUN_MATH_COLUMN_DELETE:
668 MathInset::idx_type idx = 0;
669 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
671 mathcursor->popToEnclosingGrid();
672 bv->lockedInsetStoreUndo(Undo::EDIT);
673 char align = arg.size() ? arg[0] : 'c';
675 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
676 case LFUN_MATH_VALIGN: p->valign(align); break;
677 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
678 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
679 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
680 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
683 updateLocal(bv, true);
688 case LFUN_EXEC_COMMAND:
689 result = UNDISPATCHED;
692 case LFUN_BREAKPARAGRAPH:
693 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
694 //lyxerr << "LFUN ignored\n";
698 // interpret this as if a backslash was typed
699 bv->lockedInsetStoreUndo(Undo::EDIT);
700 mathcursor->interpret('\\');
701 updateLocal(bv, true);
705 case LFUN_INSERT_MATH:
706 case LFUN_SELFINSERT:
708 bv->lockedInsetStoreUndo(Undo::EDIT);
710 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
712 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
713 updateLocal(bv, true);
717 case LFUN_MATH_PANEL:
718 result = UNDISPATCHED;
722 if (mathcursor->selection())
723 mathcursor->selClear();
725 result = UNDISPATCHED;
729 result = UNDISPATCHED;
732 mathcursor->normalize();
735 lyx::Assert(mathcursor);
737 if (mathcursor->selection() || was_selection)
738 toggleInsetSelection(bv);
740 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
741 result == UNDISPATCHED)
744 bv->unlockInset(this);
748 return result; // original version
752 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
757 // write something to the minibuffer
758 // translate to latex
759 mathcursor->markInsert();
762 string str = os.str();
763 mathcursor->markErase();
764 string::size_type pos = 0;
766 for (string::iterator it = str.begin(); it != str.end(); ++it) {
769 else if (*it == '\0') {
771 pos = it - str.begin();
777 res = res.substr(pos - 30);
779 res = res.substr(0, 60);
780 bv->owner()->message(res);
785 Inset::Code InsetFormulaBase::lyxCode() const
787 return Inset::MATH_CODE;
791 int InsetFormulaBase::ylow() const
793 return yo_ - ascent(view_, font_);
797 int InsetFormulaBase::yhigh() const
799 return yo_ + descent(view_, font_);
803 int InsetFormulaBase::xlow() const
809 int InsetFormulaBase::xhigh() const
811 return xo_ + width(view_, font_);
815 /////////////////////////////////////////////////////////////////////
818 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
824 static InsetFormulaBase * lastformula = 0;
825 static MathIterator current = MathIterator(ibegin(par().nucleus()));
827 static string laststr;
829 if (lastformula != this || laststr != str) {
830 //lyxerr << "reset lastformula to " << this << "\n";
833 current = ibegin(par().nucleus());
835 mathed_parse_cell(ar, str);
839 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
841 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
842 if (it.cell().matchpart(ar, it.position().pos_)) {
843 mathcursor->setSelection(it.cursor(), ar.size());
846 // I guess some of the following can go
847 bv->toggleSelection(true);
849 updateLocal(bv, true);
856 //lyxerr << "not found!\n";
858 // we have to unlock ourself in this function by default!
859 // don't ask me why...
860 bv->unlockInset(this);
865 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
868 lyxerr << "searching backward not implemented in mathed" << endl;
869 return searchForward(bv, what, a, b);
873 /////////////////////////////////////////////////////////////////////
876 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
878 if (bv->available()) {
879 // use selection if available..
881 //if (action == LFUN_MATH_IMPORT_SELECTION)
885 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
887 InsetFormulaBase * f;
889 f = new InsetFormula;
890 if (openNewInset(bv, f)) {
891 // don't do that also for LFUN_MATH_MODE unless you want end up with
892 // always changing to mathrm when opening an inlined inset
893 // -- I really hate "LyXfunc overloading"...
895 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
896 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
899 // create a macro if we see "\\newcommand" somewhere, and an ordinary
901 if (sel.find("\\newcommand") == string::npos &&
902 sel.find("\\def") == string::npos)
903 f = new InsetFormula(sel);
906 if (!mathed_parse_macro(name, sel))
908 f = new InsetFormulaMacro(sel);
910 bv->getLyXText()->cutSelection(bv);
914 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
918 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
920 mathDispatchCreation(bv, arg, true);
924 void mathDispatchMathMode(BufferView * bv, string const & arg)
926 mathDispatchCreation(bv, arg, false);
930 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
932 mathDispatchCreation(bv, arg, true);
936 void mathDispatchMathMacro(BufferView * bv, string const & arg)
938 if (bv->available()) {
940 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
943 string const s1 = token(s, ' ', 1);
944 int const na = s1.empty() ? 0 : lyx::atoi(s1);
945 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
951 void mathDispatchMathDelim(BufferView * bv, string const & arg)
953 if (bv->available()) {
954 if (openNewInset(bv, new InsetFormula))
955 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
960 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
962 if (bv->available()) {
963 if (openNewInset(bv, new InsetFormula))
964 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
969 void mathDispatchInsertMath(BufferView * bv, string const & arg)
971 if (bv->available()) {
972 if (arg.size() && arg[0] == '\\') {
973 InsetFormula * f = new InsetFormula(arg);
974 if (!bv->insertInset(f))
976 else if (!mathcursor) // hotfix
977 bv->getLyXText()->cursorRight(bv);
979 mathDispatchMathMode(bv, arg);
985 void mathDispatchGreek(BufferView * bv, string const & arg)
987 if (bv->available()) {
988 InsetFormula * f = new InsetFormula;
989 if (openNewInset(bv, f)) {
990 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
997 void mathDispatch(BufferView *, kb_action, string const &)