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 "math_parser.h"
30 #include "BufferView.h"
34 #include "LaTeXFeatures.h"
37 #include "support/lstrings.h"
41 #include "math_arrayinset.h"
42 #include "math_spaceinset.h"
43 #include "math_macrotable.h"
44 #include "math_factory.h"
45 #include "support/lyxlib.h"
46 #include "undo_funcs.h"
52 extern char const * latex_mathenv[];
53 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 LyXText * lt = bv->getLyXText();
85 if (!bv->insertInset(new_inset)) {
89 new_inset->edit(bv, 0, 0, 0);
94 // returns the nearest enclosing grid
95 MathArrayInset * matrixpar(MathInset::idx_type & idx)
98 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
106 InsetFormulaBase::InsetFormulaBase()
109 // This is needed as long the math parser is not re-entrant
110 MathMacroTable::builtinMacros();
111 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
115 // Check if uses AMS macros
116 void InsetFormulaBase::validate(LaTeXFeatures &) const
120 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const * f) const
127 lyxerr << "fontsize: " << f->size() << "\n";
128 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
133 string const InsetFormulaBase::editMessage() const
135 return _("Math editor mode");
139 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
141 if (!bv->lockInset(this))
142 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
144 mathcursor = new MathCursor(this, x == 0);
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);
155 void InsetFormulaBase::edit(BufferView * bv, bool front)
157 // looks hackish but seems to work
158 edit(bv, front ? 0 : 1, 0, 0);
162 void InsetFormulaBase::insetUnlock(BufferView * bv)
165 if (mathcursor->inMacroMode()) {
166 mathcursor->macroModeClose();
167 updateLocal(bv, true);
172 bv->updateInset(this, false);
176 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
178 mathcursor->getPos(x, y);
185 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
190 if (isCursorVisible())
191 bv->hideLockedInsetCursor();
195 mathcursor->getPos(x, y);
201 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
202 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
203 bv->showLockedInsetCursor(x, y, asc, des);
206 toggleCursorVisible();
210 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
212 if (!isCursorVisible()) {
216 mathcursor->getPos(x, y);
221 MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
222 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
223 bv->fitLockedInsetCursor(x, y, asc, des);
225 toggleInsetCursor(bv);
230 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
232 if (isCursorVisible())
233 toggleInsetCursor(bv);
237 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
240 bv->updateInset(this, false);
244 vector<string> const InsetFormulaBase::getLabelList() const
246 return std::vector<string>();
250 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
253 bv->updateInset(this, dirty);
257 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
258 int x, int y, int /*button*/)
264 mathcursor->setPos(x, y);
271 bv->updateInset(this, false);
276 void InsetFormulaBase::insetButtonPress(BufferView * bv,
277 int x, int y, int /*button*/)
282 if (mathcursor && mathcursor->selection()) {
283 mathcursor->selClear();
284 bv->updateInset(this, false);
289 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
290 int x, int y, int /*button*/)
292 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
295 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
296 mathcursor->selStart();
298 mathcursor->getPos(sel_x, sel_y);
299 } else if (sel_flag) {
303 mathcursor->setPos(x, y);
305 mathcursor->getPos(x, y);
306 if (sel_x != x || sel_y != y)
307 bv->updateInset(this, false);
314 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
316 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
320 UpdatableInset::RESULT
321 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
324 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
325 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
330 RESULT result = DISPATCHED;
332 bool was_macro = mathcursor->inMacroMode();
333 bool was_selection = mathcursor->selection();
337 mathcursor->normalize();
341 // --- Cursor Movements ---------------------------------------------
344 sel = true; // fall through...
347 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
348 updateLocal(bv, false);
353 sel = true; // fall through
356 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
357 updateLocal(bv, false);
365 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
366 updateLocal(bv, false);
374 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
375 updateLocal(bv, false);
382 mathcursor->home(sel);
383 updateLocal(bv, false);
390 mathcursor->end(sel);
391 updateLocal(bv, false);
394 case LFUN_DELETE_LINE_FORWARD:
395 bv->lockedInsetStoreUndo(Undo::DELETE);
396 mathcursor->delLine();
397 updateLocal(bv, true);
401 mathcursor->idxNext();
402 updateLocal(bv, false);
406 mathcursor->idxPrev();
407 updateLocal(bv, false);
411 bv->lockedInsetStoreUndo(Undo::EDIT);
412 mathcursor->splitCell();
413 updateLocal(bv, true);
417 bv->lockedInsetStoreUndo(Undo::DELETE);
418 mathcursor->backspace();
419 bv->updateInset(this, true);
423 bv->lockedInsetStoreUndo(Undo::DELETE);
425 bv->updateInset(this, true);
429 // sprintf(dispatch_buffer, "%d %d",);
430 // dispatch_result = dispatch_buffer;
433 lyxerr << "LFUN_SETXY broken!\n";
438 istringstream is(arg.c_str());
440 par()->getXY(x1, y1);
441 mathcursor->setPos(x1 + x, y1 + y);
442 updateLocal(bv, false);
448 mathcursor->macroModeClose();
449 bv->lockedInsetStoreUndo(Undo::INSERT);
450 mathcursor->selPaste();
451 updateLocal(bv, true);
455 bv->lockedInsetStoreUndo(Undo::DELETE);
456 mathcursor->selCut();
457 updateLocal(bv, true);
461 mathcursor->selCopy();
464 case LFUN_WORDRIGHTSEL:
465 case LFUN_WORDLEFTSEL:
468 // --- accented characters ------------------------------
470 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
471 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
472 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
473 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
474 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
475 case LFUN_MACRON: handleAccent(bv, "bar"); break;
476 case LFUN_DOT: handleAccent(bv, "dot"); break;
477 case LFUN_CARON: handleAccent(bv, "check"); break;
478 case LFUN_BREVE: handleAccent(bv, "breve"); break;
479 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
482 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
483 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
484 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
485 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
486 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
487 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
488 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
489 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
492 handleFont(bv, LM_TC_GREEK1);
494 mathcursor->interpret(arg[0]);
498 handleFont(bv, LM_TC_TEXTRM);
499 //bv->owner()->message(_("math text mode toggled"));
502 case LFUN_MATH_LIMITS:
503 bv->lockedInsetStoreUndo(Undo::INSERT);
504 if (mathcursor->toggleLimits())
505 updateLocal(bv, true);
510 bv->lockedInsetStoreUndo(Undo::INSERT);
511 latexkeys const * l = in_word_set(arg);
512 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
513 updateLocal(bv, true);
517 case LFUN_INSERT_MATRIX:
519 bv->lockedInsetStoreUndo(Undo::INSERT);
520 mathcursor->interpret("matrix " + arg);
521 updateLocal(bv, true);
525 case LFUN_MATH_SPACE:
527 bv->lockedInsetStoreUndo(Undo::EDIT);
528 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
529 updateLocal(bv, true);
533 case LFUN_MATH_DELIM:
535 bv->lockedInsetStoreUndo(Undo::INSERT);
536 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
539 istringstream is(arg.c_str());
542 lyxerr << "can't parse delimeters from '" << arg << "'\n";
545 bv->lockedInsetStoreUndo(Undo::EDIT);
546 mathcursor->handleDelim(ls, rs);
547 updateLocal(bv, true);
551 case LFUN_PROTECTEDSPACE:
552 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
553 bv->lockedInsetStoreUndo(Undo::INSERT);
554 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
555 updateLocal(bv, true);
559 bv->owner()->message(_("Invalid action in math mode!"));
563 case LFUN_MATH_HALIGN:
565 bv->lockedInsetStoreUndo(Undo::INSERT);
566 lyxerr << "handling halign '" << arg << "'\n";
567 MathInset::idx_type idx;
568 MathArrayInset * p = matrixpar(idx);
571 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
572 updateLocal(bv, true);
576 case LFUN_MATH_VALIGN:
578 bv->lockedInsetStoreUndo(Undo::INSERT);
579 lyxerr << "handling valign '" << arg << "'\n";
580 MathInset::idx_type idx;
581 MathArrayInset * p = matrixpar(idx);
584 p->valign(arg.size() ? arg[0] : 'c');
585 updateLocal(bv, true);
589 case LFUN_MATH_ROW_INSERT:
591 bv->lockedInsetStoreUndo(Undo::INSERT);
592 MathInset::idx_type idx;
593 MathArrayInset * p = matrixpar(idx);
594 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
597 p->addRow(p->row(idx));
598 updateLocal(bv, true);
602 case LFUN_MATH_ROW_DELETE:
604 bv->lockedInsetStoreUndo(Undo::INSERT);
605 MathInset::idx_type idx;
606 MathArrayInset * p = matrixpar(idx);
607 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
610 p->delRow(p->row(idx));
611 updateLocal(bv, true);
615 case LFUN_MATH_COLUMN_INSERT:
617 bv->lockedInsetStoreUndo(Undo::INSERT);
618 MathInset::idx_type idx;
619 MathArrayInset * p = matrixpar(idx);
622 p->addCol(p->col(idx));
623 updateLocal(bv, true);
627 case LFUN_MATH_COLUMN_DELETE:
629 bv->lockedInsetStoreUndo(Undo::INSERT);
630 MathInset::idx_type idx;
631 MathArrayInset * p = matrixpar(idx);
634 p->delCol(p->col(idx));
635 updateLocal(bv, true);
639 case LFUN_EXEC_COMMAND:
640 result = UNDISPATCHED;
644 case LFUN_INSERT_MATH:
645 case LFUN_SELFINSERT:
647 bv->lockedInsetStoreUndo(Undo::INSERT);
648 mathcursor->interpret(arg);
649 updateLocal(bv, true);
653 case LFUN_MATH_PANEL:
654 result = UNDISPATCHED;
658 result = UNDISPATCHED;
661 lyx::Assert(mathcursor);
662 //mathcursor->normalize();
664 if (//was_macro != mathcursor->inMacroMode() &&
665 action >= 0 && action != LFUN_BACKSPACE)
666 updateLocal(bv, true);
668 if (mathcursor->selection() || was_selection)
669 toggleInsetSelection(bv);
671 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
672 result == UNDISPATCHED)
675 bv->unlockInset(this);
677 return result; // original version
681 Inset::Code InsetFormulaBase::lyxCode() const
683 return Inset::MATH_CODE;
687 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
689 if (bv->available()) {
690 // use selection if available..
692 //if (action == LFUN_MATH_IMPORT_SELECTION)
696 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
698 InsetFormulaBase * f;
700 f = new InsetFormula;
701 if (openNewInset(bv, f)) {
702 // don't do that also for LFUN_MATH_MODE unless you want end up with
703 // always changing to mathrm when opening an inlined inset
704 // -- I really hate "LyXfunc overloading"...
706 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
707 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
710 // create a macro if we see "\\newcommand" somewhere, and an ordinary
712 if (sel.find("\\newcommand") == string::npos)
713 f = new InsetFormula(sel);
715 f = new InsetFormulaMacro(sel);
716 bv->getLyXText()->cutSelection(bv);
720 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
724 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
726 mathDispatchCreation(bv, arg, true);
730 void mathDispatchMathMode(BufferView * bv, string const & arg)
732 mathDispatchCreation(bv, arg, false);
736 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
738 mathDispatchCreation(bv, arg, true);
742 void mathDispatchMathMacro(BufferView * bv, string const & arg)
744 if (bv->available()) {
746 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
749 string const s1 = token(s, ' ', 1);
750 int const na = s1.empty() ? 0 : lyx::atoi(s1);
751 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
757 void mathDispatchMathDelim(BufferView * bv, string const & arg)
759 if (bv->available()) {
760 if (openNewInset(bv, new InsetFormula))
761 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
766 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
768 if (bv->available()) {
769 if (openNewInset(bv, new InsetFormula))
770 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
775 void mathDispatchInsertMath(BufferView * bv, string const & arg)
777 if (bv->available()) {
778 if (arg.size() && arg[0] == '\\') {
779 InsetFormula * f = new InsetFormula(arg);
780 if (!bv->insertInset(f))
783 mathDispatchMathMode(bv, arg);
788 void mathDispatchGreek(BufferView * bv, string const & arg)
790 if (bv->available()) {
791 InsetFormula * f = new InsetFormula;
792 if (openNewInset(bv, f)) {
793 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);