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 *, int & x, int & y) const
213 // calling metrics here destroys the cached xo,yo positions e.g. in
214 // MathParboxinset. And it would be too expensive anyway...
216 mathcursor->getPos(x, y);
219 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
226 if (isCursorVisible())
233 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
235 if (isCursorVisible())
239 getCursorPos(bv, x, y);
240 math_font_max_dim(font_, asc, des);
241 bv->showLockedInsetCursor(x, y, asc, des);
242 setCursorVisible(true);
243 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
247 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
249 if (!isCursorVisible())
251 bv->hideLockedInsetCursor();
252 setCursorVisible(false);
253 //lyxerr << "hideInsetCursor: \n";
257 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
262 math_font_max_dim(font_, asc, des);
263 getCursorPos(bv, x, y);
265 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
266 bv->fitLockedInsetCursor(x, y, asc, des);
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
273 bv->updateInset(this, false);
277 vector<string> const InsetFormulaBase::getLabelList() const
279 return vector<string>();
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
288 bv->updateInset(this, dirty);
292 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
293 int /*x*/, int /*y*/, mouse_button::state button)
298 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
301 bv->updateInset(this, false);
303 if (button == mouse_button::button3) {
304 // try to dispatch to enclosed insets first
305 if (mathcursor->dispatch("mouse-3-release"))
308 // launch math panel for right mouse button
309 bv->owner()->getDialogs()->showMathPanel();
313 if (button == mouse_button::button1) {
314 // try to dispatch to enclosed insets first
315 if (mathcursor->dispatch("mouse-1-release"))
318 // try to set the cursor
320 //mathcursor = new MathCursor(this, x == 0);
322 //mathcursor->setPos(x + xo_, y + yo_);
329 void InsetFormulaBase::insetButtonPress(BufferView * bv,
330 int x, int y, mouse_button::state button)
332 //lyxerr << "insetButtonPress: "
333 // << x << " " << y << " but: " << button << "\n";
334 //lyxerr << "formula: ";
337 releaseMathCursor(bv);
338 mathcursor = new MathCursor(this, x == 0);
340 if (button == mouse_button::button1) {
341 // just set the cursor here
342 lyxerr << "setting cursor\n";
346 mathcursor->selClear();
347 mathcursor->setPos(x + xo_, y + yo_);
349 if (mathcursor->dispatch("mouse-1-press")) {
355 if (button == mouse_button::button3) {
356 if (mathcursor->dispatch("mouse-3-press")) {
361 bv->updateInset(this, false);
365 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
366 int x, int y, mouse_button::state button)
371 if (button == mouse_button::button1)
372 if (mathcursor->dispatch("mouse-1-motion"))
375 if (button == mouse_button::button3)
376 if (mathcursor->dispatch("mouse-3-motion"))
379 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
380 //lyxerr << "insetMotionNotify: ignored\n";
386 if (!mathcursor->selection())
387 mathcursor->selStart();
389 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
390 // << ' ' << button << "\n";
392 mathcursor->setPos(x + xo_, y + yo_);
394 bv->updateInset(this, false);
398 UpdatableInset::RESULT
399 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
402 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
403 // << " arg: '" << arg
404 // << "' cursor: " << mathcursor << "\n";
408 string argument = arg;
410 RESULT result = DISPATCHED;
412 bool was_macro = mathcursor->inMacroMode();
413 bool was_selection = mathcursor->selection();
417 mathcursor->normalize();
422 case LFUN_WORDRIGHTSEL:
424 sel = true; // fall through...
426 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
427 //lyxerr << "calling scroll 20\n";
429 updateLocal(bv, false);
430 // write something to the minibuffer
431 //bv->owner()->message(mathcursor->info());
434 case LFUN_WORDLEFTSEL:
436 sel = true; // fall through
438 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
439 updateLocal(bv, false);
443 sel = true; // fall through
445 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
446 updateLocal(bv, false);
450 sel = true; // fall through
452 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
453 updateLocal(bv, false);
457 sel = true; // fall through
459 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
460 updateLocal(bv, false);
464 sel = true; // fall through
466 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
467 updateLocal(bv, false);
472 result = FINISHED_UP;
473 updateLocal(bv, false);
478 result = FINISHED_DOWN;
479 updateLocal(bv, false);
482 case LFUN_DELETE_LINE_FORWARD:
483 bv->lockedInsetStoreUndo(Undo::DELETE);
484 mathcursor->delLine();
485 updateLocal(bv, true);
489 mathcursor->idxNext();
490 updateLocal(bv, false);
494 mathcursor->idxPrev();
495 updateLocal(bv, false);
499 bv->lockedInsetStoreUndo(Undo::EDIT);
500 mathcursor->splitCell();
501 updateLocal(bv, true);
504 case LFUN_DELETE_WORD_BACKWARD:
506 bv->lockedInsetStoreUndo(Undo::DELETE);
507 mathcursor->backspace();
508 updateLocal(bv, true);
511 case LFUN_DELETE_WORD_FORWARD:
513 bv->lockedInsetStoreUndo(Undo::DELETE);
515 bv->updateInset(this, true);
519 // sprintf(dispatch_buffer, "%d %d",);
520 // dispatch_result = dispatch_buffer;
523 lyxerr << "LFUN_SETXY broken!\n";
526 istringstream is(arg.c_str());
528 mathcursor->setPos(x, y);
529 updateLocal(bv, false);
535 mathcursor->macroModeClose();
536 bv->lockedInsetStoreUndo(Undo::EDIT);
537 mathcursor->selPaste();
538 updateLocal(bv, true);
542 bv->lockedInsetStoreUndo(Undo::DELETE);
543 mathcursor->selCut();
544 updateLocal(bv, true);
548 mathcursor->selCopy();
552 // Special casing for superscript in case of LyX handling
554 case LFUN_CIRCUMFLEX:
556 // do superscript if LyX handles
558 bv->lockedInsetStoreUndo(Undo::EDIT);
559 mathcursor->script(true);
560 updateLocal(bv, true);
577 case LFUN_HUNG_UMLAUT:
581 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
582 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
583 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
584 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
585 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
586 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
587 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
588 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
589 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
590 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
591 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
594 handleFont(bv, arg, "lyxgreek1");
596 mathcursor->interpret(arg);
600 if (mathcursor->currentMode()) {
601 handleFont(bv, arg, "textrm");
603 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
604 updateLocal(bv, true);
606 //bv->owner()->message(_("math text mode toggled"));
609 case LFUN_MATH_LIMITS:
610 bv->lockedInsetStoreUndo(Undo::EDIT);
611 if (mathcursor->toggleLimits())
612 updateLocal(bv, true);
618 bv->lockedInsetStoreUndo(Undo::EDIT);
619 mathcursor->setSize(arg);
620 updateLocal(bv, true);
625 case LFUN_INSERT_MATRIX:
627 bv->lockedInsetStoreUndo(Undo::EDIT);
628 mathcursor->interpret("matrix " + arg);
629 updateLocal(bv, true);
633 case LFUN_SUPERSCRIPT:
636 bv->lockedInsetStoreUndo(Undo::EDIT);
637 mathcursor->script(action == LFUN_SUPERSCRIPT);
638 updateLocal(bv, true);
642 case LFUN_MATH_DELIM:
644 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
646 string rs = split(arg, ls, ' ');
647 // Reasonable default values
653 bv->lockedInsetStoreUndo(Undo::EDIT);
654 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
655 updateLocal(bv, true);
659 case LFUN_PROTECTEDSPACE:
660 case LFUN_MATH_SPACE:
661 bv->lockedInsetStoreUndo(Undo::EDIT);
662 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
663 updateLocal(bv, true);
667 bv->owner()->message(_("Invalid action in math mode!"));
671 case LFUN_MATH_HALIGN:
672 case LFUN_MATH_VALIGN:
673 case LFUN_MATH_ROW_INSERT:
674 case LFUN_MATH_ROW_DELETE:
675 case LFUN_MATH_COLUMN_INSERT:
676 case LFUN_MATH_COLUMN_DELETE:
678 MathInset::idx_type idx = 0;
679 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
681 mathcursor->popToEnclosingGrid();
682 bv->lockedInsetStoreUndo(Undo::EDIT);
683 char align = arg.size() ? arg[0] : 'c';
685 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
686 case LFUN_MATH_VALIGN: p->valign(align); break;
687 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
688 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
689 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
690 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
693 updateLocal(bv, true);
698 case LFUN_EXEC_COMMAND:
699 result = UNDISPATCHED;
703 // interpret this as if a backslash was typed
704 bv->lockedInsetStoreUndo(Undo::EDIT);
705 mathcursor->interpret('\\');
706 updateLocal(bv, true);
709 case LFUN_BREAKPARAGRAPH:
710 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
711 case LFUN_BREAKPARAGRAPH_SKIP:
716 case LFUN_INSERT_MATH:
717 case LFUN_SELFINSERT:
718 if (!argument.empty()) {
719 bv->lockedInsetStoreUndo(Undo::EDIT);
720 if (argument.size() == 1)
721 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
723 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
724 updateLocal(bv, true);
728 case LFUN_MATH_PANEL:
729 result = UNDISPATCHED;
733 if (mathcursor->selection())
734 mathcursor->selClear();
736 result = UNDISPATCHED;
739 case LFUN_INSET_TOGGLE:
740 mathcursor->insetToggle();
741 updateLocal(bv, true);
744 case LFUN_REF_INSERT:
745 //if (argument.empty()) {
746 // InsetCommandParams p("ref");
747 // owner_->getDialogs()->createRef(p.getAsString());
749 // InsetCommandParams p;
750 // p.setFromString(argument);
752 // InsetRef * inset = new InsetRef(p, *buffer_);
753 // if (!insertInset(inset))
756 // updateInset(inset, true);
760 InsetCommandParams p("ref");
761 bv->owner()->getDialogs()->createRef(p.getAsString());
763 //mathcursor->handleNest(new InsetRef2);
764 //mathcursor->insert(arg);
765 mathcursor->insert(MathAtom(new RefInset(arg)));
767 updateLocal(bv, true);
771 result = UNDISPATCHED;
774 mathcursor->normalize();
777 lyx::Assert(mathcursor);
779 if (mathcursor->selection() || was_selection)
780 toggleInsetSelection(bv);
782 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
783 result == UNDISPATCHED) {
788 releaseMathCursor(bv);
789 bv->unlockInset(this);
792 return result; // original version
796 void InsetFormulaBase::revealCodes(BufferView * bv) const
800 bv->owner()->message(mathcursor->info());
803 // write something to the minibuffer
804 // translate to latex
805 mathcursor->markInsert();
808 string str = os.str();
809 mathcursor->markErase();
810 string::size_type pos = 0;
812 for (string::iterator it = str.begin(); it != str.end(); ++it) {
815 else if (*it == '\0') {
817 pos = it - str.begin();
823 res = res.substr(pos - 30);
825 res = res.substr(0, 60);
826 bv->owner()->message(res);
831 Inset::Code InsetFormulaBase::lyxCode() const
833 return Inset::MATH_CODE;
837 int InsetFormulaBase::ylow() const
839 return yo_ - ascent(view_, font_);
843 int InsetFormulaBase::yhigh() const
845 return yo_ + descent(view_, font_);
849 int InsetFormulaBase::xlow() const
855 int InsetFormulaBase::xhigh() const
857 return xo_ + width(view_, font_);
861 /////////////////////////////////////////////////////////////////////
864 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
870 static InsetFormulaBase * lastformula = 0;
871 static MathIterator current = MathIterator(ibegin(par().nucleus()));
873 static string laststr;
875 if (lastformula != this || laststr != str) {
876 //lyxerr << "reset lastformula to " << this << "\n";
879 current = ibegin(par().nucleus());
881 mathed_parse_cell(ar, str);
885 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
887 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
888 if (it.cell().matchpart(ar, it.back().pos_)) {
889 bv->unlockInset(bv->theLockingInset());
890 if (!bv->lockInset(this)) {
891 lyxerr << "Cannot lock inset" << endl;
895 mathcursor = new MathCursor(this, true);
897 mathcursor->setSelection(it, ar.size());
900 updateLocal(bv, false);
905 //lyxerr << "not found!\n";
911 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
914 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
915 return searchForward(bv, what, a, b);
919 /////////////////////////////////////////////////////////////////////
922 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
924 if (!bv->available())
927 // use selection if available..
929 //if (action == LFUN_MATH_IMPORT_SELECTION)
933 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
936 InsetFormula * f = new InsetFormula(bv);
937 if (openNewInset(bv, f)) {
939 // don't do that also for LFUN_MATH_MODE unless you want end up with
940 // always changing to mathrm when opening an inlined inset
941 // -- I really hate "LyXfunc overloading"...
943 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
944 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
947 // create a macro if we see "\\newcommand" somewhere, and an ordinary
949 InsetFormulaBase * f;
950 if (sel.find("\\newcommand") == string::npos &&
951 sel.find("\\def") == string::npos)
952 f = new InsetFormula(sel);
954 f = new InsetFormulaMacro(sel);
955 bv->getLyXText()->cutSelection(bv);
958 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
962 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
964 mathDispatchCreation(bv, arg, true);
968 void mathDispatchMathMode(BufferView * bv, string const & arg)
970 mathDispatchCreation(bv, arg, false);
974 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
976 mathDispatchCreation(bv, arg, true);
980 void mathDispatchMathMacro(BufferView * bv, string const & arg)
982 if (!bv->available())
985 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
988 string const s1 = token(s, ' ', 1);
989 int const na = s1.empty() ? 0 : lyx::atoi(s1);
990 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
995 void mathDispatchMathDelim(BufferView * bv, string const & arg)
997 if (!bv->available())
999 InsetFormula * f = new InsetFormula(bv);
1000 if (openNewInset(bv, f)) {
1001 f->mutate("simple");
1002 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1007 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1009 if (!bv->available())
1011 InsetFormula * f = new InsetFormula(bv);
1012 if (openNewInset(bv, f)) {
1013 f->mutate("simple");
1014 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1019 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1021 if (!bv->available())
1023 InsetFormula * f = new InsetFormula(bv);
1024 if (openNewInset(bv, f)) {
1025 f->mutate("simple");
1026 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1031 void mathDispatchGreek(BufferView * bv, string const & arg)
1033 if (!bv->available())
1035 InsetFormula * f = new InsetFormula(bv);
1036 if (openNewInset(bv, f)) {
1037 f->mutate("simple");
1038 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1044 void mathDispatch(BufferView *, kb_action, string const &)
1048 void mathDispatch(BufferView *, string const &)