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 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
158 if (!bv->lockInset(this))
159 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
162 mathcursor = new MathCursor(this, x == 0);
164 // if that is removed, we won't get the magenta box when entering an
165 // inset for the first time
166 bv->updateInset(this, false);
170 void InsetFormulaBase::edit(BufferView * bv, bool front)
172 // looks hackish but seems to work
173 edit(bv, front ? 0 : 1, 0, 0);
177 void InsetFormulaBase::insetUnlock(BufferView * bv)
180 if (mathcursor->inMacroMode()) {
181 mathcursor->macroModeClose();
182 updateLocal(bv, true);
187 bv->updateInset(this, false);
191 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
193 mathcursor->getPos(x, y);
196 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
200 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
205 if (isCursorVisible())
206 bv->hideLockedInsetCursor();
211 mathcursor->getPos(x, y);
215 math_font_max_dim(font_, asc, des);
216 bv->showLockedInsetCursor(x, y, asc, des);
217 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
220 toggleCursorVisible();
224 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
226 if (isCursorVisible())
232 mathcursor->getPos(x, y);
236 math_font_max_dim(font_, asc, des);
237 bv->fitLockedInsetCursor(x, y, asc, des);
238 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
240 toggleInsetCursor(bv);
244 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
246 if (isCursorVisible())
247 toggleInsetCursor(bv);
251 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
254 bv->updateInset(this, false);
258 vector<string> const InsetFormulaBase::getLabelList() const
260 return vector<string>();
264 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
267 bv->updateInset(this, dirty);
271 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
272 int /*x*/, int /*y*/, int /*button*/)
274 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
279 bv->updateInset(this, false);
284 void InsetFormulaBase::insetButtonPress(BufferView * bv,
285 int x, int y, int button)
287 //lyxerr << "insetButtonPress: "
288 // << x << " " << y << " but: " << button << "\n";
295 mathcursor = new MathCursor(this, x == 0);
300 mathcursor->selClear();
301 mathcursor->setPos(x + xo_, y + yo_);
305 lyxerr << "insetButtonPress: 2\n";
308 bv->lockedInsetStoreUndo(Undo::EDIT);
310 mathcursor->selGet(ar);
311 mathcursor->setPos(x + xo_, y + yo_);
313 bv->getLyXText()->selectionAsString(bv->buffer(), false);
314 mathed_parse_cell(ar, sel);
315 mathcursor->insert(ar);
320 // launch math panel for right mouse button
321 bv->owner()->getDialogs()->showMathPanel();
324 bv->updateInset(this, false);
328 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
329 int x, int y, int /*button*/)
334 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
335 //lyxerr << "insetMotionNotify: ignored\n";
341 if (!mathcursor->selection())
342 mathcursor->selStart();
344 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
345 // << ' ' << button << "\n";
347 mathcursor->setPos(x + xo_, y + yo_);
349 bv->updateInset(this, false);
353 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
355 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
359 UpdatableInset::RESULT
360 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
363 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
364 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
369 if (mathcursor->asHyperActiveInset()) {
370 lyxerr << " uurr.... getting dificult now\n";
371 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
374 RESULT result = DISPATCHED;
376 bool was_macro = mathcursor->inMacroMode();
377 bool was_selection = mathcursor->selection();
381 mathcursor->normalize();
386 // --- Cursor Movements ---------------------------------------------
389 sel = true; // fall through...
392 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
393 //lyxerr << "calling scroll 20\n";
395 updateLocal(bv, false);
396 // write something to the minibuffer
397 //bv->owner()->message(mathcursor->info());
402 sel = true; // fall through
405 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
406 updateLocal(bv, false);
414 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
415 updateLocal(bv, false);
423 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
424 updateLocal(bv, false);
431 mathcursor->home(sel);
432 updateLocal(bv, false);
439 mathcursor->end(sel);
440 updateLocal(bv, false);
443 case LFUN_DELETE_LINE_FORWARD:
444 bv->lockedInsetStoreUndo(Undo::DELETE);
445 mathcursor->delLine();
446 updateLocal(bv, true);
450 mathcursor->idxNext();
451 updateLocal(bv, false);
455 mathcursor->idxPrev();
456 updateLocal(bv, false);
460 bv->lockedInsetStoreUndo(Undo::EDIT);
461 mathcursor->splitCell();
462 updateLocal(bv, true);
465 case LFUN_DELETE_WORD_BACKWARD:
467 bv->lockedInsetStoreUndo(Undo::DELETE);
468 mathcursor->backspace();
469 bv->updateInset(this, true);
472 case LFUN_DELETE_WORD_FORWARD:
474 bv->lockedInsetStoreUndo(Undo::DELETE);
476 bv->updateInset(this, true);
480 // sprintf(dispatch_buffer, "%d %d",);
481 // dispatch_result = dispatch_buffer;
484 lyxerr << "LFUN_SETXY broken!\n";
487 istringstream is(arg.c_str());
489 mathcursor->setPos(x, y);
490 updateLocal(bv, false);
496 mathcursor->macroModeClose();
497 bv->lockedInsetStoreUndo(Undo::EDIT);
498 mathcursor->selPaste();
499 updateLocal(bv, true);
503 bv->lockedInsetStoreUndo(Undo::DELETE);
504 mathcursor->selCut();
505 updateLocal(bv, true);
509 mathcursor->selCopy();
512 case LFUN_WORDRIGHTSEL:
513 case LFUN_WORDLEFTSEL:
516 // Special casing for superscript in case of LyX handling
518 case LFUN_CIRCUMFLEX:
520 // do superscript if LyX handles
522 bv->lockedInsetStoreUndo(Undo::EDIT);
523 mathcursor->script(true);
524 updateLocal(bv, true);
540 case LFUN_HUNG_UMLAUT:
544 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
545 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
546 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
547 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
548 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
549 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
550 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
551 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
552 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
553 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
554 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
557 handleFont(bv, arg, LM_TC_GREEK1);
559 mathcursor->interpret(arg);
564 handleFont(bv, arg, LM_TC_TEXTRM);
568 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
569 updateLocal(bv, true);
572 //bv->owner()->message(_("math text mode toggled"));
575 case LFUN_MATH_LIMITS:
576 bv->lockedInsetStoreUndo(Undo::EDIT);
577 if (mathcursor->toggleLimits())
578 updateLocal(bv, true);
584 bv->lockedInsetStoreUndo(Undo::EDIT);
585 mathcursor->setSize(arg);
586 updateLocal(bv, true);
591 case LFUN_INSERT_MATRIX:
593 bv->lockedInsetStoreUndo(Undo::EDIT);
594 mathcursor->interpret("matrix " + arg);
595 updateLocal(bv, true);
599 case LFUN_MATH_SPACE:
601 bv->lockedInsetStoreUndo(Undo::EDIT);
602 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
603 updateLocal(bv, true);
607 case LFUN_SUPERSCRIPT:
610 bv->lockedInsetStoreUndo(Undo::EDIT);
611 mathcursor->script((action == LFUN_SUPERSCRIPT));
612 updateLocal(bv, true);
616 case LFUN_MATH_DELIM:
618 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
621 istringstream is(arg.c_str());
624 lyxerr << "can't parse delimeters from '" << arg << "'\n";
627 bv->lockedInsetStoreUndo(Undo::EDIT);
628 mathcursor->handleDelim(ls, rs);
629 updateLocal(bv, true);
633 case LFUN_PROTECTEDSPACE:
634 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
635 bv->lockedInsetStoreUndo(Undo::EDIT);
636 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
637 updateLocal(bv, true);
641 bv->owner()->message(_("Invalid action in math mode!"));
645 case LFUN_MATH_HALIGN:
646 case LFUN_MATH_VALIGN:
647 case LFUN_MATH_ROW_INSERT:
648 case LFUN_MATH_ROW_DELETE:
649 case LFUN_MATH_COLUMN_INSERT:
650 case LFUN_MATH_COLUMN_DELETE:
652 MathInset::idx_type idx = 0;
653 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
655 mathcursor->popToEnclosingGrid();
656 bv->lockedInsetStoreUndo(Undo::EDIT);
657 char align = arg.size() ? arg[0] : 'c';
659 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
660 case LFUN_MATH_VALIGN: p->valign(align); break;
661 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
662 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
663 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
664 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
667 updateLocal(bv, true);
672 case LFUN_EXEC_COMMAND:
673 result = UNDISPATCHED;
676 case LFUN_BREAKPARAGRAPH:
677 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
678 //lyxerr << "LFUN ignored\n";
682 // interpret this as if a backslash was typed
683 bv->lockedInsetStoreUndo(Undo::EDIT);
684 mathcursor->interpret('\\');
685 updateLocal(bv, true);
689 case LFUN_INSERT_MATH:
690 case LFUN_SELFINSERT:
692 bv->lockedInsetStoreUndo(Undo::EDIT);
694 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
696 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
697 updateLocal(bv, true);
701 case LFUN_MATH_PANEL:
702 result = UNDISPATCHED;
706 if (mathcursor->selection())
707 mathcursor->selClear();
709 result = UNDISPATCHED;
713 result = UNDISPATCHED;
716 mathcursor->normalize();
719 lyx::Assert(mathcursor);
721 if (mathcursor->selection() || was_selection)
722 toggleInsetSelection(bv);
724 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
725 result == UNDISPATCHED)
728 bv->unlockInset(this);
732 return result; // original version
736 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
741 // write something to the minibuffer
742 // translate to latex
743 mathcursor->markInsert();
746 string str = os.str();
747 mathcursor->markErase();
748 string::size_type pos = 0;
750 for (string::iterator it = str.begin(); it != str.end(); ++it) {
753 else if (*it == '\0') {
755 pos = it - str.begin();
761 res = res.substr(pos - 30);
763 res = res.substr(0, 60);
764 bv->owner()->message(res);
769 Inset::Code InsetFormulaBase::lyxCode() const
771 return Inset::MATH_CODE;
775 int InsetFormulaBase::ylow() const
777 return yo_ - ascent(view_, font_);
781 int InsetFormulaBase::yhigh() const
783 return yo_ + descent(view_, font_);
787 int InsetFormulaBase::xlow() const
793 int InsetFormulaBase::xhigh() const
795 return xo_ + width(view_, font_);
799 /////////////////////////////////////////////////////////////////////
802 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
808 static InsetFormulaBase * lastformula = 0;
809 static MathIterator current = MathIterator(ibegin(par().nucleus()));
811 static string laststr;
813 if (lastformula != this || laststr != str) {
814 //lyxerr << "reset lastformula to " << this << "\n";
817 current = ibegin(par().nucleus());
819 mathed_parse_cell(ar, str);
823 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
825 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
826 if (it.cell().matchpart(ar, it.position().pos_)) {
827 mathcursor->setSelection(it.cursor(), ar.size());
830 // I guess some of the following can go
831 bv->toggleSelection(true);
833 updateLocal(bv, true);
840 //lyxerr << "not found!\n";
842 // we have to unlock ourself in this function by default!
843 // don't ask me why...
844 bv->unlockInset(this);
849 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
852 lyxerr << "searching backward not implemented in mathed" << endl;
853 return searchForward(bv, what, a, b);
857 /////////////////////////////////////////////////////////////////////
860 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
862 if (bv->available()) {
863 // use selection if available..
865 //if (action == LFUN_MATH_IMPORT_SELECTION)
869 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
871 InsetFormulaBase * f;
873 f = new InsetFormula;
874 if (openNewInset(bv, f)) {
875 // don't do that also for LFUN_MATH_MODE unless you want end up with
876 // always changing to mathrm when opening an inlined inset
877 // -- I really hate "LyXfunc overloading"...
879 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
880 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
883 // create a macro if we see "\\newcommand" somewhere, and an ordinary
885 if (sel.find("\\newcommand") == string::npos &&
886 sel.find("\\def") == string::npos)
887 f = new InsetFormula(sel);
890 if (!mathed_parse_macro(name, sel))
892 f = new InsetFormulaMacro(sel);
894 bv->getLyXText()->cutSelection(bv);
898 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
902 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
904 mathDispatchCreation(bv, arg, true);
908 void mathDispatchMathMode(BufferView * bv, string const & arg)
910 mathDispatchCreation(bv, arg, false);
914 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
916 mathDispatchCreation(bv, arg, true);
920 void mathDispatchMathMacro(BufferView * bv, string const & arg)
922 if (bv->available()) {
924 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
927 string const s1 = token(s, ' ', 1);
928 int const na = s1.empty() ? 0 : lyx::atoi(s1);
929 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
935 void mathDispatchMathDelim(BufferView * bv, string const & arg)
937 if (bv->available()) {
938 if (openNewInset(bv, new InsetFormula))
939 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
944 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
946 if (bv->available()) {
947 if (openNewInset(bv, new InsetFormula))
948 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
953 void mathDispatchInsertMath(BufferView * bv, string const & arg)
955 if (bv->available()) {
956 if (arg.size() && arg[0] == '\\') {
957 InsetFormula * f = new InsetFormula(arg);
958 if (!bv->insertInset(f))
961 mathDispatchMathMode(bv, arg);
966 void mathDispatchGreek(BufferView * bv, string const & arg)
968 if (bv->available()) {
969 InsetFormula * f = new InsetFormula;
970 if (openNewInset(bv, f)) {
971 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
978 void mathDispatch(BufferView *, kb_action, string const &)