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"
35 #include "math_support.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;
64 void handleFont(BufferView * bv, MathTextCodes t)
66 if (mathcursor->selection())
67 bv->lockedInsetStoreUndo(Undo::EDIT);
68 mathcursor->handleFont(t);
72 void handleAccent(BufferView * bv, string const & name)
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->insert(createMathInset(name));
79 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
81 if (!bv->insertInset(new_inset)) {
85 new_inset->edit(bv, 0, 0, 0);
94 InsetFormulaBase::InsetFormulaBase()
95 : view_(0), font_(), xo_(0), yo_(0)
97 // This is needed as long the math parser is not re-entrant
98 MathMacroTable::builtinMacros();
99 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
103 // Check if uses AMS macros
104 void InsetFormulaBase::validate(LaTeXFeatures &) const
108 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
113 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
118 string const InsetFormulaBase::editMessage() const
120 return _("Math editor mode");
124 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
126 if (!bv->lockInset(this))
127 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
129 mathcursor = new MathCursor(this, x == 0);
131 // if that is removed, we won't get the magenta box when entering an
132 // inset for the first time
133 bv->updateInset(this, false);
140 void InsetFormulaBase::edit(BufferView * bv, bool front)
142 // looks hackish but seems to work
143 edit(bv, front ? 0 : 1, 0, 0);
147 void InsetFormulaBase::insetUnlock(BufferView * bv)
150 if (mathcursor->inMacroMode()) {
151 mathcursor->macroModeClose();
152 updateLocal(bv, true);
157 bv->updateInset(this, false);
161 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
163 mathcursor->getPos(x, y);
166 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
170 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
175 if (isCursorVisible())
176 bv->hideLockedInsetCursor();
181 mathcursor->getPos(x, y);
186 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
187 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
188 bv->showLockedInsetCursor(x, y, asc, des);
189 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
192 toggleCursorVisible();
196 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
198 if (!isCursorVisible()) {
202 mathcursor->getPos(x, y);
205 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
206 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
207 //bv->fitLockedInsetCursor(x, y, asc, des);
209 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
211 toggleInsetCursor(bv);
216 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
218 if (isCursorVisible())
219 toggleInsetCursor(bv);
223 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
226 bv->updateInset(this, false);
230 vector<string> const InsetFormulaBase::getLabelList() const
232 return std::vector<string>();
236 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
239 bv->updateInset(this, dirty);
243 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
244 int x, int y, int /*button*/)
248 mathcursor->setPos(x + xo_, y + yo_);
249 //lyxerr << "insetButtonRelease: " << x + xo_ << " " << y + yo_ << "\n";
256 bv->updateInset(this, false);
261 void InsetFormulaBase::insetButtonPress(BufferView * bv,
262 int x, int y, int /*button*/)
267 if (mathcursor && mathcursor->selection()) {
268 mathcursor->selClear();
269 bv->updateInset(this, false);
274 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
275 int x, int y, int /*button*/)
277 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
280 mathcursor->setPos(sel_x, sel_y);
281 mathcursor->selStart();
283 mathcursor->getPos(sel_x, sel_y);
284 } else if (sel_flag) {
286 mathcursor->setPos(x, y);
288 mathcursor->getPos(x, y);
289 if (sel_x != x || sel_y != y)
290 bv->updateInset(this, false);
297 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
299 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
303 UpdatableInset::RESULT
304 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
307 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
308 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
313 if (mathcursor->asHyperActiveInset()) {
314 lyxerr << " uurr.... getting dificult now\n";
315 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
318 RESULT result = DISPATCHED;
320 bool was_macro = mathcursor->inMacroMode();
321 bool was_selection = mathcursor->selection();
325 mathcursor->normalize();
329 // --- Cursor Movements ---------------------------------------------
332 sel = true; // fall through...
335 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
336 updateLocal(bv, false);
341 sel = true; // fall through
344 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
345 updateLocal(bv, false);
353 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
354 updateLocal(bv, false);
362 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
363 updateLocal(bv, false);
370 mathcursor->home(sel);
371 updateLocal(bv, false);
378 mathcursor->end(sel);
379 updateLocal(bv, false);
382 case LFUN_DELETE_LINE_FORWARD:
383 bv->lockedInsetStoreUndo(Undo::DELETE);
384 mathcursor->delLine();
385 updateLocal(bv, true);
389 mathcursor->idxNext();
390 updateLocal(bv, false);
394 mathcursor->idxPrev();
395 updateLocal(bv, false);
399 bv->lockedInsetStoreUndo(Undo::EDIT);
400 mathcursor->splitCell();
401 updateLocal(bv, true);
404 case LFUN_DELETE_WORD_BACKWARD:
406 bv->lockedInsetStoreUndo(Undo::DELETE);
407 mathcursor->backspace();
408 bv->updateInset(this, true);
411 case LFUN_DELETE_WORD_FORWARD:
413 bv->lockedInsetStoreUndo(Undo::DELETE);
415 bv->updateInset(this, true);
419 // sprintf(dispatch_buffer, "%d %d",);
420 // dispatch_result = dispatch_buffer;
423 lyxerr << "LFUN_SETXY broken!\n";
426 istringstream is(arg.c_str());
428 mathcursor->setPos(x, y);
429 updateLocal(bv, false);
435 mathcursor->macroModeClose();
436 bv->lockedInsetStoreUndo(Undo::EDIT);
437 mathcursor->selPaste();
438 updateLocal(bv, true);
442 bv->lockedInsetStoreUndo(Undo::DELETE);
443 mathcursor->selCut();
444 updateLocal(bv, true);
448 mathcursor->selCopy();
451 case LFUN_WORDRIGHTSEL:
452 case LFUN_WORDLEFTSEL:
455 // --- accented characters ------------------------------
457 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
458 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
459 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
460 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
461 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
462 case LFUN_MACRON: handleAccent(bv, "bar"); break;
463 case LFUN_DOT: handleAccent(bv, "dot"); break;
464 case LFUN_CARON: handleAccent(bv, "check"); break;
465 case LFUN_BREVE: handleAccent(bv, "breve"); break;
466 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
469 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
470 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
471 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
472 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
473 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
474 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
475 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
476 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
479 handleFont(bv, LM_TC_GREEK1);
481 mathcursor->interpret(arg);
485 handleFont(bv, LM_TC_TEXTRM);
486 //bv->owner()->message(_("math text mode toggled"));
489 case LFUN_MATH_LIMITS:
490 bv->lockedInsetStoreUndo(Undo::EDIT);
491 if (mathcursor->toggleLimits())
492 updateLocal(bv, true);
498 bv->lockedInsetStoreUndo(Undo::EDIT);
499 mathcursor->setSize(arg);
500 updateLocal(bv, true);
505 case LFUN_INSERT_MATRIX:
507 bv->lockedInsetStoreUndo(Undo::EDIT);
508 mathcursor->interpret("matrix " + arg);
509 updateLocal(bv, true);
513 case LFUN_MATH_SPACE:
515 bv->lockedInsetStoreUndo(Undo::EDIT);
516 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
517 updateLocal(bv, true);
521 case LFUN_MATH_DELIM:
523 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
526 istringstream is(arg.c_str());
529 lyxerr << "can't parse delimeters from '" << arg << "'\n";
532 bv->lockedInsetStoreUndo(Undo::EDIT);
533 mathcursor->handleDelim(ls, rs);
534 updateLocal(bv, true);
538 case LFUN_PROTECTEDSPACE:
539 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
540 bv->lockedInsetStoreUndo(Undo::EDIT);
541 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
542 updateLocal(bv, true);
546 bv->owner()->message(_("Invalid action in math mode!"));
550 case LFUN_MATH_HALIGN:
551 case LFUN_MATH_VALIGN:
552 case LFUN_MATH_ROW_INSERT:
553 case LFUN_MATH_ROW_DELETE:
554 case LFUN_MATH_COLUMN_INSERT:
555 case LFUN_MATH_COLUMN_DELETE:
557 MathInset::idx_type idx = 0;
558 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
560 bv->lockedInsetStoreUndo(Undo::EDIT);
561 char al = arg.size() ? arg[0] : 'c';
563 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
564 case LFUN_MATH_VALIGN: p->valign(al); break;
565 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
566 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
567 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
568 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
571 updateLocal(bv, true);
576 case LFUN_EXEC_COMMAND:
577 result = UNDISPATCHED;
580 case LFUN_BREAKPARAGRAPH:
581 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
582 //lyxerr << "LFUN ignored\n";
586 // interpret this as if a backslash was typed
587 bv->lockedInsetStoreUndo(Undo::EDIT);
588 mathcursor->interpret("\\");
589 updateLocal(bv, true);
593 case LFUN_INSERT_MATH:
594 case LFUN_SELFINSERT:
596 bv->lockedInsetStoreUndo(Undo::EDIT);
597 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
598 updateLocal(bv, true);
602 case LFUN_MATH_PANEL:
603 result = UNDISPATCHED;
607 result = UNDISPATCHED;
610 lyx::Assert(mathcursor);
611 //mathcursor->normalize();
613 if (//was_macro != mathcursor->inMacroMode() &&
614 action >= 0 && action != LFUN_BACKSPACE)
615 updateLocal(bv, true);
617 if (mathcursor->selection() || was_selection)
618 toggleInsetSelection(bv);
620 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
621 result == UNDISPATCHED)
624 bv->unlockInset(this);
626 return result; // original version
630 Inset::Code InsetFormulaBase::lyxCode() const
632 return Inset::MATH_CODE;
636 int InsetFormulaBase::upperY() const
638 return yo_ - ascent(view_, font_);
642 int InsetFormulaBase::lowerY() const
644 return yo_ + descent(view_, font_);
648 /////////////////////////////////////////////////////////////////////
651 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
653 if (bv->available()) {
654 // use selection if available..
656 //if (action == LFUN_MATH_IMPORT_SELECTION)
660 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
662 InsetFormulaBase * f;
664 f = new InsetFormula;
665 if (openNewInset(bv, f)) {
666 // don't do that also for LFUN_MATH_MODE unless you want end up with
667 // always changing to mathrm when opening an inlined inset
668 // -- I really hate "LyXfunc overloading"...
670 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
671 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
674 // create a macro if we see "\\newcommand" somewhere, and an ordinary
676 if (sel.find("\\newcommand") == string::npos)
677 f = new InsetFormula(sel);
679 f = new InsetFormulaMacro(sel);
680 bv->getLyXText()->cutSelection(bv);
684 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
688 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
690 mathDispatchCreation(bv, arg, true);
694 void mathDispatchMathMode(BufferView * bv, string const & arg)
696 mathDispatchCreation(bv, arg, false);
700 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
702 mathDispatchCreation(bv, arg, true);
706 void mathDispatchMathMacro(BufferView * bv, string const & arg)
708 if (bv->available()) {
710 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
713 string const s1 = token(s, ' ', 1);
714 int const na = s1.empty() ? 0 : lyx::atoi(s1);
715 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
721 void mathDispatchMathDelim(BufferView * bv, string const & arg)
723 if (bv->available()) {
724 if (openNewInset(bv, new InsetFormula))
725 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
730 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
732 if (bv->available()) {
733 if (openNewInset(bv, new InsetFormula))
734 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
739 void mathDispatchInsertMath(BufferView * bv, string const & arg)
741 if (bv->available()) {
742 if (arg.size() && arg[0] == '\\') {
743 InsetFormula * f = new InsetFormula(arg);
744 if (!bv->insertInset(f))
747 mathDispatchMathMode(bv, arg);
752 void mathDispatchGreek(BufferView * bv, string const & arg)
754 if (bv->available()) {
755 InsetFormula * f = new InsetFormula;
756 if (openNewInset(bv, f)) {
757 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);