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"
28 #include "commandtags.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "math_support.h"
36 #include "support/lstrings.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/font_metrics.h"
39 #include "frontends/mouse_state.h"
41 #include "math_arrayinset.h"
42 #include "math_charinset.h"
43 #include "math_cursor.h"
44 #include "math_factory.h"
45 #include "math_fontinset.h"
46 #include "math_hullinset.h"
47 #include "math_iterator.h"
48 #include "math_macrotable.h"
49 #include "math_parser.h"
51 #include "math_spaceinset.h"
52 #include "undo_funcs.h"
53 #include "textpainter.h"
54 #include "frontends/Dialogs.h"
56 #include "insets/insetcommandparams.h"
57 #include "ref_inset.h"
64 MathCursor * mathcursor = 0;
75 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
77 if (!bv->insertInset(new_inset)) {
81 new_inset->edit(bv, true);
90 InsetFormulaBase::InsetFormulaBase()
91 : view_(0), font_(), xo_(0), yo_(0)
93 // This is needed as long the math parser is not re-entrant
94 MathMacroTable::builtinMacros();
95 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
96 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
97 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
98 //lyxerr << "sizeof(LyXFont): " << sizeof(LyXFont) << "\n";
102 // simply scrap this function if you want
103 void InsetFormulaBase::mutateToText()
106 // translate to latex
108 latex(NULL, os, false, false);
109 string str = os.str();
112 LyXText * lt = view_->getLyXText();
113 string::const_iterator cit = str.begin();
114 string::const_iterator end = str.end();
115 for (; cit != end; ++cit)
116 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
119 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
124 void InsetFormulaBase::handleFont
125 (BufferView * bv, string const & arg, string const & font)
127 bv->lockedInsetStoreUndo(Undo::EDIT);
128 bool sel = mathcursor->selection();
130 updateLocal(bv, true);
131 mathcursor->handleNest(new MathFontInset(font));
132 mathcursor->insert(arg);
134 updateLocal(bv, false);
138 // Check if uses AMS macros
139 void InsetFormulaBase::validate(LaTeXFeatures &) const
143 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
150 void InsetFormulaBase::metrics(BufferView * bv) const
156 //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
157 mi.base.style = LM_ST_TEXT;
158 mi.base.font = font_;
159 mi.base.font.setColor(LColor::math);
164 string const InsetFormulaBase::editMessage() const
166 return _("Math editor mode");
170 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
172 if (!bv->lockInset(this))
173 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
175 mathcursor = new MathCursor(this, true);
177 mathcursor->setPos(x, y);
178 //lyxerr << "setting pos to " << x << "," << y << "\n";
180 // if that is removed, we won't get the magenta box when entering an
181 // inset for the first time
182 bv->updateInset(this, false);
186 void InsetFormulaBase::edit(BufferView * bv, bool front)
188 if (!bv->lockInset(this))
189 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
191 mathcursor = new MathCursor(this, front);
193 bv->updateInset(this, false);
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
200 if (mathcursor->inMacroMode()) {
201 mathcursor->macroModeClose();
202 updateLocal(bv, true);
207 bv->updateInset(this, false);
211 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
214 mathcursor->getPos(x, y);
217 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
221 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
226 if (isCursorVisible())
227 bv->hideLockedInsetCursor();
232 mathcursor->getPos(x, y);
236 math_font_max_dim(font_, asc, des);
237 bv->showLockedInsetCursor(x, y, asc, des);
238 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
241 toggleCursorVisible();
245 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
247 if (isCursorVisible())
253 mathcursor->getPos(x, y);
257 math_font_max_dim(font_, asc, des);
258 bv->fitLockedInsetCursor(x, y, asc, des);
259 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
261 toggleInsetCursor(bv);
265 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
267 if (isCursorVisible())
268 toggleInsetCursor(bv);
272 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
277 int const asc = font_metrics::maxAscent(font_);
278 int const desc = font_metrics::maxDescent(font_);
281 getCursorPos(bv, x, y);
282 bv->fitLockedInsetCursor(x, y, asc, desc);
286 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
289 bv->updateInset(this, false);
293 vector<string> const InsetFormulaBase::getLabelList() const
295 return vector<string>();
299 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
304 bv->updateInset(this, dirty);
308 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
309 int /*x*/, int /*y*/, mouse_button::state button)
314 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
317 bv->updateInset(this, false);
319 if (button == mouse_button::button3) {
320 // try to dispatch to enclosed insets first
321 if (mathcursor->dispatch("mouse-3-release"))
324 // launch math panel for right mouse button
325 bv->owner()->getDialogs()->showMathPanel();
329 if (button == mouse_button::button1) {
330 // try to dispatch to enclosed insets first
331 if (mathcursor->dispatch("mouse-1-release"))
334 // try to set the cursor
336 //mathcursor = new MathCursor(this, x == 0);
338 //mathcursor->setPos(x + xo_, y + yo_);
345 void InsetFormulaBase::insetButtonPress(BufferView * bv,
346 int x, int y, mouse_button::state button)
348 lyxerr << "insetButtonPress: "
349 << x << " " << y << " but: " << button << "\n";
350 lyxerr << "formula: ";
354 mathcursor = new MathCursor(this, x == 0);
356 if (button == mouse_button::button1) {
357 // just set the cursor here
358 lyxerr << "setting cursor\n";
362 mathcursor->selClear();
363 mathcursor->setPos(x + xo_, y + yo_);
365 if (mathcursor->dispatch("mouse-1-press")) {
371 if (button == mouse_button::button3) {
372 if (mathcursor->dispatch("mouse-3-press")) {
377 bv->updateInset(this, false);
381 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
382 int x, int y, mouse_button::state button)
387 if (button == mouse_button::button1)
388 if (mathcursor->dispatch("mouse-1-motion"))
391 if (button == mouse_button::button3)
392 if (mathcursor->dispatch("mouse-3-motion"))
395 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
396 //lyxerr << "insetMotionNotify: ignored\n";
402 if (!mathcursor->selection())
403 mathcursor->selStart();
405 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
406 // << ' ' << button << "\n";
408 mathcursor->setPos(x + xo_, y + yo_);
410 bv->updateInset(this, false);
414 UpdatableInset::RESULT
415 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
418 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
419 // << " arg: '" << arg
420 // << "' cursor: " << mathcursor
426 RESULT result = DISPATCHED;
428 bool was_macro = mathcursor->inMacroMode();
429 bool was_selection = mathcursor->selection();
433 mathcursor->normalize();
438 // --- Cursor Movements ---------------------------------------------
441 sel = true; // fall through...
444 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
445 //lyxerr << "calling scroll 20\n";
447 updateLocal(bv, false);
448 // write something to the minibuffer
449 //bv->owner()->message(mathcursor->info());
454 sel = true; // fall through
457 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
458 updateLocal(bv, false);
466 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
467 updateLocal(bv, false);
475 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
476 updateLocal(bv, false);
483 mathcursor->home(sel);
484 updateLocal(bv, false);
491 mathcursor->end(sel);
492 updateLocal(bv, false);
495 case LFUN_DELETE_LINE_FORWARD:
496 bv->lockedInsetStoreUndo(Undo::DELETE);
497 mathcursor->delLine();
498 updateLocal(bv, true);
502 mathcursor->idxNext();
503 updateLocal(bv, false);
507 mathcursor->idxPrev();
508 updateLocal(bv, false);
512 bv->lockedInsetStoreUndo(Undo::EDIT);
513 mathcursor->splitCell();
514 updateLocal(bv, true);
517 case LFUN_DELETE_WORD_BACKWARD:
519 bv->lockedInsetStoreUndo(Undo::DELETE);
520 mathcursor->backspace();
521 updateLocal(bv, true);
524 case LFUN_DELETE_WORD_FORWARD:
526 bv->lockedInsetStoreUndo(Undo::DELETE);
528 bv->updateInset(this, true);
532 // sprintf(dispatch_buffer, "%d %d",);
533 // dispatch_result = dispatch_buffer;
536 lyxerr << "LFUN_SETXY broken!\n";
539 istringstream is(arg.c_str());
541 mathcursor->setPos(x, y);
542 updateLocal(bv, false);
548 mathcursor->macroModeClose();
549 bv->lockedInsetStoreUndo(Undo::EDIT);
550 mathcursor->selPaste();
551 updateLocal(bv, true);
555 bv->lockedInsetStoreUndo(Undo::DELETE);
556 mathcursor->selCut();
557 updateLocal(bv, true);
561 mathcursor->selCopy();
564 case LFUN_WORDRIGHTSEL:
565 case LFUN_WORDLEFTSEL:
568 // Special casing for superscript in case of LyX handling
570 case LFUN_CIRCUMFLEX:
572 // do superscript if LyX handles
574 bv->lockedInsetStoreUndo(Undo::EDIT);
575 mathcursor->script(true);
576 updateLocal(bv, true);
592 case LFUN_HUNG_UMLAUT:
596 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
597 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
598 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
599 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
600 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
601 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
602 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
603 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
604 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
605 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
606 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
609 handleFont(bv, arg, "lyxgreek1");
611 mathcursor->interpret(arg);
615 if (mathcursor->inMathMode()) {
616 handleFont(bv, arg, "textrm");
618 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
619 updateLocal(bv, true);
621 //bv->owner()->message(_("math text mode toggled"));
624 case LFUN_MATH_LIMITS:
625 bv->lockedInsetStoreUndo(Undo::EDIT);
626 if (mathcursor->toggleLimits())
627 updateLocal(bv, true);
633 bv->lockedInsetStoreUndo(Undo::EDIT);
634 mathcursor->setSize(arg);
635 updateLocal(bv, true);
640 case LFUN_INSERT_MATRIX:
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->interpret("matrix " + arg);
644 updateLocal(bv, true);
648 case LFUN_SUPERSCRIPT:
651 bv->lockedInsetStoreUndo(Undo::EDIT);
652 mathcursor->script(action == LFUN_SUPERSCRIPT);
653 updateLocal(bv, true);
657 case LFUN_MATH_DELIM:
659 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
661 string rs = split(arg, ls, ' ');
662 // Reasonable default values
668 bv->lockedInsetStoreUndo(Undo::EDIT);
669 mathcursor->handleDelim(ls, rs);
670 updateLocal(bv, true);
674 case LFUN_PROTECTEDSPACE:
675 case LFUN_MATH_SPACE:
676 bv->lockedInsetStoreUndo(Undo::EDIT);
677 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
678 updateLocal(bv, true);
682 bv->owner()->message(_("Invalid action in math mode!"));
686 case LFUN_MATH_HALIGN:
687 case LFUN_MATH_VALIGN:
688 case LFUN_MATH_ROW_INSERT:
689 case LFUN_MATH_ROW_DELETE:
690 case LFUN_MATH_COLUMN_INSERT:
691 case LFUN_MATH_COLUMN_DELETE:
693 MathInset::idx_type idx = 0;
694 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
696 mathcursor->popToEnclosingGrid();
697 bv->lockedInsetStoreUndo(Undo::EDIT);
698 char align = arg.size() ? arg[0] : 'c';
700 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
701 case LFUN_MATH_VALIGN: p->valign(align); break;
702 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
703 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
704 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
705 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
708 updateLocal(bv, true);
713 case LFUN_EXEC_COMMAND:
714 result = UNDISPATCHED;
717 case LFUN_BREAKPARAGRAPH:
718 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
719 //lyxerr << "LFUN ignored\n";
723 // interpret this as if a backslash was typed
724 bv->lockedInsetStoreUndo(Undo::EDIT);
725 mathcursor->interpret('\\');
726 updateLocal(bv, true);
730 case LFUN_INSERT_MATH:
731 case LFUN_SELFINSERT:
733 bv->lockedInsetStoreUndo(Undo::EDIT);
735 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
737 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
738 updateLocal(bv, true);
742 case LFUN_MATH_PANEL:
743 result = UNDISPATCHED;
747 if (mathcursor->selection())
748 mathcursor->selClear();
750 result = UNDISPATCHED;
753 case LFUN_INSET_TOGGLE:
754 mathcursor->insetToggle();
755 updateLocal(bv, true);
758 case LFUN_REF_INSERT:
759 //if (argument.empty()) {
760 // InsetCommandParams p("ref");
761 // owner_->getDialogs()->createRef(p.getAsString());
763 // InsetCommandParams p;
764 // p.setFromString(argument);
766 // InsetRef * inset = new InsetRef(p, *buffer_);
767 // if (!insertInset(inset))
770 // updateInset(inset, true);
774 InsetCommandParams p("ref");
775 bv->owner()->getDialogs()->createRef(p.getAsString());
777 //mathcursor->handleNest(new InsetRef2);
778 //mathcursor->insert(arg);
779 mathcursor->insert(MathAtom(new RefInset(arg)));
781 updateLocal(bv, true);
785 result = UNDISPATCHED;
788 mathcursor->normalize();
791 lyx::Assert(mathcursor);
793 if (mathcursor->selection() || was_selection)
794 toggleInsetSelection(bv);
796 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
797 result == UNDISPATCHED)
800 bv->unlockInset(this);
804 return result; // original version
808 void InsetFormulaBase::revealCodes(BufferView * bv) const
812 bv->owner()->message(mathcursor->info());
815 // write something to the minibuffer
816 // translate to latex
817 mathcursor->markInsert();
820 string str = os.str();
821 mathcursor->markErase();
822 string::size_type pos = 0;
824 for (string::iterator it = str.begin(); it != str.end(); ++it) {
827 else if (*it == '\0') {
829 pos = it - str.begin();
835 res = res.substr(pos - 30);
837 res = res.substr(0, 60);
838 bv->owner()->message(res);
843 Inset::Code InsetFormulaBase::lyxCode() const
845 return Inset::MATH_CODE;
849 int InsetFormulaBase::ylow() const
851 return yo_ - ascent(view_, font_);
855 int InsetFormulaBase::yhigh() const
857 return yo_ + descent(view_, font_);
861 int InsetFormulaBase::xlow() const
867 int InsetFormulaBase::xhigh() const
869 return xo_ + width(view_, font_);
873 /////////////////////////////////////////////////////////////////////
876 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
882 static InsetFormulaBase * lastformula = 0;
883 static MathIterator current = MathIterator(ibegin(par().nucleus()));
885 static string laststr;
887 if (lastformula != this || laststr != str) {
888 //lyxerr << "reset lastformula to " << this << "\n";
891 current = ibegin(par().nucleus());
893 mathed_parse_cell(ar, str);
897 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
899 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
900 if (it.cell().matchpart(ar, it.position().pos_)) {
901 bv->unlockInset(bv->theLockingInset());
902 if (!bv->lockInset(this)) {
903 lyxerr << "Cannot lock inset" << endl;
907 mathcursor = new MathCursor(this, true);
909 mathcursor->setSelection(it.cursor(), ar.size());
912 updateLocal(bv, false);
917 //lyxerr << "not found!\n";
923 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
926 lyxerr << "searching backward not implemented in mathed" << endl;
927 return searchForward(bv, what, a, b);
931 /////////////////////////////////////////////////////////////////////
934 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
936 if (!bv->available())
939 // use selection if available..
941 //if (action == LFUN_MATH_IMPORT_SELECTION)
945 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
948 InsetFormula * f = new InsetFormula(bv);
949 if (openNewInset(bv, f)) {
951 // don't do that also for LFUN_MATH_MODE unless you want end up with
952 // always changing to mathrm when opening an inlined inset
953 // -- I really hate "LyXfunc overloading"...
955 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
956 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
959 // create a macro if we see "\\newcommand" somewhere, and an ordinary
961 if (sel.find("\\newcommand") == string::npos &&
962 sel.find("\\def") == string::npos)
964 InsetFormula * f = new InsetFormula(sel);
965 bv->getLyXText()->cutSelection(bv);
969 if (!mathed_parse_macro(name, sel))
971 InsetFormulaMacro * f = new InsetFormulaMacro(sel);
972 bv->getLyXText()->cutSelection(bv);
976 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
980 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
982 mathDispatchCreation(bv, arg, true);
986 void mathDispatchMathMode(BufferView * bv, string const & arg)
988 mathDispatchCreation(bv, arg, false);
992 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
994 mathDispatchCreation(bv, arg, true);
998 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1000 if (!bv->available())
1003 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1006 string const s1 = token(s, ' ', 1);
1007 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1008 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1013 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1015 if (!bv->available())
1017 InsetFormula * f = new InsetFormula(bv);
1018 if (openNewInset(bv, f)) {
1019 f->mutate("simple");
1020 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1025 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1027 if (!bv->available())
1029 InsetFormula * f = new InsetFormula(bv);
1030 if (openNewInset(bv, f)) {
1031 f->mutate("simple");
1032 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1037 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1039 if (!bv->available())
1041 InsetFormula * f = new InsetFormula(bv);
1042 if (openNewInset(bv, f)) {
1043 f->mutate("simple");
1044 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1049 void mathDispatchGreek(BufferView * bv, string const & arg)
1051 if (!bv->available())
1053 InsetFormula * f = new InsetFormula(bv);
1054 if (openNewInset(bv, f)) {
1055 f->mutate("simple");
1056 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1062 void mathDispatch(BufferView *, kb_action, string const &)
1066 void mathDispatch(BufferView *, string const &)