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_macrotable.h"
44 #include "math_parser.h"
45 #include "math_spaceinset.h"
46 #include "undo_funcs.h"
52 MathCursor * mathcursor = 0;
66 void handleFont(BufferView * bv, MathTextCodes t)
68 if (mathcursor->selection())
69 bv->lockedInsetStoreUndo(Undo::EDIT);
70 mathcursor->handleFont(t);
74 void handleAccent(BufferView * bv, string const & name)
76 bv->lockedInsetStoreUndo(Undo::EDIT);
77 mathcursor->insert(createMathInset(name));
81 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
83 if (!bv->insertInset(new_inset)) {
87 new_inset->edit(bv, 0, 0, 0);
96 InsetFormulaBase::InsetFormulaBase()
97 : view_(0), font_(), xo_(0), yo_(0)
99 // This is needed as long the math parser is not re-entrant
100 MathMacroTable::builtinMacros();
101 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
105 // Check if uses AMS macros
106 void InsetFormulaBase::validate(LaTeXFeatures &) const
110 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
116 void InsetFormulaBase::metrics(BufferView * bv) const
120 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
125 string const InsetFormulaBase::editMessage() const
127 return _("Math editor mode");
131 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
133 if (!bv->lockInset(this))
134 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
136 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
138 mathcursor = new MathCursor(this, x == 0);
140 // handle ignored click
141 if (hack_x || hack_y) {
142 insetButtonPress(bv, hack_x, hack_y, hack_button);
147 // if that is removed, we won't get the magenta box when entering an
148 // inset for the first time
149 bv->updateInset(this, false);
153 void InsetFormulaBase::edit(BufferView * bv, bool front)
155 // looks hackish but seems to work
156 edit(bv, front ? 0 : 1, 0, 0);
160 void InsetFormulaBase::insetUnlock(BufferView * bv)
163 if (mathcursor->inMacroMode()) {
164 mathcursor->macroModeClose();
165 updateLocal(bv, true);
170 bv->updateInset(this, false);
174 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 mathcursor->getPos(x, y);
179 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
183 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
188 if (isCursorVisible())
189 bv->hideLockedInsetCursor();
194 mathcursor->getPos(x, y);
198 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
199 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
200 bv->showLockedInsetCursor(x, y, asc, des);
201 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
204 toggleCursorVisible();
208 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
210 if (isCursorVisible())
216 mathcursor->getPos(x, y);
220 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
221 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
222 bv->fitLockedInsetCursor(x, y, asc, des);
223 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << "\n";
225 toggleInsetCursor(bv);
229 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
231 if (isCursorVisible())
232 toggleInsetCursor(bv);
236 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
239 bv->updateInset(this, false);
243 vector<string> const InsetFormulaBase::getLabelList() const
245 return std::vector<string>();
249 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
252 bv->updateInset(this, dirty);
256 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
257 int /*x*/, int /*y*/, int /*button*/)
261 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
264 bv->updateInset(this, false);
269 void InsetFormulaBase::insetButtonPress(BufferView * bv,
270 int x, int y, int button)
272 // hack to cope with mouseclick that comes before the call to edit()
276 hack_button = button;
280 //lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
281 // << " but: " << button << "\n";
289 mathcursor->selClear();
290 mathcursor->setPos(x + xo_, y + yo_);
295 lyxerr << "insetButtonPress: 2\n";
298 bv->lockedInsetStoreUndo(Undo::EDIT);
300 mathcursor->selGet(ar);
301 mathcursor->setPos(x + xo_, y + yo_);
303 bv->getLyXText()->selectionAsString(bv->buffer(), false);
304 mathed_parse_cell(ar, sel);
305 mathcursor->insert(ar);
310 bv->updateInset(this, false);
314 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
315 int x, int y, int /*button*/)
320 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
321 //lyxerr << "insetMotionNotify: ignored\n";
327 if (!mathcursor->selection())
328 mathcursor->selStart();
330 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
331 // << ' ' << button << "\n";
333 mathcursor->setPos(x + xo_, y + yo_);
335 bv->updateInset(this, false);
339 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
341 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
345 UpdatableInset::RESULT
346 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
349 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
350 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
355 if (mathcursor->asHyperActiveInset()) {
356 lyxerr << " uurr.... getting dificult now\n";
357 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
360 RESULT result = DISPATCHED;
362 bool was_macro = mathcursor->inMacroMode();
363 bool was_selection = mathcursor->selection();
367 mathcursor->normalize();
371 // --- Cursor Movements ---------------------------------------------
374 sel = true; // fall through...
377 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
378 updateLocal(bv, false);
383 sel = true; // fall through
386 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
387 updateLocal(bv, false);
395 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
396 updateLocal(bv, false);
404 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
405 updateLocal(bv, false);
412 mathcursor->home(sel);
413 updateLocal(bv, false);
420 mathcursor->end(sel);
421 updateLocal(bv, false);
424 case LFUN_DELETE_LINE_FORWARD:
425 bv->lockedInsetStoreUndo(Undo::DELETE);
426 mathcursor->delLine();
427 updateLocal(bv, true);
431 mathcursor->idxNext();
432 updateLocal(bv, false);
436 mathcursor->idxPrev();
437 updateLocal(bv, false);
441 bv->lockedInsetStoreUndo(Undo::EDIT);
442 mathcursor->splitCell();
443 updateLocal(bv, true);
446 case LFUN_DELETE_WORD_BACKWARD:
448 bv->lockedInsetStoreUndo(Undo::DELETE);
449 mathcursor->backspace();
450 bv->updateInset(this, true);
453 case LFUN_DELETE_WORD_FORWARD:
455 bv->lockedInsetStoreUndo(Undo::DELETE);
457 bv->updateInset(this, true);
461 // sprintf(dispatch_buffer, "%d %d",);
462 // dispatch_result = dispatch_buffer;
465 lyxerr << "LFUN_SETXY broken!\n";
468 istringstream is(arg.c_str());
470 mathcursor->setPos(x, y);
471 updateLocal(bv, false);
477 mathcursor->macroModeClose();
478 bv->lockedInsetStoreUndo(Undo::EDIT);
479 mathcursor->selPaste();
480 updateLocal(bv, true);
484 bv->lockedInsetStoreUndo(Undo::DELETE);
485 mathcursor->selCut();
486 updateLocal(bv, true);
490 mathcursor->selCopy();
493 case LFUN_WORDRIGHTSEL:
494 case LFUN_WORDLEFTSEL:
497 // --- accented characters ------------------------------
499 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
500 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
501 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
502 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
503 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
504 case LFUN_MACRON: handleAccent(bv, "bar"); break;
505 case LFUN_DOT: handleAccent(bv, "dot"); break;
506 case LFUN_CARON: handleAccent(bv, "check"); break;
507 case LFUN_BREVE: handleAccent(bv, "breve"); break;
508 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
511 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
512 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
513 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
514 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
515 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
516 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
517 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
518 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
521 handleFont(bv, LM_TC_GREEK1);
523 mathcursor->interpret(arg);
527 //handleFont(bv, LM_TC_TEXTRM);
528 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
529 updateLocal(bv, true);
530 //bv->owner()->message(_("math text mode toggled"));
533 case LFUN_MATH_LIMITS:
534 bv->lockedInsetStoreUndo(Undo::EDIT);
535 if (mathcursor->toggleLimits())
536 updateLocal(bv, true);
542 bv->lockedInsetStoreUndo(Undo::EDIT);
543 mathcursor->setSize(arg);
544 updateLocal(bv, true);
549 case LFUN_INSERT_MATRIX:
551 bv->lockedInsetStoreUndo(Undo::EDIT);
552 mathcursor->interpret("matrix " + arg);
553 updateLocal(bv, true);
557 case LFUN_MATH_SPACE:
559 bv->lockedInsetStoreUndo(Undo::EDIT);
560 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
561 updateLocal(bv, true);
565 case LFUN_SUPERSCRIPT:
568 bv->lockedInsetStoreUndo(Undo::EDIT);
569 mathcursor->script((action == LFUN_SUPERSCRIPT));
570 updateLocal(bv, true);
574 case LFUN_MATH_DELIM:
576 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
579 istringstream is(arg.c_str());
582 lyxerr << "can't parse delimeters from '" << arg << "'\n";
585 bv->lockedInsetStoreUndo(Undo::EDIT);
586 mathcursor->handleDelim(ls, rs);
587 updateLocal(bv, true);
591 case LFUN_PROTECTEDSPACE:
592 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
593 bv->lockedInsetStoreUndo(Undo::EDIT);
594 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
595 updateLocal(bv, true);
599 bv->owner()->message(_("Invalid action in math mode!"));
603 case LFUN_MATH_HALIGN:
604 case LFUN_MATH_VALIGN:
605 case LFUN_MATH_ROW_INSERT:
606 case LFUN_MATH_ROW_DELETE:
607 case LFUN_MATH_COLUMN_INSERT:
608 case LFUN_MATH_COLUMN_DELETE:
610 MathInset::idx_type idx = 0;
611 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
613 bv->lockedInsetStoreUndo(Undo::EDIT);
614 char al = arg.size() ? arg[0] : 'c';
616 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
617 case LFUN_MATH_VALIGN: p->valign(al); break;
618 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
619 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
620 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
621 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
624 updateLocal(bv, true);
629 case LFUN_EXEC_COMMAND:
630 result = UNDISPATCHED;
633 case LFUN_BREAKPARAGRAPH:
634 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
635 //lyxerr << "LFUN ignored\n";
639 // interpret this as if a backslash was typed
640 bv->lockedInsetStoreUndo(Undo::EDIT);
641 mathcursor->interpret("\\");
642 updateLocal(bv, true);
646 case LFUN_INSERT_MATH:
647 case LFUN_SELFINSERT:
649 bv->lockedInsetStoreUndo(Undo::EDIT);
650 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
651 updateLocal(bv, true);
655 case LFUN_MATH_PANEL:
656 result = UNDISPATCHED;
660 if (mathcursor->selection())
661 mathcursor->selClear();
663 result = UNDISPATCHED;
667 result = UNDISPATCHED;
670 mathcursor->normalize();
672 lyx::Assert(mathcursor);
674 if (mathcursor->selection() || was_selection)
675 toggleInsetSelection(bv);
677 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
678 result == UNDISPATCHED)
681 bv->unlockInset(this);
683 return result; // original version
687 Inset::Code InsetFormulaBase::lyxCode() const
689 return Inset::MATH_CODE;
693 int InsetFormulaBase::ylow() const
695 return yo_ - ascent(view_, font_);
699 int InsetFormulaBase::yhigh() const
701 return yo_ + descent(view_, font_);
705 int InsetFormulaBase::xlow() const
711 int InsetFormulaBase::xhigh() const
713 return xo_ + width(view_, font_);
717 /////////////////////////////////////////////////////////////////////
720 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
722 if (bv->available()) {
723 // use selection if available..
725 //if (action == LFUN_MATH_IMPORT_SELECTION)
729 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
731 InsetFormulaBase * f;
733 f = new InsetFormula;
734 if (openNewInset(bv, f)) {
735 // don't do that also for LFUN_MATH_MODE unless you want end up with
736 // always changing to mathrm when opening an inlined inset
737 // -- I really hate "LyXfunc overloading"...
739 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
740 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
743 // create a macro if we see "\\newcommand" somewhere, and an ordinary
745 if (sel.find("\\newcommand") == string::npos)
746 f = new InsetFormula(sel);
748 f = new InsetFormulaMacro(sel);
749 bv->getLyXText()->cutSelection(bv);
753 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
757 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
759 mathDispatchCreation(bv, arg, true);
763 void mathDispatchMathMode(BufferView * bv, string const & arg)
765 mathDispatchCreation(bv, arg, false);
769 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
771 mathDispatchCreation(bv, arg, true);
775 void mathDispatchMathMacro(BufferView * bv, string const & arg)
777 if (bv->available()) {
779 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
782 string const s1 = token(s, ' ', 1);
783 int const na = s1.empty() ? 0 : lyx::atoi(s1);
784 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
790 void mathDispatchMathDelim(BufferView * bv, string const & arg)
792 if (bv->available()) {
793 if (openNewInset(bv, new InsetFormula))
794 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
799 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
801 if (bv->available()) {
802 if (openNewInset(bv, new InsetFormula))
803 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
808 void mathDispatchInsertMath(BufferView * bv, string const & arg)
810 if (bv->available()) {
811 if (arg.size() && arg[0] == '\\') {
812 InsetFormula * f = new InsetFormula(arg);
813 if (!bv->insertInset(f))
816 mathDispatchMathMode(bv, arg);
821 void mathDispatchGreek(BufferView * bv, string const & arg)
823 if (bv->available()) {
824 InsetFormula * f = new InsetFormula;
825 if (openNewInset(bv, f)) {
826 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
833 void mathDispatch(BufferView *, kb_action, string const &)