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"
32 #include "LaTeXFeatures.h"
34 #include "math_support.h"
35 #include "math_metricsinfo.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"
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()->dispatch(LFUN_ESCAPE);
126 void InsetFormulaBase::handleFont
127 (BufferView * bv, string const & arg, string const & font)
129 // this whole function is a hack and won't work for incremental font
131 bv->lockedInsetStoreUndo(Undo::EDIT);
132 if (mathcursor->par()->name() == font) {
133 mathcursor->handleFont(font);
134 updateLocal(bv, true);
136 bool sel = mathcursor->selection();
138 updateLocal(bv, true);
139 mathcursor->handleNest(createMathInset(font));
140 mathcursor->insert(arg);
142 updateLocal(bv, false);
147 // Check if uses AMS macros
148 void InsetFormulaBase::validate(LaTeXFeatures &) const
152 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
159 void InsetFormulaBase::metrics(BufferView * bv) const
162 view_ = bv->owner()->view();
164 mi.base.style = LM_ST_TEXT;
165 mi.base.font = font_;
166 mi.base.font.setColor(LColor::math);
171 string const InsetFormulaBase::editMessage() const
173 return _("Math editor mode");
177 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
179 if (!bv->lockInset(this))
180 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
181 releaseMathCursor(bv);
182 mathcursor = new MathCursor(this, true);
184 mathcursor->setPos(x + xo_, y + yo_);
185 // if that is removed, we won't get the magenta box when entering an
186 // inset for the first time
187 bv->updateInset(this, false);
191 void InsetFormulaBase::edit(BufferView * bv, bool front)
193 if (!bv->lockInset(this))
194 lyxerr << "Cannot lock math inset in edit call!\n";
195 releaseMathCursor(bv);
196 mathcursor = new MathCursor(this, front);
198 bv->updateInset(this, false);
202 void InsetFormulaBase::insetUnlock(BufferView * bv)
205 if (mathcursor->inMacroMode()) {
206 mathcursor->macroModeClose();
207 updateLocal(bv, true);
209 releaseMathCursor(bv);
212 bv->updateInset(this, false);
216 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
218 // calling metrics here destroys the cached xo,yo positions e.g. in
219 // MathParboxinset. And it would be too expensive anyway...
222 lyxerr << "getCursorPos - should not happen";
226 mathcursor->getPos(x, y);
227 x = mathcursor->targetX();
230 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
234 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
236 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
237 if (isCursorVisible())
244 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
246 if (isCursorVisible())
250 mathcursor->getPos(x, y);
251 math_font_max_dim(font_, asc, des);
252 bv->showLockedInsetCursor(x, y - yo_, asc, des);
253 setCursorVisible(true);
254 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
258 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
260 if (!isCursorVisible())
262 bv->hideLockedInsetCursor();
263 setCursorVisible(false);
264 //lyxerr << "hideInsetCursor: \n";
268 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
273 math_font_max_dim(font_, asc, des);
274 getCursorPos(bv, x, y);
276 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
277 bv->fitLockedInsetCursor(x, y, asc, des);
281 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
284 bv->updateInset(this, false);
288 vector<string> const InsetFormulaBase::getLabelList() const
290 return vector<string>();
294 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
296 bv->updateInset(this, dirty);
300 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
305 BufferView * bv = cmd.view();
308 bv->updateInset(this, true);
309 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << "\n";
311 if (cmd.button() == mouse_button::button3) {
312 // try to dispatch to enclosed insets first
313 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
314 // launch math panel for right mouse button
315 bv->owner()->getDialogs().showMathPanel();
320 if (cmd.button() == mouse_button::button2) {
321 mathcursor->selClear();
322 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
323 mathcursor->insert(asArray(bv->getClipboard()));
324 bv->updateInset(this, true);
328 if (cmd.button() == mouse_button::button1) {
329 // try to dispatch to enclosed insets first
330 mathcursor->dispatch(cmd);
331 cmd.view()->stuffClipboard(mathcursor->grabSelection());
332 // try to set the cursor
334 //mathcursor = new MathCursor(this, x == 0);
336 //mathcursor->setPos(x + xo_, y + yo_);
344 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
346 BufferView * bv = cmd.view();
347 releaseMathCursor(bv);
348 mathcursor = new MathCursor(this, cmd.x == 0);
349 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << "\n";
351 if (cmd.button() == mouse_button::button3) {
352 mathcursor->dispatch(cmd);
356 if (cmd.button() == mouse_button::button1) {
357 // just set the cursor here
358 //lyxerr << "setting cursor\n";
362 mathcursor->selClear();
363 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
364 mathcursor->dispatch(cmd);
368 bv->updateInset(this, false);
373 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
378 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
381 // only select with button 1
382 if (cmd.button() != mouse_button::button1)
385 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
391 if (!mathcursor->selection())
392 mathcursor->selStart();
394 BufferView * bv = cmd.view();
396 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
398 bv->updateInset(this, false);
403 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
405 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
406 // << " arg: '" << cmd.argument
407 // << " x: '" << cmd.x
408 // << " y: '" << cmd.y
409 // << "' button: " << cmd.button() << "\n";
411 switch (cmd.action) {
412 case LFUN_MOUSE_PRESS:
413 return lfunMousePress(cmd);
414 case LFUN_MOUSE_MOTION:
415 return lfunMouseMotion(cmd);
416 case LFUN_MOUSE_RELEASE:
417 return lfunMouseRelease(cmd);
418 case LFUN_MOUSE_DOUBLE:
419 //lyxerr << "Mouse double\n";
420 return localDispatch(FuncRequest(LFUN_WORDSEL));
428 BufferView * bv = cmd.view();
429 string argument = cmd.argument;
430 RESULT result = DISPATCHED;
432 bool was_macro = mathcursor->inMacroMode();
433 bool was_selection = mathcursor->selection();
437 mathcursor->normalize();
440 switch (cmd.action) {
442 case LFUN_MATH_MUTATE:
443 case LFUN_MATH_DISPLAY:
444 case LFUN_MATH_NUMBER:
445 case LFUN_MATH_NONUMBER:
448 case LFUN_DELETE_LINE_FORWARD:
449 case LFUN_INSERT_LABEL:
450 case LFUN_MATH_EXTERN:
451 case LFUN_TABULAR_FEATURE:
452 case LFUN_PASTESELECTION:
453 case LFUN_MATH_LIMITS:
454 bv->lockedInsetStoreUndo(Undo::EDIT);
455 mathcursor->dispatch(cmd);
456 updateLocal(bv, true);
460 sel = true; // fall through...
462 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
463 //lyxerr << "calling scroll 20\n";
465 updateLocal(bv, false);
466 // write something to the minibuffer
467 //bv->owner()->message(mathcursor->info());
471 sel = true; // fall through
473 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
474 updateLocal(bv, false);
478 sel = true; // fall through
480 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
481 updateLocal(bv, false);
485 sel = true; // fall through
487 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
488 updateLocal(bv, false);
492 mathcursor->home(false);
493 mathcursor->end(true);
494 updateLocal(bv, false);
498 case LFUN_WORDLEFTSEL:
499 sel = true; // fall through
502 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
503 updateLocal(bv, false);
507 case LFUN_WORDRIGHTSEL:
508 sel = true; // fall through
511 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
512 updateLocal(bv, false);
517 case LFUN_BEGINNINGBUFSEL:
518 case LFUN_BEGINNINGBUF:
519 result = FINISHED_UP;
520 updateLocal(bv, false);
527 result = FINISHED_DOWN;
528 updateLocal(bv, false);
532 mathcursor->idxNext();
533 updateLocal(bv, false);
537 mathcursor->idxPrev();
538 updateLocal(bv, false);
541 case LFUN_DELETE_WORD_BACKWARD:
543 bv->lockedInsetStoreUndo(Undo::DELETE);
544 mathcursor->backspace();
545 updateLocal(bv, true);
548 case LFUN_DELETE_WORD_FORWARD:
550 bv->lockedInsetStoreUndo(Undo::DELETE);
552 bv->updateInset(this, true);
556 // sprintf(dispatch_buffer, "%d %d",);
557 // dispatch_result = dispatch_buffer;
560 lyxerr << "LFUN_SETXY broken!\n";
563 istringstream is(cmd.argument.c_str());
565 mathcursor->setPos(x, y);
566 updateLocal(bv, false);
572 mathcursor->macroModeClose();
573 bv->lockedInsetStoreUndo(Undo::EDIT);
574 mathcursor->selPaste();
575 updateLocal(bv, true);
579 bv->lockedInsetStoreUndo(Undo::DELETE);
580 mathcursor->selCut();
581 updateLocal(bv, true);
585 mathcursor->selCopy();
589 // Special casing for superscript in case of LyX handling
591 case LFUN_CIRCUMFLEX:
592 if (cmd.argument.empty()) {
593 // do superscript if LyX handles
595 bv->lockedInsetStoreUndo(Undo::EDIT);
596 mathcursor->script(true);
597 updateLocal(bv, true);
614 case LFUN_HUNG_UMLAUT:
618 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
619 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
620 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
621 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
622 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
623 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
624 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
625 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
626 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
627 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
628 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
631 handleFont(bv, cmd.argument, "lyxgreek1");
632 if (cmd.argument.size())
633 mathcursor->insert(asArray(cmd.argument));
637 if (mathcursor->currentMode() == MathInset::TEXT_MODE) {
638 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
639 updateLocal(bv, true);
641 handleFont(bv, cmd.argument, "textrm");
643 //bv->owner()->message(_("math text mode toggled"));
649 bv->lockedInsetStoreUndo(Undo::EDIT);
650 mathcursor->setSize(arg);
651 updateLocal(bv, true);
656 case LFUN_INSERT_MATRIX: {
657 bv->lockedInsetStoreUndo(Undo::EDIT);
662 istringstream is(argument);
663 is >> m >> n >> v_align >> h_align;
667 mathcursor->niceInsert(
668 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
669 updateLocal(bv, true);
673 case LFUN_SUPERSCRIPT:
676 bv->lockedInsetStoreUndo(Undo::EDIT);
677 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
678 updateLocal(bv, true);
682 case LFUN_MATH_DELIM:
684 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
686 string rs = split(cmd.argument, ls, ' ');
687 // Reasonable default values
693 bv->lockedInsetStoreUndo(Undo::EDIT);
694 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
695 updateLocal(bv, true);
699 case LFUN_PROTECTEDSPACE:
700 case LFUN_MATH_SPACE:
701 bv->lockedInsetStoreUndo(Undo::EDIT);
702 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
703 updateLocal(bv, true);
707 bv->owner()->message(_("Invalid action in math mode!"));
711 case LFUN_EXEC_COMMAND:
712 result = UNDISPATCHED;
716 // interpret this as if a backslash was typed
717 bv->lockedInsetStoreUndo(Undo::EDIT);
718 mathcursor->interpret('\\');
719 updateLocal(bv, true);
722 case LFUN_BREAKPARAGRAPH:
723 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
724 case LFUN_BREAKPARAGRAPH_SKIP:
728 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
729 // handling such that "self-insert" works on "arbitrary stuff" too, and
730 // math-insert only handles special math things like "matrix".
731 case LFUN_INSERT_MATH:
732 bv->lockedInsetStoreUndo(Undo::EDIT);
733 mathcursor->niceInsert(argument);
734 updateLocal(bv, true);
738 case LFUN_SELFINSERT:
739 if (!argument.empty()) {
740 bv->lockedInsetStoreUndo(Undo::EDIT);
741 if (argument.size() == 1)
742 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
744 mathcursor->insert(asArray(argument));
745 updateLocal(bv, true);
749 case LFUN_MATH_PANEL:
750 result = UNDISPATCHED;
754 if (mathcursor->selection())
755 mathcursor->selClear();
757 result = UNDISPATCHED;
760 case LFUN_INSET_TOGGLE:
761 mathcursor->insetToggle();
762 updateLocal(bv, true);
765 case LFUN_REF_INSERT:
766 //if (argument.empty()) {
767 // InsetCommandParams p("ref");
768 // owner_->getDialogs().createRef(p.getAsString());
770 // InsetCommandParams p;
771 // p.setFromString(argument);
773 // InsetRef * inset = new InsetRef(p, *buffer_);
774 // if (!insertInset(inset))
777 // updateInset(inset, true);
780 if (cmd.argument.empty()) {
781 InsetCommandParams p("ref");
782 bv->owner()->getDialogs().createRef(p.getAsString());
784 //mathcursor->handleNest(new InsetRef2);
785 //mathcursor->insert(arg);
786 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
788 updateLocal(bv, true);
792 result = UNDISPATCHED;
795 mathcursor->normalize();
798 lyx::Assert(mathcursor);
800 if (mathcursor->selection() || was_selection)
801 toggleInsetSelection(bv);
803 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
804 result == UNDISPATCHED) {
808 cmd.view()->stuffClipboard(mathcursor->grabSelection());
810 releaseMathCursor(bv);
811 bv->unlockInset(this);
814 return result; // original version
818 void InsetFormulaBase::revealCodes(BufferView * bv) const
822 bv->owner()->message(mathcursor->info());
825 // write something to the minibuffer
826 // translate to latex
827 mathcursor->markInsert();
830 string str = os.str();
831 mathcursor->markErase();
832 string::size_type pos = 0;
834 for (string::iterator it = str.begin(); it != str.end(); ++it) {
837 else if (*it == '\0') {
839 pos = it - str.begin();
845 res = res.substr(pos - 30);
847 res = res.substr(0, 60);
848 bv->owner()->message(res);
853 Inset::Code InsetFormulaBase::lyxCode() const
855 return Inset::MATH_CODE;
859 int InsetFormulaBase::ylow() const
861 return yo_ - ascent(view(), font_);
865 int InsetFormulaBase::yhigh() const
867 return yo_ + descent(view(), font_);
871 int InsetFormulaBase::xlow() const
877 int InsetFormulaBase::xhigh() const
879 return xo_ + width(view(), font_);
883 /////////////////////////////////////////////////////////////////////
886 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
892 static InsetFormulaBase * lastformula = 0;
893 static MathIterator current = MathIterator(ibegin(par().nucleus()));
895 static string laststr;
897 if (lastformula != this || laststr != str) {
898 //lyxerr << "reset lastformula to " << this << "\n";
901 current = ibegin(par().nucleus());
903 mathed_parse_cell(ar, str);
907 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
909 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
910 if (it.cell().matchpart(ar, it.back().pos_)) {
911 bv->unlockInset(bv->theLockingInset());
912 if (!bv->lockInset(this)) {
913 lyxerr << "Cannot lock inset" << endl;
917 mathcursor = new MathCursor(this, true);
919 mathcursor->setSelection(it, ar.size());
922 updateLocal(bv, false);
927 //lyxerr << "not found!\n";
933 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
936 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
937 return searchForward(bv, what, a, b);
941 bool InsetFormulaBase::display() const
943 return par()->asHullInset() && par()->asHullInset()->display();
947 string InsetFormulaBase::selectionAsString() const
949 return mathcursor ? mathcursor->grabSelection() : string();
952 /////////////////////////////////////////////////////////////////////
955 void mathDispatchCreation(FuncRequest const & cmd, bool display)
957 BufferView * bv = cmd.view();
958 // use selection if available..
960 //if (action == LFUN_MATH_IMPORT_SELECTION)
964 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
967 InsetFormula * f = new InsetFormula(bv);
968 if (openNewInset(bv, f)) {
969 bv->theLockingInset()->
970 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
971 // don't do that also for LFUN_MATH_MODE unless you want end up with
972 // always changing to mathrm when opening an inlined inset
973 // -- I really hate "LyXfunc overloading"...
975 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
976 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
979 // create a macro if we see "\\newcommand" somewhere, and an ordinary
981 InsetFormulaBase * f;
982 if (sel.find("\\newcommand") == string::npos &&
983 sel.find("\\def") == string::npos)
984 f = new InsetFormula(sel);
986 f = new InsetFormulaMacro(sel);
987 bv->getLyXText()->cutSelection(bv);
990 cmd.message(N_("Math editor mode"));
994 void mathDispatch(FuncRequest const & cmd)
996 BufferView * bv = cmd.view();
997 if (!bv->available())
1000 switch (cmd.action) {
1002 case LFUN_MATH_DISPLAY:
1003 mathDispatchCreation(cmd, true);
1006 case LFUN_MATH_MODE:
1007 mathDispatchCreation(cmd, false);
1010 case LFUN_MATH_IMPORT_SELECTION:
1011 mathDispatchCreation(cmd, false);
1014 case LFUN_MATH_MACRO:
1015 if (cmd.argument.empty())
1016 cmd.errorMessage(N_("Missing argument"));
1018 string s = cmd.argument;
1019 string const s1 = token(s, ' ', 1);
1020 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1021 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1026 case LFUN_INSERT_MATH:
1027 case LFUN_INSERT_MATRIX:
1028 case LFUN_MATH_DELIM: {
1029 InsetFormula * f = new InsetFormula(bv);
1030 if (openNewInset(bv, f)) {
1031 bv->theLockingInset()->
1032 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
1033 bv->theLockingInset()->localDispatch(cmd);