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;
73 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
75 if (mathcursor->selection())
76 bv->lockedInsetStoreUndo(Undo::EDIT);
77 mathcursor->handleFont(t);
78 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
79 mathcursor->insert(*it);
83 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
85 if (!bv->insertInset(new_inset)) {
89 new_inset->edit(bv, 0, 0, 0);
98 InsetFormulaBase::InsetFormulaBase()
99 : view_(0), font_(), xo_(0), yo_(0)
101 // This is needed as long the math parser is not re-entrant
102 MathMacroTable::builtinMacros();
103 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
104 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
105 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
109 // simply scrap this function if you want
110 void InsetFormulaBase::mutateToText()
113 // translate to latex
115 latex(NULL, os, false, false);
116 string str = os.str();
119 LyXText * lt = view_->getLyXText();
120 string::const_iterator cit = str.begin();
121 string::const_iterator end = str.end();
122 for (; cit != end; ++cit)
123 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
126 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
131 // Check if uses AMS macros
132 void InsetFormulaBase::validate(LaTeXFeatures &) const
136 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
143 void InsetFormulaBase::metrics(BufferView * bv) const
147 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
152 string const InsetFormulaBase::editMessage() const
154 return _("Math editor mode");
158 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
160 if (!bv->lockInset(this))
161 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
163 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
165 mathcursor = new MathCursor(this, x == 0);
167 // handle ignored click
168 if (hack_x || hack_y) {
169 insetButtonPress(bv, hack_x, hack_y, hack_button);
174 // if that is removed, we won't get the magenta box when entering an
175 // inset for the first time
176 bv->updateInset(this, false);
180 void InsetFormulaBase::edit(BufferView * bv, bool front)
182 // looks hackish but seems to work
183 edit(bv, front ? 0 : 1, 0, 0);
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*/, int /*button*/)
286 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
289 bv->updateInset(this, false);
294 void InsetFormulaBase::insetButtonPress(BufferView * bv,
295 int x, int y, int button)
297 // hack to cope with mouseclick that comes before the call to edit()
301 hack_button = button;
305 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
306 << " but: " << button << "\n";
314 mathcursor->selClear();
315 mathcursor->setPos(x + xo_, y + yo_);
320 lyxerr << "insetButtonPress: 2\n";
323 bv->lockedInsetStoreUndo(Undo::EDIT);
325 mathcursor->selGet(ar);
326 mathcursor->setPos(x + xo_, y + yo_);
328 bv->getLyXText()->selectionAsString(bv->buffer(), false);
329 mathed_parse_cell(ar, sel);
330 mathcursor->insert(ar);
335 // launch math panel for right mouse button
336 bv->owner()->getDialogs()->showMathPanel();
339 bv->updateInset(this, false);
343 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
344 int x, int y, int /*button*/)
349 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
350 //lyxerr << "insetMotionNotify: ignored\n";
356 if (!mathcursor->selection())
357 mathcursor->selStart();
359 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
360 // << ' ' << button << "\n";
362 mathcursor->setPos(x + xo_, y + yo_);
364 bv->updateInset(this, false);
368 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
370 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
374 UpdatableInset::RESULT
375 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
378 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
379 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
384 if (mathcursor->asHyperActiveInset()) {
385 lyxerr << " uurr.... getting dificult now\n";
386 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
389 RESULT result = DISPATCHED;
391 bool was_macro = mathcursor->inMacroMode();
392 bool was_selection = mathcursor->selection();
396 mathcursor->normalize();
401 // --- Cursor Movements ---------------------------------------------
404 sel = true; // fall through...
407 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
408 //lyxerr << "calling scroll 20\n";
410 updateLocal(bv, false);
411 // write something to the minibuffer
412 //bv->owner()->message(mathcursor->info());
417 sel = true; // fall through
420 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
421 updateLocal(bv, false);
429 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
430 updateLocal(bv, false);
438 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
439 updateLocal(bv, false);
446 mathcursor->home(sel);
447 updateLocal(bv, false);
454 mathcursor->end(sel);
455 updateLocal(bv, false);
458 case LFUN_DELETE_LINE_FORWARD:
459 bv->lockedInsetStoreUndo(Undo::DELETE);
460 mathcursor->delLine();
461 updateLocal(bv, true);
465 mathcursor->idxNext();
466 updateLocal(bv, false);
470 mathcursor->idxPrev();
471 updateLocal(bv, false);
475 bv->lockedInsetStoreUndo(Undo::EDIT);
476 mathcursor->splitCell();
477 updateLocal(bv, true);
480 case LFUN_DELETE_WORD_BACKWARD:
482 bv->lockedInsetStoreUndo(Undo::DELETE);
483 mathcursor->backspace();
484 bv->updateInset(this, true);
487 case LFUN_DELETE_WORD_FORWARD:
489 bv->lockedInsetStoreUndo(Undo::DELETE);
491 bv->updateInset(this, true);
495 // sprintf(dispatch_buffer, "%d %d",);
496 // dispatch_result = dispatch_buffer;
499 lyxerr << "LFUN_SETXY broken!\n";
502 istringstream is(arg.c_str());
504 mathcursor->setPos(x, y);
505 updateLocal(bv, false);
511 mathcursor->macroModeClose();
512 bv->lockedInsetStoreUndo(Undo::EDIT);
513 mathcursor->selPaste();
514 updateLocal(bv, true);
518 bv->lockedInsetStoreUndo(Undo::DELETE);
519 mathcursor->selCut();
520 updateLocal(bv, true);
524 mathcursor->selCopy();
527 case LFUN_WORDRIGHTSEL:
528 case LFUN_WORDLEFTSEL:
531 // Special casing for superscript in case of LyX handling
533 case LFUN_CIRCUMFLEX:
535 // do superscript if LyX handles
537 bv->lockedInsetStoreUndo(Undo::EDIT);
538 mathcursor->script(true);
539 updateLocal(bv, true);
555 case LFUN_HUNG_UMLAUT:
559 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
560 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
561 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
562 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
563 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
564 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
565 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
566 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
567 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
568 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
569 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
572 handleFont(bv, arg, LM_TC_GREEK1);
574 mathcursor->interpret(arg);
579 handleFont(bv, arg, LM_TC_TEXTRM);
583 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
584 updateLocal(bv, true);
587 //bv->owner()->message(_("math text mode toggled"));
590 case LFUN_MATH_LIMITS:
591 bv->lockedInsetStoreUndo(Undo::EDIT);
592 if (mathcursor->toggleLimits())
593 updateLocal(bv, true);
599 bv->lockedInsetStoreUndo(Undo::EDIT);
600 mathcursor->setSize(arg);
601 updateLocal(bv, true);
606 case LFUN_INSERT_MATRIX:
608 bv->lockedInsetStoreUndo(Undo::EDIT);
609 mathcursor->interpret("matrix " + arg);
610 updateLocal(bv, true);
614 case LFUN_MATH_SPACE:
616 bv->lockedInsetStoreUndo(Undo::EDIT);
617 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
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";
636 istringstream is(arg.c_str());
639 lyxerr << "can't parse delimeters from '" << arg << "'\n";
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->handleDelim(ls, rs);
644 updateLocal(bv, true);
648 case LFUN_PROTECTEDSPACE:
649 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
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 bv->lockedInsetStoreUndo(Undo::EDIT);
671 char al = arg.size() ? arg[0] : 'c';
673 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
674 case LFUN_MATH_VALIGN: p->valign(al); break;
675 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
676 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
677 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
678 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
681 updateLocal(bv, true);
686 case LFUN_EXEC_COMMAND:
687 result = UNDISPATCHED;
690 case LFUN_BREAKPARAGRAPH:
691 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
692 //lyxerr << "LFUN ignored\n";
696 // interpret this as if a backslash was typed
697 bv->lockedInsetStoreUndo(Undo::EDIT);
698 mathcursor->interpret("\\");
699 updateLocal(bv, true);
703 case LFUN_INSERT_MATH:
704 case LFUN_SELFINSERT:
706 bv->lockedInsetStoreUndo(Undo::EDIT);
707 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
708 updateLocal(bv, true);
712 case LFUN_MATH_PANEL:
713 result = UNDISPATCHED;
717 if (mathcursor->selection())
718 mathcursor->selClear();
720 result = UNDISPATCHED;
724 result = UNDISPATCHED;
727 mathcursor->normalize();
730 lyx::Assert(mathcursor);
732 if (mathcursor->selection() || was_selection)
733 toggleInsetSelection(bv);
735 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
736 result == UNDISPATCHED)
739 bv->unlockInset(this);
743 return result; // original version
747 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
752 // write something to the minibuffer
753 // translate to latex
754 mathcursor->markInsert();
757 string str = os.str();
758 mathcursor->markErase();
759 string::size_type pos = 0;
761 for (string::iterator it = str.begin(); it != str.end(); ++it) {
764 else if (*it == '\0') {
766 pos = it - str.begin();
772 res = res.substr(pos - 30);
774 res = res.substr(0, 60);
775 bv->owner()->message(res);
780 Inset::Code InsetFormulaBase::lyxCode() const
782 return Inset::MATH_CODE;
786 int InsetFormulaBase::ylow() const
788 return yo_ - ascent(view_, font_);
792 int InsetFormulaBase::yhigh() const
794 return yo_ + descent(view_, font_);
798 int InsetFormulaBase::xlow() const
804 int InsetFormulaBase::xhigh() const
806 return xo_ + width(view_, font_);
810 /////////////////////////////////////////////////////////////////////
813 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
819 static InsetFormulaBase * lastformula = 0;
820 static MathIterator current = MathIterator(ibegin(par().nucleus()));
822 static string laststr;
824 if (lastformula != this || laststr != str) {
825 //lyxerr << "reset lastformula to " << this << "\n";
828 current = ibegin(par().nucleus());
830 mathed_parse_cell(ar, str);
834 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
836 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
837 if (it.cell().matchpart(ar, it.position().pos_)) {
838 mathcursor->setSelection(it.cursor(), ar.size());
841 // I guess some of the following can go
842 bv->toggleSelection(true);
844 updateLocal(bv, true);
851 //lyxerr << "not found!\n";
853 // we have to unlock ourself in this function by default!
854 // don't ask me why...
855 bv->unlockInset(this);
860 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
863 lyxerr << "searching backward not implemented in mathed" << endl;
864 return searchForward(bv, what, a, b);
868 /////////////////////////////////////////////////////////////////////
871 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
873 if (bv->available()) {
874 // use selection if available..
876 //if (action == LFUN_MATH_IMPORT_SELECTION)
880 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
882 InsetFormulaBase * f;
884 f = new InsetFormula;
885 if (openNewInset(bv, f)) {
886 // don't do that also for LFUN_MATH_MODE unless you want end up with
887 // always changing to mathrm when opening an inlined inset
888 // -- I really hate "LyXfunc overloading"...
890 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
891 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
894 // create a macro if we see "\\newcommand" somewhere, and an ordinary
896 if (sel.find("\\newcommand") == string::npos)
897 f = new InsetFormula(sel);
900 if (!mathed_parse_macro(name, sel))
902 f = new InsetFormulaMacro(sel);
904 bv->getLyXText()->cutSelection(bv);
908 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
912 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
914 mathDispatchCreation(bv, arg, true);
918 void mathDispatchMathMode(BufferView * bv, string const & arg)
920 mathDispatchCreation(bv, arg, false);
924 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
926 mathDispatchCreation(bv, arg, true);
930 void mathDispatchMathMacro(BufferView * bv, string const & arg)
932 if (bv->available()) {
934 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
937 string const s1 = token(s, ' ', 1);
938 int const na = s1.empty() ? 0 : lyx::atoi(s1);
939 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
945 void mathDispatchMathDelim(BufferView * bv, string const & arg)
947 if (bv->available()) {
948 if (openNewInset(bv, new InsetFormula))
949 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
954 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
956 if (bv->available()) {
957 if (openNewInset(bv, new InsetFormula))
958 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
963 void mathDispatchInsertMath(BufferView * bv, string const & arg)
965 if (bv->available()) {
966 if (arg.size() && arg[0] == '\\') {
967 InsetFormula * f = new InsetFormula(arg);
968 if (!bv->insertInset(f))
971 mathDispatchMathMode(bv, arg);
976 void mathDispatchGreek(BufferView * bv, string const & arg)
978 if (bv->available()) {
979 InsetFormula * f = new InsetFormula;
980 if (openNewInset(bv, f)) {
981 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
988 void mathDispatch(BufferView *, kb_action, string const &)