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.
18 #include "support/LAssert.h"
20 #include "formulamacro.h"
22 #include "funcrequest.h"
23 #include "BufferView.h"
26 #include "LaTeXFeatures.h"
28 #include "math_support.h"
29 #include "metricsinfo.h"
30 #include "math_data.h"
31 #include "support/lstrings.h"
32 #include "support/lyxlib.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/mouse_state.h"
36 #include "math_arrayinset.h"
37 #include "math_charinset.h"
38 #include "math_deliminset.h"
39 #include "math_cursor.h"
40 #include "math_factory.h"
41 #include "math_fontinset.h"
42 #include "math_hullinset.h"
43 #include "math_iterator.h"
44 #include "math_macrotable.h"
45 #include "math_parser.h"
47 #include "math_spaceinset.h"
48 #include "undo_funcs.h"
49 #include "textpainter.h"
50 #include "frontends/Dialogs.h"
52 #include "ref_inset.h"
56 using namespace lyx::support;
65 MathCursor * mathcursor = 0;
73 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
75 if (!bv->insertInset(new_inset)) {
79 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
88 InsetFormulaBase::InsetFormulaBase()
91 // This is needed as long the math parser is not re-entrant
93 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
94 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << "\n";
95 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
96 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
100 // simply scrap this function if you want
101 void InsetFormulaBase::mutateToText()
104 // translate to latex
106 latex(NULL, os, false, false);
107 string str = os.str();
110 LyXText * lt = view_->getLyXText();
111 string::const_iterator cit = str.begin();
112 string::const_iterator end = str.end();
113 for (; cit != end; ++cit)
114 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
117 //view_->owner()->dispatch(LFUN_ESCAPE);
122 void InsetFormulaBase::handleFont
123 (BufferView * bv, string const & arg, string const & font)
125 // this whole function is a hack and won't work for incremental font
127 bv->lockedInsetStoreUndo(Undo::EDIT);
128 if (mathcursor->inset()->name() == font)
129 mathcursor->handleFont(font);
131 mathcursor->handleNest(createMathInset(font));
132 mathcursor->insert(arg);
137 BufferView * InsetFormulaBase::view() const
143 void InsetFormulaBase::validate(LaTeXFeatures &) const
147 string const InsetFormulaBase::editMessage() const
149 return _("Math editor mode");
153 void InsetFormulaBase::insetUnlock(BufferView * bv)
156 if (mathcursor->inMacroMode()) {
157 mathcursor->macroModeClose();
158 bv->updateInset(this);
160 releaseMathCursor(bv);
163 bv->updateInset(this);
167 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
169 mathcursor->getPos(x, y);
173 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
175 // calling metrics here destroys the cached xo,yo positions e.g. in
176 // MathParboxinset. And it would be too expensive anyway...
179 lyxerr << "getCursorPos - should not happen";
183 mathcursor->getPos(x, y);
184 x = mathcursor->targetX();
187 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
191 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
198 //math_font_max_dim(font_, asc, des);
199 getCursorPos(bv, x, y);
201 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
202 bv->fitLockedInsetCursor(x, y, asc, des);
206 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
209 bv->updateInset(this);
213 vector<string> const InsetFormulaBase::getLabelList() const
215 return vector<string>();
219 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
224 BufferView * bv = cmd.view();
225 bv->updateInset(this);
226 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
228 if (cmd.button() == mouse_button::button3) {
229 // try to dispatch to enclosed insets first
230 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
231 // launch math panel for right mouse button
232 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
233 bv->owner()->getDialogs().show("mathpanel");
238 if (cmd.button() == mouse_button::button2) {
240 asArray(bv->getClipboard(), ar);
241 mathcursor->selClear();
242 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
243 mathcursor->insert(ar);
244 bv->updateInset(this);
248 if (cmd.button() == mouse_button::button1) {
249 // try to dispatch to enclosed insets first
250 mathcursor->dispatch(cmd);
251 cmd.view()->stuffClipboard(mathcursor->grabSelection());
252 // try to set the cursor
254 //mathcursor = new MathCursor(this, x == 0);
256 //mathcursor->setPos(x + xo_, y + yo_);
264 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
266 BufferView * bv = cmd.view();
267 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
269 if (!mathcursor || mathcursor->formula() != this) {
270 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
271 releaseMathCursor(bv);
272 mathcursor = new MathCursor(this, cmd.x == 0);
274 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
277 if (cmd.button() == mouse_button::button3) {
278 mathcursor->dispatch(cmd);
282 if (cmd.button() == mouse_button::button1) {
285 mathcursor->selClear();
286 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
287 mathcursor->dispatch(cmd);
291 bv->updateInset(this);
296 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
301 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
304 // only select with button 1
305 if (cmd.button() != mouse_button::button1)
308 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
314 if (!mathcursor->selection())
315 mathcursor->selStart();
317 BufferView * bv = cmd.view();
318 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
319 bv->updateInset(this);
324 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
326 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
327 // << " arg: '" << cmd.argument
328 // << " x: '" << cmd.x
329 // << " y: '" << cmd.y
330 // << "' button: " << cmd.button() << endl;
332 BufferView * bv = cmd.view();
334 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
335 bool remove_inset = false;
337 switch (cmd.action) {
338 case LFUN_INSET_EDIT:
339 lyxerr << "Called EDIT with '" << cmd.argument << "'\n";
340 if (!bv->lockInset(this))
341 lyxerr << "Cannot lock math inset in edit call!\n";
342 releaseMathCursor(bv);
343 if (!cmd.argument.empty()) {
344 mathcursor = new MathCursor(this, cmd.argument == "left");
347 mathcursor = new MathCursor(this, true);
349 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
351 // if that is removed, we won't get the magenta box when entering an
352 // inset for the first time
353 bv->updateInset(this);
356 case LFUN_MOUSE_PRESS:
357 //lyxerr << "Mouse single press\n";
358 return lfunMousePress(cmd);
359 case LFUN_MOUSE_MOTION:
360 //lyxerr << "Mouse motion\n";
361 return lfunMouseMotion(cmd);
362 case LFUN_MOUSE_RELEASE:
363 //lyxerr << "Mouse single release\n";
364 return lfunMouseRelease(cmd);
365 case LFUN_MOUSE_DOUBLE:
366 //lyxerr << "Mouse double\n";
367 return localDispatch(FuncRequest(LFUN_WORDSEL));
375 string argument = cmd.argument;
376 RESULT result = DISPATCHED;
378 bool was_macro = mathcursor->inMacroMode();
379 bool was_selection = mathcursor->selection();
381 mathcursor->normalize();
384 switch (cmd.action) {
386 case LFUN_MATH_MUTATE:
387 case LFUN_MATH_DISPLAY:
388 case LFUN_MATH_NUMBER:
389 case LFUN_MATH_NONUMBER:
390 case LFUN_CELL_SPLIT:
392 case LFUN_DELETE_LINE_FORWARD:
393 case LFUN_INSERT_LABEL:
394 case LFUN_MATH_EXTERN:
395 case LFUN_TABULAR_FEATURE:
396 case LFUN_PASTESELECTION:
397 case LFUN_MATH_LIMITS:
398 bv->lockedInsetStoreUndo(Undo::EDIT);
399 mathcursor->dispatch(cmd);
403 sel = true; // fall through...
405 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
406 //lyxerr << "calling scroll 20\n";
408 // write something to the minibuffer
409 //bv->owner()->message(mathcursor->info());
413 sel = true; // fall through
415 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
419 sel = true; // fall through
421 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
425 sel = true; // fall through
427 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
431 mathcursor->home(false);
432 mathcursor->end(true);
435 case LFUN_UP_PARAGRAPHSEL:
436 case LFUN_UP_PARAGRAPH:
437 case LFUN_DOWN_PARAGRAPHSEL:
438 case LFUN_DOWN_PARAGRAPH:
443 case LFUN_WORDLEFTSEL:
444 sel = true; // fall through
447 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
451 case LFUN_WORDRIGHTSEL:
452 sel = true; // fall through
455 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
460 case LFUN_BEGINNINGBUFSEL:
461 case LFUN_BEGINNINGBUF:
469 result = FINISHED_RIGHT;
472 case LFUN_CELL_FORWARD:
473 mathcursor->idxNext();
476 case LFUN_CELL_BACKWARD:
477 mathcursor->idxPrev();
480 case LFUN_DELETE_WORD_BACKWARD:
482 bv->lockedInsetStoreUndo(Undo::EDIT);
483 if (!mathcursor->backspace()) {
489 case LFUN_DELETE_WORD_FORWARD:
491 bv->lockedInsetStoreUndo(Undo::EDIT);
492 if (!mathcursor->erase()) {
499 // sprintf(dispatch_buffer, "%d %d",);
500 // dispatch_result = dispatch_buffer;
503 lyxerr << "LFUN_SETXY broken!\n";
506 istringstream is(cmd.argument.c_str());
508 mathcursor->setPos(x, y);
514 istringstream is(cmd.argument.c_str());
517 mathcursor->macroModeClose();
518 bv->lockedInsetStoreUndo(Undo::EDIT);
519 mathcursor->selPaste(n);
524 bv->lockedInsetStoreUndo(Undo::DELETE);
525 mathcursor->selCut();
529 mathcursor->selCopy();
533 // Special casing for superscript in case of LyX handling
535 case LFUN_CIRCUMFLEX:
536 if (cmd.argument.empty()) {
537 // do superscript if LyX handles
539 bv->lockedInsetStoreUndo(Undo::EDIT);
540 mathcursor->script(true);
557 case LFUN_HUNG_UMLAUT:
561 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
562 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
563 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
564 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
565 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
566 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
567 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
568 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
569 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
570 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
573 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
574 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
576 handleFont(bv, cmd.argument, "textrm");
577 //bv->owner()->message(_("math text mode toggled"));
583 bv->lockedInsetStoreUndo(Undo::EDIT);
584 mathcursor->setSize(arg);
589 case LFUN_INSERT_MATRIX: {
590 bv->lockedInsetStoreUndo(Undo::EDIT);
595 istringstream is(STRCONV(argument));
596 is >> m >> n >> v_align >> h_align;
600 mathcursor->niceInsert(
601 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
605 case LFUN_SUPERSCRIPT:
608 bv->lockedInsetStoreUndo(Undo::EDIT);
609 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
613 case LFUN_MATH_DELIM:
615 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
617 string rs = split(cmd.argument, ls, ' ');
618 // Reasonable default values
624 bv->lockedInsetStoreUndo(Undo::EDIT);
625 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
629 case LFUN_SPACE_INSERT:
630 case LFUN_MATH_SPACE:
631 bv->lockedInsetStoreUndo(Undo::EDIT);
632 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
636 bv->owner()->message(_("Invalid action in math mode!"));
640 case LFUN_EXEC_COMMAND:
641 result = UNDISPATCHED;
645 // interpret this as if a backslash was typed
646 bv->lockedInsetStoreUndo(Undo::EDIT);
647 mathcursor->interpret('\\');
650 case LFUN_BREAKPARAGRAPH:
651 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
652 case LFUN_BREAKPARAGRAPH_SKIP:
656 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
657 // handling such that "self-insert" works on "arbitrary stuff" too, and
658 // math-insert only handles special math things like "matrix".
659 case LFUN_INSERT_MATH:
660 bv->lockedInsetStoreUndo(Undo::EDIT);
661 mathcursor->niceInsert(argument);
665 case LFUN_SELFINSERT:
666 if (!argument.empty()) {
667 bv->lockedInsetStoreUndo(Undo::EDIT);
668 if (argument.size() == 1)
669 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
671 mathcursor->insert(argument);
676 if (mathcursor->selection())
677 mathcursor->selClear();
679 result = UNDISPATCHED;
682 case LFUN_INSET_TOGGLE:
683 mathcursor->insetToggle();
686 case LFUN_DIALOG_SHOW:
687 if (argument == "mathpanel")
688 result = UNDISPATCHED;
691 case LFUN_DIALOG_SHOW_NEW_INSET: {
692 string const & name = argument;
696 data = tmp.createDialogStr(name);
700 result = UNDISPATCHED;
702 bv->owner()->getDialogs().show(name, data, 0);
707 case LFUN_INSET_APPLY: {
708 string const name = cmd.getArg(0);
710 bv->owner()->getDialogs().getOpenInset(name);
713 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
714 result = base->localDispatch(fr);
717 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
718 mathcursor->insert(ar);
721 result = UNDISPATCHED;
728 result = UNDISPATCHED;
731 if (result == DISPATCHED)
732 bv->updateInset(this);
734 mathcursor->normalize();
739 if (mathcursor->selection() || was_selection)
740 toggleInsetSelection(bv);
742 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
743 result == UNDISPATCHED) {
746 cmd.view()->stuffClipboard(mathcursor->grabSelection());
748 releaseMathCursor(bv);
749 bv->unlockInset(this);
751 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
754 return result; // original version
758 void InsetFormulaBase::revealCodes(BufferView * bv) const
762 bv->owner()->message(mathcursor->info());
765 // write something to the minibuffer
766 // translate to latex
767 mathcursor->markInsert();
770 string str = os.str();
771 mathcursor->markErase();
772 string::size_type pos = 0;
774 for (string::iterator it = str.begin(); it != str.end(); ++it) {
777 else if (*it == '\0') {
779 pos = it - str.begin();
785 res = res.substr(pos - 30);
787 res = res.substr(0, 60);
788 bv->owner()->message(res);
793 Inset::Code InsetFormulaBase::lyxCode() const
795 return Inset::MATH_CODE;
799 int InsetFormulaBase::ylow() const
801 return yo_ - dim_.asc;
805 int InsetFormulaBase::yhigh() const
807 return yo_ + dim_.des;
811 int InsetFormulaBase::xlow() const
817 int InsetFormulaBase::xhigh() const
819 return xo_ + dim_.wid;
823 /////////////////////////////////////////////////////////////////////
826 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
832 static InsetFormulaBase * lastformula = 0;
833 static MathIterator current = MathIterator(ibegin(par().nucleus()));
835 static string laststr;
837 if (lastformula != this || laststr != str) {
838 //lyxerr << "reset lastformula to " << this << "\n";
841 current = ibegin(par().nucleus());
843 mathed_parse_cell(ar, str);
847 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
849 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
850 if (it.cell().matchpart(ar, it.back().pos_)) {
851 bv->unlockInset(bv->theLockingInset());
852 if (!bv->lockInset(this)) {
853 lyxerr << "Cannot lock inset" << endl;
857 mathcursor = new MathCursor(this, true);
859 mathcursor->setSelection(it, ar.size());
862 bv->updateInset(this);
867 //lyxerr << "not found!\n";
873 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
876 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
877 return searchForward(bv, what, a, b);
881 bool InsetFormulaBase::display() const
883 return par()->asHullInset() && par()->asHullInset()->display();
887 string InsetFormulaBase::selectionAsString() const
889 return mathcursor ? mathcursor->grabSelection() : string();
892 /////////////////////////////////////////////////////////////////////
895 void mathDispatchCreation(FuncRequest const & cmd, bool display)
897 BufferView * bv = cmd.view();
898 // use selection if available..
900 //if (action == LFUN_MATH_IMPORT_SELECTION)
904 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
907 InsetFormula * f = new InsetFormula(bv);
908 if (openNewInset(bv, f)) {
909 bv->theLockingInset()->
910 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
911 // don't do that also for LFUN_MATH_MODE unless you want end up with
912 // always changing to mathrm when opening an inlined inset
913 // -- I really hate "LyXfunc overloading"...
915 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
916 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
919 // create a macro if we see "\\newcommand" somewhere, and an ordinary
921 InsetFormulaBase * f;
922 if (sel.find("\\newcommand") == string::npos &&
923 sel.find("\\def") == string::npos)
924 f = new InsetFormula(sel);
926 f = new InsetFormulaMacro(sel);
927 bv->getLyXText()->cutSelection(true, false);
930 cmd.message(N_("Math editor mode"));
934 void mathDispatch(FuncRequest const & cmd)
936 BufferView * bv = cmd.view();
937 if (!bv->available())
940 switch (cmd.action) {
942 case LFUN_MATH_DISPLAY:
943 mathDispatchCreation(cmd, true);
947 mathDispatchCreation(cmd, false);
950 case LFUN_MATH_IMPORT_SELECTION:
951 mathDispatchCreation(cmd, false);
954 case LFUN_MATH_MACRO:
955 if (cmd.argument.empty())
956 cmd.errorMessage(N_("Missing argument"));
958 string s = cmd.argument;
959 string const s1 = token(s, ' ', 1);
960 int const na = s1.empty() ? 0 : atoi(s1);
961 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
965 case LFUN_INSERT_MATH:
966 case LFUN_INSERT_MATRIX:
967 case LFUN_MATH_DELIM: {
968 InsetFormula * f = new InsetFormula(bv);
969 if (openNewInset(bv, f)) {
970 bv->theLockingInset()->
971 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
972 bv->theLockingInset()->localDispatch(cmd);