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()
106 : view_(0), font_(), xo_(0), yo_(0)
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);
177 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
181 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
186 if (isCursorVisible())
187 bv->hideLockedInsetCursor();
191 mathcursor->getPos(x, y);
196 MathMetricsInfo mi(bv, font_, 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()) {
212 mathcursor->getPos(x, y);
215 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
216 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
217 bv->fitLockedInsetCursor(x, y, asc, des);
218 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
220 toggleInsetCursor(bv);
225 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
227 if (isCursorVisible())
228 toggleInsetCursor(bv);
232 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
235 bv->updateInset(this, false);
239 vector<string> const InsetFormulaBase::getLabelList() const
241 return std::vector<string>();
245 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
248 bv->updateInset(this, dirty);
252 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
253 int x, int y, int /*button*/)
257 mathcursor->setPos(x + xo_, y + yo_);
258 //lyxerr << "insetButtonRelease: " << x + xo_ << " " << y + yo_ << "\n";
265 bv->updateInset(this, false);
270 void InsetFormulaBase::insetButtonPress(BufferView * bv,
271 int x, int y, int /*button*/)
276 if (mathcursor && mathcursor->selection()) {
277 mathcursor->selClear();
278 bv->updateInset(this, false);
283 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
284 int x, int y, int /*button*/)
286 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
289 mathcursor->setPos(sel_x, sel_y);
290 mathcursor->selStart();
292 mathcursor->getPos(sel_x, sel_y);
293 } else if (sel_flag) {
295 mathcursor->setPos(x, y);
297 mathcursor->getPos(x, y);
298 if (sel_x != x || sel_y != y)
299 bv->updateInset(this, false);
306 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
308 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
312 UpdatableInset::RESULT
313 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
316 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
317 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
322 if (mathcursor->asHyperActiveInset()) {
323 lyxerr << " uurr.... getting dificult now\n";
324 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
327 RESULT result = DISPATCHED;
329 bool was_macro = mathcursor->inMacroMode();
330 bool was_selection = mathcursor->selection();
334 mathcursor->normalize();
338 // --- Cursor Movements ---------------------------------------------
341 sel = true; // fall through...
344 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
345 updateLocal(bv, false);
350 sel = true; // fall through
353 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
354 updateLocal(bv, false);
362 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
363 updateLocal(bv, false);
371 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
372 updateLocal(bv, false);
379 mathcursor->home(sel);
380 updateLocal(bv, false);
387 mathcursor->end(sel);
388 updateLocal(bv, false);
391 case LFUN_DELETE_LINE_FORWARD:
392 bv->lockedInsetStoreUndo(Undo::DELETE);
393 mathcursor->delLine();
394 updateLocal(bv, true);
398 mathcursor->idxNext();
399 updateLocal(bv, false);
403 mathcursor->idxPrev();
404 updateLocal(bv, false);
408 bv->lockedInsetStoreUndo(Undo::EDIT);
409 mathcursor->splitCell();
410 updateLocal(bv, true);
413 case LFUN_DELETE_WORD_BACKWARD:
415 bv->lockedInsetStoreUndo(Undo::DELETE);
416 mathcursor->backspace();
417 bv->updateInset(this, true);
420 case LFUN_DELETE_WORD_FORWARD:
422 bv->lockedInsetStoreUndo(Undo::DELETE);
424 bv->updateInset(this, true);
428 // sprintf(dispatch_buffer, "%d %d",);
429 // dispatch_result = dispatch_buffer;
432 lyxerr << "LFUN_SETXY broken!\n";
435 istringstream is(arg.c_str());
437 mathcursor->setPos(x, y);
438 updateLocal(bv, false);
444 mathcursor->macroModeClose();
445 bv->lockedInsetStoreUndo(Undo::INSERT);
446 mathcursor->selPaste();
447 updateLocal(bv, true);
451 bv->lockedInsetStoreUndo(Undo::DELETE);
452 mathcursor->selCut();
453 updateLocal(bv, true);
457 mathcursor->selCopy();
460 case LFUN_WORDRIGHTSEL:
461 case LFUN_WORDLEFTSEL:
464 // --- accented characters ------------------------------
466 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
467 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
468 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
469 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
470 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
471 case LFUN_MACRON: handleAccent(bv, "bar"); break;
472 case LFUN_DOT: handleAccent(bv, "dot"); break;
473 case LFUN_CARON: handleAccent(bv, "check"); break;
474 case LFUN_BREVE: handleAccent(bv, "breve"); break;
475 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
478 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
479 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
480 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
481 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
482 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
483 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
484 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
485 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
488 handleFont(bv, LM_TC_GREEK1);
490 mathcursor->interpret(arg);
494 handleFont(bv, LM_TC_TEXTRM);
495 //bv->owner()->message(_("math text mode toggled"));
498 case LFUN_MATH_LIMITS:
499 bv->lockedInsetStoreUndo(Undo::INSERT);
500 if (mathcursor->toggleLimits())
501 updateLocal(bv, true);
507 bv->lockedInsetStoreUndo(Undo::INSERT);
508 mathcursor->setSize(arg);
509 updateLocal(bv, true);
514 case LFUN_INSERT_MATRIX:
516 bv->lockedInsetStoreUndo(Undo::INSERT);
517 mathcursor->interpret("matrix " + arg);
518 updateLocal(bv, true);
522 case LFUN_MATH_SPACE:
524 bv->lockedInsetStoreUndo(Undo::EDIT);
525 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
526 updateLocal(bv, true);
530 case LFUN_MATH_DELIM:
532 bv->lockedInsetStoreUndo(Undo::INSERT);
533 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
536 istringstream is(arg.c_str());
539 lyxerr << "can't parse delimeters from '" << arg << "'\n";
542 bv->lockedInsetStoreUndo(Undo::EDIT);
543 mathcursor->handleDelim(ls, rs);
544 updateLocal(bv, true);
548 case LFUN_PROTECTEDSPACE:
549 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
550 bv->lockedInsetStoreUndo(Undo::INSERT);
551 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
552 updateLocal(bv, true);
556 bv->owner()->message(_("Invalid action in math mode!"));
560 case LFUN_MATH_HALIGN:
562 bv->lockedInsetStoreUndo(Undo::INSERT);
563 lyxerr << "handling halign '" << arg << "'\n";
564 MathInset::idx_type idx;
565 MathArrayInset * p = matrixpar(idx);
568 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
569 updateLocal(bv, true);
573 case LFUN_MATH_VALIGN:
575 bv->lockedInsetStoreUndo(Undo::INSERT);
576 lyxerr << "handling valign '" << arg << "'\n";
577 MathInset::idx_type idx;
578 MathArrayInset * p = matrixpar(idx);
581 p->valign(arg.size() ? arg[0] : 'c');
582 updateLocal(bv, true);
586 case LFUN_MATH_ROW_INSERT:
588 bv->lockedInsetStoreUndo(Undo::INSERT);
589 MathInset::idx_type idx;
590 MathArrayInset * p = matrixpar(idx);
591 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
594 p->addRow(p->row(idx));
595 updateLocal(bv, true);
599 case LFUN_MATH_ROW_DELETE:
601 bv->lockedInsetStoreUndo(Undo::INSERT);
602 MathInset::idx_type idx;
603 MathArrayInset * p = matrixpar(idx);
604 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
607 p->delRow(p->row(idx));
608 updateLocal(bv, true);
612 case LFUN_MATH_COLUMN_INSERT:
614 bv->lockedInsetStoreUndo(Undo::INSERT);
615 MathInset::idx_type idx;
616 MathArrayInset * p = matrixpar(idx);
619 p->addCol(p->col(idx));
620 updateLocal(bv, true);
624 case LFUN_MATH_COLUMN_DELETE:
626 bv->lockedInsetStoreUndo(Undo::INSERT);
627 MathInset::idx_type idx;
628 MathArrayInset * p = matrixpar(idx);
631 p->delCol(p->col(idx));
632 updateLocal(bv, true);
636 case LFUN_EXEC_COMMAND:
637 result = UNDISPATCHED;
640 case LFUN_BREAKPARAGRAPH:
641 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
642 //lyxerr << "LFUN ignored\n";
646 case LFUN_INSERT_MATH:
647 case LFUN_SELFINSERT:
649 bv->lockedInsetStoreUndo(Undo::INSERT);
650 mathcursor->interpret(arg);
651 updateLocal(bv, true);
655 case LFUN_MATH_PANEL:
656 result = UNDISPATCHED;
660 result = UNDISPATCHED;
663 lyx::Assert(mathcursor);
664 //mathcursor->normalize();
666 if (//was_macro != mathcursor->inMacroMode() &&
667 action >= 0 && action != LFUN_BACKSPACE)
668 updateLocal(bv, true);
670 if (mathcursor->selection() || was_selection)
671 toggleInsetSelection(bv);
673 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
674 result == UNDISPATCHED)
677 bv->unlockInset(this);
679 return result; // original version
683 Inset::Code InsetFormulaBase::lyxCode() const
685 return Inset::MATH_CODE;
689 int InsetFormulaBase::upperY() const
691 return yo_ - ascent(view_, font_);
695 int InsetFormulaBase::lowerY() const
697 return yo_ + descent(view_, font_);
701 /////////////////////////////////////////////////////////////////////
704 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
706 if (bv->available()) {
707 // use selection if available..
709 //if (action == LFUN_MATH_IMPORT_SELECTION)
713 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
715 InsetFormulaBase * f;
717 f = new InsetFormula;
718 if (openNewInset(bv, f)) {
719 // don't do that also for LFUN_MATH_MODE unless you want end up with
720 // always changing to mathrm when opening an inlined inset
721 // -- I really hate "LyXfunc overloading"...
723 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
724 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
727 // create a macro if we see "\\newcommand" somewhere, and an ordinary
729 if (sel.find("\\newcommand") == string::npos)
730 f = new InsetFormula(sel);
732 f = new InsetFormulaMacro(sel);
733 bv->getLyXText()->cutSelection(bv);
737 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
741 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
743 mathDispatchCreation(bv, arg, true);
747 void mathDispatchMathMode(BufferView * bv, string const & arg)
749 mathDispatchCreation(bv, arg, false);
753 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
755 mathDispatchCreation(bv, arg, true);
759 void mathDispatchMathMacro(BufferView * bv, string const & arg)
761 if (bv->available()) {
763 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
766 string const s1 = token(s, ' ', 1);
767 int const na = s1.empty() ? 0 : lyx::atoi(s1);
768 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
774 void mathDispatchMathDelim(BufferView * bv, string const & arg)
776 if (bv->available()) {
777 if (openNewInset(bv, new InsetFormula))
778 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
783 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
785 if (bv->available()) {
786 if (openNewInset(bv, new InsetFormula))
787 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
792 void mathDispatchInsertMath(BufferView * bv, string const & arg)
794 if (bv->available()) {
795 if (arg.size() && arg[0] == '\\') {
796 InsetFormula * f = new InsetFormula(arg);
797 if (!bv->insertInset(f))
800 mathDispatchMathMode(bv, arg);
805 void mathDispatchGreek(BufferView * bv, string const & arg)
807 if (bv->available()) {
808 InsetFormula * f = new InsetFormula;
809 if (openNewInset(bv, f)) {
810 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);