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 "frontends/LyXView.h"
33 #include "frontends/font_metrics.h"
34 #include "frontends/mouse_state.h"
35 #include "math_arrayinset.h"
36 #include "math_charinset.h"
37 #include "math_deliminset.h"
38 #include "math_cursor.h"
39 #include "math_factory.h"
40 #include "math_fontinset.h"
41 #include "math_hullinset.h"
42 #include "math_iterator.h"
43 #include "math_macrotable.h"
44 #include "math_parser.h"
46 #include "math_spaceinset.h"
47 #include "undo_funcs.h"
48 #include "textpainter.h"
49 #include "frontends/Dialogs.h"
51 #include "ref_inset.h"
62 MathCursor * mathcursor = 0;
70 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
72 if (!bv->insertInset(new_inset)) {
76 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
85 InsetFormulaBase::InsetFormulaBase()
88 // This is needed as long the math parser is not re-entrant
90 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
91 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << "\n";
92 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
93 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
97 // simply scrap this function if you want
98 void InsetFormulaBase::mutateToText()
101 // translate to latex
103 latex(NULL, os, false, false);
104 string str = os.str();
107 LyXText * lt = view_->getLyXText();
108 string::const_iterator cit = str.begin();
109 string::const_iterator end = str.end();
110 for (; cit != end; ++cit)
111 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
114 //view_->owner()->dispatch(LFUN_ESCAPE);
119 void InsetFormulaBase::handleFont
120 (BufferView * bv, string const & arg, string const & font)
122 // this whole function is a hack and won't work for incremental font
124 bv->lockedInsetStoreUndo(Undo::EDIT);
125 if (mathcursor->inset()->name() == font)
126 mathcursor->handleFont(font);
128 mathcursor->handleNest(createMathInset(font));
129 mathcursor->insert(arg);
134 BufferView * InsetFormulaBase::view() const
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
144 string const InsetFormulaBase::editMessage() const
146 return _("Math editor mode");
150 void InsetFormulaBase::insetUnlock(BufferView * bv)
153 if (mathcursor->inMacroMode()) {
154 mathcursor->macroModeClose();
155 bv->updateInset(this);
157 releaseMathCursor(bv);
160 bv->updateInset(this);
164 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
166 mathcursor->getPos(x, y);
170 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
172 // calling metrics here destroys the cached xo,yo positions e.g. in
173 // MathParboxinset. And it would be too expensive anyway...
176 lyxerr << "getCursorPos - should not happen";
180 mathcursor->getPos(x, y);
181 x = mathcursor->targetX();
184 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
188 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
195 //math_font_max_dim(font_, asc, des);
196 getCursorPos(bv, x, y);
198 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
199 bv->fitLockedInsetCursor(x, y, asc, des);
203 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
206 bv->updateInset(this);
210 vector<string> const InsetFormulaBase::getLabelList() const
212 return vector<string>();
216 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
221 BufferView * bv = cmd.view();
222 bv->updateInset(this);
223 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
225 if (cmd.button() == mouse_button::button3) {
226 // try to dispatch to enclosed insets first
227 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
228 // launch math panel for right mouse button
229 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
230 bv->owner()->getDialogs().show("mathpanel");
235 if (cmd.button() == mouse_button::button2) {
237 asArray(bv->getClipboard(), ar);
238 mathcursor->selClear();
239 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
240 mathcursor->insert(ar);
241 bv->updateInset(this);
245 if (cmd.button() == mouse_button::button1) {
246 // try to dispatch to enclosed insets first
247 mathcursor->dispatch(cmd);
248 cmd.view()->stuffClipboard(mathcursor->grabSelection());
249 // try to set the cursor
251 //mathcursor = new MathCursor(this, x == 0);
253 //mathcursor->setPos(x + xo_, y + yo_);
261 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
263 BufferView * bv = cmd.view();
264 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
266 if (!mathcursor || mathcursor->formula() != this) {
267 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
268 releaseMathCursor(bv);
269 mathcursor = new MathCursor(this, cmd.x == 0);
271 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
274 if (cmd.button() == mouse_button::button3) {
275 mathcursor->dispatch(cmd);
279 if (cmd.button() == mouse_button::button1) {
282 mathcursor->selClear();
283 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
284 mathcursor->dispatch(cmd);
288 bv->updateInset(this);
293 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
298 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
301 // only select with button 1
302 if (cmd.button() != mouse_button::button1)
305 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
311 if (!mathcursor->selection())
312 mathcursor->selStart();
314 BufferView * bv = cmd.view();
315 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
316 bv->updateInset(this);
321 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
323 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
324 // << " arg: '" << cmd.argument
325 // << " x: '" << cmd.x
326 // << " y: '" << cmd.y
327 // << "' button: " << cmd.button() << endl;
329 BufferView * bv = cmd.view();
331 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
332 bool remove_inset = false;
334 switch (cmd.action) {
335 case LFUN_INSET_EDIT:
336 lyxerr << "Called EDIT with '" << cmd.argument << "'\n";
337 if (!bv->lockInset(this))
338 lyxerr << "Cannot lock math inset in edit call!\n";
339 releaseMathCursor(bv);
340 if (!cmd.argument.empty()) {
341 mathcursor = new MathCursor(this, cmd.argument == "left");
344 mathcursor = new MathCursor(this, true);
346 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
348 // if that is removed, we won't get the magenta box when entering an
349 // inset for the first time
350 bv->updateInset(this);
353 case LFUN_MOUSE_PRESS:
354 //lyxerr << "Mouse single press\n";
355 return lfunMousePress(cmd);
356 case LFUN_MOUSE_MOTION:
357 //lyxerr << "Mouse motion\n";
358 return lfunMouseMotion(cmd);
359 case LFUN_MOUSE_RELEASE:
360 //lyxerr << "Mouse single release\n";
361 return lfunMouseRelease(cmd);
362 case LFUN_MOUSE_DOUBLE:
363 //lyxerr << "Mouse double\n";
364 return localDispatch(FuncRequest(LFUN_WORDSEL));
372 string argument = cmd.argument;
373 RESULT result = DISPATCHED;
375 bool was_macro = mathcursor->inMacroMode();
376 bool was_selection = mathcursor->selection();
378 mathcursor->normalize();
381 switch (cmd.action) {
383 case LFUN_MATH_MUTATE:
384 case LFUN_MATH_DISPLAY:
385 case LFUN_MATH_NUMBER:
386 case LFUN_MATH_NONUMBER:
387 case LFUN_CELL_SPLIT:
389 case LFUN_DELETE_LINE_FORWARD:
390 case LFUN_INSERT_LABEL:
391 case LFUN_MATH_EXTERN:
392 case LFUN_TABULAR_FEATURE:
393 case LFUN_PASTESELECTION:
394 case LFUN_MATH_LIMITS:
395 bv->lockedInsetStoreUndo(Undo::EDIT);
396 mathcursor->dispatch(cmd);
400 sel = true; // fall through...
402 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
403 //lyxerr << "calling scroll 20\n";
405 // write something to the minibuffer
406 //bv->owner()->message(mathcursor->info());
410 sel = true; // fall through
412 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
416 sel = true; // fall through
418 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
422 sel = true; // fall through
424 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
428 mathcursor->home(false);
429 mathcursor->end(true);
432 case LFUN_UP_PARAGRAPHSEL:
433 case LFUN_UP_PARAGRAPH:
434 case LFUN_DOWN_PARAGRAPHSEL:
435 case LFUN_DOWN_PARAGRAPH:
440 case LFUN_WORDLEFTSEL:
441 sel = true; // fall through
444 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
448 case LFUN_WORDRIGHTSEL:
449 sel = true; // fall through
452 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
457 case LFUN_BEGINNINGBUFSEL:
458 case LFUN_BEGINNINGBUF:
466 result = FINISHED_RIGHT;
469 case LFUN_CELL_FORWARD:
470 mathcursor->idxNext();
473 case LFUN_CELL_BACKWARD:
474 mathcursor->idxPrev();
477 case LFUN_DELETE_WORD_BACKWARD:
479 bv->lockedInsetStoreUndo(Undo::EDIT);
480 if (!mathcursor->backspace()) {
486 case LFUN_DELETE_WORD_FORWARD:
488 bv->lockedInsetStoreUndo(Undo::EDIT);
489 if (!mathcursor->erase()) {
496 // sprintf(dispatch_buffer, "%d %d",);
497 // dispatch_result = dispatch_buffer;
500 lyxerr << "LFUN_SETXY broken!\n";
503 istringstream is(cmd.argument.c_str());
505 mathcursor->setPos(x, y);
511 istringstream is(cmd.argument.c_str());
514 mathcursor->macroModeClose();
515 bv->lockedInsetStoreUndo(Undo::EDIT);
516 mathcursor->selPaste(n);
521 bv->lockedInsetStoreUndo(Undo::DELETE);
522 mathcursor->selCut();
526 mathcursor->selCopy();
530 // Special casing for superscript in case of LyX handling
532 case LFUN_CIRCUMFLEX:
533 if (cmd.argument.empty()) {
534 // do superscript if LyX handles
536 bv->lockedInsetStoreUndo(Undo::EDIT);
537 mathcursor->script(true);
554 case LFUN_HUNG_UMLAUT:
558 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
559 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
560 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
561 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
562 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
563 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
564 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
565 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
566 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
567 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
570 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
571 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
573 handleFont(bv, cmd.argument, "textrm");
574 //bv->owner()->message(_("math text mode toggled"));
580 bv->lockedInsetStoreUndo(Undo::EDIT);
581 mathcursor->setSize(arg);
586 case LFUN_INSERT_MATRIX: {
587 bv->lockedInsetStoreUndo(Undo::EDIT);
592 istringstream is(STRCONV(argument));
593 is >> m >> n >> v_align >> h_align;
597 mathcursor->niceInsert(
598 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
602 case LFUN_SUPERSCRIPT:
605 bv->lockedInsetStoreUndo(Undo::EDIT);
606 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
610 case LFUN_MATH_DELIM:
612 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
614 string rs = split(cmd.argument, ls, ' ');
615 // Reasonable default values
621 bv->lockedInsetStoreUndo(Undo::EDIT);
622 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
626 case LFUN_SPACE_INSERT:
627 case LFUN_MATH_SPACE:
628 bv->lockedInsetStoreUndo(Undo::EDIT);
629 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
633 bv->owner()->message(_("Invalid action in math mode!"));
637 case LFUN_EXEC_COMMAND:
638 result = UNDISPATCHED;
642 // interpret this as if a backslash was typed
643 bv->lockedInsetStoreUndo(Undo::EDIT);
644 mathcursor->interpret('\\');
647 case LFUN_BREAKPARAGRAPH:
648 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
649 case LFUN_BREAKPARAGRAPH_SKIP:
653 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
654 // handling such that "self-insert" works on "arbitrary stuff" too, and
655 // math-insert only handles special math things like "matrix".
656 case LFUN_INSERT_MATH:
657 bv->lockedInsetStoreUndo(Undo::EDIT);
658 mathcursor->niceInsert(argument);
662 case LFUN_SELFINSERT:
663 if (!argument.empty()) {
664 bv->lockedInsetStoreUndo(Undo::EDIT);
665 if (argument.size() == 1)
666 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
668 mathcursor->insert(argument);
672 case LFUN_MATH_PANEL:
673 result = UNDISPATCHED;
677 if (mathcursor->selection())
678 mathcursor->selClear();
680 result = UNDISPATCHED;
683 case LFUN_INSET_TOGGLE:
684 mathcursor->insetToggle();
687 case LFUN_DIALOG_SHOW_NEW_INSET: {
688 string const & name = argument;
692 data = tmp.createDialogStr(name);
696 result = UNDISPATCHED;
698 bv->owner()->getDialogs().show(name, data, 0);
703 case LFUN_INSET_APPLY: {
704 string const name = cmd.getArg(0);
706 bv->owner()->getDialogs().getOpenInset(name);
709 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
710 result = base->localDispatch(fr);
713 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
714 mathcursor->insert(ar);
717 result = UNDISPATCHED;
724 result = UNDISPATCHED;
727 if (result == DISPATCHED)
728 bv->updateInset(this);
730 mathcursor->normalize();
733 lyx::Assert(mathcursor);
735 if (mathcursor->selection() || was_selection)
736 toggleInsetSelection(bv);
738 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
739 result == UNDISPATCHED) {
742 cmd.view()->stuffClipboard(mathcursor->grabSelection());
744 releaseMathCursor(bv);
745 bv->unlockInset(this);
747 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
750 return result; // original version
754 void InsetFormulaBase::revealCodes(BufferView * bv) const
758 bv->owner()->message(mathcursor->info());
761 // write something to the minibuffer
762 // translate to latex
763 mathcursor->markInsert();
766 string str = os.str();
767 mathcursor->markErase();
768 string::size_type pos = 0;
770 for (string::iterator it = str.begin(); it != str.end(); ++it) {
773 else if (*it == '\0') {
775 pos = it - str.begin();
781 res = res.substr(pos - 30);
783 res = res.substr(0, 60);
784 bv->owner()->message(res);
789 Inset::Code InsetFormulaBase::lyxCode() const
791 return Inset::MATH_CODE;
795 int InsetFormulaBase::ylow() const
797 return yo_ - dim_.asc;
801 int InsetFormulaBase::yhigh() const
803 return yo_ + dim_.des;
807 int InsetFormulaBase::xlow() const
813 int InsetFormulaBase::xhigh() const
815 return xo_ + dim_.wid;
819 /////////////////////////////////////////////////////////////////////
822 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
828 static InsetFormulaBase * lastformula = 0;
829 static MathIterator current = MathIterator(ibegin(par().nucleus()));
831 static string laststr;
833 if (lastformula != this || laststr != str) {
834 //lyxerr << "reset lastformula to " << this << "\n";
837 current = ibegin(par().nucleus());
839 mathed_parse_cell(ar, str);
843 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
845 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
846 if (it.cell().matchpart(ar, it.back().pos_)) {
847 bv->unlockInset(bv->theLockingInset());
848 if (!bv->lockInset(this)) {
849 lyxerr << "Cannot lock inset" << endl;
853 mathcursor = new MathCursor(this, true);
855 mathcursor->setSelection(it, ar.size());
858 bv->updateInset(this);
863 //lyxerr << "not found!\n";
869 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
872 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
873 return searchForward(bv, what, a, b);
877 bool InsetFormulaBase::display() const
879 return par()->asHullInset() && par()->asHullInset()->display();
883 string InsetFormulaBase::selectionAsString() const
885 return mathcursor ? mathcursor->grabSelection() : string();
888 /////////////////////////////////////////////////////////////////////
891 void mathDispatchCreation(FuncRequest const & cmd, bool display)
893 BufferView * bv = cmd.view();
894 // use selection if available..
896 //if (action == LFUN_MATH_IMPORT_SELECTION)
900 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
903 InsetFormula * f = new InsetFormula(bv);
904 if (openNewInset(bv, f)) {
905 bv->theLockingInset()->
906 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
907 // don't do that also for LFUN_MATH_MODE unless you want end up with
908 // always changing to mathrm when opening an inlined inset
909 // -- I really hate "LyXfunc overloading"...
911 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
912 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
915 // create a macro if we see "\\newcommand" somewhere, and an ordinary
917 InsetFormulaBase * f;
918 if (sel.find("\\newcommand") == string::npos &&
919 sel.find("\\def") == string::npos)
920 f = new InsetFormula(sel);
922 f = new InsetFormulaMacro(sel);
923 bv->getLyXText()->cutSelection(true, false);
926 cmd.message(N_("Math editor mode"));
930 void mathDispatch(FuncRequest const & cmd)
932 BufferView * bv = cmd.view();
933 if (!bv->available())
936 switch (cmd.action) {
938 case LFUN_MATH_DISPLAY:
939 mathDispatchCreation(cmd, true);
943 mathDispatchCreation(cmd, false);
946 case LFUN_MATH_IMPORT_SELECTION:
947 mathDispatchCreation(cmd, false);
950 case LFUN_MATH_MACRO:
951 if (cmd.argument.empty())
952 cmd.errorMessage(N_("Missing argument"));
954 string s = cmd.argument;
955 string const s1 = token(s, ' ', 1);
956 int const na = s1.empty() ? 0 : lyx::atoi(s1);
957 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
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);