]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
bug + spped fixes + small stuff
[lyx.git] / src / mathed / math_nestinset.C
1 /**
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.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_nestinset.h"
14
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"
25
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
28 #include "cursor.h"
29 #include "debug.h"
30 #include "dispatchresult.h"
31 #include "funcrequest.h"
32 #include "gettext.h"
33 #include "LColor.h"
34 #include "undo.h"
35
36 #include "support/std_sstream.h"
37 #include "support/lstrings.h"
38
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/Painter.h"
42
43
44 using std::endl;
45 using std::string;
46 using std::istringstream;
47
48
49 namespace {
50
51 // local global
52 int first_x;
53 int first_y;
54
55 } // namespace anon
56
57
58
59
60 MathNestInset::MathNestInset(idx_type nargs)
61         : cells_(nargs), lock_(false)
62 {}
63
64
65 MathInset::idx_type MathNestInset::nargs() const
66 {
67         return cells_.size();
68 }
69
70
71 MathArray & MathNestInset::cell(idx_type i)
72 {
73         return cells_[i];
74 }
75
76
77 MathArray const & MathNestInset::cell(idx_type i) const
78 {
79         return cells_[i];
80 }
81
82
83 void MathNestInset::getCursorPos(CursorSlice const & cur,
84         int & x, int & y) const
85 {
86         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
87         MathArray const & ar = cur.cell();
88         x = ar.xo() + ar.pos2x(cur.pos());
89         y = ar.yo();
90         // move cursor visually into empty cells ("blue rectangles");
91         if (cur.cell().empty())
92                 x += 2;
93 }
94
95
96 void MathNestInset::substitute(MathMacro const & m)
97 {
98         for (idx_type i = 0; i < nargs(); ++i)
99                 cell(i).substitute(m);
100 }
101
102
103 void MathNestInset::metrics(MetricsInfo const & mi) const
104 {
105         MetricsInfo m = mi;
106         for (idx_type i = 0; i < nargs(); ++i)
107                 cell(i).metrics(m);
108 }
109
110
111 bool MathNestInset::idxNext(LCursor & cur) const
112 {
113         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
114         if (cur.idx() == cur.lastidx())
115                 return false;
116         ++cur.idx();
117         cur.pos() = 0;
118         return true;
119 }
120
121
122 bool MathNestInset::idxRight(LCursor & cur) const
123 {
124         return idxNext(cur);
125 }
126
127
128 bool MathNestInset::idxPrev(LCursor & cur) const
129 {
130         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
131         if (cur.idx() == 0)
132                 return false;
133         --cur.idx();
134         cur.pos() = cur.lastpos();
135         return true;
136 }
137
138
139 bool MathNestInset::idxLeft(LCursor & cur) const
140 {
141         return idxPrev(cur);
142 }
143
144
145 bool MathNestInset::idxFirst(LCursor & cur) const
146 {
147         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
148         if (nargs() == 0)
149                 return false;
150         cur.idx() = 0;
151         cur.pos() = 0;
152         return true;
153 }
154
155
156 bool MathNestInset::idxLast(LCursor & cur) const
157 {
158         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
159         if (nargs() == 0)
160                 return false;
161         cur.idx() = cur.lastidx();
162         cur.pos() = cur.lastpos();
163         return true;
164 }
165
166
167 bool MathNestInset::idxHome(LCursor & cur) const
168 {
169         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
170         if (cur.pos() == 0)
171                 return false;
172         cur.pos() = 0;
173         return true;
174 }
175
176
177 bool MathNestInset::idxEnd(LCursor & cur) const
178 {
179         BOOST_ASSERT(ptr_cmp(cur.inset(), this));
180         if (cur.lastpos() == cur.lastpos())
181                 return false;
182         cur.pos() = cur.lastpos();
183         return true;
184 }
185
186
187 void MathNestInset::dump() const
188 {
189         WriteStream os(lyxerr);
190         os << "---------------------------------------------\n";
191         write(os);
192         os << "\n";
193         for (idx_type i = 0; i < nargs(); ++i)
194                 os << cell(i) << "\n";
195         os << "---------------------------------------------\n";
196 }
197
198
199 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
200 void MathNestInset::draw(PainterInfo &, int, int) const
201 {
202 #if 0
203         if (lock_)
204                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
205                                         LColor::mathlockbg);
206 #endif
207 }
208
209
210 void MathNestInset::drawSelection(PainterInfo & pi, int, int) const
211 {
212         // this should use the x/y values given, not the cached values
213         LCursor & cur = pi.base.bv->cursor();
214         if (!cur.selection())
215                 return;
216         if (!ptr_cmp(cur.inset(), this))
217                 return;
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);
227         } else {
228                 for (idx_type i = 0; i < nargs(); ++i) {
229                         if (idxBetween(i, s1.idx(), s2.idx())) {
230                                 MathArray const & c = cell(i);
231                                 int x1 = c.xo();
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);
236                         }
237                 }
238         }
239 }
240
241
242 void MathNestInset::validate(LaTeXFeatures & features) const
243 {
244         for (idx_type i = 0; i < nargs(); ++i)
245                 cell(i).validate(features);
246 }
247
248
249 void MathNestInset::replace(ReplaceData & rep)
250 {
251         for (idx_type i = 0; i < nargs(); ++i)
252                 cell(i).replace(rep);
253 }
254
255
256 bool MathNestInset::contains(MathArray const & ar) const
257 {
258         for (idx_type i = 0; i < nargs(); ++i)
259                 if (cell(i).contains(ar))
260                         return true;
261         return false;
262 }
263
264
265 bool MathNestInset::lock() const
266 {
267         return lock_;
268 }
269
270
271 void MathNestInset::lock(bool l)
272 {
273         lock_ = l;
274 }
275
276
277 bool MathNestInset::isActive() const
278 {
279         return nargs() > 0;
280 }
281
282
283 MathArray MathNestInset::glue() const
284 {
285         MathArray ar;
286         for (size_t i = 0; i < nargs(); ++i)
287                 ar.append(cell(i));
288         return ar;
289 }
290
291
292 void MathNestInset::write(WriteStream & os) const
293 {
294         os << '\\' << name().c_str();
295         for (size_t i = 0; i < nargs(); ++i)
296                 os << '{' << cell(i) << '}';
297         if (nargs() == 0)
298                 os.pendingSpace(true);
299         if (lock_ && !os.latex()) {
300                 os << "\\lyxlock";
301                 os.pendingSpace(true);
302         }
303 }
304
305
306 void MathNestInset::normalize(NormalStream & os) const
307 {
308         os << '[' << name().c_str();
309         for (size_t i = 0; i < nargs(); ++i)
310                 os << ' ' << cell(i);
311         os << ']';
312 }
313
314
315 void MathNestInset::notifyCursorLeaves(idx_type idx)
316 {
317         cell(idx).notifyCursorLeaves();
318 }
319
320
321 void MathNestInset::handleFont
322         (LCursor & cur, string const & arg, string const & font)
323 {
324         // this whole function is a hack and won't work for incremental font
325         // changes...
326         //recordUndo(cur, Undo::ATOMIC);
327
328         if (cur.inset()->asMathInset()->name() == font)
329                 cur.handleFont(font);
330         else {
331                 cur.handleNest(createMathInset(font));
332                 cur.insert(arg);
333         }
334 }
335
336
337 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
338 {
339         //recordUndo(cur, Undo::ATOMIC);
340         LyXFont font;
341         bool b;
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);
347         }
348 }
349
350
351 DispatchResult
352 MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
353 {
354         lyxerr << "MathNestInset: request: " << cmd << std::endl;
355
356         switch (cmd.action) {
357
358         case LFUN_PASTE:
359                 if (!cmd.argument.empty()) {
360                         MathArray ar;
361                         mathed_parse_cell(ar, cmd.argument);
362                         cur.cell().insert(cur.pos(), ar);
363                         cur.pos() += ar.size();
364                 }
365                 return DispatchResult(true, true);
366 /*
367         case LFUN_PASTE: {
368                 size_t n = 0;
369                 istringstream is(cmd.argument.c_str());
370                 is >> n;
371                 if (was_macro)
372                         cur.macroModeClose();
373                 //recordUndo(cur, Undo::ATOMIC);
374                 cur.selPaste(n);
375                 return DispatchResult(true, true);
376         }
377 */
378
379         case LFUN_PASTESELECTION:
380                 return dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard())); 
381
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));
390
391         case LFUN_RIGHTSEL:
392         case LFUN_RIGHT:
393                 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
394                 return cur.right() ?
395                         DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
396
397         case LFUN_LEFTSEL:
398         case LFUN_LEFT:
399                 cur.selHandle(cmd.action == LFUN_LEFTSEL);
400                 return cur.left() ?
401                         DispatchResult(true, true) : DispatchResult(false, FINISHED);
402
403         case LFUN_UPSEL:
404         case LFUN_UP:
405                 cur.selHandle(cmd.action == LFUN_UPSEL);
406                 return cur.up() ?
407                         DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
408
409         case LFUN_DOWNSEL:
410         case LFUN_DOWN:
411                 cur.selHandle(cmd.action == LFUN_DOWNSEL);
412                 return cur.down() ?
413                         DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
414
415         case LFUN_WORDSEL:
416                 cur.home();
417                 cur.resetAnchor();
418                 cur.selection() = true;
419                 cur.end();
420                 return DispatchResult(true, true);
421
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);
427
428         case LFUN_WORDLEFTSEL:
429         case LFUN_WORDLEFT:
430                 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL);
431                 return cur.home()
432                         ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
433
434         case LFUN_WORDRIGHTSEL:
435         case LFUN_WORDRIGHT:
436                 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL);
437                 return cur.end()
438                         ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
439
440         case LFUN_HOMESEL:
441         case LFUN_HOME:
442                 cur.selHandle(cmd.action == LFUN_HOMESEL);
443                 return cur.home()
444                         ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
445
446         case LFUN_ENDSEL:
447         case LFUN_END:
448                 cur.selHandle(cmd.action == LFUN_ENDSEL);
449                 return cur.end()
450                         ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
451
452         case LFUN_PRIORSEL:
453         case LFUN_PRIOR:
454         case LFUN_BEGINNINGBUFSEL:
455         case LFUN_BEGINNINGBUF:
456                 return DispatchResult(true, FINISHED);
457
458         case LFUN_NEXTSEL:
459         case LFUN_NEXT:
460         case LFUN_ENDBUFSEL:
461         case LFUN_ENDBUF:
462                 return DispatchResult(false, FINISHED_RIGHT);
463
464         case LFUN_CELL_FORWARD:
465                 cur.inset()->idxNext(cur);
466                 return DispatchResult(true, true);
467
468         case LFUN_CELL_BACKWARD:
469                 cur.inset()->idxPrev(cur);
470                 return DispatchResult(true, true);
471
472         case LFUN_DELETE_WORD_BACKWARD:
473         case LFUN_BACKSPACE:
474                 //recordUndo(cur, Undo::ATOMIC);
475                 cur.backspace();
476                 return DispatchResult(true, true);
477
478         case LFUN_DELETE_WORD_FORWARD:
479         case LFUN_DELETE:
480                 //recordUndo(cur, Undo::ATOMIC);
481                 cur.erase();
482                 return DispatchResult(true, FINISHED);
483
484         case LFUN_ESCAPE:
485                 if (!cur.selection())
486                         return DispatchResult(true, true);
487                 cur.selClear();
488                 return DispatchResult(false);
489
490         case LFUN_INSET_TOGGLE:
491                 cur.lockToggle();
492                 return DispatchResult(true, true);
493
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);
500                                 else
501                                         return DispatchResult(false, FINISHED_RIGHT);
502                         }
503                         cur.insert(cmd.argument);
504                 }
505                 return DispatchResult(false, FINISHED_RIGHT);
506
507
508 #if 0
509 //
510 // this needs to be incorporated
511 //
512         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
513         bool remove_inset = false;
514
515         DispatchResult result(true);
516         bool was_macro     = cur.inMacroMode();
517
518         cur.normalize();
519         cur.touch();
520 #endif
521
522         //    case LFUN_GETXY:
523         //      sprintf(dispatch_buffer, "%d %d",);
524         //      DispatchResult= dispatch_buffer;
525         //      break;
526         case LFUN_SETXY: {
527                 lyxerr << "LFUN_SETXY broken!" << endl;
528                 int x = 0;
529                 int y = 0;
530                 istringstream is(cmd.argument.c_str());
531                 is >> x >> y;
532                 cur.setScreenPos(x, y);
533                 return DispatchResult(true, true);
534         }
535
536         case LFUN_CUT:
537                 //recordUndo(cur, Undo::DELETE);
538                 cur.selCut();
539                 return DispatchResult(true, true);
540
541         case LFUN_COPY:
542                 cur.selCopy();
543                 return DispatchResult(true, true);
544
545
546         // Special casing for superscript in case of LyX handling
547         // dead-keys:
548         case LFUN_CIRCUMFLEX:
549                 if (cmd.argument.empty()) {
550                         // do superscript if LyX handles
551                         // deadkeys
552                         //recordUndo(cur, Undo::ATOMIC);
553                         cur.script(true);
554                 }
555                 return DispatchResult(true, true);
556
557         case LFUN_UMLAUT:
558         case LFUN_ACUTE:
559         case LFUN_GRAVE:
560         case LFUN_BREVE:
561         case LFUN_DOT:
562         case LFUN_MACRON:
563         case LFUN_CARON:
564         case LFUN_TILDE:
565         case LFUN_CEDILLA:
566         case LFUN_CIRCLE:
567         case LFUN_UNDERDOT:
568         case LFUN_TIE:
569         case LFUN_OGONEK:
570         case LFUN_HUNG_UMLAUT:
571                 return DispatchResult(true, true);
572
573         //  Math fonts
574         case LFUN_FREEFONT_APPLY:
575         case LFUN_FREEFONT_UPDATE:
576                 handleFont2(cur, cmd.argument);
577                 return DispatchResult(true, true);
578
579         case LFUN_BOLD:
580                 handleFont(cur, cmd.argument, "mathbf");
581                 return DispatchResult(true, true);
582         case LFUN_SANS:
583                 handleFont(cur, cmd.argument, "mathsf");
584                 return DispatchResult(true, true);
585         case LFUN_EMPH:
586                 handleFont(cur, cmd.argument, "mathcal");
587                 return DispatchResult(true, true);
588         case LFUN_ROMAN:
589                 handleFont(cur, cmd.argument, "mathrm");
590                 return DispatchResult(true, true);
591         case LFUN_CODE:
592                 handleFont(cur, cmd.argument, "texttt");
593                 return DispatchResult(true, true);
594         case LFUN_FRAK:
595                 handleFont(cur, cmd.argument, "mathfrak");
596                 return DispatchResult(true, true);
597         case LFUN_ITAL:
598                 handleFont(cur, cmd.argument, "mathit");
599                 return DispatchResult(true, true);
600         case LFUN_NOUN:
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);
606         case LFUN_DEFAULT:
607                 handleFont(cur, cmd.argument, "textnormal");
608                 return DispatchResult(true, true);
609
610         case LFUN_MATH_MODE:
611 #if 1
612                 cur.macroModeClose();
613                 cur.selClearOrDel();
614                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
615                 cur.posLeft();
616                 cur.pushLeft(cur.nextInset());
617 #else
618                 if (cur.currentMode() == InsetBase::TEXT_MODE)
619                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
620                 else
621                         handleFont(cur, cmd.argument, "textrm");
622                 //cur.owner()->message(_("math text mode toggled"));
623 #endif
624                 return DispatchResult(true, true);
625
626         case LFUN_MATH_SIZE:
627 #if 0
628                 if (!arg.empty()) {
629                         //recordUndo(cur, Undo::ATOMIC);
630                         cur.setSize(arg);
631                 }
632 #endif
633                 return DispatchResult(true, true);
634
635         case LFUN_INSERT_MATRIX: {
636                 //recordUndo(cur, Undo::ATOMIC);
637                 unsigned int m = 1;
638                 unsigned int n = 1;
639                 string v_align;
640                 string h_align;
641                 istringstream is(cmd.argument);
642                 is >> m >> n >> v_align >> h_align;
643                 if (m < 1)
644                         m = 1;
645                 if (n < 1)
646                         n = 1;
647                 v_align += 'c';
648                 cur.niceInsert(
649                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
650                 return DispatchResult(true, true);
651         }
652
653         case LFUN_MATH_DELIM: {
654                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
655                 string ls;
656                 string rs = lyx::support::split(cmd.argument, ls, ' ');
657                 // Reasonable default values
658                 if (ls.empty())
659                         ls = '(';
660                 if (rs.empty())
661                         rs = ')';
662                 //recordUndo(cur, Undo::ATOMIC);
663                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
664                 return DispatchResult(true, true);
665         }
666
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);
672
673         case LFUN_UNDO:
674 #warning look here
675                 //cur.bv().owner()->message(_("Invalid action in math mode!"));
676                 return DispatchResult(true, true);
677
678         case LFUN_INSET_ERT:
679                 // interpret this as if a backslash was typed
680                 //recordUndo(cur, Undo::ATOMIC);
681                 cur.interpret('\\');
682                 return DispatchResult(true, true);
683
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);
691
692         case LFUN_DIALOG_SHOW:
693                 return DispatchResult(false);
694
695         case LFUN_DIALOG_SHOW_NEW_INSET: {
696                 string const & name = cmd.argument;
697                 string data;
698 #if 0
699                 if (name == "ref") {
700                         RefInset tmp(name);
701                         data = tmp.createDialogStr(name);
702                 }
703 #endif
704                 if (data.empty())
705                         return DispatchResult(false);
706                 cur.bv().owner()->getDialogs().show(name, data, 0);
707                 return DispatchResult(true, true);
708         }
709
710         case LFUN_INSET_APPLY: {
711                 string const name = cmd.getArg(0);
712                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
713
714                 if (base) {
715                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
716                         return base->dispatch(cur, fr);
717                 }
718                 MathArray ar;
719                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
720                         cur.insert(ar);
721                         return DispatchResult(true, true);
722                 }
723                 return DispatchResult(false);
724         }
725
726 #warning look here
727 #if 0
728
729         case LFUN_WORD_REPLACE:
730         case LFUN_WORD_FIND:
731                 return
732                         searchForward(&cur.bv(), cmd.getArg(0), false, false)
733                                 ? DispatchResult(true, true) : DispatchResult(false);
734
735         cur.normalize();
736         cur.touch();
737
738         BOOST_ASSERT(cur.inMathed());
739
740         if (result.dispatched()) {
741                 revealCodes(cur);
742                 cur.bv().stuffClipboard(cur.grabSelection());
743         } else {
744                 cur.releaseMathCursor();
745                 if (remove_inset)
746                         cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
747         }
748
749         return result;  // original version
750 #endif
751
752         default:
753                 return MathDimInset::priv_dispatch(cur, cmd);
754         }
755 }
756
757
758 void MathNestInset::edit(LCursor & cur, bool left)
759 {
760         cur.push(this);
761         cur.idx() = left ? 0 : cur.lastidx();
762         cur.pos() = left ? 0 : cur.lastpos();
763         cur.resetAnchor();
764 }
765
766
767 void MathNestInset::edit(LCursor & cur, int x, int y)
768 {
769         int idx_min = 0;
770         int dist_min = 1000000;
771         for (idx_type i = 0; i < nargs(); ++i) {
772                 int d = cell(i).dist(x, y);
773                 if (d < dist_min) {
774                         dist_min = d;
775                         idx_min = i;
776                 }
777         }
778         MathArray & ar = cell(idx_min);
779         cur.push(this);
780         cur.idx() = idx_min;
781         cur.pos() = ar.x2pos(x - ar.xo());
782         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
783         if (dist_min == 0) {
784                 // hit inside cell
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);
788         }
789 }
790
791
792 DispatchResult
793 MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
794 {
795         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
796
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);
801         }
802
803         if (cmd.button() == mouse_button::button2) {
804                 MathArray ar;
805                 asArray(cur.bv().getClipboard(), ar);
806                 cur.selClear();
807                 cur.setScreenPos(cmd.x, cmd.y);
808                 cur.insert(ar);
809                 cur.bv().update();
810                 return DispatchResult(true, true);
811         }
812
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);
817         }
818
819         return DispatchResult(false);
820 }
821
822
823 DispatchResult
824 MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
825 {
826         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
827         if (cmd.button() == mouse_button::button1) {
828                 first_x = cmd.x;
829                 first_y = cmd.y;
830                 cur.selClear();
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);
835         }
836
837         if (cmd.button() == mouse_button::button2) {
838                 return priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
839         }
840
841         if (cmd.button() == mouse_button::button3) {
842                 return DispatchResult(true, true);
843         }
844
845         return DispatchResult(true, true);
846 }
847
848
849 DispatchResult
850 MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
851 {
852         // only select with button 1
853         if (cmd.button() != mouse_button::button1)
854                 return DispatchResult(true, true);
855
856         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
857                 return DispatchResult(true, true);
858
859         first_x = cmd.x;
860         first_y = cmd.y;
861
862         if (!cur.selection())
863                 cur.selBegin();
864
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);
869 }