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 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << "\n";
349 if (!mathcursor || mathcursor->formula() != this) {
350 lyxerr << "re-create cursor\n";
351 releaseMathCursor(bv);
352 mathcursor = new MathCursor(this, cmd.x == 0);
354 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
357 if (cmd.button() == mouse_button::button3) {
358 mathcursor->dispatch(cmd);
362 if (cmd.button() == mouse_button::button1) {
365 mathcursor->selClear();
366 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
367 mathcursor->dispatch(cmd);
371 bv->updateInset(this, false);
376 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
381 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
384 // only select with button 1
385 if (cmd.button() != mouse_button::button1)
388 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
394 if (!mathcursor->selection())
395 mathcursor->selStart();
397 BufferView * bv = cmd.view();
399 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
401 bv->updateInset(this, false);
406 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
408 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
409 // << " arg: '" << cmd.argument
410 // << " x: '" << cmd.x
411 // << " y: '" << cmd.y
412 // << "' button: " << cmd.button() << "\n";
414 switch (cmd.action) {
415 case LFUN_MOUSE_PRESS:
416 return lfunMousePress(cmd);
417 case LFUN_MOUSE_MOTION:
418 return lfunMouseMotion(cmd);
419 case LFUN_MOUSE_RELEASE:
420 return lfunMouseRelease(cmd);
421 case LFUN_MOUSE_DOUBLE:
422 //lyxerr << "Mouse double\n";
423 return localDispatch(FuncRequest(LFUN_WORDSEL));
431 BufferView * bv = cmd.view();
432 string argument = cmd.argument;
433 RESULT result = DISPATCHED;
435 bool was_macro = mathcursor->inMacroMode();
436 bool was_selection = mathcursor->selection();
440 mathcursor->normalize();
443 switch (cmd.action) {
445 case LFUN_MATH_MUTATE:
446 case LFUN_MATH_DISPLAY:
447 case LFUN_MATH_NUMBER:
448 case LFUN_MATH_NONUMBER:
451 case LFUN_DELETE_LINE_FORWARD:
452 case LFUN_INSERT_LABEL:
453 case LFUN_MATH_EXTERN:
454 case LFUN_TABULAR_FEATURE:
455 case LFUN_PASTESELECTION:
456 case LFUN_MATH_LIMITS:
457 bv->lockedInsetStoreUndo(Undo::EDIT);
458 mathcursor->dispatch(cmd);
459 updateLocal(bv, true);
463 sel = true; // fall through...
465 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
466 //lyxerr << "calling scroll 20\n";
468 updateLocal(bv, false);
469 // write something to the minibuffer
470 //bv->owner()->message(mathcursor->info());
474 sel = true; // fall through
476 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
477 updateLocal(bv, false);
481 sel = true; // fall through
483 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
484 updateLocal(bv, false);
488 sel = true; // fall through
490 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
491 updateLocal(bv, false);
495 mathcursor->home(false);
496 mathcursor->end(true);
497 updateLocal(bv, false);
501 case LFUN_WORDLEFTSEL:
502 sel = true; // fall through
505 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
506 updateLocal(bv, false);
510 case LFUN_WORDRIGHTSEL:
511 sel = true; // fall through
514 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
515 updateLocal(bv, false);
520 case LFUN_BEGINNINGBUFSEL:
521 case LFUN_BEGINNINGBUF:
522 result = FINISHED_UP;
523 updateLocal(bv, false);
530 result = FINISHED_DOWN;
531 updateLocal(bv, false);
535 mathcursor->idxNext();
536 updateLocal(bv, false);
540 mathcursor->idxPrev();
541 updateLocal(bv, false);
544 case LFUN_DELETE_WORD_BACKWARD:
546 bv->lockedInsetStoreUndo(Undo::DELETE);
547 mathcursor->backspace();
548 updateLocal(bv, true);
551 case LFUN_DELETE_WORD_FORWARD:
553 bv->lockedInsetStoreUndo(Undo::DELETE);
555 bv->updateInset(this, true);
559 // sprintf(dispatch_buffer, "%d %d",);
560 // dispatch_result = dispatch_buffer;
563 lyxerr << "LFUN_SETXY broken!\n";
566 istringstream is(cmd.argument.c_str());
568 mathcursor->setPos(x, y);
569 updateLocal(bv, false);
575 mathcursor->macroModeClose();
576 bv->lockedInsetStoreUndo(Undo::EDIT);
577 mathcursor->selPaste();
578 updateLocal(bv, true);
582 bv->lockedInsetStoreUndo(Undo::DELETE);
583 mathcursor->selCut();
584 updateLocal(bv, true);
588 mathcursor->selCopy();
592 // Special casing for superscript in case of LyX handling
594 case LFUN_CIRCUMFLEX:
595 if (cmd.argument.empty()) {
596 // do superscript if LyX handles
598 bv->lockedInsetStoreUndo(Undo::EDIT);
599 mathcursor->script(true);
600 updateLocal(bv, true);
617 case LFUN_HUNG_UMLAUT:
621 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
622 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
623 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
624 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
625 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
626 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
627 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
628 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
629 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
630 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
631 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
634 handleFont(bv, cmd.argument, "lyxgreek1");
635 if (cmd.argument.size())
636 mathcursor->insert(asArray(cmd.argument));
640 if (mathcursor->currentMode() == MathInset::TEXT_MODE) {
641 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
642 updateLocal(bv, true);
644 handleFont(bv, cmd.argument, "textrm");
646 //bv->owner()->message(_("math text mode toggled"));
652 bv->lockedInsetStoreUndo(Undo::EDIT);
653 mathcursor->setSize(arg);
654 updateLocal(bv, true);
659 case LFUN_INSERT_MATRIX: {
660 bv->lockedInsetStoreUndo(Undo::EDIT);
665 istringstream is(argument);
666 is >> m >> n >> v_align >> h_align;
670 mathcursor->niceInsert(
671 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
672 updateLocal(bv, true);
676 case LFUN_SUPERSCRIPT:
679 bv->lockedInsetStoreUndo(Undo::EDIT);
680 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
681 updateLocal(bv, true);
685 case LFUN_MATH_DELIM:
687 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
689 string rs = split(cmd.argument, ls, ' ');
690 // Reasonable default values
696 bv->lockedInsetStoreUndo(Undo::EDIT);
697 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
698 updateLocal(bv, true);
702 case LFUN_PROTECTEDSPACE:
703 case LFUN_MATH_SPACE:
704 bv->lockedInsetStoreUndo(Undo::EDIT);
705 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
706 updateLocal(bv, true);
710 bv->owner()->message(_("Invalid action in math mode!"));
714 case LFUN_EXEC_COMMAND:
715 result = UNDISPATCHED;
719 // interpret this as if a backslash was typed
720 bv->lockedInsetStoreUndo(Undo::EDIT);
721 mathcursor->interpret('\\');
722 updateLocal(bv, true);
725 case LFUN_BREAKPARAGRAPH:
726 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
727 case LFUN_BREAKPARAGRAPH_SKIP:
731 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
732 // handling such that "self-insert" works on "arbitrary stuff" too, and
733 // math-insert only handles special math things like "matrix".
734 case LFUN_INSERT_MATH:
735 bv->lockedInsetStoreUndo(Undo::EDIT);
736 mathcursor->niceInsert(argument);
737 updateLocal(bv, true);
741 case LFUN_SELFINSERT:
742 if (!argument.empty()) {
743 bv->lockedInsetStoreUndo(Undo::EDIT);
744 if (argument.size() == 1)
745 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
747 mathcursor->insert(asArray(argument));
748 updateLocal(bv, true);
752 case LFUN_MATH_PANEL:
753 result = UNDISPATCHED;
757 if (mathcursor->selection())
758 mathcursor->selClear();
760 result = UNDISPATCHED;
763 case LFUN_INSET_TOGGLE:
764 mathcursor->insetToggle();
765 updateLocal(bv, true);
768 case LFUN_REF_INSERT:
769 //if (argument.empty()) {
770 // InsetCommandParams p("ref");
771 // owner_->getDialogs().createRef(p.getAsString());
773 // InsetCommandParams p;
774 // p.setFromString(argument);
776 // InsetRef * inset = new InsetRef(p, *buffer_);
777 // if (!insertInset(inset))
780 // updateInset(inset, true);
783 if (cmd.argument.empty()) {
784 InsetCommandParams p("ref");
785 bv->owner()->getDialogs().createRef(p.getAsString());
787 //mathcursor->handleNest(new InsetRef2);
788 //mathcursor->insert(arg);
789 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
791 updateLocal(bv, true);
795 result = UNDISPATCHED;
798 mathcursor->normalize();
801 lyx::Assert(mathcursor);
803 if (mathcursor->selection() || was_selection)
804 toggleInsetSelection(bv);
806 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
807 result == UNDISPATCHED) {
811 cmd.view()->stuffClipboard(mathcursor->grabSelection());
813 releaseMathCursor(bv);
814 bv->unlockInset(this);
817 return result; // original version
821 void InsetFormulaBase::revealCodes(BufferView * bv) const
825 bv->owner()->message(mathcursor->info());
828 // write something to the minibuffer
829 // translate to latex
830 mathcursor->markInsert();
833 string str = os.str();
834 mathcursor->markErase();
835 string::size_type pos = 0;
837 for (string::iterator it = str.begin(); it != str.end(); ++it) {
840 else if (*it == '\0') {
842 pos = it - str.begin();
848 res = res.substr(pos - 30);
850 res = res.substr(0, 60);
851 bv->owner()->message(res);
856 Inset::Code InsetFormulaBase::lyxCode() const
858 return Inset::MATH_CODE;
862 int InsetFormulaBase::ylow() const
864 return yo_ - ascent(view(), font_);
868 int InsetFormulaBase::yhigh() const
870 return yo_ + descent(view(), font_);
874 int InsetFormulaBase::xlow() const
880 int InsetFormulaBase::xhigh() const
882 return xo_ + width(view(), font_);
886 /////////////////////////////////////////////////////////////////////
889 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
895 static InsetFormulaBase * lastformula = 0;
896 static MathIterator current = MathIterator(ibegin(par().nucleus()));
898 static string laststr;
900 if (lastformula != this || laststr != str) {
901 //lyxerr << "reset lastformula to " << this << "\n";
904 current = ibegin(par().nucleus());
906 mathed_parse_cell(ar, str);
910 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
912 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
913 if (it.cell().matchpart(ar, it.back().pos_)) {
914 bv->unlockInset(bv->theLockingInset());
915 if (!bv->lockInset(this)) {
916 lyxerr << "Cannot lock inset" << endl;
920 mathcursor = new MathCursor(this, true);
922 mathcursor->setSelection(it, ar.size());
925 updateLocal(bv, false);
930 //lyxerr << "not found!\n";
936 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
939 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
940 return searchForward(bv, what, a, b);
944 bool InsetFormulaBase::display() const
946 return par()->asHullInset() && par()->asHullInset()->display();
950 string InsetFormulaBase::selectionAsString() const
952 return mathcursor ? mathcursor->grabSelection() : string();
955 /////////////////////////////////////////////////////////////////////
958 void mathDispatchCreation(FuncRequest const & cmd, bool display)
960 BufferView * bv = cmd.view();
961 // use selection if available..
963 //if (action == LFUN_MATH_IMPORT_SELECTION)
967 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
970 InsetFormula * f = new InsetFormula(bv);
971 if (openNewInset(bv, f)) {
972 bv->theLockingInset()->
973 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
974 // don't do that also for LFUN_MATH_MODE unless you want end up with
975 // always changing to mathrm when opening an inlined inset
976 // -- I really hate "LyXfunc overloading"...
978 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
979 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
982 // create a macro if we see "\\newcommand" somewhere, and an ordinary
984 InsetFormulaBase * f;
985 if (sel.find("\\newcommand") == string::npos &&
986 sel.find("\\def") == string::npos)
987 f = new InsetFormula(sel);
989 f = new InsetFormulaMacro(sel);
990 bv->getLyXText()->cutSelection(bv);
993 cmd.message(N_("Math editor mode"));
997 void mathDispatch(FuncRequest const & cmd)
999 BufferView * bv = cmd.view();
1000 if (!bv->available())
1003 switch (cmd.action) {
1005 case LFUN_MATH_DISPLAY:
1006 mathDispatchCreation(cmd, true);
1009 case LFUN_MATH_MODE:
1010 mathDispatchCreation(cmd, false);
1013 case LFUN_MATH_IMPORT_SELECTION:
1014 mathDispatchCreation(cmd, false);
1017 case LFUN_MATH_MACRO:
1018 if (cmd.argument.empty())
1019 cmd.errorMessage(N_("Missing argument"));
1021 string s = cmd.argument;
1022 string const s1 = token(s, ' ', 1);
1023 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1024 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1029 case LFUN_INSERT_MATH:
1030 case LFUN_INSERT_MATRIX:
1031 case LFUN_MATH_DELIM: {
1032 InsetFormula * f = new InsetFormula(bv);
1033 if (openNewInset(bv, f)) {
1034 bv->theLockingInset()->
1035 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
1036 bv->theLockingInset()->localDispatch(cmd);