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 "math_cursor.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
36 #include "support/lstrings.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "math_macrotable.h"
43 #include "math_factory.h"
44 #include "support/lyxlib.h"
45 #include "undo_funcs.h"
51 extern char const * latex_mathenv[];
52 MathCursor * mathcursor = 0;
63 void handleFont(BufferView * bv, MathTextCodes t)
65 if (mathcursor->selection())
66 bv->lockedInsetStoreUndo(Undo::EDIT);
67 mathcursor->handleFont(t);
71 void handleAccent(BufferView * bv, string const & name)
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->insert(createMathInset(name));
78 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
80 LyXText * lt = bv->getLyXText();
84 if (!bv->insertInset(new_inset)) {
88 new_inset->edit(bv, 0, 0, 0);
93 // returns the nearest enclosing grid
94 MathArrayInset * matrixpar(MathInset::idx_type & idx)
97 return mathcursor ? mathcursor->enclosingArray(idx) : 0;
105 InsetFormulaBase::InsetFormulaBase()
108 // This is needed as long the math parser is not re-entrant
109 MathMacroTable::builtinMacros();
110 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
114 // Check if uses AMS macros
115 void InsetFormulaBase::validate(LaTeXFeatures &) const
119 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
124 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
129 string const InsetFormulaBase::editMessage() const
131 return _("Math editor mode");
135 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
137 if (!bv->lockInset(this))
138 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
140 mathcursor = new MathCursor(this, x == 0);
142 // if that is removed, we won't get the magenta box when entering an
143 // inset for the first time
144 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);
181 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
186 if (isCursorVisible())
187 bv->hideLockedInsetCursor();
191 mathcursor->getPos(x, y);
197 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
198 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
199 bv->showLockedInsetCursor(x, y, asc, des);
202 toggleCursorVisible();
206 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
208 if (!isCursorVisible()) {
212 mathcursor->getPos(x, y);
217 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
218 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
219 bv->fitLockedInsetCursor(x, y, asc, des);
221 toggleInsetCursor(bv);
226 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
228 if (isCursorVisible())
229 toggleInsetCursor(bv);
233 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
236 bv->updateInset(this, false);
240 vector<string> const InsetFormulaBase::getLabelList() const
242 return std::vector<string>();
246 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
249 bv->updateInset(this, dirty);
253 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
254 int x, int y, int /*button*/)
260 mathcursor->setPos(x, y);
267 bv->updateInset(this, false);
272 void InsetFormulaBase::insetButtonPress(BufferView * bv,
273 int x, int y, int /*button*/)
278 if (mathcursor && mathcursor->selection()) {
279 mathcursor->selClear();
280 bv->updateInset(this, false);
285 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
286 int x, int y, int /*button*/)
288 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
291 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
292 mathcursor->selStart();
294 mathcursor->getPos(sel_x, sel_y);
295 } else if (sel_flag) {
299 mathcursor->setPos(x, y);
301 mathcursor->getPos(x, y);
302 if (sel_x != x || sel_y != y)
303 bv->updateInset(this, false);
310 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
312 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
316 UpdatableInset::RESULT
317 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
320 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
321 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
326 RESULT result = DISPATCHED;
328 bool was_macro = mathcursor->inMacroMode();
329 bool was_selection = mathcursor->selection();
333 mathcursor->normalize();
337 // --- Cursor Movements ---------------------------------------------
340 sel = true; // fall through...
343 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
344 updateLocal(bv, false);
349 sel = true; // fall through
352 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
353 updateLocal(bv, false);
361 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
362 updateLocal(bv, false);
370 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
371 updateLocal(bv, false);
378 mathcursor->home(sel);
379 updateLocal(bv, false);
386 mathcursor->end(sel);
387 updateLocal(bv, false);
390 case LFUN_DELETE_LINE_FORWARD:
391 bv->lockedInsetStoreUndo(Undo::DELETE);
392 mathcursor->delLine();
393 updateLocal(bv, true);
397 mathcursor->idxNext();
398 updateLocal(bv, false);
402 mathcursor->idxPrev();
403 updateLocal(bv, false);
407 bv->lockedInsetStoreUndo(Undo::EDIT);
408 mathcursor->splitCell();
409 updateLocal(bv, true);
412 case LFUN_DELETE_WORD_BACKWARD:
414 bv->lockedInsetStoreUndo(Undo::DELETE);
415 mathcursor->backspace();
416 bv->updateInset(this, true);
419 case LFUN_DELETE_WORD_FORWARD:
421 bv->lockedInsetStoreUndo(Undo::DELETE);
423 bv->updateInset(this, true);
427 // sprintf(dispatch_buffer, "%d %d",);
428 // dispatch_result = dispatch_buffer;
431 lyxerr << "LFUN_SETXY broken!\n";
436 istringstream is(arg.c_str());
438 par()->getXY(x1, y1);
439 mathcursor->setPos(x1 + x, y1 + y);
440 updateLocal(bv, false);
446 mathcursor->macroModeClose();
447 bv->lockedInsetStoreUndo(Undo::INSERT);
448 mathcursor->selPaste();
449 updateLocal(bv, true);
453 bv->lockedInsetStoreUndo(Undo::DELETE);
454 mathcursor->selCut();
455 updateLocal(bv, true);
459 mathcursor->selCopy();
462 case LFUN_WORDRIGHTSEL:
463 case LFUN_WORDLEFTSEL:
466 // --- accented characters ------------------------------
468 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
469 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
470 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
471 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
472 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
473 case LFUN_MACRON: handleAccent(bv, "bar"); break;
474 case LFUN_DOT: handleAccent(bv, "dot"); break;
475 case LFUN_CARON: handleAccent(bv, "check"); break;
476 case LFUN_BREVE: handleAccent(bv, "breve"); break;
477 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
480 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
481 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
482 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
483 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
484 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
485 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
486 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
487 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
490 handleFont(bv, LM_TC_GREEK1);
492 mathcursor->interpret(arg);
496 handleFont(bv, LM_TC_TEXTRM);
497 //bv->owner()->message(_("math text mode toggled"));
500 case LFUN_MATH_LIMITS:
501 bv->lockedInsetStoreUndo(Undo::INSERT);
502 if (mathcursor->toggleLimits())
503 updateLocal(bv, true);
509 bv->lockedInsetStoreUndo(Undo::INSERT);
510 mathcursor->setSize(arg);
511 updateLocal(bv, true);
516 case LFUN_INSERT_MATRIX:
518 bv->lockedInsetStoreUndo(Undo::INSERT);
519 mathcursor->interpret("matrix " + arg);
520 updateLocal(bv, true);
524 case LFUN_MATH_SPACE:
526 bv->lockedInsetStoreUndo(Undo::EDIT);
527 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
528 updateLocal(bv, true);
532 case LFUN_MATH_DELIM:
534 bv->lockedInsetStoreUndo(Undo::INSERT);
535 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
538 istringstream is(arg.c_str());
541 lyxerr << "can't parse delimeters from '" << arg << "'\n";
544 bv->lockedInsetStoreUndo(Undo::EDIT);
545 mathcursor->handleDelim(ls, rs);
546 updateLocal(bv, true);
550 case LFUN_PROTECTEDSPACE:
551 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
552 bv->lockedInsetStoreUndo(Undo::INSERT);
553 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
554 updateLocal(bv, true);
558 bv->owner()->message(_("Invalid action in math mode!"));
562 case LFUN_MATH_HALIGN:
564 bv->lockedInsetStoreUndo(Undo::INSERT);
565 lyxerr << "handling halign '" << arg << "'\n";
566 MathInset::idx_type idx;
567 MathArrayInset * p = matrixpar(idx);
570 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
571 updateLocal(bv, true);
575 case LFUN_MATH_VALIGN:
577 bv->lockedInsetStoreUndo(Undo::INSERT);
578 lyxerr << "handling valign '" << arg << "'\n";
579 MathInset::idx_type idx;
580 MathArrayInset * p = matrixpar(idx);
583 p->valign(arg.size() ? arg[0] : 'c');
584 updateLocal(bv, true);
588 case LFUN_MATH_ROW_INSERT:
590 bv->lockedInsetStoreUndo(Undo::INSERT);
591 MathInset::idx_type idx;
592 MathArrayInset * p = matrixpar(idx);
593 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
596 p->addRow(p->row(idx));
597 updateLocal(bv, true);
601 case LFUN_MATH_ROW_DELETE:
603 bv->lockedInsetStoreUndo(Undo::INSERT);
604 MathInset::idx_type idx;
605 MathArrayInset * p = matrixpar(idx);
606 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
609 p->delRow(p->row(idx));
610 updateLocal(bv, true);
614 case LFUN_MATH_COLUMN_INSERT:
616 bv->lockedInsetStoreUndo(Undo::INSERT);
617 MathInset::idx_type idx;
618 MathArrayInset * p = matrixpar(idx);
621 p->addCol(p->col(idx));
622 updateLocal(bv, true);
626 case LFUN_MATH_COLUMN_DELETE:
628 bv->lockedInsetStoreUndo(Undo::INSERT);
629 MathInset::idx_type idx;
630 MathArrayInset * p = matrixpar(idx);
633 p->delCol(p->col(idx));
634 updateLocal(bv, true);
638 case LFUN_EXEC_COMMAND:
639 result = UNDISPATCHED;
643 case LFUN_INSERT_MATH:
644 case LFUN_SELFINSERT:
646 bv->lockedInsetStoreUndo(Undo::INSERT);
647 mathcursor->interpret(arg);
648 updateLocal(bv, true);
652 case LFUN_MATH_PANEL:
653 result = UNDISPATCHED;
657 result = UNDISPATCHED;
660 lyx::Assert(mathcursor);
661 //mathcursor->normalize();
663 if (//was_macro != mathcursor->inMacroMode() &&
664 action >= 0 && action != LFUN_BACKSPACE)
665 updateLocal(bv, true);
667 if (mathcursor->selection() || was_selection)
668 toggleInsetSelection(bv);
670 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
671 result == UNDISPATCHED)
674 bv->unlockInset(this);
676 return result; // original version
680 Inset::Code InsetFormulaBase::lyxCode() const
682 return Inset::MATH_CODE;
686 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
688 if (bv->available()) {
689 // use selection if available..
691 //if (action == LFUN_MATH_IMPORT_SELECTION)
695 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
697 InsetFormulaBase * f;
699 f = new InsetFormula;
700 if (openNewInset(bv, f)) {
701 // don't do that also for LFUN_MATH_MODE unless you want end up with
702 // always changing to mathrm when opening an inlined inset
703 // -- I really hate "LyXfunc overloading"...
705 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
706 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
709 // create a macro if we see "\\newcommand" somewhere, and an ordinary
711 if (sel.find("\\newcommand") == string::npos)
712 f = new InsetFormula(sel);
714 f = new InsetFormulaMacro(sel);
715 bv->getLyXText()->cutSelection(bv);
719 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
723 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
725 mathDispatchCreation(bv, arg, true);
729 void mathDispatchMathMode(BufferView * bv, string const & arg)
731 mathDispatchCreation(bv, arg, false);
735 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
737 mathDispatchCreation(bv, arg, true);
741 void mathDispatchMathMacro(BufferView * bv, string const & arg)
743 if (bv->available()) {
745 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
748 string const s1 = token(s, ' ', 1);
749 int const na = s1.empty() ? 0 : lyx::atoi(s1);
750 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
756 void mathDispatchMathDelim(BufferView * bv, string const & arg)
758 if (bv->available()) {
759 if (openNewInset(bv, new InsetFormula))
760 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
765 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
767 if (bv->available()) {
768 if (openNewInset(bv, new InsetFormula))
769 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
774 void mathDispatchInsertMath(BufferView * bv, string const & arg)
776 if (bv->available()) {
777 if (arg.size() && arg[0] == '\\') {
778 InsetFormula * f = new InsetFormula(arg);
779 if (!bv->insertInset(f))
782 mathDispatchMathMode(bv, arg);
787 void mathDispatchGreek(BufferView * bv, string const & arg)
789 if (bv->available()) {
790 InsetFormula * f = new InsetFormula;
791 if (openNewInset(bv, f)) {
792 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);