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::button2) {
312 mathcursor->selClear();
313 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
314 mathcursor->insert(asArray(bv->getClipboard()));
315 bv->updateInset(this, true);
319 if (cmd.button() == mouse_button::button1) {
320 // try to dispatch to enclosed insets first
321 mathcursor->dispatch(cmd);
322 // try to set the cursor
324 //mathcursor = new MathCursor(this, x == 0);
326 //mathcursor->setPos(x + xo_, y + yo_);
334 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
336 BufferView * bv = cmd.view();
337 releaseMathCursor(bv);
338 mathcursor = new MathCursor(this, cmd.x == 0);
340 if (cmd.button() == mouse_button::button1) {
341 // just set the cursor here
342 //lyxerr << "setting cursor\n";
346 mathcursor->selClear();
347 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
348 mathcursor->dispatch(cmd);
352 if (cmd.button() == mouse_button::button3) {
353 mathcursor->dispatch(cmd);
358 bv->updateInset(this, false);
363 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
368 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
371 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
372 //lyxerr << "insetMotionNotify: ignored\n";
378 if (!mathcursor->selection())
379 mathcursor->selStart();
381 BufferView * bv = cmd.view();
383 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
385 bv->updateInset(this, false);
390 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
392 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
393 // << " arg: '" << cmd.argument
394 // << " x: '" << cmd.x
395 // << " y: '" << cmd.y
396 // << "' button: " << cmd.button() << "\n";
398 switch (cmd.action) {
399 case LFUN_MOUSE_PRESS:
400 return lfunMousePress(cmd);
401 case LFUN_MOUSE_MOTION:
402 return lfunMouseMotion(cmd);
403 case LFUN_MOUSE_RELEASE:
404 return lfunMouseRelease(cmd);
405 case LFUN_MOUSE_DOUBLE:
406 //lyxerr << "Mouse double\n";
407 return localDispatch(FuncRequest(LFUN_WORDSEL));
415 BufferView * bv = cmd.view();
416 string argument = cmd.argument;
417 RESULT result = DISPATCHED;
419 bool was_macro = mathcursor->inMacroMode();
420 bool was_selection = mathcursor->selection();
424 mathcursor->normalize();
427 switch (cmd.action) {
429 case LFUN_MATH_MUTATE:
430 case LFUN_MATH_DISPLAY:
431 case LFUN_MATH_NUMBER:
432 case LFUN_MATH_NONUMBER:
435 case LFUN_DELETE_LINE_FORWARD:
436 case LFUN_INSERT_LABEL:
437 case LFUN_MATH_EXTERN:
438 case LFUN_TABULAR_FEATURE:
439 case LFUN_PASTESELECTION:
440 case LFUN_MATH_LIMITS:
441 bv->lockedInsetStoreUndo(Undo::EDIT);
442 mathcursor->dispatch(cmd);
443 updateLocal(bv, true);
447 sel = true; // fall through...
449 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
450 //lyxerr << "calling scroll 20\n";
452 updateLocal(bv, false);
453 // write something to the minibuffer
454 //bv->owner()->message(mathcursor->info());
458 sel = true; // fall through
460 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
461 updateLocal(bv, false);
465 sel = true; // fall through
467 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
468 updateLocal(bv, false);
472 sel = true; // fall through
474 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
475 updateLocal(bv, false);
479 mathcursor->home(false);
480 mathcursor->end(true);
481 updateLocal(bv, false);
485 case LFUN_WORDLEFTSEL:
486 sel = true; // fall through
489 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
490 updateLocal(bv, false);
494 case LFUN_WORDRIGHTSEL:
495 sel = true; // fall through
498 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
499 updateLocal(bv, false);
504 case LFUN_BEGINNINGBUFSEL:
505 case LFUN_BEGINNINGBUF:
506 result = FINISHED_UP;
507 updateLocal(bv, false);
514 result = FINISHED_DOWN;
515 updateLocal(bv, false);
519 mathcursor->idxNext();
520 updateLocal(bv, false);
524 mathcursor->idxPrev();
525 updateLocal(bv, false);
528 case LFUN_DELETE_WORD_BACKWARD:
530 bv->lockedInsetStoreUndo(Undo::DELETE);
531 mathcursor->backspace();
532 updateLocal(bv, true);
535 case LFUN_DELETE_WORD_FORWARD:
537 bv->lockedInsetStoreUndo(Undo::DELETE);
539 bv->updateInset(this, true);
543 // sprintf(dispatch_buffer, "%d %d",);
544 // dispatch_result = dispatch_buffer;
547 lyxerr << "LFUN_SETXY broken!\n";
550 istringstream is(cmd.argument.c_str());
552 mathcursor->setPos(x, y);
553 updateLocal(bv, false);
559 mathcursor->macroModeClose();
560 bv->lockedInsetStoreUndo(Undo::EDIT);
561 mathcursor->selPaste();
562 updateLocal(bv, true);
566 bv->lockedInsetStoreUndo(Undo::DELETE);
567 mathcursor->selCut();
568 updateLocal(bv, true);
572 mathcursor->selCopy();
576 // Special casing for superscript in case of LyX handling
578 case LFUN_CIRCUMFLEX:
579 if (cmd.argument.empty()) {
580 // do superscript if LyX handles
582 bv->lockedInsetStoreUndo(Undo::EDIT);
583 mathcursor->script(true);
584 updateLocal(bv, true);
601 case LFUN_HUNG_UMLAUT:
605 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
606 case LFUN_BOLD: handleFont(bv, cmd.argument, "textbf"); break;
607 case LFUN_SANS: handleFont(bv, cmd.argument, "textsf"); break;
608 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
609 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
610 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
611 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
612 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
613 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
614 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
615 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
618 handleFont(bv, cmd.argument, "lyxgreek1");
619 if (cmd.argument.size())
620 mathcursor->interpret(cmd.argument);
624 if (mathcursor->currentMode())
625 handleFont(bv, cmd.argument, "textrm");
627 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
628 updateLocal(bv, true);
630 //bv->owner()->message(_("math text mode toggled"));
636 bv->lockedInsetStoreUndo(Undo::EDIT);
637 mathcursor->setSize(arg);
638 updateLocal(bv, true);
643 case LFUN_INSERT_MATRIX:
644 if (!cmd.argument.empty()) {
645 bv->lockedInsetStoreUndo(Undo::EDIT);
646 mathcursor->interpret("matrix " + cmd.argument);
647 updateLocal(bv, true);
651 case LFUN_SUPERSCRIPT:
654 bv->lockedInsetStoreUndo(Undo::EDIT);
655 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
656 updateLocal(bv, true);
660 case LFUN_MATH_DELIM:
662 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
664 string rs = split(cmd.argument, ls, ' ');
665 // Reasonable default values
671 bv->lockedInsetStoreUndo(Undo::EDIT);
672 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
673 updateLocal(bv, true);
677 case LFUN_PROTECTEDSPACE:
678 case LFUN_MATH_SPACE:
679 bv->lockedInsetStoreUndo(Undo::EDIT);
680 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
681 updateLocal(bv, true);
685 bv->owner()->message(_("Invalid action in math mode!"));
689 case LFUN_EXEC_COMMAND:
690 result = UNDISPATCHED;
694 // interpret this as if a backslash was typed
695 bv->lockedInsetStoreUndo(Undo::EDIT);
696 mathcursor->interpret('\\');
697 updateLocal(bv, true);
700 case LFUN_BREAKPARAGRAPH:
701 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
702 case LFUN_BREAKPARAGRAPH_SKIP:
707 case LFUN_INSERT_MATH:
708 case LFUN_SELFINSERT:
709 if (!argument.empty()) {
710 bv->lockedInsetStoreUndo(Undo::EDIT);
711 if (argument.size() == 1)
712 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
714 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
715 updateLocal(bv, true);
719 case LFUN_MATH_PANEL:
720 result = UNDISPATCHED;
724 if (mathcursor->selection())
725 mathcursor->selClear();
727 result = UNDISPATCHED;
730 case LFUN_INSET_TOGGLE:
731 mathcursor->insetToggle();
732 updateLocal(bv, true);
735 case LFUN_REF_INSERT:
736 //if (argument.empty()) {
737 // InsetCommandParams p("ref");
738 // owner_->getDialogs().createRef(p.getAsString());
740 // InsetCommandParams p;
741 // p.setFromString(argument);
743 // InsetRef * inset = new InsetRef(p, *buffer_);
744 // if (!insertInset(inset))
747 // updateInset(inset, true);
750 if (cmd.argument.empty()) {
751 InsetCommandParams p("ref");
752 bv->owner()->getDialogs().createRef(p.getAsString());
754 //mathcursor->handleNest(new InsetRef2);
755 //mathcursor->insert(arg);
756 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
758 updateLocal(bv, true);
762 result = UNDISPATCHED;
765 mathcursor->normalize();
768 lyx::Assert(mathcursor);
770 if (mathcursor->selection() || was_selection)
771 toggleInsetSelection(bv);
773 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
774 result == UNDISPATCHED) {
779 releaseMathCursor(bv);
780 bv->unlockInset(this);
783 return result; // original version
787 void InsetFormulaBase::revealCodes(BufferView * bv) const
791 bv->owner()->message(mathcursor->info());
794 // write something to the minibuffer
795 // translate to latex
796 mathcursor->markInsert();
799 string str = os.str();
800 mathcursor->markErase();
801 string::size_type pos = 0;
803 for (string::iterator it = str.begin(); it != str.end(); ++it) {
806 else if (*it == '\0') {
808 pos = it - str.begin();
814 res = res.substr(pos - 30);
816 res = res.substr(0, 60);
817 bv->owner()->message(res);
822 Inset::Code InsetFormulaBase::lyxCode() const
824 return Inset::MATH_CODE;
828 int InsetFormulaBase::ylow() const
830 return yo_ - ascent(view(), font_);
834 int InsetFormulaBase::yhigh() const
836 return yo_ + descent(view(), font_);
840 int InsetFormulaBase::xlow() const
846 int InsetFormulaBase::xhigh() const
848 return xo_ + width(view(), font_);
852 /////////////////////////////////////////////////////////////////////
855 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
861 static InsetFormulaBase * lastformula = 0;
862 static MathIterator current = MathIterator(ibegin(par().nucleus()));
864 static string laststr;
866 if (lastformula != this || laststr != str) {
867 //lyxerr << "reset lastformula to " << this << "\n";
870 current = ibegin(par().nucleus());
872 mathed_parse_cell(ar, str);
876 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
878 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
879 if (it.cell().matchpart(ar, it.back().pos_)) {
880 bv->unlockInset(bv->theLockingInset());
881 if (!bv->lockInset(this)) {
882 lyxerr << "Cannot lock inset" << endl;
886 mathcursor = new MathCursor(this, true);
888 mathcursor->setSelection(it, ar.size());
891 updateLocal(bv, false);
896 //lyxerr << "not found!\n";
902 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
905 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
906 return searchForward(bv, what, a, b);
910 bool InsetFormulaBase::display() const
912 return par()->asHullInset() && par()->asHullInset()->display();
916 /////////////////////////////////////////////////////////////////////
919 void mathDispatchCreation(FuncRequest const & cmd, bool display)
921 BufferView * bv = cmd.view();
922 // use selection if available..
924 //if (action == LFUN_MATH_IMPORT_SELECTION)
928 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
931 InsetFormula * f = new InsetFormula(bv);
932 if (openNewInset(bv, f)) {
933 bv->theLockingInset()->
934 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
935 // don't do that also for LFUN_MATH_MODE unless you want end up with
936 // always changing to mathrm when opening an inlined inset
937 // -- I really hate "LyXfunc overloading"...
939 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
940 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
943 // create a macro if we see "\\newcommand" somewhere, and an ordinary
945 InsetFormulaBase * f;
946 if (sel.find("\\newcommand") == string::npos &&
947 sel.find("\\def") == string::npos)
948 f = new InsetFormula(sel);
950 f = new InsetFormulaMacro(sel);
951 bv->getLyXText()->cutSelection(bv);
954 cmd.message(N_("Math editor mode"));
958 void mathDispatch(FuncRequest const & cmd)
960 BufferView * bv = cmd.view();
961 if (!bv->available())
964 switch (cmd.action) {
966 case LFUN_MATH_DISPLAY:
967 mathDispatchCreation(cmd, true);
971 mathDispatchCreation(cmd, false);
974 case LFUN_MATH_IMPORT_SELECTION:
975 mathDispatchCreation(cmd, false);
978 case LFUN_MATH_MACRO:
979 if (cmd.argument.empty())
980 cmd.errorMessage(N_("Missing argument"));
982 string s = cmd.argument;
983 string const s1 = token(s, ' ', 1);
984 int const na = s1.empty() ? 0 : lyx::atoi(s1);
985 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
990 case LFUN_INSERT_MATH:
991 case LFUN_INSERT_MATRIX:
992 case LFUN_MATH_DELIM: {
993 InsetFormula * f = new InsetFormula(bv);
994 if (openNewInset(bv, f)) {
995 bv->theLockingInset()->
996 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
997 bv->theLockingInset()->localDispatch(cmd);