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_cursor.h"
42 #include "math_factory.h"
43 #include "math_hullinset.h"
44 #include "math_iterator.h"
45 #include "math_macrotable.h"
46 #include "math_parser.h"
48 #include "math_spaceinset.h"
49 #include "undo_funcs.h"
50 #include "textpainter.h"
51 #include "frontends/Dialogs.h"
58 MathCursor * mathcursor = 0;
72 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
74 if (mathcursor->selection())
75 bv->lockedInsetStoreUndo(Undo::EDIT);
76 mathcursor->handleFont(t);
77 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
78 mathcursor->insert(*it);
82 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
84 if (!bv->insertInset(new_inset)) {
88 new_inset->edit(bv, 0, 0, 0);
97 InsetFormulaBase::InsetFormulaBase()
98 : view_(0), font_(), xo_(0), yo_(0)
100 // This is needed as long the math parser is not re-entrant
101 MathMacroTable::builtinMacros();
102 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\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 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
162 mathcursor = new MathCursor(this, x == 0);
164 // handle ignored click
165 if (hack_x || hack_y) {
166 insetButtonPress(bv, hack_x, hack_y, hack_button);
171 // if that is removed, we won't get the magenta box when entering an
172 // inset for the first time
173 bv->updateInset(this, false);
177 void InsetFormulaBase::edit(BufferView * bv, bool front)
179 // looks hackish but seems to work
180 edit(bv, front ? 0 : 1, 0, 0);
184 void InsetFormulaBase::insetUnlock(BufferView * bv)
187 if (mathcursor->inMacroMode()) {
188 mathcursor->macroModeClose();
189 updateLocal(bv, true);
194 bv->updateInset(this, false);
198 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
200 mathcursor->getPos(x, y);
203 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
207 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
212 if (isCursorVisible())
213 bv->hideLockedInsetCursor();
218 mathcursor->getPos(x, y);
222 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
223 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
224 bv->showLockedInsetCursor(x, y, asc, des);
225 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
228 toggleCursorVisible();
232 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
234 if (isCursorVisible())
240 mathcursor->getPos(x, y);
244 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
245 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
246 bv->fitLockedInsetCursor(x, y, asc, des);
247 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
249 toggleInsetCursor(bv);
253 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
255 if (isCursorVisible())
256 toggleInsetCursor(bv);
260 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
263 bv->updateInset(this, false);
267 vector<string> const InsetFormulaBase::getLabelList() const
269 return vector<string>();
273 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
276 bv->updateInset(this, dirty);
280 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
281 int /*x*/, int /*y*/, int /*button*/)
285 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
288 bv->updateInset(this, false);
293 void InsetFormulaBase::insetButtonPress(BufferView * bv,
294 int x, int y, int button)
296 // hack to cope with mouseclick that comes before the call to edit()
300 hack_button = button;
304 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
305 << " but: " << button << "\n";
313 mathcursor->selClear();
314 mathcursor->setPos(x + xo_, y + yo_);
319 lyxerr << "insetButtonPress: 2\n";
322 bv->lockedInsetStoreUndo(Undo::EDIT);
324 mathcursor->selGet(ar);
325 mathcursor->setPos(x + xo_, y + yo_);
327 bv->getLyXText()->selectionAsString(bv->buffer(), false);
328 mathed_parse_cell(ar, sel);
329 mathcursor->insert(ar);
334 // launch math panel for right mouse button
335 bv->owner()->getDialogs()->showMathPanel();
338 bv->updateInset(this, false);
342 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
343 int x, int y, int /*button*/)
348 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
349 //lyxerr << "insetMotionNotify: ignored\n";
355 if (!mathcursor->selection())
356 mathcursor->selStart();
358 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
359 // << ' ' << button << "\n";
361 mathcursor->setPos(x + xo_, y + yo_);
363 bv->updateInset(this, false);
367 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
369 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
373 UpdatableInset::RESULT
374 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
377 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
378 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
383 if (mathcursor->asHyperActiveInset()) {
384 lyxerr << " uurr.... getting dificult now\n";
385 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
388 RESULT result = DISPATCHED;
390 bool was_macro = mathcursor->inMacroMode();
391 bool was_selection = mathcursor->selection();
395 mathcursor->normalize();
400 // --- Cursor Movements ---------------------------------------------
403 sel = true; // fall through...
406 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
407 //lyxerr << "calling scroll 20\n";
409 updateLocal(bv, false);
410 // write something to the minibuffer
411 //bv->owner()->message(mathcursor->info());
416 sel = true; // fall through
419 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
420 updateLocal(bv, false);
428 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
429 updateLocal(bv, false);
437 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
438 updateLocal(bv, false);
445 mathcursor->home(sel);
446 updateLocal(bv, false);
453 mathcursor->end(sel);
454 updateLocal(bv, false);
457 case LFUN_DELETE_LINE_FORWARD:
458 bv->lockedInsetStoreUndo(Undo::DELETE);
459 mathcursor->delLine();
460 updateLocal(bv, true);
467 TextPainter tpain(par()->width(), par()->height());
468 par()->draw(tpain, 0, par()->ascent());
470 mathcursor->idxNext();
471 updateLocal(bv, false);
475 mathcursor->idxPrev();
476 updateLocal(bv, false);
480 bv->lockedInsetStoreUndo(Undo::EDIT);
481 mathcursor->splitCell();
482 updateLocal(bv, true);
485 case LFUN_DELETE_WORD_BACKWARD:
487 bv->lockedInsetStoreUndo(Undo::DELETE);
488 mathcursor->backspace();
489 bv->updateInset(this, true);
492 case LFUN_DELETE_WORD_FORWARD:
494 bv->lockedInsetStoreUndo(Undo::DELETE);
496 bv->updateInset(this, true);
500 // sprintf(dispatch_buffer, "%d %d",);
501 // dispatch_result = dispatch_buffer;
504 lyxerr << "LFUN_SETXY broken!\n";
507 istringstream is(arg.c_str());
509 mathcursor->setPos(x, y);
510 updateLocal(bv, false);
516 mathcursor->macroModeClose();
517 bv->lockedInsetStoreUndo(Undo::EDIT);
518 mathcursor->selPaste();
519 updateLocal(bv, true);
523 bv->lockedInsetStoreUndo(Undo::DELETE);
524 mathcursor->selCut();
525 updateLocal(bv, true);
529 mathcursor->selCopy();
532 case LFUN_WORDRIGHTSEL:
533 case LFUN_WORDLEFTSEL:
536 // Special casing for superscript in case of LyX handling
538 case LFUN_CIRCUMFLEX:
540 // do superscript if LyX handles
542 bv->lockedInsetStoreUndo(Undo::EDIT);
543 mathcursor->script(true);
544 updateLocal(bv, true);
560 case LFUN_HUNG_UMLAUT:
564 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
565 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
566 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
567 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
568 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
569 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
570 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
571 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
572 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
573 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
574 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
577 handleFont(bv, arg, LM_TC_GREEK1);
579 mathcursor->interpret(arg);
584 handleFont(bv, arg, LM_TC_TEXTRM);
588 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
589 updateLocal(bv, true);
592 //bv->owner()->message(_("math text mode toggled"));
595 case LFUN_MATH_LIMITS:
596 bv->lockedInsetStoreUndo(Undo::EDIT);
597 if (mathcursor->toggleLimits())
598 updateLocal(bv, true);
604 bv->lockedInsetStoreUndo(Undo::EDIT);
605 mathcursor->setSize(arg);
606 updateLocal(bv, true);
611 case LFUN_INSERT_MATRIX:
613 bv->lockedInsetStoreUndo(Undo::EDIT);
614 mathcursor->interpret("matrix " + arg);
615 updateLocal(bv, true);
619 case LFUN_MATH_SPACE:
621 bv->lockedInsetStoreUndo(Undo::EDIT);
622 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
623 updateLocal(bv, true);
627 case LFUN_SUPERSCRIPT:
630 bv->lockedInsetStoreUndo(Undo::EDIT);
631 mathcursor->script((action == LFUN_SUPERSCRIPT));
632 updateLocal(bv, true);
636 case LFUN_MATH_DELIM:
638 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
641 istringstream is(arg.c_str());
644 lyxerr << "can't parse delimeters from '" << arg << "'\n";
647 bv->lockedInsetStoreUndo(Undo::EDIT);
648 mathcursor->handleDelim(ls, rs);
649 updateLocal(bv, true);
653 case LFUN_PROTECTEDSPACE:
654 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
655 bv->lockedInsetStoreUndo(Undo::EDIT);
656 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
657 updateLocal(bv, true);
661 bv->owner()->message(_("Invalid action in math mode!"));
665 case LFUN_MATH_HALIGN:
666 case LFUN_MATH_VALIGN:
667 case LFUN_MATH_ROW_INSERT:
668 case LFUN_MATH_ROW_DELETE:
669 case LFUN_MATH_COLUMN_INSERT:
670 case LFUN_MATH_COLUMN_DELETE:
672 MathInset::idx_type idx = 0;
673 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
675 bv->lockedInsetStoreUndo(Undo::EDIT);
676 char al = arg.size() ? arg[0] : 'c';
678 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
679 case LFUN_MATH_VALIGN: p->valign(al); break;
680 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
681 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
682 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
683 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
686 updateLocal(bv, true);
691 case LFUN_EXEC_COMMAND:
692 result = UNDISPATCHED;
695 case LFUN_BREAKPARAGRAPH:
696 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
697 //lyxerr << "LFUN ignored\n";
701 // interpret this as if a backslash was typed
702 bv->lockedInsetStoreUndo(Undo::EDIT);
703 mathcursor->interpret("\\");
704 updateLocal(bv, true);
708 case LFUN_INSERT_MATH:
709 case LFUN_SELFINSERT:
711 bv->lockedInsetStoreUndo(Undo::EDIT);
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 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))
976 mathDispatchMathMode(bv, arg);
981 void mathDispatchGreek(BufferView * bv, string const & arg)
983 if (bv->available()) {
984 InsetFormula * f = new InsetFormula;
985 if (openNewInset(bv, f)) {
986 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
993 void mathDispatch(BufferView *, kb_action, string const &)