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);
197 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
198 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
199 bv->showLockedInsetCursor(x, y, asc, des);
200 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
203 toggleCursorVisible();
207 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
209 if (isCursorVisible())
215 mathcursor->getPos(x, y);
219 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
220 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
221 bv->fitLockedInsetCursor(x, y, asc, des);
222 //lyxerr << "showInsetCursor: x: " << x << " y: " << 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_SUPERSCRIPT:
564 bv->lockedInsetStoreUndo(Undo::EDIT);
565 mathcursor->script((action == LFUN_SUPERSCRIPT));
566 updateLocal(bv, true);
570 case LFUN_MATH_DELIM:
572 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
575 istringstream is(arg.c_str());
578 lyxerr << "can't parse delimeters from '" << arg << "'\n";
581 bv->lockedInsetStoreUndo(Undo::EDIT);
582 mathcursor->handleDelim(ls, rs);
583 updateLocal(bv, true);
587 case LFUN_PROTECTEDSPACE:
588 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
589 bv->lockedInsetStoreUndo(Undo::EDIT);
590 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
591 updateLocal(bv, true);
595 bv->owner()->message(_("Invalid action in math mode!"));
599 case LFUN_MATH_HALIGN:
600 case LFUN_MATH_VALIGN:
601 case LFUN_MATH_ROW_INSERT:
602 case LFUN_MATH_ROW_DELETE:
603 case LFUN_MATH_COLUMN_INSERT:
604 case LFUN_MATH_COLUMN_DELETE:
606 MathInset::idx_type idx = 0;
607 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
609 bv->lockedInsetStoreUndo(Undo::EDIT);
610 char al = arg.size() ? arg[0] : 'c';
612 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
613 case LFUN_MATH_VALIGN: p->valign(al); break;
614 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
615 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
616 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
617 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
620 updateLocal(bv, true);
625 case LFUN_EXEC_COMMAND:
626 result = UNDISPATCHED;
629 case LFUN_BREAKPARAGRAPH:
630 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
631 //lyxerr << "LFUN ignored\n";
635 // interpret this as if a backslash was typed
636 bv->lockedInsetStoreUndo(Undo::EDIT);
637 mathcursor->interpret("\\");
638 updateLocal(bv, true);
642 case LFUN_INSERT_MATH:
643 case LFUN_SELFINSERT:
645 bv->lockedInsetStoreUndo(Undo::EDIT);
646 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
647 updateLocal(bv, true);
651 case LFUN_MATH_PANEL:
652 result = UNDISPATCHED;
656 if (mathcursor->selection())
657 mathcursor->selClear();
659 result = UNDISPATCHED;
663 result = UNDISPATCHED;
666 mathcursor->normalize();
668 lyx::Assert(mathcursor);
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::ylow() const
691 return yo_ - ascent(view_, font_);
695 int InsetFormulaBase::yhigh() const
697 return yo_ + descent(view_, font_);
701 int InsetFormulaBase::xlow() const
707 int InsetFormulaBase::xhigh() const
709 return xo_ + width(view_, font_);
713 /////////////////////////////////////////////////////////////////////
716 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
718 if (bv->available()) {
719 // use selection if available..
721 //if (action == LFUN_MATH_IMPORT_SELECTION)
725 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
727 InsetFormulaBase * f;
729 f = new InsetFormula;
730 if (openNewInset(bv, f)) {
731 // don't do that also for LFUN_MATH_MODE unless you want end up with
732 // always changing to mathrm when opening an inlined inset
733 // -- I really hate "LyXfunc overloading"...
735 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
736 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
739 // create a macro if we see "\\newcommand" somewhere, and an ordinary
741 if (sel.find("\\newcommand") == string::npos)
742 f = new InsetFormula(sel);
744 f = new InsetFormulaMacro(sel);
745 bv->getLyXText()->cutSelection(bv);
749 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
753 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
755 mathDispatchCreation(bv, arg, true);
759 void mathDispatchMathMode(BufferView * bv, string const & arg)
761 mathDispatchCreation(bv, arg, false);
765 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
767 mathDispatchCreation(bv, arg, true);
771 void mathDispatchMathMacro(BufferView * bv, string const & arg)
773 if (bv->available()) {
775 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
778 string const s1 = token(s, ' ', 1);
779 int const na = s1.empty() ? 0 : lyx::atoi(s1);
780 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
786 void mathDispatchMathDelim(BufferView * bv, string const & arg)
788 if (bv->available()) {
789 if (openNewInset(bv, new InsetFormula))
790 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
795 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
797 if (bv->available()) {
798 if (openNewInset(bv, new InsetFormula))
799 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
804 void mathDispatchInsertMath(BufferView * bv, string const & arg)
806 if (bv->available()) {
807 if (arg.size() && arg[0] == '\\') {
808 InsetFormula * f = new InsetFormula(arg);
809 if (!bv->insertInset(f))
812 mathDispatchMathMode(bv, arg);
817 void mathDispatchGreek(BufferView * bv, string const & arg)
819 if (bv->available()) {
820 InsetFormula * f = new InsetFormula;
821 if (openNewInset(bv, f)) {
822 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
829 void mathDispatch(BufferView *, kb_action, string const &)