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 recordUndo(bv, Undo::ATOMIC);
129 if (mathcursor->inset()->name() == font)
130 mathcursor->handleFont(font);
132 mathcursor->handleNest(createMathInset(font));
133 mathcursor->insert(arg);
138 BufferView * InsetFormulaBase::view() const
144 void InsetFormulaBase::validate(LaTeXFeatures &) const
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::insetUnlock(BufferView * bv)
157 if (mathcursor->inMacroMode()) {
158 mathcursor->macroModeClose();
159 bv->updateInset(this);
161 releaseMathCursor(bv);
164 bv->updateInset(this);
168 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
170 mathcursor->getPos(x, y);
174 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 // calling metrics here destroys the cached xo,yo positions e.g. in
177 // MathParboxinset. And it would be too expensive anyway...
180 lyxerr << "getCursorPos - should not happen";
184 mathcursor->getPos(x, y);
185 x = mathcursor->targetX();
188 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
192 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
199 //math_font_max_dim(font_, asc, des);
200 getCursorPos(bv, x, y);
202 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
203 bv->fitLockedInsetCursor(x, y, asc, des);
207 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
210 bv->updateInset(this);
214 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
219 BufferView * bv = cmd.view();
220 bv->updateInset(this);
221 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
223 if (cmd.button() == mouse_button::button3) {
224 // try to dispatch to enclosed insets first
225 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
226 // launch math panel for right mouse button
227 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
228 bv->owner()->getDialogs().show("mathpanel");
233 if (cmd.button() == mouse_button::button2) {
235 asArray(bv->getClipboard(), ar);
236 mathcursor->selClear();
237 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
238 mathcursor->insert(ar);
239 bv->updateInset(this);
243 if (cmd.button() == mouse_button::button1) {
244 // try to dispatch to enclosed insets first
245 mathcursor->dispatch(cmd);
246 cmd.view()->stuffClipboard(mathcursor->grabSelection());
247 // try to set the cursor
249 //mathcursor = new MathCursor(this, x == 0);
251 //mathcursor->setPos(x + xo_, y + yo_);
259 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
261 BufferView * bv = cmd.view();
262 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
264 if (!mathcursor || mathcursor->formula() != this) {
265 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
266 releaseMathCursor(bv);
267 mathcursor = new MathCursor(this, cmd.x == 0);
269 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
272 if (cmd.button() == mouse_button::button3) {
273 mathcursor->dispatch(cmd);
277 if (cmd.button() == mouse_button::button1) {
280 mathcursor->selClear();
281 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
282 mathcursor->dispatch(cmd);
286 bv->updateInset(this);
291 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
296 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
299 // only select with button 1
300 if (cmd.button() != mouse_button::button1)
303 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
309 if (!mathcursor->selection())
310 mathcursor->selStart();
312 BufferView * bv = cmd.view();
313 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
314 bv->updateInset(this);
319 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
321 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
322 // << " arg: '" << cmd.argument
323 // << " x: '" << cmd.x
324 // << " y: '" << cmd.y
325 // << "' button: " << cmd.button() << endl;
327 BufferView * bv = cmd.view();
329 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
330 bool remove_inset = false;
332 switch (cmd.action) {
333 case LFUN_INSET_EDIT:
334 lyxerr << "Called EDIT with '" << cmd.argument << "'\n";
335 if (!bv->lockInset(this))
336 lyxerr << "Cannot lock math inset in edit call!\n";
337 releaseMathCursor(bv);
338 if (!cmd.argument.empty()) {
339 mathcursor = new MathCursor(this, cmd.argument == "left");
342 mathcursor = new MathCursor(this, true);
344 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
346 // if that is removed, we won't get the magenta box when entering an
347 // inset for the first time
348 bv->updateInset(this);
351 case LFUN_MOUSE_PRESS:
352 //lyxerr << "Mouse single press\n";
353 return lfunMousePress(cmd);
354 case LFUN_MOUSE_MOTION:
355 //lyxerr << "Mouse motion\n";
356 return lfunMouseMotion(cmd);
357 case LFUN_MOUSE_RELEASE:
358 //lyxerr << "Mouse single release\n";
359 return lfunMouseRelease(cmd);
360 case LFUN_MOUSE_DOUBLE:
361 //lyxerr << "Mouse double\n";
362 return localDispatch(FuncRequest(LFUN_WORDSEL));
370 string argument = cmd.argument;
371 RESULT result = DISPATCHED;
373 bool was_macro = mathcursor->inMacroMode();
374 bool was_selection = mathcursor->selection();
376 mathcursor->normalize();
379 switch (cmd.action) {
381 case LFUN_MATH_MUTATE:
382 case LFUN_MATH_DISPLAY:
383 case LFUN_MATH_NUMBER:
384 case LFUN_MATH_NONUMBER:
385 case LFUN_CELL_SPLIT:
387 case LFUN_DELETE_LINE_FORWARD:
388 case LFUN_INSERT_LABEL:
389 case LFUN_MATH_EXTERN:
390 case LFUN_TABULAR_FEATURE:
391 case LFUN_PASTESELECTION:
392 case LFUN_MATH_LIMITS:
393 recordUndo(bv, Undo::ATOMIC);
394 mathcursor->dispatch(cmd);
398 sel = true; // fall through...
400 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
401 //lyxerr << "calling scroll 20\n";
403 // write something to the minibuffer
404 //bv->owner()->message(mathcursor->info());
408 sel = true; // fall through
410 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
414 sel = true; // fall through
416 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
420 sel = true; // fall through
422 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
426 mathcursor->home(false);
427 mathcursor->end(true);
430 case LFUN_UP_PARAGRAPHSEL:
431 case LFUN_UP_PARAGRAPH:
432 case LFUN_DOWN_PARAGRAPHSEL:
433 case LFUN_DOWN_PARAGRAPH:
438 case LFUN_WORDLEFTSEL:
439 sel = true; // fall through
442 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
446 case LFUN_WORDRIGHTSEL:
447 sel = true; // fall through
450 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
455 case LFUN_BEGINNINGBUFSEL:
456 case LFUN_BEGINNINGBUF:
464 result = FINISHED_RIGHT;
467 case LFUN_CELL_FORWARD:
468 mathcursor->idxNext();
471 case LFUN_CELL_BACKWARD:
472 mathcursor->idxPrev();
475 case LFUN_DELETE_WORD_BACKWARD:
477 recordUndo(bv, Undo::ATOMIC);
478 if (!mathcursor->backspace()) {
484 case LFUN_DELETE_WORD_FORWARD:
486 recordUndo(bv, Undo::ATOMIC);
487 if (!mathcursor->erase()) {
494 // sprintf(dispatch_buffer, "%d %d",);
495 // dispatch_result = dispatch_buffer;
498 lyxerr << "LFUN_SETXY broken!\n";
501 istringstream is(cmd.argument.c_str());
503 mathcursor->setPos(x, y);
509 istringstream is(cmd.argument.c_str());
512 mathcursor->macroModeClose();
513 recordUndo(bv, Undo::ATOMIC);
514 mathcursor->selPaste(n);
519 recordUndo(bv, Undo::DELETE);
520 mathcursor->selCut();
524 mathcursor->selCopy();
528 // Special casing for superscript in case of LyX handling
530 case LFUN_CIRCUMFLEX:
531 if (cmd.argument.empty()) {
532 // do superscript if LyX handles
534 recordUndo(bv, Undo::ATOMIC);
535 mathcursor->script(true);
552 case LFUN_HUNG_UMLAUT:
556 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
557 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
558 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
559 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
560 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
561 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
562 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
563 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
564 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
565 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
568 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
569 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
571 handleFont(bv, cmd.argument, "textrm");
572 //bv->owner()->message(_("math text mode toggled"));
578 recordUndo(bv, Undo::ATOMIC);
579 mathcursor->setSize(arg);
584 case LFUN_INSERT_MATRIX: {
585 recordUndo(bv, Undo::ATOMIC);
590 istringstream is(STRCONV(argument));
591 is >> m >> n >> v_align >> h_align;
595 mathcursor->niceInsert(
596 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
600 case LFUN_SUPERSCRIPT:
603 recordUndo(bv, Undo::ATOMIC);
604 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
608 case LFUN_MATH_DELIM:
610 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
612 string rs = split(cmd.argument, ls, ' ');
613 // Reasonable default values
619 recordUndo(bv, Undo::ATOMIC);
620 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
624 case LFUN_SPACE_INSERT:
625 case LFUN_MATH_SPACE:
626 recordUndo(bv, Undo::ATOMIC);
627 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
631 bv->owner()->message(_("Invalid action in math mode!"));
635 case LFUN_EXEC_COMMAND:
636 result = UNDISPATCHED;
640 // interpret this as if a backslash was typed
641 recordUndo(bv, Undo::ATOMIC);
642 mathcursor->interpret('\\');
645 case LFUN_BREAKPARAGRAPH:
646 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
647 case LFUN_BREAKPARAGRAPH_SKIP:
651 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
652 // handling such that "self-insert" works on "arbitrary stuff" too, and
653 // math-insert only handles special math things like "matrix".
654 case LFUN_INSERT_MATH:
655 recordUndo(bv, Undo::ATOMIC);
656 mathcursor->niceInsert(argument);
660 case LFUN_SELFINSERT:
661 if (!argument.empty()) {
662 recordUndo(bv, Undo::ATOMIC);
663 if (argument.size() == 1)
664 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
666 mathcursor->insert(argument);
671 if (mathcursor->selection())
672 mathcursor->selClear();
674 result = UNDISPATCHED;
677 case LFUN_INSET_TOGGLE:
678 mathcursor->insetToggle();
681 case LFUN_DIALOG_SHOW:
682 result = UNDISPATCHED;
685 case LFUN_DIALOG_SHOW_NEW_INSET: {
686 string const & name = argument;
690 data = tmp.createDialogStr(name);
694 result = UNDISPATCHED;
696 bv->owner()->getDialogs().show(name, data, 0);
701 case LFUN_INSET_APPLY: {
702 string const name = cmd.getArg(0);
704 bv->owner()->getDialogs().getOpenInset(name);
707 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
708 result = base->localDispatch(fr);
711 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
712 mathcursor->insert(ar);
715 result = UNDISPATCHED;
722 result = UNDISPATCHED;
725 if (result == DISPATCHED)
726 bv->updateInset(this);
728 mathcursor->normalize();
733 if (mathcursor->selection() || was_selection)
734 toggleInsetSelection(bv);
736 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
737 result == UNDISPATCHED) {
740 cmd.view()->stuffClipboard(mathcursor->grabSelection());
742 releaseMathCursor(bv);
743 bv->unlockInset(this);
745 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
748 return result; // original version
752 void InsetFormulaBase::revealCodes(BufferView * bv) const
756 bv->owner()->message(mathcursor->info());
759 // write something to the minibuffer
760 // translate to latex
761 mathcursor->markInsert();
764 string str = os.str();
765 mathcursor->markErase();
766 string::size_type pos = 0;
768 for (string::iterator it = str.begin(); it != str.end(); ++it) {
771 else if (*it == '\0') {
773 pos = it - str.begin();
779 res = res.substr(pos - 30);
781 res = res.substr(0, 60);
782 bv->owner()->message(res);
787 InsetOld::Code InsetFormulaBase::lyxCode() const
789 return InsetOld::MATH_CODE;
793 int InsetFormulaBase::ylow() const
795 return yo_ - dim_.asc;
799 int InsetFormulaBase::yhigh() const
801 return yo_ + dim_.des;
805 int InsetFormulaBase::xlow() const
811 int InsetFormulaBase::xhigh() const
813 return xo_ + dim_.wid;
817 /////////////////////////////////////////////////////////////////////
820 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
826 static InsetFormulaBase * lastformula = 0;
827 static MathIterator current = MathIterator(ibegin(par().nucleus()));
829 static string laststr;
831 if (lastformula != this || laststr != str) {
832 //lyxerr << "reset lastformula to " << this << "\n";
835 current = ibegin(par().nucleus());
837 mathed_parse_cell(ar, str);
841 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
843 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
844 if (it.cell().matchpart(ar, it.back().pos_)) {
845 bv->unlockInset(bv->theLockingInset());
846 if (!bv->lockInset(this)) {
847 lyxerr << "Cannot lock inset" << endl;
851 mathcursor = new MathCursor(this, true);
853 mathcursor->setSelection(it, ar.size());
856 bv->updateInset(this);
861 //lyxerr << "not found!\n";
867 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
870 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
871 return searchForward(bv, what, a, b);
875 bool InsetFormulaBase::display() const
877 return par()->asHullInset() && par()->asHullInset()->display();
881 string InsetFormulaBase::selectionAsString() const
883 return mathcursor ? mathcursor->grabSelection() : string();
886 /////////////////////////////////////////////////////////////////////
889 void mathDispatchCreation(FuncRequest const & cmd, bool display)
891 BufferView * bv = cmd.view();
892 // use selection if available..
894 //if (action == LFUN_MATH_IMPORT_SELECTION)
898 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
901 InsetFormula * f = new InsetFormula(bv);
902 if (openNewInset(bv, f)) {
903 bv->theLockingInset()->
904 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
905 // don't do that also for LFUN_MATH_MODE unless you want end up with
906 // always changing to mathrm when opening an inlined inset
907 // -- I really hate "LyXfunc overloading"...
909 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
910 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
913 // create a macro if we see "\\newcommand" somewhere, and an ordinary
915 InsetFormulaBase * f;
916 if (sel.find("\\newcommand") == string::npos &&
917 sel.find("\\def") == string::npos)
918 f = new InsetFormula(sel);
920 f = new InsetFormulaMacro(sel);
921 bv->getLyXText()->cutSelection(true, false);
924 cmd.message(N_("Math editor mode"));
928 void mathDispatch(FuncRequest const & cmd)
930 BufferView * bv = cmd.view();
931 if (!bv->available())
934 switch (cmd.action) {
936 case LFUN_MATH_DISPLAY:
937 mathDispatchCreation(cmd, true);
941 mathDispatchCreation(cmd, false);
944 case LFUN_MATH_IMPORT_SELECTION:
945 mathDispatchCreation(cmd, false);
948 case LFUN_MATH_MACRO:
949 if (cmd.argument.empty())
950 cmd.errorMessage(N_("Missing argument"));
952 string s = cmd.argument;
953 string const s1 = token(s, ' ', 1);
954 int const nargs = s1.empty() ? 0 : atoi(s1);
955 string const s2 = token(s, ' ', 2);
956 string const type = s2.empty() ? "newcommand" : s2;
957 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
961 case LFUN_INSERT_MATH:
962 case LFUN_INSERT_MATRIX:
963 case LFUN_MATH_DELIM: {
964 InsetFormula * f = new InsetFormula(bv);
965 if (openNewInset(bv, f)) {
966 bv->theLockingInset()->
967 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
968 bv->theLockingInset()->localDispatch(cmd);