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"
27 #include "commandtags.h"
28 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
40 #include "math_arrayinset.h"
41 #include "math_charinset.h"
42 #include "math_cursor.h"
43 #include "math_factory.h"
44 #include "math_hullinset.h"
45 #include "math_iterator.h"
46 #include "math_macrotable.h"
47 #include "math_parser.h"
49 #include "math_spaceinset.h"
50 #include "undo_funcs.h"
51 #include "textpainter.h"
52 #include "frontends/Dialogs.h"
59 MathCursor * mathcursor = 0;
70 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
72 if (mathcursor->selection())
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->handleFont(t);
75 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
76 mathcursor->insert(*it);
80 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
82 if (!bv->insertInset(new_inset)) {
86 new_inset->edit(bv, 0, 0, 0);
95 InsetFormulaBase::InsetFormulaBase()
96 : view_(0), font_(), xo_(0), yo_(0)
98 // This is needed as long the math parser is not re-entrant
99 MathMacroTable::builtinMacros();
100 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
101 //lyxerr << "sizeof(MathMetricsInfo): " << sizeof(MathMetricsInfo) << "\n";
102 //lyxerr << "sizeof(MathCharInset): " << sizeof(MathCharInset) << "\n";
106 // simply scrap this function if you want
107 void InsetFormulaBase::mutateToText()
110 // translate to latex
112 latex(NULL, os, false, false);
113 string str = os.str();
116 LyXText * lt = view_->getLyXText();
117 string::const_iterator cit = str.begin();
118 string::const_iterator end = str.end();
119 for (; cit != end; ++cit)
120 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
123 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
128 // Check if uses AMS macros
129 void InsetFormulaBase::validate(LaTeXFeatures &) const
133 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
140 void InsetFormulaBase::metrics(BufferView * bv) const
144 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
149 string const InsetFormulaBase::editMessage() const
151 return _("Math editor mode");
155 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
157 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
158 if (!bv->lockInset(this))
159 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
162 mathcursor = new MathCursor(this, x == 0);
164 // if that is removed, we won't get the magenta box when entering an
165 // inset for the first time
166 bv->updateInset(this, false);
170 void InsetFormulaBase::edit(BufferView * bv, bool front)
172 // looks hackish but seems to work
173 edit(bv, front ? 0 : 1, 0, 0);
177 void InsetFormulaBase::insetUnlock(BufferView * bv)
180 if (mathcursor->inMacroMode()) {
181 mathcursor->macroModeClose();
182 updateLocal(bv, true);
187 bv->updateInset(this, false);
191 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
193 mathcursor->getPos(x, y);
196 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
200 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
205 if (isCursorVisible())
206 bv->hideLockedInsetCursor();
211 mathcursor->getPos(x, y);
215 math_font_max_dim(font_, asc, des);
216 bv->showLockedInsetCursor(x, y, asc, des);
217 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
220 toggleCursorVisible();
224 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
226 if (isCursorVisible())
232 mathcursor->getPos(x, y);
236 math_font_max_dim(font_, asc, des);
237 bv->fitLockedInsetCursor(x, y, asc, des);
238 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
240 toggleInsetCursor(bv);
244 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
246 if (isCursorVisible())
247 toggleInsetCursor(bv);
251 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
254 bv->updateInset(this, false);
258 vector<string> const InsetFormulaBase::getLabelList() const
260 return vector<string>();
264 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
267 bv->updateInset(this, dirty);
271 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
272 int /*x*/, int /*y*/, int button)
274 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
280 bv->updateInset(this, false);
283 // launch math panel for right mouse button
284 bv->owner()->getDialogs()->showMathPanel();
291 void InsetFormulaBase::insetButtonPress(BufferView * bv,
292 int x, int y, int button)
294 //lyxerr << "insetButtonPress: "
295 // << x << " " << y << " but: " << button << "\n";
302 mathcursor = new MathCursor(this, x == 0);
307 mathcursor->selClear();
308 mathcursor->setPos(x + xo_, y + yo_);
312 lyxerr << "insetButtonPress: 2\n";
315 bv->lockedInsetStoreUndo(Undo::EDIT);
317 mathcursor->selGet(ar);
318 mathcursor->setPos(x + xo_, y + yo_);
320 bv->getLyXText()->selectionAsString(bv->buffer(), false);
321 mathed_parse_cell(ar, sel);
322 mathcursor->insert(ar);
327 // launch math panel for right mouse button
328 bv->owner()->getDialogs()->showMathPanel();
331 bv->updateInset(this, false);
335 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
336 int x, int y, int /*button*/)
341 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
342 //lyxerr << "insetMotionNotify: ignored\n";
348 if (!mathcursor->selection())
349 mathcursor->selStart();
351 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
352 // << ' ' << button << "\n";
354 mathcursor->setPos(x + xo_, y + yo_);
356 bv->updateInset(this, false);
360 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
362 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
366 UpdatableInset::RESULT
367 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
370 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
371 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
376 if (mathcursor->asHyperActiveInset()) {
377 lyxerr << " uurr.... getting dificult now\n";
378 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
381 RESULT result = DISPATCHED;
383 bool was_macro = mathcursor->inMacroMode();
384 bool was_selection = mathcursor->selection();
388 mathcursor->normalize();
393 // --- Cursor Movements ---------------------------------------------
396 sel = true; // fall through...
399 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
400 //lyxerr << "calling scroll 20\n";
402 updateLocal(bv, false);
403 // write something to the minibuffer
404 //bv->owner()->message(mathcursor->info());
409 sel = true; // fall through
412 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
413 updateLocal(bv, false);
421 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
422 updateLocal(bv, false);
430 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
431 updateLocal(bv, false);
438 mathcursor->home(sel);
439 updateLocal(bv, false);
446 mathcursor->end(sel);
447 updateLocal(bv, false);
450 case LFUN_DELETE_LINE_FORWARD:
451 bv->lockedInsetStoreUndo(Undo::DELETE);
452 mathcursor->delLine();
453 updateLocal(bv, true);
457 mathcursor->idxNext();
458 updateLocal(bv, false);
462 mathcursor->idxPrev();
463 updateLocal(bv, false);
467 bv->lockedInsetStoreUndo(Undo::EDIT);
468 mathcursor->splitCell();
469 updateLocal(bv, true);
472 case LFUN_DELETE_WORD_BACKWARD:
474 bv->lockedInsetStoreUndo(Undo::DELETE);
475 mathcursor->backspace();
476 bv->updateInset(this, true);
479 case LFUN_DELETE_WORD_FORWARD:
481 bv->lockedInsetStoreUndo(Undo::DELETE);
483 bv->updateInset(this, true);
487 // sprintf(dispatch_buffer, "%d %d",);
488 // dispatch_result = dispatch_buffer;
491 lyxerr << "LFUN_SETXY broken!\n";
494 istringstream is(arg.c_str());
496 mathcursor->setPos(x, y);
497 updateLocal(bv, false);
503 mathcursor->macroModeClose();
504 bv->lockedInsetStoreUndo(Undo::EDIT);
505 mathcursor->selPaste();
506 updateLocal(bv, true);
510 bv->lockedInsetStoreUndo(Undo::DELETE);
511 mathcursor->selCut();
512 updateLocal(bv, true);
516 mathcursor->selCopy();
519 case LFUN_WORDRIGHTSEL:
520 case LFUN_WORDLEFTSEL:
523 // Special casing for superscript in case of LyX handling
525 case LFUN_CIRCUMFLEX:
527 // do superscript if LyX handles
529 bv->lockedInsetStoreUndo(Undo::EDIT);
530 mathcursor->script(true);
531 updateLocal(bv, true);
547 case LFUN_HUNG_UMLAUT:
551 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
552 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
553 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
554 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
555 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
556 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
557 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
558 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
559 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
560 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
561 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
564 handleFont(bv, arg, LM_TC_GREEK1);
566 mathcursor->interpret(arg);
571 handleFont(bv, arg, LM_TC_TEXTRM);
575 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
576 updateLocal(bv, true);
579 //bv->owner()->message(_("math text mode toggled"));
582 case LFUN_MATH_LIMITS:
583 bv->lockedInsetStoreUndo(Undo::EDIT);
584 if (mathcursor->toggleLimits())
585 updateLocal(bv, true);
591 bv->lockedInsetStoreUndo(Undo::EDIT);
592 mathcursor->setSize(arg);
593 updateLocal(bv, true);
598 case LFUN_INSERT_MATRIX:
600 bv->lockedInsetStoreUndo(Undo::EDIT);
601 mathcursor->interpret("matrix " + arg);
602 updateLocal(bv, true);
606 case LFUN_MATH_SPACE:
608 bv->lockedInsetStoreUndo(Undo::EDIT);
609 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
610 updateLocal(bv, true);
614 case LFUN_SUPERSCRIPT:
617 bv->lockedInsetStoreUndo(Undo::EDIT);
618 mathcursor->script((action == LFUN_SUPERSCRIPT));
619 updateLocal(bv, true);
623 case LFUN_MATH_DELIM:
625 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
628 istringstream is(arg.c_str());
631 lyxerr << "can't parse delimeters from '" << arg << "'\n";
634 bv->lockedInsetStoreUndo(Undo::EDIT);
635 mathcursor->handleDelim(ls, rs);
636 updateLocal(bv, true);
640 case LFUN_PROTECTEDSPACE:
641 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
642 bv->lockedInsetStoreUndo(Undo::EDIT);
643 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
644 updateLocal(bv, true);
648 bv->owner()->message(_("Invalid action in math mode!"));
652 case LFUN_MATH_HALIGN:
653 case LFUN_MATH_VALIGN:
654 case LFUN_MATH_ROW_INSERT:
655 case LFUN_MATH_ROW_DELETE:
656 case LFUN_MATH_COLUMN_INSERT:
657 case LFUN_MATH_COLUMN_DELETE:
659 MathInset::idx_type idx = 0;
660 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
662 mathcursor->popToEnclosingGrid();
663 bv->lockedInsetStoreUndo(Undo::EDIT);
664 char align = arg.size() ? arg[0] : 'c';
666 case LFUN_MATH_HALIGN: p->halign(align, p->col(idx)); break;
667 case LFUN_MATH_VALIGN: p->valign(align); break;
668 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
669 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
670 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
671 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
674 updateLocal(bv, true);
679 case LFUN_EXEC_COMMAND:
680 result = UNDISPATCHED;
683 case LFUN_BREAKPARAGRAPH:
684 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
685 //lyxerr << "LFUN ignored\n";
689 // interpret this as if a backslash was typed
690 bv->lockedInsetStoreUndo(Undo::EDIT);
691 mathcursor->interpret('\\');
692 updateLocal(bv, true);
696 case LFUN_INSERT_MATH:
697 case LFUN_SELFINSERT:
699 bv->lockedInsetStoreUndo(Undo::EDIT);
701 result = mathcursor->interpret(arg[0]) ? DISPATCHED : FINISHED_RIGHT;
703 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
704 updateLocal(bv, true);
708 case LFUN_MATH_PANEL:
709 result = UNDISPATCHED;
713 if (mathcursor->selection())
714 mathcursor->selClear();
716 result = UNDISPATCHED;
720 result = UNDISPATCHED;
723 mathcursor->normalize();
726 lyx::Assert(mathcursor);
728 if (mathcursor->selection() || was_selection)
729 toggleInsetSelection(bv);
731 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
732 result == UNDISPATCHED)
735 bv->unlockInset(this);
739 return result; // original version
743 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
748 // write something to the minibuffer
749 // translate to latex
750 mathcursor->markInsert();
753 string str = os.str();
754 mathcursor->markErase();
755 string::size_type pos = 0;
757 for (string::iterator it = str.begin(); it != str.end(); ++it) {
760 else if (*it == '\0') {
762 pos = it - str.begin();
768 res = res.substr(pos - 30);
770 res = res.substr(0, 60);
771 bv->owner()->message(res);
776 Inset::Code InsetFormulaBase::lyxCode() const
778 return Inset::MATH_CODE;
782 int InsetFormulaBase::ylow() const
784 return yo_ - ascent(view_, font_);
788 int InsetFormulaBase::yhigh() const
790 return yo_ + descent(view_, font_);
794 int InsetFormulaBase::xlow() const
800 int InsetFormulaBase::xhigh() const
802 return xo_ + width(view_, font_);
806 /////////////////////////////////////////////////////////////////////
809 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
815 static InsetFormulaBase * lastformula = 0;
816 static MathIterator current = MathIterator(ibegin(par().nucleus()));
818 static string laststr;
820 if (lastformula != this || laststr != str) {
821 //lyxerr << "reset lastformula to " << this << "\n";
824 current = ibegin(par().nucleus());
826 mathed_parse_cell(ar, str);
830 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
832 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
833 if (it.cell().matchpart(ar, it.position().pos_)) {
834 mathcursor->setSelection(it.cursor(), ar.size());
837 // I guess some of the following can go
838 bv->toggleSelection(true);
840 updateLocal(bv, true);
847 //lyxerr << "not found!\n";
849 // we have to unlock ourself in this function by default!
850 // don't ask me why...
851 bv->unlockInset(this);
856 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
859 lyxerr << "searching backward not implemented in mathed" << endl;
860 return searchForward(bv, what, a, b);
864 /////////////////////////////////////////////////////////////////////
867 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
869 if (bv->available()) {
870 // use selection if available..
872 //if (action == LFUN_MATH_IMPORT_SELECTION)
876 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
878 InsetFormulaBase * f;
880 f = new InsetFormula;
881 if (openNewInset(bv, f)) {
882 // don't do that also for LFUN_MATH_MODE unless you want end up with
883 // always changing to mathrm when opening an inlined inset
884 // -- I really hate "LyXfunc overloading"...
886 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
887 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
890 // create a macro if we see "\\newcommand" somewhere, and an ordinary
892 if (sel.find("\\newcommand") == string::npos &&
893 sel.find("\\def") == string::npos)
894 f = new InsetFormula(sel);
897 if (!mathed_parse_macro(name, sel))
899 f = new InsetFormulaMacro(sel);
901 bv->getLyXText()->cutSelection(bv);
905 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
909 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
911 mathDispatchCreation(bv, arg, true);
915 void mathDispatchMathMode(BufferView * bv, string const & arg)
917 mathDispatchCreation(bv, arg, false);
921 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
923 mathDispatchCreation(bv, arg, true);
927 void mathDispatchMathMacro(BufferView * bv, string const & arg)
929 if (bv->available()) {
931 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
934 string const s1 = token(s, ' ', 1);
935 int const na = s1.empty() ? 0 : lyx::atoi(s1);
936 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
942 void mathDispatchMathDelim(BufferView * bv, string const & arg)
944 if (bv->available()) {
945 if (openNewInset(bv, new InsetFormula))
946 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
951 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
953 if (bv->available()) {
954 if (openNewInset(bv, new InsetFormula))
955 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
960 void mathDispatchInsertMath(BufferView * bv, string const & arg)
962 if (bv->available()) {
963 if (arg.size() && arg[0] == '\\') {
964 InsetFormula * f = new InsetFormula(arg);
965 if (!bv->insertInset(f))
968 mathDispatchMathMode(bv, arg);
973 void mathDispatchGreek(BufferView * bv, string const & arg)
975 if (bv->available()) {
976 InsetFormula * f = new InsetFormula;
977 if (openNewInset(bv, f)) {
978 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
985 void mathDispatch(BufferView *, kb_action, string const &)