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);
351 void MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
353 lyxerr << "MathNestInset: request: " << cmd << std::endl;
355 switch (cmd.action) {
358 if (!cmd.argument.empty()) {
360 mathed_parse_cell(ar, cmd.argument);
361 cur.cell().insert(cur.pos(), ar);
362 cur.pos() += ar.size();
368 istringstream is(cmd.argument.c_str());
371 cur.macroModeClose();
372 //recordUndo(cur, Undo::ATOMIC);
378 case LFUN_PASTESELECTION:
379 dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard()));
382 case LFUN_MOUSE_PRESS:
383 lfunMousePress(cur, cmd);
386 case LFUN_MOUSE_MOTION:
387 lfunMouseMotion(cur, cmd);
390 case LFUN_MOUSE_RELEASE:
391 lfunMouseRelease(cur, cmd);
394 case LFUN_MOUSE_DOUBLE:
395 case LFUN_MOUSE_TRIPLE:
396 //lyxerr << "Mouse double" << endl;
397 //lyxerr << "Mouse triple" << endl;
398 dispatch(cur, FuncRequest(LFUN_WORDSEL));
401 case LFUN_FINISHED_LEFT:
402 cur.pop(cur.currentDepth());
403 cur.bv().cursor() = cur;
406 case LFUN_FINISHED_RIGHT:
407 cur.pop(cur.currentDepth());
409 cur.bv().cursor() = cur;
412 case LFUN_FINISHED_UP:
413 cur.pop(cur.currentDepth());
414 //idxUpDown(cur, true);
415 cur.bv().cursor() = cur;
418 case LFUN_FINISHED_DOWN:
419 cur.pop(cur.currentDepth());
420 //idxUpDown(cur, false);
421 cur.bv().cursor() = cur;
426 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
428 cur.dispatched(FINISHED_RIGHT);
433 cur.selHandle(cmd.action == LFUN_LEFTSEL);
435 cur.dispatched(FINISHED);
440 cur.selHandle(cmd.action == LFUN_UPSEL);
442 cur.dispatched(FINISHED_UP);
447 cur.selHandle(cmd.action == LFUN_DOWNSEL);
449 cur.dispatched(FINISHED_DOWN);
455 cur.selection() = true;
459 case LFUN_UP_PARAGRAPHSEL:
460 case LFUN_UP_PARAGRAPH:
461 case LFUN_DOWN_PARAGRAPHSEL:
462 case LFUN_DOWN_PARAGRAPH:
465 case LFUN_WORDLEFTSEL:
467 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL);
469 cur.dispatched(FINISHED);
472 case LFUN_WORDRIGHTSEL:
474 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL);
476 cur.dispatched(FINISHED_RIGHT);
481 cur.selHandle(cmd.action == LFUN_HOMESEL);
483 cur.dispatched(FINISHED_RIGHT);
488 cur.selHandle(cmd.action == LFUN_ENDSEL);
490 cur.dispatched(FINISHED_RIGHT);
495 case LFUN_BEGINNINGBUFSEL:
496 case LFUN_BEGINNINGBUF:
497 cur.dispatched(FINISHED);
504 cur.dispatched(FINISHED_RIGHT);
507 case LFUN_CELL_FORWARD:
508 cur.inset()->idxNext(cur);
511 case LFUN_CELL_BACKWARD:
512 cur.inset()->idxPrev(cur);
515 case LFUN_DELETE_WORD_BACKWARD:
517 //recordUndo(cur, Undo::ATOMIC);
521 case LFUN_DELETE_WORD_FORWARD:
523 //recordUndo(cur, Undo::ATOMIC);
525 cur.dispatched(FINISHED);
532 cur.dispatched(FINISHED);
535 case LFUN_INSET_TOGGLE:
539 case LFUN_SELFINSERT:
540 if (cmd.argument.empty()) {
541 cur.dispatched(FINISHED_RIGHT);
544 //recordUndo(cur, Undo::ATOMIC);
545 if (cmd.argument.size() != 1) {
546 cur.insert(cmd.argument);
549 if (!cur.interpret(cmd.argument[0]))
550 cur.dispatched(FINISHED_RIGHT);
555 // this needs to be incorporated
557 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
558 bool remove_inset = false;
560 DispatchResult result(true);
561 bool was_macro = cur.inMacroMode();
568 // sprintf(dispatch_buffer, "%d %d",);
569 // DispatchResult= dispatch_buffer;
572 lyxerr << "LFUN_SETXY broken!" << endl;
575 istringstream is(cmd.argument.c_str());
577 cur.setScreenPos(x, y);
582 //recordUndo(cur, Undo::DELETE);
590 // Special casing for superscript in case of LyX handling
592 case LFUN_CIRCUMFLEX:
593 if (cmd.argument.empty()) {
594 // do superscript if LyX handles
596 //recordUndo(cur, Undo::ATOMIC);
614 case LFUN_HUNG_UMLAUT:
618 case LFUN_FREEFONT_APPLY:
619 case LFUN_FREEFONT_UPDATE:
620 handleFont2(cur, cmd.argument);
624 handleFont(cur, cmd.argument, "mathbf");
627 handleFont(cur, cmd.argument, "mathsf");
630 handleFont(cur, cmd.argument, "mathcal");
633 handleFont(cur, cmd.argument, "mathrm");
636 handleFont(cur, cmd.argument, "texttt");
639 handleFont(cur, cmd.argument, "mathfrak");
642 handleFont(cur, cmd.argument, "mathit");
645 handleFont(cur, cmd.argument, "mathbb");
647 //case LFUN_FREEFONT_APPLY:
648 handleFont(cur, cmd.argument, "textrm");
651 handleFont(cur, cmd.argument, "textnormal");
656 cur.macroModeClose();
658 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
660 cur.pushLeft(cur.nextInset());
662 if (cur.currentMode() == InsetBase::TEXT_MODE)
663 cur.niceInsert(MathAtom(new MathHullInset("simple")));
665 handleFont(cur, cmd.argument, "textrm");
666 //cur.owner()->message(_("math text mode toggled"));
673 //recordUndo(cur, Undo::ATOMIC);
679 case LFUN_INSERT_MATRIX: {
680 //recordUndo(cur, Undo::ATOMIC);
685 istringstream is(cmd.argument);
686 is >> m >> n >> v_align >> h_align;
693 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
697 case LFUN_MATH_DELIM: {
698 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
700 string rs = lyx::support::split(cmd.argument, ls, ' ');
701 // Reasonable default values
706 //recordUndo(cur, Undo::ATOMIC);
707 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
711 case LFUN_SPACE_INSERT:
712 case LFUN_MATH_SPACE:
713 //recordUndo(cur, Undo::ATOMIC);
714 cur.insert(MathAtom(new MathSpaceInset(",")));
719 //cur.bv().owner()->message(_("Invalid action in math mode!"));
723 // interpret this as if a backslash was typed
724 //recordUndo(cur, Undo::ATOMIC);
728 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
729 // handling such that "self-insert" works on "arbitrary stuff" too, and
730 // math-insert only handles special math things like "matrix".
731 case LFUN_INSERT_MATH:
732 //recordUndo(cur, Undo::ATOMIC);
733 cur.niceInsert(cmd.argument);
736 case LFUN_DIALOG_SHOW_NEW_INSET: {
737 string const & name = cmd.argument;
742 data = tmp.createDialogStr(name);
745 cur.bv().owner()->getDialogs().show(name, data, 0);
749 case LFUN_INSET_APPLY: {
750 string const name = cmd.getArg(0);
751 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
754 base->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
758 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
769 case LFUN_WORD_REPLACE:
771 if (!searchForward(&cur.bv(), cmd.getArg(0), false, false))
778 BOOST_ASSERT(cur.inMathed());
780 if (result.dispatched()) {
782 cur.bv().stuffClipboard(cur.grabSelection());
785 cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
791 MathDimInset::priv_dispatch(cur, cmd);
797 void MathNestInset::edit(LCursor & cur, bool left)
800 cur.idx() = left ? 0 : cur.lastidx();
801 cur.pos() = left ? 0 : cur.lastpos();
806 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
809 int dist_min = 1000000;
810 for (idx_type i = 0; i < nargs(); ++i) {
811 int d = cell(i).dist(x, y);
817 MathArray & ar = cell(idx_min);
820 cur.pos() = ar.x2pos(x - ar.xo());
821 lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
824 for (pos_type i = 0, n = ar.size(); i < n; ++i)
825 if (ar[i]->covers(x, y))
826 return ar[i].nucleus()->editXY(cur, x, y);
832 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
834 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
836 if (cmd.button() == mouse_button::button1) {
837 // try to dispatch to enclosed insets first
838 //cur.bv().stuffClipboard(cur.grabSelection());
842 if (cmd.button() == mouse_button::button2) {
844 asArray(cur.bv().getClipboard(), ar);
846 cur.setScreenPos(cmd.x, cmd.y);
852 if (cmd.button() == mouse_button::button3) {
853 // try to dispatch to enclosed insets first
854 cur.bv().owner()->getDialogs().show("mathpanel");
862 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
864 lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
865 if (cmd.button() == mouse_button::button1) {
869 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
870 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
871 cur.bv().cursor() = cur;
874 if (cmd.button() == mouse_button::button2) {
875 priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
880 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
882 // only select with button 1
883 if (cmd.button() != mouse_button::button1)
886 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
892 if (!cur.selection())
895 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
896 cur.bv().cursor().cursor_ = cur.cursor_;
897 cur.bv().cursor().selection() = true;