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 "math_data.h"
31 #include "support/lstrings.h"
32 #include "support/lyxlib.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/font_metrics.h"
35 #include "frontends/mouse_state.h"
36 #include "math_arrayinset.h"
37 #include "math_charinset.h"
38 #include "math_deliminset.h"
39 #include "math_cursor.h"
40 #include "math_factory.h"
41 #include "math_fontinset.h"
42 #include "math_hullinset.h"
43 #include "math_iterator.h"
44 #include "math_macrotable.h"
45 #include "math_parser.h"
47 #include "math_spaceinset.h"
48 #include "undo_funcs.h"
49 #include "textpainter.h"
50 #include "frontends/Dialogs.h"
52 #include "ref_inset.h"
56 using namespace lyx::support;
65 MathCursor * mathcursor = 0;
73 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
75 if (!bv->insertInset(new_inset)) {
79 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
88 InsetFormulaBase::InsetFormulaBase()
91 // This is needed as long the math parser is not re-entrant
93 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
94 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << "\n";
95 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
96 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
100 // simply scrap this function if you want
101 void InsetFormulaBase::mutateToText()
104 // translate to latex
106 latex(NULL, os, false, false);
107 string str = os.str();
110 LyXText * lt = view_->getLyXText();
111 string::const_iterator cit = str.begin();
112 string::const_iterator end = str.end();
113 for (; cit != end; ++cit)
114 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
117 //view_->owner()->dispatch(LFUN_ESCAPE);
122 void InsetFormulaBase::handleFont
123 (BufferView * bv, string const & arg, string const & font)
125 // this whole function is a hack and won't work for incremental font
127 recordUndo(bv, Undo::ATOMIC);
129 if (mathcursor->inset()->name() == font)
130 mathcursor->handleFont(font);
132 mathcursor->handleNest(createMathInset(font));
133 mathcursor->insert(arg);
138 BufferView * InsetFormulaBase::view() const
144 void InsetFormulaBase::validate(LaTeXFeatures &) const
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::insetUnlock(BufferView * bv)
157 if (mathcursor->inMacroMode()) {
158 mathcursor->macroModeClose();
159 bv->updateInset(this);
161 releaseMathCursor(bv);
164 bv->updateInset(this);
168 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
170 mathcursor->getPos(x, y);
174 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 // calling metrics here destroys the cached xo,yo positions e.g. in
177 // MathParboxinset. And it would be too expensive anyway...
180 lyxerr << "getCursorPos - should not happen";
184 mathcursor->getPos(x, y);
185 x = mathcursor->targetX();
188 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
192 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
199 //math_font_max_dim(font_, asc, des);
200 getCursorPos(bv, x, y);
202 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
203 bv->fitLockedInsetCursor(x, y, asc, des);
207 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
210 bv->updateInset(this);
214 vector<string> const InsetFormulaBase::getLabelList() const
216 return vector<string>();
220 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
225 BufferView * bv = cmd.view();
226 bv->updateInset(this);
227 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
229 if (cmd.button() == mouse_button::button3) {
230 // try to dispatch to enclosed insets first
231 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
232 // launch math panel for right mouse button
233 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
234 bv->owner()->getDialogs().show("mathpanel");
239 if (cmd.button() == mouse_button::button2) {
241 asArray(bv->getClipboard(), ar);
242 mathcursor->selClear();
243 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
244 mathcursor->insert(ar);
245 bv->updateInset(this);
249 if (cmd.button() == mouse_button::button1) {
250 // try to dispatch to enclosed insets first
251 mathcursor->dispatch(cmd);
252 cmd.view()->stuffClipboard(mathcursor->grabSelection());
253 // try to set the cursor
255 //mathcursor = new MathCursor(this, x == 0);
257 //mathcursor->setPos(x + xo_, y + yo_);
265 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
267 BufferView * bv = cmd.view();
268 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
270 if (!mathcursor || mathcursor->formula() != this) {
271 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
272 releaseMathCursor(bv);
273 mathcursor = new MathCursor(this, cmd.x == 0);
275 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
278 if (cmd.button() == mouse_button::button3) {
279 mathcursor->dispatch(cmd);
283 if (cmd.button() == mouse_button::button1) {
286 mathcursor->selClear();
287 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
288 mathcursor->dispatch(cmd);
292 bv->updateInset(this);
297 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
302 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
305 // only select with button 1
306 if (cmd.button() != mouse_button::button1)
309 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
315 if (!mathcursor->selection())
316 mathcursor->selStart();
318 BufferView * bv = cmd.view();
319 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
320 bv->updateInset(this);
325 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
327 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
328 // << " arg: '" << cmd.argument
329 // << " x: '" << cmd.x
330 // << " y: '" << cmd.y
331 // << "' button: " << cmd.button() << endl;
333 BufferView * bv = cmd.view();
335 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
336 bool remove_inset = false;
338 switch (cmd.action) {
339 case LFUN_INSET_EDIT:
340 lyxerr << "Called EDIT with '" << cmd.argument << "'\n";
341 if (!bv->lockInset(this))
342 lyxerr << "Cannot lock math inset in edit call!\n";
343 releaseMathCursor(bv);
344 if (!cmd.argument.empty()) {
345 mathcursor = new MathCursor(this, cmd.argument == "left");
348 mathcursor = new MathCursor(this, true);
350 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
352 // if that is removed, we won't get the magenta box when entering an
353 // inset for the first time
354 bv->updateInset(this);
357 case LFUN_MOUSE_PRESS:
358 //lyxerr << "Mouse single press\n";
359 return lfunMousePress(cmd);
360 case LFUN_MOUSE_MOTION:
361 //lyxerr << "Mouse motion\n";
362 return lfunMouseMotion(cmd);
363 case LFUN_MOUSE_RELEASE:
364 //lyxerr << "Mouse single release\n";
365 return lfunMouseRelease(cmd);
366 case LFUN_MOUSE_DOUBLE:
367 //lyxerr << "Mouse double\n";
368 return localDispatch(FuncRequest(LFUN_WORDSEL));
376 string argument = cmd.argument;
377 RESULT result = DISPATCHED;
379 bool was_macro = mathcursor->inMacroMode();
380 bool was_selection = mathcursor->selection();
382 mathcursor->normalize();
385 switch (cmd.action) {
387 case LFUN_MATH_MUTATE:
388 case LFUN_MATH_DISPLAY:
389 case LFUN_MATH_NUMBER:
390 case LFUN_MATH_NONUMBER:
391 case LFUN_CELL_SPLIT:
393 case LFUN_DELETE_LINE_FORWARD:
394 case LFUN_INSERT_LABEL:
395 case LFUN_MATH_EXTERN:
396 case LFUN_TABULAR_FEATURE:
397 case LFUN_PASTESELECTION:
398 case LFUN_MATH_LIMITS:
399 recordUndo(bv, Undo::ATOMIC);
400 mathcursor->dispatch(cmd);
404 sel = true; // fall through...
406 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
407 //lyxerr << "calling scroll 20\n";
409 // write something to the minibuffer
410 //bv->owner()->message(mathcursor->info());
414 sel = true; // fall through
416 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
420 sel = true; // fall through
422 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
426 sel = true; // fall through
428 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
432 mathcursor->home(false);
433 mathcursor->end(true);
436 case LFUN_UP_PARAGRAPHSEL:
437 case LFUN_UP_PARAGRAPH:
438 case LFUN_DOWN_PARAGRAPHSEL:
439 case LFUN_DOWN_PARAGRAPH:
444 case LFUN_WORDLEFTSEL:
445 sel = true; // fall through
448 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
452 case LFUN_WORDRIGHTSEL:
453 sel = true; // fall through
456 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
461 case LFUN_BEGINNINGBUFSEL:
462 case LFUN_BEGINNINGBUF:
470 result = FINISHED_RIGHT;
473 case LFUN_CELL_FORWARD:
474 mathcursor->idxNext();
477 case LFUN_CELL_BACKWARD:
478 mathcursor->idxPrev();
481 case LFUN_DELETE_WORD_BACKWARD:
483 recordUndo(bv, Undo::ATOMIC);
484 if (!mathcursor->backspace()) {
490 case LFUN_DELETE_WORD_FORWARD:
492 recordUndo(bv, Undo::ATOMIC);
493 if (!mathcursor->erase()) {
500 // sprintf(dispatch_buffer, "%d %d",);
501 // dispatch_result = dispatch_buffer;
504 lyxerr << "LFUN_SETXY broken!\n";
507 istringstream is(cmd.argument.c_str());
509 mathcursor->setPos(x, y);
515 istringstream is(cmd.argument.c_str());
518 mathcursor->macroModeClose();
519 recordUndo(bv, Undo::ATOMIC);
520 mathcursor->selPaste(n);
525 recordUndo(bv, Undo::DELETE);
526 mathcursor->selCut();
530 mathcursor->selCopy();
534 // Special casing for superscript in case of LyX handling
536 case LFUN_CIRCUMFLEX:
537 if (cmd.argument.empty()) {
538 // do superscript if LyX handles
540 recordUndo(bv, Undo::ATOMIC);
541 mathcursor->script(true);
558 case LFUN_HUNG_UMLAUT:
562 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
563 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
564 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
565 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
566 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
567 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
568 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
569 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
570 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
571 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
574 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
575 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
577 handleFont(bv, cmd.argument, "textrm");
578 //bv->owner()->message(_("math text mode toggled"));
584 recordUndo(bv, Undo::ATOMIC);
585 mathcursor->setSize(arg);
590 case LFUN_INSERT_MATRIX: {
591 recordUndo(bv, Undo::ATOMIC);
596 istringstream is(STRCONV(argument));
597 is >> m >> n >> v_align >> h_align;
601 mathcursor->niceInsert(
602 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
606 case LFUN_SUPERSCRIPT:
609 recordUndo(bv, Undo::ATOMIC);
610 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
614 case LFUN_MATH_DELIM:
616 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
618 string rs = split(cmd.argument, ls, ' ');
619 // Reasonable default values
625 recordUndo(bv, Undo::ATOMIC);
626 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
630 case LFUN_SPACE_INSERT:
631 case LFUN_MATH_SPACE:
632 recordUndo(bv, Undo::ATOMIC);
633 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
637 bv->owner()->message(_("Invalid action in math mode!"));
641 case LFUN_EXEC_COMMAND:
642 result = UNDISPATCHED;
646 // interpret this as if a backslash was typed
647 recordUndo(bv, Undo::ATOMIC);
648 mathcursor->interpret('\\');
651 case LFUN_BREAKPARAGRAPH:
652 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
653 case LFUN_BREAKPARAGRAPH_SKIP:
657 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
658 // handling such that "self-insert" works on "arbitrary stuff" too, and
659 // math-insert only handles special math things like "matrix".
660 case LFUN_INSERT_MATH:
661 recordUndo(bv, Undo::ATOMIC);
662 mathcursor->niceInsert(argument);
666 case LFUN_SELFINSERT:
667 if (!argument.empty()) {
668 recordUndo(bv, Undo::ATOMIC);
669 if (argument.size() == 1)
670 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
672 mathcursor->insert(argument);
677 if (mathcursor->selection())
678 mathcursor->selClear();
680 result = UNDISPATCHED;
683 case LFUN_INSET_TOGGLE:
684 mathcursor->insetToggle();
687 case LFUN_DIALOG_SHOW:
688 result = UNDISPATCHED;
691 case LFUN_DIALOG_SHOW_NEW_INSET: {
692 string const & name = argument;
696 data = tmp.createDialogStr(name);
700 result = UNDISPATCHED;
702 bv->owner()->getDialogs().show(name, data, 0);
707 case LFUN_INSET_APPLY: {
708 string const name = cmd.getArg(0);
710 bv->owner()->getDialogs().getOpenInset(name);
713 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
714 result = base->localDispatch(fr);
717 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
718 mathcursor->insert(ar);
721 result = UNDISPATCHED;
728 result = UNDISPATCHED;
731 if (result == DISPATCHED)
732 bv->updateInset(this);
734 mathcursor->normalize();
739 if (mathcursor->selection() || was_selection)
740 toggleInsetSelection(bv);
742 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
743 result == UNDISPATCHED) {
746 cmd.view()->stuffClipboard(mathcursor->grabSelection());
748 releaseMathCursor(bv);
749 bv->unlockInset(this);
751 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
754 return result; // original version
758 void InsetFormulaBase::revealCodes(BufferView * bv) const
762 bv->owner()->message(mathcursor->info());
765 // write something to the minibuffer
766 // translate to latex
767 mathcursor->markInsert();
770 string str = os.str();
771 mathcursor->markErase();
772 string::size_type pos = 0;
774 for (string::iterator it = str.begin(); it != str.end(); ++it) {
777 else if (*it == '\0') {
779 pos = it - str.begin();
785 res = res.substr(pos - 30);
787 res = res.substr(0, 60);
788 bv->owner()->message(res);
793 Inset::Code InsetFormulaBase::lyxCode() const
795 return Inset::MATH_CODE;
799 int InsetFormulaBase::ylow() const
801 return yo_ - dim_.asc;
805 int InsetFormulaBase::yhigh() const
807 return yo_ + dim_.des;
811 int InsetFormulaBase::xlow() const
817 int InsetFormulaBase::xhigh() const
819 return xo_ + dim_.wid;
823 /////////////////////////////////////////////////////////////////////
826 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
832 static InsetFormulaBase * lastformula = 0;
833 static MathIterator current = MathIterator(ibegin(par().nucleus()));
835 static string laststr;
837 if (lastformula != this || laststr != str) {
838 //lyxerr << "reset lastformula to " << this << "\n";
841 current = ibegin(par().nucleus());
843 mathed_parse_cell(ar, str);
847 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
849 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
850 if (it.cell().matchpart(ar, it.back().pos_)) {
851 bv->unlockInset(bv->theLockingInset());
852 if (!bv->lockInset(this)) {
853 lyxerr << "Cannot lock inset" << endl;
857 mathcursor = new MathCursor(this, true);
859 mathcursor->setSelection(it, ar.size());
862 bv->updateInset(this);
867 //lyxerr << "not found!\n";
873 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
876 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
877 return searchForward(bv, what, a, b);
881 bool InsetFormulaBase::display() const
883 return par()->asHullInset() && par()->asHullInset()->display();
887 string InsetFormulaBase::selectionAsString() const
889 return mathcursor ? mathcursor->grabSelection() : string();
892 /////////////////////////////////////////////////////////////////////
895 void mathDispatchCreation(FuncRequest const & cmd, bool display)
897 BufferView * bv = cmd.view();
898 // use selection if available..
900 //if (action == LFUN_MATH_IMPORT_SELECTION)
904 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
907 InsetFormula * f = new InsetFormula(bv);
908 if (openNewInset(bv, f)) {
909 bv->theLockingInset()->
910 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
911 // don't do that also for LFUN_MATH_MODE unless you want end up with
912 // always changing to mathrm when opening an inlined inset
913 // -- I really hate "LyXfunc overloading"...
915 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
916 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
919 // create a macro if we see "\\newcommand" somewhere, and an ordinary
921 InsetFormulaBase * f;
922 if (sel.find("\\newcommand") == string::npos &&
923 sel.find("\\def") == string::npos)
924 f = new InsetFormula(sel);
926 f = new InsetFormulaMacro(sel);
927 bv->getLyXText()->cutSelection(true, false);
930 cmd.message(N_("Math editor mode"));
934 void mathDispatch(FuncRequest const & cmd)
936 BufferView * bv = cmd.view();
937 if (!bv->available())
940 switch (cmd.action) {
942 case LFUN_MATH_DISPLAY:
943 mathDispatchCreation(cmd, true);
947 mathDispatchCreation(cmd, false);
950 case LFUN_MATH_IMPORT_SELECTION:
951 mathDispatchCreation(cmd, false);
954 case LFUN_MATH_MACRO:
955 if (cmd.argument.empty())
956 cmd.errorMessage(N_("Missing argument"));
958 string s = cmd.argument;
959 string const s1 = token(s, ' ', 1);
960 int const nargs = s1.empty() ? 0 : atoi(s1);
961 string const s2 = token(s, ' ', 2);
962 string const type = s2.empty() ? "newcommand" : s2;
963 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
967 case LFUN_INSERT_MATH:
968 case LFUN_INSERT_MATRIX:
969 case LFUN_MATH_DELIM: {
970 InsetFormula * f = new InsetFormula(bv);
971 if (openNewInset(bv, f)) {
972 bv->theLockingInset()->
973 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
974 bv->theLockingInset()->localDispatch(cmd);