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"
36 #include "frontends/LyXView.h"
37 #include "frontends/font_metrics.h"
38 #include "frontends/mouse_state.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, mouse_button::none);
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, mouse_button::state)
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*/, mouse_button::state button)
283 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
289 bv->updateInset(this, false);
291 if (button == mouse_button::button3) {
292 // launch math panel for right mouse button
293 bv->owner()->getDialogs()->showMathPanel();
300 void InsetFormulaBase::insetButtonPress(BufferView * bv,
301 int x, int y, mouse_button::state 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 == mouse_button::button1 || !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, mouse_button::state)
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 UpdatableInset::RESULT
387 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
390 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
391 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
396 RESULT result = DISPATCHED;
398 bool was_macro = mathcursor->inMacroMode();
399 bool was_selection = mathcursor->selection();
403 mathcursor->normalize();
408 // --- Cursor Movements ---------------------------------------------
411 sel = true; // fall through...
414 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
415 //lyxerr << "calling scroll 20\n";
417 updateLocal(bv, false);
418 // write something to the minibuffer
419 //bv->owner()->message(mathcursor->info());
424 sel = true; // fall through
427 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
428 updateLocal(bv, false);
436 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
437 updateLocal(bv, false);
445 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
446 updateLocal(bv, false);
453 mathcursor->home(sel);
454 updateLocal(bv, false);
461 mathcursor->end(sel);
462 updateLocal(bv, false);
465 case LFUN_DELETE_LINE_FORWARD:
466 bv->lockedInsetStoreUndo(Undo::DELETE);
467 mathcursor->delLine();
468 updateLocal(bv, true);
472 mathcursor->idxNext();
473 updateLocal(bv, false);
477 mathcursor->idxPrev();
478 updateLocal(bv, false);
482 bv->lockedInsetStoreUndo(Undo::EDIT);
483 mathcursor->splitCell();
484 updateLocal(bv, true);
487 case LFUN_DELETE_WORD_BACKWARD:
489 bv->lockedInsetStoreUndo(Undo::DELETE);
490 mathcursor->backspace();
491 bv->updateInset(this, true);
494 case LFUN_DELETE_WORD_FORWARD:
496 bv->lockedInsetStoreUndo(Undo::DELETE);
498 bv->updateInset(this, true);
502 // sprintf(dispatch_buffer, "%d %d",);
503 // dispatch_result = dispatch_buffer;
506 lyxerr << "LFUN_SETXY broken!\n";
509 istringstream is(arg.c_str());
511 mathcursor->setPos(x, y);
512 updateLocal(bv, false);
518 mathcursor->macroModeClose();
519 bv->lockedInsetStoreUndo(Undo::EDIT);
520 mathcursor->selPaste();
521 updateLocal(bv, true);
525 bv->lockedInsetStoreUndo(Undo::DELETE);
526 mathcursor->selCut();
527 updateLocal(bv, true);
531 mathcursor->selCopy();
534 case LFUN_WORDRIGHTSEL:
535 case LFUN_WORDLEFTSEL:
538 // Special casing for superscript in case of LyX handling
540 case LFUN_CIRCUMFLEX:
542 // do superscript if LyX handles
544 bv->lockedInsetStoreUndo(Undo::EDIT);
545 mathcursor->script(true);
546 updateLocal(bv, true);
562 case LFUN_HUNG_UMLAUT:
566 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
567 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
568 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
569 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
570 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
571 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
572 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
573 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
574 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
575 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
576 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
579 handleFont(bv, arg, LM_TC_GREEK1);
581 mathcursor->interpret(arg);
586 handleFont(bv, arg, LM_TC_TEXTRM);
590 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
591 updateLocal(bv, true);
594 //bv->owner()->message(_("math text mode toggled"));
597 case LFUN_MATH_LIMITS:
598 bv->lockedInsetStoreUndo(Undo::EDIT);
599 if (mathcursor->toggleLimits())
600 updateLocal(bv, true);
606 bv->lockedInsetStoreUndo(Undo::EDIT);
607 mathcursor->setSize(arg);
608 updateLocal(bv, true);
613 case LFUN_INSERT_MATRIX:
615 bv->lockedInsetStoreUndo(Undo::EDIT);
616 mathcursor->interpret("matrix " + arg);
617 updateLocal(bv, true);
621 case LFUN_SUPERSCRIPT:
624 bv->lockedInsetStoreUndo(Undo::EDIT);
625 mathcursor->script(action == LFUN_SUPERSCRIPT);
626 updateLocal(bv, true);
630 case LFUN_MATH_DELIM:
632 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
634 string rs = split(arg, ls, ' ');
635 // Reasonable default values
641 bv->lockedInsetStoreUndo(Undo::EDIT);
642 mathcursor->handleDelim(ls, rs);
643 updateLocal(bv, true);
647 case LFUN_PROTECTEDSPACE:
648 case LFUN_MATH_SPACE:
649 bv->lockedInsetStoreUndo(Undo::EDIT);
650 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
651 updateLocal(bv, true);
655 bv->owner()->message(_("Invalid action in math mode!"));
659 case LFUN_MATH_HALIGN:
660 case LFUN_MATH_VALIGN:
661 case LFUN_MATH_ROW_INSERT:
662 case LFUN_MATH_ROW_DELETE:
663 case LFUN_MATH_COLUMN_INSERT:
664 case LFUN_MATH_COLUMN_DELETE:
666 MathInset::idx_type idx = 0;
667 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
669 mathcursor->popToEnclosingGrid();
670 bv->lockedInsetStoreUndo(Undo::EDIT);
671 char align = arg.size() ? arg[0] : 'c';
673 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
674 case LFUN_MATH_VALIGN: p->valign(align); 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);
708 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
710 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
711 updateLocal(bv, true);
715 case LFUN_MATH_PANEL:
716 result = UNDISPATCHED;
720 if (mathcursor->selection())
721 mathcursor->selClear();
723 result = UNDISPATCHED;
727 result = UNDISPATCHED;
730 mathcursor->normalize();
733 lyx::Assert(mathcursor);
735 if (mathcursor->selection() || was_selection)
736 toggleInsetSelection(bv);
738 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
739 result == UNDISPATCHED)
742 bv->unlockInset(this);
746 return result; // original version
750 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
755 // write something to the minibuffer
756 // translate to latex
757 mathcursor->markInsert();
760 string str = os.str();
761 mathcursor->markErase();
762 string::size_type pos = 0;
764 for (string::iterator it = str.begin(); it != str.end(); ++it) {
767 else if (*it == '\0') {
769 pos = it - str.begin();
775 res = res.substr(pos - 30);
777 res = res.substr(0, 60);
778 bv->owner()->message(res);
783 Inset::Code InsetFormulaBase::lyxCode() const
785 return Inset::MATH_CODE;
789 int InsetFormulaBase::ylow() const
791 return yo_ - ascent(view_, font_);
795 int InsetFormulaBase::yhigh() const
797 return yo_ + descent(view_, font_);
801 int InsetFormulaBase::xlow() const
807 int InsetFormulaBase::xhigh() const
809 return xo_ + width(view_, font_);
813 /////////////////////////////////////////////////////////////////////
816 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
822 static InsetFormulaBase * lastformula = 0;
823 static MathIterator current = MathIterator(ibegin(par().nucleus()));
825 static string laststr;
827 if (lastformula != this || laststr != str) {
828 //lyxerr << "reset lastformula to " << this << "\n";
831 current = ibegin(par().nucleus());
833 mathed_parse_cell(ar, str);
837 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
839 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
840 if (it.cell().matchpart(ar, it.position().pos_)) {
841 mathcursor->setSelection(it.cursor(), ar.size());
844 // I guess some of the following can go
845 bv->toggleSelection(true);
847 updateLocal(bv, true);
854 //lyxerr << "not found!\n";
856 // we have to unlock ourself in this function by default!
857 // don't ask me why...
858 bv->unlockInset(this);
863 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
866 lyxerr << "searching backward not implemented in mathed" << endl;
867 return searchForward(bv, what, a, b);
871 /////////////////////////////////////////////////////////////////////
874 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
876 if (bv->available()) {
877 // use selection if available..
879 //if (action == LFUN_MATH_IMPORT_SELECTION)
883 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
885 InsetFormulaBase * f;
887 f = new InsetFormula;
888 if (openNewInset(bv, f)) {
889 // don't do that also for LFUN_MATH_MODE unless you want end up with
890 // always changing to mathrm when opening an inlined inset
891 // -- I really hate "LyXfunc overloading"...
893 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
894 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
897 // create a macro if we see "\\newcommand" somewhere, and an ordinary
899 if (sel.find("\\newcommand") == string::npos &&
900 sel.find("\\def") == string::npos)
901 f = new InsetFormula(sel);
904 if (!mathed_parse_macro(name, sel))
906 f = new InsetFormulaMacro(sel);
908 bv->getLyXText()->cutSelection(bv);
912 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
916 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
918 mathDispatchCreation(bv, arg, true);
922 void mathDispatchMathMode(BufferView * bv, string const & arg)
924 mathDispatchCreation(bv, arg, false);
928 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
930 mathDispatchCreation(bv, arg, true);
934 void mathDispatchMathMacro(BufferView * bv, string const & arg)
936 if (bv->available()) {
938 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
941 string const s1 = token(s, ' ', 1);
942 int const na = s1.empty() ? 0 : lyx::atoi(s1);
943 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
949 void mathDispatchMathDelim(BufferView * bv, string const & arg)
951 if (bv->available()) {
952 if (openNewInset(bv, new InsetFormula))
953 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
958 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
960 if (bv->available()) {
961 if (openNewInset(bv, new InsetFormula))
962 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
967 void mathDispatchInsertMath(BufferView * bv, string const & arg)
969 if (bv->available()) {
970 if (arg.size() && arg[0] == '\\') {
971 InsetFormula * f = new InsetFormula(arg);
972 if (!bv->insertInset(f))
974 else if (!mathcursor) // hotfix
975 bv->getLyXText()->cursorRight(bv);
977 mathDispatchMathMode(bv, arg);
983 void mathDispatchGreek(BufferView * bv, string const & arg)
985 if (bv->available()) {
986 InsetFormula * f = new InsetFormula;
987 if (openNewInset(bv, f)) {
988 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
995 void mathDispatch(BufferView *, kb_action, string const &)