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 "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "math_data.h"
22 #include "math_mathmlstream.h"
23 #include "math_parser.h"
26 #include "frontends/Painter.h"
29 MathNestInset::MathNestInset(idx_type nargs)
30 : cells_(nargs), lock_(false)
34 MathInset::idx_type MathNestInset::nargs() const
40 MathArray & MathNestInset::cell(idx_type i)
46 MathArray const & MathNestInset::cell(idx_type i) const
52 void MathNestInset::getScreenPos(idx_type idx, pos_type pos, int & x, int & y) const
54 MathArray const & ar = cell(idx);
55 x = ar.xo() + ar.pos2x(pos);
57 // move cursor visually into empty cells ("blue rectangles");
58 if (cell(idx).empty())
63 void MathNestInset::substitute(MathMacro const & m)
65 for (idx_type i = 0; i < nargs(); ++i)
66 cell(i).substitute(m);
70 void MathNestInset::metrics(MetricsInfo const & mi) const
73 for (idx_type i = 0; i < nargs(); ++i)
78 bool MathNestInset::idxNext(LCursor & cur) const
80 if (cur.idx() + 1 >= nargs())
88 bool MathNestInset::idxRight(LCursor & cur) const
94 bool MathNestInset::idxPrev(LCursor & cur) const
99 cur.pos() = cur.lastpos();
104 bool MathNestInset::idxLeft(LCursor & cur) const
110 bool MathNestInset::idxFirst(LCursor & cur) const
120 bool MathNestInset::idxLast(LCursor & cur) const
124 cur.idx() = nargs() - 1;
125 cur.pos() = cur.lastpos();
130 bool MathNestInset::idxHome(LCursor & cur) const
139 bool MathNestInset::idxEnd(LCursor & cur) const
141 if (cur.lastpos() == cur.lastpos())
143 cur.pos() = cur.lastpos();
148 void MathNestInset::dump() const
150 WriteStream os(lyxerr);
151 os << "---------------------------------------------\n";
154 for (idx_type i = 0; i < nargs(); ++i)
155 os << cell(i) << "\n";
156 os << "---------------------------------------------\n";
160 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
161 void MathNestInset::draw(PainterInfo &, int, int) const
165 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
171 void MathNestInset::drawSelection(PainterInfo & pi,
172 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
175 MathArray const & c = cell(idx1);
176 int x1 = c.xo() + c.pos2x(pos1);
177 int y1 = c.yo() - c.ascent();
178 int x2 = c.xo() + c.pos2x(pos2);
179 int y2 = c.yo() + c.descent();
180 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
182 for (idx_type i = 0; i < nargs(); ++i) {
183 if (idxBetween(i, idx1, idx2)) {
184 MathArray const & c = cell(i);
186 int y1 = c.yo() - c.ascent();
187 int x2 = c.xo() + c.width();
188 int y2 = c.yo() + c.descent();
189 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
196 void MathNestInset::validate(LaTeXFeatures & features) const
198 for (idx_type i = 0; i < nargs(); ++i)
199 cell(i).validate(features);
203 void MathNestInset::replace(ReplaceData & rep)
205 for (idx_type i = 0; i < nargs(); ++i)
206 cell(i).replace(rep);
210 bool MathNestInset::contains(MathArray const & ar) const
212 for (idx_type i = 0; i < nargs(); ++i)
213 if (cell(i).contains(ar))
219 bool MathNestInset::editing(BufferView * bv) const
221 return bv->cursor().isInside(this);
225 bool MathNestInset::lock() const
231 void MathNestInset::lock(bool l)
237 bool MathNestInset::isActive() const
243 MathArray MathNestInset::glue() const
246 for (size_t i = 0; i < nargs(); ++i)
252 void MathNestInset::write(WriteStream & os) const
254 os << '\\' << name().c_str();
255 for (size_t i = 0; i < nargs(); ++i)
256 os << '{' << cell(i) << '}';
258 os.pendingSpace(true);
259 if (lock_ && !os.latex()) {
261 os.pendingSpace(true);
266 void MathNestInset::normalize(NormalStream & os) const
268 os << '[' << name().c_str();
269 for (size_t i = 0; i < nargs(); ++i)
270 os << ' ' << cell(i);
275 void MathNestInset::notifyCursorLeaves(idx_type idx)
277 cell(idx).notifyCursorLeaves();
282 MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
284 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
285 // << " arg: '" << cmd.argument
286 // << "' x: '" << cmd.x
287 // << " y: '" << cmd.y
288 // << "' button: " << cmd.button() << endl;
290 switch (cmd.action) {
294 mathed_parse_cell(ar, cmd.argument);
295 cur.cell().insert(cur.pos(), ar);
296 cur.pos() += ar.size();
297 return DispatchResult(true, true);
300 case LFUN_PASTESELECTION:
301 return dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard()));
303 case LFUN_MOUSE_PRESS:
304 if (cmd.button() == mouse_button::button2)
305 return priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
306 return DispatchResult(false);
309 cur.selection() = true; // fall through...
312 DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
313 //lyxerr << "calling scroll 20" << endl;
314 //scroll(&cur.bv(), 20);
315 // write something to the minibuffer
316 //cur.bv().owner()->message(cur.info());
319 cur.selection() = true; // fall through
322 DispatchResult(true, true) : DispatchResult(false, FINISHED);
325 cur.selection() = true; // fall through
328 DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
331 cur.selection() = true; // fall through
334 DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
338 cur.selection() = true;
340 return DispatchResult(true, true);
342 case LFUN_UP_PARAGRAPHSEL:
343 case LFUN_UP_PARAGRAPH:
344 case LFUN_DOWN_PARAGRAPHSEL:
345 case LFUN_DOWN_PARAGRAPH:
346 return DispatchResult(true, FINISHED);
349 case LFUN_WORDLEFTSEL:
350 cur.selection() = true; // fall through
354 ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
357 case LFUN_WORDRIGHTSEL:
358 cur.selection() = true; // fall through
362 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
366 case LFUN_BEGINNINGBUFSEL:
367 case LFUN_BEGINNINGBUF:
368 return DispatchResult(true, FINISHED);
374 return DispatchResult(false, FINISHED_RIGHT);
376 case LFUN_CELL_FORWARD:
377 cur.inset()->idxNext(cur);
378 return DispatchResult(true, true);
380 case LFUN_CELL_BACKWARD:
381 cur.inset()->idxPrev(cur);
382 return DispatchResult(true, true);
384 case LFUN_DELETE_WORD_BACKWARD:
386 recordUndo(cur, Undo::ATOMIC);
388 return DispatchResult(true, true);
390 case LFUN_DELETE_WORD_FORWARD:
392 recordUndo(cur, Undo::ATOMIC);
394 return DispatchResult(true, FINISHED);
397 if (!cur.selection())
398 return DispatchResult(true, true);
400 return DispatchResult(false);
402 case LFUN_INSET_TOGGLE:
404 return DispatchResult(true, true);
406 case LFUN_SELFINSERT:
407 if (!cmd.argument.empty()) {
408 recordUndo(cur, Undo::ATOMIC);
409 if (cmd.argument.size() == 1) {
410 if (cur.interpret(cmd.argument[0]))
411 return DispatchResult(true, true);
413 return DispatchResult(false, FINISHED_RIGHT);
415 cur.insert(cmd.argument);
417 return DispatchResult(false, FINISHED_RIGHT);
422 // this needs to bee incorporated
424 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
425 // << " arg: '" << cmd.argument
426 // << "' x: '" << cmd.x
427 // << " y: '" << cmd.y
428 // << "' button: " << cmd.button() << endl;
430 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
431 bool remove_inset = false;
433 switch (cmd.action) {
434 case LFUN_MOUSE_PRESS:
435 //lyxerr << "Mouse single press" << endl;
436 return lfunMousePress(cur, cmd);
437 case LFUN_MOUSE_MOTION:
438 //lyxerr << "Mouse motion" << endl;
439 return lfunMouseMotion(cur, cmd);
440 case LFUN_MOUSE_RELEASE:
441 //lyxerr << "Mouse single release" << endl;
442 return lfunMouseRelease(cur, cmd);
443 case LFUN_MOUSE_DOUBLE:
444 //lyxerr << "Mouse double" << endl;
445 return dispatch(cur, FuncRequest(LFUN_WORDSEL));
450 DispatchResult result(true);
451 string argument = cmd.argument;
452 bool was_macro = cur.inMacroMode();
457 switch (cmd.action) {
459 case LFUN_MATH_MUTATE:
460 case LFUN_MATH_DISPLAY:
461 case LFUN_MATH_NUMBER:
462 case LFUN_MATH_NONUMBER:
463 case LFUN_CELL_SPLIT:
465 case LFUN_DELETE_LINE_FORWARD:
466 case LFUN_INSERT_LABEL:
467 case LFUN_MATH_EXTERN:
468 case LFUN_TABULAR_FEATURE:
469 case LFUN_PASTESELECTION:
470 case LFUN_MATH_LIMITS:
471 recordUndo(cur, Undo::ATOMIC);
476 // sprintf(dispatch_buffer, "%d %d",);
477 // DispatchResult= dispatch_buffer;
480 lyxerr << "LFUN_SETXY broken!" << endl;
483 istringstream is(cmd.argument.c_str());
485 cur.setScreenPos(x, y);
491 istringstream is(cmd.argument.c_str());
494 cur.macroModeClose();
495 recordUndo(cur, Undo::ATOMIC);
501 recordUndo(cur, Undo::DELETE);
510 // Special casing for superscript in case of LyX handling
512 case LFUN_CIRCUMFLEX:
513 if (cmd.argument.empty()) {
514 // do superscript if LyX handles
516 recordUndo(cur, Undo::ATOMIC);
534 case LFUN_HUNG_UMLAUT:
538 case LFUN_FREEFONT_APPLY:
539 case LFUN_FREEFONT_UPDATE:
540 handleFont2(cur, cmd.argument);
543 case LFUN_BOLD: handleFont(cur, cmd.argument, "mathbf"); break;
544 case LFUN_SANS: handleFont(cur, cmd.argument, "mathsf"); break;
545 case LFUN_EMPH: handleFont(cur, cmd.argument, "mathcal"); break;
546 case LFUN_ROMAN: handleFont(cur, cmd.argument, "mathrm"); break;
547 case LFUN_CODE: handleFont(cur, cmd.argument, "texttt"); break;
548 case LFUN_FRAK: handleFont(cur, cmd.argument, "mathfrak"); break;
549 case LFUN_ITAL: handleFont(cur, cmd.argument, "mathit"); break;
550 case LFUN_NOUN: handleFont(cur, cmd.argument, "mathbb"); break;
551 //case LFUN_FREEFONT_APPLY: handleFont(cur, cmd.argument, "textrm"); break;
552 case LFUN_DEFAULT: handleFont(cur, cmd.argument, "textnormal"); break;
555 if (cur.currentMode() == InsetBase::TEXT_MODE)
556 cur.niceInsert(MathAtom(new MathHullInset("simple")));
558 handleFont(cur, cmd.argument, "textrm");
559 //cur.owner()->message(_("math text mode toggled"));
565 recordUndo(cur, Undo::ATOMIC);
571 case LFUN_INSERT_MATRIX: {
572 recordUndo(cur, Undo::ATOMIC);
577 istringstream is(argument);
578 is >> m >> n >> v_align >> h_align;
583 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
587 case LFUN_MATH_DELIM: {
588 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
590 string rs = split(cmd.argument, ls, ' ');
591 // Reasonable default values
596 recordUndo(cur, Undo::ATOMIC);
597 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
601 case LFUN_SPACE_INSERT:
602 case LFUN_MATH_SPACE:
603 recordUndo(cur, Undo::ATOMIC);
604 cur.insert(MathAtom(new MathSpaceInset(",")));
608 cur.bv().owner()->message(_("Invalid action in math mode!"));
612 case LFUN_EXEC_COMMAND:
613 result = DispatchResult(false);
617 // interpret this as if a backslash was typed
618 recordUndo(cur, Undo::ATOMIC);
622 case LFUN_BREAKPARAGRAPH:
623 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
624 case LFUN_BREAKPARAGRAPH_SKIP:
628 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
629 // handling such that "self-insert" works on "arbitrary stuff" too, and
630 // math-insert only handles special math things like "matrix".
631 case LFUN_INSERT_MATH:
632 recordUndo(cur, Undo::ATOMIC);
633 cur.niceInsert(argument);
636 case LFUN_INSET_TOGGLE:
640 case LFUN_DIALOG_SHOW:
641 result = DispatchResult(false);
644 case LFUN_DIALOG_SHOW_NEW_INSET: {
645 string const & name = argument;
649 data = tmp.createDialogStr(name);
653 result = DispatchResult(false);
655 cur.bv().owner()->getDialogs().show(name, data, 0);
659 case LFUN_INSET_APPLY: {
660 string const name = cmd.getArg(0);
661 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
664 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
665 result = base->dispatch(cur, fr);
668 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
670 result = DispatchResult(true, true);
672 result = DispatchResult(false);
678 case LFUN_WORD_REPLACE:
679 case LFUN_WORD_FIND: {
681 searchForward(&cur.bv(), cmd.getArg(0), false, false)
682 ? DispatchResult(true, true) : DispatchResult(false);
686 case LFUN_INSERT_MATH:
687 case LFUN_INSERT_MATRIX:
688 case LFUN_MATH_DELIM: {
689 MathHullInset * f = new MathHullInset;
690 if (openNewInset(cur, f)) {
691 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
692 cur.inset()->dispatch(cur, cmd);
698 result = DispatchResult(false);
701 if (result == DispatchResult(true, true))
707 BOOST_ASSERT(cur.inMathed());
709 if (result.dispatched()) {
711 cur.bv().stuffClipboard(cur.grabSelection());
713 cur.releaseMathCursor();
715 cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
718 return result; // original version
722 return MathInset::priv_dispatch(cur, cmd);
727 void MathNestInset::metricsMarkers(int) const
734 void MathNestInset::metricsMarkers2(int) const
742 void MathNestInset::drawMarkers(PainterInfo & pi, int x, int y) const
744 if (!editing(pi.base.bv))
746 int t = x + dim_.width() - 1;
747 int d = y + dim_.descent();
748 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
749 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
750 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
751 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
755 void MathNestInset::drawMarkers2(PainterInfo & pi, int x, int y) const
757 if (!editing(pi.base.bv))
759 drawMarkers(pi, x, y);
760 int t = x + dim_.width() - 1;
761 int a = y - dim_.ascent();
762 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
763 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
764 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
765 pi.pain.line(t - 3, a, t, a, LColor::mathframe);