2 * \file math_nestinset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_nestinset.h"
15 #include "math_arrayinset.h"
16 #include "math_biginset.h"
17 #include "math_boxinset.h"
18 #include "math_braceinset.h"
19 #include "math_colorinset.h"
20 #include "math_commentinset.h"
21 #include "math_data.h"
22 #include "math_deliminset.h"
23 #include "math_factory.h"
24 #include "math_hullinset.h"
25 #include "math_mathmlstream.h"
26 #include "math_macroarg.h"
27 //#include "math_mboxinset.h"
28 #include "math_parser.h"
29 #include "math_scriptinset.h"
30 #include "math_spaceinset.h"
31 #include "math_symbolinset.h"
32 #include "math_support.h"
33 #include "math_unknowninset.h"
34 #include "ref_inset.h"
36 #include "BufferView.h"
37 #include "CutAndPaste.h"
38 #include "FuncStatus.h"
40 #include "bufferview_funcs.h"
41 #include "coordcache.h"
44 #include "dispatchresult.h"
45 #include "funcrequest.h"
47 #include "outputparams.h"
50 #include "support/lstrings.h"
52 #include "frontends/Dialogs.h"
53 #include "frontends/LyXView.h"
54 #include "frontends/Painter.h"
55 #include "frontends/nullpainter.h"
59 using lyx::cap::copySelection;
60 using lyx::cap::grabAndEraseSelection;
61 using lyx::cap::cutSelection;
62 using lyx::cap::replaceSelection;
63 using lyx::cap::selClearOrDel;
67 using std::istringstream;
70 MathNestInset::MathNestInset(idx_type nargs)
71 : cells_(nargs), lock_(false)
75 MathInset::idx_type MathNestInset::nargs() const
81 MathArray & MathNestInset::cell(idx_type i)
87 MathArray const & MathNestInset::cell(idx_type i) const
93 void MathNestInset::cursorPos(CursorSlice const & sl, bool /*boundary*/,
94 int & x, int & y) const
96 // FIXME: This is a hack. Ideally, the coord cache should not store
97 // absolute positions, but relative ones. This would mean to call
98 // setXY() not in MathArray::draw(), but in the parent insets' draw()
99 // with the correctly adjusted x,y values. But this means that we'd have
100 // to touch all (math)inset's draw() methods. Right now, we'll store
101 // absolute value, and make them here relative, only to make them
102 // absolute again when actually drawing the cursor. What a mess.
103 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
104 MathArray const & ar = sl.cell();
105 if (!theCoords.getArrays().has(&ar)) {
106 // this can (semi-)legally happen if we just created this cell
107 // and it never has been drawn before. So don't ASSERT.
108 //lyxerr << "no cached data for array " << &ar << endl;
113 Point const pt = theCoords.getArrays().xy(&ar);
114 if (!theCoords.getInsets().has(this)) {
116 //lyxerr << "no cached data for inset " << this << endl;
121 Point const pt2 = theCoords.getInsets().xy(this);
122 //lyxerr << "retrieving position cache for MathArray "
123 // << pt.x_ << ' ' << pt.y_ << std::endl;
124 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
126 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
127 // << " asc: " << ascent() << " des: " << descent()
128 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
129 // move cursor visually into empty cells ("blue rectangles");
135 void MathNestInset::metrics(MetricsInfo const & mi) const
138 for (idx_type i = 0, n = nargs(); i != n; ++i)
143 bool MathNestInset::idxNext(LCursor & cur) const
145 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
146 if (cur.idx() == cur.lastidx())
154 bool MathNestInset::idxRight(LCursor & cur) const
160 bool MathNestInset::idxPrev(LCursor & cur) const
162 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
166 cur.pos() = cur.lastpos();
171 bool MathNestInset::idxLeft(LCursor & cur) const
177 bool MathNestInset::idxFirst(LCursor & cur) const
179 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
188 bool MathNestInset::idxLast(LCursor & cur) const
190 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
193 cur.idx() = cur.lastidx();
194 cur.pos() = cur.lastpos();
199 void MathNestInset::dump() const
201 WriteStream os(lyxerr);
202 os << "---------------------------------------------\n";
205 for (idx_type i = 0, n = nargs(); i != n; ++i)
206 os << cell(i) << "\n";
207 os << "---------------------------------------------\n";
211 void MathNestInset::draw(PainterInfo & pi, int x, int y) const
215 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
218 setPosCache(pi, x, y);
222 void MathNestInset::drawSelection(PainterInfo & pi, int x, int y) const
224 // this should use the x/y values given, not the cached values
225 LCursor & cur = pi.base.bv->cursor();
226 if (!cur.selection())
228 if (!ptr_cmp(&cur.inset(), this))
231 // FIXME: hack to get position cache warm
232 static NullPainter nop;
233 PainterInfo pinop(pi);
237 CursorSlice s1 = cur.selBegin();
238 CursorSlice s2 = cur.selEnd();
239 //lyxerr << "MathNestInset::drawing selection: "
240 // << " s1: " << s1 << " s2: " << s2 << endl;
241 if (s1.idx() == s2.idx()) {
242 MathArray const & c = cell(s1.idx());
243 int x1 = c.xo() + c.pos2x(s1.pos());
244 int y1 = c.yo() - c.ascent();
245 int x2 = c.xo() + c.pos2x(s2.pos());
246 int y2 = c.yo() + c.descent();
247 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
248 //lyxerr << "MathNestInset::drawing selection 3: "
249 // << " x1: " << x1 << " x2: " << x2
250 // << " y1: " << y1 << " y2: " << y2 << endl;
252 for (idx_type i = 0; i < nargs(); ++i) {
253 if (idxBetween(i, s1.idx(), s2.idx())) {
254 MathArray const & c = cell(i);
256 int y1 = c.yo() - c.ascent();
257 int x2 = c.xo() + c.width();
258 int y2 = c.yo() + c.descent();
259 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
266 void MathNestInset::validate(LaTeXFeatures & features) const
268 for (idx_type i = 0; i < nargs(); ++i)
269 cell(i).validate(features);
273 void MathNestInset::replace(ReplaceData & rep)
275 for (idx_type i = 0; i < nargs(); ++i)
276 cell(i).replace(rep);
280 bool MathNestInset::contains(MathArray const & ar) const
282 for (idx_type i = 0; i < nargs(); ++i)
283 if (cell(i).contains(ar))
289 bool MathNestInset::lock() const
295 void MathNestInset::lock(bool l)
301 bool MathNestInset::isActive() const
307 MathArray MathNestInset::glue() const
310 for (size_t i = 0; i < nargs(); ++i)
316 void MathNestInset::write(WriteStream & os) const
318 os << '\\' << name().c_str();
319 for (size_t i = 0; i < nargs(); ++i)
320 os << '{' << cell(i) << '}';
322 os.pendingSpace(true);
323 if (lock_ && !os.latex()) {
325 os.pendingSpace(true);
330 void MathNestInset::normalize(NormalStream & os) const
332 os << '[' << name().c_str();
333 for (size_t i = 0; i < nargs(); ++i)
334 os << ' ' << cell(i);
339 int MathNestInset::latex(Buffer const &, std::ostream & os,
340 OutputParams const & runparams) const
342 WriteStream wi(os, runparams.moving_arg, true);
348 void MathNestInset::notifyCursorLeaves(LCursor & cur)
354 MathArray & ar = cur.cell();
355 // remove base-only "scripts"
356 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
357 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
358 if (p && p->nargs() == 1) {
359 MathArray ar = p->nuc();
362 cur.adjust(i, ar.size() - 1);
366 // glue adjacent font insets of the same kind
367 for (pos_type i = 0; i + 1 < size(); ++i) {
368 MathFontInset * p = operator[](i).nucleus()->asFontInset();
369 MathFontInset const * q = operator[](i + 1)->asFontInset();
370 if (p && q && p->name() == q->name()) {
371 p->cell(0).append(q->cell(0));
380 void MathNestInset::handleFont
381 (LCursor & cur, string const & arg, string const & font)
383 // this whole function is a hack and won't work for incremental font
385 recordUndo(cur, Undo::ATOMIC);
387 if (cur.inset().asMathInset()->name() == font)
388 cur.handleFont(font);
390 cur.handleNest(createMathInset(font));
396 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
398 recordUndo(cur, Undo::ATOMIC);
401 bv_funcs::string2font(arg, font, b);
402 if (font.color() != LColor::inherit) {
403 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
404 cur.handleNest(at, 0);
409 void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
411 //lyxerr << "MathNestInset: request: " << cmd << std::endl;
412 //CursorSlice sl = cur.current();
414 switch (cmd.action) {
418 cur.message(_("Paste"));
419 replaceSelection(cur);
421 istringstream is(cmd.argument);
423 string const selection = lyx::cap::getSelection(cur.buffer(), n);
424 cur.niceInsert(selection);
425 cur.clearSelection(); // bug 393
426 cur.bv().switchKeyMap();
433 cutSelection(cur, true, true);
434 cur.message(_("Cut"));
435 // Prevent stale position >= size crash
436 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
442 cur.message(_("Copy"));
445 case LFUN_MOUSE_PRESS:
446 lfunMousePress(cur, cmd);
449 case LFUN_MOUSE_MOTION:
450 lfunMouseMotion(cur, cmd);
453 case LFUN_MOUSE_RELEASE:
454 lfunMouseRelease(cur, cmd);
457 case LFUN_FINISHED_LEFT:
458 cur.bv().cursor() = cur;
461 case LFUN_FINISHED_RIGHT:
463 cur.bv().cursor() = cur;
466 case LFUN_FINISHED_UP:
467 cur.bv().cursor() = cur;
470 case LFUN_FINISHED_DOWN:
472 cur.bv().cursor() = cur;
477 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
478 cur.autocorrect() = false;
480 cur.macroModeClose();
481 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
482 cur.pushLeft(*cur.nextAtom().nucleus());
483 cur.inset().idxFirst(cur);
484 } else if (cur.posRight() || idxRight(cur)
485 || cur.popRight() || cur.selection())
488 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
495 cur.selHandle(cmd.action == LFUN_LEFTSEL);
496 cur.autocorrect() = false;
498 cur.macroModeClose();
499 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
501 cur.push(*cur.nextAtom().nucleus());
502 cur.inset().idxLast(cur);
503 } else if (cur.posLeft() || idxLeft(cur)
504 || cur.popLeft() || cur.selection())
507 cmd = FuncRequest(LFUN_FINISHED_LEFT);
514 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
515 if (cur.inMacroMode()) {
517 cur.macroModeClose();
520 cur.selHandle(cmd.action == LFUN_UPSEL);
522 cmd = FuncRequest(LFUN_FINISHED_UP);
525 // fixes bug 1598. Please check!
531 if (cur.inMacroMode()) {
532 cur.macroModeClose();
535 cur.selHandle(cmd.action == LFUN_DOWNSEL);
537 cmd = FuncRequest(LFUN_FINISHED_DOWN);
540 // fixes bug 1598. Please check!
544 case LFUN_MOUSE_DOUBLE:
545 case LFUN_MOUSE_TRIPLE:
550 cur.selection() = true;
551 cur.pos() = cur.lastpos();
552 cur.idx() = cur.lastidx();
555 case LFUN_UP_PARAGRAPHSEL:
556 case LFUN_UP_PARAGRAPH:
557 case LFUN_DOWN_PARAGRAPHSEL:
558 case LFUN_DOWN_PARAGRAPH:
563 case LFUN_WORDLEFTSEL:
565 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
566 cur.macroModeClose();
567 if (cur.pos() != 0) {
569 } else if (cur.col() != 0) {
570 cur.idx() -= cur.col();
572 } else if (cur.idx() != 0) {
576 cmd = FuncRequest(LFUN_FINISHED_LEFT);
581 case LFUN_WORDRIGHTSEL:
585 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
586 cur.macroModeClose();
588 if (cur.pos() != cur.lastpos()) {
589 cur.pos() = cur.lastpos();
590 } else if (ncols() && (cur.col() != cur.lastcol())) {
591 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
592 cur.pos() = cur.lastpos();
593 } else if (cur.idx() != cur.lastidx()) {
594 cur.idx() = cur.lastidx();
595 cur.pos() = cur.lastpos();
597 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
604 cmd = FuncRequest(LFUN_FINISHED_LEFT);
610 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
614 case LFUN_CELL_FORWARD:
615 cur.inset().idxNext(cur);
618 case LFUN_CELL_BACKWARD:
619 cur.inset().idxPrev(cur);
622 case LFUN_DELETE_WORD_BACKWARD:
625 // May affect external cell:
626 recordUndoInset(cur, Undo::ATOMIC);
628 recordUndo(cur, Undo::ATOMIC);
632 case LFUN_DELETE_WORD_FORWARD:
634 if (cur.pos() == cur.lastpos())
635 // May affect external cell:
636 recordUndoInset(cur, Undo::ATOMIC);
638 recordUndo(cur, Undo::ATOMIC);
644 cur.clearSelection();
646 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
651 case LFUN_INSET_TOGGLE:
654 if (cur.pos() != cur.lastpos()) {
655 // toggle previous inset ...
656 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
657 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
658 // ... or enclosing inset if we are in the last inset position
659 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
664 case LFUN_SELFINSERT:
665 if (cmd.argument.size() != 1) {
667 cur.insert(cmd.argument);
670 // Don't record undo steps if we are in macro mode and
671 // cmd.argument is the next character of the macro name.
672 // Otherwise we'll get an invalid cursor if we undo after
673 // the macro was finished and the macro is a known command,
674 // e.g. sqrt. LCursor::macroModeClose replaces in this case
675 // the MathUnknownInset with name "frac" by an empty
676 // MathFracInset -> a pos value > 0 is invalid.
677 // A side effect is that an undo before the macro is finished
678 // undoes the complete macro, not only the last character.
679 if (!cur.inMacroMode())
682 // spacial handling of space. If we insert an inset
683 // via macro mode, we want to put the cursor inside it
684 // if relevant. Think typing "\frac<space>".
685 if (cmd.argument[0] == ' '
686 && cur.inMacroMode() && cur.macroName() != "\\"
687 && cur.macroModeClose()) {
688 MathAtom const atom = cur.prevAtom();
689 if (atom->asNestInset() && atom->nargs() > 0) {
691 cur.pushLeft(*cur.nextInset());
693 } else if (!interpret(cur, cmd.argument[0])) {
694 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
700 // sprintf(dispatch_buffer, "%d %d",);
704 lyxerr << "LFUN_SETXY broken!" << endl;
707 istringstream is(cmd.argument);
709 cur.setScreenPos(x, y);
713 // Special casing for superscript in case of LyX handling
715 case LFUN_CIRCUMFLEX:
716 if (cmd.argument.empty()) {
717 // do superscript if LyX handles
719 recordUndo(cur, Undo::ATOMIC);
720 script(cur, true, grabAndEraseSelection(cur));
737 case LFUN_HUNG_UMLAUT:
741 case LFUN_FREEFONT_APPLY:
742 case LFUN_FREEFONT_UPDATE:
743 handleFont2(cur, cmd.argument);
747 if (currentMode() == TEXT_MODE)
748 handleFont(cur, cmd.argument, "textbf");
750 handleFont(cur, cmd.argument, "mathbf");
753 if (currentMode() == TEXT_MODE)
754 handleFont(cur, cmd.argument, "textsf");
756 handleFont(cur, cmd.argument, "mathsf");
759 if (currentMode() == TEXT_MODE)
760 handleFont(cur, cmd.argument, "emph");
762 handleFont(cur, cmd.argument, "mathcal");
765 if (currentMode() == TEXT_MODE)
766 handleFont(cur, cmd.argument, "textrm");
768 handleFont(cur, cmd.argument, "mathrm");
771 if (currentMode() == TEXT_MODE)
772 handleFont(cur, cmd.argument, "texttt");
774 handleFont(cur, cmd.argument, "mathtt");
777 handleFont(cur, cmd.argument, "mathfrak");
780 if (currentMode() == TEXT_MODE)
781 handleFont(cur, cmd.argument, "textit");
783 handleFont(cur, cmd.argument, "mathit");
786 if (currentMode() == TEXT_MODE)
787 // FIXME: should be "noun"
788 handleFont(cur, cmd.argument, "textsc");
790 handleFont(cur, cmd.argument, "mathbb");
792 //case LFUN_FREEFONT_APPLY:
793 handleFont(cur, cmd.argument, "textrm");
796 handleFont(cur, cmd.argument, "textnormal");
799 case LFUN_MATH_MODE: {
801 // ignore math-mode on when already in math mode
802 if (currentMode() == InsetBase::MATH_MODE && cmd.argument == "on")
804 cur.macroModeClose();
805 string const save_selection = grabAndEraseSelection(cur);
807 //cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
808 cur.plainInsert(MathAtom(new MathBoxInset("mbox")));
810 cur.pushLeft(*cur.nextInset());
811 cur.niceInsert(save_selection);
813 if (currentMode() == InsetBase::TEXT_MODE) {
814 cur.niceInsert(MathAtom(new MathHullInset("simple")));
815 cur.message(_("create new math text environment ($...$)"));
817 handleFont(cur, cmd.argument, "textrm");
818 cur.message(_("entered math text mode (textrm)"));
831 case LFUN_INSERT_MATRIX: {
832 recordUndo(cur, Undo::ATOMIC);
837 istringstream is(cmd.argument);
838 is >> m >> n >> v_align >> h_align;
845 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
849 case LFUN_MATH_DELIM: {
850 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
852 string rs = lyx::support::split(cmd.argument, ls, ' ');
853 // Reasonable default values
858 recordUndo(cur, Undo::ATOMIC);
859 // Don't do this with multi-cell selections
860 if (cur.selBegin().idx() == cur.selEnd().idx())
861 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
865 case LFUN_SPACE_INSERT:
866 case LFUN_MATH_SPACE:
867 recordUndo(cur, Undo::ATOMIC);
868 cur.insert(MathAtom(new MathSpaceInset(",")));
872 // interpret this as if a backslash was typed
873 recordUndo(cur, Undo::ATOMIC);
874 interpret(cur, '\\');
878 // interpret this as if a _ was typed
879 recordUndo(cur, Undo::ATOMIC);
883 case LFUN_SUPERSCRIPT:
884 // interpret this as if a ^ was typed
885 recordUndo(cur, Undo::ATOMIC);
889 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
890 // handling such that "self-insert" works on "arbitrary stuff" too, and
891 // math-insert only handles special math things like "matrix".
892 case LFUN_INSERT_MATH: {
893 recordUndo(cur, Undo::ATOMIC);
894 if (cmd.argument == "^" || cmd.argument == "_") {
895 interpret(cur, cmd.argument[0]);
897 cur.niceInsert(cmd.argument);
901 case LFUN_DIALOG_SHOW_NEW_INSET: {
902 string const & name = cmd.argument;
906 data = tmp.createDialogStr(name);
908 cur.bv().owner()->getDialogs().show(name, data, 0);
913 MathDimInset::doDispatch(cur, cmd);
919 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
920 FuncStatus & flag) const
922 // the font related toggles
923 //string tc = "mathnormal";
925 string const arg = cmd.argument;
926 switch (cmd.action) {
927 case LFUN_TABULAR_FEATURE:
931 case LFUN_TABULAR_FEATURE:
932 // FIXME: check temporarily disabled
934 char align = mathcursor::valign();
939 if (cmd.argument.empty()) {
943 if (!contains("tcb", cmd.argument[0])) {
947 flag.setOnOff(cmd.argument[0] == align);
950 /// We have to handle them since 1.4 blocks all unhandled actions
961 case LFUN_MATH_MUTATE:
962 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
963 flag.setOnOff(false);
966 // we just need to be in math mode to enable that
968 case LFUN_MATH_SPACE:
969 case LFUN_MATH_LIMITS:
970 case LFUN_MATH_NONUMBER:
971 case LFUN_MATH_NUMBER:
972 case LFUN_MATH_EXTERN:
977 flag.enabled(currentMode() != TEXT_MODE);
980 case LFUN_INSERT_MATH: {
982 arg == "\\textbf" || arg == "\\textsf" ||
983 arg == "\\textrm" || arg == "\\textmd" ||
984 arg == "\\textit" || arg == "\\textsc" ||
985 arg == "\\textsl" || arg == "\\textup" ||
986 arg == "\\texttt" || arg == "\\textbb" ||
987 arg == "\\textnormal";
988 flag.enabled(currentMode() != TEXT_MODE || textarg);
992 case LFUN_INSERT_MATRIX:
993 flag.enabled(currentMode() == MATH_MODE);
1003 void MathNestInset::edit(LCursor & cur, bool left)
1006 cur.idx() = left ? 0 : cur.lastidx();
1007 cur.pos() = left ? 0 : cur.lastpos();
1009 //lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
1013 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
1016 int dist_min = 1000000;
1017 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1018 int const d = cell(i).dist(x, y);
1024 MathArray & ar = cell(idx_min);
1026 cur.idx() = idx_min;
1027 cur.pos() = ar.x2pos(x - ar.xo());
1028 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1029 if (dist_min == 0) {
1031 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1032 if (ar[i]->covers(x, y))
1033 return ar[i].nucleus()->editXY(cur, x, y);
1039 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1041 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1042 if (cmd.button() == mouse_button::button1) {
1043 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1044 cur.bv().mouseSetCursor(cur);
1047 if (cmd.button() == mouse_button::button2) {
1049 asArray(cur.bv().getClipboard(), ar);
1050 cur.clearSelection();
1051 editXY(cur, cmd.x, cmd.y);
1059 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1061 // only select with button 1
1062 if (cmd.button() == mouse_button::button1) {
1063 LCursor & bvcur = cur.bv().cursor();
1064 if (bvcur.anchor_.hasPart(cur)) {
1065 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1066 bvcur.setCursor(cur);
1067 bvcur.selection() = true;
1068 //lyxerr << "MOTION " << bvcur << endl;
1077 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1079 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1081 if (cmd.button() == mouse_button::button1) {
1082 //cur.bv().stuffClipboard(cur.grabSelection());
1086 if (cmd.button() == mouse_button::button3) {
1087 // try to dispatch to enclosed insets first
1088 cur.bv().owner()->getDialogs().show("mathpanel");
1096 bool MathNestInset::interpret(LCursor & cur, char c)
1098 //lyxerr << "interpret 2: '" << c << "'" << endl;
1099 string save_selection;
1100 if (c == '^' || c == '_')
1101 save_selection = grabAndEraseSelection(cur);
1106 if (cur.inMacroMode()) {
1107 string name = cur.macroName();
1109 /// are we currently typing '#1' or '#2' or...?
1110 if (name == "\\#") {
1113 if (n >= 1 && n <= 9)
1114 cur.insert(new MathMacroArgument(n));
1119 cur.activeMacro()->setName(name + c);
1123 // handle 'special char' macros
1128 if (currentMode() == MathInset::TEXT_MODE)
1129 cur.niceInsert(createMathInset("textbackslash"));
1131 cur.niceInsert(createMathInset("backslash"));
1132 } else if (c == '{') {
1134 cur.niceInsert(MathAtom(new MathBraceInset));
1135 } else if (c == '%') {
1137 cur.niceInsert(MathAtom(new MathCommentInset));
1138 } else if (c == '#') {
1139 BOOST_ASSERT(cur.activeMacro());
1140 cur.activeMacro()->setName(name + c);
1143 cur.niceInsert(createMathInset(string(1, c)));
1148 // One character big delimiters. The others are handled in
1149 // LCursor::plainInsert.
1150 latexkeys const * l = in_word_set(name.substr(1));
1151 if (name[0] == '\\' && l && l->inset == "big") {
1161 delim = string(1, c);
1164 if (MathBigInset::isBigInsetDelim(delim)) {
1165 // name + delim ared a valid MathBigInset.
1166 // We can't use cur.macroModeClose() because
1167 // it does not handle delim.
1168 MathUnknownInset * p = cur.activeMacro();
1171 cur.cell().erase(cur.pos());
1172 cur.plainInsert(MathAtom(
1173 new MathBigInset(name.substr(1), delim)));
1178 // leave macro mode and try again if necessary
1179 cur.macroModeClose();
1181 cur.niceInsert(MathAtom(new MathBraceInset));
1187 // This is annoying as one has to press <space> far too often.
1191 // leave autocorrect mode if necessary
1192 if (autocorrect() && c == ' ') {
1193 autocorrect() = false;
1198 // just clear selection on pressing the space bar
1199 if (cur.selection() && c == ' ') {
1200 cur.selection() = false;
1207 //lyxerr << "starting with macro" << endl;
1208 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1213 if (currentMode() == MathInset::TEXT_MODE)
1219 if (currentMode() == MathInset::TEXT_MODE) {
1220 // insert spaces in text mode,
1221 // but suppress direct insertion of two spaces in a row
1222 // the still allows typing '<space>a<space>' and deleting the 'a', but
1223 // it is better than nothing...
1224 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1228 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1229 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1234 // if are at the very end, leave the formula
1235 return cur.pos() != cur.lastpos();
1238 // These shouldn't work in text mode:
1239 if (currentMode() != MathInset::TEXT_MODE) {
1241 script(cur, false, save_selection);
1245 script(cur, true, save_selection);
1249 cur.niceInsert(createMathInset("sim"));
1254 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%'
1255 || c == '_' || c == '^') {
1256 cur.niceInsert(createMathInset(string(1, c)));
1261 // try auto-correction
1262 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1265 // no special circumstances, so insert the character without any fuss
1267 cur.autocorrect() = true;
1272 bool MathNestInset::script(LCursor & cur, bool up, string const &
1275 // Hack to get \^ and \_ working
1276 lyxerr << "handling script: up: " << up << endl;
1277 if (cur.inMacroMode() && cur.macroName() == "\\") {
1279 cur.niceInsert(createMathInset("mathcircumflex"));
1281 interpret(cur, '_');
1285 cur.macroModeClose();
1286 if (asScriptInset() && cur.idx() == 0) {
1287 // we are in a nucleus of a script inset, move to _our_ script
1288 MathScriptInset * inset = asScriptInset();
1289 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1291 cur.idx() = inset->idxOfScript(up);
1293 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1295 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1298 cur.idx() = inset->idxOfScript(up);
1299 cur.pos() = cur.lastpos();
1301 // convert the thing to our left to a scriptinset or create a new
1302 // one if in the very first position of the array
1303 if (cur.pos() == 0) {
1304 //lyxerr << "new scriptinset" << endl;
1305 cur.insert(new MathScriptInset(up));
1307 //lyxerr << "converting prev atom " << endl;
1308 cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1311 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1316 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1317 cur.niceInsert(save_selection);
1319 //lyxerr << "inserting selection 2:\n" << save_selection << endl;