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 case LFUN_MOUSE_TRIPLE:
390 //lyxerr << "Mouse double" << endl;
391 //lyxerr << "Mouse triple" << endl;
392 return dispatch(cur, FuncRequest(LFUN_WORDSEL));
396 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
398 DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
402 cur.selHandle(cmd.action == LFUN_LEFTSEL);
404 DispatchResult(true, true) : DispatchResult(false, FINISHED);
408 cur.selHandle(cmd.action == LFUN_UPSEL);
410 DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
414 cur.selHandle(cmd.action == LFUN_DOWNSEL);
416 DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
421 cur.selection() = true;
423 return DispatchResult(true, true);
425 case LFUN_UP_PARAGRAPHSEL:
426 case LFUN_UP_PARAGRAPH:
427 case LFUN_DOWN_PARAGRAPHSEL:
428 case LFUN_DOWN_PARAGRAPH:
429 return DispatchResult(true, FINISHED);
431 case LFUN_WORDLEFTSEL:
433 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL);
435 ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
437 case LFUN_WORDRIGHTSEL:
439 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL);
441 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
445 cur.selHandle(cmd.action == LFUN_HOMESEL);
447 ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
451 cur.selHandle(cmd.action == LFUN_ENDSEL);
453 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
457 case LFUN_BEGINNINGBUFSEL:
458 case LFUN_BEGINNINGBUF:
459 return DispatchResult(true, FINISHED);
465 return DispatchResult(false, FINISHED_RIGHT);
467 case LFUN_CELL_FORWARD:
468 cur.inset()->idxNext(cur);
469 return DispatchResult(true, true);
471 case LFUN_CELL_BACKWARD:
472 cur.inset()->idxPrev(cur);
473 return DispatchResult(true, true);
475 case LFUN_DELETE_WORD_BACKWARD:
477 //recordUndo(cur, Undo::ATOMIC);
479 return DispatchResult(true, true);
481 case LFUN_DELETE_WORD_FORWARD:
483 //recordUndo(cur, Undo::ATOMIC);
485 return DispatchResult(true, FINISHED);
488 if (!cur.selection())
489 return DispatchResult(true, true);
491 return DispatchResult(false);
493 case LFUN_INSET_TOGGLE:
495 return DispatchResult(true, true);
497 case LFUN_SELFINSERT:
498 if (!cmd.argument.empty()) {
499 //recordUndo(cur, Undo::ATOMIC);
500 if (cmd.argument.size() == 1) {
501 if (cur.interpret(cmd.argument[0]))
502 return DispatchResult(true, true);
504 return DispatchResult(false, FINISHED_RIGHT);
506 cur.insert(cmd.argument);
508 return DispatchResult(false, FINISHED_RIGHT);
513 // this needs to be incorporated
515 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
516 bool remove_inset = false;
518 DispatchResult result(true);
519 bool was_macro = cur.inMacroMode();
526 // sprintf(dispatch_buffer, "%d %d",);
527 // DispatchResult= dispatch_buffer;
530 lyxerr << "LFUN_SETXY broken!" << endl;
533 istringstream is(cmd.argument.c_str());
535 cur.setScreenPos(x, y);
536 return DispatchResult(true, true);
540 //recordUndo(cur, Undo::DELETE);
542 return DispatchResult(true, true);
546 return DispatchResult(true, true);
549 // Special casing for superscript in case of LyX handling
551 case LFUN_CIRCUMFLEX:
552 if (cmd.argument.empty()) {
553 // do superscript if LyX handles
555 //recordUndo(cur, Undo::ATOMIC);
558 return DispatchResult(true, true);
573 case LFUN_HUNG_UMLAUT:
574 return DispatchResult(true, true);
577 case LFUN_FREEFONT_APPLY:
578 case LFUN_FREEFONT_UPDATE:
579 handleFont2(cur, cmd.argument);
580 return DispatchResult(true, true);
583 handleFont(cur, cmd.argument, "mathbf");
584 return DispatchResult(true, true);
586 handleFont(cur, cmd.argument, "mathsf");
587 return DispatchResult(true, true);
589 handleFont(cur, cmd.argument, "mathcal");
590 return DispatchResult(true, true);
592 handleFont(cur, cmd.argument, "mathrm");
593 return DispatchResult(true, true);
595 handleFont(cur, cmd.argument, "texttt");
596 return DispatchResult(true, true);
598 handleFont(cur, cmd.argument, "mathfrak");
599 return DispatchResult(true, true);
601 handleFont(cur, cmd.argument, "mathit");
602 return DispatchResult(true, true);
604 handleFont(cur, cmd.argument, "mathbb");
605 return DispatchResult(true, true);
606 //case LFUN_FREEFONT_APPLY:
607 handleFont(cur, cmd.argument, "textrm");
608 return DispatchResult(true, true);
610 handleFont(cur, cmd.argument, "textnormal");
611 return DispatchResult(true, true);
615 cur.macroModeClose();
617 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
619 cur.pushLeft(cur.nextInset());
621 if (cur.currentMode() == InsetBase::TEXT_MODE)
622 cur.niceInsert(MathAtom(new MathHullInset("simple")));
624 handleFont(cur, cmd.argument, "textrm");
625 //cur.owner()->message(_("math text mode toggled"));
627 return DispatchResult(true, true);
632 //recordUndo(cur, Undo::ATOMIC);
636 return DispatchResult(true, true);
638 case LFUN_INSERT_MATRIX: {
639 //recordUndo(cur, Undo::ATOMIC);
644 istringstream is(cmd.argument);
645 is >> m >> n >> v_align >> h_align;
652 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
653 return DispatchResult(true, true);
656 case LFUN_MATH_DELIM: {
657 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
659 string rs = lyx::support::split(cmd.argument, ls, ' ');
660 // Reasonable default values
665 //recordUndo(cur, Undo::ATOMIC);
666 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
667 return DispatchResult(true, true);
670 case LFUN_SPACE_INSERT:
671 case LFUN_MATH_SPACE:
672 //recordUndo(cur, Undo::ATOMIC);
673 cur.insert(MathAtom(new MathSpaceInset(",")));
674 return DispatchResult(true, true);
678 //cur.bv().owner()->message(_("Invalid action in math mode!"));
679 return DispatchResult(true, true);
682 // interpret this as if a backslash was typed
683 //recordUndo(cur, Undo::ATOMIC);
685 return DispatchResult(true, true);
687 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
688 // handling such that "self-insert" works on "arbitrary stuff" too, and
689 // math-insert only handles special math things like "matrix".
690 case LFUN_INSERT_MATH:
691 //recordUndo(cur, Undo::ATOMIC);
692 cur.niceInsert(cmd.argument);
693 return DispatchResult(true, true);
695 case LFUN_DIALOG_SHOW:
696 return DispatchResult(false);
698 case LFUN_DIALOG_SHOW_NEW_INSET: {
699 string const & name = cmd.argument;
704 data = tmp.createDialogStr(name);
708 return DispatchResult(false);
709 cur.bv().owner()->getDialogs().show(name, data, 0);
710 return DispatchResult(true, true);
713 case LFUN_INSET_APPLY: {
714 string const name = cmd.getArg(0);
715 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
718 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
719 return base->dispatch(cur, fr);
722 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
724 return DispatchResult(true, true);
726 return DispatchResult(false);
732 case LFUN_WORD_REPLACE:
735 searchForward(&cur.bv(), cmd.getArg(0), false, false)
736 ? DispatchResult(true, true) : DispatchResult(false);
741 BOOST_ASSERT(cur.inMathed());
743 if (result.dispatched()) {
745 cur.bv().stuffClipboard(cur.grabSelection());
747 cur.releaseMathCursor();
749 cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
752 return result; // original version
756 return MathDimInset::priv_dispatch(cur, cmd);
761 void MathNestInset::edit(LCursor & cur, bool left)
764 cur.idx() = left ? 0 : cur.lastidx();
765 cur.pos() = left ? 0 : cur.lastpos();
770 void MathNestInset::edit(LCursor & cur, int x, int y)
773 int dist_min = 1000000;
774 for (idx_type i = 0; i < nargs(); ++i) {
775 int d = cell(i).dist(x, y);
781 MathArray & ar = cell(idx_min);
784 cur.pos() = ar.x2pos(x - ar.xo());
785 lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
788 for (pos_type i = 0, n = ar.size(); i < n; ++i)
789 if (ar[i]->covers(x, y))
790 ar[i].nucleus()->edit(cur, x, y);
796 MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
798 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
800 if (cmd.button() == mouse_button::button1) {
801 // try to dispatch to enclosed insets first
802 //cur.bv().stuffClipboard(cur.grabSelection());
803 return DispatchResult(true, true);
806 if (cmd.button() == mouse_button::button2) {
808 asArray(cur.bv().getClipboard(), ar);
810 cur.setScreenPos(cmd.x, cmd.y);
813 return DispatchResult(true, true);
816 if (cmd.button() == mouse_button::button3) {
817 // try to dispatch to enclosed insets first
818 cur.bv().owner()->getDialogs().show("mathpanel");
819 return DispatchResult(true, true);
822 return DispatchResult(false);
827 MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
829 lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
830 if (cmd.button() == mouse_button::button1) {
834 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
835 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
836 cur.bv().cursor() = cur;
837 return DispatchResult(true, true);
840 if (cmd.button() == mouse_button::button2) {
841 return priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
844 if (cmd.button() == mouse_button::button3) {
845 return DispatchResult(true, true);
848 return DispatchResult(true, true);
853 MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
855 // only select with button 1
856 if (cmd.button() != mouse_button::button1)
857 return DispatchResult(true, true);
859 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
860 return DispatchResult(true, true);
865 if (!cur.selection())
868 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
869 cur.bv().cursor().cursor_ = cur.cursor_;
870 cur.bv().cursor().selection() = true;
871 return DispatchResult(true, true);