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 "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 = 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);
208 bv->updateInset(this, false);
212 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
214 // calling metrics here destroys the cached xo,yo positions e.g. in
215 // MathParboxinset. And it would be too expensive anyway...
217 mathcursor->getPos(x, y);
220 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
224 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
226 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
227 if (isCursorVisible())
234 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
236 if (isCursorVisible())
240 getCursorPos(bv, x, y);
241 math_font_max_dim(font_, asc, des);
242 bv->showLockedInsetCursor(x, y, asc, des);
243 setCursorVisible(true);
244 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
248 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
250 if (!isCursorVisible())
252 bv->hideLockedInsetCursor();
253 setCursorVisible(false);
254 //lyxerr << "hideInsetCursor: \n";
258 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
263 math_font_max_dim(font_, asc, des);
264 getCursorPos(bv, x, y);
266 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
267 bv->fitLockedInsetCursor(x, y, asc, des);
271 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
274 bv->updateInset(this, false);
278 vector<string> const InsetFormulaBase::getLabelList() const
280 return vector<string>();
284 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
289 bv->updateInset(this, dirty);
293 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
294 int /*x*/, int /*y*/, mouse_button::state button)
299 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
302 bv->updateInset(this, false);
304 if (button == mouse_button::button3) {
305 // try to dispatch to enclosed insets first
306 if (mathcursor->dispatch("mouse-3-release"))
309 // launch math panel for right mouse button
310 bv->owner()->getDialogs()->showMathPanel();
314 if (button == mouse_button::button1) {
315 // try to dispatch to enclosed insets first
316 if (mathcursor->dispatch("mouse-1-release"))
319 // try to set the cursor
321 //mathcursor = new MathCursor(this, x == 0);
323 //mathcursor->setPos(x + xo_, y + yo_);
330 void InsetFormulaBase::insetButtonPress(BufferView * bv,
331 int x, int y, mouse_button::state button)
333 //lyxerr << "insetButtonPress: "
334 // << x << " " << y << " but: " << button << "\n";
335 //lyxerr << "formula: ";
338 releaseMathCursor(bv);
339 mathcursor = new MathCursor(this, x == 0);
341 if (button == mouse_button::button1) {
342 // just set the cursor here
343 lyxerr << "setting cursor\n";
347 mathcursor->selClear();
348 mathcursor->setPos(x + xo_, y + yo_);
350 if (mathcursor->dispatch("mouse-1-press")) {
356 if (button == mouse_button::button3) {
357 if (mathcursor->dispatch("mouse-3-press")) {
362 bv->updateInset(this, false);
366 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
367 int x, int y, mouse_button::state button)
372 if (button == mouse_button::button1)
373 if (mathcursor->dispatch("mouse-1-motion"))
376 if (button == mouse_button::button3)
377 if (mathcursor->dispatch("mouse-3-motion"))
380 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
381 //lyxerr << "insetMotionNotify: ignored\n";
387 if (!mathcursor->selection())
388 mathcursor->selStart();
390 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
391 // << ' ' << button << "\n";
393 mathcursor->setPos(x + xo_, y + yo_);
395 bv->updateInset(this, false);
399 UpdatableInset::RESULT
400 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
403 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
404 // << " arg: '" << arg
405 // << "' cursor: " << mathcursor << "\n";
409 string argument = arg;
411 RESULT result = DISPATCHED;
413 bool was_macro = mathcursor->inMacroMode();
414 bool was_selection = mathcursor->selection();
418 mathcursor->normalize();
423 case LFUN_WORDRIGHTSEL:
425 sel = true; // fall through...
427 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
428 //lyxerr << "calling scroll 20\n";
430 updateLocal(bv, false);
431 // write something to the minibuffer
432 //bv->owner()->message(mathcursor->info());
435 case LFUN_WORDLEFTSEL:
437 sel = true; // fall through
439 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
440 updateLocal(bv, false);
444 sel = true; // fall through
446 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
447 updateLocal(bv, false);
451 sel = true; // fall through
453 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
454 updateLocal(bv, false);
458 sel = true; // fall through
460 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
461 updateLocal(bv, false);
465 sel = true; // fall through
467 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
468 updateLocal(bv, false);
473 result = FINISHED_UP;
474 updateLocal(bv, false);
479 result = FINISHED_DOWN;
480 updateLocal(bv, false);
483 case LFUN_DELETE_LINE_FORWARD:
484 bv->lockedInsetStoreUndo(Undo::DELETE);
485 mathcursor->delLine();
486 updateLocal(bv, true);
490 mathcursor->idxNext();
491 updateLocal(bv, false);
495 mathcursor->idxPrev();
496 updateLocal(bv, false);
500 bv->lockedInsetStoreUndo(Undo::EDIT);
501 mathcursor->splitCell();
502 updateLocal(bv, true);
505 case LFUN_DELETE_WORD_BACKWARD:
507 bv->lockedInsetStoreUndo(Undo::DELETE);
508 mathcursor->backspace();
509 updateLocal(bv, true);
512 case LFUN_DELETE_WORD_FORWARD:
514 bv->lockedInsetStoreUndo(Undo::DELETE);
516 bv->updateInset(this, true);
520 // sprintf(dispatch_buffer, "%d %d",);
521 // dispatch_result = dispatch_buffer;
524 lyxerr << "LFUN_SETXY broken!\n";
527 istringstream is(arg.c_str());
529 mathcursor->setPos(x, y);
530 updateLocal(bv, false);
536 mathcursor->macroModeClose();
537 bv->lockedInsetStoreUndo(Undo::EDIT);
538 mathcursor->selPaste();
539 updateLocal(bv, true);
543 bv->lockedInsetStoreUndo(Undo::DELETE);
544 mathcursor->selCut();
545 updateLocal(bv, true);
549 mathcursor->selCopy();
553 // Special casing for superscript in case of LyX handling
555 case LFUN_CIRCUMFLEX:
557 // do superscript if LyX handles
559 bv->lockedInsetStoreUndo(Undo::EDIT);
560 mathcursor->script(true);
561 updateLocal(bv, true);
578 case LFUN_HUNG_UMLAUT:
582 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, "lyxgreek"); break;
583 case LFUN_BOLD: handleFont(bv, arg, "textbf"); break;
584 case LFUN_SANS: handleFont(bv, arg, "textsf"); break;
585 case LFUN_EMPH: handleFont(bv, arg, "mathcal"); break;
586 case LFUN_ROMAN: handleFont(bv, arg, "mathrm"); break;
587 case LFUN_CODE: handleFont(bv, arg, "texttt"); break;
588 case LFUN_FRAK: handleFont(bv, arg, "mathfrak"); break;
589 case LFUN_ITAL: handleFont(bv, arg, "mathit"); break;
590 case LFUN_NOUN: handleFont(bv, arg, "mathbb"); break;
591 case LFUN_DEFAULT: handleFont(bv, arg, "textnormal"); break;
592 case LFUN_FREE: handleFont(bv, arg, "textrm"); break;
595 handleFont(bv, arg, "lyxgreek1");
597 mathcursor->interpret(arg);
601 if (mathcursor->currentMode()) {
602 handleFont(bv, arg, "textrm");
604 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
605 updateLocal(bv, true);
607 //bv->owner()->message(_("math text mode toggled"));
610 case LFUN_MATH_LIMITS:
611 bv->lockedInsetStoreUndo(Undo::EDIT);
612 if (mathcursor->toggleLimits())
613 updateLocal(bv, true);
619 bv->lockedInsetStoreUndo(Undo::EDIT);
620 mathcursor->setSize(arg);
621 updateLocal(bv, true);
626 case LFUN_INSERT_MATRIX:
628 bv->lockedInsetStoreUndo(Undo::EDIT);
629 mathcursor->interpret("matrix " + arg);
630 updateLocal(bv, true);
634 case LFUN_SUPERSCRIPT:
637 bv->lockedInsetStoreUndo(Undo::EDIT);
638 mathcursor->script(action == LFUN_SUPERSCRIPT);
639 updateLocal(bv, true);
643 case LFUN_MATH_DELIM:
645 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
647 string rs = split(arg, ls, ' ');
648 // Reasonable default values
654 bv->lockedInsetStoreUndo(Undo::EDIT);
655 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
656 updateLocal(bv, true);
660 case LFUN_PROTECTEDSPACE:
661 case LFUN_MATH_SPACE:
662 bv->lockedInsetStoreUndo(Undo::EDIT);
663 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
664 updateLocal(bv, true);
668 bv->owner()->message(_("Invalid action in math mode!"));
672 case LFUN_MATH_HALIGN:
673 case LFUN_MATH_VALIGN:
674 case LFUN_MATH_ROW_INSERT:
675 case LFUN_MATH_ROW_DELETE:
676 case LFUN_MATH_COLUMN_INSERT:
677 case LFUN_MATH_COLUMN_DELETE:
679 MathInset::idx_type idx = 0;
680 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
682 mathcursor->popToEnclosingGrid();
683 bv->lockedInsetStoreUndo(Undo::EDIT);
684 char align = arg.size() ? arg[0] : 'c';
686 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
687 case LFUN_MATH_VALIGN: p->valign(align); break;
688 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
689 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
690 case LFUN_MATH_COLUMN_INSERT: p->addFancyCol(p->col(idx)); break;
691 case LFUN_MATH_COLUMN_DELETE: p->delFancyCol(p->col(idx)); break;
694 updateLocal(bv, true);
699 case LFUN_EXEC_COMMAND:
700 result = UNDISPATCHED;
704 // interpret this as if a backslash was typed
705 bv->lockedInsetStoreUndo(Undo::EDIT);
706 mathcursor->interpret('\\');
707 updateLocal(bv, true);
710 case LFUN_BREAKPARAGRAPH:
711 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
712 case LFUN_BREAKPARAGRAPH_SKIP:
717 case LFUN_INSERT_MATH:
718 case LFUN_SELFINSERT:
719 if (!argument.empty()) {
720 bv->lockedInsetStoreUndo(Undo::EDIT);
721 if (argument.size() == 1)
722 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
724 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
725 updateLocal(bv, true);
729 case LFUN_MATH_PANEL:
730 result = UNDISPATCHED;
734 if (mathcursor->selection())
735 mathcursor->selClear();
737 result = UNDISPATCHED;
740 case LFUN_INSET_TOGGLE:
741 mathcursor->insetToggle();
742 updateLocal(bv, true);
745 case LFUN_REF_INSERT:
746 //if (argument.empty()) {
747 // InsetCommandParams p("ref");
748 // owner_->getDialogs()->createRef(p.getAsString());
750 // InsetCommandParams p;
751 // p.setFromString(argument);
753 // InsetRef * inset = new InsetRef(p, *buffer_);
754 // if (!insertInset(inset))
757 // updateInset(inset, true);
761 InsetCommandParams p("ref");
762 bv->owner()->getDialogs()->createRef(p.getAsString());
764 //mathcursor->handleNest(new InsetRef2);
765 //mathcursor->insert(arg);
766 mathcursor->insert(MathAtom(new RefInset(arg)));
768 updateLocal(bv, true);
772 result = UNDISPATCHED;
775 mathcursor->normalize();
778 lyx::Assert(mathcursor);
780 if (mathcursor->selection() || was_selection)
781 toggleInsetSelection(bv);
783 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
784 result == UNDISPATCHED) {
789 releaseMathCursor(bv);
790 bv->unlockInset(this);
793 return result; // original version
797 void InsetFormulaBase::revealCodes(BufferView * bv) const
801 bv->owner()->message(mathcursor->info());
804 // write something to the minibuffer
805 // translate to latex
806 mathcursor->markInsert();
809 string str = os.str();
810 mathcursor->markErase();
811 string::size_type pos = 0;
813 for (string::iterator it = str.begin(); it != str.end(); ++it) {
816 else if (*it == '\0') {
818 pos = it - str.begin();
824 res = res.substr(pos - 30);
826 res = res.substr(0, 60);
827 bv->owner()->message(res);
832 Inset::Code InsetFormulaBase::lyxCode() const
834 return Inset::MATH_CODE;
838 int InsetFormulaBase::ylow() const
840 return yo_ - ascent(view(), font_);
844 int InsetFormulaBase::yhigh() const
846 return yo_ + descent(view(), font_);
850 int InsetFormulaBase::xlow() const
856 int InsetFormulaBase::xhigh() const
858 return xo_ + width(view(), font_);
862 /////////////////////////////////////////////////////////////////////
865 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
871 static InsetFormulaBase * lastformula = 0;
872 static MathIterator current = MathIterator(ibegin(par().nucleus()));
874 static string laststr;
876 if (lastformula != this || laststr != str) {
877 //lyxerr << "reset lastformula to " << this << "\n";
880 current = ibegin(par().nucleus());
882 mathed_parse_cell(ar, str);
886 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
888 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
889 if (it.cell().matchpart(ar, it.back().pos_)) {
890 bv->unlockInset(bv->theLockingInset());
891 if (!bv->lockInset(this)) {
892 lyxerr << "Cannot lock inset" << endl;
896 mathcursor = new MathCursor(this, true);
898 mathcursor->setSelection(it, ar.size());
901 updateLocal(bv, false);
906 //lyxerr << "not found!\n";
912 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
915 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
916 return searchForward(bv, what, a, b);
920 /////////////////////////////////////////////////////////////////////
923 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
925 if (!bv->available())
928 // use selection if available..
930 //if (action == LFUN_MATH_IMPORT_SELECTION)
934 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
937 InsetFormula * f = new InsetFormula(bv);
938 if (openNewInset(bv, f)) {
940 // don't do that also for LFUN_MATH_MODE unless you want end up with
941 // always changing to mathrm when opening an inlined inset
942 // -- I really hate "LyXfunc overloading"...
944 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
945 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
948 // create a macro if we see "\\newcommand" somewhere, and an ordinary
950 InsetFormulaBase * f;
951 if (sel.find("\\newcommand") == string::npos &&
952 sel.find("\\def") == string::npos)
953 f = new InsetFormula(sel);
955 f = new InsetFormulaMacro(sel);
956 bv->getLyXText()->cutSelection(bv);
959 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
963 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
965 mathDispatchCreation(bv, arg, true);
969 void mathDispatchMathMode(BufferView * bv, string const & arg)
971 mathDispatchCreation(bv, arg, false);
975 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
977 mathDispatchCreation(bv, arg, true);
981 void mathDispatchMathMacro(BufferView * bv, string const & arg)
983 if (!bv->available())
986 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
989 string const s1 = token(s, ' ', 1);
990 int const na = s1.empty() ? 0 : lyx::atoi(s1);
991 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
996 void mathDispatchMathDelim(BufferView * bv, string const & arg)
998 if (!bv->available())
1000 InsetFormula * f = new InsetFormula(bv);
1001 if (openNewInset(bv, f)) {
1002 f->mutate("simple");
1003 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1008 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1010 if (!bv->available())
1012 InsetFormula * f = new InsetFormula(bv);
1013 if (openNewInset(bv, f)) {
1014 f->mutate("simple");
1015 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1020 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1022 if (!bv->available())
1024 InsetFormula * f = new InsetFormula(bv);
1025 if (openNewInset(bv, f)) {
1026 f->mutate("simple");
1027 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATH, arg);
1032 void mathDispatchGreek(BufferView * bv, string const & arg)
1034 if (!bv->available())
1036 InsetFormula * f = new InsetFormula(bv);
1037 if (openNewInset(bv, f)) {
1038 f->mutate("simple");
1039 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
1045 void mathDispatch(BufferView *, kb_action, string const &)
1049 void mathDispatch(BufferView *, string const &)