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())
214 mathcursor->getPos(x, y);
217 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
218 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
219 //bv->fitLockedInsetCursor(x, y, asc, des);
221 //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
223 toggleInsetCursor(bv);
227 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
229 if (isCursorVisible())
230 toggleInsetCursor(bv);
234 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
237 bv->updateInset(this, false);
241 vector<string> const InsetFormulaBase::getLabelList() const
243 return std::vector<string>();
247 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
250 bv->updateInset(this, dirty);
254 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
255 int /*x*/, int /*y*/, int /*button*/)
259 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
262 bv->updateInset(this, false);
266 void InsetFormulaBase::insetButtonPress(BufferView * bv,
267 int x, int y, int button)
269 // hack to cope with mouseclick that comes before the call to edit()
273 hack_button = button;
277 //lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
278 // << " but: " << button << "\n";
286 mathcursor->selClear();
287 mathcursor->setPos(x + xo_, y + yo_);
292 lyxerr << "insetButtonPress: 2\n";
295 bv->lockedInsetStoreUndo(Undo::EDIT);
297 mathcursor->selGet(ar);
298 mathcursor->setPos(x + xo_, y + yo_);
300 bv->getLyXText()->selectionAsString(bv->buffer(), false);
301 mathed_parse_cell(ar, sel);
302 mathcursor->insert(ar);
307 bv->updateInset(this, false);
311 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
312 int x, int y, int /*button*/)
317 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
318 //lyxerr << "insetMotionNotify: ignored\n";
324 if (!mathcursor->selection())
325 mathcursor->selStart();
327 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
328 // << ' ' << button << "\n";
330 mathcursor->setPos(x + xo_, y + yo_);
332 bv->updateInset(this, false);
336 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
338 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
342 UpdatableInset::RESULT
343 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
346 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
347 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
352 if (mathcursor->asHyperActiveInset()) {
353 lyxerr << " uurr.... getting dificult now\n";
354 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
357 RESULT result = DISPATCHED;
359 bool was_macro = mathcursor->inMacroMode();
360 bool was_selection = mathcursor->selection();
364 mathcursor->normalize();
368 // --- Cursor Movements ---------------------------------------------
371 sel = true; // fall through...
374 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
375 updateLocal(bv, false);
380 sel = true; // fall through
383 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
384 updateLocal(bv, false);
392 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
393 updateLocal(bv, false);
401 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
402 updateLocal(bv, false);
409 mathcursor->home(sel);
410 updateLocal(bv, false);
417 mathcursor->end(sel);
418 updateLocal(bv, false);
421 case LFUN_DELETE_LINE_FORWARD:
422 bv->lockedInsetStoreUndo(Undo::DELETE);
423 mathcursor->delLine();
424 updateLocal(bv, true);
428 mathcursor->idxNext();
429 updateLocal(bv, false);
433 mathcursor->idxPrev();
434 updateLocal(bv, false);
438 bv->lockedInsetStoreUndo(Undo::EDIT);
439 mathcursor->splitCell();
440 updateLocal(bv, true);
443 case LFUN_DELETE_WORD_BACKWARD:
445 bv->lockedInsetStoreUndo(Undo::DELETE);
446 mathcursor->backspace();
447 bv->updateInset(this, true);
450 case LFUN_DELETE_WORD_FORWARD:
452 bv->lockedInsetStoreUndo(Undo::DELETE);
454 bv->updateInset(this, true);
458 // sprintf(dispatch_buffer, "%d %d",);
459 // dispatch_result = dispatch_buffer;
462 lyxerr << "LFUN_SETXY broken!\n";
465 istringstream is(arg.c_str());
467 mathcursor->setPos(x, y);
468 updateLocal(bv, false);
474 mathcursor->macroModeClose();
475 bv->lockedInsetStoreUndo(Undo::EDIT);
476 mathcursor->selPaste();
477 updateLocal(bv, true);
481 bv->lockedInsetStoreUndo(Undo::DELETE);
482 mathcursor->selCut();
483 updateLocal(bv, true);
487 mathcursor->selCopy();
490 case LFUN_WORDRIGHTSEL:
491 case LFUN_WORDLEFTSEL:
494 // --- accented characters ------------------------------
496 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
497 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
498 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
499 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
500 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
501 case LFUN_MACRON: handleAccent(bv, "bar"); break;
502 case LFUN_DOT: handleAccent(bv, "dot"); break;
503 case LFUN_CARON: handleAccent(bv, "check"); break;
504 case LFUN_BREVE: handleAccent(bv, "breve"); break;
505 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
508 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
509 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
510 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
511 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
512 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
513 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
514 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
515 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
518 handleFont(bv, LM_TC_GREEK1);
520 mathcursor->interpret(arg);
524 handleFont(bv, LM_TC_TEXTRM);
525 //bv->owner()->message(_("math text mode toggled"));
528 case LFUN_MATH_LIMITS:
529 bv->lockedInsetStoreUndo(Undo::EDIT);
530 if (mathcursor->toggleLimits())
531 updateLocal(bv, true);
537 bv->lockedInsetStoreUndo(Undo::EDIT);
538 mathcursor->setSize(arg);
539 updateLocal(bv, true);
544 case LFUN_INSERT_MATRIX:
546 bv->lockedInsetStoreUndo(Undo::EDIT);
547 mathcursor->interpret("matrix " + arg);
548 updateLocal(bv, true);
552 case LFUN_MATH_SPACE:
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
556 updateLocal(bv, true);
560 case LFUN_MATH_DELIM:
562 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
565 istringstream is(arg.c_str());
568 lyxerr << "can't parse delimeters from '" << arg << "'\n";
571 bv->lockedInsetStoreUndo(Undo::EDIT);
572 mathcursor->handleDelim(ls, rs);
573 updateLocal(bv, true);
577 case LFUN_PROTECTEDSPACE:
578 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
579 bv->lockedInsetStoreUndo(Undo::EDIT);
580 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
581 updateLocal(bv, true);
585 bv->owner()->message(_("Invalid action in math mode!"));
589 case LFUN_MATH_HALIGN:
590 case LFUN_MATH_VALIGN:
591 case LFUN_MATH_ROW_INSERT:
592 case LFUN_MATH_ROW_DELETE:
593 case LFUN_MATH_COLUMN_INSERT:
594 case LFUN_MATH_COLUMN_DELETE:
596 MathInset::idx_type idx = 0;
597 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
599 bv->lockedInsetStoreUndo(Undo::EDIT);
600 char al = arg.size() ? arg[0] : 'c';
602 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
603 case LFUN_MATH_VALIGN: p->valign(al); break;
604 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
605 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
606 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
607 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
610 updateLocal(bv, true);
615 case LFUN_EXEC_COMMAND:
616 result = UNDISPATCHED;
619 case LFUN_BREAKPARAGRAPH:
620 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
621 //lyxerr << "LFUN ignored\n";
625 // interpret this as if a backslash was typed
626 bv->lockedInsetStoreUndo(Undo::EDIT);
627 mathcursor->interpret("\\");
628 updateLocal(bv, true);
632 case LFUN_INSERT_MATH:
633 case LFUN_SELFINSERT:
635 bv->lockedInsetStoreUndo(Undo::EDIT);
636 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
637 updateLocal(bv, true);
641 case LFUN_MATH_PANEL:
642 result = UNDISPATCHED;
646 result = UNDISPATCHED;
649 mathcursor->normalize();
651 lyx::Assert(mathcursor);
653 if (mathcursor->selection() || was_selection)
654 toggleInsetSelection(bv);
656 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
657 result == UNDISPATCHED)
660 bv->unlockInset(this);
662 return result; // original version
666 Inset::Code InsetFormulaBase::lyxCode() const
668 return Inset::MATH_CODE;
672 int InsetFormulaBase::ylow() const
674 return yo_ - ascent(view_, font_);
678 int InsetFormulaBase::yhigh() const
680 return yo_ + descent(view_, font_);
684 int InsetFormulaBase::xlow() const
690 int InsetFormulaBase::xhigh() const
692 return xo_ + width(view_, font_);
696 /////////////////////////////////////////////////////////////////////
699 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
701 if (bv->available()) {
702 // use selection if available..
704 //if (action == LFUN_MATH_IMPORT_SELECTION)
708 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
710 InsetFormulaBase * f;
712 f = new InsetFormula;
713 if (openNewInset(bv, f)) {
714 // don't do that also for LFUN_MATH_MODE unless you want end up with
715 // always changing to mathrm when opening an inlined inset
716 // -- I really hate "LyXfunc overloading"...
718 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
719 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
722 // create a macro if we see "\\newcommand" somewhere, and an ordinary
724 if (sel.find("\\newcommand") == string::npos)
725 f = new InsetFormula(sel);
727 f = new InsetFormulaMacro(sel);
728 bv->getLyXText()->cutSelection(bv);
732 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
736 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
738 mathDispatchCreation(bv, arg, true);
742 void mathDispatchMathMode(BufferView * bv, string const & arg)
744 mathDispatchCreation(bv, arg, false);
748 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
750 mathDispatchCreation(bv, arg, true);
754 void mathDispatchMathMacro(BufferView * bv, string const & arg)
756 if (bv->available()) {
758 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
761 string const s1 = token(s, ' ', 1);
762 int const na = s1.empty() ? 0 : lyx::atoi(s1);
763 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
769 void mathDispatchMathDelim(BufferView * bv, string const & arg)
771 if (bv->available()) {
772 if (openNewInset(bv, new InsetFormula))
773 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
778 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
780 if (bv->available()) {
781 if (openNewInset(bv, new InsetFormula))
782 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
787 void mathDispatchInsertMath(BufferView * bv, string const & arg)
789 if (bv->available()) {
790 if (arg.size() && arg[0] == '\\') {
791 InsetFormula * f = new InsetFormula(arg);
792 if (!bv->insertInset(f))
795 mathDispatchMathMode(bv, arg);
800 void mathDispatchGreek(BufferView * bv, string const & arg)
802 if (bv->available()) {
803 InsetFormula * f = new InsetFormula;
804 if (openNewInset(bv, f)) {
805 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
812 void mathDispatch(BufferView *, kb_action, string const &)