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);
381 // write something to the minibuffer
382 //bv->owner()->message(mathcursor->info());
387 sel = true; // fall through
390 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
391 updateLocal(bv, false);
399 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
400 updateLocal(bv, false);
408 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
409 updateLocal(bv, false);
416 mathcursor->home(sel);
417 updateLocal(bv, false);
424 mathcursor->end(sel);
425 updateLocal(bv, false);
428 case LFUN_DELETE_LINE_FORWARD:
429 bv->lockedInsetStoreUndo(Undo::DELETE);
430 mathcursor->delLine();
431 updateLocal(bv, true);
435 mathcursor->idxNext();
436 updateLocal(bv, false);
440 mathcursor->idxPrev();
441 updateLocal(bv, false);
445 bv->lockedInsetStoreUndo(Undo::EDIT);
446 mathcursor->splitCell();
447 updateLocal(bv, true);
450 case LFUN_DELETE_WORD_BACKWARD:
452 bv->lockedInsetStoreUndo(Undo::DELETE);
453 mathcursor->backspace();
454 bv->updateInset(this, true);
457 case LFUN_DELETE_WORD_FORWARD:
459 bv->lockedInsetStoreUndo(Undo::DELETE);
461 bv->updateInset(this, true);
465 // sprintf(dispatch_buffer, "%d %d",);
466 // dispatch_result = dispatch_buffer;
469 lyxerr << "LFUN_SETXY broken!\n";
472 istringstream is(arg.c_str());
474 mathcursor->setPos(x, y);
475 updateLocal(bv, false);
481 mathcursor->macroModeClose();
482 bv->lockedInsetStoreUndo(Undo::EDIT);
483 mathcursor->selPaste();
484 updateLocal(bv, true);
488 bv->lockedInsetStoreUndo(Undo::DELETE);
489 mathcursor->selCut();
490 updateLocal(bv, true);
494 mathcursor->selCopy();
497 case LFUN_WORDRIGHTSEL:
498 case LFUN_WORDLEFTSEL:
501 // --- accented characters ------------------------------
503 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
504 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
505 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
506 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
507 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
508 case LFUN_MACRON: handleAccent(bv, "bar"); break;
509 case LFUN_DOT: handleAccent(bv, "dot"); break;
510 case LFUN_CARON: handleAccent(bv, "check"); break;
511 case LFUN_BREVE: handleAccent(bv, "breve"); break;
512 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
513 case LFUN_UNDERBAR: handleAccent(bv, "underbar"); break;
516 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
517 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
518 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
519 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
520 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
521 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
522 case LFUN_FRAK: handleFont(bv, LM_TC_EUFRAK); break;
523 case LFUN_ITAL: handleFont(bv, LM_TC_IT); break;
524 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
525 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
526 case LFUN_FREE: handleFont(bv, LM_TC_TEXTRM); break;
529 handleFont(bv, LM_TC_GREEK1);
531 mathcursor->interpret(arg);
535 //handleFont(bv, LM_TC_TEXTRM);
537 //mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
538 //updateLocal(bv, true);
540 //bv->owner()->message(_("math text mode toggled"));
543 case LFUN_MATH_LIMITS:
544 bv->lockedInsetStoreUndo(Undo::EDIT);
545 if (mathcursor->toggleLimits())
546 updateLocal(bv, true);
552 bv->lockedInsetStoreUndo(Undo::EDIT);
553 mathcursor->setSize(arg);
554 updateLocal(bv, true);
559 case LFUN_INSERT_MATRIX:
561 bv->lockedInsetStoreUndo(Undo::EDIT);
562 mathcursor->interpret("matrix " + arg);
563 updateLocal(bv, true);
567 case LFUN_MATH_SPACE:
569 bv->lockedInsetStoreUndo(Undo::EDIT);
570 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
571 updateLocal(bv, true);
575 case LFUN_SUPERSCRIPT:
578 bv->lockedInsetStoreUndo(Undo::EDIT);
579 mathcursor->script((action == LFUN_SUPERSCRIPT));
580 updateLocal(bv, true);
584 case LFUN_MATH_DELIM:
586 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
589 istringstream is(arg.c_str());
592 lyxerr << "can't parse delimeters from '" << arg << "'\n";
595 bv->lockedInsetStoreUndo(Undo::EDIT);
596 mathcursor->handleDelim(ls, rs);
597 updateLocal(bv, true);
601 case LFUN_PROTECTEDSPACE:
602 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
603 bv->lockedInsetStoreUndo(Undo::EDIT);
604 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
605 updateLocal(bv, true);
609 bv->owner()->message(_("Invalid action in math mode!"));
613 case LFUN_MATH_HALIGN:
614 case LFUN_MATH_VALIGN:
615 case LFUN_MATH_ROW_INSERT:
616 case LFUN_MATH_ROW_DELETE:
617 case LFUN_MATH_COLUMN_INSERT:
618 case LFUN_MATH_COLUMN_DELETE:
620 MathInset::idx_type idx = 0;
621 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
623 bv->lockedInsetStoreUndo(Undo::EDIT);
624 char al = arg.size() ? arg[0] : 'c';
626 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
627 case LFUN_MATH_VALIGN: p->valign(al); break;
628 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
629 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
630 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
631 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
634 updateLocal(bv, true);
639 case LFUN_EXEC_COMMAND:
640 result = UNDISPATCHED;
643 case LFUN_BREAKPARAGRAPH:
644 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
645 //lyxerr << "LFUN ignored\n";
649 // interpret this as if a backslash was typed
650 bv->lockedInsetStoreUndo(Undo::EDIT);
651 mathcursor->interpret("\\");
652 updateLocal(bv, true);
656 case LFUN_INSERT_MATH:
657 case LFUN_SELFINSERT:
659 bv->lockedInsetStoreUndo(Undo::EDIT);
660 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
661 updateLocal(bv, true);
665 case LFUN_MATH_PANEL:
666 result = UNDISPATCHED;
670 if (mathcursor->selection())
671 mathcursor->selClear();
673 result = UNDISPATCHED;
677 result = UNDISPATCHED;
680 mathcursor->normalize();
682 lyx::Assert(mathcursor);
684 if (mathcursor->selection() || was_selection)
685 toggleInsetSelection(bv);
687 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
688 result == UNDISPATCHED)
691 bv->unlockInset(this);
693 return result; // original version
697 Inset::Code InsetFormulaBase::lyxCode() const
699 return Inset::MATH_CODE;
703 int InsetFormulaBase::ylow() const
705 return yo_ - ascent(view_, font_);
709 int InsetFormulaBase::yhigh() const
711 return yo_ + descent(view_, font_);
715 int InsetFormulaBase::xlow() const
721 int InsetFormulaBase::xhigh() const
723 return xo_ + width(view_, font_);
727 /////////////////////////////////////////////////////////////////////
730 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
736 static InsetFormulaBase * lastformula = 0;
737 static MathIterator current = MathIterator(ibegin(par().nucleus()));
739 static string laststr;
741 if (lastformula != this || laststr != str) {
742 //lyxerr << "reset lastformula to " << this << "\n";
745 current = ibegin(par().nucleus());
747 mathed_parse_cell(ar, str);
751 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
753 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
754 if (it.cell().matchpart(ar, it.position().pos_)) {
755 mathcursor->setSelection(it.cursor(), ar.size());
758 // I guess some of the following can go
759 bv->toggleSelection(true);
761 updateLocal(bv, true);
768 //lyxerr << "not found!\n";
770 // we have to unlock ourself in this function by default!
771 // don't ask me why...
772 bv->unlockInset(this);
777 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
780 lyxerr << "searching backward not implemented in mathed" << endl;
781 return searchForward(bv, what, a, b);
785 /////////////////////////////////////////////////////////////////////
788 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
790 if (bv->available()) {
791 // use selection if available..
793 //if (action == LFUN_MATH_IMPORT_SELECTION)
797 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
799 InsetFormulaBase * f;
801 f = new InsetFormula;
802 if (openNewInset(bv, f)) {
803 // don't do that also for LFUN_MATH_MODE unless you want end up with
804 // always changing to mathrm when opening an inlined inset
805 // -- I really hate "LyXfunc overloading"...
807 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
808 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
811 // create a macro if we see "\\newcommand" somewhere, and an ordinary
813 if (sel.find("\\newcommand") == string::npos)
814 f = new InsetFormula(sel);
816 f = new InsetFormulaMacro(sel);
817 bv->getLyXText()->cutSelection(bv);
821 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
825 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
827 mathDispatchCreation(bv, arg, true);
831 void mathDispatchMathMode(BufferView * bv, string const & arg)
833 mathDispatchCreation(bv, arg, false);
837 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
839 mathDispatchCreation(bv, arg, true);
843 void mathDispatchMathMacro(BufferView * bv, string const & arg)
845 if (bv->available()) {
847 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
850 string const s1 = token(s, ' ', 1);
851 int const na = s1.empty() ? 0 : lyx::atoi(s1);
852 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
858 void mathDispatchMathDelim(BufferView * bv, string const & arg)
860 if (bv->available()) {
861 if (openNewInset(bv, new InsetFormula))
862 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
867 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
869 if (bv->available()) {
870 if (openNewInset(bv, new InsetFormula))
871 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
876 void mathDispatchInsertMath(BufferView * bv, string const & arg)
878 if (bv->available()) {
879 if (arg.size() && arg[0] == '\\') {
880 InsetFormula * f = new InsetFormula(arg);
881 if (!bv->insertInset(f))
884 mathDispatchMathMode(bv, arg);
889 void mathDispatchGreek(BufferView * bv, string const & arg)
891 if (bv->available()) {
892 InsetFormula * f = new InsetFormula;
893 if (openNewInset(bv, f)) {
894 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
901 void mathDispatch(BufferView *, kb_action, string const &)