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_cursor.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "math_macrotable.h"
43 #include "math_factory.h"
44 #include "math_parser.h"
45 #include "undo_funcs.h"
51 MathCursor * mathcursor = 0;
65 void handleFont(BufferView * bv, MathTextCodes t)
67 if (mathcursor->selection())
68 bv->lockedInsetStoreUndo(Undo::EDIT);
69 mathcursor->handleFont(t);
73 void handleAccent(BufferView * bv, string const & name)
75 bv->lockedInsetStoreUndo(Undo::EDIT);
76 mathcursor->insert(createMathInset(name));
80 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
82 if (!bv->insertInset(new_inset)) {
86 new_inset->edit(bv, 0, 0, 0);
95 InsetFormulaBase::InsetFormulaBase()
96 : view_(0), font_(), xo_(0), yo_(0)
98 // This is needed as long the math parser is not re-entrant
99 MathMacroTable::builtinMacros();
100 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
104 // Check if uses AMS macros
105 void InsetFormulaBase::validate(LaTeXFeatures &) const
109 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
115 void InsetFormulaBase::metrics(BufferView * bv) const
119 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
124 string const InsetFormulaBase::editMessage() const
126 return _("Math editor mode");
130 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
132 if (!bv->lockInset(this))
133 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
135 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
137 mathcursor = new MathCursor(this, x == 0);
139 // handle ignored click
140 if (hack_x || hack_y) {
141 insetButtonPress(bv, hack_x, hack_y, hack_button);
146 // if that is removed, we won't get the magenta box when entering an
147 // inset for the first time
148 bv->updateInset(this, false);
152 void InsetFormulaBase::edit(BufferView * bv, bool front)
154 // looks hackish but seems to work
155 edit(bv, front ? 0 : 1, 0, 0);
159 void InsetFormulaBase::insetUnlock(BufferView * bv)
162 if (mathcursor->inMacroMode()) {
163 mathcursor->macroModeClose();
164 updateLocal(bv, true);
169 bv->updateInset(this, false);
173 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
175 mathcursor->getPos(x, y);
178 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
182 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
187 if (isCursorVisible())
188 bv->hideLockedInsetCursor();
193 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())
215 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);
222 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
224 toggleInsetCursor(bv);
228 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
230 if (isCursorVisible())
231 toggleInsetCursor(bv);
235 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
238 bv->updateInset(this, false);
242 vector<string> const InsetFormulaBase::getLabelList() const
244 return std::vector<string>();
248 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
251 bv->updateInset(this, dirty);
255 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
256 int /*x*/, int /*y*/, int /*button*/)
260 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
263 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();
369 // --- Cursor Movements ---------------------------------------------
372 sel = true; // fall through...
375 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
376 updateLocal(bv, false);
381 sel = true; // fall through
384 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
385 updateLocal(bv, false);
393 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
394 updateLocal(bv, false);
402 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
403 updateLocal(bv, false);
410 mathcursor->home(sel);
411 updateLocal(bv, false);
418 mathcursor->end(sel);
419 updateLocal(bv, false);
422 case LFUN_DELETE_LINE_FORWARD:
423 bv->lockedInsetStoreUndo(Undo::DELETE);
424 mathcursor->delLine();
425 updateLocal(bv, true);
429 mathcursor->idxNext();
430 updateLocal(bv, false);
434 mathcursor->idxPrev();
435 updateLocal(bv, false);
439 bv->lockedInsetStoreUndo(Undo::EDIT);
440 mathcursor->splitCell();
441 updateLocal(bv, true);
444 case LFUN_DELETE_WORD_BACKWARD:
446 bv->lockedInsetStoreUndo(Undo::DELETE);
447 mathcursor->backspace();
448 bv->updateInset(this, true);
451 case LFUN_DELETE_WORD_FORWARD:
453 bv->lockedInsetStoreUndo(Undo::DELETE);
455 bv->updateInset(this, true);
459 // sprintf(dispatch_buffer, "%d %d",);
460 // dispatch_result = dispatch_buffer;
463 lyxerr << "LFUN_SETXY broken!\n";
466 istringstream is(arg.c_str());
468 mathcursor->setPos(x, y);
469 updateLocal(bv, false);
475 mathcursor->macroModeClose();
476 bv->lockedInsetStoreUndo(Undo::EDIT);
477 mathcursor->selPaste();
478 updateLocal(bv, true);
482 bv->lockedInsetStoreUndo(Undo::DELETE);
483 mathcursor->selCut();
484 updateLocal(bv, true);
488 mathcursor->selCopy();
491 case LFUN_WORDRIGHTSEL:
492 case LFUN_WORDLEFTSEL:
495 // --- accented characters ------------------------------
497 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
498 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
499 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
500 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
501 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
502 case LFUN_MACRON: handleAccent(bv, "bar"); break;
503 case LFUN_DOT: handleAccent(bv, "dot"); break;
504 case LFUN_CARON: handleAccent(bv, "check"); break;
505 case LFUN_BREVE: handleAccent(bv, "breve"); break;
506 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
509 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
510 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
511 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
512 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
513 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
514 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
515 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
516 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
519 handleFont(bv, LM_TC_GREEK1);
521 mathcursor->interpret(arg);
525 handleFont(bv, LM_TC_TEXTRM);
526 //bv->owner()->message(_("math text mode toggled"));
529 case LFUN_MATH_LIMITS:
530 bv->lockedInsetStoreUndo(Undo::EDIT);
531 if (mathcursor->toggleLimits())
532 updateLocal(bv, true);
538 bv->lockedInsetStoreUndo(Undo::EDIT);
539 mathcursor->setSize(arg);
540 updateLocal(bv, true);
545 case LFUN_INSERT_MATRIX:
547 bv->lockedInsetStoreUndo(Undo::EDIT);
548 mathcursor->interpret("matrix " + arg);
549 updateLocal(bv, true);
553 case LFUN_MATH_SPACE:
555 bv->lockedInsetStoreUndo(Undo::EDIT);
556 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
557 updateLocal(bv, true);
561 case LFUN_MATH_DELIM:
563 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
566 istringstream is(arg.c_str());
569 lyxerr << "can't parse delimeters from '" << arg << "'\n";
572 bv->lockedInsetStoreUndo(Undo::EDIT);
573 mathcursor->handleDelim(ls, rs);
574 updateLocal(bv, true);
578 case LFUN_PROTECTEDSPACE:
579 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
580 bv->lockedInsetStoreUndo(Undo::EDIT);
581 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
582 updateLocal(bv, true);
586 bv->owner()->message(_("Invalid action in math mode!"));
590 case LFUN_MATH_HALIGN:
591 case LFUN_MATH_VALIGN:
592 case LFUN_MATH_ROW_INSERT:
593 case LFUN_MATH_ROW_DELETE:
594 case LFUN_MATH_COLUMN_INSERT:
595 case LFUN_MATH_COLUMN_DELETE:
597 MathInset::idx_type idx = 0;
598 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
600 bv->lockedInsetStoreUndo(Undo::EDIT);
601 char al = arg.size() ? arg[0] : 'c';
603 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
604 case LFUN_MATH_VALIGN: p->valign(al); break;
605 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
606 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
607 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
608 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
611 updateLocal(bv, true);
616 case LFUN_EXEC_COMMAND:
617 result = UNDISPATCHED;
620 case LFUN_BREAKPARAGRAPH:
621 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
622 //lyxerr << "LFUN ignored\n";
626 // interpret this as if a backslash was typed
627 bv->lockedInsetStoreUndo(Undo::EDIT);
628 mathcursor->interpret("\\");
629 updateLocal(bv, true);
633 case LFUN_INSERT_MATH:
634 case LFUN_SELFINSERT:
636 bv->lockedInsetStoreUndo(Undo::EDIT);
637 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
638 updateLocal(bv, true);
642 case LFUN_MATH_PANEL:
643 result = UNDISPATCHED;
647 result = UNDISPATCHED;
650 mathcursor->normalize();
652 lyx::Assert(mathcursor);
654 if (mathcursor->selection() || was_selection)
655 toggleInsetSelection(bv);
657 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
658 result == UNDISPATCHED)
661 bv->unlockInset(this);
663 return result; // original version
667 Inset::Code InsetFormulaBase::lyxCode() const
669 return Inset::MATH_CODE;
673 int InsetFormulaBase::upperY() const
675 return yo_ - ascent(view_, font_);
679 int InsetFormulaBase::lowerY() const
681 return yo_ + descent(view_, font_);
685 /////////////////////////////////////////////////////////////////////
688 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
690 if (bv->available()) {
691 // use selection if available..
693 //if (action == LFUN_MATH_IMPORT_SELECTION)
697 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
699 InsetFormulaBase * f;
701 f = new InsetFormula;
702 if (openNewInset(bv, f)) {
703 // don't do that also for LFUN_MATH_MODE unless you want end up with
704 // always changing to mathrm when opening an inlined inset
705 // -- I really hate "LyXfunc overloading"...
707 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
708 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
711 // create a macro if we see "\\newcommand" somewhere, and an ordinary
713 if (sel.find("\\newcommand") == string::npos)
714 f = new InsetFormula(sel);
716 f = new InsetFormulaMacro(sel);
717 bv->getLyXText()->cutSelection(bv);
721 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
725 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
727 mathDispatchCreation(bv, arg, true);
731 void mathDispatchMathMode(BufferView * bv, string const & arg)
733 mathDispatchCreation(bv, arg, false);
737 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
739 mathDispatchCreation(bv, arg, true);
743 void mathDispatchMathMacro(BufferView * bv, string const & arg)
745 if (bv->available()) {
747 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
750 string const s1 = token(s, ' ', 1);
751 int const na = s1.empty() ? 0 : lyx::atoi(s1);
752 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
758 void mathDispatchMathDelim(BufferView * bv, string const & arg)
760 if (bv->available()) {
761 if (openNewInset(bv, new InsetFormula))
762 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
767 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
769 if (bv->available()) {
770 if (openNewInset(bv, new InsetFormula))
771 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
776 void mathDispatchInsertMath(BufferView * bv, string const & arg)
778 if (bv->available()) {
779 if (arg.size() && arg[0] == '\\') {
780 InsetFormula * f = new InsetFormula(arg);
781 if (!bv->insertInset(f))
784 mathDispatchMathMode(bv, arg);
789 void mathDispatchGreek(BufferView * bv, string const & arg)
791 if (bv->available()) {
792 InsetFormula * f = new InsetFormula;
793 if (openNewInset(bv, f)) {
794 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
801 void mathDispatch(BufferView *, kb_action, string const &)