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) 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::insetUnlock(BufferView * bv)
172 if (mathcursor->inMacroMode()) {
173 mathcursor->macroModeClose();
174 bv->updateInset(this);
176 releaseMathCursor(bv);
179 bv->updateInset(this);
183 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
185 mathcursor->getPos(x, y);
189 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
191 // calling metrics here destroys the cached xo,yo positions e.g. in
192 // MathParboxinset. And it would be too expensive anyway...
195 lyxerr << "getCursorPos - should not happen";
199 mathcursor->getPos(x, y);
200 x = mathcursor->targetX();
203 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
207 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
212 math_font_max_dim(font_, asc, des);
213 getCursorPos(bv, x, y);
215 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
216 bv->fitLockedInsetCursor(x, y, asc, des);
220 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
223 bv->updateInset(this);
227 vector<string> const InsetFormulaBase::getLabelList() const
229 return vector<string>();
233 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
238 BufferView * bv = cmd.view();
239 bv->updateInset(this);
240 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
242 if (cmd.button() == mouse_button::button3) {
243 // try to dispatch to enclosed insets first
244 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
245 // launch math panel for right mouse button
246 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
247 bv->owner()->getDialogs().showMathPanel();
252 if (cmd.button() == mouse_button::button2) {
253 mathcursor->selClear();
254 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
255 mathcursor->insert(asArray(bv->getClipboard()));
256 bv->updateInset(this);
260 if (cmd.button() == mouse_button::button1) {
261 // try to dispatch to enclosed insets first
262 mathcursor->dispatch(cmd);
263 cmd.view()->stuffClipboard(mathcursor->grabSelection());
264 // try to set the cursor
266 //mathcursor = new MathCursor(this, x == 0);
268 //mathcursor->setPos(x + xo_, y + yo_);
276 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
278 BufferView * bv = cmd.view();
279 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
281 if (!mathcursor || mathcursor->formula() != this) {
282 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
283 releaseMathCursor(bv);
284 mathcursor = new MathCursor(this, cmd.x == 0);
286 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
289 if (cmd.button() == mouse_button::button3) {
290 mathcursor->dispatch(cmd);
294 if (cmd.button() == mouse_button::button1) {
297 mathcursor->selClear();
298 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
299 mathcursor->dispatch(cmd);
303 bv->updateInset(this);
308 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
313 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
316 // only select with button 1
317 if (cmd.button() != mouse_button::button1)
320 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
326 if (!mathcursor->selection())
327 mathcursor->selStart();
329 BufferView * bv = cmd.view();
330 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
331 bv->updateInset(this);
336 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
338 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
339 // << " arg: '" << cmd.argument
340 // << " x: '" << cmd.x
341 // << " y: '" << cmd.y
342 // << "' button: " << cmd.button() << endl;
344 BufferView * bv = cmd.view();
346 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
347 bool remove_inset = false;
349 switch (cmd.action) {
350 case LFUN_INSET_EDIT:
351 if (!bv->lockInset(this))
352 lyxerr << "Cannot lock math inset in edit call!\n";
353 releaseMathCursor(bv);
354 if (!cmd.argument.empty()) {
355 mathcursor = new MathCursor(this, cmd.argument == "left");
358 mathcursor = new MathCursor(this, true);
360 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
362 // if that is removed, we won't get the magenta box when entering an
363 // inset for the first time
364 bv->updateInset(this);
367 case LFUN_MOUSE_PRESS:
368 //lyxerr << "Mouse single press\n";
369 return lfunMousePress(cmd);
370 case LFUN_MOUSE_MOTION:
371 //lyxerr << "Mouse motion\n";
372 return lfunMouseMotion(cmd);
373 case LFUN_MOUSE_RELEASE:
374 //lyxerr << "Mouse single release\n";
375 return lfunMouseRelease(cmd);
376 case LFUN_MOUSE_DOUBLE:
377 //lyxerr << "Mouse double\n";
378 return localDispatch(FuncRequest(LFUN_WORDSEL));
386 string argument = cmd.argument;
387 RESULT result = DISPATCHED;
389 bool was_macro = mathcursor->inMacroMode();
390 bool was_selection = mathcursor->selection();
392 mathcursor->normalize();
395 switch (cmd.action) {
397 case LFUN_MATH_MUTATE:
398 case LFUN_MATH_DISPLAY:
399 case LFUN_MATH_NUMBER:
400 case LFUN_MATH_NONUMBER:
401 case LFUN_CELL_SPLIT:
403 case LFUN_DELETE_LINE_FORWARD:
404 case LFUN_INSERT_LABEL:
405 case LFUN_MATH_EXTERN:
406 case LFUN_TABULAR_FEATURE:
407 case LFUN_PASTESELECTION:
408 case LFUN_MATH_LIMITS:
409 bv->lockedInsetStoreUndo(Undo::EDIT);
410 mathcursor->dispatch(cmd);
414 sel = true; // fall through...
416 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
417 //lyxerr << "calling scroll 20\n";
419 // write something to the minibuffer
420 //bv->owner()->message(mathcursor->info());
424 sel = true; // fall through
426 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
430 sel = true; // fall through
432 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
436 sel = true; // fall through
438 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
442 mathcursor->home(false);
443 mathcursor->end(true);
446 case LFUN_UP_PARAGRAPHSEL:
447 case LFUN_UP_PARAGRAPH:
448 case LFUN_DOWN_PARAGRAPHSEL:
449 case LFUN_DOWN_PARAGRAPH:
454 case LFUN_WORDLEFTSEL:
455 sel = true; // fall through
458 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
462 case LFUN_WORDRIGHTSEL:
463 sel = true; // fall through
466 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
471 case LFUN_BEGINNINGBUFSEL:
472 case LFUN_BEGINNINGBUF:
480 result = FINISHED_RIGHT;
483 case LFUN_CELL_FORWARD:
484 mathcursor->idxNext();
487 case LFUN_CELL_BACKWARD:
488 mathcursor->idxPrev();
491 case LFUN_DELETE_WORD_BACKWARD:
493 bv->lockedInsetStoreUndo(Undo::EDIT);
494 if (!mathcursor->backspace()) {
500 case LFUN_DELETE_WORD_FORWARD:
502 bv->lockedInsetStoreUndo(Undo::EDIT);
503 if (!mathcursor->erase()) {
510 // sprintf(dispatch_buffer, "%d %d",);
511 // dispatch_result = dispatch_buffer;
514 lyxerr << "LFUN_SETXY broken!\n";
517 istringstream is(cmd.argument.c_str());
519 mathcursor->setPos(x, y);
525 istringstream is(cmd.argument.c_str());
528 mathcursor->macroModeClose();
529 bv->lockedInsetStoreUndo(Undo::EDIT);
530 mathcursor->selPaste(n);
535 bv->lockedInsetStoreUndo(Undo::DELETE);
536 mathcursor->selCut();
540 mathcursor->selCopy();
544 // Special casing for superscript in case of LyX handling
546 case LFUN_CIRCUMFLEX:
547 if (cmd.argument.empty()) {
548 // do superscript if LyX handles
550 bv->lockedInsetStoreUndo(Undo::EDIT);
551 mathcursor->script(true);
568 case LFUN_HUNG_UMLAUT:
572 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
573 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
574 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
575 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
576 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
577 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
578 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
579 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
580 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
581 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
584 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
585 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
587 handleFont(bv, cmd.argument, "textrm");
588 //bv->owner()->message(_("math text mode toggled"));
594 bv->lockedInsetStoreUndo(Undo::EDIT);
595 mathcursor->setSize(arg);
600 case LFUN_INSERT_MATRIX: {
601 bv->lockedInsetStoreUndo(Undo::EDIT);
606 istringstream is(STRCONV(argument));
607 is >> m >> n >> v_align >> h_align;
611 mathcursor->niceInsert(
612 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
616 case LFUN_SUPERSCRIPT:
619 bv->lockedInsetStoreUndo(Undo::EDIT);
620 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
624 case LFUN_MATH_DELIM:
626 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
628 string rs = split(cmd.argument, ls, ' ');
629 // Reasonable default values
635 bv->lockedInsetStoreUndo(Undo::EDIT);
636 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
640 case LFUN_PROTECTEDSPACE:
641 case LFUN_MATH_SPACE:
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
647 bv->owner()->message(_("Invalid action in math mode!"));
651 case LFUN_EXEC_COMMAND:
652 result = UNDISPATCHED;
656 // interpret this as if a backslash was typed
657 bv->lockedInsetStoreUndo(Undo::EDIT);
658 mathcursor->interpret('\\');
661 case LFUN_BREAKPARAGRAPH:
662 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
663 case LFUN_BREAKPARAGRAPH_SKIP:
667 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
668 // handling such that "self-insert" works on "arbitrary stuff" too, and
669 // math-insert only handles special math things like "matrix".
670 case LFUN_INSERT_MATH:
671 bv->lockedInsetStoreUndo(Undo::EDIT);
672 mathcursor->niceInsert(argument);
676 case LFUN_SELFINSERT:
677 if (!argument.empty()) {
678 bv->lockedInsetStoreUndo(Undo::EDIT);
679 if (argument.size() == 1)
680 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
682 mathcursor->insert(asArray(argument));
686 case LFUN_MATH_PANEL:
687 result = UNDISPATCHED;
691 if (mathcursor->selection())
692 mathcursor->selClear();
694 result = UNDISPATCHED;
697 case LFUN_INSET_TOGGLE:
698 mathcursor->insetToggle();
701 case LFUN_DIALOG_SHOW_NEW_INSET: {
702 string const & name = argument;
706 data = tmp.createDialogStr(name);
710 result = UNDISPATCHED;
712 bv->owner()->getDialogs().show(name, data, 0);
717 case LFUN_INSET_APPLY: {
718 string const name = cmd.getArg(0);
720 bv->owner()->getDialogs().getOpenInset(name);
723 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
724 result = base->localDispatch(fr);
727 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
728 mathcursor->insert(ar);
731 result = UNDISPATCHED;
738 result = UNDISPATCHED;
741 if (result == DISPATCHED)
742 bv->updateInset(this);
744 mathcursor->normalize();
747 lyx::Assert(mathcursor);
749 if (mathcursor->selection() || was_selection)
750 toggleInsetSelection(bv);
752 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
753 result == UNDISPATCHED) {
756 cmd.view()->stuffClipboard(mathcursor->grabSelection());
758 releaseMathCursor(bv);
759 bv->unlockInset(this);
761 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
764 return result; // original version
768 void InsetFormulaBase::revealCodes(BufferView * bv) const
772 bv->owner()->message(mathcursor->info());
775 // write something to the minibuffer
776 // translate to latex
777 mathcursor->markInsert();
780 string str = os.str();
781 mathcursor->markErase();
782 string::size_type pos = 0;
784 for (string::iterator it = str.begin(); it != str.end(); ++it) {
787 else if (*it == '\0') {
789 pos = it - str.begin();
795 res = res.substr(pos - 30);
797 res = res.substr(0, 60);
798 bv->owner()->message(res);
803 Inset::Code InsetFormulaBase::lyxCode() const
805 return Inset::MATH_CODE;
809 int InsetFormulaBase::ylow() const
811 return yo_ - ascent(view(), font_);
815 int InsetFormulaBase::yhigh() const
817 return yo_ + descent(view(), font_);
821 int InsetFormulaBase::xlow() const
827 int InsetFormulaBase::xhigh() const
829 return xo_ + width(view(), font_);
833 /////////////////////////////////////////////////////////////////////
836 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
842 static InsetFormulaBase * lastformula = 0;
843 static MathIterator current = MathIterator(ibegin(par().nucleus()));
845 static string laststr;
847 if (lastformula != this || laststr != str) {
848 //lyxerr << "reset lastformula to " << this << "\n";
851 current = ibegin(par().nucleus());
853 mathed_parse_cell(ar, str);
857 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
859 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
860 if (it.cell().matchpart(ar, it.back().pos_)) {
861 bv->unlockInset(bv->theLockingInset());
862 if (!bv->lockInset(this)) {
863 lyxerr << "Cannot lock inset" << endl;
867 mathcursor = new MathCursor(this, true);
869 mathcursor->setSelection(it, ar.size());
872 bv->updateInset(this);
877 //lyxerr << "not found!\n";
883 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
886 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
887 return searchForward(bv, what, a, b);
891 bool InsetFormulaBase::display() const
893 return par()->asHullInset() && par()->asHullInset()->display();
897 string InsetFormulaBase::selectionAsString() const
899 return mathcursor ? mathcursor->grabSelection() : string();
902 /////////////////////////////////////////////////////////////////////
905 void mathDispatchCreation(FuncRequest const & cmd, bool display)
907 BufferView * bv = cmd.view();
908 // use selection if available..
910 //if (action == LFUN_MATH_IMPORT_SELECTION)
914 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
917 InsetFormula * f = new InsetFormula(bv);
918 if (openNewInset(bv, f)) {
919 bv->theLockingInset()->
920 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
921 // don't do that also for LFUN_MATH_MODE unless you want end up with
922 // always changing to mathrm when opening an inlined inset
923 // -- I really hate "LyXfunc overloading"...
925 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
926 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
929 // create a macro if we see "\\newcommand" somewhere, and an ordinary
931 InsetFormulaBase * f;
932 if (sel.find("\\newcommand") == string::npos &&
933 sel.find("\\def") == string::npos)
934 f = new InsetFormula(sel);
936 f = new InsetFormulaMacro(sel);
937 bv->getLyXText()->cutSelection(bv);
940 cmd.message(N_("Math editor mode"));
944 void mathDispatch(FuncRequest const & cmd)
946 BufferView * bv = cmd.view();
947 if (!bv->available())
950 switch (cmd.action) {
952 case LFUN_MATH_DISPLAY:
953 mathDispatchCreation(cmd, true);
957 mathDispatchCreation(cmd, false);
960 case LFUN_MATH_IMPORT_SELECTION:
961 mathDispatchCreation(cmd, false);
964 case LFUN_MATH_MACRO:
965 if (cmd.argument.empty())
966 cmd.errorMessage(N_("Missing argument"));
968 string s = cmd.argument;
969 string const s1 = token(s, ' ', 1);
970 int const na = s1.empty() ? 0 : lyx::atoi(s1);
971 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
975 case LFUN_INSERT_MATH:
976 case LFUN_INSERT_MATRIX:
977 case LFUN_MATH_DELIM: {
978 InsetFormula * f = new InsetFormula(bv);
979 if (openNewInset(bv, f)) {
980 bv->theLockingInset()->
981 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
982 bv->theLockingInset()->localDispatch(cmd);