]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
30921ba6f8116e433f0f4b6d2132260356551241
[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 void MathNestInset::dump() const
168 {
169         WriteStream os(lyxerr);
170         os << "---------------------------------------------\n";
171         write(os);
172         os << "\n";
173         for (idx_type i = 0; i < nargs(); ++i)
174                 os << cell(i) << "\n";
175         os << "---------------------------------------------\n";
176 }
177
178
179 //void MathNestInset::draw(PainterInfo & pi, int x, int y) const
180 void MathNestInset::draw(PainterInfo &, int, int) const
181 {
182 #if 0
183         if (lock_)
184                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
185                                         LColor::mathlockbg);
186 #endif
187 }
188
189
190 void MathNestInset::drawSelection(PainterInfo & pi, int, int) const
191 {
192         // this should use the x/y values given, not the cached values
193         LCursor & cur = pi.base.bv->cursor();
194         if (!cur.selection())
195                 return;
196         if (!ptr_cmp(cur.inset(), this))
197                 return;
198         CursorSlice & s1 = cur.selBegin();
199         CursorSlice & s2 = cur.selEnd();
200         if (s1.idx() == s2.idx()) {
201                 MathArray const & c = cell(s1.idx());
202                 int x1 = c.xo() + c.pos2x(s1.pos());
203                 int y1 = c.yo() - c.ascent();
204                 int x2 = c.xo() + c.pos2x(s2.pos());
205                 int y2 = c.yo() + c.descent();
206                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
207         } else {
208                 for (idx_type i = 0; i < nargs(); ++i) {
209                         if (idxBetween(i, s1.idx(), s2.idx())) {
210                                 MathArray const & c = cell(i);
211                                 int x1 = c.xo();
212                                 int y1 = c.yo() - c.ascent();
213                                 int x2 = c.xo() + c.width();
214                                 int y2 = c.yo() + c.descent();
215                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
216                         }
217                 }
218         }
219 }
220
221
222 void MathNestInset::validate(LaTeXFeatures & features) const
223 {
224         for (idx_type i = 0; i < nargs(); ++i)
225                 cell(i).validate(features);
226 }
227
228
229 void MathNestInset::replace(ReplaceData & rep)
230 {
231         for (idx_type i = 0; i < nargs(); ++i)
232                 cell(i).replace(rep);
233 }
234
235
236 bool MathNestInset::contains(MathArray const & ar) const
237 {
238         for (idx_type i = 0; i < nargs(); ++i)
239                 if (cell(i).contains(ar))
240                         return true;
241         return false;
242 }
243
244
245 bool MathNestInset::lock() const
246 {
247         return lock_;
248 }
249
250
251 void MathNestInset::lock(bool l)
252 {
253         lock_ = l;
254 }
255
256
257 bool MathNestInset::isActive() const
258 {
259         return nargs() > 0;
260 }
261
262
263 MathArray MathNestInset::glue() const
264 {
265         MathArray ar;
266         for (size_t i = 0; i < nargs(); ++i)
267                 ar.append(cell(i));
268         return ar;
269 }
270
271
272 void MathNestInset::write(WriteStream & os) const
273 {
274         os << '\\' << name().c_str();
275         for (size_t i = 0; i < nargs(); ++i)
276                 os << '{' << cell(i) << '}';
277         if (nargs() == 0)
278                 os.pendingSpace(true);
279         if (lock_ && !os.latex()) {
280                 os << "\\lyxlock";
281                 os.pendingSpace(true);
282         }
283 }
284
285
286 void MathNestInset::normalize(NormalStream & os) const
287 {
288         os << '[' << name().c_str();
289         for (size_t i = 0; i < nargs(); ++i)
290                 os << ' ' << cell(i);
291         os << ']';
292 }
293
294
295 void MathNestInset::notifyCursorLeaves(idx_type idx)
296 {
297         cell(idx).notifyCursorLeaves();
298 }
299
300
301 void MathNestInset::handleFont
302         (LCursor & cur, string const & arg, string const & font)
303 {
304         // this whole function is a hack and won't work for incremental font
305         // changes...
306         //recordUndo(cur, Undo::ATOMIC);
307
308         if (cur.inset()->asMathInset()->name() == font)
309                 cur.handleFont(font);
310         else {
311                 cur.handleNest(createMathInset(font));
312                 cur.insert(arg);
313         }
314 }
315
316
317 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
318 {
319         //recordUndo(cur, Undo::ATOMIC);
320         LyXFont font;
321         bool b;
322         bv_funcs::string2font(arg, font, b);
323         if (font.color() != LColor::inherit) {
324                 MathAtom at = createMathInset("color");
325                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
326                 cur.handleNest(at, 1);
327         }
328 }
329
330
331 void MathNestInset::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
332 {
333         lyxerr << "MathNestInset: request: " << cmd << std::endl;
334         CursorSlice sl = cur.current();
335
336         switch (cmd.action) {
337
338         case LFUN_PASTE:
339                 if (!cmd.argument.empty()) {
340                         MathArray ar;
341                         mathed_parse_cell(ar, cmd.argument);
342                         cur.cell().insert(cur.pos(), ar);
343                         cur.pos() += ar.size();
344                 }
345                 break;
346 /*
347         case LFUN_PASTE: {
348                 size_t n = 0;
349                 istringstream is(cmd.argument.c_str());
350                 is >> n;
351                 if (was_macro)
352                         cur.macroModeClose();
353                 //recordUndo(cur, Undo::ATOMIC);
354                 cur.selPaste(n);
355                 break;
356         }
357 */
358
359         case LFUN_PASTESELECTION:
360                 dispatch(cur, FuncRequest(LFUN_PASTE, cur.bv().getClipboard()));
361                 break;
362
363         case LFUN_MOUSE_PRESS:
364                 lfunMousePress(cur, cmd);
365                 break;
366
367         case LFUN_MOUSE_MOTION:
368                 lfunMouseMotion(cur, cmd);
369                 break;
370
371         case LFUN_MOUSE_RELEASE:
372                 lfunMouseRelease(cur, cmd);
373                 break;
374
375         case LFUN_MOUSE_DOUBLE:
376         case LFUN_MOUSE_TRIPLE:
377                 //lyxerr << "Mouse double" << endl;
378                 //lyxerr << "Mouse triple" << endl;
379                 dispatch(cur, FuncRequest(LFUN_WORDSEL));
380                 break;
381
382         case LFUN_FINISHED_LEFT:
383                 cur.pop(cur.currentDepth());
384                 cur.bv().cursor() = cur;
385                 break;
386
387         case LFUN_FINISHED_RIGHT:
388                 cur.pop(cur.currentDepth());
389                 ++cur.pos();
390                 cur.bv().cursor() = cur;
391                 break;
392
393         case LFUN_FINISHED_UP:
394                 cur.pop(cur.currentDepth());
395                 //idxUpDown(cur, true);
396                 cur.bv().cursor() = cur;
397                 break;
398
399         case LFUN_FINISHED_DOWN:
400                 cur.pop(cur.currentDepth());
401                 //idxUpDown(cur, false);
402                 cur.bv().cursor() = cur;
403                 break;
404
405         case LFUN_RIGHTSEL:
406         case LFUN_RIGHT:
407                 lyxerr << "mathnest RIGHT: from:\n" << cur << endl;
408                 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
409                 cur.autocorrect() = false;
410                 cur.clearTargetX();
411                 if (cur.inMacroMode())
412                         cur.macroModeClose();
413                 else if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
414                         cur.pushLeft(cur.nextAtom().nucleus());
415                         cur.inset()->idxFirst(cur);
416                 } else if (cur.posRight() || idxRight(cur)
417                         || cur.popRight() || cur.selection())
418                         ;
419                 else
420                         cur.dispatched(FINISHED_RIGHT);
421                 lyxerr << "mathnest RIGHT: to:\n" << cur << endl;
422                 break;
423
424         case LFUN_LEFTSEL:
425         case LFUN_LEFT:
426                 cur.selHandle(cmd.action == LFUN_LEFTSEL);
427                 cur.autocorrect() = false;
428                 cur.clearTargetX();
429                 if (cur.inMacroMode())
430                         cur.macroModeClose();
431                 else if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
432                         cur.posLeft();
433                         cur.push(cur.nextAtom().nucleus());
434                         cur.inset()->idxLast(cur);
435                 } else if (cur.posLeft() || idxLeft(cur)
436                         || cur.popLeft() || cur.selection())
437                         ;
438                 else
439                         cur.dispatched(FINISHED_LEFT);
440                 break;
441
442         case LFUN_UPSEL:
443         case LFUN_UP:
444                 cur.selHandle(cmd.action == LFUN_UPSEL);
445                 if (!cur.up())
446                         cur.dispatched(FINISHED_UP);
447                 break;
448
449         case LFUN_DOWNSEL:
450         case LFUN_DOWN:
451                 cur.selHandle(cmd.action == LFUN_DOWNSEL);
452                 if (!cur.down())
453                         cur.dispatched(FINISHED_DOWN);
454                 break;
455
456         case LFUN_WORDSEL:
457                 cur.pos() = 0;
458                 cur.idx() = 0;
459                 cur.resetAnchor();
460                 cur.selection() = true;
461                 cur.pos() = cur.lastpos();
462                 cur.idx() = cur.lastidx();
463                 break;
464
465         case LFUN_UP_PARAGRAPHSEL:
466         case LFUN_UP_PARAGRAPH:
467         case LFUN_DOWN_PARAGRAPHSEL:
468         case LFUN_DOWN_PARAGRAPH:
469                 break;
470
471         case LFUN_HOMESEL:
472         case LFUN_HOME:
473         case LFUN_WORDLEFTSEL:
474         case LFUN_WORDLEFT:
475                 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
476                 cur.macroModeClose();
477                 if (cur.pos() != 0) {
478                         cur.pos() = 0;
479                 } else if (cur.col() != 0) {
480                         cur.idx() -= cur.col();
481                         cur.pos() = 0;
482                 } else if (cur.idx() != 0) {
483                         cur.idx() = 0;
484                         cur.pos() = 0;
485                 } else {
486                         cur.dispatched(FINISHED_LEFT);
487                 }
488                 break;
489
490         case LFUN_WORDRIGHTSEL:
491         case LFUN_WORDRIGHT:
492         case LFUN_ENDSEL:
493         case LFUN_END:
494                 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
495                 cur.macroModeClose();
496                 cur.clearTargetX();
497                 if (cur.pos() != cur.lastpos()) {
498                         cur.pos() = cur.lastpos();
499                 } else if (cur.col() != cur.lastcol()) {
500                         cur.idx() = cur.idx() - cur.col() + cur.lastcol();
501                         cur.pos() = cur.lastpos();
502                 } else if (cur.idx() != cur.lastidx()) {
503                         cur.idx() = cur.lastidx();
504                         cur.pos() = cur.lastpos();
505                 } else {
506                         cur.dispatched(FINISHED_RIGHT);
507                 }
508                 break;
509
510         case LFUN_PRIORSEL:
511         case LFUN_PRIOR:
512         case LFUN_BEGINNINGBUFSEL:
513         case LFUN_BEGINNINGBUF:
514                 cur.dispatched(FINISHED_LEFT);
515                 break;
516
517         case LFUN_NEXTSEL:
518         case LFUN_NEXT:
519         case LFUN_ENDBUFSEL:
520         case LFUN_ENDBUF:
521                 cur.dispatched(FINISHED_RIGHT);
522                 break;
523
524         case LFUN_CELL_FORWARD:
525                 cur.inset()->idxNext(cur);
526                 break;
527
528         case LFUN_CELL_BACKWARD:
529                 cur.inset()->idxPrev(cur);
530                 break;
531
532         case LFUN_DELETE_WORD_BACKWARD:
533         case LFUN_BACKSPACE:
534                 //recordUndo(cur, Undo::ATOMIC);
535                 cur.backspace();
536                 break;
537
538         case LFUN_DELETE_WORD_FORWARD:
539         case LFUN_DELETE:
540                 //recordUndo(cur, Undo::ATOMIC);
541                 cur.erase();
542                 cur.dispatched(FINISHED_LEFT);
543                 break;
544
545         case LFUN_ESCAPE:
546                 if (cur.selection()) 
547                         cur.selClear();
548                 else 
549                         cur.dispatched(FINISHED_LEFT);
550                 break;
551
552         case LFUN_INSET_TOGGLE:
553                 cur.lockToggle();
554                 break;
555
556         case LFUN_SELFINSERT:
557                 if (cmd.argument.empty()) {
558                         cur.dispatched(FINISHED_RIGHT);
559                         break;
560                 }
561                 //recordUndo(cur, Undo::ATOMIC);
562                 if (cmd.argument.size() != 1) {
563                         cur.insert(cmd.argument);
564                         break;
565                 }
566                 if (!cur.interpret(cmd.argument[0]))
567                         cur.dispatched(FINISHED_RIGHT);
568                 break;
569
570 #if 0
571 //
572 // this needs to be incorporated
573 //
574         // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
575         bool remove_inset = false;
576
577         DispatchResult result(true);
578         bool was_macro     = cur.inMacroMode();
579
580         cur.normalize();
581         cur.touch();
582 #endif
583
584         //    case LFUN_GETXY:
585         //      sprintf(dispatch_buffer, "%d %d",);
586         //      DispatchResult= dispatch_buffer;
587         //      break;
588         case LFUN_SETXY: {
589                 lyxerr << "LFUN_SETXY broken!" << endl;
590                 int x = 0;
591                 int y = 0;
592                 istringstream is(cmd.argument.c_str());
593                 is >> x >> y;
594                 cur.setScreenPos(x, y);
595                 break;
596         }
597
598         case LFUN_CUT:
599                 //recordUndo(cur, Undo::DELETE);
600                 cur.selCut();
601                 break;
602
603         case LFUN_COPY:
604                 cur.selCopy();
605                 break;
606
607         // Special casing for superscript in case of LyX handling
608         // dead-keys:
609         case LFUN_CIRCUMFLEX:
610                 if (cmd.argument.empty()) {
611                         // do superscript if LyX handles
612                         // deadkeys
613                         //recordUndo(cur, Undo::ATOMIC);
614                         cur.script(true);
615                 }
616                 break;
617
618         case LFUN_UMLAUT:
619         case LFUN_ACUTE:
620         case LFUN_GRAVE:
621         case LFUN_BREVE:
622         case LFUN_DOT:
623         case LFUN_MACRON:
624         case LFUN_CARON:
625         case LFUN_TILDE:
626         case LFUN_CEDILLA:
627         case LFUN_CIRCLE:
628         case LFUN_UNDERDOT:
629         case LFUN_TIE:
630         case LFUN_OGONEK:
631         case LFUN_HUNG_UMLAUT:
632                 break;
633
634         //  Math fonts
635         case LFUN_FREEFONT_APPLY:
636         case LFUN_FREEFONT_UPDATE:
637                 handleFont2(cur, cmd.argument);
638                 break;
639
640         case LFUN_BOLD:
641                 handleFont(cur, cmd.argument, "mathbf");
642                 break;
643         case LFUN_SANS:
644                 handleFont(cur, cmd.argument, "mathsf");
645                 break;
646         case LFUN_EMPH:
647                 handleFont(cur, cmd.argument, "mathcal");
648                 break;
649         case LFUN_ROMAN:
650                 handleFont(cur, cmd.argument, "mathrm");
651                 break;
652         case LFUN_CODE:
653                 handleFont(cur, cmd.argument, "texttt");
654                 break;
655         case LFUN_FRAK:
656                 handleFont(cur, cmd.argument, "mathfrak");
657                 break;
658         case LFUN_ITAL:
659                 handleFont(cur, cmd.argument, "mathit");
660                 break;
661         case LFUN_NOUN:
662                 handleFont(cur, cmd.argument, "mathbb");
663                 break;
664         //case LFUN_FREEFONT_APPLY:
665                 handleFont(cur, cmd.argument, "textrm");
666                 break;
667         case LFUN_DEFAULT:
668                 handleFont(cur, cmd.argument, "textnormal");
669                 break;
670
671         case LFUN_MATH_MODE:
672 #if 1
673                 cur.macroModeClose();
674                 cur.selClearOrDel();
675                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
676                 cur.posLeft();
677                 cur.pushLeft(cur.nextInset());
678 #else
679                 if (cur.currentMode() == InsetBase::TEXT_MODE)
680                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
681                 else
682                         handleFont(cur, cmd.argument, "textrm");
683                 //cur.owner()->message(_("math text mode toggled"));
684 #endif
685                 break;
686
687         case LFUN_MATH_SIZE:
688 #if 0
689                 if (!arg.empty()) {
690                         //recordUndo(cur, Undo::ATOMIC);
691                         cur.setSize(arg);
692                 }
693 #endif
694                 break;
695
696         case LFUN_INSERT_MATRIX: {
697                 //recordUndo(cur, Undo::ATOMIC);
698                 unsigned int m = 1;
699                 unsigned int n = 1;
700                 string v_align;
701                 string h_align;
702                 istringstream is(cmd.argument);
703                 is >> m >> n >> v_align >> h_align;
704                 if (m < 1)
705                         m = 1;
706                 if (n < 1)
707                         n = 1;
708                 v_align += 'c';
709                 cur.niceInsert(
710                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
711                 break;
712         }
713
714         case LFUN_MATH_DELIM: {
715                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
716                 string ls;
717                 string rs = lyx::support::split(cmd.argument, ls, ' ');
718                 // Reasonable default values
719                 if (ls.empty())
720                         ls = '(';
721                 if (rs.empty())
722                         rs = ')';
723                 //recordUndo(cur, Undo::ATOMIC);
724                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
725                 break;
726         }
727
728         case LFUN_SPACE_INSERT:
729         case LFUN_MATH_SPACE:
730                 //recordUndo(cur, Undo::ATOMIC);
731                 cur.insert(MathAtom(new MathSpaceInset(",")));
732                 break;
733
734         case LFUN_UNDO:
735 #warning look here
736                 //cur.bv().owner()->message(_("Invalid action in math mode!"));
737                 break;
738
739         case LFUN_INSET_ERT:
740                 // interpret this as if a backslash was typed
741                 //recordUndo(cur, Undo::ATOMIC);
742                 cur.interpret('\\');
743                 break;
744
745 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
746 // handling such that "self-insert" works on "arbitrary stuff" too, and
747 // math-insert only handles special math things like "matrix".
748         case LFUN_INSERT_MATH:
749                 //recordUndo(cur, Undo::ATOMIC);
750                 cur.niceInsert(cmd.argument);
751                 break;
752
753         case LFUN_DIALOG_SHOW_NEW_INSET: {
754                 string const & name = cmd.argument;
755                 string data;
756 #if 0
757                 if (name == "ref") {
758                         RefInset tmp(name);
759                         data = tmp.createDialogStr(name);
760                 }
761 #endif
762                 cur.bv().owner()->getDialogs().show(name, data, 0);
763                 break;
764         }
765
766         case LFUN_INSET_APPLY: {
767                 string const name = cmd.getArg(0);
768                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
769
770                 if (base) {
771                         base->dispatch(cur, FuncRequest(LFUN_INSET_MODIFY, cmd.argument));
772                         break;
773                 }
774                 MathArray ar;
775                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
776                         cur.insert(ar);
777                         break;
778                 }
779                 cur.notdispatched();
780                 break;
781         }
782
783 #warning look here
784 #if 0
785
786         case LFUN_WORD_REPLACE:
787         case LFUN_WORD_FIND:
788                 if (!searchForward(&cur.bv(), cmd.getArg(0), false, false))
789                         cur.notdispatched();
790                 break;
791
792         cur.normalize();
793         cur.touch();
794
795         BOOST_ASSERT(cur.inMathed());
796
797         if (result.dispatched()) {
798                 revealCodes(cur);
799                 cur.bv().stuffClipboard(cur.grabSelection());
800         } else {
801                 if (remove_inset)
802                         cur.bv().owner()->dispatch(FuncRequest(LFUN_DELETE));
803         }
804         break;
805 #endif
806
807         default:
808                 MathDimInset::priv_dispatch(cur, cmd);
809                 break;
810         }
811 }
812
813
814 void MathNestInset::edit(LCursor & cur, bool left)
815 {
816         cur.push(this);
817         cur.idx() = left ? 0 : cur.lastidx();
818         cur.pos() = left ? 0 : cur.lastpos();
819         cur.resetAnchor();
820 }
821
822
823 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
824 {
825         int idx_min = 0;
826         int dist_min = 1000000;
827         for (idx_type i = 0; i < nargs(); ++i) {
828                 int d = cell(i).dist(x, y);
829                 if (d < dist_min) {
830                         dist_min = d;
831                         idx_min = i;
832                 }
833         }
834         MathArray & ar = cell(idx_min);
835         cur.push(this);
836         cur.idx() = idx_min;
837         cur.pos() = ar.x2pos(x - ar.xo());
838         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
839         if (dist_min == 0) {
840                 // hit inside cell
841                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
842                         if (ar[i]->covers(x, y))
843                                 return ar[i].nucleus()->editXY(cur, x, y);
844         }
845         return this;
846 }
847
848
849 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest const & cmd)
850 {
851         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
852
853         if (cmd.button() == mouse_button::button1) {
854                 // try to dispatch to enclosed insets first
855                 //cur.bv().stuffClipboard(cur.grabSelection());
856                 return;
857         }
858
859         if (cmd.button() == mouse_button::button2) {
860                 MathArray ar;
861                 asArray(cur.bv().getClipboard(), ar);
862                 cur.selClear();
863                 cur.setScreenPos(cmd.x, cmd.y);
864                 cur.insert(ar);
865                 cur.bv().update();
866                 return;
867         }
868
869         if (cmd.button() == mouse_button::button3) {
870                 // try to dispatch to enclosed insets first
871                 cur.bv().owner()->getDialogs().show("mathpanel");
872                 return;
873         }
874
875         cur.notdispatched();
876 }
877
878
879 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest const & cmd)
880 {
881         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
882         if (cmd.button() == mouse_button::button1) {
883                 first_x = cmd.x;
884                 first_y = cmd.y;
885                 cur.selClear();
886                 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
887                 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
888                 cur.bv().cursor() = cur;
889         }
890
891         if (cmd.button() == mouse_button::button2) {
892                 priv_dispatch(cur, FuncRequest(LFUN_PASTESELECTION));
893         }
894 }
895
896
897 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest const & cmd)
898 {
899         // only select with button 1
900         if (cmd.button() != mouse_button::button1)
901                 return;
902
903         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
904                 return;
905
906         first_x = cmd.x;
907         first_y = cmd.y;
908
909         if (!cur.selection())
910                 cur.selBegin();
911
912         //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
913         cur.bv().cursor().cursor_ = cur.cursor_;
914         cur.bv().cursor().selection() = true;
915         return;
916 }