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_fontinset.h"
45 #include "math_hullinset.h"
46 #include "math_iterator.h"
47 #include "math_macrotable.h"
48 #include "math_parser.h"
50 #include "math_spaceinset.h"
51 #include "undo_funcs.h"
52 #include "textpainter.h"
53 #include "frontends/Dialogs.h"
55 #include "insets/insetcommandparams.h"
56 #include "ref_inset.h"
63 MathCursor * mathcursor = 0;
74 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
76 if (!bv->insertInset(new_inset)) {
80 new_inset->edit(bv, true);
89 InsetFormulaBase::InsetFormulaBase()
90 : view_(0), font_(), xo_(0), yo_(0)
92 // This is needed as long the math parser is not re-entrant
94 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
95 //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
96 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
97 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
101 // simply scrap this function if you want
102 void InsetFormulaBase::mutateToText()
105 // translate to latex
107 latex(NULL, os, false, false);
108 string str = os.str();
111 LyXText * lt = view_->getLyXText();
112 string::const_iterator cit = str.begin();
113 string::const_iterator end = str.end();
114 for (; cit != end; ++cit)
115 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
118 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
123 void InsetFormulaBase::handleFont
124 (BufferView * bv, string const & arg, string const & font)
126 bv->lockedInsetStoreUndo(Undo::EDIT);
127 bool sel = mathcursor->selection();
129 updateLocal(bv, true);
130 mathcursor->handleNest(new MathFontInset(font));
131 mathcursor->insert(arg);
133 updateLocal(bv, false);
137 // Check if uses AMS macros
138 void InsetFormulaBase::validate(LaTeXFeatures &) const
142 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
149 void InsetFormulaBase::metrics(BufferView * bv) const
155 //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
156 mi.base.style = LM_ST_TEXT;
157 mi.base.font = font_;
158 mi.base.font.setColor(LColor::math);
163 string const InsetFormulaBase::editMessage() const
165 return _("Math editor mode");
169 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
171 if (!bv->lockInset(this))
172 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174 mathcursor = new MathCursor(this, true);
176 mathcursor->setPos(x, y);
177 //lyxerr << "setting pos to " << x << "," << y << "\n";
179 // if that is removed, we won't get the magenta box when entering an
180 // inset for the first time
181 bv->updateInset(this, false);
185 void InsetFormulaBase::edit(BufferView * bv, bool front)
187 if (!bv->lockInset(this))
188 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
190 mathcursor = new MathCursor(this, front);
192 bv->updateInset(this, false);
196 void InsetFormulaBase::insetUnlock(BufferView * bv)
199 if (mathcursor->inMacroMode()) {
200 mathcursor->macroModeClose();
201 updateLocal(bv, true);
206 bv->updateInset(this, false);
210 void InsetFormulaBase::getCursorPos(BufferView * bv, int & x, int & y) const
213 mathcursor->getPos(x, y);
216 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
220 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 if (isCursorVisible())
226 bv->hideLockedInsetCursor();
231 mathcursor->getPos(x, y);
235 math_font_max_dim(font_, asc, des);
236 bv->showLockedInsetCursor(x, y, asc, des);
237 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
240 toggleCursorVisible();
244 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
246 if (isCursorVisible())
252 mathcursor->getPos(x, y);
256 math_font_max_dim(font_, asc, des);
257 bv->fitLockedInsetCursor(x, y, asc, des);
258 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
260 toggleInsetCursor(bv);
264 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
266 if (isCursorVisible())
267 toggleInsetCursor(bv);
271 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
276 int const asc = font_metrics::maxAscent(font_);
277 int const desc = font_metrics::maxDescent(font_);
280 getCursorPos(bv, x, y);
281 bv->fitLockedInsetCursor(x, y, asc, desc);
285 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
288 bv->updateInset(this, false);
292 vector<string> const InsetFormulaBase::getLabelList() const
294 return vector<string>();
298 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
303 bv->updateInset(this, dirty);
307 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
308 int /*x*/, int /*y*/, mouse_button::state button)
313 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
316 bv->updateInset(this, false);
318 if (button == mouse_button::button3) {
319 // try to dispatch to enclosed insets first
320 if (mathcursor->dispatch("mouse-3-release"))
323 // launch math panel for right mouse button
324 bv->owner()->getDialogs()->showMathPanel();
328 if (button == mouse_button::button1) {
329 // try to dispatch to enclosed insets first
330 if (mathcursor->dispatch("mouse-1-release"))
333 // try to set the cursor
335 //mathcursor = new MathCursor(this, x == 0);
337 //mathcursor->setPos(x + xo_, y + yo_);
344 void InsetFormulaBase::insetButtonPress(BufferView * bv,
345 int x, int y, mouse_button::state button)
347 lyxerr << "insetButtonPress: "
348 << x << " " << y << " but: " << button << "\n";
349 lyxerr << "formula: ";
353 mathcursor = new MathCursor(this, x == 0);
355 if (button == mouse_button::button1) {
356 // just set the cursor here
357 lyxerr << "setting cursor\n";
361 mathcursor->selClear();
362 mathcursor->setPos(x + xo_, y + yo_);
364 if (mathcursor->dispatch("mouse-1-press")) {
370 if (button == mouse_button::button3) {
371 if (mathcursor->dispatch("mouse-3-press")) {
376 bv->updateInset(this, false);
380 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
381 int x, int y, mouse_button::state button)
386 if (button == mouse_button::button1)
387 if (mathcursor->dispatch("mouse-1-motion"))
390 if (button == mouse_button::button3)
391 if (mathcursor->dispatch("mouse-3-motion"))
394 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
395 //lyxerr << "insetMotionNotify: ignored\n";
401 if (!mathcursor->selection())
402 mathcursor->selStart();
404 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
405 // << ' ' << button << "\n";
407 mathcursor->setPos(x + xo_, y + yo_);
409 bv->updateInset(this, false);
413 UpdatableInset::RESULT
414 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
417 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
418 // << " arg: '" << arg
419 // << "' cursor: " << mathcursor
425 RESULT result = DISPATCHED;
427 bool was_macro = mathcursor->inMacroMode();
428 bool was_selection = mathcursor->selection();
432 mathcursor->normalize();
437 // --- Cursor Movements ---------------------------------------------
440 sel = true; // fall through...
443 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
444 //lyxerr << "calling scroll 20\n";
446 updateLocal(bv, false);
447 // write something to the minibuffer
448 //bv->owner()->message(mathcursor->info());
453 sel = true; // fall through
456 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
457 updateLocal(bv, false);
465 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
466 updateLocal(bv, false);
474 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
475 updateLocal(bv, false);
482 mathcursor->home(sel);
483 updateLocal(bv, false);
490 mathcursor->end(sel);
491 updateLocal(bv, false);
494 case LFUN_DELETE_LINE_FORWARD:
495 bv->lockedInsetStoreUndo(Undo::DELETE);
496 mathcursor->delLine();
497 updateLocal(bv, true);
501 mathcursor->idxNext();
502 updateLocal(bv, false);
506 mathcursor->idxPrev();
507 updateLocal(bv, false);
511 bv->lockedInsetStoreUndo(Undo::EDIT);
512 mathcursor->splitCell();
513 updateLocal(bv, true);
516 case LFUN_DELETE_WORD_BACKWARD:
518 bv->lockedInsetStoreUndo(Undo::DELETE);
519 mathcursor->backspace();
520 updateLocal(bv, true);
523 case LFUN_DELETE_WORD_FORWARD:
525 bv->lockedInsetStoreUndo(Undo::DELETE);
527 bv->updateInset(this, true);
531 // sprintf(dispatch_buffer, "%d %d",);
532 // dispatch_result = dispatch_buffer;
535 lyxerr << "LFUN_SETXY broken!\n";
538 istringstream is(arg.c_str());
540 mathcursor->setPos(x, y);
541 updateLocal(bv, false);
547 mathcursor->macroModeClose();
548 bv->lockedInsetStoreUndo(Undo::EDIT);
549 mathcursor->selPaste();
550 updateLocal(bv, true);
554 bv->lockedInsetStoreUndo(Undo::DELETE);
555 mathcursor->selCut();
556 updateLocal(bv, true);
560 mathcursor->selCopy();
563 case LFUN_WORDRIGHTSEL:
564 case LFUN_WORDLEFTSEL:
567 // Special casing for superscript in case of LyX handling
569 case LFUN_CIRCUMFLEX:
571 // do superscript if LyX handles
573 bv->lockedInsetStoreUndo(Undo::EDIT);
574 mathcursor->script(true);
575 updateLocal(bv, true);
591 case LFUN_HUNG_UMLAUT:
595 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
596 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
597 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
598 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
599 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
600 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
601 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
602 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
603 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
604 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
605 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
608 handleFont(bv, arg, "lyxgreek1");
610 mathcursor->interpret(arg);
614 if (mathcursor->inMathMode()) {
615 handleFont(bv, arg, "textrm");
617 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
618 updateLocal(bv, true);
620 //bv->owner()->message(_("math text mode toggled"));
623 case LFUN_MATH_LIMITS:
624 bv->lockedInsetStoreUndo(Undo::EDIT);
625 if (mathcursor->toggleLimits())
626 updateLocal(bv, true);
632 bv->lockedInsetStoreUndo(Undo::EDIT);
633 mathcursor->setSize(arg);
634 updateLocal(bv, true);
639 case LFUN_INSERT_MATRIX:
641 bv->lockedInsetStoreUndo(Undo::EDIT);
642 mathcursor->interpret("matrix " + arg);
643 updateLocal(bv, true);
647 case LFUN_SUPERSCRIPT:
650 bv->lockedInsetStoreUndo(Undo::EDIT);
651 mathcursor->script(action == LFUN_SUPERSCRIPT);
652 updateLocal(bv, true);
656 case LFUN_MATH_DELIM:
658 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
660 string rs = split(arg, ls, ' ');
661 // Reasonable default values
667 bv->lockedInsetStoreUndo(Undo::EDIT);
668 mathcursor->handleDelim(ls, rs);
669 updateLocal(bv, true);
673 case LFUN_PROTECTEDSPACE:
674 case LFUN_MATH_SPACE:
675 bv->lockedInsetStoreUndo(Undo::EDIT);
676 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
677 updateLocal(bv, true);
681 bv->owner()->message(_("Invalid action in math mode!"));
685 case LFUN_MATH_HALIGN:
686 case LFUN_MATH_VALIGN:
687 case LFUN_MATH_ROW_INSERT:
688 case LFUN_MATH_ROW_DELETE:
689 case LFUN_MATH_COLUMN_INSERT:
690 case LFUN_MATH_COLUMN_DELETE:
692 MathInset::idx_type idx = 0;
693 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
695 mathcursor->popToEnclosingGrid();
696 bv->lockedInsetStoreUndo(Undo::EDIT);
697 char align = arg.size() ? arg[0] : 'c';
699 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
700 case LFUN_MATH_VALIGN: p->valign(align); break;
701 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
702 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
703 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
704 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
707 updateLocal(bv, true);
712 case LFUN_EXEC_COMMAND:
713 result = UNDISPATCHED;
716 case LFUN_BREAKPARAGRAPH:
717 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
718 //lyxerr << "LFUN ignored\n";
722 // interpret this as if a backslash was typed
723 bv->lockedInsetStoreUndo(Undo::EDIT);
724 mathcursor->interpret('\\');
725 updateLocal(bv, true);
729 case LFUN_INSERT_MATH:
730 case LFUN_SELFINSERT:
732 bv->lockedInsetStoreUndo(Undo::EDIT);
734 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
736 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
737 updateLocal(bv, true);
741 case LFUN_MATH_PANEL:
742 result = UNDISPATCHED;
746 if (mathcursor->selection())
747 mathcursor->selClear();
749 result = UNDISPATCHED;
752 case LFUN_INSET_TOGGLE:
753 mathcursor->insetToggle();
754 updateLocal(bv, true);
757 case LFUN_REF_INSERT:
758 //if (argument.empty()) {
759 // InsetCommandParams p("ref");
760 // owner_->getDialogs()->createRef(p.getAsString());
762 // InsetCommandParams p;
763 // p.setFromString(argument);
765 // InsetRef * inset = new InsetRef(p, *buffer_);
766 // if (!insertInset(inset))
769 // updateInset(inset, true);
773 InsetCommandParams p("ref");
774 bv->owner()->getDialogs()->createRef(p.getAsString());
776 //mathcursor->handleNest(new InsetRef2);
777 //mathcursor->insert(arg);
778 mathcursor->insert(MathAtom(new RefInset(arg)));
780 updateLocal(bv, true);
784 result = UNDISPATCHED;
787 mathcursor->normalize();
790 lyx::Assert(mathcursor);
792 if (mathcursor->selection() || was_selection)
793 toggleInsetSelection(bv);
795 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
796 result == UNDISPATCHED)
799 bv->unlockInset(this);
803 return result; // original version
807 void InsetFormulaBase::revealCodes(BufferView * bv) const
811 bv->owner()->message(mathcursor->info());
814 // write something to the minibuffer
815 // translate to latex
816 mathcursor->markInsert();
819 string str = os.str();
820 mathcursor->markErase();
821 string::size_type pos = 0;
823 for (string::iterator it = str.begin(); it != str.end(); ++it) {
826 else if (*it == '\0') {
828 pos = it - str.begin();
834 res = res.substr(pos - 30);
836 res = res.substr(0, 60);
837 bv->owner()->message(res);
842 Inset::Code InsetFormulaBase::lyxCode() const
844 return Inset::MATH_CODE;
848 int InsetFormulaBase::ylow() const
850 return yo_ - ascent(view_, font_);
854 int InsetFormulaBase::yhigh() const
856 return yo_ + descent(view_, font_);
860 int InsetFormulaBase::xlow() const
866 int InsetFormulaBase::xhigh() const
868 return xo_ + width(view_, font_);
872 /////////////////////////////////////////////////////////////////////
875 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
881 static InsetFormulaBase * lastformula = 0;
882 static MathIterator current = MathIterator(ibegin(par().nucleus()));
884 static string laststr;
886 if (lastformula != this || laststr != str) {
887 //lyxerr << "reset lastformula to " << this << "\n";
890 current = ibegin(par().nucleus());
892 mathed_parse_cell(ar, str);
896 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
898 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
899 if (it.cell().matchpart(ar, it.position().pos_)) {
900 bv->unlockInset(bv->theLockingInset());
901 if (!bv->lockInset(this)) {
902 lyxerr << "Cannot lock inset" << endl;
906 mathcursor = new MathCursor(this, true);
908 mathcursor->setSelection(it.cursor(), ar.size());
911 updateLocal(bv, false);
916 //lyxerr << "not found!\n";
922 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
925 lyxerr << "searching backward not implemented in mathed" << endl;
926 return searchForward(bv, what, a, b);
930 /////////////////////////////////////////////////////////////////////
933 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
935 if (!bv->available())
938 // use selection if available..
940 //if (action == LFUN_MATH_IMPORT_SELECTION)
944 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
947 InsetFormula * f = new InsetFormula(bv);
948 if (openNewInset(bv, f)) {
950 // don't do that also for LFUN_MATH_MODE unless you want end up with
951 // always changing to mathrm when opening an inlined inset
952 // -- I really hate "LyXfunc overloading"...
954 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
955 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
958 // create a macro if we see "\\newcommand" somewhere, and an ordinary
960 InsetFormulaBase * f;
961 if (sel.find("\\newcommand") == string::npos &&
962 sel.find("\\def") == string::npos)
963 f = new InsetFormula(sel);
965 f = new InsetFormulaMacro(sel);
966 bv->getLyXText()->cutSelection(bv);
969 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
973 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
975 mathDispatchCreation(bv, arg, true);
979 void mathDispatchMathMode(BufferView * bv, string const & arg)
981 mathDispatchCreation(bv, arg, false);
985 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
987 mathDispatchCreation(bv, arg, true);
991 void mathDispatchMathMacro(BufferView * bv, string const & arg)
993 if (!bv->available())
996 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
999 string const s1 = token(s, ' ', 1);
1000 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1001 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1006 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1008 if (!bv->available())
1010 InsetFormula * f = new InsetFormula(bv);
1011 if (openNewInset(bv, f)) {
1012 f->mutate("simple");
1013 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1018 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1020 if (!bv->available())
1022 InsetFormula * f = new InsetFormula(bv);
1023 if (openNewInset(bv, f)) {
1024 f->mutate("simple");
1025 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1030 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1032 if (!bv->available())
1034 InsetFormula * f = new InsetFormula(bv);
1035 if (openNewInset(bv, f)) {
1036 f->mutate("simple");
1037 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1042 void mathDispatchGreek(BufferView * bv, string const & arg)
1044 if (!bv->available())
1046 InsetFormula * f = new InsetFormula(bv);
1047 if (openNewInset(bv, f)) {
1048 f->mutate("simple");
1049 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1055 void mathDispatch(BufferView *, kb_action, string const &)
1059 void mathDispatch(BufferView *, string const &)