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, y);
177 //lyxerr << "setting pos to " << x << "," << y << "\n";
179 // if that is removed, we won't get the magenta box when entering an
180 // inset for the first time
181 bv->updateInset(this, false);
185 void InsetFormulaBase::edit(BufferView * bv, bool front)
187 if (!bv->lockInset(this))
188 lyxerr << "Cannot lock math inset in edit call!\n";
189 releaseMathCursor(bv);
190 mathcursor = new MathCursor(this, front);
192 bv->updateInset(this, false);
196 void InsetFormulaBase::insetUnlock(BufferView * bv)
199 if (mathcursor->inMacroMode()) {
200 mathcursor->macroModeClose();
201 updateLocal(bv, true);
203 releaseMathCursor(bv);
206 bv->updateInset(this, false);
210 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
212 // calling metrics here destroys the cached xo,yo positions e.g. in
213 // MathParboxinset. And it would be too expensive anyway...
216 lyxerr << "getCursorPos - should not happen";
220 mathcursor->getPos(x, y);
223 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
227 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
229 //lyxerr << "toggleInsetCursor: " << isCursorVisible() << "\n";
230 if (isCursorVisible())
237 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
239 if (isCursorVisible())
243 getCursorPos(bv, x, y);
244 math_font_max_dim(font_, asc, des);
245 bv->showLockedInsetCursor(x, y, asc, des);
246 setCursorVisible(true);
247 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
251 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
253 if (!isCursorVisible())
255 bv->hideLockedInsetCursor();
256 setCursorVisible(false);
257 //lyxerr << "hideInsetCursor: \n";
261 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
266 math_font_max_dim(font_, asc, des);
267 getCursorPos(bv, x, y);
269 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
270 bv->fitLockedInsetCursor(x, y, asc, des);
274 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
277 bv->updateInset(this, false);
281 vector<string> const InsetFormulaBase::getLabelList() const
283 return vector<string>();
287 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
289 bv->updateInset(this, dirty);
293 Inset::RESULT InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
298 BufferView * bv = cmd.view();
301 bv->updateInset(this, false);
303 if (cmd.button() == mouse_button::button3) {
304 // try to dispatch to enclosed insets first
305 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
306 // launch math panel for right mouse button
307 bv->owner()->getDialogs().showMathPanel();
312 if (cmd.button() == mouse_button::button1) {
313 // try to dispatch to enclosed insets first
314 mathcursor->dispatch(cmd);
315 // try to set the cursor
317 //mathcursor = new MathCursor(this, x == 0);
319 //mathcursor->setPos(x + xo_, y + yo_);
326 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
328 BufferView * bv = cmd.view();
329 releaseMathCursor(bv);
330 mathcursor = new MathCursor(this, cmd.x == 0);
332 if (cmd.button() == mouse_button::button1) {
333 // just set the cursor here
334 //lyxerr << "setting cursor\n";
338 mathcursor->selClear();
339 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
340 mathcursor->dispatch(cmd);
343 if (cmd.button() == mouse_button::button3) {
344 mathcursor->dispatch(cmd);
348 bv->updateInset(this, false);
353 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
358 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
361 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2) {
362 //lyxerr << "insetMotionNotify: ignored\n";
368 if (!mathcursor->selection())
369 mathcursor->selStart();
371 BufferView * bv = cmd.view();
373 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
375 bv->updateInset(this, false);
380 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
382 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
383 // << " arg: '" << cmd.argument
384 // << " x: '" << cmd.x
385 // << " y: '" << cmd.y
386 // << "' button: " << cmd.button() << "\n";
388 switch (cmd.action) {
389 case LFUN_MOUSE_PRESS:
390 return lfunMousePress(cmd);
391 case LFUN_MOUSE_MOTION:
392 return lfunMouseMotion(cmd);
393 case LFUN_MOUSE_RELEASE:
394 return lfunMouseRelease(cmd);
402 BufferView * bv = cmd.view();
403 string argument = cmd.argument;
404 RESULT result = DISPATCHED;
406 bool was_macro = mathcursor->inMacroMode();
407 bool was_selection = mathcursor->selection();
411 mathcursor->normalize();
414 switch (cmd.action) {
416 case LFUN_MATH_MUTATE:
417 case LFUN_MATH_DISPLAY:
418 case LFUN_MATH_NUMBER:
419 case LFUN_MATH_NONUMBER:
422 case LFUN_DELETE_LINE_FORWARD:
423 case LFUN_INSERT_LABEL:
424 case LFUN_MATH_EXTERN:
425 case LFUN_TABULAR_FEATURE:
426 bv->lockedInsetStoreUndo(Undo::EDIT);
427 mathcursor->dispatch(cmd);
428 updateLocal(bv, true);
431 case LFUN_WORDRIGHTSEL:
433 sel = true; // fall through...
435 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
436 //lyxerr << "calling scroll 20\n";
438 updateLocal(bv, false);
439 // write something to the minibuffer
440 //bv->owner()->message(mathcursor->info());
443 case LFUN_WORDLEFTSEL:
445 sel = true; // fall through
447 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
448 updateLocal(bv, false);
452 sel = true; // fall through
454 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
455 updateLocal(bv, false);
459 sel = true; // fall through
461 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
462 updateLocal(bv, false);
466 sel = true; // fall through
468 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
469 updateLocal(bv, false);
473 sel = true; // fall through
475 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
476 updateLocal(bv, false);
481 result = FINISHED_UP;
482 updateLocal(bv, false);
487 result = FINISHED_DOWN;
488 updateLocal(bv, false);
492 mathcursor->idxNext();
493 updateLocal(bv, false);
497 mathcursor->idxPrev();
498 updateLocal(bv, false);
501 case LFUN_DELETE_WORD_BACKWARD:
503 bv->lockedInsetStoreUndo(Undo::DELETE);
504 mathcursor->backspace();
505 updateLocal(bv, true);
508 case LFUN_DELETE_WORD_FORWARD:
510 bv->lockedInsetStoreUndo(Undo::DELETE);
512 bv->updateInset(this, true);
516 // sprintf(dispatch_buffer, "%d %d",);
517 // dispatch_result = dispatch_buffer;
520 lyxerr << "LFUN_SETXY broken!\n";
523 istringstream is(cmd.argument.c_str());
525 mathcursor->setPos(x, y);
526 updateLocal(bv, false);
531 case LFUN_PASTESELECTION: {
532 string const clip = bv->getClipboard();
534 mathed_parse_normal(par_, clip);
541 mathcursor->macroModeClose();
542 bv->lockedInsetStoreUndo(Undo::EDIT);
543 mathcursor->selPaste();
544 updateLocal(bv, true);
548 bv->lockedInsetStoreUndo(Undo::DELETE);
549 mathcursor->selCut();
550 updateLocal(bv, true);
554 mathcursor->selCopy();
558 // Special casing for superscript in case of LyX handling
560 case LFUN_CIRCUMFLEX:
561 if (cmd.argument.empty()) {
562 // do superscript if LyX handles
564 bv->lockedInsetStoreUndo(Undo::EDIT);
565 mathcursor->script(true);
566 updateLocal(bv, true);
583 case LFUN_HUNG_UMLAUT:
587 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
588 case LFUN_BOLD: handleFont(bv, cmd.argument, "textbf"); break;
589 case LFUN_SANS: handleFont(bv, cmd.argument, "textsf"); break;
590 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
591 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
592 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
593 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
594 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
595 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
596 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
597 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
600 handleFont(bv, cmd.argument, "lyxgreek1");
601 if (cmd.argument.size())
602 mathcursor->interpret(cmd.argument);
606 if (mathcursor->currentMode())
607 handleFont(bv, cmd.argument, "textrm");
609 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
610 updateLocal(bv, true);
612 //bv->owner()->message(_("math text mode toggled"));
615 case LFUN_MATH_LIMITS:
616 bv->lockedInsetStoreUndo(Undo::EDIT);
617 if (mathcursor->toggleLimits())
618 updateLocal(bv, true);
624 bv->lockedInsetStoreUndo(Undo::EDIT);
625 mathcursor->setSize(arg);
626 updateLocal(bv, true);
631 case LFUN_INSERT_MATRIX:
632 if (!cmd.argument.empty()) {
633 bv->lockedInsetStoreUndo(Undo::EDIT);
634 mathcursor->interpret("matrix " + cmd.argument);
635 updateLocal(bv, true);
639 case LFUN_SUPERSCRIPT:
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
644 updateLocal(bv, true);
648 case LFUN_MATH_DELIM:
650 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
652 string rs = split(cmd.argument, ls, ' ');
653 // Reasonable default values
659 bv->lockedInsetStoreUndo(Undo::EDIT);
660 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
661 updateLocal(bv, true);
665 case LFUN_PROTECTEDSPACE:
666 case LFUN_MATH_SPACE:
667 bv->lockedInsetStoreUndo(Undo::EDIT);
668 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
669 updateLocal(bv, true);
673 bv->owner()->message(_("Invalid action in math mode!"));
677 case LFUN_EXEC_COMMAND:
678 result = UNDISPATCHED;
682 // interpret this as if a backslash was typed
683 bv->lockedInsetStoreUndo(Undo::EDIT);
684 mathcursor->interpret('\\');
685 updateLocal(bv, true);
688 case LFUN_BREAKPARAGRAPH:
689 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
690 case LFUN_BREAKPARAGRAPH_SKIP:
695 case LFUN_INSERT_MATH:
696 case LFUN_SELFINSERT:
697 if (!argument.empty()) {
698 bv->lockedInsetStoreUndo(Undo::EDIT);
699 if (argument.size() == 1)
700 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
702 result = mathcursor->interpret(argument) ? DISPATCHED : FINISHED_RIGHT;
703 updateLocal(bv, true);
707 case LFUN_MATH_PANEL:
708 result = UNDISPATCHED;
712 if (mathcursor->selection())
713 mathcursor->selClear();
715 result = UNDISPATCHED;
718 case LFUN_INSET_TOGGLE:
719 mathcursor->insetToggle();
720 updateLocal(bv, true);
723 case LFUN_REF_INSERT:
724 //if (argument.empty()) {
725 // InsetCommandParams p("ref");
726 // owner_->getDialogs().createRef(p.getAsString());
728 // InsetCommandParams p;
729 // p.setFromString(argument);
731 // InsetRef * inset = new InsetRef(p, *buffer_);
732 // if (!insertInset(inset))
735 // updateInset(inset, true);
738 if (cmd.argument.empty()) {
739 InsetCommandParams p("ref");
740 bv->owner()->getDialogs().createRef(p.getAsString());
742 //mathcursor->handleNest(new InsetRef2);
743 //mathcursor->insert(arg);
744 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
746 updateLocal(bv, true);
750 result = UNDISPATCHED;
753 mathcursor->normalize();
756 lyx::Assert(mathcursor);
758 if (mathcursor->selection() || was_selection)
759 toggleInsetSelection(bv);
761 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
762 result == UNDISPATCHED) {
767 releaseMathCursor(bv);
768 bv->unlockInset(this);
771 return result; // original version
775 void InsetFormulaBase::revealCodes(BufferView * bv) const
779 bv->owner()->message(mathcursor->info());
782 // write something to the minibuffer
783 // translate to latex
784 mathcursor->markInsert();
787 string str = os.str();
788 mathcursor->markErase();
789 string::size_type pos = 0;
791 for (string::iterator it = str.begin(); it != str.end(); ++it) {
794 else if (*it == '\0') {
796 pos = it - str.begin();
802 res = res.substr(pos - 30);
804 res = res.substr(0, 60);
805 bv->owner()->message(res);
810 Inset::Code InsetFormulaBase::lyxCode() const
812 return Inset::MATH_CODE;
816 int InsetFormulaBase::ylow() const
818 return yo_ - ascent(view(), font_);
822 int InsetFormulaBase::yhigh() const
824 return yo_ + descent(view(), font_);
828 int InsetFormulaBase::xlow() const
834 int InsetFormulaBase::xhigh() const
836 return xo_ + width(view(), font_);
840 /////////////////////////////////////////////////////////////////////
843 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
849 static InsetFormulaBase * lastformula = 0;
850 static MathIterator current = MathIterator(ibegin(par().nucleus()));
852 static string laststr;
854 if (lastformula != this || laststr != str) {
855 //lyxerr << "reset lastformula to " << this << "\n";
858 current = ibegin(par().nucleus());
860 mathed_parse_cell(ar, str);
864 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
866 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
867 if (it.cell().matchpart(ar, it.back().pos_)) {
868 bv->unlockInset(bv->theLockingInset());
869 if (!bv->lockInset(this)) {
870 lyxerr << "Cannot lock inset" << endl;
874 mathcursor = new MathCursor(this, true);
876 mathcursor->setSelection(it, ar.size());
879 updateLocal(bv, false);
884 //lyxerr << "not found!\n";
890 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
893 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
894 return searchForward(bv, what, a, b);
898 bool InsetFormulaBase::display() const
900 return par()->asHullInset() && par()->asHullInset()->display();
904 /////////////////////////////////////////////////////////////////////
907 void mathDispatchCreation(FuncRequest const & cmd, bool display)
909 BufferView * bv = cmd.view();
910 // use selection if available..
912 //if (action == LFUN_MATH_IMPORT_SELECTION)
916 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
919 InsetFormula * f = new InsetFormula(bv);
920 if (openNewInset(bv, f)) {
921 bv->theLockingInset()->
922 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
923 // don't do that also for LFUN_MATH_MODE unless you want end up with
924 // always changing to mathrm when opening an inlined inset
925 // -- I really hate "LyXfunc overloading"...
927 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
928 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
931 // create a macro if we see "\\newcommand" somewhere, and an ordinary
933 InsetFormulaBase * f;
934 if (sel.find("\\newcommand") == string::npos &&
935 sel.find("\\def") == string::npos)
936 f = new InsetFormula(sel);
938 f = new InsetFormulaMacro(sel);
939 bv->getLyXText()->cutSelection(bv);
942 cmd.message(N_("Math editor mode"));
946 void mathDispatch(FuncRequest const & cmd)
948 BufferView * bv = cmd.view();
949 if (!bv->available())
952 switch (cmd.action) {
954 case LFUN_MATH_DISPLAY:
955 mathDispatchCreation(cmd, true);
959 mathDispatchCreation(cmd, false);
962 case LFUN_MATH_IMPORT_SELECTION:
963 mathDispatchCreation(cmd, false);
966 case LFUN_MATH_MACRO:
967 if (cmd.argument.empty())
968 cmd.errorMessage(N_("Missing argument"));
970 string s = cmd.argument;
971 string const s1 = token(s, ' ', 1);
972 int const na = s1.empty() ? 0 : lyx::atoi(s1);
973 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
978 case LFUN_INSERT_MATH:
979 case LFUN_INSERT_MATRIX:
980 case LFUN_MATH_DELIM: {
981 InsetFormula * f = new InsetFormula(bv);
982 if (openNewInset(bv, f)) {
983 bv->theLockingInset()->
984 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
985 bv->theLockingInset()->localDispatch(cmd);