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.
18 #include "support/LAssert.h"
20 #include "formulamacro.h"
22 #include "funcrequest.h"
23 #include "BufferView.h"
26 #include "LaTeXFeatures.h"
28 #include "math_support.h"
29 #include "metricsinfo.h"
30 #include "support/lstrings.h"
31 #include "frontends/LyXView.h"
32 #include "frontends/font_metrics.h"
33 #include "frontends/mouse_state.h"
35 #include "math_arrayinset.h"
36 #include "math_charinset.h"
37 #include "math_deliminset.h"
38 #include "math_cursor.h"
39 #include "math_factory.h"
40 #include "math_fontinset.h"
41 #include "math_hullinset.h"
42 #include "math_iterator.h"
43 #include "math_macrotable.h"
44 #include "math_parser.h"
46 #include "math_spaceinset.h"
47 #include "undo_funcs.h"
48 #include "textpainter.h"
49 #include "frontends/Dialogs.h"
51 #include "ref_inset.h"
62 MathCursor * mathcursor = 0;
70 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
72 if (!bv->insertInset(new_inset)) {
76 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
85 InsetFormulaBase::InsetFormulaBase()
86 : font_(), xo_(0), yo_(0)
88 // This is needed as long the math parser is not re-entrant
90 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
91 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << "\n";
92 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
93 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
97 // simply scrap this function if you want
98 void InsetFormulaBase::mutateToText()
101 // translate to latex
103 latex(NULL, os, false, false);
104 string str = os.str();
107 LyXText * lt = view_->getLyXText();
108 string::const_iterator cit = str.begin();
109 string::const_iterator end = str.end();
110 for (; cit != end; ++cit)
111 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
114 //view_->owner()->dispatch(LFUN_ESCAPE);
119 void InsetFormulaBase::handleFont
120 (BufferView * bv, string const & arg, string const & font)
122 // this whole function is a hack and won't work for incremental font
124 bv->lockedInsetStoreUndo(Undo::EDIT);
125 if (mathcursor->par()->name() == font)
126 mathcursor->handleFont(font);
128 mathcursor->handleNest(createMathInset(font));
129 mathcursor->insert(arg);
134 BufferView * InsetFormulaBase::view() const
136 return view_.lock().get();
140 void InsetFormulaBase::validate(LaTeXFeatures &) const
144 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f,
145 Dimension & dim) const
152 void InsetFormulaBase::metrics(BufferView * bv) const
155 view_ = bv->owner()->view();
158 mi.base.style = LM_ST_TEXT;
159 mi.base.font = font_;
160 mi.base.font.setColor(LColor::math);
165 string const InsetFormulaBase::editMessage() const
167 return _("Math editor mode");
171 void InsetFormulaBase::insetUnlock(BufferView * bv)
174 if (mathcursor->inMacroMode()) {
175 mathcursor->macroModeClose();
176 bv->updateInset(this);
178 releaseMathCursor(bv);
181 bv->updateInset(this);
185 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
187 mathcursor->getPos(x, y);
191 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
193 // calling metrics here destroys the cached xo,yo positions e.g. in
194 // MathParboxinset. And it would be too expensive anyway...
197 lyxerr << "getCursorPos - should not happen";
201 mathcursor->getPos(x, y);
202 x = mathcursor->targetX();
205 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
209 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
214 math_font_max_dim(font_, asc, des);
215 getCursorPos(bv, x, y);
217 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
218 bv->fitLockedInsetCursor(x, y, asc, des);
222 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
225 bv->updateInset(this);
229 vector<string> const InsetFormulaBase::getLabelList() const
231 return vector<string>();
235 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
240 BufferView * bv = cmd.view();
241 bv->updateInset(this);
242 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
244 if (cmd.button() == mouse_button::button3) {
245 // try to dispatch to enclosed insets first
246 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
247 // launch math panel for right mouse button
248 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
249 bv->owner()->getDialogs().showMathPanel();
254 if (cmd.button() == mouse_button::button2) {
255 mathcursor->selClear();
256 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
257 mathcursor->insert(asArray(bv->getClipboard()));
258 bv->updateInset(this);
262 if (cmd.button() == mouse_button::button1) {
263 // try to dispatch to enclosed insets first
264 mathcursor->dispatch(cmd);
265 cmd.view()->stuffClipboard(mathcursor->grabSelection());
266 // try to set the cursor
268 //mathcursor = new MathCursor(this, x == 0);
270 //mathcursor->setPos(x + xo_, y + yo_);
278 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
280 BufferView * bv = cmd.view();
281 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
283 if (!mathcursor || mathcursor->formula() != this) {
284 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
285 releaseMathCursor(bv);
286 mathcursor = new MathCursor(this, cmd.x == 0);
288 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
291 if (cmd.button() == mouse_button::button3) {
292 mathcursor->dispatch(cmd);
296 if (cmd.button() == mouse_button::button1) {
299 mathcursor->selClear();
300 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
301 mathcursor->dispatch(cmd);
305 bv->updateInset(this);
310 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
315 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
318 // only select with button 1
319 if (cmd.button() != mouse_button::button1)
322 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
328 if (!mathcursor->selection())
329 mathcursor->selStart();
331 BufferView * bv = cmd.view();
332 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
333 bv->updateInset(this);
338 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
340 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
341 // << " arg: '" << cmd.argument
342 // << " x: '" << cmd.x
343 // << " y: '" << cmd.y
344 // << "' button: " << cmd.button() << endl;
346 BufferView * bv = cmd.view();
348 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
349 bool remove_inset = false;
351 switch (cmd.action) {
352 case LFUN_INSET_EDIT:
353 lyxerr << "Called EDIT with '" << cmd.argument << "'\n";
354 if (!bv->lockInset(this))
355 lyxerr << "Cannot lock math inset in edit call!\n";
356 releaseMathCursor(bv);
357 if (!cmd.argument.empty()) {
358 mathcursor = new MathCursor(this, cmd.argument == "left");
361 mathcursor = new MathCursor(this, true);
363 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
365 // if that is removed, we won't get the magenta box when entering an
366 // inset for the first time
367 bv->updateInset(this);
370 case LFUN_MOUSE_PRESS:
371 //lyxerr << "Mouse single press\n";
372 return lfunMousePress(cmd);
373 case LFUN_MOUSE_MOTION:
374 //lyxerr << "Mouse motion\n";
375 return lfunMouseMotion(cmd);
376 case LFUN_MOUSE_RELEASE:
377 //lyxerr << "Mouse single release\n";
378 return lfunMouseRelease(cmd);
379 case LFUN_MOUSE_DOUBLE:
380 //lyxerr << "Mouse double\n";
381 return localDispatch(FuncRequest(LFUN_WORDSEL));
389 string argument = cmd.argument;
390 RESULT result = DISPATCHED;
392 bool was_macro = mathcursor->inMacroMode();
393 bool was_selection = mathcursor->selection();
395 mathcursor->normalize();
398 switch (cmd.action) {
400 case LFUN_MATH_MUTATE:
401 case LFUN_MATH_DISPLAY:
402 case LFUN_MATH_NUMBER:
403 case LFUN_MATH_NONUMBER:
404 case LFUN_CELL_SPLIT:
406 case LFUN_DELETE_LINE_FORWARD:
407 case LFUN_INSERT_LABEL:
408 case LFUN_MATH_EXTERN:
409 case LFUN_TABULAR_FEATURE:
410 case LFUN_PASTESELECTION:
411 case LFUN_MATH_LIMITS:
412 bv->lockedInsetStoreUndo(Undo::EDIT);
413 mathcursor->dispatch(cmd);
417 sel = true; // fall through...
419 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
420 //lyxerr << "calling scroll 20\n";
422 // write something to the minibuffer
423 //bv->owner()->message(mathcursor->info());
427 sel = true; // fall through
429 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
433 sel = true; // fall through
435 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
439 sel = true; // fall through
441 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
445 mathcursor->home(false);
446 mathcursor->end(true);
449 case LFUN_UP_PARAGRAPHSEL:
450 case LFUN_UP_PARAGRAPH:
451 case LFUN_DOWN_PARAGRAPHSEL:
452 case LFUN_DOWN_PARAGRAPH:
457 case LFUN_WORDLEFTSEL:
458 sel = true; // fall through
461 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
465 case LFUN_WORDRIGHTSEL:
466 sel = true; // fall through
469 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
474 case LFUN_BEGINNINGBUFSEL:
475 case LFUN_BEGINNINGBUF:
483 result = FINISHED_RIGHT;
486 case LFUN_CELL_FORWARD:
487 mathcursor->idxNext();
490 case LFUN_CELL_BACKWARD:
491 mathcursor->idxPrev();
494 case LFUN_DELETE_WORD_BACKWARD:
496 bv->lockedInsetStoreUndo(Undo::EDIT);
497 if (!mathcursor->backspace()) {
503 case LFUN_DELETE_WORD_FORWARD:
505 bv->lockedInsetStoreUndo(Undo::EDIT);
506 if (!mathcursor->erase()) {
513 // sprintf(dispatch_buffer, "%d %d",);
514 // dispatch_result = dispatch_buffer;
517 lyxerr << "LFUN_SETXY broken!\n";
520 istringstream is(cmd.argument.c_str());
522 mathcursor->setPos(x, y);
528 istringstream is(cmd.argument.c_str());
531 mathcursor->macroModeClose();
532 bv->lockedInsetStoreUndo(Undo::EDIT);
533 mathcursor->selPaste(n);
538 bv->lockedInsetStoreUndo(Undo::DELETE);
539 mathcursor->selCut();
543 mathcursor->selCopy();
547 // Special casing for superscript in case of LyX handling
549 case LFUN_CIRCUMFLEX:
550 if (cmd.argument.empty()) {
551 // do superscript if LyX handles
553 bv->lockedInsetStoreUndo(Undo::EDIT);
554 mathcursor->script(true);
571 case LFUN_HUNG_UMLAUT:
575 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
576 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
577 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
578 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
579 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
580 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
581 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
582 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
583 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
584 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
587 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
588 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
590 handleFont(bv, cmd.argument, "textrm");
591 //bv->owner()->message(_("math text mode toggled"));
597 bv->lockedInsetStoreUndo(Undo::EDIT);
598 mathcursor->setSize(arg);
603 case LFUN_INSERT_MATRIX: {
604 bv->lockedInsetStoreUndo(Undo::EDIT);
609 istringstream is(STRCONV(argument));
610 is >> m >> n >> v_align >> h_align;
614 mathcursor->niceInsert(
615 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
619 case LFUN_SUPERSCRIPT:
622 bv->lockedInsetStoreUndo(Undo::EDIT);
623 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
627 case LFUN_MATH_DELIM:
629 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
631 string rs = split(cmd.argument, ls, ' ');
632 // Reasonable default values
638 bv->lockedInsetStoreUndo(Undo::EDIT);
639 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
643 case LFUN_SPACE_INSERT:
644 case LFUN_MATH_SPACE:
645 bv->lockedInsetStoreUndo(Undo::EDIT);
646 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
650 bv->owner()->message(_("Invalid action in math mode!"));
654 case LFUN_EXEC_COMMAND:
655 result = UNDISPATCHED;
659 // interpret this as if a backslash was typed
660 bv->lockedInsetStoreUndo(Undo::EDIT);
661 mathcursor->interpret('\\');
664 case LFUN_BREAKPARAGRAPH:
665 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
666 case LFUN_BREAKPARAGRAPH_SKIP:
670 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
671 // handling such that "self-insert" works on "arbitrary stuff" too, and
672 // math-insert only handles special math things like "matrix".
673 case LFUN_INSERT_MATH:
674 bv->lockedInsetStoreUndo(Undo::EDIT);
675 mathcursor->niceInsert(argument);
679 case LFUN_SELFINSERT:
680 if (!argument.empty()) {
681 bv->lockedInsetStoreUndo(Undo::EDIT);
682 if (argument.size() == 1)
683 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
685 mathcursor->insert(asArray(argument));
689 case LFUN_MATH_PANEL:
690 result = UNDISPATCHED;
694 if (mathcursor->selection())
695 mathcursor->selClear();
697 result = UNDISPATCHED;
700 case LFUN_INSET_TOGGLE:
701 mathcursor->insetToggle();
704 case LFUN_DIALOG_SHOW_NEW_INSET: {
705 string const & name = argument;
709 data = tmp.createDialogStr(name);
713 result = UNDISPATCHED;
715 bv->owner()->getDialogs().show(name, data, 0);
720 case LFUN_INSET_APPLY: {
721 string const name = cmd.getArg(0);
723 bv->owner()->getDialogs().getOpenInset(name);
726 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
727 result = base->localDispatch(fr);
730 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
731 mathcursor->insert(ar);
734 result = UNDISPATCHED;
741 result = UNDISPATCHED;
744 if (result == DISPATCHED)
745 bv->updateInset(this);
747 mathcursor->normalize();
750 lyx::Assert(mathcursor);
752 if (mathcursor->selection() || was_selection)
753 toggleInsetSelection(bv);
755 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
756 result == UNDISPATCHED) {
759 cmd.view()->stuffClipboard(mathcursor->grabSelection());
761 releaseMathCursor(bv);
762 bv->unlockInset(this);
764 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
767 return result; // original version
771 void InsetFormulaBase::revealCodes(BufferView * bv) const
775 bv->owner()->message(mathcursor->info());
778 // write something to the minibuffer
779 // translate to latex
780 mathcursor->markInsert();
783 string str = os.str();
784 mathcursor->markErase();
785 string::size_type pos = 0;
787 for (string::iterator it = str.begin(); it != str.end(); ++it) {
790 else if (*it == '\0') {
792 pos = it - str.begin();
798 res = res.substr(pos - 30);
800 res = res.substr(0, 60);
801 bv->owner()->message(res);
806 Inset::Code InsetFormulaBase::lyxCode() const
808 return Inset::MATH_CODE;
812 int InsetFormulaBase::ylow() const
814 return yo_ - ascent(view(), font_);
818 int InsetFormulaBase::yhigh() const
820 return yo_ + descent(view(), font_);
824 int InsetFormulaBase::xlow() const
830 int InsetFormulaBase::xhigh() const
832 return xo_ + width(view(), font_);
836 /////////////////////////////////////////////////////////////////////
839 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
845 static InsetFormulaBase * lastformula = 0;
846 static MathIterator current = MathIterator(ibegin(par().nucleus()));
848 static string laststr;
850 if (lastformula != this || laststr != str) {
851 //lyxerr << "reset lastformula to " << this << "\n";
854 current = ibegin(par().nucleus());
856 mathed_parse_cell(ar, str);
860 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
862 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
863 if (it.cell().matchpart(ar, it.back().pos_)) {
864 bv->unlockInset(bv->theLockingInset());
865 if (!bv->lockInset(this)) {
866 lyxerr << "Cannot lock inset" << endl;
870 mathcursor = new MathCursor(this, true);
872 mathcursor->setSelection(it, ar.size());
875 bv->updateInset(this);
880 //lyxerr << "not found!\n";
886 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
889 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
890 return searchForward(bv, what, a, b);
894 bool InsetFormulaBase::display() const
896 return par()->asHullInset() && par()->asHullInset()->display();
900 string InsetFormulaBase::selectionAsString() const
902 return mathcursor ? mathcursor->grabSelection() : string();
905 /////////////////////////////////////////////////////////////////////
908 void mathDispatchCreation(FuncRequest const & cmd, bool display)
910 BufferView * bv = cmd.view();
911 // use selection if available..
913 //if (action == LFUN_MATH_IMPORT_SELECTION)
917 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
920 InsetFormula * f = new InsetFormula(bv);
921 if (openNewInset(bv, f)) {
922 bv->theLockingInset()->
923 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
924 // don't do that also for LFUN_MATH_MODE unless you want end up with
925 // always changing to mathrm when opening an inlined inset
926 // -- I really hate "LyXfunc overloading"...
928 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
929 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
932 // create a macro if we see "\\newcommand" somewhere, and an ordinary
934 InsetFormulaBase * f;
935 if (sel.find("\\newcommand") == string::npos &&
936 sel.find("\\def") == string::npos)
937 f = new InsetFormula(sel);
939 f = new InsetFormulaMacro(sel);
940 bv->getLyXText()->cutSelection(bv);
943 cmd.message(N_("Math editor mode"));
947 void mathDispatch(FuncRequest const & cmd)
949 BufferView * bv = cmd.view();
950 if (!bv->available())
953 switch (cmd.action) {
955 case LFUN_MATH_DISPLAY:
956 mathDispatchCreation(cmd, true);
960 mathDispatchCreation(cmd, false);
963 case LFUN_MATH_IMPORT_SELECTION:
964 mathDispatchCreation(cmd, false);
967 case LFUN_MATH_MACRO:
968 if (cmd.argument.empty())
969 cmd.errorMessage(N_("Missing argument"));
971 string s = cmd.argument;
972 string const s1 = token(s, ' ', 1);
973 int const na = s1.empty() ? 0 : lyx::atoi(s1);
974 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);