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 "funcrequest.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "math_support.h"
36 #include "math_metricsinfo.h"
37 #include "support/lstrings.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/font_metrics.h"
40 #include "frontends/mouse_state.h"
42 #include "math_arrayinset.h"
43 #include "math_charinset.h"
44 #include "math_deliminset.h"
45 #include "math_cursor.h"
46 #include "math_factory.h"
47 #include "math_fontinset.h"
48 #include "math_hullinset.h"
49 #include "math_iterator.h"
50 #include "math_macrotable.h"
51 #include "math_parser.h"
53 #include "math_spaceinset.h"
54 #include "undo_funcs.h"
55 #include "textpainter.h"
56 #include "frontends/Dialogs.h"
58 #include "insets/insetcommandparams.h"
59 #include "ref_inset.h"
66 MathCursor * mathcursor = 0;
77 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
79 if (!bv->insertInset(new_inset)) {
83 new_inset->edit(bv, true);
92 InsetFormulaBase::InsetFormulaBase()
93 : font_(), xo_(0), yo_(0)
95 // This is needed as long the math parser is not re-entrant
97 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
98 //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
99 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
100 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
108 // translate to latex
110 latex(NULL, os, false, false);
111 string str = os.str();
114 LyXText * lt = view_->getLyXText();
115 string::const_iterator cit = str.begin();
116 string::const_iterator end = str.end();
117 for (; cit != end; ++cit)
118 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
121 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
126 void InsetFormulaBase::handleFont
127 (BufferView * bv, string const & arg, string const & font)
129 bv->lockedInsetStoreUndo(Undo::EDIT);
130 bool sel = mathcursor->selection();
132 updateLocal(bv, true);
133 mathcursor->handleNest(createMathInset(font));
134 mathcursor->insert(arg);
136 updateLocal(bv, false);
140 // Check if uses AMS macros
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
145 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
152 void InsetFormulaBase::metrics(BufferView * bv) const
155 view_ = bv->owner()->view();
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;
174 releaseMathCursor(bv);
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 << "Cannot lock math inset in edit call!\n";
190 releaseMathCursor(bv);
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);
204 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, FuncRequest const & ev)
401 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
402 // << " arg: '" << arg
403 // << "' cursor: " << mathcursor << "\n";
408 string argument = ev.argument;
409 RESULT result = DISPATCHED;
411 bool was_macro = mathcursor->inMacroMode();
412 bool was_selection = mathcursor->selection();
416 mathcursor->normalize();
421 case LFUN_WORDRIGHTSEL:
423 sel = true; // fall through...
425 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
426 //lyxerr << "calling scroll 20\n";
428 updateLocal(bv, false);
429 // write something to the minibuffer
430 //bv->owner()->message(mathcursor->info());
433 case LFUN_WORDLEFTSEL:
435 sel = true; // fall through
437 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
438 updateLocal(bv, false);
442 sel = true; // fall through
444 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
445 updateLocal(bv, false);
449 sel = true; // fall through
451 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
452 updateLocal(bv, false);
456 sel = true; // fall through
458 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
459 updateLocal(bv, false);
463 sel = true; // fall through
465 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
466 updateLocal(bv, false);
471 result = FINISHED_UP;
472 updateLocal(bv, false);
477 result = FINISHED_DOWN;
478 updateLocal(bv, false);
481 case LFUN_DELETE_LINE_FORWARD:
482 bv->lockedInsetStoreUndo(Undo::DELETE);
483 mathcursor->delLine();
484 updateLocal(bv, true);
488 mathcursor->idxNext();
489 updateLocal(bv, false);
493 mathcursor->idxPrev();
494 updateLocal(bv, false);
498 bv->lockedInsetStoreUndo(Undo::EDIT);
499 mathcursor->splitCell();
500 updateLocal(bv, true);
503 case LFUN_DELETE_WORD_BACKWARD:
505 bv->lockedInsetStoreUndo(Undo::DELETE);
506 mathcursor->backspace();
507 updateLocal(bv, true);
510 case LFUN_DELETE_WORD_FORWARD:
512 bv->lockedInsetStoreUndo(Undo::DELETE);
514 bv->updateInset(this, true);
518 // sprintf(dispatch_buffer, "%d %d",);
519 // dispatch_result = dispatch_buffer;
522 lyxerr << "LFUN_SETXY broken!\n";
525 istringstream is(ev.argument.c_str());
527 mathcursor->setPos(x, y);
528 updateLocal(bv, false);
534 mathcursor->macroModeClose();
535 bv->lockedInsetStoreUndo(Undo::EDIT);
536 mathcursor->selPaste();
537 updateLocal(bv, true);
541 bv->lockedInsetStoreUndo(Undo::DELETE);
542 mathcursor->selCut();
543 updateLocal(bv, true);
547 mathcursor->selCopy();
551 // Special casing for superscript in case of LyX handling
553 case LFUN_CIRCUMFLEX:
554 if (ev.argument.empty()) {
555 // do superscript if LyX handles
557 bv->lockedInsetStoreUndo(Undo::EDIT);
558 mathcursor->script(true);
559 updateLocal(bv, true);
576 case LFUN_HUNG_UMLAUT:
580 case LFUN_GREEK_TOGGLE: handleFont(bv, ev.argument, "lyxgreek"); break;
581 case LFUN_BOLD: handleFont(bv, ev.argument, "textbf"); break;
582 case LFUN_SANS: handleFont(bv, ev.argument, "textsf"); break;
583 case LFUN_EMPH: handleFont(bv, ev.argument, "mathcal"); break;
584 case LFUN_ROMAN: handleFont(bv, ev.argument, "mathrm"); break;
585 case LFUN_CODE: handleFont(bv, ev.argument, "texttt"); break;
586 case LFUN_FRAK: handleFont(bv, ev.argument, "mathfrak"); break;
587 case LFUN_ITAL: handleFont(bv, ev.argument, "mathit"); break;
588 case LFUN_NOUN: handleFont(bv, ev.argument, "mathbb"); break;
589 case LFUN_DEFAULT: handleFont(bv, ev.argument, "textnormal"); break;
590 case LFUN_FREE: handleFont(bv, ev.argument, "textrm"); break;
593 handleFont(bv, ev.argument, "lyxgreek1");
594 if (ev.argument.size())
595 mathcursor->interpret(ev.argument);
599 if (mathcursor->currentMode()) {
600 handleFont(bv, ev.argument, "textrm");
602 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
603 updateLocal(bv, true);
605 //bv->owner()->message(_("math text mode toggled"));
608 case LFUN_MATH_LIMITS:
609 bv->lockedInsetStoreUndo(Undo::EDIT);
610 if (mathcursor->toggleLimits())
611 updateLocal(bv, true);
617 bv->lockedInsetStoreUndo(Undo::EDIT);
618 mathcursor->setSize(arg);
619 updateLocal(bv, true);
624 case LFUN_INSERT_MATRIX:
625 if (!ev.argument.empty()) {
626 bv->lockedInsetStoreUndo(Undo::EDIT);
627 mathcursor->interpret("matrix " + ev.argument);
628 updateLocal(bv, true);
632 case LFUN_SUPERSCRIPT:
635 bv->lockedInsetStoreUndo(Undo::EDIT);
636 mathcursor->script(ev.action == LFUN_SUPERSCRIPT);
637 updateLocal(bv, true);
641 case LFUN_MATH_DELIM:
643 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
645 string rs = split(ev.argument, ls, ' ');
646 // Reasonable default values
652 bv->lockedInsetStoreUndo(Undo::EDIT);
653 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
654 updateLocal(bv, true);
658 case LFUN_PROTECTEDSPACE:
659 case LFUN_MATH_SPACE:
660 bv->lockedInsetStoreUndo(Undo::EDIT);
661 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
662 updateLocal(bv, true);
666 bv->owner()->message(_("Invalid action in math mode!"));
670 case LFUN_MATH_HALIGN:
671 case LFUN_MATH_VALIGN:
672 case LFUN_MATH_ROW_INSERT:
673 case LFUN_MATH_ROW_DELETE:
674 case LFUN_MATH_COLUMN_INSERT:
675 case LFUN_MATH_COLUMN_DELETE:
677 MathInset::idx_type idx = 0;
678 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
680 mathcursor->popToEnclosingGrid();
681 bv->lockedInsetStoreUndo(Undo::EDIT);
682 char align = ev.argument.size() ? ev.argument[0] : 'c';
684 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
685 case LFUN_MATH_VALIGN: p->valign(align); break;
686 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
687 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
688 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
689 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
692 updateLocal(bv, true);
697 case LFUN_EXEC_COMMAND:
698 result = UNDISPATCHED;
702 // interpret this as if a backslash was typed
703 bv->lockedInsetStoreUndo(Undo::EDIT);
704 mathcursor->interpret('\\');
705 updateLocal(bv, true);
708 case LFUN_BREAKPARAGRAPH:
709 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
710 case LFUN_BREAKPARAGRAPH_SKIP:
715 case LFUN_INSERT_MATH:
716 case LFUN_SELFINSERT:
717 if (!argument.empty()) {
718 bv->lockedInsetStoreUndo(Undo::EDIT);
719 if (argument.size() == 1)
720 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
722 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
723 updateLocal(bv, true);
727 case LFUN_MATH_PANEL:
728 result = UNDISPATCHED;
732 if (mathcursor->selection())
733 mathcursor->selClear();
735 result = UNDISPATCHED;
738 case LFUN_INSET_TOGGLE:
739 mathcursor->insetToggle();
740 updateLocal(bv, true);
743 case LFUN_REF_INSERT:
744 //if (argument.empty()) {
745 // InsetCommandParams p("ref");
746 // owner_->getDialogs()->createRef(p.getAsString());
748 // InsetCommandParams p;
749 // p.setFromString(argument);
751 // InsetRef * inset = new InsetRef(p, *buffer_);
752 // if (!insertInset(inset))
755 // updateInset(inset, true);
758 if (ev.argument.empty()) {
759 InsetCommandParams p("ref");
760 bv->owner()->getDialogs()->createRef(p.getAsString());
762 //mathcursor->handleNest(new InsetRef2);
763 //mathcursor->insert(arg);
764 mathcursor->insert(MathAtom(new RefInset(ev.argument)));
766 updateLocal(bv, true);
770 result = UNDISPATCHED;
773 mathcursor->normalize();
776 lyx::Assert(mathcursor);
778 if (mathcursor->selection() || was_selection)
779 toggleInsetSelection(bv);
781 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
782 result == UNDISPATCHED) {
787 releaseMathCursor(bv);
788 bv->unlockInset(this);
791 return result; // original version
795 void InsetFormulaBase::revealCodes(BufferView * bv) const
799 bv->owner()->message(mathcursor->info());
802 // write something to the minibuffer
803 // translate to latex
804 mathcursor->markInsert();
807 string str = os.str();
808 mathcursor->markErase();
809 string::size_type pos = 0;
811 for (string::iterator it = str.begin(); it != str.end(); ++it) {
814 else if (*it == '\0') {
816 pos = it - str.begin();
822 res = res.substr(pos - 30);
824 res = res.substr(0, 60);
825 bv->owner()->message(res);
830 Inset::Code InsetFormulaBase::lyxCode() const
832 return Inset::MATH_CODE;
836 int InsetFormulaBase::ylow() const
838 return yo_ - ascent(view(), font_);
842 int InsetFormulaBase::yhigh() const
844 return yo_ + descent(view(), font_);
848 int InsetFormulaBase::xlow() const
854 int InsetFormulaBase::xhigh() const
856 return xo_ + width(view(), font_);
860 /////////////////////////////////////////////////////////////////////
863 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
869 static InsetFormulaBase * lastformula = 0;
870 static MathIterator current = MathIterator(ibegin(par().nucleus()));
872 static string laststr;
874 if (lastformula != this || laststr != str) {
875 //lyxerr << "reset lastformula to " << this << "\n";
878 current = ibegin(par().nucleus());
880 mathed_parse_cell(ar, str);
884 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
886 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
887 if (it.cell().matchpart(ar, it.back().pos_)) {
888 bv->unlockInset(bv->theLockingInset());
889 if (!bv->lockInset(this)) {
890 lyxerr << "Cannot lock inset" << endl;
894 mathcursor = new MathCursor(this, true);
896 mathcursor->setSelection(it, ar.size());
899 updateLocal(bv, false);
904 //lyxerr << "not found!\n";
910 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
913 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
914 return searchForward(bv, what, a, b);
918 /////////////////////////////////////////////////////////////////////
921 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
923 if (!bv->available())
926 // use selection if available..
928 //if (action == LFUN_MATH_IMPORT_SELECTION)
932 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
935 InsetFormula * f = new InsetFormula(bv);
936 if (openNewInset(bv, f)) {
938 // don't do that also for LFUN_MATH_MODE unless you want end up with
939 // always changing to mathrm when opening an inlined inset
940 // -- I really hate "LyXfunc overloading"...
942 f->localDispatch(bv, FuncRequest(LFUN_MATH_DISPLAY));
943 f->localDispatch(bv, FuncRequest(LFUN_INSERT_MATH, arg));
946 // create a macro if we see "\\newcommand" somewhere, and an ordinary
948 InsetFormulaBase * f;
949 if (sel.find("\\newcommand") == string::npos &&
950 sel.find("\\def") == string::npos)
951 f = new InsetFormula(sel);
953 f = new InsetFormulaMacro(sel);
954 bv->getLyXText()->cutSelection(bv);
957 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
961 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
963 mathDispatchCreation(bv, arg, true);
967 void mathDispatchMathMode(BufferView * bv, string const & arg)
969 mathDispatchCreation(bv, arg, false);
973 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
975 mathDispatchCreation(bv, arg, true);
979 void mathDispatchMathMacro(BufferView * bv, string const & arg)
981 if (!bv->available())
984 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
987 string const s1 = token(s, ' ', 1);
988 int const na = s1.empty() ? 0 : lyx::atoi(s1);
989 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
994 void mathDispatchMathDelim(BufferView * bv, string const & arg)
996 if (!bv->available())
998 InsetFormula * f = new InsetFormula(bv);
999 if (openNewInset(bv, f)) {
1000 f->mutate("simple");
1001 bv->theLockingInset()->localDispatch(bv, FuncRequest(LFUN_MATH_DELIM, arg));
1006 void mathDispatchInsertMatrix(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, FuncRequest(LFUN_INSERT_MATRIX, arg));
1018 void mathDispatchInsertMath(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, FuncRequest(LFUN_INSERT_MATH, arg));
1030 void mathDispatchGreek(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, FuncRequest(LFUN_GREEK, arg));
1043 void mathDispatch(BufferView *, kb_action, string const &)
1047 void mathDispatch(BufferView *, string const &)