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"
56 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";
104 // simply scrap this function if you want
105 void InsetFormulaBase::mutateToText()
108 // translate to latex
110 latex(NULL, os, false, false);
111 string str = os.str();
114 LyXText * lt = view_->getLyXText();
115 string::const_iterator cit = str.begin();
116 string::const_iterator end = str.end();
117 for (; cit != end; ++cit)
118 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
121 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
126 // Check if uses AMS macros
127 void InsetFormulaBase::validate(LaTeXFeatures &) const
131 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
138 void InsetFormulaBase::metrics(BufferView * bv) const
142 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
147 string const InsetFormulaBase::editMessage() const
149 return _("Math editor mode");
153 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
155 if (!bv->lockInset(this))
156 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
158 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
160 mathcursor = new MathCursor(this, x == 0);
162 // handle ignored click
163 if (hack_x || hack_y) {
164 insetButtonPress(bv, hack_x, hack_y, hack_button);
169 // if that is removed, we won't get the magenta box when entering an
170 // inset for the first time
171 bv->updateInset(this, false);
175 void InsetFormulaBase::edit(BufferView * bv, bool front)
177 // looks hackish but seems to work
178 edit(bv, front ? 0 : 1, 0, 0);
182 void InsetFormulaBase::insetUnlock(BufferView * bv)
185 if (mathcursor->inMacroMode()) {
186 mathcursor->macroModeClose();
187 updateLocal(bv, true);
192 bv->updateInset(this, false);
196 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
198 mathcursor->getPos(x, y);
201 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
205 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
210 if (isCursorVisible())
211 bv->hideLockedInsetCursor();
216 mathcursor->getPos(x, y);
220 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
221 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
222 bv->showLockedInsetCursor(x, y, asc, des);
223 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
226 toggleCursorVisible();
230 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
232 if (isCursorVisible())
238 mathcursor->getPos(x, y);
242 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
243 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
244 bv->fitLockedInsetCursor(x, y, asc, des);
245 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
247 toggleInsetCursor(bv);
251 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
253 if (isCursorVisible())
254 toggleInsetCursor(bv);
258 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
261 bv->updateInset(this, false);
265 vector<string> const InsetFormulaBase::getLabelList() const
267 return std::vector<string>();
271 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
274 bv->updateInset(this, dirty);
278 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
279 int /*x*/, int /*y*/, int /*button*/)
283 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
286 bv->updateInset(this, false);
291 void InsetFormulaBase::insetButtonPress(BufferView * bv,
292 int x, int y, int button)
294 // hack to cope with mouseclick that comes before the call to edit()
298 hack_button = button;
302 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
303 << " but: " << button << "\n";
311 mathcursor->selClear();
312 mathcursor->setPos(x + xo_, y + yo_);
317 lyxerr << "insetButtonPress: 2\n";
320 bv->lockedInsetStoreUndo(Undo::EDIT);
322 mathcursor->selGet(ar);
323 mathcursor->setPos(x + xo_, y + yo_);
325 bv->getLyXText()->selectionAsString(bv->buffer(), false);
326 mathed_parse_cell(ar, sel);
327 mathcursor->insert(ar);
332 bv->updateInset(this, false);
336 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
337 int x, int y, int /*button*/)
342 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
343 //lyxerr << "insetMotionNotify: ignored\n";
349 if (!mathcursor->selection())
350 mathcursor->selStart();
352 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
353 // << ' ' << button << "\n";
355 mathcursor->setPos(x + xo_, y + yo_);
357 bv->updateInset(this, false);
361 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
363 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
367 UpdatableInset::RESULT
368 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
371 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
372 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
377 if (mathcursor->asHyperActiveInset()) {
378 lyxerr << " uurr.... getting dificult now\n";
379 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
382 RESULT result = DISPATCHED;
384 bool was_macro = mathcursor->inMacroMode();
385 bool was_selection = mathcursor->selection();
389 mathcursor->normalize();
392 // --- Cursor Movements ---------------------------------------------
395 sel = true; // fall through...
398 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
399 updateLocal(bv, false);
400 // write something to the minibuffer
401 //bv->owner()->message(mathcursor->info());
406 sel = true; // fall through
409 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
410 updateLocal(bv, false);
418 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
419 updateLocal(bv, false);
427 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
428 updateLocal(bv, false);
435 mathcursor->home(sel);
436 updateLocal(bv, false);
443 mathcursor->end(sel);
444 updateLocal(bv, false);
447 case LFUN_DELETE_LINE_FORWARD:
448 bv->lockedInsetStoreUndo(Undo::DELETE);
449 mathcursor->delLine();
450 updateLocal(bv, true);
454 mathcursor->idxNext();
455 updateLocal(bv, false);
459 mathcursor->idxPrev();
460 updateLocal(bv, false);
464 bv->lockedInsetStoreUndo(Undo::EDIT);
465 mathcursor->splitCell();
466 updateLocal(bv, true);
469 case LFUN_DELETE_WORD_BACKWARD:
471 bv->lockedInsetStoreUndo(Undo::DELETE);
472 mathcursor->backspace();
473 bv->updateInset(this, true);
476 case LFUN_DELETE_WORD_FORWARD:
478 bv->lockedInsetStoreUndo(Undo::DELETE);
480 bv->updateInset(this, true);
484 // sprintf(dispatch_buffer, "%d %d",);
485 // dispatch_result = dispatch_buffer;
488 lyxerr << "LFUN_SETXY broken!\n";
491 istringstream is(arg.c_str());
493 mathcursor->setPos(x, y);
494 updateLocal(bv, false);
500 mathcursor->macroModeClose();
501 bv->lockedInsetStoreUndo(Undo::EDIT);
502 mathcursor->selPaste();
503 updateLocal(bv, true);
507 bv->lockedInsetStoreUndo(Undo::DELETE);
508 mathcursor->selCut();
509 updateLocal(bv, true);
513 mathcursor->selCopy();
516 case LFUN_WORDRIGHTSEL:
517 case LFUN_WORDLEFTSEL:
520 // Special casing for superscript in case of LyX handling
522 case LFUN_CIRCUMFLEX:
524 // do superscript if LyX handles
526 bv->lockedInsetStoreUndo(Undo::EDIT);
527 mathcursor->script(true);
528 updateLocal(bv, true);
544 case LFUN_HUNG_UMLAUT:
548 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
549 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
550 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
551 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
552 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
553 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
554 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
555 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
556 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
557 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
558 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
561 handleFont(bv, arg, LM_TC_GREEK1);
563 mathcursor->interpret(arg);
567 //handleFont(bv, arg, LM_TC_TEXTRM);
569 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
570 //updateLocal(bv, true);
572 //bv->owner()->message(_("math text mode toggled"));
575 case LFUN_MATH_LIMITS:
576 bv->lockedInsetStoreUndo(Undo::EDIT);
577 if (mathcursor->toggleLimits())
578 updateLocal(bv, true);
584 bv->lockedInsetStoreUndo(Undo::EDIT);
585 mathcursor->setSize(arg);
586 updateLocal(bv, true);
591 case LFUN_INSERT_MATRIX:
593 bv->lockedInsetStoreUndo(Undo::EDIT);
594 mathcursor->interpret("matrix " + arg);
595 updateLocal(bv, true);
599 case LFUN_MATH_SPACE:
601 bv->lockedInsetStoreUndo(Undo::EDIT);
602 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
603 updateLocal(bv, true);
607 case LFUN_SUPERSCRIPT:
610 bv->lockedInsetStoreUndo(Undo::EDIT);
611 mathcursor->script((action == LFUN_SUPERSCRIPT));
612 updateLocal(bv, true);
616 case LFUN_MATH_DELIM:
618 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
621 istringstream is(arg.c_str());
624 lyxerr << "can't parse delimeters from '" << arg << "'\n";
627 bv->lockedInsetStoreUndo(Undo::EDIT);
628 mathcursor->handleDelim(ls, rs);
629 updateLocal(bv, true);
633 case LFUN_PROTECTEDSPACE:
634 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
635 bv->lockedInsetStoreUndo(Undo::EDIT);
636 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
637 updateLocal(bv, true);
641 bv->owner()->message(_("Invalid action in math mode!"));
645 case LFUN_MATH_HALIGN:
646 case LFUN_MATH_VALIGN:
647 case LFUN_MATH_ROW_INSERT:
648 case LFUN_MATH_ROW_DELETE:
649 case LFUN_MATH_COLUMN_INSERT:
650 case LFUN_MATH_COLUMN_DELETE:
652 MathInset::idx_type idx = 0;
653 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
655 bv->lockedInsetStoreUndo(Undo::EDIT);
656 char al = arg.size() ? arg[0] : 'c';
658 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
659 case LFUN_MATH_VALIGN: p->valign(al); break;
660 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
661 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
662 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
663 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
666 updateLocal(bv, true);
671 case LFUN_EXEC_COMMAND:
672 result = UNDISPATCHED;
675 case LFUN_BREAKPARAGRAPH:
676 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
677 //lyxerr << "LFUN ignored\n";
681 // interpret this as if a backslash was typed
682 bv->lockedInsetStoreUndo(Undo::EDIT);
683 mathcursor->interpret("\\");
684 updateLocal(bv, true);
688 case LFUN_INSERT_MATH:
689 case LFUN_SELFINSERT:
691 bv->lockedInsetStoreUndo(Undo::EDIT);
692 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
693 updateLocal(bv, true);
697 case LFUN_MATH_PANEL:
698 result = UNDISPATCHED;
702 if (mathcursor->selection())
703 mathcursor->selClear();
705 result = UNDISPATCHED;
709 result = UNDISPATCHED;
712 mathcursor->normalize();
714 lyx::Assert(mathcursor);
716 if (mathcursor->selection() || was_selection)
717 toggleInsetSelection(bv);
719 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
720 result == UNDISPATCHED)
723 bv->unlockInset(this);
725 return result; // original version
729 Inset::Code InsetFormulaBase::lyxCode() const
731 return Inset::MATH_CODE;
735 int InsetFormulaBase::ylow() const
737 return yo_ - ascent(view_, font_);
741 int InsetFormulaBase::yhigh() const
743 return yo_ + descent(view_, font_);
747 int InsetFormulaBase::xlow() const
753 int InsetFormulaBase::xhigh() const
755 return xo_ + width(view_, font_);
759 /////////////////////////////////////////////////////////////////////
762 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
768 static InsetFormulaBase * lastformula = 0;
769 static MathIterator current = MathIterator(ibegin(par().nucleus()));
771 static string laststr;
773 if (lastformula != this || laststr != str) {
774 //lyxerr << "reset lastformula to " << this << "\n";
777 current = ibegin(par().nucleus());
779 mathed_parse_cell(ar, str);
783 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
785 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
786 if (it.cell().matchpart(ar, it.position().pos_)) {
787 mathcursor->setSelection(it.cursor(), ar.size());
790 // I guess some of the following can go
791 bv->toggleSelection(true);
793 updateLocal(bv, true);
800 //lyxerr << "not found!\n";
802 // we have to unlock ourself in this function by default!
803 // don't ask me why...
804 bv->unlockInset(this);
809 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
812 lyxerr << "searching backward not implemented in mathed" << endl;
813 return searchForward(bv, what, a, b);
817 /////////////////////////////////////////////////////////////////////
820 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
822 if (bv->available()) {
823 // use selection if available..
825 //if (action == LFUN_MATH_IMPORT_SELECTION)
829 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
831 InsetFormulaBase * f;
833 f = new InsetFormula;
834 if (openNewInset(bv, f)) {
835 // don't do that also for LFUN_MATH_MODE unless you want end up with
836 // always changing to mathrm when opening an inlined inset
837 // -- I really hate "LyXfunc overloading"...
839 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
840 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
843 // create a macro if we see "\\newcommand" somewhere, and an ordinary
845 if (sel.find("\\newcommand") == string::npos)
846 f = new InsetFormula(sel);
849 if (!mathed_parse_macro(name, sel))
851 f = new InsetFormulaMacro(sel);
853 bv->getLyXText()->cutSelection(bv);
857 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
861 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
863 mathDispatchCreation(bv, arg, true);
867 void mathDispatchMathMode(BufferView * bv, string const & arg)
869 mathDispatchCreation(bv, arg, false);
873 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
875 mathDispatchCreation(bv, arg, true);
879 void mathDispatchMathMacro(BufferView * bv, string const & arg)
881 if (bv->available()) {
883 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
886 string const s1 = token(s, ' ', 1);
887 int const na = s1.empty() ? 0 : lyx::atoi(s1);
888 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
894 void mathDispatchMathDelim(BufferView * bv, string const & arg)
896 if (bv->available()) {
897 if (openNewInset(bv, new InsetFormula))
898 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
903 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
905 if (bv->available()) {
906 if (openNewInset(bv, new InsetFormula))
907 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
912 void mathDispatchInsertMath(BufferView * bv, string const & arg)
914 if (bv->available()) {
915 if (arg.size() && arg[0] == '\\') {
916 InsetFormula * f = new InsetFormula(arg);
917 if (!bv->insertInset(f))
920 mathDispatchMathMode(bv, arg);
925 void mathDispatchGreek(BufferView * bv, string const & arg)
927 if (bv->available()) {
928 InsetFormula * f = new InsetFormula;
929 if (openNewInset(bv, f)) {
930 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
937 void mathDispatch(BufferView *, kb_action, string const &)