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"
23 #include "formulamacro.h"
25 #include "funcrequest.h"
26 #include "BufferView.h"
29 #include "LaTeXFeatures.h"
31 #include "math_support.h"
32 #include "metricsinfo.h"
33 #include "support/lstrings.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/font_metrics.h"
36 #include "frontends/mouse_state.h"
38 #include "math_arrayinset.h"
39 #include "math_charinset.h"
40 #include "math_deliminset.h"
41 #include "math_cursor.h"
42 #include "math_factory.h"
43 #include "math_fontinset.h"
44 #include "math_hullinset.h"
45 #include "math_iterator.h"
46 #include "math_macrotable.h"
47 #include "math_parser.h"
49 #include "math_spaceinset.h"
50 #include "undo_funcs.h"
51 #include "textpainter.h"
52 #include "frontends/Dialogs.h"
54 #include "ref_inset.h"
62 MathCursor * mathcursor = 0;
73 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
75 if (!bv->insertInset(new_inset)) {
79 new_inset->edit(bv, true);
88 InsetFormulaBase::InsetFormulaBase()
89 : font_(), xo_(0), yo_(0)
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->par()->name() == font)
129 mathcursor->handleFont(font);
131 mathcursor->handleNest(createMathInset(font));
132 mathcursor->insert(arg);
137 BufferView * InsetFormulaBase::view() const
139 return view_.lock().get();
143 void InsetFormulaBase::validate(LaTeXFeatures &) const
147 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
154 void InsetFormulaBase::metrics(BufferView * bv) const
157 view_ = bv->owner()->view();
159 mi.base.style = LM_ST_TEXT;
160 mi.base.font = font_;
161 mi.base.font.setColor(LColor::math);
166 string const InsetFormulaBase::editMessage() const
168 return _("Math editor mode");
172 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
174 if (!bv->lockInset(this))
175 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
176 releaseMathCursor(bv);
177 mathcursor = new MathCursor(this, true);
179 mathcursor->setPos(x + xo_, y + yo_);
180 // if that is removed, we won't get the magenta box when entering an
181 // inset for the first time
182 bv->updateInset(this);
186 void InsetFormulaBase::edit(BufferView * bv, bool front)
188 if (!bv->lockInset(this))
189 lyxerr << "Cannot lock math inset in edit call!\n";
190 releaseMathCursor(bv);
191 mathcursor = new MathCursor(this, front);
193 bv->updateInset(this);
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
200 if (mathcursor->inMacroMode()) {
201 mathcursor->macroModeClose();
202 bv->updateInset(this);
204 releaseMathCursor(bv);
207 bv->updateInset(this);
211 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
213 mathcursor->getPos(x, y);
217 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
219 // calling metrics here destroys the cached xo,yo positions e.g. in
220 // MathParboxinset. And it would be too expensive anyway...
223 lyxerr << "getCursorPos - should not happen";
227 mathcursor->getPos(x, y);
228 x = mathcursor->targetX();
231 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
235 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
240 math_font_max_dim(font_, asc, des);
241 getCursorPos(bv, x, y);
243 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
244 bv->fitLockedInsetCursor(x, y, asc, des);
248 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
251 bv->updateInset(this);
255 vector<string> const InsetFormulaBase::getLabelList() const
257 return vector<string>();
261 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
266 BufferView * bv = cmd.view();
267 bv->updateInset(this);
268 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
270 if (cmd.button() == mouse_button::button3) {
271 // try to dispatch to enclosed insets first
272 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
273 // launch math panel for right mouse button
274 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
275 bv->owner()->getDialogs().showMathPanel();
280 if (cmd.button() == mouse_button::button2) {
281 mathcursor->selClear();
282 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
283 mathcursor->insert(asArray(bv->getClipboard()));
284 bv->updateInset(this);
288 if (cmd.button() == mouse_button::button1) {
289 // try to dispatch to enclosed insets first
290 mathcursor->dispatch(cmd);
291 cmd.view()->stuffClipboard(mathcursor->grabSelection());
292 // try to set the cursor
294 //mathcursor = new MathCursor(this, x == 0);
296 //mathcursor->setPos(x + xo_, y + yo_);
304 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
306 BufferView * bv = cmd.view();
307 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
309 if (!mathcursor || mathcursor->formula() != this) {
310 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
311 releaseMathCursor(bv);
312 mathcursor = new MathCursor(this, cmd.x == 0);
314 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
317 if (cmd.button() == mouse_button::button3) {
318 mathcursor->dispatch(cmd);
322 if (cmd.button() == mouse_button::button1) {
325 mathcursor->selClear();
326 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
327 mathcursor->dispatch(cmd);
331 bv->updateInset(this);
336 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
341 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
344 // only select with button 1
345 if (cmd.button() != mouse_button::button1)
348 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
354 if (!mathcursor->selection())
355 mathcursor->selStart();
357 BufferView * bv = cmd.view();
358 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
359 bv->updateInset(this);
364 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
366 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
367 // << " arg: '" << cmd.argument
368 // << " x: '" << cmd.x
369 // << " y: '" << cmd.y
370 // << "' button: " << cmd.button() << endl;
372 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
373 bool remove_inset = false;
375 switch (cmd.action) {
376 case LFUN_MOUSE_PRESS:
377 //lyxerr << "Mouse single press\n";
378 return lfunMousePress(cmd);
379 case LFUN_MOUSE_MOTION:
380 //lyxerr << "Mouse motion\n";
381 return lfunMouseMotion(cmd);
382 case LFUN_MOUSE_RELEASE:
383 //lyxerr << "Mouse single release\n";
384 return lfunMouseRelease(cmd);
385 case LFUN_MOUSE_DOUBLE:
386 //lyxerr << "Mouse double\n";
387 return localDispatch(FuncRequest(LFUN_WORDSEL));
395 BufferView * bv = cmd.view();
396 string argument = cmd.argument;
397 RESULT result = DISPATCHED;
399 bool was_macro = mathcursor->inMacroMode();
400 bool was_selection = mathcursor->selection();
402 mathcursor->normalize();
405 switch (cmd.action) {
407 case LFUN_MATH_MUTATE:
408 case LFUN_MATH_DISPLAY:
409 case LFUN_MATH_NUMBER:
410 case LFUN_MATH_NONUMBER:
411 case LFUN_CELL_SPLIT:
413 case LFUN_DELETE_LINE_FORWARD:
414 case LFUN_INSERT_LABEL:
415 case LFUN_MATH_EXTERN:
416 case LFUN_TABULAR_FEATURE:
417 case LFUN_PASTESELECTION:
418 case LFUN_MATH_LIMITS:
419 bv->lockedInsetStoreUndo(Undo::EDIT);
420 mathcursor->dispatch(cmd);
424 sel = true; // fall through...
426 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
427 //lyxerr << "calling scroll 20\n";
429 // write something to the minibuffer
430 //bv->owner()->message(mathcursor->info());
434 sel = true; // fall through
436 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
440 sel = true; // fall through
442 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
446 sel = true; // fall through
448 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
452 mathcursor->home(false);
453 mathcursor->end(true);
456 case LFUN_UP_PARAGRAPHSEL:
457 case LFUN_UP_PARAGRAPH:
458 case LFUN_DOWN_PARAGRAPHSEL:
459 case LFUN_DOWN_PARAGRAPH:
464 case LFUN_WORDLEFTSEL:
465 sel = true; // fall through
468 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
472 case LFUN_WORDRIGHTSEL:
473 sel = true; // fall through
476 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
481 case LFUN_BEGINNINGBUFSEL:
482 case LFUN_BEGINNINGBUF:
490 result = FINISHED_RIGHT;
493 case LFUN_CELL_FORWARD:
494 mathcursor->idxNext();
497 case LFUN_CELL_BACKWARD:
498 mathcursor->idxPrev();
501 case LFUN_DELETE_WORD_BACKWARD:
503 bv->lockedInsetStoreUndo(Undo::EDIT);
504 if (!mathcursor->backspace()) {
510 case LFUN_DELETE_WORD_FORWARD:
512 bv->lockedInsetStoreUndo(Undo::EDIT);
513 if (!mathcursor->erase()) {
520 // sprintf(dispatch_buffer, "%d %d",);
521 // dispatch_result = dispatch_buffer;
524 lyxerr << "LFUN_SETXY broken!\n";
527 istringstream is(cmd.argument.c_str());
529 mathcursor->setPos(x, y);
535 istringstream is(cmd.argument.c_str());
538 mathcursor->macroModeClose();
539 bv->lockedInsetStoreUndo(Undo::EDIT);
540 mathcursor->selPaste(n);
545 bv->lockedInsetStoreUndo(Undo::DELETE);
546 mathcursor->selCut();
550 mathcursor->selCopy();
554 // Special casing for superscript in case of LyX handling
556 case LFUN_CIRCUMFLEX:
557 if (cmd.argument.empty()) {
558 // do superscript if LyX handles
560 bv->lockedInsetStoreUndo(Undo::EDIT);
561 mathcursor->script(true);
578 case LFUN_HUNG_UMLAUT:
582 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
583 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
584 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
585 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
586 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
587 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
588 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
589 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
590 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
591 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
594 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
595 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
597 handleFont(bv, cmd.argument, "textrm");
598 //bv->owner()->message(_("math text mode toggled"));
604 bv->lockedInsetStoreUndo(Undo::EDIT);
605 mathcursor->setSize(arg);
610 case LFUN_INSERT_MATRIX: {
611 bv->lockedInsetStoreUndo(Undo::EDIT);
616 istringstream is(STRCONV(argument));
617 is >> m >> n >> v_align >> h_align;
621 mathcursor->niceInsert(
622 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
626 case LFUN_SUPERSCRIPT:
629 bv->lockedInsetStoreUndo(Undo::EDIT);
630 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
634 case LFUN_MATH_DELIM:
636 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
638 string rs = split(cmd.argument, ls, ' ');
639 // Reasonable default values
645 bv->lockedInsetStoreUndo(Undo::EDIT);
646 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
650 case LFUN_PROTECTEDSPACE:
651 case LFUN_MATH_SPACE:
652 bv->lockedInsetStoreUndo(Undo::EDIT);
653 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
657 bv->owner()->message(_("Invalid action in math mode!"));
661 case LFUN_EXEC_COMMAND:
662 result = UNDISPATCHED;
666 // interpret this as if a backslash was typed
667 bv->lockedInsetStoreUndo(Undo::EDIT);
668 mathcursor->interpret('\\');
671 case LFUN_BREAKPARAGRAPH:
672 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
673 case LFUN_BREAKPARAGRAPH_SKIP:
677 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
678 // handling such that "self-insert" works on "arbitrary stuff" too, and
679 // math-insert only handles special math things like "matrix".
680 case LFUN_INSERT_MATH:
681 bv->lockedInsetStoreUndo(Undo::EDIT);
682 mathcursor->niceInsert(argument);
686 case LFUN_SELFINSERT:
687 if (!argument.empty()) {
688 bv->lockedInsetStoreUndo(Undo::EDIT);
689 if (argument.size() == 1)
690 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
692 mathcursor->insert(asArray(argument));
696 case LFUN_MATH_PANEL:
697 result = UNDISPATCHED;
701 if (mathcursor->selection())
702 mathcursor->selClear();
704 result = UNDISPATCHED;
707 case LFUN_INSET_TOGGLE:
708 mathcursor->insetToggle();
711 case LFUN_DIALOG_SHOW_NEW_INSET: {
712 string const & name = argument;
716 data = tmp.createDialogStr(name);
720 result = UNDISPATCHED;
722 bv->owner()->getDialogs().show(name, data, 0);
727 case LFUN_INSET_APPLY: {
728 string const name = cmd.getArg(0);
730 bv->owner()->getDialogs().getOpenInset(name);
733 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
734 result = base->localDispatch(fr);
737 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
738 mathcursor->insert(ar);
741 result = UNDISPATCHED;
748 result = UNDISPATCHED;
751 if (result == DISPATCHED)
752 bv->updateInset(this);
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) {
766 cmd.view()->stuffClipboard(mathcursor->grabSelection());
768 releaseMathCursor(bv);
769 bv->unlockInset(this);
771 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
774 return result; // original version
778 void InsetFormulaBase::revealCodes(BufferView * bv) const
782 bv->owner()->message(mathcursor->info());
785 // write something to the minibuffer
786 // translate to latex
787 mathcursor->markInsert();
790 string str = os.str();
791 mathcursor->markErase();
792 string::size_type pos = 0;
794 for (string::iterator it = str.begin(); it != str.end(); ++it) {
797 else if (*it == '\0') {
799 pos = it - str.begin();
805 res = res.substr(pos - 30);
807 res = res.substr(0, 60);
808 bv->owner()->message(res);
813 Inset::Code InsetFormulaBase::lyxCode() const
815 return Inset::MATH_CODE;
819 int InsetFormulaBase::ylow() const
821 return yo_ - ascent(view(), font_);
825 int InsetFormulaBase::yhigh() const
827 return yo_ + descent(view(), font_);
831 int InsetFormulaBase::xlow() const
837 int InsetFormulaBase::xhigh() const
839 return xo_ + width(view(), font_);
843 /////////////////////////////////////////////////////////////////////
846 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
852 static InsetFormulaBase * lastformula = 0;
853 static MathIterator current = MathIterator(ibegin(par().nucleus()));
855 static string laststr;
857 if (lastformula != this || laststr != str) {
858 //lyxerr << "reset lastformula to " << this << "\n";
861 current = ibegin(par().nucleus());
863 mathed_parse_cell(ar, str);
867 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
869 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
870 if (it.cell().matchpart(ar, it.back().pos_)) {
871 bv->unlockInset(bv->theLockingInset());
872 if (!bv->lockInset(this)) {
873 lyxerr << "Cannot lock inset" << endl;
877 mathcursor = new MathCursor(this, true);
879 mathcursor->setSelection(it, ar.size());
882 bv->updateInset(this);
887 //lyxerr << "not found!\n";
893 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
896 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
897 return searchForward(bv, what, a, b);
901 bool InsetFormulaBase::display() const
903 return par()->asHullInset() && par()->asHullInset()->display();
907 string InsetFormulaBase::selectionAsString() const
909 return mathcursor ? mathcursor->grabSelection() : string();
912 /////////////////////////////////////////////////////////////////////
915 void mathDispatchCreation(FuncRequest const & cmd, bool display)
917 BufferView * bv = cmd.view();
918 // use selection if available..
920 //if (action == LFUN_MATH_IMPORT_SELECTION)
924 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
927 InsetFormula * f = new InsetFormula(bv);
928 if (openNewInset(bv, f)) {
929 bv->theLockingInset()->
930 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
931 // don't do that also for LFUN_MATH_MODE unless you want end up with
932 // always changing to mathrm when opening an inlined inset
933 // -- I really hate "LyXfunc overloading"...
935 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
936 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
939 // create a macro if we see "\\newcommand" somewhere, and an ordinary
941 InsetFormulaBase * f;
942 if (sel.find("\\newcommand") == string::npos &&
943 sel.find("\\def") == string::npos)
944 f = new InsetFormula(sel);
946 f = new InsetFormulaMacro(sel);
947 bv->getLyXText()->cutSelection(bv);
950 cmd.message(N_("Math editor mode"));
954 void mathDispatch(FuncRequest const & cmd)
956 BufferView * bv = cmd.view();
957 if (!bv->available())
960 switch (cmd.action) {
962 case LFUN_MATH_DISPLAY:
963 mathDispatchCreation(cmd, true);
967 mathDispatchCreation(cmd, false);
970 case LFUN_MATH_IMPORT_SELECTION:
971 mathDispatchCreation(cmd, false);
974 case LFUN_MATH_MACRO:
975 if (cmd.argument.empty())
976 cmd.errorMessage(N_("Missing argument"));
978 string s = cmd.argument;
979 string const s1 = token(s, ' ', 1);
980 int const na = s1.empty() ? 0 : lyx::atoi(s1);
981 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
985 case LFUN_INSERT_MATH:
986 case LFUN_INSERT_MATRIX:
987 case LFUN_MATH_DELIM: {
988 InsetFormula * f = new InsetFormula(bv);
989 if (openNewInset(bv, f)) {
990 bv->theLockingInset()->
991 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
992 bv->theLockingInset()->localDispatch(cmd);