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 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);
708 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
709 updateLocal(bv, true);
713 case LFUN_MATH_PANEL:
714 result = UNDISPATCHED;
718 if (mathcursor->selection())
719 mathcursor->selClear();
721 result = UNDISPATCHED;
725 result = UNDISPATCHED;
728 mathcursor->normalize();
731 lyx::Assert(mathcursor);
733 if (mathcursor->selection() || was_selection)
734 toggleInsetSelection(bv);
736 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
737 result == UNDISPATCHED)
740 bv->unlockInset(this);
744 return result; // original version
748 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
753 // write something to the minibuffer
754 // translate to latex
755 mathcursor->markInsert();
758 string str = os.str();
759 mathcursor->markErase();
760 string::size_type pos = 0;
762 for (string::iterator it = str.begin(); it != str.end(); ++it) {
765 else if (*it == '\0') {
767 pos = it - str.begin();
773 res = res.substr(pos - 30);
775 res = res.substr(0, 60);
776 bv->owner()->message(res);
781 Inset::Code InsetFormulaBase::lyxCode() const
783 return Inset::MATH_CODE;
787 int InsetFormulaBase::ylow() const
789 return yo_ - ascent(view_, font_);
793 int InsetFormulaBase::yhigh() const
795 return yo_ + descent(view_, font_);
799 int InsetFormulaBase::xlow() const
805 int InsetFormulaBase::xhigh() const
807 return xo_ + width(view_, font_);
811 /////////////////////////////////////////////////////////////////////
814 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
820 static InsetFormulaBase * lastformula = 0;
821 static MathIterator current = MathIterator(ibegin(par().nucleus()));
823 static string laststr;
825 if (lastformula != this || laststr != str) {
826 //lyxerr << "reset lastformula to " << this << "\n";
829 current = ibegin(par().nucleus());
831 mathed_parse_cell(ar, str);
835 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
837 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
838 if (it.cell().matchpart(ar, it.position().pos_)) {
839 mathcursor->setSelection(it.cursor(), ar.size());
842 // I guess some of the following can go
843 bv->toggleSelection(true);
845 updateLocal(bv, true);
852 //lyxerr << "not found!\n";
854 // we have to unlock ourself in this function by default!
855 // don't ask me why...
856 bv->unlockInset(this);
861 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
864 lyxerr << "searching backward not implemented in mathed" << endl;
865 return searchForward(bv, what, a, b);
869 /////////////////////////////////////////////////////////////////////
872 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
874 if (bv->available()) {
875 // use selection if available..
877 //if (action == LFUN_MATH_IMPORT_SELECTION)
881 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
883 InsetFormulaBase * f;
885 f = new InsetFormula;
886 if (openNewInset(bv, f)) {
887 // don't do that also for LFUN_MATH_MODE unless you want end up with
888 // always changing to mathrm when opening an inlined inset
889 // -- I really hate "LyXfunc overloading"...
891 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
892 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
895 // create a macro if we see "\\newcommand" somewhere, and an ordinary
897 if (sel.find("\\newcommand") == string::npos &&
898 sel.find("\\def") == string::npos)
899 f = new InsetFormula(sel);
902 if (!mathed_parse_macro(name, sel))
904 f = new InsetFormulaMacro(sel);
906 bv->getLyXText()->cutSelection(bv);
910 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
914 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
916 mathDispatchCreation(bv, arg, true);
920 void mathDispatchMathMode(BufferView * bv, string const & arg)
922 mathDispatchCreation(bv, arg, false);
926 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
928 mathDispatchCreation(bv, arg, true);
932 void mathDispatchMathMacro(BufferView * bv, string const & arg)
934 if (bv->available()) {
936 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
939 string const s1 = token(s, ' ', 1);
940 int const na = s1.empty() ? 0 : lyx::atoi(s1);
941 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
947 void mathDispatchMathDelim(BufferView * bv, string const & arg)
949 if (bv->available()) {
950 if (openNewInset(bv, new InsetFormula))
951 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
956 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
958 if (bv->available()) {
959 if (openNewInset(bv, new InsetFormula))
960 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
965 void mathDispatchInsertMath(BufferView * bv, string const & arg)
967 if (bv->available()) {
968 if (arg.size() && arg[0] == '\\') {
969 InsetFormula * f = new InsetFormula(arg);
970 if (!bv->insertInset(f))
973 mathDispatchMathMode(bv, arg);
978 void mathDispatchGreek(BufferView * bv, string const & arg)
980 if (bv->available()) {
981 InsetFormula * f = new InsetFormula;
982 if (openNewInset(bv, f)) {
983 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
990 void mathDispatch(BufferView *, kb_action, string const &)