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"
65 MathCursor * mathcursor = 0;
76 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
78 if (!bv->insertInset(new_inset)) {
82 new_inset->edit(bv, true);
91 InsetFormulaBase::InsetFormulaBase()
92 : font_(), xo_(0), yo_(0)
94 // This is needed as long the math parser is not re-entrant
96 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
97 //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
98 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
99 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
103 // simply scrap this function if you want
104 void InsetFormulaBase::mutateToText()
107 // translate to latex
109 latex(NULL, os, false, false);
110 string str = os.str();
113 LyXText * lt = view_->getLyXText();
114 string::const_iterator cit = str.begin();
115 string::const_iterator end = str.end();
116 for (; cit != end; ++cit)
117 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
120 //view_->owner()->dispatch(LFUN_ESCAPE);
125 void InsetFormulaBase::handleFont
126 (BufferView * bv, string const & arg, string const & font)
128 bv->lockedInsetStoreUndo(Undo::EDIT);
129 bool sel = mathcursor->selection();
131 updateLocal(bv, true);
132 mathcursor->handleNest(createMathInset(font));
133 mathcursor->insert(arg);
135 updateLocal(bv, false);
139 // Check if uses AMS macros
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
144 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
151 void InsetFormulaBase::metrics(BufferView * bv) const
154 view_ = bv->owner()->view();
156 mi.base.style = LM_ST_TEXT;
157 mi.base.font = font_;
158 mi.base.font.setColor(LColor::math);
163 string const InsetFormulaBase::editMessage() const
165 return _("Math editor mode");
169 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
171 if (!bv->lockInset(this))
172 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
173 releaseMathCursor(bv);
174 mathcursor = new MathCursor(this, true);
176 mathcursor->setPos(x + xo_, y + yo_);
177 // if that is removed, we won't get the magenta box when entering an
178 // inset for the first time
179 bv->updateInset(this, false);
183 void InsetFormulaBase::edit(BufferView * bv, bool front)
185 if (!bv->lockInset(this))
186 lyxerr << "Cannot lock math inset in edit call!\n";
187 releaseMathCursor(bv);
188 mathcursor = new MathCursor(this, front);
190 bv->updateInset(this, false);
194 void InsetFormulaBase::insetUnlock(BufferView * bv)
197 if (mathcursor->inMacroMode()) {
198 mathcursor->macroModeClose();
199 updateLocal(bv, true);
201 releaseMathCursor(bv);
204 bv->updateInset(this, false);
208 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
210 // calling metrics here destroys the cached xo,yo positions e.g. in
211 // MathParboxinset. And it would be too expensive anyway...
214 lyxerr << "getCursorPos - should not happen";
218 mathcursor->getPos(x, y);
219 x = mathcursor->targetX();
222 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
226 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
228 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
229 if (isCursorVisible())
236 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
238 if (isCursorVisible())
242 mathcursor->getPos(x, y);
243 math_font_max_dim(font_, asc, des);
244 bv->showLockedInsetCursor(x, y - yo_, asc, des);
245 setCursorVisible(true);
246 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
250 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
252 if (!isCursorVisible())
254 bv->hideLockedInsetCursor();
255 setCursorVisible(false);
256 //lyxerr << "hideInsetCursor: \n";
260 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
265 math_font_max_dim(font_, asc, des);
266 getCursorPos(bv, x, y);
268 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
269 bv->fitLockedInsetCursor(x, y, asc, des);
273 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
276 bv->updateInset(this, false);
280 vector<string> const InsetFormulaBase::getLabelList() const
282 return vector<string>();
286 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
288 bv->updateInset(this, dirty);
292 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
297 BufferView * bv = cmd.view();
300 bv->updateInset(this, false);
302 if (cmd.button() == mouse_button::button3) {
303 // try to dispatch to enclosed insets first
304 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
305 // launch math panel for right mouse button
306 bv->owner()->getDialogs().showMathPanel();
311 if (cmd.button() == mouse_button::button1) {
312 // try to dispatch to enclosed insets first
313 mathcursor->dispatch(cmd);
314 // try to set the cursor
316 //mathcursor = new MathCursor(this, x == 0);
318 //mathcursor->setPos(x + xo_, y + yo_);
325 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
327 BufferView * bv = cmd.view();
328 releaseMathCursor(bv);
329 mathcursor = new MathCursor(this, cmd.x == 0);
331 if (cmd.button() == mouse_button::button1) {
332 // just set the cursor here
333 //lyxerr << "setting cursor\n";
337 mathcursor->selClear();
338 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
339 mathcursor->dispatch(cmd);
342 if (cmd.button() == mouse_button::button3) {
343 mathcursor->dispatch(cmd);
347 bv->updateInset(this, false);
352 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
357 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
360 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
361 //lyxerr << "insetMotionNotify: ignored\n";
367 if (!mathcursor->selection())
368 mathcursor->selStart();
370 BufferView * bv = cmd.view();
372 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
374 bv->updateInset(this, false);
379 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
381 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
382 // << " arg: '" << cmd.argument
383 // << " x: '" << cmd.x
384 // << " y: '" << cmd.y
385 // << "' button: " << cmd.button() << "\n";
387 switch (cmd.action) {
388 case LFUN_MOUSE_PRESS:
389 return lfunMousePress(cmd);
390 case LFUN_MOUSE_MOTION:
391 return lfunMouseMotion(cmd);
392 case LFUN_MOUSE_RELEASE:
393 return lfunMouseRelease(cmd);
394 case LFUN_MOUSE_DOUBLE:
395 //lyxerr << "Mouse double\n";
396 return localDispatch(FuncRequest(LFUN_WORDSEL));
404 BufferView * bv = cmd.view();
405 string argument = cmd.argument;
406 RESULT result = DISPATCHED;
408 bool was_macro = mathcursor->inMacroMode();
409 bool was_selection = mathcursor->selection();
413 mathcursor->normalize();
416 switch (cmd.action) {
418 case LFUN_MATH_MUTATE:
419 case LFUN_MATH_DISPLAY:
420 case LFUN_MATH_NUMBER:
421 case LFUN_MATH_NONUMBER:
424 case LFUN_DELETE_LINE_FORWARD:
425 case LFUN_INSERT_LABEL:
426 case LFUN_MATH_EXTERN:
427 case LFUN_TABULAR_FEATURE:
428 case LFUN_PASTESELECTION:
429 case LFUN_MATH_LIMITS:
430 bv->lockedInsetStoreUndo(Undo::EDIT);
431 mathcursor->dispatch(cmd);
432 updateLocal(bv, true);
436 sel = true; // fall through...
438 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
439 //lyxerr << "calling scroll 20\n";
441 updateLocal(bv, false);
442 // write something to the minibuffer
443 //bv->owner()->message(mathcursor->info());
447 sel = true; // fall through
449 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
450 updateLocal(bv, false);
454 sel = true; // fall through
456 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
457 updateLocal(bv, false);
461 sel = true; // fall through
463 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
464 updateLocal(bv, false);
468 mathcursor->home(false);
469 mathcursor->end(true);
470 updateLocal(bv, false);
474 case LFUN_WORDLEFTSEL:
475 sel = true; // fall through
478 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
479 updateLocal(bv, false);
483 case LFUN_WORDRIGHTSEL:
484 sel = true; // fall through
487 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
488 updateLocal(bv, false);
493 case LFUN_BEGINNINGBUFSEL:
494 case LFUN_BEGINNINGBUF:
495 result = FINISHED_UP;
496 updateLocal(bv, false);
503 result = FINISHED_DOWN;
504 updateLocal(bv, false);
508 mathcursor->idxNext();
509 updateLocal(bv, false);
513 mathcursor->idxPrev();
514 updateLocal(bv, false);
517 case LFUN_DELETE_WORD_BACKWARD:
519 bv->lockedInsetStoreUndo(Undo::DELETE);
520 mathcursor->backspace();
521 updateLocal(bv, true);
524 case LFUN_DELETE_WORD_FORWARD:
526 bv->lockedInsetStoreUndo(Undo::DELETE);
528 bv->updateInset(this, true);
532 // sprintf(dispatch_buffer, "%d %d",);
533 // dispatch_result = dispatch_buffer;
536 lyxerr << "LFUN_SETXY broken!\n";
539 istringstream is(cmd.argument.c_str());
541 mathcursor->setPos(x, y);
542 updateLocal(bv, false);
548 mathcursor->macroModeClose();
549 bv->lockedInsetStoreUndo(Undo::EDIT);
550 mathcursor->selPaste();
551 updateLocal(bv, true);
555 bv->lockedInsetStoreUndo(Undo::DELETE);
556 mathcursor->selCut();
557 updateLocal(bv, true);
561 mathcursor->selCopy();
565 // Special casing for superscript in case of LyX handling
567 case LFUN_CIRCUMFLEX:
568 if (cmd.argument.empty()) {
569 // do superscript if LyX handles
571 bv->lockedInsetStoreUndo(Undo::EDIT);
572 mathcursor->script(true);
573 updateLocal(bv, true);
590 case LFUN_HUNG_UMLAUT:
594 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
595 case LFUN_BOLD: handleFont(bv, cmd.argument, "textbf"); break;
596 case LFUN_SANS: handleFont(bv, cmd.argument, "textsf"); break;
597 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
598 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
599 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
600 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
601 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
602 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
603 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
604 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
607 handleFont(bv, cmd.argument, "lyxgreek1");
608 if (cmd.argument.size())
609 mathcursor->interpret(cmd.argument);
613 if (mathcursor->currentMode())
614 handleFont(bv, cmd.argument, "textrm");
616 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
617 updateLocal(bv, true);
619 //bv->owner()->message(_("math text mode toggled"));
625 bv->lockedInsetStoreUndo(Undo::EDIT);
626 mathcursor->setSize(arg);
627 updateLocal(bv, true);
632 case LFUN_INSERT_MATRIX:
633 if (!cmd.argument.empty()) {
634 bv->lockedInsetStoreUndo(Undo::EDIT);
635 mathcursor->interpret("matrix " + cmd.argument);
636 updateLocal(bv, true);
640 case LFUN_SUPERSCRIPT:
643 bv->lockedInsetStoreUndo(Undo::EDIT);
644 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
645 updateLocal(bv, true);
649 case LFUN_MATH_DELIM:
651 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
653 string rs = split(cmd.argument, ls, ' ');
654 // Reasonable default values
660 bv->lockedInsetStoreUndo(Undo::EDIT);
661 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
662 updateLocal(bv, true);
666 case LFUN_PROTECTEDSPACE:
667 case LFUN_MATH_SPACE:
668 bv->lockedInsetStoreUndo(Undo::EDIT);
669 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
670 updateLocal(bv, true);
674 bv->owner()->message(_("Invalid action in math mode!"));
678 case LFUN_EXEC_COMMAND:
679 result = UNDISPATCHED;
683 // interpret this as if a backslash was typed
684 bv->lockedInsetStoreUndo(Undo::EDIT);
685 mathcursor->interpret('\\');
686 updateLocal(bv, true);
689 case LFUN_BREAKPARAGRAPH:
690 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
691 case LFUN_BREAKPARAGRAPH_SKIP:
696 case LFUN_INSERT_MATH:
697 case LFUN_SELFINSERT:
698 if (!argument.empty()) {
699 bv->lockedInsetStoreUndo(Undo::EDIT);
700 if (argument.size() == 1)
701 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
703 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
704 updateLocal(bv, true);
708 case LFUN_MATH_PANEL:
709 result = UNDISPATCHED;
713 if (mathcursor->selection())
714 mathcursor->selClear();
716 result = UNDISPATCHED;
719 case LFUN_INSET_TOGGLE:
720 mathcursor->insetToggle();
721 updateLocal(bv, true);
724 case LFUN_REF_INSERT:
725 //if (argument.empty()) {
726 // InsetCommandParams p("ref");
727 // owner_->getDialogs().createRef(p.getAsString());
729 // InsetCommandParams p;
730 // p.setFromString(argument);
732 // InsetRef * inset = new InsetRef(p, *buffer_);
733 // if (!insertInset(inset))
736 // updateInset(inset, true);
739 if (cmd.argument.empty()) {
740 InsetCommandParams p("ref");
741 bv->owner()->getDialogs().createRef(p.getAsString());
743 //mathcursor->handleNest(new InsetRef2);
744 //mathcursor->insert(arg);
745 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
747 updateLocal(bv, true);
751 result = UNDISPATCHED;
754 mathcursor->normalize();
757 lyx::Assert(mathcursor);
759 if (mathcursor->selection() || was_selection)
760 toggleInsetSelection(bv);
762 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
763 result == UNDISPATCHED) {
768 releaseMathCursor(bv);
769 bv->unlockInset(this);
772 return result; // original version
776 void InsetFormulaBase::revealCodes(BufferView * bv) const
780 bv->owner()->message(mathcursor->info());
783 // write something to the minibuffer
784 // translate to latex
785 mathcursor->markInsert();
788 string str = os.str();
789 mathcursor->markErase();
790 string::size_type pos = 0;
792 for (string::iterator it = str.begin(); it != str.end(); ++it) {
795 else if (*it == '\0') {
797 pos = it - str.begin();
803 res = res.substr(pos - 30);
805 res = res.substr(0, 60);
806 bv->owner()->message(res);
811 Inset::Code InsetFormulaBase::lyxCode() const
813 return Inset::MATH_CODE;
817 int InsetFormulaBase::ylow() const
819 return yo_ - ascent(view(), font_);
823 int InsetFormulaBase::yhigh() const
825 return yo_ + descent(view(), font_);
829 int InsetFormulaBase::xlow() const
835 int InsetFormulaBase::xhigh() const
837 return xo_ + width(view(), font_);
841 /////////////////////////////////////////////////////////////////////
844 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
850 static InsetFormulaBase * lastformula = 0;
851 static MathIterator current = MathIterator(ibegin(par().nucleus()));
853 static string laststr;
855 if (lastformula != this || laststr != str) {
856 //lyxerr << "reset lastformula to " << this << "\n";
859 current = ibegin(par().nucleus());
861 mathed_parse_cell(ar, str);
865 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
867 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
868 if (it.cell().matchpart(ar, it.back().pos_)) {
869 bv->unlockInset(bv->theLockingInset());
870 if (!bv->lockInset(this)) {
871 lyxerr << "Cannot lock inset" << endl;
875 mathcursor = new MathCursor(this, true);
877 mathcursor->setSelection(it, ar.size());
880 updateLocal(bv, false);
885 //lyxerr << "not found!\n";
891 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
894 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
895 return searchForward(bv, what, a, b);
899 bool InsetFormulaBase::display() const
901 return par()->asHullInset() && par()->asHullInset()->display();
905 /////////////////////////////////////////////////////////////////////
908 void mathDispatchCreation(FuncRequest const & cmd, bool display)
910 BufferView * bv = cmd.view();
911 // use selection if available..
913 //if (action == LFUN_MATH_IMPORT_SELECTION)
917 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
920 InsetFormula * f = new InsetFormula(bv);
921 if (openNewInset(bv, f)) {
922 bv->theLockingInset()->
923 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
924 // don't do that also for LFUN_MATH_MODE unless you want end up with
925 // always changing to mathrm when opening an inlined inset
926 // -- I really hate "LyXfunc overloading"...
928 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
929 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
932 // create a macro if we see "\\newcommand" somewhere, and an ordinary
934 InsetFormulaBase * f;
935 if (sel.find("\\newcommand") == string::npos &&
936 sel.find("\\def") == string::npos)
937 f = new InsetFormula(sel);
939 f = new InsetFormulaMacro(sel);
940 bv->getLyXText()->cutSelection(bv);
943 cmd.message(N_("Math editor mode"));
947 void mathDispatch(FuncRequest const & cmd)
949 BufferView * bv = cmd.view();
950 if (!bv->available())
953 switch (cmd.action) {
955 case LFUN_MATH_DISPLAY:
956 mathDispatchCreation(cmd, true);
960 mathDispatchCreation(cmd, false);
963 case LFUN_MATH_IMPORT_SELECTION:
964 mathDispatchCreation(cmd, false);
967 case LFUN_MATH_MACRO:
968 if (cmd.argument.empty())
969 cmd.errorMessage(N_("Missing argument"));
971 string s = cmd.argument;
972 string const s1 = token(s, ' ', 1);
973 int const na = s1.empty() ? 0 : lyx::atoi(s1);
974 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
979 case LFUN_INSERT_MATH:
980 case LFUN_INSERT_MATRIX:
981 case LFUN_MATH_DELIM: {
982 InsetFormula * f = new InsetFormula(bv);
983 if (openNewInset(bv, f)) {
984 bv->theLockingInset()->
985 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
986 bv->theLockingInset()->localDispatch(cmd);