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_cursor.h"
42 #include "math_factory.h"
43 #include "math_hullinset.h"
44 #include "math_iterator.h"
45 #include "math_macrotable.h"
46 #include "math_parser.h"
48 #include "math_spaceinset.h"
49 #include "undo_funcs.h"
50 #include "frontends/Dialogs.h"
57 MathCursor * mathcursor = 0;
71 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
73 if (mathcursor->selection())
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->handleFont(t);
76 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
77 mathcursor->insert(*it);
81 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
83 if (!bv->insertInset(new_inset)) {
87 new_inset->edit(bv, 0, 0, 0);
96 InsetFormulaBase::InsetFormulaBase()
97 : view_(0), font_(), xo_(0), yo_(0)
99 // This is needed as long the math parser is not re-entrant
100 MathMacroTable::builtinMacros();
101 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
105 // simply scrap this function if you want
106 void InsetFormulaBase::mutateToText()
109 // translate to latex
111 latex(NULL, os, false, false);
112 string str = os.str();
115 LyXText * lt = view_->getLyXText();
116 string::const_iterator cit = str.begin();
117 string::const_iterator end = str.end();
118 for (; cit != end; ++cit)
119 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
122 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
127 // Check if uses AMS macros
128 void InsetFormulaBase::validate(LaTeXFeatures &) const
132 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
139 void InsetFormulaBase::metrics(BufferView * bv) const
143 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
156 if (!bv->lockInset(this))
157 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
159 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
161 mathcursor = new MathCursor(this, x == 0);
163 // handle ignored click
164 if (hack_x || hack_y) {
165 insetButtonPress(bv, hack_x, hack_y, hack_button);
170 // if that is removed, we won't get the magenta box when entering an
171 // inset for the first time
172 bv->updateInset(this, false);
176 void InsetFormulaBase::edit(BufferView * bv, bool front)
178 // looks hackish but seems to work
179 edit(bv, front ? 0 : 1, 0, 0);
183 void InsetFormulaBase::insetUnlock(BufferView * bv)
186 if (mathcursor->inMacroMode()) {
187 mathcursor->macroModeClose();
188 updateLocal(bv, true);
193 bv->updateInset(this, false);
197 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
199 mathcursor->getPos(x, y);
202 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
206 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
211 if (isCursorVisible())
212 bv->hideLockedInsetCursor();
217 mathcursor->getPos(x, y);
221 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
222 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
223 bv->showLockedInsetCursor(x, y, asc, des);
224 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
227 toggleCursorVisible();
231 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
233 if (isCursorVisible())
239 mathcursor->getPos(x, y);
243 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
244 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
245 bv->fitLockedInsetCursor(x, y, asc, des);
246 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
248 toggleInsetCursor(bv);
252 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
254 if (isCursorVisible())
255 toggleInsetCursor(bv);
259 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
262 bv->updateInset(this, false);
266 vector<string> const InsetFormulaBase::getLabelList() const
268 return std::vector<string>();
272 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
275 bv->updateInset(this, dirty);
279 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
280 int /*x*/, int /*y*/, int /*button*/)
284 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
287 bv->updateInset(this, false);
292 void InsetFormulaBase::insetButtonPress(BufferView * bv,
293 int x, int y, int button)
295 // hack to cope with mouseclick that comes before the call to edit()
299 hack_button = button;
303 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
304 << " but: " << button << "\n";
312 mathcursor->selClear();
313 mathcursor->setPos(x + xo_, y + yo_);
318 lyxerr << "insetButtonPress: 2\n";
321 bv->lockedInsetStoreUndo(Undo::EDIT);
323 mathcursor->selGet(ar);
324 mathcursor->setPos(x + xo_, y + yo_);
326 bv->getLyXText()->selectionAsString(bv->buffer(), false);
327 mathed_parse_cell(ar, sel);
328 mathcursor->insert(ar);
333 // launch math panel for right mouse button
334 bv->owner()->getDialogs()->showMathPanel();
337 bv->updateInset(this, false);
341 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
342 int x, int y, int /*button*/)
347 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
348 //lyxerr << "insetMotionNotify: ignored\n";
354 if (!mathcursor->selection())
355 mathcursor->selStart();
357 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
358 // << ' ' << button << "\n";
360 mathcursor->setPos(x + xo_, y + yo_);
362 bv->updateInset(this, false);
366 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
368 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
372 UpdatableInset::RESULT
373 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
376 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
377 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
382 if (mathcursor->asHyperActiveInset()) {
383 lyxerr << " uurr.... getting dificult now\n";
384 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
387 RESULT result = DISPATCHED;
389 bool was_macro = mathcursor->inMacroMode();
390 bool was_selection = mathcursor->selection();
394 mathcursor->normalize();
397 // --- Cursor Movements ---------------------------------------------
400 sel = true; // fall through...
403 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
404 //lyxerr << "calling scroll 20\n";
406 updateLocal(bv, false);
407 // write something to the minibuffer
408 //bv->owner()->message(mathcursor->info());
413 sel = true; // fall through
416 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
417 updateLocal(bv, false);
425 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
426 updateLocal(bv, false);
434 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
435 updateLocal(bv, false);
442 mathcursor->home(sel);
443 updateLocal(bv, false);
450 mathcursor->end(sel);
451 updateLocal(bv, false);
454 case LFUN_DELETE_LINE_FORWARD:
455 bv->lockedInsetStoreUndo(Undo::DELETE);
456 mathcursor->delLine();
457 updateLocal(bv, true);
461 mathcursor->idxNext();
462 updateLocal(bv, false);
466 mathcursor->idxPrev();
467 updateLocal(bv, false);
471 bv->lockedInsetStoreUndo(Undo::EDIT);
472 mathcursor->splitCell();
473 updateLocal(bv, true);
476 case LFUN_DELETE_WORD_BACKWARD:
478 bv->lockedInsetStoreUndo(Undo::DELETE);
479 mathcursor->backspace();
480 bv->updateInset(this, true);
483 case LFUN_DELETE_WORD_FORWARD:
485 bv->lockedInsetStoreUndo(Undo::DELETE);
487 bv->updateInset(this, true);
491 // sprintf(dispatch_buffer, "%d %d",);
492 // dispatch_result = dispatch_buffer;
495 lyxerr << "LFUN_SETXY broken!\n";
498 istringstream is(arg.c_str());
500 mathcursor->setPos(x, y);
501 updateLocal(bv, false);
507 mathcursor->macroModeClose();
508 bv->lockedInsetStoreUndo(Undo::EDIT);
509 mathcursor->selPaste();
510 updateLocal(bv, true);
514 bv->lockedInsetStoreUndo(Undo::DELETE);
515 mathcursor->selCut();
516 updateLocal(bv, true);
520 mathcursor->selCopy();
523 case LFUN_WORDRIGHTSEL:
524 case LFUN_WORDLEFTSEL:
527 // Special casing for superscript in case of LyX handling
529 case LFUN_CIRCUMFLEX:
531 // do superscript if LyX handles
533 bv->lockedInsetStoreUndo(Undo::EDIT);
534 mathcursor->script(true);
535 updateLocal(bv, true);
551 case LFUN_HUNG_UMLAUT:
555 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
556 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
557 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
558 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
559 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
560 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
561 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
562 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
563 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
564 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
565 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
568 handleFont(bv, arg, LM_TC_GREEK1);
570 mathcursor->interpret(arg);
574 //handleFont(bv, arg, LM_TC_TEXTRM);
576 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
577 //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 bv->lockedInsetStoreUndo(Undo::EDIT);
663 char al = arg.size() ? arg[0] : 'c';
665 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
666 case LFUN_MATH_VALIGN: p->valign(al); break;
667 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
668 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
669 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
670 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
673 updateLocal(bv, true);
678 case LFUN_EXEC_COMMAND:
679 result = UNDISPATCHED;
682 case LFUN_BREAKPARAGRAPH:
683 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
684 //lyxerr << "LFUN ignored\n";
688 // interpret this as if a backslash was typed
689 bv->lockedInsetStoreUndo(Undo::EDIT);
690 mathcursor->interpret("\\");
691 updateLocal(bv, true);
695 case LFUN_INSERT_MATH:
696 case LFUN_SELFINSERT:
698 bv->lockedInsetStoreUndo(Undo::EDIT);
699 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
700 updateLocal(bv, true);
704 case LFUN_MATH_PANEL:
705 result = UNDISPATCHED;
709 if (mathcursor->selection())
710 mathcursor->selClear();
712 result = UNDISPATCHED;
716 result = UNDISPATCHED;
719 mathcursor->normalize();
721 lyx::Assert(mathcursor);
723 if (mathcursor->selection() || was_selection)
724 toggleInsetSelection(bv);
726 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
727 result == UNDISPATCHED)
730 bv->unlockInset(this);
732 return result; // original version
736 Inset::Code InsetFormulaBase::lyxCode() const
738 return Inset::MATH_CODE;
742 int InsetFormulaBase::ylow() const
744 return yo_ - ascent(view_, font_);
748 int InsetFormulaBase::yhigh() const
750 return yo_ + descent(view_, font_);
754 int InsetFormulaBase::xlow() const
760 int InsetFormulaBase::xhigh() const
762 return xo_ + width(view_, font_);
766 /////////////////////////////////////////////////////////////////////
769 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
775 static InsetFormulaBase * lastformula = 0;
776 static MathIterator current = MathIterator(ibegin(par().nucleus()));
778 static string laststr;
780 if (lastformula != this || laststr != str) {
781 //lyxerr << "reset lastformula to " << this << "\n";
784 current = ibegin(par().nucleus());
786 mathed_parse_cell(ar, str);
790 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
792 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
793 if (it.cell().matchpart(ar, it.position().pos_)) {
794 mathcursor->setSelection(it.cursor(), ar.size());
797 // I guess some of the following can go
798 bv->toggleSelection(true);
800 updateLocal(bv, true);
807 //lyxerr << "not found!\n";
809 // we have to unlock ourself in this function by default!
810 // don't ask me why...
811 bv->unlockInset(this);
816 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
819 lyxerr << "searching backward not implemented in mathed" << endl;
820 return searchForward(bv, what, a, b);
824 /////////////////////////////////////////////////////////////////////
827 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
829 if (bv->available()) {
830 // use selection if available..
832 //if (action == LFUN_MATH_IMPORT_SELECTION)
836 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
838 InsetFormulaBase * f;
840 f = new InsetFormula;
841 if (openNewInset(bv, f)) {
842 // don't do that also for LFUN_MATH_MODE unless you want end up with
843 // always changing to mathrm when opening an inlined inset
844 // -- I really hate "LyXfunc overloading"...
846 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
847 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
850 // create a macro if we see "\\newcommand" somewhere, and an ordinary
852 if (sel.find("\\newcommand") == string::npos)
853 f = new InsetFormula(sel);
856 if (!mathed_parse_macro(name, sel))
858 f = new InsetFormulaMacro(sel);
860 bv->getLyXText()->cutSelection(bv);
864 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
868 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
870 mathDispatchCreation(bv, arg, true);
874 void mathDispatchMathMode(BufferView * bv, string const & arg)
876 mathDispatchCreation(bv, arg, false);
880 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
882 mathDispatchCreation(bv, arg, true);
886 void mathDispatchMathMacro(BufferView * bv, string const & arg)
888 if (bv->available()) {
890 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
893 string const s1 = token(s, ' ', 1);
894 int const na = s1.empty() ? 0 : lyx::atoi(s1);
895 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
901 void mathDispatchMathDelim(BufferView * bv, string const & arg)
903 if (bv->available()) {
904 if (openNewInset(bv, new InsetFormula))
905 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
910 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
912 if (bv->available()) {
913 if (openNewInset(bv, new InsetFormula))
914 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
919 void mathDispatchInsertMath(BufferView * bv, string const & arg)
921 if (bv->available()) {
922 if (arg.size() && arg[0] == '\\') {
923 InsetFormula * f = new InsetFormula(arg);
924 if (!bv->insertInset(f))
927 mathDispatchMathMode(bv, arg);
932 void mathDispatchGreek(BufferView * bv, string const & arg)
934 if (bv->available()) {
935 InsetFormula * f = new InsetFormula;
936 if (openNewInset(bv, f)) {
937 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
944 void mathDispatch(BufferView *, kb_action, string const &)