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 bool MathNestInset::idxHome(LCursor & cur) const
169 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
177 bool MathNestInset::idxEnd(LCursor & cur) const
179 BOOST_ASSERT(ptr_cmp(cur.inset(), this));
180 if (cur.lastpos() == cur.lastpos())
182 cur.pos() = cur.lastpos();
187 void MathNestInset::dump() const
189 WriteStream os(lyxerr);
190 os << "---------------------------------------------\n";
193 for (idx_type i = 0; i < nargs(); ++i)
194 os << cell(i) << "\n";
195 os << "---------------------------------------------\n";
199 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
200 void MathNestInset::draw(PainterInfo &, int, int) const
204 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
210 void MathNestInset::drawSelection(PainterInfo & pi, int, int) const
212 // this should use the x/y values given, not the cached values
213 LCursor & cur = pi.base.bv->cursor();
214 if (!cur.selection())
216 if (!ptr_cmp(cur.inset(), this))
218 CursorSlice & s1 = cur.selBegin();
219 CursorSlice & s2 = cur.selEnd();
220 if (s1.idx() == s2.idx()) {
221 MathArray const & c = cell(s1.idx());
222 int x1 = c.xo() + c.pos2x(s1.pos());
223 int y1 = c.yo() - c.ascent();
224 int x2 = c.xo() + c.pos2x(s2.pos());
225 int y2 = c.yo() + c.descent();
226 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
228 for (idx_type i = 0; i < nargs(); ++i) {
229 if (idxBetween(i, s1.idx(), s2.idx())) {
230 MathArray const & c = cell(i);
232 int y1 = c.yo() - c.ascent();
233 int x2 = c.xo() + c.width();
234 int y2 = c.yo() + c.descent();
235 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
242 void MathNestInset::validate(LaTeXFeatures & features) const
244 for (idx_type i = 0; i < nargs(); ++i)
245 cell(i).validate(features);
249 void MathNestInset::replace(ReplaceData & rep)
251 for (idx_type i = 0; i < nargs(); ++i)
252 cell(i).replace(rep);
256 bool MathNestInset::contains(MathArray const & ar) const
258 for (idx_type i = 0; i < nargs(); ++i)
259 if (cell(i).contains(ar))
265 bool MathNestInset::lock() const
271 void MathNestInset::lock(bool l)
277 bool MathNestInset::isActive() const
283 MathArray MathNestInset::glue() const
286 for (size_t i = 0; i < nargs(); ++i)
292 void MathNestInset::write(WriteStream & os) const
294 os << '\\' << name().c_str();
295 for (size_t i = 0; i < nargs(); ++i)
296 os << '{' << cell(i) << '}';
298 os.pendingSpace(true);
299 if (lock_ && !os.latex()) {
301 os.pendingSpace(true);
306 void MathNestInset::normalize(NormalStream & os) const
308 os << '[' << name().c_str();
309 for (size_t i = 0; i < nargs(); ++i)
310 os << ' ' << cell(i);
315 void MathNestInset::notifyCursorLeaves(idx_type idx)
317 cell(idx).notifyCursorLeaves();
321 void MathNestInset::handleFont
322 (LCursor & cur, string const & arg, string const & font)
324 // this whole function is a hack and won't work for incremental font
326 //recordUndo(cur, Undo::ATOMIC);
328 if (cur.inset()->asMathInset()->name() == font)
329 cur.handleFont(font);
331 cur.handleNest(createMathInset(font));
337 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
339 //recordUndo(cur, Undo::ATOMIC);
342 bv_funcs::string2font(arg, font, b);
343 if (font.color() != LColor::inherit) {
344 MathAtom at = createMathInset("color");
345 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
346 cur.handleNest(at, 1);
352 MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
354 lyxerr << "MathNestInset: request: " << cmd << std::endl;
356 switch (cmd.action) {
359 if (!cmd.argument.empty()) {
361 mathed_parse_cell(ar, cmd.argument);
362 cur.cell().insert(cur.pos(), ar);
363 cur.pos() += ar.size();
365 return DispatchResult(true, true);
369 istringstream is(cmd.argument.c_str());
372 cur.macroModeClose();
373 //recordUndo(cur, Undo::ATOMIC);
375 return DispatchResult(true, true);
379 case LFUN_PASTESELECTION:
380 return dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard()));
382 case LFUN_MOUSE_PRESS:
383 return lfunMousePress(cur, cmd);
384 case LFUN_MOUSE_MOTION:
385 return lfunMouseMotion(cur, cmd);
386 case LFUN_MOUSE_RELEASE:
387 return lfunMouseRelease(cur, cmd);
388 case LFUN_MOUSE_DOUBLE:
389 return dispatch(cur, FuncRequest(LFUN_WORDSEL));
393 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
395 DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
399 cur.selHandle(cmd.action == LFUN_LEFTSEL);
401 DispatchResult(true, true) : DispatchResult(false, FINISHED);
405 cur.selHandle(cmd.action == LFUN_UPSEL);
407 DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
411 cur.selHandle(cmd.action == LFUN_DOWNSEL);
413 DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
418 cur.selection() = true;
420 return DispatchResult(true, true);
422 case LFUN_UP_PARAGRAPHSEL:
423 case LFUN_UP_PARAGRAPH:
424 case LFUN_DOWN_PARAGRAPHSEL:
425 case LFUN_DOWN_PARAGRAPH:
426 return DispatchResult(true, FINISHED);
428 case LFUN_WORDLEFTSEL:
430 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL);
432 ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
434 case LFUN_WORDRIGHTSEL:
436 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL);
438 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
442 cur.selHandle(cmd.action == LFUN_HOMESEL);
444 ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
448 cur.selHandle(cmd.action == LFUN_ENDSEL);
450 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
454 case LFUN_BEGINNINGBUFSEL:
455 case LFUN_BEGINNINGBUF:
456 return DispatchResult(true, FINISHED);
462 return DispatchResult(false, FINISHED_RIGHT);
464 case LFUN_CELL_FORWARD:
465 cur.inset()->idxNext(cur);
466 return DispatchResult(true, true);
468 case LFUN_CELL_BACKWARD:
469 cur.inset()->idxPrev(cur);
470 return DispatchResult(true, true);
472 case LFUN_DELETE_WORD_BACKWARD:
474 //recordUndo(cur, Undo::ATOMIC);
476 return DispatchResult(true, true);
478 case LFUN_DELETE_WORD_FORWARD:
480 //recordUndo(cur, Undo::ATOMIC);
482 return DispatchResult(true, FINISHED);
485 if (!cur.selection())
486 return DispatchResult(true, true);
488 return DispatchResult(false);
490 case LFUN_INSET_TOGGLE:
492 return DispatchResult(true, true);
494 case LFUN_SELFINSERT:
495 if (!cmd.argument.empty()) {
496 //recordUndo(cur, Undo::ATOMIC);
497 if (cmd.argument.size() == 1) {
498 if (cur.interpret(cmd.argument[0]))
499 return DispatchResult(true, true);
501 return DispatchResult(false, FINISHED_RIGHT);
503 cur.insert(cmd.argument);
505 return DispatchResult(false, FINISHED_RIGHT);
510 // this needs to be incorporated
512 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
513 bool remove_inset = false;
515 DispatchResult result(true);
516 bool was_macro = cur.inMacroMode();
523 // sprintf(dispatch_buffer, "%d %d",);
524 // DispatchResult= dispatch_buffer;
527 lyxerr << "LFUN_SETXY broken!" << endl;
530 istringstream is(cmd.argument.c_str());
532 cur.setScreenPos(x, y);
533 return DispatchResult(true, true);
537 //recordUndo(cur, Undo::DELETE);
539 return DispatchResult(true, true);
543 return DispatchResult(true, true);
546 // Special casing for superscript in case of LyX handling
548 case LFUN_CIRCUMFLEX:
549 if (cmd.argument.empty()) {
550 // do superscript if LyX handles
552 //recordUndo(cur, Undo::ATOMIC);
555 return DispatchResult(true, true);
570 case LFUN_HUNG_UMLAUT:
571 return DispatchResult(true, true);
574 case LFUN_FREEFONT_APPLY:
575 case LFUN_FREEFONT_UPDATE:
576 handleFont2(cur, cmd.argument);
577 return DispatchResult(true, true);
580 handleFont(cur, cmd.argument, "mathbf");
581 return DispatchResult(true, true);
583 handleFont(cur, cmd.argument, "mathsf");
584 return DispatchResult(true, true);
586 handleFont(cur, cmd.argument, "mathcal");
587 return DispatchResult(true, true);
589 handleFont(cur, cmd.argument, "mathrm");
590 return DispatchResult(true, true);
592 handleFont(cur, cmd.argument, "texttt");
593 return DispatchResult(true, true);
595 handleFont(cur, cmd.argument, "mathfrak");
596 return DispatchResult(true, true);
598 handleFont(cur, cmd.argument, "mathit");
599 return DispatchResult(true, true);
601 handleFont(cur, cmd.argument, "mathbb");
602 return DispatchResult(true, true);
603 //case LFUN_FREEFONT_APPLY:
604 handleFont(cur, cmd.argument, "textrm");
605 return DispatchResult(true, true);
607 handleFont(cur, cmd.argument, "textnormal");
608 return DispatchResult(true, true);
612 cur.macroModeClose();
614 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
616 cur.pushLeft(cur.nextInset());
618 if (cur.currentMode() == InsetBase::TEXT_MODE)
619 cur.niceInsert(MathAtom(new MathHullInset("simple")));
621 handleFont(cur, cmd.argument, "textrm");
622 //cur.owner()->message(_("math text mode toggled"));
624 return DispatchResult(true, true);
629 //recordUndo(cur, Undo::ATOMIC);
633 return DispatchResult(true, true);
635 case LFUN_INSERT_MATRIX: {
636 //recordUndo(cur, Undo::ATOMIC);
641 istringstream is(cmd.argument);
642 is >> m >> n >> v_align >> h_align;
649 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
650 return DispatchResult(true, true);
653 case LFUN_MATH_DELIM: {
654 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
656 string rs = lyx::support::split(cmd.argument, ls, ' ');
657 // Reasonable default values
662 //recordUndo(cur, Undo::ATOMIC);
663 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
664 return DispatchResult(true, true);
667 case LFUN_SPACE_INSERT:
668 case LFUN_MATH_SPACE:
669 //recordUndo(cur, Undo::ATOMIC);
670 cur.insert(MathAtom(new MathSpaceInset(",")));
671 return DispatchResult(true, true);
675 //cur.bv().owner()->message(_("Invalid action in math mode!"));
676 return DispatchResult(true, true);
679 // interpret this as if a backslash was typed
680 //recordUndo(cur, Undo::ATOMIC);
682 return DispatchResult(true, true);
684 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
685 // handling such that "self-insert" works on "arbitrary stuff" too, and
686 // math-insert only handles special math things like "matrix".
687 case LFUN_INSERT_MATH:
688 //recordUndo(cur, Undo::ATOMIC);
689 cur.niceInsert(cmd.argument);
690 return DispatchResult(true, true);
692 case LFUN_DIALOG_SHOW:
693 return DispatchResult(false);
695 case LFUN_DIALOG_SHOW_NEW_INSET: {
696 string const & name = cmd.argument;
701 data = tmp.createDialogStr(name);
705 return DispatchResult(false);
706 cur.bv().owner()->getDialogs().show(name, data, 0);
707 return DispatchResult(true, true);
710 case LFUN_INSET_APPLY: {
711 string const name = cmd.getArg(0);
712 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
715 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
716 return base->dispatch(cur, fr);
719 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
721 return DispatchResult(true, true);
723 return DispatchResult(false);
729 case LFUN_WORD_REPLACE:
732 searchForward(&cur.bv(), cmd.getArg(0), false, false)
733 ? DispatchResult(true, true) : DispatchResult(false);
738 BOOST_ASSERT(cur.inMathed());
740 if (result.dispatched()) {
742 cur.bv().stuffClipboard(cur.grabSelection());
744 cur.releaseMathCursor();
746 cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
749 return result; // original version
753 return MathDimInset::priv_dispatch(cur, cmd);
758 void MathNestInset::edit(LCursor & cur, bool left)
761 cur.idx() = left ? 0 : cur.lastidx();
762 cur.pos() = left ? 0 : cur.lastpos();
767 void MathNestInset::edit(LCursor & cur, int x, int y)
770 int dist_min = 1000000;
771 for (idx_type i = 0; i < nargs(); ++i) {
772 int d = cell(i).dist(x, y);
778 MathArray & ar = cell(idx_min);
781 cur.pos() = ar.x2pos(x - ar.xo());
782 lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
785 for (pos_type i = 0, n = ar.size(); i < n; ++i)
786 if (ar[i]->covers(x, y))
787 ar[i].nucleus()->edit(cur, x, y);
793 MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
795 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
797 if (cmd.button() == mouse_button::button1) {
798 // try to dispatch to enclosed insets first
799 //cur.bv().stuffClipboard(cur.grabSelection());
800 return DispatchResult(true, true);
803 if (cmd.button() == mouse_button::button2) {
805 asArray(cur.bv().getClipboard(), ar);
807 cur.setScreenPos(cmd.x, cmd.y);
810 return DispatchResult(true, true);
813 if (cmd.button() == mouse_button::button3) {
814 // try to dispatch to enclosed insets first
815 cur.bv().owner()->getDialogs().show("mathpanel");
816 return DispatchResult(true, true);
819 return DispatchResult(false);
824 MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
826 lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
827 if (cmd.button() == mouse_button::button1) {
831 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
832 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
833 cur.bv().cursor() = cur;
834 return DispatchResult(true, true);
837 if (cmd.button() == mouse_button::button2) {
838 return priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
841 if (cmd.button() == mouse_button::button3) {
842 return DispatchResult(true, true);
845 return DispatchResult(true, true);
850 MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
852 // only select with button 1
853 if (cmd.button() != mouse_button::button1)
854 return DispatchResult(true, true);
856 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
857 return DispatchResult(true, true);
862 if (!cur.selection())
865 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
866 cur.bv().cursor().cursor_ = cur.cursor_;
867 cur.bv().cursor().selection() = true;
868 return DispatchResult(true, true);