]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
f489193b8221056a84fce4136d1f49495cea0133
[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         case LFUN_MOUSE_TRIPLE:
390                 //lyxerr << "Mouse double" << endl;
391                 //lyxerr << "Mouse triple" << endl;
392                 return dispatch(cur, FuncRequest(LFUN_WORDSEL));
393
394         case LFUN_RIGHTSEL:
395         case LFUN_RIGHT:
396                 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
397                 return cur.right() ?
398                         DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
399
400         case LFUN_LEFTSEL:
401         case LFUN_LEFT:
402                 cur.selHandle(cmd.action == LFUN_LEFTSEL);
403                 return cur.left() ?
404                         DispatchResult(true, true) : DispatchResult(false, FINISHED);
405
406         case LFUN_UPSEL:
407         case LFUN_UP:
408                 cur.selHandle(cmd.action == LFUN_UPSEL);
409                 return cur.up() ?
410                         DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
411
412         case LFUN_DOWNSEL:
413         case LFUN_DOWN:
414                 cur.selHandle(cmd.action == LFUN_DOWNSEL);
415                 return cur.down() ?
416                         DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
417
418         case LFUN_WORDSEL:
419                 cur.home();
420                 cur.resetAnchor();
421                 cur.selection() = true;
422                 cur.end();
423                 return DispatchResult(true, true);
424
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);
430
431         case LFUN_WORDLEFTSEL:
432         case LFUN_WORDLEFT:
433                 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL);
434                 return cur.home()
435                         ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
436
437         case LFUN_WORDRIGHTSEL:
438         case LFUN_WORDRIGHT:
439                 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL);
440                 return cur.end()
441                         ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
442
443         case LFUN_HOMESEL:
444         case LFUN_HOME:
445                 cur.selHandle(cmd.action == LFUN_HOMESEL);
446                 return cur.home()
447                         ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
448
449         case LFUN_ENDSEL:
450         case LFUN_END:
451                 cur.selHandle(cmd.action == LFUN_ENDSEL);
452                 return cur.end()
453                         ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
454
455         case LFUN_PRIORSEL:
456         case LFUN_PRIOR:
457         case LFUN_BEGINNINGBUFSEL:
458         case LFUN_BEGINNINGBUF:
459                 return DispatchResult(true, FINISHED);
460
461         case LFUN_NEXTSEL:
462         case LFUN_NEXT:
463         case LFUN_ENDBUFSEL:
464         case LFUN_ENDBUF:
465                 return DispatchResult(false, FINISHED_RIGHT);
466
467         case LFUN_CELL_FORWARD:
468                 cur.inset()->idxNext(cur);
469                 return DispatchResult(true, true);
470
471         case LFUN_CELL_BACKWARD:
472                 cur.inset()->idxPrev(cur);
473                 return DispatchResult(true, true);
474
475         case LFUN_DELETE_WORD_BACKWARD:
476         case LFUN_BACKSPACE:
477                 //recordUndo(cur, Undo::ATOMIC);
478                 cur.backspace();
479                 return DispatchResult(true, true);
480
481         case LFUN_DELETE_WORD_FORWARD:
482         case LFUN_DELETE:
483                 //recordUndo(cur, Undo::ATOMIC);
484                 cur.erase();
485                 return DispatchResult(true, FINISHED);
486
487         case LFUN_ESCAPE:
488                 if (!cur.selection())
489                         return DispatchResult(true, true);
490                 cur.selClear();
491                 return DispatchResult(false);
492
493         case LFUN_INSET_TOGGLE:
494                 cur.lockToggle();
495                 return DispatchResult(true, true);
496
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);
503                                 else
504                                         return DispatchResult(false, FINISHED_RIGHT);
505                         }
506                         cur.insert(cmd.argument);
507                 }
508                 return DispatchResult(false, FINISHED_RIGHT);
509
510
511 #if 0
512 //
513 // this needs to be incorporated
514 //
515         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
516         bool remove_inset = false;
517
518         DispatchResult result(true);
519         bool was_macro     = cur.inMacroMode();
520
521         cur.normalize();
522         cur.touch();
523 #endif
524
525         //    case LFUN_GETXY:
526         //      sprintf(dispatch_buffer, "%d %d",);
527         //      DispatchResult= dispatch_buffer;
528         //      break;
529         case LFUN_SETXY: {
530                 lyxerr << "LFUN_SETXY broken!" << endl;
531                 int x = 0;
532                 int y = 0;
533                 istringstream is(cmd.argument.c_str());
534                 is >> x >> y;
535                 cur.setScreenPos(x, y);
536                 return DispatchResult(true, true);
537         }
538
539         case LFUN_CUT:
540                 //recordUndo(cur, Undo::DELETE);
541                 cur.selCut();
542                 return DispatchResult(true, true);
543
544         case LFUN_COPY:
545                 cur.selCopy();
546                 return DispatchResult(true, true);
547
548
549         // Special casing for superscript in case of LyX handling
550         // dead-keys:
551         case LFUN_CIRCUMFLEX:
552                 if (cmd.argument.empty()) {
553                         // do superscript if LyX handles
554                         // deadkeys
555                         //recordUndo(cur, Undo::ATOMIC);
556                         cur.script(true);
557                 }
558                 return DispatchResult(true, true);
559
560         case LFUN_UMLAUT:
561         case LFUN_ACUTE:
562         case LFUN_GRAVE:
563         case LFUN_BREVE:
564         case LFUN_DOT:
565         case LFUN_MACRON:
566         case LFUN_CARON:
567         case LFUN_TILDE:
568         case LFUN_CEDILLA:
569         case LFUN_CIRCLE:
570         case LFUN_UNDERDOT:
571         case LFUN_TIE:
572         case LFUN_OGONEK:
573         case LFUN_HUNG_UMLAUT:
574                 return DispatchResult(true, true);
575
576         //  Math fonts
577         case LFUN_FREEFONT_APPLY:
578         case LFUN_FREEFONT_UPDATE:
579                 handleFont2(cur, cmd.argument);
580                 return DispatchResult(true, true);
581
582         case LFUN_BOLD:
583                 handleFont(cur, cmd.argument, "mathbf");
584                 return DispatchResult(true, true);
585         case LFUN_SANS:
586                 handleFont(cur, cmd.argument, "mathsf");
587                 return DispatchResult(true, true);
588         case LFUN_EMPH:
589                 handleFont(cur, cmd.argument, "mathcal");
590                 return DispatchResult(true, true);
591         case LFUN_ROMAN:
592                 handleFont(cur, cmd.argument, "mathrm");
593                 return DispatchResult(true, true);
594         case LFUN_CODE:
595                 handleFont(cur, cmd.argument, "texttt");
596                 return DispatchResult(true, true);
597         case LFUN_FRAK:
598                 handleFont(cur, cmd.argument, "mathfrak");
599                 return DispatchResult(true, true);
600         case LFUN_ITAL:
601                 handleFont(cur, cmd.argument, "mathit");
602                 return DispatchResult(true, true);
603         case LFUN_NOUN:
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);
609         case LFUN_DEFAULT:
610                 handleFont(cur, cmd.argument, "textnormal");
611                 return DispatchResult(true, true);
612
613         case LFUN_MATH_MODE:
614 #if 1
615                 cur.macroModeClose();
616                 cur.selClearOrDel();
617                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
618                 cur.posLeft();
619                 cur.pushLeft(cur.nextInset());
620 #else
621                 if (cur.currentMode() == InsetBase::TEXT_MODE)
622                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
623                 else
624                         handleFont(cur, cmd.argument, "textrm");
625                 //cur.owner()->message(_("math text mode toggled"));
626 #endif
627                 return DispatchResult(true, true);
628
629         case LFUN_MATH_SIZE:
630 #if 0
631                 if (!arg.empty()) {
632                         //recordUndo(cur, Undo::ATOMIC);
633                         cur.setSize(arg);
634                 }
635 #endif
636                 return DispatchResult(true, true);
637
638         case LFUN_INSERT_MATRIX: {
639                 //recordUndo(cur, Undo::ATOMIC);
640                 unsigned int m = 1;
641                 unsigned int n = 1;
642                 string v_align;
643                 string h_align;
644                 istringstream is(cmd.argument);
645                 is >> m >> n >> v_align >> h_align;
646                 if (m < 1)
647                         m = 1;
648                 if (n < 1)
649                         n = 1;
650                 v_align += 'c';
651                 cur.niceInsert(
652                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
653                 return DispatchResult(true, true);
654         }
655
656         case LFUN_MATH_DELIM: {
657                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
658                 string ls;
659                 string rs = lyx::support::split(cmd.argument, ls, ' ');
660                 // Reasonable default values
661                 if (ls.empty())
662                         ls = '(';
663                 if (rs.empty())
664                         rs = ')';
665                 //recordUndo(cur, Undo::ATOMIC);
666                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
667                 return DispatchResult(true, true);
668         }
669
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);
675
676         case LFUN_UNDO:
677 #warning look here
678                 //cur.bv().owner()->message(_("Invalid action in math mode!"));
679                 return DispatchResult(true, true);
680
681         case LFUN_INSET_ERT:
682                 // interpret this as if a backslash was typed
683                 //recordUndo(cur, Undo::ATOMIC);
684                 cur.interpret('\\');
685                 return DispatchResult(true, true);
686
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);
694
695         case LFUN_DIALOG_SHOW:
696                 return DispatchResult(false);
697
698         case LFUN_DIALOG_SHOW_NEW_INSET: {
699                 string const & name = cmd.argument;
700                 string data;
701 #if 0
702                 if (name == "ref") {
703                         RefInset tmp(name);
704                         data = tmp.createDialogStr(name);
705                 }
706 #endif
707                 if (data.empty())
708                         return DispatchResult(false);
709                 cur.bv().owner()->getDialogs().show(name, data, 0);
710                 return DispatchResult(true, true);
711         }
712
713         case LFUN_INSET_APPLY: {
714                 string const name = cmd.getArg(0);
715                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
716
717                 if (base) {
718                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
719                         return base->dispatch(cur, fr);
720                 }
721                 MathArray ar;
722                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
723                         cur.insert(ar);
724                         return DispatchResult(true, true);
725                 }
726                 return DispatchResult(false);
727         }
728
729 #warning look here
730 #if 0
731
732         case LFUN_WORD_REPLACE:
733         case LFUN_WORD_FIND:
734                 return
735                         searchForward(&cur.bv(), cmd.getArg(0), false, false)
736                                 ? DispatchResult(true, true) : DispatchResult(false);
737
738         cur.normalize();
739         cur.touch();
740
741         BOOST_ASSERT(cur.inMathed());
742
743         if (result.dispatched()) {
744                 revealCodes(cur);
745                 cur.bv().stuffClipboard(cur.grabSelection());
746         } else {
747                 cur.releaseMathCursor();
748                 if (remove_inset)
749                         cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
750         }
751
752         return result;  // original version
753 #endif
754
755         default:
756                 return MathDimInset::priv_dispatch(cur, cmd);
757         }
758 }
759
760
761 void MathNestInset::edit(LCursor & cur, bool left)
762 {
763         cur.push(this);
764         cur.idx() = left ? 0 : cur.lastidx();
765         cur.pos() = left ? 0 : cur.lastpos();
766         cur.resetAnchor();
767 }
768
769
770 void MathNestInset::edit(LCursor & cur, int x, int y)
771 {
772         int idx_min = 0;
773         int dist_min = 1000000;
774         for (idx_type i = 0; i < nargs(); ++i) {
775                 int d = cell(i).dist(x, y);
776                 if (d < dist_min) {
777                         dist_min = d;
778                         idx_min = i;
779                 }
780         }
781         MathArray & ar = cell(idx_min);
782         cur.push(this);
783         cur.idx() = idx_min;
784         cur.pos() = ar.x2pos(x - ar.xo());
785         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
786         if (dist_min == 0) {
787                 // hit inside cell
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);
791         }
792 }
793
794
795 DispatchResult
796 MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
797 {
798         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
799
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);
804         }
805
806         if (cmd.button() == mouse_button::button2) {
807                 MathArray ar;
808                 asArray(cur.bv().getClipboard(), ar);
809                 cur.selClear();
810                 cur.setScreenPos(cmd.x, cmd.y);
811                 cur.insert(ar);
812                 cur.bv().update();
813                 return DispatchResult(true, true);
814         }
815
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);
820         }
821
822         return DispatchResult(false);
823 }
824
825
826 DispatchResult
827 MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
828 {
829         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
830         if (cmd.button() == mouse_button::button1) {
831                 first_x = cmd.x;
832                 first_y = cmd.y;
833                 cur.selClear();
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);
838         }
839
840         if (cmd.button() == mouse_button::button2) {
841                 return priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
842         }
843
844         if (cmd.button() == mouse_button::button3) {
845                 return DispatchResult(true, true);
846         }
847
848         return DispatchResult(true, true);
849 }
850
851
852 DispatchResult
853 MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
854 {
855         // only select with button 1
856         if (cmd.button() != mouse_button::button1)
857                 return DispatchResult(true, true);
858
859         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
860                 return DispatchResult(true, true);
861
862         first_x = cmd.x;
863         first_y = cmd.y;
864
865         if (!cur.selection())
866                 cur.selBegin();
867
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);
872 }