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 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 + xo_, y + yo_);
178 // if that is removed, we won't get the magenta box when entering an
179 // inset for the first time
180 bv->updateInset(this, false);
184 void InsetFormulaBase::edit(BufferView * bv, bool front)
186 if (!bv->lockInset(this))
187 lyxerr << "Cannot lock math inset in edit call!\n";
188 releaseMathCursor(bv);
189 mathcursor = new MathCursor(this, front);
191 bv->updateInset(this, false);
195 void InsetFormulaBase::insetUnlock(BufferView * bv)
198 if (mathcursor->inMacroMode()) {
199 mathcursor->macroModeClose();
200 updateLocal(bv, true);
202 releaseMathCursor(bv);
205 bv->updateInset(this, false);
209 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
211 // calling metrics here destroys the cached xo,yo positions e.g. in
212 // MathParboxinset. And it would be too expensive anyway...
215 lyxerr << "getCursorPos - should not happen";
219 mathcursor->getPos(x, y);
220 x = mathcursor->targetX();
223 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
227 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
229 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
230 if (isCursorVisible())
237 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
239 if (isCursorVisible())
243 mathcursor->getPos(x, y);
244 math_font_max_dim(font_, asc, des);
245 bv->showLockedInsetCursor(x, y - yo_, asc, des);
246 setCursorVisible(true);
247 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
251 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
253 if (!isCursorVisible())
255 bv->hideLockedInsetCursor();
256 setCursorVisible(false);
257 //lyxerr << "hideInsetCursor: \n";
261 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
266 math_font_max_dim(font_, asc, des);
267 getCursorPos(bv, x, y);
269 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
270 bv->fitLockedInsetCursor(x, y, asc, des);
274 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
277 bv->updateInset(this, false);
281 vector<string> const InsetFormulaBase::getLabelList() const
283 return vector<string>();
287 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
289 bv->updateInset(this, dirty);
293 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
298 BufferView * bv = cmd.view();
301 bv->updateInset(this, true);
302 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << "\n";
304 if (cmd.button() == mouse_button::button3) {
305 // try to dispatch to enclosed insets first
306 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
307 // launch math panel for right mouse button
308 bv->owner()->getDialogs().showMathPanel();
313 if (cmd.button() == mouse_button::button2) {
314 mathcursor->selClear();
315 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
316 mathcursor->insert(asArray(bv->getClipboard()));
317 bv->updateInset(this, true);
321 if (cmd.button() == mouse_button::button1) {
322 // try to dispatch to enclosed insets first
323 mathcursor->dispatch(cmd);
324 // try to set the cursor
326 //mathcursor = new MathCursor(this, x == 0);
328 //mathcursor->setPos(x + xo_, y + yo_);
336 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
338 BufferView * bv = cmd.view();
339 releaseMathCursor(bv);
340 mathcursor = new MathCursor(this, cmd.x == 0);
341 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << "\n";
343 if (cmd.button() == mouse_button::button3) {
344 mathcursor->dispatch(cmd);
348 if (cmd.button() == mouse_button::button1) {
349 // just set the cursor here
350 //lyxerr << "setting cursor\n";
354 mathcursor->selClear();
355 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
356 mathcursor->dispatch(cmd);
360 bv->updateInset(this, false);
365 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
370 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
373 // only select with button 1
374 if (cmd.button() != mouse_button::button1)
377 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
383 if (!mathcursor->selection())
384 mathcursor->selStart();
386 BufferView * bv = cmd.view();
388 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
390 bv->updateInset(this, false);
395 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
397 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
398 // << " arg: '" << cmd.argument
399 // << " x: '" << cmd.x
400 // << " y: '" << cmd.y
401 // << "' button: " << cmd.button() << "\n";
403 switch (cmd.action) {
404 case LFUN_MOUSE_PRESS:
405 return lfunMousePress(cmd);
406 case LFUN_MOUSE_MOTION:
407 return lfunMouseMotion(cmd);
408 case LFUN_MOUSE_RELEASE:
409 return lfunMouseRelease(cmd);
410 case LFUN_MOUSE_DOUBLE:
411 //lyxerr << "Mouse double\n";
412 return localDispatch(FuncRequest(LFUN_WORDSEL));
420 BufferView * bv = cmd.view();
421 string argument = cmd.argument;
422 RESULT result = DISPATCHED;
424 bool was_macro = mathcursor->inMacroMode();
425 bool was_selection = mathcursor->selection();
429 mathcursor->normalize();
432 switch (cmd.action) {
434 case LFUN_MATH_MUTATE:
435 case LFUN_MATH_DISPLAY:
436 case LFUN_MATH_NUMBER:
437 case LFUN_MATH_NONUMBER:
440 case LFUN_DELETE_LINE_FORWARD:
441 case LFUN_INSERT_LABEL:
442 case LFUN_MATH_EXTERN:
443 case LFUN_TABULAR_FEATURE:
444 case LFUN_PASTESELECTION:
445 case LFUN_MATH_LIMITS:
446 bv->lockedInsetStoreUndo(Undo::EDIT);
447 mathcursor->dispatch(cmd);
448 updateLocal(bv, true);
452 sel = true; // fall through...
454 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
455 //lyxerr << "calling scroll 20\n";
457 updateLocal(bv, false);
458 // write something to the minibuffer
459 //bv->owner()->message(mathcursor->info());
463 sel = true; // fall through
465 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
466 updateLocal(bv, false);
470 sel = true; // fall through
472 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
473 updateLocal(bv, false);
477 sel = true; // fall through
479 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
480 updateLocal(bv, false);
484 mathcursor->home(false);
485 mathcursor->end(true);
486 updateLocal(bv, false);
490 case LFUN_WORDLEFTSEL:
491 sel = true; // fall through
494 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
495 updateLocal(bv, false);
499 case LFUN_WORDRIGHTSEL:
500 sel = true; // fall through
503 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
504 updateLocal(bv, false);
509 case LFUN_BEGINNINGBUFSEL:
510 case LFUN_BEGINNINGBUF:
511 result = FINISHED_UP;
512 updateLocal(bv, false);
519 result = FINISHED_DOWN;
520 updateLocal(bv, false);
524 mathcursor->idxNext();
525 updateLocal(bv, false);
529 mathcursor->idxPrev();
530 updateLocal(bv, false);
533 case LFUN_DELETE_WORD_BACKWARD:
535 bv->lockedInsetStoreUndo(Undo::DELETE);
536 mathcursor->backspace();
537 updateLocal(bv, true);
540 case LFUN_DELETE_WORD_FORWARD:
542 bv->lockedInsetStoreUndo(Undo::DELETE);
544 bv->updateInset(this, true);
548 // sprintf(dispatch_buffer, "%d %d",);
549 // dispatch_result = dispatch_buffer;
552 lyxerr << "LFUN_SETXY broken!\n";
555 istringstream is(cmd.argument.c_str());
557 mathcursor->setPos(x, y);
558 updateLocal(bv, false);
564 mathcursor->macroModeClose();
565 bv->lockedInsetStoreUndo(Undo::EDIT);
566 mathcursor->selPaste();
567 updateLocal(bv, true);
571 bv->lockedInsetStoreUndo(Undo::DELETE);
572 mathcursor->selCut();
573 updateLocal(bv, true);
577 mathcursor->selCopy();
581 // Special casing for superscript in case of LyX handling
583 case LFUN_CIRCUMFLEX:
584 if (cmd.argument.empty()) {
585 // do superscript if LyX handles
587 bv->lockedInsetStoreUndo(Undo::EDIT);
588 mathcursor->script(true);
589 updateLocal(bv, true);
606 case LFUN_HUNG_UMLAUT:
610 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
611 case LFUN_BOLD: handleFont(bv, cmd.argument, "textbf"); break;
612 case LFUN_SANS: handleFont(bv, cmd.argument, "textsf"); break;
613 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
614 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
615 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
616 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
617 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
618 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
619 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
620 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
623 handleFont(bv, cmd.argument, "lyxgreek1");
624 if (cmd.argument.size())
625 mathcursor->insert(asArray(cmd.argument));
629 if (mathcursor->currentMode())
630 handleFont(bv, cmd.argument, "textrm");
632 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
633 updateLocal(bv, true);
635 //bv->owner()->message(_("math text mode toggled"));
641 bv->lockedInsetStoreUndo(Undo::EDIT);
642 mathcursor->setSize(arg);
643 updateLocal(bv, true);
648 case LFUN_INSERT_MATRIX: {
649 bv->lockedInsetStoreUndo(Undo::EDIT);
654 istringstream is(argument);
655 is >> m >> n >> v_align >> h_align;
659 mathcursor->niceInsert(
660 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
661 updateLocal(bv, true);
665 case LFUN_SUPERSCRIPT:
668 bv->lockedInsetStoreUndo(Undo::EDIT);
669 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
670 updateLocal(bv, true);
674 case LFUN_MATH_DELIM:
676 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
678 string rs = split(cmd.argument, ls, ' ');
679 // Reasonable default values
685 bv->lockedInsetStoreUndo(Undo::EDIT);
686 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
687 updateLocal(bv, true);
691 case LFUN_PROTECTEDSPACE:
692 case LFUN_MATH_SPACE:
693 bv->lockedInsetStoreUndo(Undo::EDIT);
694 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
695 updateLocal(bv, true);
699 bv->owner()->message(_("Invalid action in math mode!"));
703 case LFUN_EXEC_COMMAND:
704 result = UNDISPATCHED;
708 // interpret this as if a backslash was typed
709 bv->lockedInsetStoreUndo(Undo::EDIT);
710 mathcursor->interpret('\\');
711 updateLocal(bv, true);
714 case LFUN_BREAKPARAGRAPH:
715 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
716 case LFUN_BREAKPARAGRAPH_SKIP:
720 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
721 // handling such that "self-insert" works on "arbitrary stuff" too, and
722 // math-insert only handles special math things like "matrix".
723 case LFUN_INSERT_MATH:
724 bv->lockedInsetStoreUndo(Undo::EDIT);
725 mathcursor->niceInsert(argument);
726 updateLocal(bv, true);
730 case LFUN_SELFINSERT:
731 if (!argument.empty()) {
732 bv->lockedInsetStoreUndo(Undo::EDIT);
733 if (argument.size() == 1)
734 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
736 mathcursor->insert(asArray(argument));
737 updateLocal(bv, true);
741 case LFUN_MATH_PANEL:
742 result = UNDISPATCHED;
746 if (mathcursor->selection())
747 mathcursor->selClear();
749 result = UNDISPATCHED;
752 case LFUN_INSET_TOGGLE:
753 mathcursor->insetToggle();
754 updateLocal(bv, true);
757 case LFUN_REF_INSERT:
758 //if (argument.empty()) {
759 // InsetCommandParams p("ref");
760 // owner_->getDialogs().createRef(p.getAsString());
762 // InsetCommandParams p;
763 // p.setFromString(argument);
765 // InsetRef * inset = new InsetRef(p, *buffer_);
766 // if (!insertInset(inset))
769 // updateInset(inset, true);
772 if (cmd.argument.empty()) {
773 InsetCommandParams p("ref");
774 bv->owner()->getDialogs().createRef(p.getAsString());
776 //mathcursor->handleNest(new InsetRef2);
777 //mathcursor->insert(arg);
778 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
780 updateLocal(bv, true);
784 result = UNDISPATCHED;
787 mathcursor->normalize();
790 lyx::Assert(mathcursor);
792 if (mathcursor->selection() || was_selection)
793 toggleInsetSelection(bv);
795 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
796 result == UNDISPATCHED) {
801 releaseMathCursor(bv);
802 bv->unlockInset(this);
805 return result; // original version
809 void InsetFormulaBase::revealCodes(BufferView * bv) const
813 bv->owner()->message(mathcursor->info());
816 // write something to the minibuffer
817 // translate to latex
818 mathcursor->markInsert();
821 string str = os.str();
822 mathcursor->markErase();
823 string::size_type pos = 0;
825 for (string::iterator it = str.begin(); it != str.end(); ++it) {
828 else if (*it == '\0') {
830 pos = it - str.begin();
836 res = res.substr(pos - 30);
838 res = res.substr(0, 60);
839 bv->owner()->message(res);
844 Inset::Code InsetFormulaBase::lyxCode() const
846 return Inset::MATH_CODE;
850 int InsetFormulaBase::ylow() const
852 return yo_ - ascent(view(), font_);
856 int InsetFormulaBase::yhigh() const
858 return yo_ + descent(view(), font_);
862 int InsetFormulaBase::xlow() const
868 int InsetFormulaBase::xhigh() const
870 return xo_ + width(view(), font_);
874 /////////////////////////////////////////////////////////////////////
877 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
883 static InsetFormulaBase * lastformula = 0;
884 static MathIterator current = MathIterator(ibegin(par().nucleus()));
886 static string laststr;
888 if (lastformula != this || laststr != str) {
889 //lyxerr << "reset lastformula to " << this << "\n";
892 current = ibegin(par().nucleus());
894 mathed_parse_cell(ar, str);
898 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
900 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
901 if (it.cell().matchpart(ar, it.back().pos_)) {
902 bv->unlockInset(bv->theLockingInset());
903 if (!bv->lockInset(this)) {
904 lyxerr << "Cannot lock inset" << endl;
908 mathcursor = new MathCursor(this, true);
910 mathcursor->setSelection(it, ar.size());
913 updateLocal(bv, false);
918 //lyxerr << "not found!\n";
924 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
927 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
928 return searchForward(bv, what, a, b);
932 bool InsetFormulaBase::display() const
934 return par()->asHullInset() && par()->asHullInset()->display();
938 /////////////////////////////////////////////////////////////////////
941 void mathDispatchCreation(FuncRequest const & cmd, bool display)
943 BufferView * bv = cmd.view();
944 // use selection if available..
946 //if (action == LFUN_MATH_IMPORT_SELECTION)
950 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
953 InsetFormula * f = new InsetFormula(bv);
954 if (openNewInset(bv, f)) {
955 bv->theLockingInset()->
956 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
957 // don't do that also for LFUN_MATH_MODE unless you want end up with
958 // always changing to mathrm when opening an inlined inset
959 // -- I really hate "LyXfunc overloading"...
961 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
962 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
965 // create a macro if we see "\\newcommand" somewhere, and an ordinary
967 InsetFormulaBase * f;
968 if (sel.find("\\newcommand") == string::npos &&
969 sel.find("\\def") == string::npos)
970 f = new InsetFormula(sel);
972 f = new InsetFormulaMacro(sel);
973 bv->getLyXText()->cutSelection(bv);
976 cmd.message(N_("Math editor mode"));
980 void mathDispatch(FuncRequest const & cmd)
982 BufferView * bv = cmd.view();
983 if (!bv->available())
986 switch (cmd.action) {
988 case LFUN_MATH_DISPLAY:
989 mathDispatchCreation(cmd, true);
993 mathDispatchCreation(cmd, false);
996 case LFUN_MATH_IMPORT_SELECTION:
997 mathDispatchCreation(cmd, false);
1000 case LFUN_MATH_MACRO:
1001 if (cmd.argument.empty())
1002 cmd.errorMessage(N_("Missing argument"));
1004 string s = cmd.argument;
1005 string const s1 = token(s, ' ', 1);
1006 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1007 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1012 case LFUN_INSERT_MATH:
1013 case LFUN_INSERT_MATRIX:
1014 case LFUN_MATH_DELIM: {
1015 InsetFormula * f = new InsetFormula(bv);
1016 if (openNewInset(bv, f)) {
1017 bv->theLockingInset()->
1018 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
1019 bv->theLockingInset()->localDispatch(cmd);