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_deliminset.h"
44 #include "math_cursor.h"
45 #include "math_factory.h"
46 #include "math_fontinset.h"
47 #include "math_hullinset.h"
48 #include "math_iterator.h"
49 #include "math_macrotable.h"
50 #include "math_parser.h"
52 #include "math_spaceinset.h"
53 #include "undo_funcs.h"
54 #include "textpainter.h"
55 #include "frontends/Dialogs.h"
57 #include "insets/insetcommandparams.h"
58 #include "ref_inset.h"
65 MathCursor * mathcursor = 0;
76 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
78 if (!bv->insertInset(new_inset)) {
82 new_inset->edit(bv, true);
91 InsetFormulaBase::InsetFormulaBase()
92 : view_(0), font_(), xo_(0), yo_(0)
94 // This is needed as long the math parser is not re-entrant
96 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
97 //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
98 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
99 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
103 // simply scrap this function if you want
104 void InsetFormulaBase::mutateToText()
107 // translate to latex
109 latex(NULL, os, false, false);
110 string str = os.str();
113 LyXText * lt = view_->getLyXText();
114 string::const_iterator cit = str.begin();
115 string::const_iterator end = str.end();
116 for (; cit != end; ++cit)
117 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
120 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
125 void InsetFormulaBase::handleFont
126 (BufferView * bv, string const & arg, string const & font)
128 bv->lockedInsetStoreUndo(Undo::EDIT);
129 bool sel = mathcursor->selection();
131 updateLocal(bv, true);
132 mathcursor->handleNest(createMathInset(font));
133 mathcursor->insert(arg);
135 updateLocal(bv, false);
139 // Check if uses AMS macros
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
144 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
151 void InsetFormulaBase::metrics(BufferView * bv) const
157 //mi.base.style = display() ? LM_ST_DISPLAY : LM_ST_TEXT;
158 mi.base.style = LM_ST_TEXT;
159 mi.base.font = font_;
160 mi.base.font.setColor(LColor::math);
165 string const InsetFormulaBase::editMessage() const
167 return _("Math editor mode");
171 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
173 if (!bv->lockInset(this))
174 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
175 releaseMathCursor(bv);
176 mathcursor = new MathCursor(this, true);
178 mathcursor->setPos(x, y);
179 //lyxerr << "setting pos to " << x << "," << y << "\n";
181 // if that is removed, we won't get the magenta box when entering an
182 // inset for the first time
183 bv->updateInset(this, false);
187 void InsetFormulaBase::edit(BufferView * bv, bool front)
189 if (!bv->lockInset(this))
190 lyxerr << "Cannot lock math inset in edit call!\n";
191 releaseMathCursor(bv);
192 mathcursor = new MathCursor(this, front);
194 bv->updateInset(this, false);
198 void InsetFormulaBase::insetUnlock(BufferView * bv)
201 if (mathcursor->inMacroMode()) {
202 mathcursor->macroModeClose();
203 updateLocal(bv, true);
205 releaseMathCursor(bv);
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)
223 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
224 if (isCursorVisible())
231 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
233 if (isCursorVisible())
237 getCursorPos(bv, x, y);
238 math_font_max_dim(font_, asc, des);
239 bv->showLockedInsetCursor(x, y, asc, des);
240 setCursorVisible(true);
241 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
245 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
247 if (!isCursorVisible())
249 bv->hideLockedInsetCursor();
250 setCursorVisible(false);
251 //lyxerr << "hideInsetCursor: \n";
255 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
260 math_font_max_dim(font_, asc, des);
261 getCursorPos(bv, x, y);
263 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
264 bv->fitLockedInsetCursor(x, y, asc, des);
268 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
271 bv->updateInset(this, false);
275 vector<string> const InsetFormulaBase::getLabelList() const
277 return vector<string>();
281 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
286 bv->updateInset(this, dirty);
290 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
291 int /*x*/, int /*y*/, mouse_button::state button)
296 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
299 bv->updateInset(this, false);
301 if (button == mouse_button::button3) {
302 // try to dispatch to enclosed insets first
303 if (mathcursor->dispatch("mouse-3-release"))
306 // launch math panel for right mouse button
307 bv->owner()->getDialogs()->showMathPanel();
311 if (button == mouse_button::button1) {
312 // try to dispatch to enclosed insets first
313 if (mathcursor->dispatch("mouse-1-release"))
316 // try to set the cursor
318 //mathcursor = new MathCursor(this, x == 0);
320 //mathcursor->setPos(x + xo_, y + yo_);
327 void InsetFormulaBase::insetButtonPress(BufferView * bv,
328 int x, int y, mouse_button::state button)
330 //lyxerr << "insetButtonPress: "
331 // << x << " " << y << " but: " << button << "\n";
332 //lyxerr << "formula: ";
335 releaseMathCursor(bv);
336 mathcursor = new MathCursor(this, x == 0);
338 if (button == mouse_button::button1) {
339 // just set the cursor here
340 lyxerr << "setting cursor\n";
344 mathcursor->selClear();
345 mathcursor->setPos(x + xo_, y + yo_);
347 if (mathcursor->dispatch("mouse-1-press")) {
353 if (button == mouse_button::button3) {
354 if (mathcursor->dispatch("mouse-3-press")) {
359 bv->updateInset(this, false);
363 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
364 int x, int y, mouse_button::state button)
369 if (button == mouse_button::button1)
370 if (mathcursor->dispatch("mouse-1-motion"))
373 if (button == mouse_button::button3)
374 if (mathcursor->dispatch("mouse-3-motion"))
377 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
378 //lyxerr << "insetMotionNotify: ignored\n";
384 if (!mathcursor->selection())
385 mathcursor->selStart();
387 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
388 // << ' ' << button << "\n";
390 mathcursor->setPos(x + xo_, y + yo_);
392 bv->updateInset(this, false);
396 UpdatableInset::RESULT
397 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
400 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
401 // << " arg: '" << arg
402 // << "' cursor: " << mathcursor << "\n";
407 RESULT result = DISPATCHED;
409 bool was_macro = mathcursor->inMacroMode();
410 bool was_selection = mathcursor->selection();
414 mathcursor->normalize();
419 case LFUN_WORDRIGHTSEL:
421 sel = true; // fall through...
423 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
424 //lyxerr << "calling scroll 20\n";
426 updateLocal(bv, false);
427 // write something to the minibuffer
428 //bv->owner()->message(mathcursor->info());
431 case LFUN_WORDLEFTSEL:
433 sel = true; // fall through
435 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
436 updateLocal(bv, false);
440 sel = true; // fall through
442 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
443 updateLocal(bv, false);
447 sel = true; // fall through
449 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
450 updateLocal(bv, false);
454 sel = true; // fall through
456 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
457 updateLocal(bv, false);
461 sel = true; // fall through
463 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
464 updateLocal(bv, false);
469 result = FINISHED_UP;
470 updateLocal(bv, false);
475 result = FINISHED_DOWN;
476 updateLocal(bv, false);
479 case LFUN_DELETE_LINE_FORWARD:
480 bv->lockedInsetStoreUndo(Undo::DELETE);
481 mathcursor->delLine();
482 updateLocal(bv, true);
486 mathcursor->idxNext();
487 updateLocal(bv, false);
491 mathcursor->idxPrev();
492 updateLocal(bv, false);
496 bv->lockedInsetStoreUndo(Undo::EDIT);
497 mathcursor->splitCell();
498 updateLocal(bv, true);
501 case LFUN_DELETE_WORD_BACKWARD:
503 bv->lockedInsetStoreUndo(Undo::DELETE);
504 mathcursor->backspace();
505 updateLocal(bv, true);
508 case LFUN_DELETE_WORD_FORWARD:
510 bv->lockedInsetStoreUndo(Undo::DELETE);
512 bv->updateInset(this, true);
516 // sprintf(dispatch_buffer, "%d %d",);
517 // dispatch_result = dispatch_buffer;
520 lyxerr << "LFUN_SETXY broken!\n";
523 istringstream is(arg.c_str());
525 mathcursor->setPos(x, y);
526 updateLocal(bv, false);
532 mathcursor->macroModeClose();
533 bv->lockedInsetStoreUndo(Undo::EDIT);
534 mathcursor->selPaste();
535 updateLocal(bv, true);
539 bv->lockedInsetStoreUndo(Undo::DELETE);
540 mathcursor->selCut();
541 updateLocal(bv, true);
545 mathcursor->selCopy();
549 // Special casing for superscript in case of LyX handling
551 case LFUN_CIRCUMFLEX:
553 // do superscript if LyX handles
555 bv->lockedInsetStoreUndo(Undo::EDIT);
556 mathcursor->script(true);
557 updateLocal(bv, true);
574 case LFUN_HUNG_UMLAUT:
578 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
579 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
580 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
581 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
582 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
583 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
584 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
585 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
586 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
587 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
588 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
591 handleFont(bv, arg, "lyxgreek1");
593 mathcursor->interpret(arg);
597 if (mathcursor->currentMode()) {
598 handleFont(bv, arg, "textrm");
600 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
601 updateLocal(bv, true);
603 //bv->owner()->message(_("math text mode toggled"));
606 case LFUN_MATH_LIMITS:
607 bv->lockedInsetStoreUndo(Undo::EDIT);
608 if (mathcursor->toggleLimits())
609 updateLocal(bv, true);
615 bv->lockedInsetStoreUndo(Undo::EDIT);
616 mathcursor->setSize(arg);
617 updateLocal(bv, true);
622 case LFUN_INSERT_MATRIX:
624 bv->lockedInsetStoreUndo(Undo::EDIT);
625 mathcursor->interpret("matrix " + arg);
626 updateLocal(bv, true);
630 case LFUN_SUPERSCRIPT:
633 bv->lockedInsetStoreUndo(Undo::EDIT);
634 mathcursor->script(action == LFUN_SUPERSCRIPT);
635 updateLocal(bv, true);
639 case LFUN_MATH_DELIM:
641 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
643 string rs = split(arg, ls, ' ');
644 // Reasonable default values
650 bv->lockedInsetStoreUndo(Undo::EDIT);
651 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
652 updateLocal(bv, true);
656 case LFUN_PROTECTEDSPACE:
657 case LFUN_MATH_SPACE:
658 bv->lockedInsetStoreUndo(Undo::EDIT);
659 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
660 updateLocal(bv, true);
664 bv->owner()->message(_("Invalid action in math mode!"));
668 case LFUN_MATH_HALIGN:
669 case LFUN_MATH_VALIGN:
670 case LFUN_MATH_ROW_INSERT:
671 case LFUN_MATH_ROW_DELETE:
672 case LFUN_MATH_COLUMN_INSERT:
673 case LFUN_MATH_COLUMN_DELETE:
675 MathInset::idx_type idx = 0;
676 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
678 mathcursor->popToEnclosingGrid();
679 bv->lockedInsetStoreUndo(Undo::EDIT);
680 char align = arg.size() ? arg[0] : 'c';
682 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
683 case LFUN_MATH_VALIGN: p->valign(align); break;
684 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
685 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
686 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
687 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
690 updateLocal(bv, true);
695 case LFUN_EXEC_COMMAND:
696 result = UNDISPATCHED;
699 case LFUN_BREAKPARAGRAPH:
700 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
701 //lyxerr << "LFUN ignored\n";
705 // interpret this as if a backslash was typed
706 bv->lockedInsetStoreUndo(Undo::EDIT);
707 mathcursor->interpret('\\');
708 updateLocal(bv, true);
712 case LFUN_INSERT_MATH:
713 case LFUN_SELFINSERT:
715 bv->lockedInsetStoreUndo(Undo::EDIT);
717 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
719 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
720 updateLocal(bv, true);
724 case LFUN_MATH_PANEL:
725 result = UNDISPATCHED;
729 if (mathcursor->selection())
730 mathcursor->selClear();
732 result = UNDISPATCHED;
735 case LFUN_INSET_TOGGLE:
736 mathcursor->insetToggle();
737 updateLocal(bv, true);
740 case LFUN_REF_INSERT:
741 //if (argument.empty()) {
742 // InsetCommandParams p("ref");
743 // owner_->getDialogs()->createRef(p.getAsString());
745 // InsetCommandParams p;
746 // p.setFromString(argument);
748 // InsetRef * inset = new InsetRef(p, *buffer_);
749 // if (!insertInset(inset))
752 // updateInset(inset, true);
756 InsetCommandParams p("ref");
757 bv->owner()->getDialogs()->createRef(p.getAsString());
759 //mathcursor->handleNest(new InsetRef2);
760 //mathcursor->insert(arg);
761 mathcursor->insert(MathAtom(new RefInset(arg)));
763 updateLocal(bv, true);
767 result = UNDISPATCHED;
770 mathcursor->normalize();
773 lyx::Assert(mathcursor);
775 if (mathcursor->selection() || was_selection)
776 toggleInsetSelection(bv);
778 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
779 result == UNDISPATCHED) {
784 releaseMathCursor(bv);
785 bv->unlockInset(this);
788 return result; // original version
792 void InsetFormulaBase::revealCodes(BufferView * bv) const
796 bv->owner()->message(mathcursor->info());
799 // write something to the minibuffer
800 // translate to latex
801 mathcursor->markInsert();
804 string str = os.str();
805 mathcursor->markErase();
806 string::size_type pos = 0;
808 for (string::iterator it = str.begin(); it != str.end(); ++it) {
811 else if (*it == '\0') {
813 pos = it - str.begin();
819 res = res.substr(pos - 30);
821 res = res.substr(0, 60);
822 bv->owner()->message(res);
827 Inset::Code InsetFormulaBase::lyxCode() const
829 return Inset::MATH_CODE;
833 int InsetFormulaBase::ylow() const
835 return yo_ - ascent(view_, font_);
839 int InsetFormulaBase::yhigh() const
841 return yo_ + descent(view_, font_);
845 int InsetFormulaBase::xlow() const
851 int InsetFormulaBase::xhigh() const
853 return xo_ + width(view_, font_);
857 /////////////////////////////////////////////////////////////////////
860 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
866 static InsetFormulaBase * lastformula = 0;
867 static MathIterator current = MathIterator(ibegin(par().nucleus()));
869 static string laststr;
871 if (lastformula != this || laststr != str) {
872 //lyxerr << "reset lastformula to " << this << "\n";
875 current = ibegin(par().nucleus());
877 mathed_parse_cell(ar, str);
881 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
883 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
884 if (it.cell().matchpart(ar, it.back().pos_)) {
885 bv->unlockInset(bv->theLockingInset());
886 if (!bv->lockInset(this)) {
887 lyxerr << "Cannot lock inset" << endl;
891 mathcursor = new MathCursor(this, true);
893 mathcursor->setSelection(it, ar.size());
896 updateLocal(bv, false);
901 //lyxerr << "not found!\n";
907 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
910 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
911 return searchForward(bv, what, a, b);
915 /////////////////////////////////////////////////////////////////////
918 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
920 if (!bv->available())
923 // use selection if available..
925 //if (action == LFUN_MATH_IMPORT_SELECTION)
929 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
932 InsetFormula * f = new InsetFormula(bv);
933 if (openNewInset(bv, f)) {
935 // don't do that also for LFUN_MATH_MODE unless you want end up with
936 // always changing to mathrm when opening an inlined inset
937 // -- I really hate "LyXfunc overloading"...
939 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
940 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
943 // create a macro if we see "\\newcommand" somewhere, and an ordinary
945 InsetFormulaBase * f;
946 if (sel.find("\\newcommand") == string::npos &&
947 sel.find("\\def") == string::npos)
948 f = new InsetFormula(sel);
950 f = new InsetFormulaMacro(sel);
951 bv->getLyXText()->cutSelection(bv);
954 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
958 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
960 mathDispatchCreation(bv, arg, true);
964 void mathDispatchMathMode(BufferView * bv, string const & arg)
966 mathDispatchCreation(bv, arg, false);
970 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
972 mathDispatchCreation(bv, arg, true);
976 void mathDispatchMathMacro(BufferView * bv, string const & arg)
978 if (!bv->available())
981 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
984 string const s1 = token(s, ' ', 1);
985 int const na = s1.empty() ? 0 : lyx::atoi(s1);
986 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
991 void mathDispatchMathDelim(BufferView * bv, string const & arg)
993 if (!bv->available())
995 InsetFormula * f = new InsetFormula(bv);
996 if (openNewInset(bv, f)) {
998 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1003 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1005 if (!bv->available())
1007 InsetFormula * f = new InsetFormula(bv);
1008 if (openNewInset(bv, f)) {
1009 f->mutate("simple");
1010 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1015 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1017 if (!bv->available())
1019 InsetFormula * f = new InsetFormula(bv);
1020 if (openNewInset(bv, f)) {
1021 f->mutate("simple");
1022 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1027 void mathDispatchGreek(BufferView * bv, string const & arg)
1029 if (!bv->available())
1031 InsetFormula * f = new InsetFormula(bv);
1032 if (openNewInset(bv, f)) {
1033 f->mutate("simple");
1034 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1040 void mathDispatch(BufferView *, kb_action, string const &)
1044 void mathDispatch(BufferView *, string const &)