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);
221 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
225 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
227 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
228 if (isCursorVisible())
235 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
237 if (isCursorVisible())
241 getCursorPos(bv, x, y);
242 math_font_max_dim(font_, asc, des);
243 bv->showLockedInsetCursor(x + xo_, y, asc, des);
244 setCursorVisible(true);
245 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
249 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
251 if (!isCursorVisible())
253 bv->hideLockedInsetCursor();
254 setCursorVisible(false);
255 //lyxerr << "hideInsetCursor: \n";
259 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
264 math_font_max_dim(font_, asc, des);
265 getCursorPos(bv, x, y);
267 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
268 bv->fitLockedInsetCursor(x, y, asc, des);
272 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
275 bv->updateInset(this, false);
279 vector<string> const InsetFormulaBase::getLabelList() const
281 return vector<string>();
285 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
287 bv->updateInset(this, dirty);
291 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
296 BufferView * bv = cmd.view();
299 bv->updateInset(this, false);
301 if (cmd.button() == mouse_button::button3) {
302 // try to dispatch to enclosed insets first
303 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
304 // launch math panel for right mouse button
305 bv->owner()->getDialogs().showMathPanel();
310 if (cmd.button() == mouse_button::button1) {
311 // try to dispatch to enclosed insets first
312 mathcursor->dispatch(cmd);
313 // try to set the cursor
315 //mathcursor = new MathCursor(this, x == 0);
317 //mathcursor->setPos(x + xo_, y + yo_);
324 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
326 BufferView * bv = cmd.view();
327 releaseMathCursor(bv);
328 mathcursor = new MathCursor(this, cmd.x == 0);
330 if (cmd.button() == mouse_button::button1) {
331 // just set the cursor here
332 //lyxerr << "setting cursor\n";
336 mathcursor->selClear();
337 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
338 mathcursor->dispatch(cmd);
341 if (cmd.button() == mouse_button::button3) {
342 mathcursor->dispatch(cmd);
346 bv->updateInset(this, false);
351 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
356 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
359 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
360 //lyxerr << "insetMotionNotify: ignored\n";
366 if (!mathcursor->selection())
367 mathcursor->selStart();
369 BufferView * bv = cmd.view();
371 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
373 bv->updateInset(this, false);
378 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
380 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
381 // << " arg: '" << cmd.argument
382 // << " x: '" << cmd.x
383 // << " y: '" << cmd.y
384 // << "' button: " << cmd.button() << "\n";
386 switch (cmd.action) {
387 case LFUN_MOUSE_PRESS:
388 return lfunMousePress(cmd);
389 case LFUN_MOUSE_MOTION:
390 return lfunMouseMotion(cmd);
391 case LFUN_MOUSE_RELEASE:
392 return lfunMouseRelease(cmd);
400 BufferView * bv = cmd.view();
401 string argument = cmd.argument;
402 RESULT result = DISPATCHED;
404 bool was_macro = mathcursor->inMacroMode();
405 bool was_selection = mathcursor->selection();
409 mathcursor->normalize();
412 switch (cmd.action) {
414 case LFUN_MATH_MUTATE:
415 case LFUN_MATH_DISPLAY:
416 case LFUN_MATH_NUMBER:
417 case LFUN_MATH_NONUMBER:
420 case LFUN_DELETE_LINE_FORWARD:
421 case LFUN_INSERT_LABEL:
422 case LFUN_MATH_EXTERN:
423 case LFUN_TABULAR_FEATURE:
424 case LFUN_PASTESELECTION:
425 bv->lockedInsetStoreUndo(Undo::EDIT);
426 mathcursor->dispatch(cmd);
427 updateLocal(bv, true);
430 case LFUN_WORDRIGHTSEL:
432 sel = true; // fall through...
434 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
435 //lyxerr << "calling scroll 20\n";
437 updateLocal(bv, false);
438 // write something to the minibuffer
439 //bv->owner()->message(mathcursor->info());
442 case LFUN_WORDLEFTSEL:
444 sel = true; // fall through
446 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
447 updateLocal(bv, false);
451 sel = true; // fall through
453 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
454 updateLocal(bv, false);
458 sel = true; // fall through
460 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
461 updateLocal(bv, false);
465 sel = true; // fall through
467 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
468 updateLocal(bv, false);
472 sel = true; // fall through
474 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
475 updateLocal(bv, false);
480 result = FINISHED_UP;
481 updateLocal(bv, false);
486 result = FINISHED_DOWN;
487 updateLocal(bv, false);
491 mathcursor->idxNext();
492 updateLocal(bv, false);
496 mathcursor->idxPrev();
497 updateLocal(bv, false);
500 case LFUN_DELETE_WORD_BACKWARD:
502 bv->lockedInsetStoreUndo(Undo::DELETE);
503 mathcursor->backspace();
504 updateLocal(bv, true);
507 case LFUN_DELETE_WORD_FORWARD:
509 bv->lockedInsetStoreUndo(Undo::DELETE);
511 bv->updateInset(this, true);
515 // sprintf(dispatch_buffer, "%d %d",);
516 // dispatch_result = dispatch_buffer;
519 lyxerr << "LFUN_SETXY broken!\n";
522 istringstream is(cmd.argument.c_str());
524 mathcursor->setPos(x, y);
525 updateLocal(bv, false);
531 mathcursor->macroModeClose();
532 bv->lockedInsetStoreUndo(Undo::EDIT);
533 mathcursor->selPaste();
534 updateLocal(bv, true);
538 bv->lockedInsetStoreUndo(Undo::DELETE);
539 mathcursor->selCut();
540 updateLocal(bv, true);
544 mathcursor->selCopy();
548 // Special casing for superscript in case of LyX handling
550 case LFUN_CIRCUMFLEX:
551 if (cmd.argument.empty()) {
552 // do superscript if LyX handles
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 mathcursor->script(true);
556 updateLocal(bv, true);
573 case LFUN_HUNG_UMLAUT:
577 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
578 case LFUN_BOLD: handleFont(bv, cmd.argument, "textbf"); break;
579 case LFUN_SANS: handleFont(bv, cmd.argument, "textsf"); break;
580 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
581 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
582 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
583 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
584 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
585 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
586 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
587 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
590 handleFont(bv, cmd.argument, "lyxgreek1");
591 if (cmd.argument.size())
592 mathcursor->interpret(cmd.argument);
596 if (mathcursor->currentMode())
597 handleFont(bv, cmd.argument, "textrm");
599 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
600 updateLocal(bv, true);
602 //bv->owner()->message(_("math text mode toggled"));
605 case LFUN_MATH_LIMITS:
606 bv->lockedInsetStoreUndo(Undo::EDIT);
607 if (mathcursor->toggleLimits())
608 updateLocal(bv, true);
614 bv->lockedInsetStoreUndo(Undo::EDIT);
615 mathcursor->setSize(arg);
616 updateLocal(bv, true);
621 case LFUN_INSERT_MATRIX:
622 if (!cmd.argument.empty()) {
623 bv->lockedInsetStoreUndo(Undo::EDIT);
624 mathcursor->interpret("matrix " + cmd.argument);
625 updateLocal(bv, true);
629 case LFUN_SUPERSCRIPT:
632 bv->lockedInsetStoreUndo(Undo::EDIT);
633 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
634 updateLocal(bv, true);
638 case LFUN_MATH_DELIM:
640 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
642 string rs = split(cmd.argument, ls, ' ');
643 // Reasonable default values
649 bv->lockedInsetStoreUndo(Undo::EDIT);
650 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
651 updateLocal(bv, true);
655 case LFUN_PROTECTEDSPACE:
656 case LFUN_MATH_SPACE:
657 bv->lockedInsetStoreUndo(Undo::EDIT);
658 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
659 updateLocal(bv, true);
663 bv->owner()->message(_("Invalid action in math mode!"));
667 case LFUN_EXEC_COMMAND:
668 result = UNDISPATCHED;
672 // interpret this as if a backslash was typed
673 bv->lockedInsetStoreUndo(Undo::EDIT);
674 mathcursor->interpret('\\');
675 updateLocal(bv, true);
678 case LFUN_BREAKPARAGRAPH:
679 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
680 case LFUN_BREAKPARAGRAPH_SKIP:
685 case LFUN_INSERT_MATH:
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 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
693 updateLocal(bv, true);
697 case LFUN_MATH_PANEL:
698 result = UNDISPATCHED;
702 if (mathcursor->selection())
703 mathcursor->selClear();
705 result = UNDISPATCHED;
708 case LFUN_INSET_TOGGLE:
709 mathcursor->insetToggle();
710 updateLocal(bv, true);
713 case LFUN_REF_INSERT:
714 //if (argument.empty()) {
715 // InsetCommandParams p("ref");
716 // owner_->getDialogs().createRef(p.getAsString());
718 // InsetCommandParams p;
719 // p.setFromString(argument);
721 // InsetRef * inset = new InsetRef(p, *buffer_);
722 // if (!insertInset(inset))
725 // updateInset(inset, true);
728 if (cmd.argument.empty()) {
729 InsetCommandParams p("ref");
730 bv->owner()->getDialogs().createRef(p.getAsString());
732 //mathcursor->handleNest(new InsetRef2);
733 //mathcursor->insert(arg);
734 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
736 updateLocal(bv, true);
740 result = UNDISPATCHED;
743 mathcursor->normalize();
746 lyx::Assert(mathcursor);
748 if (mathcursor->selection() || was_selection)
749 toggleInsetSelection(bv);
751 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
752 result == UNDISPATCHED) {
757 releaseMathCursor(bv);
758 bv->unlockInset(this);
761 return result; // original version
765 void InsetFormulaBase::revealCodes(BufferView * bv) const
769 bv->owner()->message(mathcursor->info());
772 // write something to the minibuffer
773 // translate to latex
774 mathcursor->markInsert();
777 string str = os.str();
778 mathcursor->markErase();
779 string::size_type pos = 0;
781 for (string::iterator it = str.begin(); it != str.end(); ++it) {
784 else if (*it == '\0') {
786 pos = it - str.begin();
792 res = res.substr(pos - 30);
794 res = res.substr(0, 60);
795 bv->owner()->message(res);
800 Inset::Code InsetFormulaBase::lyxCode() const
802 return Inset::MATH_CODE;
806 int InsetFormulaBase::ylow() const
808 return yo_ - ascent(view(), font_);
812 int InsetFormulaBase::yhigh() const
814 return yo_ + descent(view(), font_);
818 int InsetFormulaBase::xlow() const
824 int InsetFormulaBase::xhigh() const
826 return xo_ + width(view(), font_);
830 /////////////////////////////////////////////////////////////////////
833 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
839 static InsetFormulaBase * lastformula = 0;
840 static MathIterator current = MathIterator(ibegin(par().nucleus()));
842 static string laststr;
844 if (lastformula != this || laststr != str) {
845 //lyxerr << "reset lastformula to " << this << "\n";
848 current = ibegin(par().nucleus());
850 mathed_parse_cell(ar, str);
854 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
856 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
857 if (it.cell().matchpart(ar, it.back().pos_)) {
858 bv->unlockInset(bv->theLockingInset());
859 if (!bv->lockInset(this)) {
860 lyxerr << "Cannot lock inset" << endl;
864 mathcursor = new MathCursor(this, true);
866 mathcursor->setSelection(it, ar.size());
869 updateLocal(bv, false);
874 //lyxerr << "not found!\n";
880 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
883 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
884 return searchForward(bv, what, a, b);
888 bool InsetFormulaBase::display() const
890 return par()->asHullInset() && par()->asHullInset()->display();
894 /////////////////////////////////////////////////////////////////////
897 void mathDispatchCreation(FuncRequest const & cmd, bool display)
899 BufferView * bv = cmd.view();
900 // use selection if available..
902 //if (action == LFUN_MATH_IMPORT_SELECTION)
906 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
909 InsetFormula * f = new InsetFormula(bv);
910 if (openNewInset(bv, f)) {
911 bv->theLockingInset()->
912 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
913 // don't do that also for LFUN_MATH_MODE unless you want end up with
914 // always changing to mathrm when opening an inlined inset
915 // -- I really hate "LyXfunc overloading"...
917 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
918 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
921 // create a macro if we see "\\newcommand" somewhere, and an ordinary
923 InsetFormulaBase * f;
924 if (sel.find("\\newcommand") == string::npos &&
925 sel.find("\\def") == string::npos)
926 f = new InsetFormula(sel);
928 f = new InsetFormulaMacro(sel);
929 bv->getLyXText()->cutSelection(bv);
932 cmd.message(N_("Math editor mode"));
936 void mathDispatch(FuncRequest const & cmd)
938 BufferView * bv = cmd.view();
939 if (!bv->available())
942 switch (cmd.action) {
944 case LFUN_MATH_DISPLAY:
945 mathDispatchCreation(cmd, true);
949 mathDispatchCreation(cmd, false);
952 case LFUN_MATH_IMPORT_SELECTION:
953 mathDispatchCreation(cmd, false);
956 case LFUN_MATH_MACRO:
957 if (cmd.argument.empty())
958 cmd.errorMessage(N_("Missing argument"));
960 string s = cmd.argument;
961 string const s1 = token(s, ' ', 1);
962 int const na = s1.empty() ? 0 : lyx::atoi(s1);
963 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
968 case LFUN_INSERT_MATH:
969 case LFUN_INSERT_MATRIX:
970 case LFUN_MATH_DELIM: {
971 InsetFormula * f = new InsetFormula(bv);
972 if (openNewInset(bv, f)) {
973 bv->theLockingInset()->
974 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
975 bv->theLockingInset()->localDispatch(cmd);