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_iterator.h"
44 #include "math_macrotable.h"
45 #include "math_parser.h"
47 #include "math_spaceinset.h"
48 #include "undo_funcs.h"
54 MathCursor * mathcursor = 0;
68 void handleFont(BufferView * bv, MathTextCodes t)
70 if (mathcursor->selection())
71 bv->lockedInsetStoreUndo(Undo::EDIT);
72 mathcursor->handleFont(t);
76 void handleAccent(BufferView * bv, string const & name)
78 bv->lockedInsetStoreUndo(Undo::EDIT);
79 mathcursor->insert(createMathInset(name));
83 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
85 if (!bv->insertInset(new_inset)) {
89 new_inset->edit(bv, 0, 0, 0);
98 InsetFormulaBase::InsetFormulaBase()
99 : view_(0), font_(), xo_(0), yo_(0)
101 // This is needed as long the math parser is not re-entrant
102 MathMacroTable::builtinMacros();
103 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
107 // Check if uses AMS macros
108 void InsetFormulaBase::validate(LaTeXFeatures &) const
112 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
119 void InsetFormulaBase::metrics(BufferView * bv) const
123 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
128 string const InsetFormulaBase::editMessage() const
130 return _("Math editor mode");
134 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
136 if (!bv->lockInset(this))
137 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
139 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
141 mathcursor = new MathCursor(this, x == 0);
143 // handle ignored click
144 if (hack_x || hack_y) {
145 insetButtonPress(bv, hack_x, hack_y, hack_button);
150 // if that is removed, we won't get the magenta box when entering an
151 // inset for the first time
152 bv->updateInset(this, false);
156 void InsetFormulaBase::edit(BufferView * bv, bool front)
158 // looks hackish but seems to work
159 edit(bv, front ? 0 : 1, 0, 0);
163 void InsetFormulaBase::insetUnlock(BufferView * bv)
166 if (mathcursor->inMacroMode()) {
167 mathcursor->macroModeClose();
168 updateLocal(bv, true);
173 bv->updateInset(this, false);
177 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
179 mathcursor->getPos(x, y);
182 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
186 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
191 if (isCursorVisible())
192 bv->hideLockedInsetCursor();
197 mathcursor->getPos(x, y);
201 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
202 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
203 bv->showLockedInsetCursor(x, y, asc, des);
204 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
207 toggleCursorVisible();
211 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
213 if (isCursorVisible())
219 mathcursor->getPos(x, y);
223 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
224 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
225 bv->fitLockedInsetCursor(x, y, asc, des);
226 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
228 toggleInsetCursor(bv);
232 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
234 if (isCursorVisible())
235 toggleInsetCursor(bv);
239 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
242 bv->updateInset(this, false);
246 vector<string> const InsetFormulaBase::getLabelList() const
248 return std::vector<string>();
252 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
255 bv->updateInset(this, dirty);
259 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
260 int /*x*/, int /*y*/, int /*button*/)
264 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
267 bv->updateInset(this, false);
272 void InsetFormulaBase::insetButtonPress(BufferView * bv,
273 int x, int y, int button)
275 // hack to cope with mouseclick that comes before the call to edit()
279 hack_button = button;
283 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
284 << " but: " << button << "\n";
292 mathcursor->selClear();
293 mathcursor->setPos(x + xo_, y + yo_);
298 lyxerr << "insetButtonPress: 2\n";
301 bv->lockedInsetStoreUndo(Undo::EDIT);
303 mathcursor->selGet(ar);
304 mathcursor->setPos(x + xo_, y + yo_);
306 bv->getLyXText()->selectionAsString(bv->buffer(), false);
307 mathed_parse_cell(ar, sel);
308 mathcursor->insert(ar);
313 bv->updateInset(this, false);
317 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
318 int x, int y, int /*button*/)
323 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
324 //lyxerr << "insetMotionNotify: ignored\n";
330 if (!mathcursor->selection())
331 mathcursor->selStart();
333 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
334 // << ' ' << button << "\n";
336 mathcursor->setPos(x + xo_, y + yo_);
338 bv->updateInset(this, false);
342 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
344 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
348 UpdatableInset::RESULT
349 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
352 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
353 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
358 if (mathcursor->asHyperActiveInset()) {
359 lyxerr << " uurr.... getting dificult now\n";
360 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
363 RESULT result = DISPATCHED;
365 bool was_macro = mathcursor->inMacroMode();
366 bool was_selection = mathcursor->selection();
370 mathcursor->normalize();
374 // --- Cursor Movements ---------------------------------------------
377 sel = true; // fall through...
380 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
381 updateLocal(bv, false);
386 sel = true; // fall through
389 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
390 updateLocal(bv, false);
398 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
399 updateLocal(bv, false);
407 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
408 updateLocal(bv, false);
415 mathcursor->home(sel);
416 updateLocal(bv, false);
423 mathcursor->end(sel);
424 updateLocal(bv, false);
427 case LFUN_DELETE_LINE_FORWARD:
428 bv->lockedInsetStoreUndo(Undo::DELETE);
429 mathcursor->delLine();
430 updateLocal(bv, true);
434 mathcursor->idxNext();
435 updateLocal(bv, false);
439 mathcursor->idxPrev();
440 updateLocal(bv, false);
444 bv->lockedInsetStoreUndo(Undo::EDIT);
445 mathcursor->splitCell();
446 updateLocal(bv, true);
449 case LFUN_DELETE_WORD_BACKWARD:
451 bv->lockedInsetStoreUndo(Undo::DELETE);
452 mathcursor->backspace();
453 bv->updateInset(this, true);
456 case LFUN_DELETE_WORD_FORWARD:
458 bv->lockedInsetStoreUndo(Undo::DELETE);
460 bv->updateInset(this, true);
464 // sprintf(dispatch_buffer, "%d %d",);
465 // dispatch_result = dispatch_buffer;
468 lyxerr << "LFUN_SETXY broken!\n";
471 istringstream is(arg.c_str());
473 mathcursor->setPos(x, y);
474 updateLocal(bv, false);
480 mathcursor->macroModeClose();
481 bv->lockedInsetStoreUndo(Undo::EDIT);
482 mathcursor->selPaste();
483 updateLocal(bv, true);
487 bv->lockedInsetStoreUndo(Undo::DELETE);
488 mathcursor->selCut();
489 updateLocal(bv, true);
493 mathcursor->selCopy();
496 case LFUN_WORDRIGHTSEL:
497 case LFUN_WORDLEFTSEL:
500 // --- accented characters ------------------------------
502 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
503 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
504 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
505 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
506 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
507 case LFUN_MACRON: handleAccent(bv, "bar"); break;
508 case LFUN_DOT: handleAccent(bv, "dot"); break;
509 case LFUN_CARON: handleAccent(bv, "check"); break;
510 case LFUN_BREVE: handleAccent(bv, "breve"); break;
511 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
514 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
515 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
516 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
517 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
518 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
519 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
520 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
521 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
522 case LFUN_FREE: handleFont(bv, LM_TC_TEXTRM); break;
525 handleFont(bv, LM_TC_GREEK1);
527 mathcursor->interpret(arg);
531 //handleFont(bv, LM_TC_TEXTRM);
533 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
534 //updateLocal(bv, true);
536 //bv->owner()->message(_("math text mode toggled"));
539 case LFUN_MATH_LIMITS:
540 bv->lockedInsetStoreUndo(Undo::EDIT);
541 if (mathcursor->toggleLimits())
542 updateLocal(bv, true);
548 bv->lockedInsetStoreUndo(Undo::EDIT);
549 mathcursor->setSize(arg);
550 updateLocal(bv, true);
555 case LFUN_INSERT_MATRIX:
557 bv->lockedInsetStoreUndo(Undo::EDIT);
558 mathcursor->interpret("matrix " + arg);
559 updateLocal(bv, true);
563 case LFUN_MATH_SPACE:
565 bv->lockedInsetStoreUndo(Undo::EDIT);
566 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
567 updateLocal(bv, true);
571 case LFUN_SUPERSCRIPT:
574 bv->lockedInsetStoreUndo(Undo::EDIT);
575 mathcursor->script((action == LFUN_SUPERSCRIPT));
576 updateLocal(bv, true);
580 case LFUN_MATH_DELIM:
582 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
585 istringstream is(arg.c_str());
588 lyxerr << "can't parse delimeters from '" << arg << "'\n";
591 bv->lockedInsetStoreUndo(Undo::EDIT);
592 mathcursor->handleDelim(ls, rs);
593 updateLocal(bv, true);
597 case LFUN_PROTECTEDSPACE:
598 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
599 bv->lockedInsetStoreUndo(Undo::EDIT);
600 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
601 updateLocal(bv, true);
605 bv->owner()->message(_("Invalid action in math mode!"));
609 case LFUN_MATH_HALIGN:
610 case LFUN_MATH_VALIGN:
611 case LFUN_MATH_ROW_INSERT:
612 case LFUN_MATH_ROW_DELETE:
613 case LFUN_MATH_COLUMN_INSERT:
614 case LFUN_MATH_COLUMN_DELETE:
616 MathInset::idx_type idx = 0;
617 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
619 bv->lockedInsetStoreUndo(Undo::EDIT);
620 char al = arg.size() ? arg[0] : 'c';
622 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
623 case LFUN_MATH_VALIGN: p->valign(al); break;
624 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
625 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
626 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
627 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
630 updateLocal(bv, true);
635 case LFUN_EXEC_COMMAND:
636 result = UNDISPATCHED;
639 case LFUN_BREAKPARAGRAPH:
640 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
641 //lyxerr << "LFUN ignored\n";
645 // interpret this as if a backslash was typed
646 bv->lockedInsetStoreUndo(Undo::EDIT);
647 mathcursor->interpret("\\");
648 updateLocal(bv, true);
652 case LFUN_INSERT_MATH:
653 case LFUN_SELFINSERT:
655 bv->lockedInsetStoreUndo(Undo::EDIT);
656 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
657 updateLocal(bv, true);
661 case LFUN_MATH_PANEL:
662 result = UNDISPATCHED;
666 if (mathcursor->selection())
667 mathcursor->selClear();
669 result = UNDISPATCHED;
673 result = UNDISPATCHED;
676 mathcursor->normalize();
678 lyx::Assert(mathcursor);
680 if (mathcursor->selection() || was_selection)
681 toggleInsetSelection(bv);
683 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
684 result == UNDISPATCHED)
687 bv->unlockInset(this);
689 return result; // original version
693 Inset::Code InsetFormulaBase::lyxCode() const
695 return Inset::MATH_CODE;
699 int InsetFormulaBase::ylow() const
701 return yo_ - ascent(view_, font_);
705 int InsetFormulaBase::yhigh() const
707 return yo_ + descent(view_, font_);
711 int InsetFormulaBase::xlow() const
717 int InsetFormulaBase::xhigh() const
719 return xo_ + width(view_, font_);
723 /////////////////////////////////////////////////////////////////////
726 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
727 bool const &, bool const &)
732 static InsetFormulaBase * lastformula = 0;
733 static MathIterator current = MathIterator(ibegin(par().nucleus()));
735 static string laststr;
737 if (lastformula != this || laststr != str) {
738 //lyxerr << "reset lastformula to " << this << "\n";
741 current = ibegin(par().nucleus());
743 mathed_parse_cell(ar, str);
747 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
749 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
750 if (it.cell().matchpart(ar, it.position().pos_)) {
751 mathcursor->setSelection(it.cursor(), ar.size());
754 // I guess some of the following can go
755 bv->toggleSelection(true);
757 updateLocal(bv, true);
764 //lyxerr << "not found!\n";
766 // we have to unlock ourself in this function by default!
767 // don't ask me why...
768 bv->unlockInset(this);
773 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
774 bool const & a, bool const & b)
776 lyxerr << "searching backward not implemented in mathed" << endl;
777 return searchForward(bv, what, a, b);
781 /////////////////////////////////////////////////////////////////////
784 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
786 if (bv->available()) {
787 // use selection if available..
789 //if (action == LFUN_MATH_IMPORT_SELECTION)
793 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
795 InsetFormulaBase * f;
797 f = new InsetFormula;
798 if (openNewInset(bv, f)) {
799 // don't do that also for LFUN_MATH_MODE unless you want end up with
800 // always changing to mathrm when opening an inlined inset
801 // -- I really hate "LyXfunc overloading"...
803 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
804 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
807 // create a macro if we see "\\newcommand" somewhere, and an ordinary
809 if (sel.find("\\newcommand") == string::npos)
810 f = new InsetFormula(sel);
812 f = new InsetFormulaMacro(sel);
813 bv->getLyXText()->cutSelection(bv);
817 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
821 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
823 mathDispatchCreation(bv, arg, true);
827 void mathDispatchMathMode(BufferView * bv, string const & arg)
829 mathDispatchCreation(bv, arg, false);
833 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
835 mathDispatchCreation(bv, arg, true);
839 void mathDispatchMathMacro(BufferView * bv, string const & arg)
841 if (bv->available()) {
843 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
846 string const s1 = token(s, ' ', 1);
847 int const na = s1.empty() ? 0 : lyx::atoi(s1);
848 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
854 void mathDispatchMathDelim(BufferView * bv, string const & arg)
856 if (bv->available()) {
857 if (openNewInset(bv, new InsetFormula))
858 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
863 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
865 if (bv->available()) {
866 if (openNewInset(bv, new InsetFormula))
867 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
872 void mathDispatchInsertMath(BufferView * bv, string const & arg)
874 if (bv->available()) {
875 if (arg.size() && arg[0] == '\\') {
876 InsetFormula * f = new InsetFormula(arg);
877 if (!bv->insertInset(f))
880 mathDispatchMathMode(bv, arg);
885 void mathDispatchGreek(BufferView * bv, string const & arg)
887 if (bv->available()) {
888 InsetFormula * f = new InsetFormula;
889 if (openNewInset(bv, f)) {
890 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
897 void mathDispatch(BufferView *, kb_action, string const &)