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 "frontends/Dialogs.h"
57 MathCursor * mathcursor = 0;
71 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
73 if (mathcursor->selection())
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->handleFont(t);
76 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
77 mathcursor->insert(*it);
81 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
83 if (!bv->insertInset(new_inset)) {
87 new_inset->edit(bv, 0, 0, 0);
96 InsetFormulaBase::InsetFormulaBase()
97 : view_(0), font_(), xo_(0), yo_(0)
99 // This is needed as long the math parser is not re-entrant
100 MathMacroTable::builtinMacros();
101 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
105 // simply scrap this function if you want
106 void InsetFormulaBase::mutateToText()
109 // translate to latex
111 latex(NULL, os, false, false);
112 string str = os.str();
115 LyXText * lt = view_->getLyXText();
116 string::const_iterator cit = str.begin();
117 string::const_iterator end = str.end();
118 for (; cit != end; ++cit)
119 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
122 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
127 // Check if uses AMS macros
128 void InsetFormulaBase::validate(LaTeXFeatures &) const
132 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
139 void InsetFormulaBase::metrics(BufferView * bv) const
143 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
156 if (!bv->lockInset(this))
157 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
159 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
161 mathcursor = new MathCursor(this, x == 0);
163 // handle ignored click
164 if (hack_x || hack_y) {
165 insetButtonPress(bv, hack_x, hack_y, hack_button);
170 // if that is removed, we won't get the magenta box when entering an
171 // inset for the first time
172 bv->updateInset(this, false);
176 void InsetFormulaBase::edit(BufferView * bv, bool front)
178 // looks hackish but seems to work
179 edit(bv, front ? 0 : 1, 0, 0);
183 void InsetFormulaBase::insetUnlock(BufferView * bv)
186 if (mathcursor->inMacroMode()) {
187 mathcursor->macroModeClose();
188 updateLocal(bv, true);
193 bv->updateInset(this, false);
197 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
199 mathcursor->getPos(x, y);
202 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
206 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
211 if (isCursorVisible())
212 bv->hideLockedInsetCursor();
217 mathcursor->getPos(x, y);
221 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
222 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
223 bv->showLockedInsetCursor(x, y, asc, des);
224 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
227 toggleCursorVisible();
231 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
233 if (isCursorVisible())
239 mathcursor->getPos(x, y);
243 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
244 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
245 bv->fitLockedInsetCursor(x, y, asc, des);
246 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
248 toggleInsetCursor(bv);
252 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
254 if (isCursorVisible())
255 toggleInsetCursor(bv);
259 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
262 bv->updateInset(this, false);
266 vector<string> const InsetFormulaBase::getLabelList() const
268 return vector<string>();
272 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
275 bv->updateInset(this, dirty);
279 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
280 int /*x*/, int /*y*/, int /*button*/)
284 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
287 bv->updateInset(this, false);
292 void InsetFormulaBase::insetButtonPress(BufferView * bv,
293 int x, int y, int button)
295 // hack to cope with mouseclick that comes before the call to edit()
299 hack_button = button;
303 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
304 << " but: " << button << "\n";
312 mathcursor->selClear();
313 mathcursor->setPos(x + xo_, y + yo_);
318 lyxerr << "insetButtonPress: 2\n";
321 bv->lockedInsetStoreUndo(Undo::EDIT);
323 mathcursor->selGet(ar);
324 mathcursor->setPos(x + xo_, y + yo_);
326 bv->getLyXText()->selectionAsString(bv->buffer(), false);
327 mathed_parse_cell(ar, sel);
328 mathcursor->insert(ar);
333 // launch math panel for right mouse button
334 bv->owner()->getDialogs()->showMathPanel();
337 bv->updateInset(this, false);
341 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
342 int x, int y, int /*button*/)
347 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
348 //lyxerr << "insetMotionNotify: ignored\n";
354 if (!mathcursor->selection())
355 mathcursor->selStart();
357 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
358 // << ' ' << button << "\n";
360 mathcursor->setPos(x + xo_, y + yo_);
362 bv->updateInset(this, false);
366 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
368 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
372 UpdatableInset::RESULT
373 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
376 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
377 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
382 if (mathcursor->asHyperActiveInset()) {
383 lyxerr << " uurr.... getting dificult now\n";
384 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
387 RESULT result = DISPATCHED;
389 bool was_macro = mathcursor->inMacroMode();
390 bool was_selection = mathcursor->selection();
394 mathcursor->normalize();
397 // --- Cursor Movements ---------------------------------------------
400 sel = true; // fall through...
403 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
404 //lyxerr << "calling scroll 20\n";
406 updateLocal(bv, false);
407 // write something to the minibuffer
408 //bv->owner()->message(mathcursor->info());
413 sel = true; // fall through
416 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
417 updateLocal(bv, false);
425 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
426 updateLocal(bv, false);
434 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
435 updateLocal(bv, false);
442 mathcursor->home(sel);
443 updateLocal(bv, false);
450 mathcursor->end(sel);
451 updateLocal(bv, false);
454 case LFUN_DELETE_LINE_FORWARD:
455 bv->lockedInsetStoreUndo(Undo::DELETE);
456 mathcursor->delLine();
457 updateLocal(bv, true);
461 mathcursor->idxNext();
462 updateLocal(bv, false);
466 mathcursor->idxPrev();
467 updateLocal(bv, false);
471 bv->lockedInsetStoreUndo(Undo::EDIT);
472 mathcursor->splitCell();
473 updateLocal(bv, true);
476 case LFUN_DELETE_WORD_BACKWARD:
478 bv->lockedInsetStoreUndo(Undo::DELETE);
479 mathcursor->backspace();
480 bv->updateInset(this, true);
483 case LFUN_DELETE_WORD_FORWARD:
485 bv->lockedInsetStoreUndo(Undo::DELETE);
487 bv->updateInset(this, true);
491 // sprintf(dispatch_buffer, "%d %d",);
492 // dispatch_result = dispatch_buffer;
495 lyxerr << "LFUN_SETXY broken!\n";
498 istringstream is(arg.c_str());
500 mathcursor->setPos(x, y);
501 updateLocal(bv, false);
507 mathcursor->macroModeClose();
508 bv->lockedInsetStoreUndo(Undo::EDIT);
509 mathcursor->selPaste();
510 updateLocal(bv, true);
514 bv->lockedInsetStoreUndo(Undo::DELETE);
515 mathcursor->selCut();
516 updateLocal(bv, true);
520 mathcursor->selCopy();
523 case LFUN_WORDRIGHTSEL:
524 case LFUN_WORDLEFTSEL:
527 // Special casing for superscript in case of LyX handling
529 case LFUN_CIRCUMFLEX:
531 // do superscript if LyX handles
533 bv->lockedInsetStoreUndo(Undo::EDIT);
534 mathcursor->script(true);
535 updateLocal(bv, true);
551 case LFUN_HUNG_UMLAUT:
555 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
556 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
557 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
558 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
559 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
560 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
561 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
562 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
563 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
564 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
565 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
568 handleFont(bv, arg, LM_TC_GREEK1);
570 mathcursor->interpret(arg);
575 handleFont(bv, arg, LM_TC_TEXTRM);
579 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
580 updateLocal(bv, true);
583 //bv->owner()->message(_("math text mode toggled"));
586 case LFUN_MATH_LIMITS:
587 bv->lockedInsetStoreUndo(Undo::EDIT);
588 if (mathcursor->toggleLimits())
589 updateLocal(bv, true);
595 bv->lockedInsetStoreUndo(Undo::EDIT);
596 mathcursor->setSize(arg);
597 updateLocal(bv, true);
602 case LFUN_INSERT_MATRIX:
604 bv->lockedInsetStoreUndo(Undo::EDIT);
605 mathcursor->interpret("matrix " + arg);
606 updateLocal(bv, true);
610 case LFUN_MATH_SPACE:
612 bv->lockedInsetStoreUndo(Undo::EDIT);
613 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
614 updateLocal(bv, true);
618 case LFUN_SUPERSCRIPT:
621 bv->lockedInsetStoreUndo(Undo::EDIT);
622 mathcursor->script((action == LFUN_SUPERSCRIPT));
623 updateLocal(bv, true);
627 case LFUN_MATH_DELIM:
629 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
632 istringstream is(arg.c_str());
635 lyxerr << "can't parse delimeters from '" << arg << "'\n";
638 bv->lockedInsetStoreUndo(Undo::EDIT);
639 mathcursor->handleDelim(ls, rs);
640 updateLocal(bv, true);
644 case LFUN_PROTECTEDSPACE:
645 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
646 bv->lockedInsetStoreUndo(Undo::EDIT);
647 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
648 updateLocal(bv, true);
652 bv->owner()->message(_("Invalid action in math mode!"));
656 case LFUN_MATH_HALIGN:
657 case LFUN_MATH_VALIGN:
658 case LFUN_MATH_ROW_INSERT:
659 case LFUN_MATH_ROW_DELETE:
660 case LFUN_MATH_COLUMN_INSERT:
661 case LFUN_MATH_COLUMN_DELETE:
663 MathInset::idx_type idx = 0;
664 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
666 bv->lockedInsetStoreUndo(Undo::EDIT);
667 char al = arg.size() ? arg[0] : 'c';
669 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
670 case LFUN_MATH_VALIGN: p->valign(al); break;
671 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
672 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
673 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
674 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
677 updateLocal(bv, true);
682 case LFUN_EXEC_COMMAND:
683 result = UNDISPATCHED;
686 case LFUN_BREAKPARAGRAPH:
687 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
688 //lyxerr << "LFUN ignored\n";
692 // interpret this as if a backslash was typed
693 bv->lockedInsetStoreUndo(Undo::EDIT);
694 mathcursor->interpret("\\");
695 updateLocal(bv, true);
699 case LFUN_INSERT_MATH:
700 case LFUN_SELFINSERT:
702 bv->lockedInsetStoreUndo(Undo::EDIT);
703 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
704 updateLocal(bv, true);
708 case LFUN_MATH_PANEL:
709 result = UNDISPATCHED;
713 if (mathcursor->selection())
714 mathcursor->selClear();
716 result = UNDISPATCHED;
720 result = UNDISPATCHED;
723 mathcursor->normalize();
725 lyx::Assert(mathcursor);
727 if (mathcursor->selection() || was_selection)
728 toggleInsetSelection(bv);
730 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
731 result == UNDISPATCHED)
734 bv->unlockInset(this);
738 return result; // original version
742 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
747 // write something to the minibuffer
748 // translate to latex
749 mathcursor->markInsert();
752 string str = os.str();
753 mathcursor->markErase();
754 string::size_type pos = 0;
756 for (string::iterator it = str.begin(); it != str.end(); ++it) {
759 else if (*it == '\0') {
761 pos = it - str.begin();
767 res = res.substr(pos - 30);
769 res = res.substr(0, 60);
770 bv->owner()->message(res);
775 Inset::Code InsetFormulaBase::lyxCode() const
777 return Inset::MATH_CODE;
781 int InsetFormulaBase::ylow() const
783 return yo_ - ascent(view_, font_);
787 int InsetFormulaBase::yhigh() const
789 return yo_ + descent(view_, font_);
793 int InsetFormulaBase::xlow() const
799 int InsetFormulaBase::xhigh() const
801 return xo_ + width(view_, font_);
805 /////////////////////////////////////////////////////////////////////
808 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
814 static InsetFormulaBase * lastformula = 0;
815 static MathIterator current = MathIterator(ibegin(par().nucleus()));
817 static string laststr;
819 if (lastformula != this || laststr != str) {
820 //lyxerr << "reset lastformula to " << this << "\n";
823 current = ibegin(par().nucleus());
825 mathed_parse_cell(ar, str);
829 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
831 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
832 if (it.cell().matchpart(ar, it.position().pos_)) {
833 mathcursor->setSelection(it.cursor(), ar.size());
836 // I guess some of the following can go
837 bv->toggleSelection(true);
839 updateLocal(bv, true);
846 //lyxerr << "not found!\n";
848 // we have to unlock ourself in this function by default!
849 // don't ask me why...
850 bv->unlockInset(this);
855 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
858 lyxerr << "searching backward not implemented in mathed" << endl;
859 return searchForward(bv, what, a, b);
863 /////////////////////////////////////////////////////////////////////
866 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
868 if (bv->available()) {
869 // use selection if available..
871 //if (action == LFUN_MATH_IMPORT_SELECTION)
875 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
877 InsetFormulaBase * f;
879 f = new InsetFormula;
880 if (openNewInset(bv, f)) {
881 // don't do that also for LFUN_MATH_MODE unless you want end up with
882 // always changing to mathrm when opening an inlined inset
883 // -- I really hate "LyXfunc overloading"...
885 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
886 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
889 // create a macro if we see "\\newcommand" somewhere, and an ordinary
891 if (sel.find("\\newcommand") == string::npos)
892 f = new InsetFormula(sel);
895 if (!mathed_parse_macro(name, sel))
897 f = new InsetFormulaMacro(sel);
899 bv->getLyXText()->cutSelection(bv);
903 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
907 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
909 mathDispatchCreation(bv, arg, true);
913 void mathDispatchMathMode(BufferView * bv, string const & arg)
915 mathDispatchCreation(bv, arg, false);
919 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
921 mathDispatchCreation(bv, arg, true);
925 void mathDispatchMathMacro(BufferView * bv, string const & arg)
927 if (bv->available()) {
929 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
932 string const s1 = token(s, ' ', 1);
933 int const na = s1.empty() ? 0 : lyx::atoi(s1);
934 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
940 void mathDispatchMathDelim(BufferView * bv, string const & arg)
942 if (bv->available()) {
943 if (openNewInset(bv, new InsetFormula))
944 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
949 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
951 if (bv->available()) {
952 if (openNewInset(bv, new InsetFormula))
953 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
958 void mathDispatchInsertMath(BufferView * bv, string const & arg)
960 if (bv->available()) {
961 if (arg.size() && arg[0] == '\\') {
962 InsetFormula * f = new InsetFormula(arg);
963 if (!bv->insertInset(f))
966 mathDispatchMathMode(bv, arg);
971 void mathDispatchGreek(BufferView * bv, string const & arg)
973 if (bv->available()) {
974 InsetFormula * f = new InsetFormula;
975 if (openNewInset(bv, f)) {
976 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
983 void mathDispatch(BufferView *, kb_action, string const &)