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();
373 // --- Cursor Movements ---------------------------------------------
376 sel = true; // fall through...
379 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
380 updateLocal(bv, false);
385 sel = true; // fall through
388 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
389 updateLocal(bv, false);
397 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
398 updateLocal(bv, false);
406 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
407 updateLocal(bv, false);
414 mathcursor->home(sel);
415 updateLocal(bv, false);
422 mathcursor->end(sel);
423 updateLocal(bv, false);
426 case LFUN_DELETE_LINE_FORWARD:
427 bv->lockedInsetStoreUndo(Undo::DELETE);
428 mathcursor->delLine();
429 updateLocal(bv, true);
433 mathcursor->idxNext();
434 updateLocal(bv, false);
438 mathcursor->idxPrev();
439 updateLocal(bv, false);
443 bv->lockedInsetStoreUndo(Undo::EDIT);
444 mathcursor->splitCell();
445 updateLocal(bv, true);
448 case LFUN_DELETE_WORD_BACKWARD:
450 bv->lockedInsetStoreUndo(Undo::DELETE);
451 mathcursor->backspace();
452 bv->updateInset(this, true);
455 case LFUN_DELETE_WORD_FORWARD:
457 bv->lockedInsetStoreUndo(Undo::DELETE);
459 bv->updateInset(this, true);
463 // sprintf(dispatch_buffer, "%d %d",);
464 // dispatch_result = dispatch_buffer;
467 lyxerr << "LFUN_SETXY broken!\n";
470 istringstream is(arg.c_str());
472 mathcursor->setPos(x, y);
473 updateLocal(bv, false);
479 mathcursor->macroModeClose();
480 bv->lockedInsetStoreUndo(Undo::EDIT);
481 mathcursor->selPaste();
482 updateLocal(bv, true);
486 bv->lockedInsetStoreUndo(Undo::DELETE);
487 mathcursor->selCut();
488 updateLocal(bv, true);
492 mathcursor->selCopy();
495 case LFUN_WORDRIGHTSEL:
496 case LFUN_WORDLEFTSEL:
499 // --- accented characters ------------------------------
501 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
502 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
503 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
504 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
505 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
506 case LFUN_MACRON: handleAccent(bv, "bar"); break;
507 case LFUN_DOT: handleAccent(bv, "dot"); break;
508 case LFUN_CARON: handleAccent(bv, "check"); break;
509 case LFUN_BREVE: handleAccent(bv, "breve"); break;
510 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
511 case LFUN_UNDERBAR: handleAccent(bv, "underbar"); 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_FRAK: handleFont(bv, LM_TC_EUFRAK); break;
521 case LFUN_ITAL: handleFont(bv, LM_TC_IT); break;
522 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
523 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
524 case LFUN_FREE: handleFont(bv, LM_TC_TEXTRM); break;
527 handleFont(bv, LM_TC_GREEK1);
529 mathcursor->interpret(arg);
533 //handleFont(bv, LM_TC_TEXTRM);
535 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
536 //updateLocal(bv, true);
538 //bv->owner()->message(_("math text mode toggled"));
541 case LFUN_MATH_LIMITS:
542 bv->lockedInsetStoreUndo(Undo::EDIT);
543 if (mathcursor->toggleLimits())
544 updateLocal(bv, true);
550 bv->lockedInsetStoreUndo(Undo::EDIT);
551 mathcursor->setSize(arg);
552 updateLocal(bv, true);
557 case LFUN_INSERT_MATRIX:
559 bv->lockedInsetStoreUndo(Undo::EDIT);
560 mathcursor->interpret("matrix " + arg);
561 updateLocal(bv, true);
565 case LFUN_MATH_SPACE:
567 bv->lockedInsetStoreUndo(Undo::EDIT);
568 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
569 updateLocal(bv, true);
573 case LFUN_SUPERSCRIPT:
576 bv->lockedInsetStoreUndo(Undo::EDIT);
577 mathcursor->script((action == LFUN_SUPERSCRIPT));
578 updateLocal(bv, true);
582 case LFUN_MATH_DELIM:
584 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
587 istringstream is(arg.c_str());
590 lyxerr << "can't parse delimeters from '" << arg << "'\n";
593 bv->lockedInsetStoreUndo(Undo::EDIT);
594 mathcursor->handleDelim(ls, rs);
595 updateLocal(bv, true);
599 case LFUN_PROTECTEDSPACE:
600 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
601 bv->lockedInsetStoreUndo(Undo::EDIT);
602 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
603 updateLocal(bv, true);
607 bv->owner()->message(_("Invalid action in math mode!"));
611 case LFUN_MATH_HALIGN:
612 case LFUN_MATH_VALIGN:
613 case LFUN_MATH_ROW_INSERT:
614 case LFUN_MATH_ROW_DELETE:
615 case LFUN_MATH_COLUMN_INSERT:
616 case LFUN_MATH_COLUMN_DELETE:
618 MathInset::idx_type idx = 0;
619 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
621 bv->lockedInsetStoreUndo(Undo::EDIT);
622 char al = arg.size() ? arg[0] : 'c';
624 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
625 case LFUN_MATH_VALIGN: p->valign(al); break;
626 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
627 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
628 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
629 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
632 updateLocal(bv, true);
637 case LFUN_EXEC_COMMAND:
638 result = UNDISPATCHED;
641 case LFUN_BREAKPARAGRAPH:
642 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
643 //lyxerr << "LFUN ignored\n";
647 // interpret this as if a backslash was typed
648 bv->lockedInsetStoreUndo(Undo::EDIT);
649 mathcursor->interpret("\\");
650 updateLocal(bv, true);
654 case LFUN_INSERT_MATH:
655 case LFUN_SELFINSERT:
657 bv->lockedInsetStoreUndo(Undo::EDIT);
658 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
659 updateLocal(bv, true);
663 case LFUN_MATH_PANEL:
664 result = UNDISPATCHED;
668 if (mathcursor->selection())
669 mathcursor->selClear();
671 result = UNDISPATCHED;
675 result = UNDISPATCHED;
678 mathcursor->normalize();
680 lyx::Assert(mathcursor);
682 if (mathcursor->selection() || was_selection)
683 toggleInsetSelection(bv);
685 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
686 result == UNDISPATCHED)
689 bv->unlockInset(this);
691 return result; // original version
695 Inset::Code InsetFormulaBase::lyxCode() const
697 return Inset::MATH_CODE;
701 int InsetFormulaBase::ylow() const
703 return yo_ - ascent(view_, font_);
707 int InsetFormulaBase::yhigh() const
709 return yo_ + descent(view_, font_);
713 int InsetFormulaBase::xlow() const
719 int InsetFormulaBase::xhigh() const
721 return xo_ + width(view_, font_);
725 /////////////////////////////////////////////////////////////////////
728 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
734 static InsetFormulaBase * lastformula = 0;
735 static MathIterator current = MathIterator(ibegin(par().nucleus()));
737 static string laststr;
739 if (lastformula != this || laststr != str) {
740 //lyxerr << "reset lastformula to " << this << "\n";
743 current = ibegin(par().nucleus());
745 mathed_parse_cell(ar, str);
749 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
751 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
752 if (it.cell().matchpart(ar, it.position().pos_)) {
753 mathcursor->setSelection(it.cursor(), ar.size());
756 // I guess some of the following can go
757 bv->toggleSelection(true);
759 updateLocal(bv, true);
766 //lyxerr << "not found!\n";
768 // we have to unlock ourself in this function by default!
769 // don't ask me why...
770 bv->unlockInset(this);
775 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
778 lyxerr << "searching backward not implemented in mathed" << endl;
779 return searchForward(bv, what, a, b);
783 /////////////////////////////////////////////////////////////////////
786 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
788 if (bv->available()) {
789 // use selection if available..
791 //if (action == LFUN_MATH_IMPORT_SELECTION)
795 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
797 InsetFormulaBase * f;
799 f = new InsetFormula;
800 if (openNewInset(bv, f)) {
801 // don't do that also for LFUN_MATH_MODE unless you want end up with
802 // always changing to mathrm when opening an inlined inset
803 // -- I really hate "LyXfunc overloading"...
805 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
806 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
809 // create a macro if we see "\\newcommand" somewhere, and an ordinary
811 if (sel.find("\\newcommand") == string::npos)
812 f = new InsetFormula(sel);
814 f = new InsetFormulaMacro(sel);
815 bv->getLyXText()->cutSelection(bv);
819 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
823 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
825 mathDispatchCreation(bv, arg, true);
829 void mathDispatchMathMode(BufferView * bv, string const & arg)
831 mathDispatchCreation(bv, arg, false);
835 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
837 mathDispatchCreation(bv, arg, true);
841 void mathDispatchMathMacro(BufferView * bv, string const & arg)
843 if (bv->available()) {
845 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
848 string const s1 = token(s, ' ', 1);
849 int const na = s1.empty() ? 0 : lyx::atoi(s1);
850 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
856 void mathDispatchMathDelim(BufferView * bv, string const & arg)
858 if (bv->available()) {
859 if (openNewInset(bv, new InsetFormula))
860 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
865 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
867 if (bv->available()) {
868 if (openNewInset(bv, new InsetFormula))
869 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
874 void mathDispatchInsertMath(BufferView * bv, string const & arg)
876 if (bv->available()) {
877 if (arg.size() && arg[0] == '\\') {
878 InsetFormula * f = new InsetFormula(arg);
879 if (!bv->insertInset(f))
882 mathDispatchMathMode(bv, arg);
887 void mathDispatchGreek(BufferView * bv, string const & arg)
889 if (bv->available()) {
890 InsetFormula * f = new InsetFormula;
891 if (openNewInset(bv, f)) {
892 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
899 void mathDispatch(BufferView *, kb_action, string const &)