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_data.h"
17 #include "math_deliminset.h"
18 #include "math_factory.h"
19 #include "math_hullinset.h"
20 #include "math_mathmlstream.h"
21 #include "math_parser.h"
22 #include "math_spaceinset.h"
23 #include "math_support.h"
24 #include "math_mboxinset.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
30 #include "dispatchresult.h"
31 #include "funcrequest.h"
36 #include "support/std_sstream.h"
37 #include "support/lstrings.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
46 using std::istringstream;
60 MathNestInset::MathNestInset(idx_type nargs)
61 : cells_(nargs), lock_(false)
65 MathInset::idx_type MathNestInset::nargs() const
71 MathArray & MathNestInset::cell(idx_type i)
77 MathArray const & MathNestInset::cell(idx_type i) const
83 void MathNestInset::getCursorPos(CursorSlice const & cur,
84 int & x, int & y) const
86 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
87 MathArray const & ar = cur.cell();
88 x = ar.xo() + ar.pos2x(cur.pos());
90 // move cursor visually into empty cells ("blue rectangles");
91 if (cur.cell().empty())
96 void MathNestInset::substitute(MathMacro const & m)
98 for (idx_type i = 0; i < nargs(); ++i)
99 cell(i).substitute(m);
103 void MathNestInset::metrics(MetricsInfo const & mi) const
106 for (idx_type i = 0; i < nargs(); ++i)
111 bool MathNestInset::idxNext(LCursor & cur) const
113 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
114 if (cur.idx() == cur.lastidx())
122 bool MathNestInset::idxRight(LCursor & cur) const
128 bool MathNestInset::idxPrev(LCursor & cur) const
130 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
134 cur.pos() = cur.lastpos();
139 bool MathNestInset::idxLeft(LCursor & cur) const
145 bool MathNestInset::idxFirst(LCursor & cur) const
147 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
156 bool MathNestInset::idxLast(LCursor & cur) const
158 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
161 cur.idx() = cur.lastidx();
162 cur.pos() = cur.lastpos();
167 void MathNestInset::dump() const
169 WriteStream os(lyxerr);
170 os << "---------------------------------------------\n";
173 for (idx_type i = 0; i < nargs(); ++i)
174 os << cell(i) << "\n";
175 os << "---------------------------------------------\n";
179 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
180 void MathNestInset::draw(PainterInfo &, int, int) const
184 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
190 void MathNestInset::drawSelection(PainterInfo & pi, int, int) const
192 // this should use the x/y values given, not the cached values
193 LCursor & cur = pi.base.bv->cursor();
194 if (!cur.selection())
196 if (!ptr_cmp(cur.inset(), this))
198 CursorSlice & s1 = cur.selBegin();
199 CursorSlice & s2 = cur.selEnd();
200 if (s1.idx() == s2.idx()) {
201 MathArray const & c = cell(s1.idx());
202 int x1 = c.xo() + c.pos2x(s1.pos());
203 int y1 = c.yo() - c.ascent();
204 int x2 = c.xo() + c.pos2x(s2.pos());
205 int y2 = c.yo() + c.descent();
206 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
208 for (idx_type i = 0; i < nargs(); ++i) {
209 if (idxBetween(i, s1.idx(), s2.idx())) {
210 MathArray const & c = cell(i);
212 int y1 = c.yo() - c.ascent();
213 int x2 = c.xo() + c.width();
214 int y2 = c.yo() + c.descent();
215 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
222 void MathNestInset::validate(LaTeXFeatures & features) const
224 for (idx_type i = 0; i < nargs(); ++i)
225 cell(i).validate(features);
229 void MathNestInset::replace(ReplaceData & rep)
231 for (idx_type i = 0; i < nargs(); ++i)
232 cell(i).replace(rep);
236 bool MathNestInset::contains(MathArray const & ar) const
238 for (idx_type i = 0; i < nargs(); ++i)
239 if (cell(i).contains(ar))
245 bool MathNestInset::lock() const
251 void MathNestInset::lock(bool l)
257 bool MathNestInset::isActive() const
263 MathArray MathNestInset::glue() const
266 for (size_t i = 0; i < nargs(); ++i)
272 void MathNestInset::write(WriteStream & os) const
274 os << '\\' << name().c_str();
275 for (size_t i = 0; i < nargs(); ++i)
276 os << '{' << cell(i) << '}';
278 os.pendingSpace(true);
279 if (lock_ && !os.latex()) {
281 os.pendingSpace(true);
286 void MathNestInset::normalize(NormalStream & os) const
288 os << '[' << name().c_str();
289 for (size_t i = 0; i < nargs(); ++i)
290 os << ' ' << cell(i);
295 void MathNestInset::notifyCursorLeaves(idx_type idx)
297 cell(idx).notifyCursorLeaves();
301 void MathNestInset::handleFont
302 (LCursor & cur, string const & arg, string const & font)
304 // this whole function is a hack and won't work for incremental font
306 //recordUndo(cur, Undo::ATOMIC);
308 if (cur.inset()->asMathInset()->name() == font)
309 cur.handleFont(font);
311 cur.handleNest(createMathInset(font));
317 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
319 //recordUndo(cur, Undo::ATOMIC);
322 bv_funcs::string2font(arg, font, b);
323 if (font.color() != LColor::inherit) {
324 MathAtom at = createMathInset("color");
325 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
326 cur.handleNest(at, 1);
331 void MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
333 lyxerr << "MathNestInset: request: " << cmd << std::endl;
334 CursorSlice sl = cur.current();
336 switch (cmd.action) {
339 if (!cmd.argument.empty()) {
341 mathed_parse_cell(ar, cmd.argument);
342 cur.cell().insert(cur.pos(), ar);
343 cur.pos() += ar.size();
349 istringstream is(cmd.argument.c_str());
352 cur.macroModeClose();
353 //recordUndo(cur, Undo::ATOMIC);
359 case LFUN_PASTESELECTION:
360 dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard()));
363 case LFUN_MOUSE_PRESS:
364 lfunMousePress(cur, cmd);
367 case LFUN_MOUSE_MOTION:
368 lfunMouseMotion(cur, cmd);
371 case LFUN_MOUSE_RELEASE:
372 lfunMouseRelease(cur, cmd);
375 case LFUN_MOUSE_DOUBLE:
376 case LFUN_MOUSE_TRIPLE:
377 //lyxerr << "Mouse double" << endl;
378 //lyxerr << "Mouse triple" << endl;
379 dispatch(cur, FuncRequest(LFUN_WORDSEL));
382 case LFUN_FINISHED_LEFT:
383 cur.pop(cur.currentDepth());
384 cur.bv().cursor() = cur;
387 case LFUN_FINISHED_RIGHT:
388 cur.pop(cur.currentDepth());
390 cur.bv().cursor() = cur;
393 case LFUN_FINISHED_UP:
394 cur.pop(cur.currentDepth());
395 //idxUpDown(cur, true);
396 cur.bv().cursor() = cur;
399 case LFUN_FINISHED_DOWN:
400 cur.pop(cur.currentDepth());
401 //idxUpDown(cur, false);
402 cur.bv().cursor() = cur;
407 lyxerr << "mathnest RIGHT: from:\n" << cur << endl;
408 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
409 cur.autocorrect() = false;
411 if (cur.inMacroMode())
412 cur.macroModeClose();
413 else if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
414 cur.pushLeft(cur.nextAtom().nucleus());
415 cur.inset()->idxFirst(cur);
416 } else if (cur.posRight() || idxRight(cur)
417 || cur.popRight() || cur.selection())
420 cur.dispatched(FINISHED_RIGHT);
421 lyxerr << "mathnest RIGHT: to:\n" << cur << endl;
426 cur.selHandle(cmd.action == LFUN_LEFTSEL);
427 cur.autocorrect() = false;
429 if (cur.inMacroMode())
430 cur.macroModeClose();
431 else if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
433 cur.push(cur.nextAtom().nucleus());
434 cur.inset()->idxLast(cur);
435 } else if (cur.posLeft() || idxLeft(cur)
436 || cur.popLeft() || cur.selection())
439 cur.dispatched(FINISHED_LEFT);
444 cur.selHandle(cmd.action == LFUN_UPSEL);
446 cur.dispatched(FINISHED_UP);
451 cur.selHandle(cmd.action == LFUN_DOWNSEL);
453 cur.dispatched(FINISHED_DOWN);
460 cur.selection() = true;
461 cur.pos() = cur.lastpos();
462 cur.idx() = cur.lastidx();
465 case LFUN_UP_PARAGRAPHSEL:
466 case LFUN_UP_PARAGRAPH:
467 case LFUN_DOWN_PARAGRAPHSEL:
468 case LFUN_DOWN_PARAGRAPH:
473 case LFUN_WORDLEFTSEL:
475 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
476 cur.macroModeClose();
477 if (cur.pos() != 0) {
479 } else if (cur.col() != 0) {
480 cur.idx() -= cur.col();
482 } else if (cur.idx() != 0) {
486 cur.dispatched(FINISHED_LEFT);
490 case LFUN_WORDRIGHTSEL:
494 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
495 cur.macroModeClose();
497 if (cur.pos() != cur.lastpos()) {
498 cur.pos() = cur.lastpos();
499 } else if (cur.col() != cur.lastcol()) {
500 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
501 cur.pos() = cur.lastpos();
502 } else if (cur.idx() != cur.lastidx()) {
503 cur.idx() = cur.lastidx();
504 cur.pos() = cur.lastpos();
506 cur.dispatched(FINISHED_RIGHT);
512 case LFUN_BEGINNINGBUFSEL:
513 case LFUN_BEGINNINGBUF:
514 cur.dispatched(FINISHED_LEFT);
521 cur.dispatched(FINISHED_RIGHT);
524 case LFUN_CELL_FORWARD:
525 cur.inset()->idxNext(cur);
528 case LFUN_CELL_BACKWARD:
529 cur.inset()->idxPrev(cur);
532 case LFUN_DELETE_WORD_BACKWARD:
534 //recordUndo(cur, Undo::ATOMIC);
538 case LFUN_DELETE_WORD_FORWARD:
540 //recordUndo(cur, Undo::ATOMIC);
542 cur.dispatched(FINISHED_LEFT);
549 cur.dispatched(FINISHED_LEFT);
552 case LFUN_INSET_TOGGLE:
556 case LFUN_SELFINSERT:
557 if (cmd.argument.empty()) {
558 cur.dispatched(FINISHED_RIGHT);
561 //recordUndo(cur, Undo::ATOMIC);
562 if (cmd.argument.size() != 1) {
563 cur.insert(cmd.argument);
566 if (!cur.interpret(cmd.argument[0]))
567 cur.dispatched(FINISHED_RIGHT);
572 // this needs to be incorporated
574 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
575 bool remove_inset = false;
577 DispatchResult result(true);
578 bool was_macro = cur.inMacroMode();
585 // sprintf(dispatch_buffer, "%d %d",);
586 // DispatchResult= dispatch_buffer;
589 lyxerr << "LFUN_SETXY broken!" << endl;
592 istringstream is(cmd.argument.c_str());
594 cur.setScreenPos(x, y);
599 //recordUndo(cur, Undo::DELETE);
607 // Special casing for superscript in case of LyX handling
609 case LFUN_CIRCUMFLEX:
610 if (cmd.argument.empty()) {
611 // do superscript if LyX handles
613 //recordUndo(cur, Undo::ATOMIC);
631 case LFUN_HUNG_UMLAUT:
635 case LFUN_FREEFONT_APPLY:
636 case LFUN_FREEFONT_UPDATE:
637 handleFont2(cur, cmd.argument);
641 handleFont(cur, cmd.argument, "mathbf");
644 handleFont(cur, cmd.argument, "mathsf");
647 handleFont(cur, cmd.argument, "mathcal");
650 handleFont(cur, cmd.argument, "mathrm");
653 handleFont(cur, cmd.argument, "texttt");
656 handleFont(cur, cmd.argument, "mathfrak");
659 handleFont(cur, cmd.argument, "mathit");
662 handleFont(cur, cmd.argument, "mathbb");
664 //case LFUN_FREEFONT_APPLY:
665 handleFont(cur, cmd.argument, "textrm");
668 handleFont(cur, cmd.argument, "textnormal");
673 cur.macroModeClose();
675 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
677 cur.pushLeft(cur.nextInset());
679 if (cur.currentMode() == InsetBase::TEXT_MODE)
680 cur.niceInsert(MathAtom(new MathHullInset("simple")));
682 handleFont(cur, cmd.argument, "textrm");
683 //cur.owner()->message(_("math text mode toggled"));
690 //recordUndo(cur, Undo::ATOMIC);
696 case LFUN_INSERT_MATRIX: {
697 //recordUndo(cur, Undo::ATOMIC);
702 istringstream is(cmd.argument);
703 is >> m >> n >> v_align >> h_align;
710 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
714 case LFUN_MATH_DELIM: {
715 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
717 string rs = lyx::support::split(cmd.argument, ls, ' ');
718 // Reasonable default values
723 //recordUndo(cur, Undo::ATOMIC);
724 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
728 case LFUN_SPACE_INSERT:
729 case LFUN_MATH_SPACE:
730 //recordUndo(cur, Undo::ATOMIC);
731 cur.insert(MathAtom(new MathSpaceInset(",")));
736 //cur.bv().owner()->message(_("Invalid action in math mode!"));
740 // interpret this as if a backslash was typed
741 //recordUndo(cur, Undo::ATOMIC);
745 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
746 // handling such that "self-insert" works on "arbitrary stuff" too, and
747 // math-insert only handles special math things like "matrix".
748 case LFUN_INSERT_MATH:
749 //recordUndo(cur, Undo::ATOMIC);
750 cur.niceInsert(cmd.argument);
753 case LFUN_DIALOG_SHOW_NEW_INSET: {
754 string const & name = cmd.argument;
759 data = tmp.createDialogStr(name);
762 cur.bv().owner()->getDialogs().show(name, data, 0);
766 case LFUN_INSET_APPLY: {
767 string const name = cmd.getArg(0);
768 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
771 base->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
775 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
786 case LFUN_WORD_REPLACE:
788 if (!searchForward(&cur.bv(), cmd.getArg(0), false, false))
795 BOOST_ASSERT(cur.inMathed());
797 if (result.dispatched()) {
799 cur.bv().stuffClipboard(cur.grabSelection());
802 cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
808 MathDimInset::priv_dispatch(cur, cmd);
814 void MathNestInset::edit(LCursor & cur, bool left)
817 cur.idx() = left ? 0 : cur.lastidx();
818 cur.pos() = left ? 0 : cur.lastpos();
823 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
826 int dist_min = 1000000;
827 for (idx_type i = 0; i < nargs(); ++i) {
828 int d = cell(i).dist(x, y);
834 MathArray & ar = cell(idx_min);
837 cur.pos() = ar.x2pos(x - ar.xo());
838 lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
841 for (pos_type i = 0, n = ar.size(); i < n; ++i)
842 if (ar[i]->covers(x, y))
843 return ar[i].nucleus()->editXY(cur, x, y);
849 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
851 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
853 if (cmd.button() == mouse_button::button1) {
854 // try to dispatch to enclosed insets first
855 //cur.bv().stuffClipboard(cur.grabSelection());
859 if (cmd.button() == mouse_button::button2) {
861 asArray(cur.bv().getClipboard(), ar);
863 cur.setScreenPos(cmd.x, cmd.y);
869 if (cmd.button() == mouse_button::button3) {
870 // try to dispatch to enclosed insets first
871 cur.bv().owner()->getDialogs().show("mathpanel");
879 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
881 lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
882 if (cmd.button() == mouse_button::button1) {
886 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
887 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
888 cur.bv().cursor() = cur;
891 if (cmd.button() == mouse_button::button2) {
892 priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
897 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
899 // only select with button 1
900 if (cmd.button() != mouse_button::button1)
903 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
909 if (!cur.selection())
912 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
913 cur.bv().cursor().cursor_ = cur.cursor_;
914 cur.bv().cursor().selection() = true;