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"
66 MathCursor * mathcursor = 0;
77 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
79 if (!bv->insertInset(new_inset)) {
83 new_inset->edit(bv, true);
92 InsetFormulaBase::InsetFormulaBase()
93 : font_(), xo_(0), yo_(0)
95 // This is needed as long the math parser is not re-entrant
97 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
98 //lyxerr << "sizeof MathMetricsInfo: " << sizeof(MathMetricsInfo) << "\n";
99 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
100 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
108 // translate to latex
110 latex(NULL, os, false, false);
111 string str = os.str();
114 LyXText * lt = view_->getLyXText();
115 string::const_iterator cit = str.begin();
116 string::const_iterator end = str.end();
117 for (; cit != end; ++cit)
118 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
121 //view_->owner()->dispatch(LFUN_ESCAPE);
126 void InsetFormulaBase::handleFont
127 (BufferView * bv, string const & arg, string const & font)
129 bv->lockedInsetStoreUndo(Undo::EDIT);
130 bool sel = mathcursor->selection();
132 updateLocal(bv, true);
133 mathcursor->handleNest(createMathInset(font));
134 mathcursor->insert(arg);
136 updateLocal(bv, false);
140 // Check if uses AMS macros
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
145 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
152 void InsetFormulaBase::metrics(BufferView * bv) const
155 view_ = bv->owner()->view();
157 mi.base.style = LM_ST_TEXT;
158 mi.base.font = font_;
159 mi.base.font.setColor(LColor::math);
164 string const InsetFormulaBase::editMessage() const
166 return _("Math editor mode");
170 void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
172 if (!bv->lockInset(this))
173 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
174 releaseMathCursor(bv);
175 mathcursor = new MathCursor(this, true);
177 mathcursor->setPos(x + xo_, y + yo_);
178 // if that is removed, we won't get the magenta box when entering an
179 // inset for the first time
180 bv->updateInset(this, false);
184 void InsetFormulaBase::edit(BufferView * bv, bool front)
186 if (!bv->lockInset(this))
187 lyxerr << "Cannot lock math inset in edit call!\n";
188 releaseMathCursor(bv);
189 mathcursor = new MathCursor(this, front);
191 bv->updateInset(this, false);
195 void InsetFormulaBase::insetUnlock(BufferView * bv)
198 if (mathcursor->inMacroMode()) {
199 mathcursor->macroModeClose();
200 updateLocal(bv, true);
202 releaseMathCursor(bv);
205 bv->updateInset(this, false);
209 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
211 // calling metrics here destroys the cached xo,yo positions e.g. in
212 // MathParboxinset. And it would be too expensive anyway...
215 lyxerr << "getCursorPos - should not happen";
219 mathcursor->getPos(x, y);
220 x = mathcursor->targetX();
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 mathcursor->getPos(x, y);
244 math_font_max_dim(font_, asc, des);
245 bv->showLockedInsetCursor(x, y - yo_, 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, true);
302 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << "\n";
304 if (cmd.button() == mouse_button::button3) {
305 // try to dispatch to enclosed insets first
306 if (mathcursor->dispatch(cmd) == MathInset::UNDISPATCHED) {
307 // launch math panel for right mouse button
308 bv->owner()->getDialogs().showMathPanel();
313 if (cmd.button() == mouse_button::button2) {
314 mathcursor->selClear();
315 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
316 mathcursor->insert(asArray(bv->getClipboard()));
317 bv->updateInset(this, true);
321 if (cmd.button() == mouse_button::button1) {
322 // try to dispatch to enclosed insets first
323 mathcursor->dispatch(cmd);
324 cmd.view()->stuffClipboard(mathcursor->grabSelection());
325 // try to set the cursor
327 //mathcursor = new MathCursor(this, x == 0);
329 //mathcursor->setPos(x + xo_, y + yo_);
337 Inset::RESULT InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
339 BufferView * bv = cmd.view();
340 releaseMathCursor(bv);
341 mathcursor = new MathCursor(this, cmd.x == 0);
342 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << "\n";
344 if (cmd.button() == mouse_button::button3) {
345 mathcursor->dispatch(cmd);
349 if (cmd.button() == mouse_button::button1) {
350 // just set the cursor here
351 //lyxerr << "setting cursor\n";
355 mathcursor->selClear();
356 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
357 mathcursor->dispatch(cmd);
361 bv->updateInset(this, false);
366 Inset::RESULT InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
371 if (mathcursor->dispatch(FuncRequest(cmd)) != MathInset::UNDISPATCHED)
374 // only select with button 1
375 if (cmd.button() != mouse_button::button1)
378 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
384 if (!mathcursor->selection())
385 mathcursor->selStart();
387 BufferView * bv = cmd.view();
389 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
391 bv->updateInset(this, false);
396 Inset::RESULT InsetFormulaBase::localDispatch(FuncRequest const & cmd)
398 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
399 // << " arg: '" << cmd.argument
400 // << " x: '" << cmd.x
401 // << " y: '" << cmd.y
402 // << "' button: " << cmd.button() << "\n";
404 switch (cmd.action) {
405 case LFUN_MOUSE_PRESS:
406 return lfunMousePress(cmd);
407 case LFUN_MOUSE_MOTION:
408 return lfunMouseMotion(cmd);
409 case LFUN_MOUSE_RELEASE:
410 return lfunMouseRelease(cmd);
411 case LFUN_MOUSE_DOUBLE:
412 //lyxerr << "Mouse double\n";
413 return localDispatch(FuncRequest(LFUN_WORDSEL));
421 BufferView * bv = cmd.view();
422 string argument = cmd.argument;
423 RESULT result = DISPATCHED;
425 bool was_macro = mathcursor->inMacroMode();
426 bool was_selection = mathcursor->selection();
430 mathcursor->normalize();
433 switch (cmd.action) {
435 case LFUN_MATH_MUTATE:
436 case LFUN_MATH_DISPLAY:
437 case LFUN_MATH_NUMBER:
438 case LFUN_MATH_NONUMBER:
441 case LFUN_DELETE_LINE_FORWARD:
442 case LFUN_INSERT_LABEL:
443 case LFUN_MATH_EXTERN:
444 case LFUN_TABULAR_FEATURE:
445 case LFUN_PASTESELECTION:
446 case LFUN_MATH_LIMITS:
447 bv->lockedInsetStoreUndo(Undo::EDIT);
448 mathcursor->dispatch(cmd);
449 updateLocal(bv, true);
453 sel = true; // fall through...
455 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
456 //lyxerr << "calling scroll 20\n";
458 updateLocal(bv, false);
459 // write something to the minibuffer
460 //bv->owner()->message(mathcursor->info());
464 sel = true; // fall through
466 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
467 updateLocal(bv, false);
471 sel = true; // fall through
473 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
474 updateLocal(bv, false);
478 sel = true; // fall through
480 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
481 updateLocal(bv, false);
485 mathcursor->home(false);
486 mathcursor->end(true);
487 updateLocal(bv, false);
491 case LFUN_WORDLEFTSEL:
492 sel = true; // fall through
495 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
496 updateLocal(bv, false);
500 case LFUN_WORDRIGHTSEL:
501 sel = true; // fall through
504 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
505 updateLocal(bv, false);
510 case LFUN_BEGINNINGBUFSEL:
511 case LFUN_BEGINNINGBUF:
512 result = FINISHED_UP;
513 updateLocal(bv, false);
520 result = FINISHED_DOWN;
521 updateLocal(bv, false);
525 mathcursor->idxNext();
526 updateLocal(bv, false);
530 mathcursor->idxPrev();
531 updateLocal(bv, false);
534 case LFUN_DELETE_WORD_BACKWARD:
536 bv->lockedInsetStoreUndo(Undo::DELETE);
537 mathcursor->backspace();
538 updateLocal(bv, true);
541 case LFUN_DELETE_WORD_FORWARD:
543 bv->lockedInsetStoreUndo(Undo::DELETE);
545 bv->updateInset(this, true);
549 // sprintf(dispatch_buffer, "%d %d",);
550 // dispatch_result = dispatch_buffer;
553 lyxerr << "LFUN_SETXY broken!\n";
556 istringstream is(cmd.argument.c_str());
558 mathcursor->setPos(x, y);
559 updateLocal(bv, false);
565 mathcursor->macroModeClose();
566 bv->lockedInsetStoreUndo(Undo::EDIT);
567 mathcursor->selPaste();
568 updateLocal(bv, true);
572 bv->lockedInsetStoreUndo(Undo::DELETE);
573 mathcursor->selCut();
574 updateLocal(bv, true);
578 mathcursor->selCopy();
582 // Special casing for superscript in case of LyX handling
584 case LFUN_CIRCUMFLEX:
585 if (cmd.argument.empty()) {
586 // do superscript if LyX handles
588 bv->lockedInsetStoreUndo(Undo::EDIT);
589 mathcursor->script(true);
590 updateLocal(bv, true);
607 case LFUN_HUNG_UMLAUT:
611 case LFUN_GREEK_TOGGLE: handleFont(bv, cmd.argument, "lyxgreek"); break;
612 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
613 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
614 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
615 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
616 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
617 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
618 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
619 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
620 case LFUN_FREE: handleFont(bv, cmd.argument, "textrm"); break;
621 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
624 handleFont(bv, cmd.argument, "lyxgreek1");
625 if (cmd.argument.size())
626 mathcursor->insert(asArray(cmd.argument));
630 if (mathcursor->currentMode())
631 handleFont(bv, cmd.argument, "textrm");
633 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
634 updateLocal(bv, true);
636 //bv->owner()->message(_("math text mode toggled"));
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->setSize(arg);
644 updateLocal(bv, true);
649 case LFUN_INSERT_MATRIX: {
650 bv->lockedInsetStoreUndo(Undo::EDIT);
655 istringstream is(argument);
656 is >> m >> n >> v_align >> h_align;
660 mathcursor->niceInsert(
661 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
662 updateLocal(bv, true);
666 case LFUN_SUPERSCRIPT:
669 bv->lockedInsetStoreUndo(Undo::EDIT);
670 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
671 updateLocal(bv, true);
675 case LFUN_MATH_DELIM:
677 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
679 string rs = split(cmd.argument, ls, ' ');
680 // Reasonable default values
686 bv->lockedInsetStoreUndo(Undo::EDIT);
687 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
688 updateLocal(bv, true);
692 case LFUN_PROTECTEDSPACE:
693 case LFUN_MATH_SPACE:
694 bv->lockedInsetStoreUndo(Undo::EDIT);
695 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
696 updateLocal(bv, true);
700 bv->owner()->message(_("Invalid action in math mode!"));
704 case LFUN_EXEC_COMMAND:
705 result = UNDISPATCHED;
709 // interpret this as if a backslash was typed
710 bv->lockedInsetStoreUndo(Undo::EDIT);
711 mathcursor->interpret('\\');
712 updateLocal(bv, true);
715 case LFUN_BREAKPARAGRAPH:
716 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
717 case LFUN_BREAKPARAGRAPH_SKIP:
721 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
722 // handling such that "self-insert" works on "arbitrary stuff" too, and
723 // math-insert only handles special math things like "matrix".
724 case LFUN_INSERT_MATH:
725 bv->lockedInsetStoreUndo(Undo::EDIT);
726 mathcursor->niceInsert(argument);
727 updateLocal(bv, true);
731 case LFUN_SELFINSERT:
732 if (!argument.empty()) {
733 bv->lockedInsetStoreUndo(Undo::EDIT);
734 if (argument.size() == 1)
735 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
737 mathcursor->insert(asArray(argument));
738 updateLocal(bv, true);
742 case LFUN_MATH_PANEL:
743 result = UNDISPATCHED;
747 if (mathcursor->selection())
748 mathcursor->selClear();
750 result = UNDISPATCHED;
753 case LFUN_INSET_TOGGLE:
754 mathcursor->insetToggle();
755 updateLocal(bv, true);
758 case LFUN_REF_INSERT:
759 //if (argument.empty()) {
760 // InsetCommandParams p("ref");
761 // owner_->getDialogs().createRef(p.getAsString());
763 // InsetCommandParams p;
764 // p.setFromString(argument);
766 // InsetRef * inset = new InsetRef(p, *buffer_);
767 // if (!insertInset(inset))
770 // updateInset(inset, true);
773 if (cmd.argument.empty()) {
774 InsetCommandParams p("ref");
775 bv->owner()->getDialogs().createRef(p.getAsString());
777 //mathcursor->handleNest(new InsetRef2);
778 //mathcursor->insert(arg);
779 mathcursor->insert(MathAtom(new RefInset(cmd.argument)));
781 updateLocal(bv, true);
785 result = UNDISPATCHED;
788 mathcursor->normalize();
791 lyx::Assert(mathcursor);
793 if (mathcursor->selection() || was_selection)
794 toggleInsetSelection(bv);
796 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
797 result == UNDISPATCHED) {
801 cmd.view()->stuffClipboard(mathcursor->grabSelection());
803 releaseMathCursor(bv);
804 bv->unlockInset(this);
807 return result; // original version
811 void InsetFormulaBase::revealCodes(BufferView * bv) const
815 bv->owner()->message(mathcursor->info());
818 // write something to the minibuffer
819 // translate to latex
820 mathcursor->markInsert();
823 string str = os.str();
824 mathcursor->markErase();
825 string::size_type pos = 0;
827 for (string::iterator it = str.begin(); it != str.end(); ++it) {
830 else if (*it == '\0') {
832 pos = it - str.begin();
838 res = res.substr(pos - 30);
840 res = res.substr(0, 60);
841 bv->owner()->message(res);
846 Inset::Code InsetFormulaBase::lyxCode() const
848 return Inset::MATH_CODE;
852 int InsetFormulaBase::ylow() const
854 return yo_ - ascent(view(), font_);
858 int InsetFormulaBase::yhigh() const
860 return yo_ + descent(view(), font_);
864 int InsetFormulaBase::xlow() const
870 int InsetFormulaBase::xhigh() const
872 return xo_ + width(view(), font_);
876 /////////////////////////////////////////////////////////////////////
879 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
885 static InsetFormulaBase * lastformula = 0;
886 static MathIterator current = MathIterator(ibegin(par().nucleus()));
888 static string laststr;
890 if (lastformula != this || laststr != str) {
891 //lyxerr << "reset lastformula to " << this << "\n";
894 current = ibegin(par().nucleus());
896 mathed_parse_cell(ar, str);
900 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
902 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
903 if (it.cell().matchpart(ar, it.back().pos_)) {
904 bv->unlockInset(bv->theLockingInset());
905 if (!bv->lockInset(this)) {
906 lyxerr << "Cannot lock inset" << endl;
910 mathcursor = new MathCursor(this, true);
912 mathcursor->setSelection(it, ar.size());
915 updateLocal(bv, false);
920 //lyxerr << "not found!\n";
926 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
929 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
930 return searchForward(bv, what, a, b);
934 bool InsetFormulaBase::display() const
936 return par()->asHullInset() && par()->asHullInset()->display();
940 string InsetFormulaBase::selectionAsString() const
942 return mathcursor ? mathcursor->grabSelection() : string();
945 /////////////////////////////////////////////////////////////////////
948 void mathDispatchCreation(FuncRequest const & cmd, bool display)
950 BufferView * bv = cmd.view();
951 // use selection if available..
953 //if (action == LFUN_MATH_IMPORT_SELECTION)
957 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
960 InsetFormula * f = new InsetFormula(bv);
961 if (openNewInset(bv, f)) {
962 bv->theLockingInset()->
963 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
964 // don't do that also for LFUN_MATH_MODE unless you want end up with
965 // always changing to mathrm when opening an inlined inset
966 // -- I really hate "LyXfunc overloading"...
968 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
969 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
972 // create a macro if we see "\\newcommand" somewhere, and an ordinary
974 InsetFormulaBase * f;
975 if (sel.find("\\newcommand") == string::npos &&
976 sel.find("\\def") == string::npos)
977 f = new InsetFormula(sel);
979 f = new InsetFormulaMacro(sel);
980 bv->getLyXText()->cutSelection(bv);
983 cmd.message(N_("Math editor mode"));
987 void mathDispatch(FuncRequest const & cmd)
989 BufferView * bv = cmd.view();
990 if (!bv->available())
993 switch (cmd.action) {
995 case LFUN_MATH_DISPLAY:
996 mathDispatchCreation(cmd, true);
1000 mathDispatchCreation(cmd, false);
1003 case LFUN_MATH_IMPORT_SELECTION:
1004 mathDispatchCreation(cmd, false);
1007 case LFUN_MATH_MACRO:
1008 if (cmd.argument.empty())
1009 cmd.errorMessage(N_("Missing argument"));
1011 string s = cmd.argument;
1012 string const s1 = token(s, ' ', 1);
1013 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1014 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1019 case LFUN_INSERT_MATH:
1020 case LFUN_INSERT_MATRIX:
1021 case LFUN_MATH_DELIM: {
1022 InsetFormula * f = new InsetFormula(bv);
1023 if (openNewInset(bv, f)) {
1024 bv->theLockingInset()->
1025 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
1026 bv->theLockingInset()->localDispatch(cmd);