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, string const & arg, MathTextCodes t)
70 if (mathcursor->selection())
71 bv->lockedInsetStoreUndo(Undo::EDIT);
72 mathcursor->handleFont(t);
73 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
74 mathcursor->insert(*it);
78 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
80 if (!bv->insertInset(new_inset)) {
84 new_inset->edit(bv, 0, 0, 0);
93 InsetFormulaBase::InsetFormulaBase()
94 : view_(0), font_(), xo_(0), yo_(0)
96 // This is needed as long the math parser is not re-entrant
97 MathMacroTable::builtinMacros();
98 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
102 // Check if uses AMS macros
103 void InsetFormulaBase::validate(LaTeXFeatures &) const
107 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
114 void InsetFormulaBase::metrics(BufferView * bv) const
118 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
123 string const InsetFormulaBase::editMessage() const
125 return _("Math editor mode");
129 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
131 if (!bv->lockInset(this))
132 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
134 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
136 mathcursor = new MathCursor(this, x == 0);
138 // handle ignored click
139 if (hack_x || hack_y) {
140 insetButtonPress(bv, hack_x, hack_y, hack_button);
145 // if that is removed, we won't get the magenta box when entering an
146 // inset for the first time
147 bv->updateInset(this, false);
151 void InsetFormulaBase::edit(BufferView * bv, bool front)
153 // looks hackish but seems to work
154 edit(bv, front ? 0 : 1, 0, 0);
158 void InsetFormulaBase::insetUnlock(BufferView * bv)
161 if (mathcursor->inMacroMode()) {
162 mathcursor->macroModeClose();
163 updateLocal(bv, true);
168 bv->updateInset(this, false);
172 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
174 mathcursor->getPos(x, y);
177 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
181 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
186 if (isCursorVisible())
187 bv->hideLockedInsetCursor();
192 mathcursor->getPos(x, y);
196 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
197 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
198 bv->showLockedInsetCursor(x, y, asc, des);
199 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
202 toggleCursorVisible();
206 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
208 if (isCursorVisible())
214 mathcursor->getPos(x, y);
218 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
219 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
220 bv->fitLockedInsetCursor(x, y, asc, des);
221 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
223 toggleInsetCursor(bv);
227 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
229 if (isCursorVisible())
230 toggleInsetCursor(bv);
234 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
237 bv->updateInset(this, false);
241 vector<string> const InsetFormulaBase::getLabelList() const
243 return std::vector<string>();
247 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
250 bv->updateInset(this, dirty);
254 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
255 int /*x*/, int /*y*/, int /*button*/)
259 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
262 bv->updateInset(this, false);
267 void InsetFormulaBase::insetButtonPress(BufferView * bv,
268 int x, int y, int button)
270 // hack to cope with mouseclick that comes before the call to edit()
274 hack_button = button;
278 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
279 << " but: " << button << "\n";
287 mathcursor->selClear();
288 mathcursor->setPos(x + xo_, y + yo_);
293 lyxerr << "insetButtonPress: 2\n";
296 bv->lockedInsetStoreUndo(Undo::EDIT);
298 mathcursor->selGet(ar);
299 mathcursor->setPos(x + xo_, y + yo_);
301 bv->getLyXText()->selectionAsString(bv->buffer(), false);
302 mathed_parse_cell(ar, sel);
303 mathcursor->insert(ar);
308 bv->updateInset(this, false);
312 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
313 int x, int y, int /*button*/)
318 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
319 //lyxerr << "insetMotionNotify: ignored\n";
325 if (!mathcursor->selection())
326 mathcursor->selStart();
328 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
329 // << ' ' << button << "\n";
331 mathcursor->setPos(x + xo_, y + yo_);
333 bv->updateInset(this, false);
337 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
339 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
343 UpdatableInset::RESULT
344 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
347 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
348 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
353 if (mathcursor->asHyperActiveInset()) {
354 lyxerr << " uurr.... getting dificult now\n";
355 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
358 RESULT result = DISPATCHED;
360 bool was_macro = mathcursor->inMacroMode();
361 bool was_selection = mathcursor->selection();
365 mathcursor->normalize();
368 // --- Cursor Movements ---------------------------------------------
371 sel = true; // fall through...
374 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
375 updateLocal(bv, false);
376 // write something to the minibuffer
377 //bv->owner()->message(mathcursor->info());
382 sel = true; // fall through
385 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
386 updateLocal(bv, false);
394 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
395 updateLocal(bv, false);
403 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
404 updateLocal(bv, false);
411 mathcursor->home(sel);
412 updateLocal(bv, false);
419 mathcursor->end(sel);
420 updateLocal(bv, false);
423 case LFUN_DELETE_LINE_FORWARD:
424 bv->lockedInsetStoreUndo(Undo::DELETE);
425 mathcursor->delLine();
426 updateLocal(bv, true);
430 mathcursor->idxNext();
431 updateLocal(bv, false);
435 mathcursor->idxPrev();
436 updateLocal(bv, false);
440 bv->lockedInsetStoreUndo(Undo::EDIT);
441 mathcursor->splitCell();
442 updateLocal(bv, true);
445 case LFUN_DELETE_WORD_BACKWARD:
447 bv->lockedInsetStoreUndo(Undo::DELETE);
448 mathcursor->backspace();
449 bv->updateInset(this, true);
452 case LFUN_DELETE_WORD_FORWARD:
454 bv->lockedInsetStoreUndo(Undo::DELETE);
456 bv->updateInset(this, true);
460 // sprintf(dispatch_buffer, "%d %d",);
461 // dispatch_result = dispatch_buffer;
464 lyxerr << "LFUN_SETXY broken!\n";
467 istringstream is(arg.c_str());
469 mathcursor->setPos(x, y);
470 updateLocal(bv, false);
476 mathcursor->macroModeClose();
477 bv->lockedInsetStoreUndo(Undo::EDIT);
478 mathcursor->selPaste();
479 updateLocal(bv, true);
483 bv->lockedInsetStoreUndo(Undo::DELETE);
484 mathcursor->selCut();
485 updateLocal(bv, true);
489 mathcursor->selCopy();
492 case LFUN_WORDRIGHTSEL:
493 case LFUN_WORDLEFTSEL:
496 // --- accented characters ------------------------------
498 case LFUN_UMLAUT: handleAccent(bv, arg, "ddot"); break;
499 case LFUN_CIRCUMFLEX: handleAccent(bv, arg, "hat"); break;
500 case LFUN_GRAVE: handleAccent(bv, arg, "grave"); break;
501 case LFUN_ACUTE: handleAccent(bv, arg, "acute"); break;
502 case LFUN_TILDE: handleAccent(bv, arg, "tilde"); break;
503 case LFUN_MACRON: handleAccent(bv, arg, "bar"); break;
504 case LFUN_DOT: handleAccent(bv, arg, "dot"); break;
505 case LFUN_CARON: handleAccent(bv, arg, "check"); break;
506 case LFUN_BREVE: handleAccent(bv, arg, "breve"); break;
507 case LFUN_VECTOR: handleAccent(bv, arg, "vec"); break;
508 case LFUN_UNDERBAR: handleAccent(bv, arg, "underbar"); break;
511 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
512 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
513 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
514 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
515 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
516 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
517 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
518 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
519 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
520 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
521 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
524 handleFont(bv, arg, LM_TC_GREEK1);
526 mathcursor->interpret(arg);
530 //handleFont(bv, arg, LM_TC_TEXTRM);
532 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
533 //updateLocal(bv, true);
535 //bv->owner()->message(_("math text mode toggled"));
538 case LFUN_MATH_LIMITS:
539 bv->lockedInsetStoreUndo(Undo::EDIT);
540 if (mathcursor->toggleLimits())
541 updateLocal(bv, true);
547 bv->lockedInsetStoreUndo(Undo::EDIT);
548 mathcursor->setSize(arg);
549 updateLocal(bv, true);
554 case LFUN_INSERT_MATRIX:
556 bv->lockedInsetStoreUndo(Undo::EDIT);
557 mathcursor->interpret("matrix " + arg);
558 updateLocal(bv, true);
562 case LFUN_MATH_SPACE:
564 bv->lockedInsetStoreUndo(Undo::EDIT);
565 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
566 updateLocal(bv, true);
570 case LFUN_SUPERSCRIPT:
573 bv->lockedInsetStoreUndo(Undo::EDIT);
574 mathcursor->script((action == LFUN_SUPERSCRIPT));
575 updateLocal(bv, true);
579 case LFUN_MATH_DELIM:
581 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
584 istringstream is(arg.c_str());
587 lyxerr << "can't parse delimeters from '" << arg << "'\n";
590 bv->lockedInsetStoreUndo(Undo::EDIT);
591 mathcursor->handleDelim(ls, rs);
592 updateLocal(bv, true);
596 case LFUN_PROTECTEDSPACE:
597 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
598 bv->lockedInsetStoreUndo(Undo::EDIT);
599 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
600 updateLocal(bv, true);
604 bv->owner()->message(_("Invalid action in math mode!"));
608 case LFUN_MATH_HALIGN:
609 case LFUN_MATH_VALIGN:
610 case LFUN_MATH_ROW_INSERT:
611 case LFUN_MATH_ROW_DELETE:
612 case LFUN_MATH_COLUMN_INSERT:
613 case LFUN_MATH_COLUMN_DELETE:
615 MathInset::idx_type idx = 0;
616 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
618 bv->lockedInsetStoreUndo(Undo::EDIT);
619 char al = arg.size() ? arg[0] : 'c';
621 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
622 case LFUN_MATH_VALIGN: p->valign(al); break;
623 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
624 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
625 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
626 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
629 updateLocal(bv, true);
634 case LFUN_EXEC_COMMAND:
635 result = UNDISPATCHED;
638 case LFUN_BREAKPARAGRAPH:
639 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
640 //lyxerr << "LFUN ignored\n";
644 // interpret this as if a backslash was typed
645 bv->lockedInsetStoreUndo(Undo::EDIT);
646 mathcursor->interpret("\\");
647 updateLocal(bv, true);
651 case LFUN_INSERT_MATH:
652 case LFUN_SELFINSERT:
654 bv->lockedInsetStoreUndo(Undo::EDIT);
655 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
656 updateLocal(bv, true);
660 case LFUN_MATH_PANEL:
661 result = UNDISPATCHED;
665 if (mathcursor->selection())
666 mathcursor->selClear();
668 result = UNDISPATCHED;
672 result = UNDISPATCHED;
675 mathcursor->normalize();
677 lyx::Assert(mathcursor);
679 if (mathcursor->selection() || was_selection)
680 toggleInsetSelection(bv);
682 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
683 result == UNDISPATCHED)
686 bv->unlockInset(this);
688 return result; // original version
692 Inset::Code InsetFormulaBase::lyxCode() const
694 return Inset::MATH_CODE;
698 int InsetFormulaBase::ylow() const
700 return yo_ - ascent(view_, font_);
704 int InsetFormulaBase::yhigh() const
706 return yo_ + descent(view_, font_);
710 int InsetFormulaBase::xlow() const
716 int InsetFormulaBase::xhigh() const
718 return xo_ + width(view_, font_);
722 /////////////////////////////////////////////////////////////////////
725 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
731 static InsetFormulaBase * lastformula = 0;
732 static MathIterator current = MathIterator(ibegin(par().nucleus()));
734 static string laststr;
736 if (lastformula != this || laststr != str) {
737 //lyxerr << "reset lastformula to " << this << "\n";
740 current = ibegin(par().nucleus());
742 mathed_parse_cell(ar, str);
746 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
748 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
749 if (it.cell().matchpart(ar, it.position().pos_)) {
750 mathcursor->setSelection(it.cursor(), ar.size());
753 // I guess some of the following can go
754 bv->toggleSelection(true);
756 updateLocal(bv, true);
763 //lyxerr << "not found!\n";
765 // we have to unlock ourself in this function by default!
766 // don't ask me why...
767 bv->unlockInset(this);
772 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
775 lyxerr << "searching backward not implemented in mathed" << endl;
776 return searchForward(bv, what, a, b);
781 void InsetFormulaBase::handleAccent(BufferView * bv,
782 string const & arg, string const & name)
784 bv->lockedInsetStoreUndo(Undo::EDIT);
785 MathAtom at = createMathInset(name);
786 mathed_parse_cell(at->cell(0), arg);
787 mathcursor->insert(at);
788 updateLocal(bv, true);
792 /////////////////////////////////////////////////////////////////////
795 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
797 if (bv->available()) {
798 // use selection if available..
800 //if (action == LFUN_MATH_IMPORT_SELECTION)
804 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
806 InsetFormulaBase * f;
808 f = new InsetFormula;
809 if (openNewInset(bv, f)) {
810 // don't do that also for LFUN_MATH_MODE unless you want end up with
811 // always changing to mathrm when opening an inlined inset
812 // -- I really hate "LyXfunc overloading"...
814 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
815 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
818 // create a macro if we see "\\newcommand" somewhere, and an ordinary
820 if (sel.find("\\newcommand") == string::npos)
821 f = new InsetFormula(sel);
823 f = new InsetFormulaMacro(sel);
824 bv->getLyXText()->cutSelection(bv);
828 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
832 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
834 mathDispatchCreation(bv, arg, true);
838 void mathDispatchMathMode(BufferView * bv, string const & arg)
840 mathDispatchCreation(bv, arg, false);
844 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
846 mathDispatchCreation(bv, arg, true);
850 void mathDispatchMathMacro(BufferView * bv, string const & arg)
852 if (bv->available()) {
854 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
857 string const s1 = token(s, ' ', 1);
858 int const na = s1.empty() ? 0 : lyx::atoi(s1);
859 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
865 void mathDispatchMathDelim(BufferView * bv, string const & arg)
867 if (bv->available()) {
868 if (openNewInset(bv, new InsetFormula))
869 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
874 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
876 if (bv->available()) {
877 if (openNewInset(bv, new InsetFormula))
878 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
883 void mathDispatchInsertMath(BufferView * bv, string const & arg)
885 if (bv->available()) {
886 if (arg.size() && arg[0] == '\\') {
887 InsetFormula * f = new InsetFormula(arg);
888 if (!bv->insertInset(f))
891 mathDispatchMathMode(bv, arg);
896 void mathDispatchGreek(BufferView * bv, string const & arg)
898 if (bv->available()) {
899 InsetFormula * f = new InsetFormula;
900 if (openNewInset(bv, f)) {
901 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
908 void mathDispatch(BufferView *, kb_action, string const &)