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"
39 #include "math_arrayinset.h"
40 #include "math_cursor.h"
41 #include "math_factory.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"
54 MathCursor * mathcursor = 0;
68 void handleFont(BufferView * bv, MathTextCodes t)
70 if (mathcursor->selection())
71 bv->lockedInsetStoreUndo(Undo::EDIT);
72 mathcursor->handleFont(t);
76 void handleAccent(BufferView * bv, string const & name)
78 bv->lockedInsetStoreUndo(Undo::EDIT);
79 mathcursor->insert(createMathInset(name));
83 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
85 if (!bv->insertInset(new_inset)) {
89 new_inset->edit(bv, 0, 0, 0);
98 InsetFormulaBase::InsetFormulaBase()
99 : view_(0), font_(), xo_(0), yo_(0)
101 // This is needed as long the math parser is not re-entrant
102 MathMacroTable::builtinMacros();
103 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
107 // Check if uses AMS macros
108 void InsetFormulaBase::validate(LaTeXFeatures &) const
112 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
119 void InsetFormulaBase::metrics(BufferView * bv) const
123 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
128 string const InsetFormulaBase::editMessage() const
130 return _("Math editor mode");
134 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
136 if (!bv->lockInset(this))
137 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
139 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
141 mathcursor = new MathCursor(this, x == 0);
143 // handle ignored click
144 if (hack_x || hack_y) {
145 insetButtonPress(bv, hack_x, hack_y, hack_button);
150 // if that is removed, we won't get the magenta box when entering an
151 // inset for the first time
152 bv->updateInset(this, false);
156 void InsetFormulaBase::edit(BufferView * bv, bool front)
158 // looks hackish but seems to work
159 edit(bv, front ? 0 : 1, 0, 0);
163 void InsetFormulaBase::insetUnlock(BufferView * bv)
166 if (mathcursor->inMacroMode()) {
167 mathcursor->macroModeClose();
168 updateLocal(bv, true);
173 bv->updateInset(this, false);
177 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
179 mathcursor->getPos(x, y);
182 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
186 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
191 if (isCursorVisible())
192 bv->hideLockedInsetCursor();
197 mathcursor->getPos(x, y);
201 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
202 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
203 bv->showLockedInsetCursor(x, y, asc, des);
204 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
207 toggleCursorVisible();
211 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
213 if (isCursorVisible())
219 mathcursor->getPos(x, y);
223 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
224 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
225 bv->fitLockedInsetCursor(x, y, asc, des);
226 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
228 toggleInsetCursor(bv);
232 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
234 if (isCursorVisible())
235 toggleInsetCursor(bv);
239 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
242 bv->updateInset(this, false);
246 vector<string> const InsetFormulaBase::getLabelList() const
248 return std::vector<string>();
252 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
255 bv->updateInset(this, dirty);
259 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
260 int /*x*/, int /*y*/, int /*button*/)
264 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
267 bv->updateInset(this, false);
272 void InsetFormulaBase::insetButtonPress(BufferView * bv,
273 int x, int y, int button)
275 // hack to cope with mouseclick that comes before the call to edit()
279 hack_button = button;
283 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
284 << " but: " << button << "\n";
292 mathcursor->selClear();
293 mathcursor->setPos(x + xo_, y + yo_);
298 lyxerr << "insetButtonPress: 2\n";
301 bv->lockedInsetStoreUndo(Undo::EDIT);
303 mathcursor->selGet(ar);
304 mathcursor->setPos(x + xo_, y + yo_);
306 bv->getLyXText()->selectionAsString(bv->buffer(), false);
307 mathed_parse_cell(ar, sel);
308 mathcursor->insert(ar);
313 bv->updateInset(this, false);
317 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
318 int x, int y, int /*button*/)
323 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
324 //lyxerr << "insetMotionNotify: ignored\n";
330 if (!mathcursor->selection())
331 mathcursor->selStart();
333 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
334 // << ' ' << button << "\n";
336 mathcursor->setPos(x + xo_, y + yo_);
338 bv->updateInset(this, false);
342 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
344 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
348 UpdatableInset::RESULT
349 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
352 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
353 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
358 if (mathcursor->asHyperActiveInset()) {
359 lyxerr << " uurr.... getting dificult now\n";
360 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
363 RESULT result = DISPATCHED;
365 bool was_macro = mathcursor->inMacroMode();
366 bool was_selection = mathcursor->selection();
370 mathcursor->normalize();
374 // --- Cursor Movements ---------------------------------------------
377 sel = true; // fall through...
380 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
381 updateLocal(bv, false);
386 sel = true; // fall through
389 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
390 updateLocal(bv, false);
398 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
399 updateLocal(bv, false);
407 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
408 updateLocal(bv, false);
415 mathcursor->home(sel);
416 updateLocal(bv, false);
423 mathcursor->end(sel);
424 updateLocal(bv, false);
427 case LFUN_DELETE_LINE_FORWARD:
428 bv->lockedInsetStoreUndo(Undo::DELETE);
429 mathcursor->delLine();
430 updateLocal(bv, true);
434 mathcursor->idxNext();
435 updateLocal(bv, false);
439 mathcursor->idxPrev();
440 updateLocal(bv, false);
444 bv->lockedInsetStoreUndo(Undo::EDIT);
445 mathcursor->splitCell();
446 updateLocal(bv, true);
449 case LFUN_DELETE_WORD_BACKWARD:
451 bv->lockedInsetStoreUndo(Undo::DELETE);
452 mathcursor->backspace();
453 bv->updateInset(this, true);
456 case LFUN_DELETE_WORD_FORWARD:
458 bv->lockedInsetStoreUndo(Undo::DELETE);
460 bv->updateInset(this, true);
464 // sprintf(dispatch_buffer, "%d %d",);
465 // dispatch_result = dispatch_buffer;
468 lyxerr << "LFUN_SETXY broken!\n";
471 istringstream is(arg.c_str());
473 mathcursor->setPos(x, y);
474 updateLocal(bv, false);
480 mathcursor->macroModeClose();
481 bv->lockedInsetStoreUndo(Undo::EDIT);
482 mathcursor->selPaste();
483 updateLocal(bv, true);
487 bv->lockedInsetStoreUndo(Undo::DELETE);
488 mathcursor->selCut();
489 updateLocal(bv, true);
493 mathcursor->selCopy();
496 case LFUN_WORDRIGHTSEL:
497 case LFUN_WORDLEFTSEL:
500 // --- accented characters ------------------------------
502 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
503 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
504 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
505 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
506 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
507 case LFUN_MACRON: handleAccent(bv, "bar"); break;
508 case LFUN_DOT: handleAccent(bv, "dot"); break;
509 case LFUN_CARON: handleAccent(bv, "check"); break;
510 case LFUN_BREVE: handleAccent(bv, "breve"); break;
511 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
514 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
515 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
516 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
517 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
518 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
519 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
520 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
521 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
524 handleFont(bv, LM_TC_GREEK1);
526 mathcursor->interpret(arg);
530 //handleFont(bv, LM_TC_TEXTRM);
531 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
532 updateLocal(bv, true);
533 //bv->owner()->message(_("math text mode toggled"));
536 case LFUN_MATH_LIMITS:
537 bv->lockedInsetStoreUndo(Undo::EDIT);
538 if (mathcursor->toggleLimits())
539 updateLocal(bv, true);
545 bv->lockedInsetStoreUndo(Undo::EDIT);
546 mathcursor->setSize(arg);
547 updateLocal(bv, true);
552 case LFUN_INSERT_MATRIX:
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 mathcursor->interpret("matrix " + arg);
556 updateLocal(bv, true);
560 case LFUN_MATH_SPACE:
562 bv->lockedInsetStoreUndo(Undo::EDIT);
563 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
564 updateLocal(bv, true);
568 case LFUN_SUPERSCRIPT:
571 bv->lockedInsetStoreUndo(Undo::EDIT);
572 mathcursor->script((action == LFUN_SUPERSCRIPT));
573 updateLocal(bv, true);
577 case LFUN_MATH_DELIM:
579 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
582 istringstream is(arg.c_str());
585 lyxerr << "can't parse delimeters from '" << arg << "'\n";
588 bv->lockedInsetStoreUndo(Undo::EDIT);
589 mathcursor->handleDelim(ls, rs);
590 updateLocal(bv, true);
594 case LFUN_PROTECTEDSPACE:
595 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
596 bv->lockedInsetStoreUndo(Undo::EDIT);
597 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
598 updateLocal(bv, true);
602 bv->owner()->message(_("Invalid action in math mode!"));
606 case LFUN_MATH_HALIGN:
607 case LFUN_MATH_VALIGN:
608 case LFUN_MATH_ROW_INSERT:
609 case LFUN_MATH_ROW_DELETE:
610 case LFUN_MATH_COLUMN_INSERT:
611 case LFUN_MATH_COLUMN_DELETE:
613 MathInset::idx_type idx = 0;
614 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
616 bv->lockedInsetStoreUndo(Undo::EDIT);
617 char al = arg.size() ? arg[0] : 'c';
619 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
620 case LFUN_MATH_VALIGN: p->valign(al); break;
621 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
622 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
623 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
624 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
627 updateLocal(bv, true);
632 case LFUN_EXEC_COMMAND:
633 result = UNDISPATCHED;
636 case LFUN_BREAKPARAGRAPH:
637 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
638 //lyxerr << "LFUN ignored\n";
642 // interpret this as if a backslash was typed
643 bv->lockedInsetStoreUndo(Undo::EDIT);
644 mathcursor->interpret("\\");
645 updateLocal(bv, true);
649 case LFUN_INSERT_MATH:
650 case LFUN_SELFINSERT:
652 bv->lockedInsetStoreUndo(Undo::EDIT);
653 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
654 updateLocal(bv, true);
658 case LFUN_MATH_PANEL:
659 result = UNDISPATCHED;
663 if (mathcursor->selection())
664 mathcursor->selClear();
666 result = UNDISPATCHED;
670 result = UNDISPATCHED;
673 mathcursor->normalize();
675 lyx::Assert(mathcursor);
677 if (mathcursor->selection() || was_selection)
678 toggleInsetSelection(bv);
680 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
681 result == UNDISPATCHED)
684 bv->unlockInset(this);
686 return result; // original version
690 Inset::Code InsetFormulaBase::lyxCode() const
692 return Inset::MATH_CODE;
696 int InsetFormulaBase::ylow() const
698 return yo_ - ascent(view_, font_);
702 int InsetFormulaBase::yhigh() const
704 return yo_ + descent(view_, font_);
708 int InsetFormulaBase::xlow() const
714 int InsetFormulaBase::xhigh() const
716 return xo_ + width(view_, font_);
720 /////////////////////////////////////////////////////////////////////
723 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
724 bool const &, bool const &)
729 static InsetFormulaBase * lastformula = 0;
730 static MathIterator current = MathIterator(ibegin(par().nucleus()));
732 static string laststr;
734 if (lastformula != this || laststr != str) {
735 //lyxerr << "reset lastformula to " << this << "\n";
738 current = ibegin(par().nucleus());
740 mathed_parse_cell(ar, str);
744 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
746 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
747 if (it.cell().matchpart(ar, it.position().pos_)) {
748 mathcursor->setSelection(it.cursor(), ar.size());
751 // I guess some of the following can go
752 bv->toggleSelection(true);
754 updateLocal(bv, true);
761 //lyxerr << "not found!\n";
763 // we have to unlock ourself in this function by default!
764 // don't ask me why...
765 bv->unlockInset(this);
770 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
771 bool const & a, bool const & b)
773 lyxerr << "searching backward not implemented in mathed" << endl;
774 return searchForward(bv, what, a, b);
778 /////////////////////////////////////////////////////////////////////
781 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
783 if (bv->available()) {
784 // use selection if available..
786 //if (action == LFUN_MATH_IMPORT_SELECTION)
790 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
792 InsetFormulaBase * f;
794 f = new InsetFormula;
795 if (openNewInset(bv, f)) {
796 // don't do that also for LFUN_MATH_MODE unless you want end up with
797 // always changing to mathrm when opening an inlined inset
798 // -- I really hate "LyXfunc overloading"...
800 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
801 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
804 // create a macro if we see "\\newcommand" somewhere, and an ordinary
806 if (sel.find("\\newcommand") == string::npos)
807 f = new InsetFormula(sel);
809 f = new InsetFormulaMacro(sel);
810 bv->getLyXText()->cutSelection(bv);
814 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
818 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
820 mathDispatchCreation(bv, arg, true);
824 void mathDispatchMathMode(BufferView * bv, string const & arg)
826 mathDispatchCreation(bv, arg, false);
830 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
832 mathDispatchCreation(bv, arg, true);
836 void mathDispatchMathMacro(BufferView * bv, string const & arg)
838 if (bv->available()) {
840 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
843 string const s1 = token(s, ' ', 1);
844 int const na = s1.empty() ? 0 : lyx::atoi(s1);
845 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
851 void mathDispatchMathDelim(BufferView * bv, string const & arg)
853 if (bv->available()) {
854 if (openNewInset(bv, new InsetFormula))
855 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
860 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
862 if (bv->available()) {
863 if (openNewInset(bv, new InsetFormula))
864 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
869 void mathDispatchInsertMath(BufferView * bv, string const & arg)
871 if (bv->available()) {
872 if (arg.size() && arg[0] == '\\') {
873 InsetFormula * f = new InsetFormula(arg);
874 if (!bv->insertInset(f))
877 mathDispatchMathMode(bv, arg);
882 void mathDispatchGreek(BufferView * bv, string const & arg)
884 if (bv->available()) {
885 InsetFormula * f = new InsetFormula;
886 if (openNewInset(bv, f)) {
887 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
894 void mathDispatch(BufferView *, kb_action, string const &)