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 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
72 if (!bv->insertInset(new_inset)) {
76 new_inset->edit(bv, 0, 0, 0);
85 InsetFormulaBase::InsetFormulaBase()
86 : view_(0), font_(), xo_(0), yo_(0)
88 // This is needed as long the math parser is not re-entrant
89 MathMacroTable::builtinMacros();
90 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
91 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
92 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
96 // simply scrap this function if you want
97 void InsetFormulaBase::mutateToText()
100 // translate to latex
102 latex(NULL, os, false, false);
103 string str = os.str();
106 LyXText * lt = view_->getLyXText();
107 string::const_iterator cit = str.begin();
108 string::const_iterator end = str.end();
109 for (; cit != end; ++cit)
110 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
113 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
118 // Check if uses AMS macros
119 void InsetFormulaBase::validate(LaTeXFeatures &) const
123 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
130 void InsetFormulaBase::metrics(BufferView * bv) const
134 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
139 string const InsetFormulaBase::editMessage() const
141 return _("Math editor mode");
145 void InsetFormulaBase::edit(BufferView * bv, int x, int y, unsigned int)
147 if (!bv->lockInset(this))
148 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
150 mathcursor = new MathCursor(this, true);
152 mathcursor->setPos(x, y);
153 //lyxerr << "setting pos to " << x << "," << y << "\n";
155 // if that is removed, we won't get the magenta box when entering an
156 // inset for the first time
157 bv->updateInset(this, false);
161 void InsetFormulaBase::edit(BufferView * bv, bool front)
163 if (!bv->lockInset(this))
164 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
166 mathcursor = new MathCursor(this, front);
168 bv->updateInset(this, false);
172 void InsetFormulaBase::handleFont
173 (BufferView * bv, string const & arg, MathTextCodes t)
175 if (mathcursor->selection()) {
176 bv->lockedInsetStoreUndo(Undo::EDIT);
177 updateLocal(bv, true);
179 mathcursor->handleFont(t);
180 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
181 mathcursor->insert(*it);
182 updateLocal(bv, false);
186 void InsetFormulaBase::insetUnlock(BufferView * bv)
189 if (mathcursor->inMacroMode()) {
190 mathcursor->macroModeClose();
191 updateLocal(bv, true);
196 bv->updateInset(this, false);
200 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
202 mathcursor->getPos(x, y);
205 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
209 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
214 if (isCursorVisible())
215 bv->hideLockedInsetCursor();
220 mathcursor->getPos(x, y);
224 math_font_max_dim(font_, asc, des);
225 bv->showLockedInsetCursor(x, y, asc, des);
226 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
229 toggleCursorVisible();
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
235 if (isCursorVisible())
241 mathcursor->getPos(x, y);
245 math_font_max_dim(font_, 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)
283 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
289 bv->updateInset(this, false);
292 // launch math panel for right mouse button
293 bv->owner()->getDialogs()->showMathPanel();
300 void InsetFormulaBase::insetButtonPress(BufferView * bv,
301 int x, int y, int button)
303 //lyxerr << "insetButtonPress: "
304 // << x << " " << y << " but: " << button << "\n";
311 mathcursor = new MathCursor(this, x == 0);
315 mathcursor->selClear();
316 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();
340 if (button == 1 || !mathcursor) {
342 mathcursor = new MathCursor(this, x == 0);
346 mathcursor->selClear();
347 mathcursor->setPos(x + xo_, y + yo_);
350 #warning Never launch a Dialog on "Press" event ONLY on "Release" event!
352 // launch math panel for right mouse button
353 bv->owner()->getDialogs()->showMathPanel();
357 bv->updateInset(this, false);
361 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
362 int x, int y, int /*button*/)
367 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
368 //lyxerr << "insetMotionNotify: ignored\n";
374 if (!mathcursor->selection())
375 mathcursor->selStart();
377 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
378 // << ' ' << button << "\n";
380 mathcursor->setPos(x + xo_, y + yo_);
382 bv->updateInset(this, false);
386 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
388 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
392 UpdatableInset::RESULT
393 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
396 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
397 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
402 RESULT result = DISPATCHED;
404 bool was_macro = mathcursor->inMacroMode();
405 bool was_selection = mathcursor->selection();
409 mathcursor->normalize();
414 // --- Cursor Movements ---------------------------------------------
417 sel = true; // fall through...
420 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
421 //lyxerr << "calling scroll 20\n";
423 updateLocal(bv, false);
424 // write something to the minibuffer
425 //bv->owner()->message(mathcursor->info());
430 sel = true; // fall through
433 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
434 updateLocal(bv, false);
442 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
443 updateLocal(bv, false);
451 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
452 updateLocal(bv, false);
459 mathcursor->home(sel);
460 updateLocal(bv, false);
467 mathcursor->end(sel);
468 updateLocal(bv, false);
471 case LFUN_DELETE_LINE_FORWARD:
472 bv->lockedInsetStoreUndo(Undo::DELETE);
473 mathcursor->delLine();
474 updateLocal(bv, true);
478 mathcursor->idxNext();
479 updateLocal(bv, false);
483 mathcursor->idxPrev();
484 updateLocal(bv, false);
488 bv->lockedInsetStoreUndo(Undo::EDIT);
489 mathcursor->splitCell();
490 updateLocal(bv, true);
493 case LFUN_DELETE_WORD_BACKWARD:
495 bv->lockedInsetStoreUndo(Undo::DELETE);
496 mathcursor->backspace();
497 bv->updateInset(this, true);
500 case LFUN_DELETE_WORD_FORWARD:
502 bv->lockedInsetStoreUndo(Undo::DELETE);
504 bv->updateInset(this, true);
508 // sprintf(dispatch_buffer, "%d %d",);
509 // dispatch_result = dispatch_buffer;
512 lyxerr << "LFUN_SETXY broken!\n";
515 istringstream is(arg.c_str());
517 mathcursor->setPos(x, y);
518 updateLocal(bv, false);
524 mathcursor->macroModeClose();
525 bv->lockedInsetStoreUndo(Undo::EDIT);
526 mathcursor->selPaste();
527 updateLocal(bv, true);
531 bv->lockedInsetStoreUndo(Undo::DELETE);
532 mathcursor->selCut();
533 updateLocal(bv, true);
537 mathcursor->selCopy();
540 case LFUN_WORDRIGHTSEL:
541 case LFUN_WORDLEFTSEL:
544 // Special casing for superscript in case of LyX handling
546 case LFUN_CIRCUMFLEX:
548 // do superscript if LyX handles
550 bv->lockedInsetStoreUndo(Undo::EDIT);
551 mathcursor->script(true);
552 updateLocal(bv, true);
568 case LFUN_HUNG_UMLAUT:
572 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
573 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
574 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
575 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
576 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
577 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
578 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
579 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
580 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
581 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
582 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
585 handleFont(bv, arg, LM_TC_GREEK1);
587 mathcursor->interpret(arg);
592 handleFont(bv, arg, LM_TC_TEXTRM);
596 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
597 updateLocal(bv, true);
600 //bv->owner()->message(_("math text mode toggled"));
603 case LFUN_MATH_LIMITS:
604 bv->lockedInsetStoreUndo(Undo::EDIT);
605 if (mathcursor->toggleLimits())
606 updateLocal(bv, true);
612 bv->lockedInsetStoreUndo(Undo::EDIT);
613 mathcursor->setSize(arg);
614 updateLocal(bv, true);
619 case LFUN_INSERT_MATRIX:
621 bv->lockedInsetStoreUndo(Undo::EDIT);
622 mathcursor->interpret("matrix " + arg);
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";
640 string rs = split(arg, ls, ' ');
641 // Reasonable default values
647 bv->lockedInsetStoreUndo(Undo::EDIT);
648 mathcursor->handleDelim(ls, rs);
649 updateLocal(bv, true);
653 case LFUN_PROTECTEDSPACE:
654 case LFUN_MATH_SPACE:
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 mathcursor->popToEnclosingGrid();
676 bv->lockedInsetStoreUndo(Undo::EDIT);
677 char align = arg.size() ? arg[0] : 'c';
679 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
680 case LFUN_MATH_VALIGN: p->valign(align); break;
681 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
682 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
683 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
684 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
687 updateLocal(bv, true);
692 case LFUN_EXEC_COMMAND:
693 result = UNDISPATCHED;
696 case LFUN_BREAKPARAGRAPH:
697 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
698 //lyxerr << "LFUN ignored\n";
702 // interpret this as if a backslash was typed
703 bv->lockedInsetStoreUndo(Undo::EDIT);
704 mathcursor->interpret('\\');
705 updateLocal(bv, true);
709 case LFUN_INSERT_MATH:
710 case LFUN_SELFINSERT:
712 bv->lockedInsetStoreUndo(Undo::EDIT);
714 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
716 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
717 updateLocal(bv, true);
721 case LFUN_MATH_PANEL:
722 result = UNDISPATCHED;
726 if (mathcursor->selection())
727 mathcursor->selClear();
729 result = UNDISPATCHED;
733 result = UNDISPATCHED;
736 mathcursor->normalize();
739 lyx::Assert(mathcursor);
741 if (mathcursor->selection() || was_selection)
742 toggleInsetSelection(bv);
744 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
745 result == UNDISPATCHED)
748 bv->unlockInset(this);
752 return result; // original version
756 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
761 // write something to the minibuffer
762 // translate to latex
763 mathcursor->markInsert();
766 string str = os.str();
767 mathcursor->markErase();
768 string::size_type pos = 0;
770 for (string::iterator it = str.begin(); it != str.end(); ++it) {
773 else if (*it == '\0') {
775 pos = it - str.begin();
781 res = res.substr(pos - 30);
783 res = res.substr(0, 60);
784 bv->owner()->message(res);
789 Inset::Code InsetFormulaBase::lyxCode() const
791 return Inset::MATH_CODE;
795 int InsetFormulaBase::ylow() const
797 return yo_ - ascent(view_, font_);
801 int InsetFormulaBase::yhigh() const
803 return yo_ + descent(view_, font_);
807 int InsetFormulaBase::xlow() const
813 int InsetFormulaBase::xhigh() const
815 return xo_ + width(view_, font_);
819 /////////////////////////////////////////////////////////////////////
822 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
828 static InsetFormulaBase * lastformula = 0;
829 static MathIterator current = MathIterator(ibegin(par().nucleus()));
831 static string laststr;
833 if (lastformula != this || laststr != str) {
834 //lyxerr << "reset lastformula to " << this << "\n";
837 current = ibegin(par().nucleus());
839 mathed_parse_cell(ar, str);
843 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
845 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
846 if (it.cell().matchpart(ar, it.position().pos_)) {
847 mathcursor->setSelection(it.cursor(), ar.size());
850 // I guess some of the following can go
851 bv->toggleSelection(true);
853 updateLocal(bv, true);
860 //lyxerr << "not found!\n";
862 // we have to unlock ourself in this function by default!
863 // don't ask me why...
864 bv->unlockInset(this);
869 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
872 lyxerr << "searching backward not implemented in mathed" << endl;
873 return searchForward(bv, what, a, b);
877 /////////////////////////////////////////////////////////////////////
880 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
882 if (bv->available()) {
883 // use selection if available..
885 //if (action == LFUN_MATH_IMPORT_SELECTION)
889 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
891 InsetFormulaBase * f;
893 f = new InsetFormula;
894 if (openNewInset(bv, f)) {
895 // don't do that also for LFUN_MATH_MODE unless you want end up with
896 // always changing to mathrm when opening an inlined inset
897 // -- I really hate "LyXfunc overloading"...
899 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
900 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
903 // create a macro if we see "\\newcommand" somewhere, and an ordinary
905 if (sel.find("\\newcommand") == string::npos &&
906 sel.find("\\def") == string::npos)
907 f = new InsetFormula(sel);
910 if (!mathed_parse_macro(name, sel))
912 f = new InsetFormulaMacro(sel);
914 bv->getLyXText()->cutSelection(bv);
918 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
922 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
924 mathDispatchCreation(bv, arg, true);
928 void mathDispatchMathMode(BufferView * bv, string const & arg)
930 mathDispatchCreation(bv, arg, false);
934 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
936 mathDispatchCreation(bv, arg, true);
940 void mathDispatchMathMacro(BufferView * bv, string const & arg)
942 if (bv->available()) {
944 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
947 string const s1 = token(s, ' ', 1);
948 int const na = s1.empty() ? 0 : lyx::atoi(s1);
949 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
955 void mathDispatchMathDelim(BufferView * bv, string const & arg)
957 if (bv->available()) {
958 if (openNewInset(bv, new InsetFormula))
959 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
964 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
966 if (bv->available()) {
967 if (openNewInset(bv, new InsetFormula))
968 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
973 void mathDispatchInsertMath(BufferView * bv, string const & arg)
975 if (bv->available()) {
976 if (arg.size() && arg[0] == '\\') {
977 InsetFormula * f = new InsetFormula(arg);
978 if (!bv->insertInset(f))
980 else if (!mathcursor) // hotfix
981 bv->getLyXText()->cursorRight(bv);
983 mathDispatchMathMode(bv, arg);
989 void mathDispatchGreek(BufferView * bv, string const & arg)
991 if (bv->available()) {
992 InsetFormula * f = new InsetFormula;
993 if (openNewInset(bv, f)) {
994 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1001 void mathDispatch(BufferView *, kb_action, string const &)