]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
3306b0638487ceadef47682a0dcc03c330ab4a0b
[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_braceinset.h"
17 #include "math_commentinset.h"
18 #include "math_data.h"
19 #include "math_deliminset.h"
20 #include "math_factory.h"
21 #include "math_hullinset.h"
22 #include "math_mathmlstream.h"
23 #include "math_macroarg.h"
24 #include "math_mboxinset.h"
25 #include "math_parser.h"
26 #include "math_scriptinset.h"
27 #include "math_spaceinset.h"
28 #include "math_symbolinset.h"
29 #include "math_support.h"
30 #include "math_unknowninset.h"
31
32 #include "BufferView.h"
33 #include "CutAndPaste.h"
34 #include "FuncStatus.h"
35 #include "LColor.h"
36 #include "bufferview_funcs.h"
37 #include "cursor.h"
38 #include "debug.h"
39 #include "dispatchresult.h"
40 #include "funcrequest.h"
41 #include "gettext.h"
42 #include "outputparams.h"
43 #include "undo.h"
44
45 #include "support/lstrings.h"
46
47 #include "frontends/Dialogs.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
50
51 #include <sstream>
52
53 using lyx::cap::copySelection;
54 using lyx::cap::grabAndEraseSelection;
55 using lyx::cap::cutSelection;
56 using lyx::cap::pasteSelection;
57 using lyx::cap::replaceSelection;
58 using lyx::cap::selClearOrDel;
59
60 using std::endl;
61 using std::string;
62 using std::istringstream;
63
64
65
66 namespace {
67
68 // local global
69 int first_x;
70 int first_y;
71
72 } // namespace anon
73
74
75
76
77 MathNestInset::MathNestInset(idx_type nargs)
78         : cells_(nargs), lock_(false)
79 {}
80
81
82 MathInset::idx_type MathNestInset::nargs() const
83 {
84         return cells_.size();
85 }
86
87
88 MathArray & MathNestInset::cell(idx_type i)
89 {
90         return cells_[i];
91 }
92
93
94 MathArray const & MathNestInset::cell(idx_type i) const
95 {
96         return cells_[i];
97 }
98
99
100 void MathNestInset::getCursorPos(LCursor const & cur, int & x, int & y) const
101 {
102         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
103         MathArray const & ar = cur.cell();
104         x = ar.xo() + ar.pos2x(cur.pos());
105         y = ar.yo() + cur.bv().top_y();
106         // move cursor visually into empty cells ("blue rectangles");
107         if (cur.cell().empty())
108                 x += 2;
109 }
110
111
112 void MathNestInset::metrics(MetricsInfo const & mi) const
113 {
114         MetricsInfo m = mi;
115         for (idx_type i = 0; i < nargs(); ++i)
116                 cell(i).metrics(m);
117 }
118
119
120 bool MathNestInset::idxNext(LCursor & cur) const
121 {
122         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
123         if (cur.idx() == cur.lastidx())
124                 return false;
125         ++cur.idx();
126         cur.pos() = 0;
127         return true;
128 }
129
130
131 bool MathNestInset::idxRight(LCursor & cur) const
132 {
133         return idxNext(cur);
134 }
135
136
137 bool MathNestInset::idxPrev(LCursor & cur) const
138 {
139         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
140         if (cur.idx() == 0)
141                 return false;
142         --cur.idx();
143         cur.pos() = cur.lastpos();
144         return true;
145 }
146
147
148 bool MathNestInset::idxLeft(LCursor & cur) const
149 {
150         return idxPrev(cur);
151 }
152
153
154 bool MathNestInset::idxFirst(LCursor & cur) const
155 {
156         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
157         if (nargs() == 0)
158                 return false;
159         cur.idx() = 0;
160         cur.pos() = 0;
161         return true;
162 }
163
164
165 bool MathNestInset::idxLast(LCursor & cur) const
166 {
167         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
168         if (nargs() == 0)
169                 return false;
170         cur.idx() = cur.lastidx();
171         cur.pos() = cur.lastpos();
172         return true;
173 }
174
175
176 void MathNestInset::dump() const
177 {
178         WriteStream os(lyxerr);
179         os << "---------------------------------------------\n";
180         write(os);
181         os << "\n";
182         for (idx_type i = 0; i < nargs(); ++i)
183                 os << cell(i) << "\n";
184         os << "---------------------------------------------\n";
185 }
186
187
188 void MathNestInset::draw(PainterInfo & pi, int x, int y) const
189 {
190 #if 0
191         if (lock_)
192                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
193                                         LColor::mathlockbg);
194 #endif
195         setPosCache(pi, x, y);
196 }
197
198
199 void MathNestInset::drawSelection(PainterInfo & pi, int, int) const
200 {
201         // this should use the x/y values given, not the cached values
202         LCursor & cur = pi.base.bv->cursor();
203         if (!cur.selection())
204                 return;
205         if (!ptr_cmp(&cur.inset(), this))
206                 return;
207         CursorSlice s1 = cur.selBegin();
208         CursorSlice s2 = cur.selEnd();
209         //lyxerr << "MathNestInset::drawing selection: "
210         //      << " s1: " << s1 << " s2: " << s2 << endl;
211         if (s1.idx() == s2.idx()) {
212                 MathArray const & c = cell(s1.idx());
213                 int x1 = c.xo() + c.pos2x(s1.pos());
214                 int y1 = c.yo() - c.ascent();
215                 int x2 = c.xo() + c.pos2x(s2.pos());
216                 int y2 = c.yo() + c.descent();
217                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
218         //lyxerr << "MathNestInset::drawing selection 3: "
219         //      << " x1: " << x1 << " x2: " << x2
220         //      << " y1: " << y1 << " y2: " << y2 << endl;
221         } else {
222                 for (idx_type i = 0; i < nargs(); ++i) {
223                         if (idxBetween(i, s1.idx(), s2.idx())) {
224                                 MathArray const & c = cell(i);
225                                 int x1 = c.xo();
226                                 int y1 = c.yo() - c.ascent();
227                                 int x2 = c.xo() + c.width();
228                                 int y2 = c.yo() + c.descent();
229                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
230                         }
231                 }
232         }
233 }
234
235
236 void MathNestInset::validate(LaTeXFeatures & features) const
237 {
238         for (idx_type i = 0; i < nargs(); ++i)
239                 cell(i).validate(features);
240 }
241
242
243 void MathNestInset::replace(ReplaceData & rep)
244 {
245         for (idx_type i = 0; i < nargs(); ++i)
246                 cell(i).replace(rep);
247 }
248
249
250 bool MathNestInset::contains(MathArray const & ar) const
251 {
252         for (idx_type i = 0; i < nargs(); ++i)
253                 if (cell(i).contains(ar))
254                         return true;
255         return false;
256 }
257
258
259 bool MathNestInset::lock() const
260 {
261         return lock_;
262 }
263
264
265 void MathNestInset::lock(bool l)
266 {
267         lock_ = l;
268 }
269
270
271 bool MathNestInset::isActive() const
272 {
273         return nargs() > 0;
274 }
275
276
277 MathArray MathNestInset::glue() const
278 {
279         MathArray ar;
280         for (size_t i = 0; i < nargs(); ++i)
281                 ar.append(cell(i));
282         return ar;
283 }
284
285
286 void MathNestInset::write(WriteStream & os) const
287 {
288         os << '\\' << name().c_str();
289         for (size_t i = 0; i < nargs(); ++i)
290                 os << '{' << cell(i) << '}';
291         if (nargs() == 0)
292                 os.pendingSpace(true);
293         if (lock_ && !os.latex()) {
294                 os << "\\lyxlock";
295                 os.pendingSpace(true);
296         }
297 }
298
299
300 void MathNestInset::normalize(NormalStream & os) const
301 {
302         os << '[' << name().c_str();
303         for (size_t i = 0; i < nargs(); ++i)
304                 os << ' ' << cell(i);
305         os << ']';
306 }
307
308
309 int MathNestInset::latex(Buffer const &, std::ostream & os,
310                         OutputParams const & runparams) const
311 {
312         WriteStream wi(os, runparams.moving_arg, true);
313         write(wi);
314         return wi.line();
315 }
316
317
318 void MathNestInset::notifyCursorLeaves(LCursor & /*cur*/)
319 {
320 #ifdef WITH_WARNINGS
321 #warning look here
322 #endif
323 #if 0
324         MathArray & ar = cur.cell();
325         // remove base-only "scripts"
326         for (pos_type i = 0; i + 1 < ar.size(); ++i) {
327                 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
328                 if (p && p->nargs() == 1) {
329                         MathArray ar = p->nuc();
330                         erase(i);
331                         insert(i, ar);
332                         cur.adjust(i, ar.size() - 1);
333                 }
334         }
335
336         // glue adjacent font insets of the same kind
337         for (pos_type i = 0; i + 1 < size(); ++i) {
338                 MathFontInset * p = operator[](i).nucleus()->asFontInset();
339                 MathFontInset const * q = operator[](i + 1)->asFontInset();
340                 if (p && q && p->name() == q->name()) {
341                         p->cell(0).append(q->cell(0));
342                         erase(i + 1);
343                         cur.adjust(i, -1);
344                 }
345         }
346 #endif
347 }
348
349
350 void MathNestInset::handleFont
351         (LCursor & cur, string const & arg, string const & font)
352 {
353         // this whole function is a hack and won't work for incremental font
354         // changes...
355         recordUndo(cur, Undo::ATOMIC);
356
357         if (cur.inset().asMathInset()->name() == font)
358                 cur.handleFont(font);
359         else {
360                 cur.handleNest(createMathInset(font));
361                 cur.insert(arg);
362         }
363 }
364
365
366 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
367 {
368         recordUndo(cur, Undo::ATOMIC);
369         LyXFont font;
370         bool b;
371         bv_funcs::string2font(arg, font, b);
372         if (font.color() != LColor::inherit) {
373                 MathAtom at = createMathInset("color");
374                 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
375                 cur.handleNest(at, 1);
376         }
377 }
378
379
380 void MathNestInset::priv_dispatch(LCursor & cur, FuncRequest & cmd)
381 {
382         //lyxerr << "MathNestInset: request: " << cmd << std::endl;
383         //CursorSlice sl = cur.current();
384
385         switch (cmd.action) {
386
387         case LFUN_PASTE: {
388                 recordUndo(cur);
389                 cur.message(_("Paste"));
390                 replaceSelection(cur);
391                 size_t n = 0;
392                 istringstream is(cmd.argument.c_str());
393                 is >> n;
394                 pasteSelection(cur, n);
395                 cur.clearSelection(); // bug 393
396                 cur.bv().switchKeyMap();
397                 finishUndo();
398                 break;
399         }
400
401         case LFUN_CUT:
402                 cutSelection(cur, true, true);
403                 cur.message(_("Cut"));
404                 break;
405
406         case LFUN_COPY:
407                 copySelection(cur);
408                 cur.message(_("Copy"));
409                 break;
410
411         case LFUN_MOUSE_PRESS:
412                 lfunMousePress(cur, cmd);
413                 break;
414
415         case LFUN_MOUSE_MOTION:
416                 lfunMouseMotion(cur, cmd);
417                 break;
418
419         case LFUN_MOUSE_RELEASE:
420                 lfunMouseRelease(cur, cmd);
421                 break;
422
423         case LFUN_FINISHED_LEFT:
424                 cur.bv().cursor() = cur;
425                 break;
426
427         case LFUN_FINISHED_RIGHT:
428                 ++cur.pos();
429                 cur.bv().cursor() = cur;
430                 break;
431
432         case LFUN_FINISHED_UP:
433                 cur.bv().cursor() = cur;
434                 break;
435
436         case LFUN_FINISHED_DOWN:
437                 cur.bv().cursor() = cur;
438                 break;
439
440         case LFUN_RIGHTSEL:
441         case LFUN_RIGHT:
442                 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
443                 cur.autocorrect() = false;
444                 cur.clearTargetX();
445                 cur.macroModeClose();
446                 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
447                         cur.pushLeft(*cur.nextAtom().nucleus());
448                         cur.inset().idxFirst(cur);
449                 } else if (cur.posRight() || idxRight(cur)
450                         || cur.popRight() || cur.selection())
451                         ;
452                 else
453                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
454                 break;
455
456         case LFUN_LEFTSEL:
457         case LFUN_LEFT:
458                 cur.selHandle(cmd.action == LFUN_LEFTSEL);
459                 cur.autocorrect() = false;
460                 cur.clearTargetX();
461                 cur.macroModeClose();
462                 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
463                         cur.posLeft();
464                         cur.push(*cur.nextAtom().nucleus());
465                         cur.inset().idxLast(cur);
466                 } else if (cur.posLeft() || idxLeft(cur)
467                         || cur.popLeft() || cur.selection())
468                         ;
469                 else
470                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
471                 break;
472
473         case LFUN_UPSEL:
474         case LFUN_UP:
475                 cur.selHandle(cmd.action == LFUN_UPSEL);
476                 if (!cur.up())
477                         cmd = FuncRequest(LFUN_FINISHED_UP);
478                 break;
479
480         case LFUN_DOWNSEL:
481         case LFUN_DOWN:
482                 cur.selHandle(cmd.action == LFUN_DOWNSEL);
483                 if (!cur.down())
484                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
485                 break;
486
487         case LFUN_MOUSE_DOUBLE:
488         case LFUN_MOUSE_TRIPLE:
489         case LFUN_WORDSEL:
490                 cur.pos() = 0;
491                 cur.idx() = 0;
492                 cur.resetAnchor();
493                 cur.selection() = true;
494                 cur.pos() = cur.lastpos();
495                 cur.idx() = cur.lastidx();
496                 break;
497
498         case LFUN_UP_PARAGRAPHSEL:
499         case LFUN_UP_PARAGRAPH:
500         case LFUN_DOWN_PARAGRAPHSEL:
501         case LFUN_DOWN_PARAGRAPH:
502                 break;
503
504         case LFUN_HOMESEL:
505         case LFUN_HOME:
506         case LFUN_WORDLEFTSEL:
507         case LFUN_WORDLEFT:
508                 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
509                 cur.macroModeClose();
510                 if (cur.pos() != 0) {
511                         cur.pos() = 0;
512                 } else if (cur.col() != 0) {
513                         cur.idx() -= cur.col();
514                         cur.pos() = 0;
515                 } else if (cur.idx() != 0) {
516                         cur.idx() = 0;
517                         cur.pos() = 0;
518                 } else {
519                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
520                 }
521                 break;
522
523         case LFUN_WORDRIGHTSEL:
524         case LFUN_WORDRIGHT:
525         case LFUN_ENDSEL:
526         case LFUN_END:
527                 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
528                 cur.macroModeClose();
529                 cur.clearTargetX();
530                 if (cur.pos() != cur.lastpos()) {
531                         cur.pos() = cur.lastpos();
532                 } else if (cur.col() != cur.lastcol()) {
533                         cur.idx() = cur.idx() - cur.col() + cur.lastcol();
534                         cur.pos() = cur.lastpos();
535                 } else if (cur.idx() != cur.lastidx()) {
536                         cur.idx() = cur.lastidx();
537                         cur.pos() = cur.lastpos();
538                 } else {
539                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
540                 }
541                 break;
542
543         case LFUN_PRIORSEL:
544         case LFUN_PRIOR:
545                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
546                 break;
547
548         case LFUN_NEXTSEL:
549         case LFUN_NEXT:
550                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
551                 break;
552
553         case LFUN_CELL_FORWARD:
554                 cur.inset().idxNext(cur);
555                 break;
556
557         case LFUN_CELL_BACKWARD:
558                 cur.inset().idxPrev(cur);
559                 break;
560
561         case LFUN_DELETE_WORD_BACKWARD:
562         case LFUN_BACKSPACE:
563                 recordUndo(cur, Undo::ATOMIC);
564                 cur.backspace();
565                 break;
566
567         case LFUN_DELETE_WORD_FORWARD:
568         case LFUN_DELETE:
569                 recordUndo(cur);
570                 cur.erase();
571                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
572                 break;
573
574         case LFUN_ESCAPE:
575                 if (cur.selection())
576                         cur.clearSelection();
577                 else
578                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
579                 break;
580
581         case LFUN_INSET_TOGGLE:
582                 recordUndo(cur);
583                 //lockToggle();
584                 if (cur.pos() != cur.lastpos()) {
585                         // toggle previous inset ...
586                         cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
587                 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
588                         // ... or enclosing inset if we are in the last inset position
589                         cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
590                         ++cur.pos();
591                 }
592                 break;
593
594         case LFUN_SELFINSERT:
595                 recordUndo(cur);
596                 if (cmd.argument.size() != 1) {
597                         cur.insert(cmd.argument);
598                         break;
599                 }
600                 if (!interpret(cur, cmd.argument[0]))
601                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
602                 break;
603
604         //case LFUN_GETXY:
605         //      sprintf(dispatch_buffer, "%d %d",);
606         //      break;
607
608         case LFUN_SETXY: {
609                 lyxerr << "LFUN_SETXY broken!" << endl;
610                 int x = 0;
611                 int y = 0;
612                 istringstream is(cmd.argument.c_str());
613                 is >> x >> y;
614                 cur.setScreenPos(x, y);
615                 break;
616         }
617
618         // Special casing for superscript in case of LyX handling
619         // dead-keys:
620         case LFUN_CIRCUMFLEX:
621                 if (cmd.argument.empty()) {
622                         // do superscript if LyX handles
623                         // deadkeys
624                         recordUndo(cur, Undo::ATOMIC);
625                         script(cur, true);
626                 }
627                 break;
628
629         case LFUN_UMLAUT:
630         case LFUN_ACUTE:
631         case LFUN_GRAVE:
632         case LFUN_BREVE:
633         case LFUN_DOT:
634         case LFUN_MACRON:
635         case LFUN_CARON:
636         case LFUN_TILDE:
637         case LFUN_CEDILLA:
638         case LFUN_CIRCLE:
639         case LFUN_UNDERDOT:
640         case LFUN_TIE:
641         case LFUN_OGONEK:
642         case LFUN_HUNG_UMLAUT:
643                 break;
644
645         //  Math fonts
646         case LFUN_FREEFONT_APPLY:
647         case LFUN_FREEFONT_UPDATE:
648                 handleFont2(cur, cmd.argument);
649                 break;
650
651         case LFUN_BOLD:
652                 handleFont(cur, cmd.argument, "mathbf");
653                 break;
654         case LFUN_SANS:
655                 handleFont(cur, cmd.argument, "mathsf");
656                 break;
657         case LFUN_EMPH:
658                 handleFont(cur, cmd.argument, "mathcal");
659                 break;
660         case LFUN_ROMAN:
661                 handleFont(cur, cmd.argument, "mathrm");
662                 break;
663         case LFUN_CODE:
664                 handleFont(cur, cmd.argument, "texttt");
665                 break;
666         case LFUN_FRAK:
667                 handleFont(cur, cmd.argument, "mathfrak");
668                 break;
669         case LFUN_ITAL:
670                 handleFont(cur, cmd.argument, "mathit");
671                 break;
672         case LFUN_NOUN:
673                 handleFont(cur, cmd.argument, "mathbb");
674                 break;
675         //case LFUN_FREEFONT_APPLY:
676                 handleFont(cur, cmd.argument, "textrm");
677                 break;
678         case LFUN_DEFAULT:
679                 handleFont(cur, cmd.argument, "textnormal");
680                 break;
681
682         case LFUN_MATH_MODE:
683 #if 1
684                 cur.macroModeClose();
685                 selClearOrDel(cur);
686                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
687                 cur.posLeft();
688                 cur.pushLeft(*cur.nextInset());
689 #else
690                 if (currentMode() == InsetBase::TEXT_MODE) {
691                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
692                         cur.message(_("create new math text environment ($...$)"));
693                 } else {
694                         handleFont(cur, cmd.argument, "textrm");
695                         cur.message(_("entered math text mode (textrm)"));
696                 }
697 #endif
698                 break;
699
700         case LFUN_MATH_SIZE:
701 #if 0
702                 recordUndo(cur);
703                 cur.setSize(arg);
704 #endif
705                 break;
706
707         case LFUN_INSERT_MATRIX: {
708                 recordUndo(cur, Undo::ATOMIC);
709                 unsigned int m = 1;
710                 unsigned int n = 1;
711                 string v_align;
712                 string h_align;
713                 istringstream is(cmd.argument);
714                 is >> m >> n >> v_align >> h_align;
715                 if (m < 1)
716                         m = 1;
717                 if (n < 1)
718                         n = 1;
719                 v_align += 'c';
720                 cur.niceInsert(
721                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
722                 break;
723         }
724
725         case LFUN_MATH_DELIM: {
726                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
727                 string ls;
728                 string rs = lyx::support::split(cmd.argument, ls, ' ');
729                 // Reasonable default values
730                 if (ls.empty())
731                         ls = '(';
732                 if (rs.empty())
733                         rs = ')';
734                 recordUndo(cur, Undo::ATOMIC);
735                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
736                 break;
737         }
738
739         case LFUN_SPACE_INSERT:
740         case LFUN_MATH_SPACE:
741                 recordUndo(cur, Undo::ATOMIC);
742                 cur.insert(MathAtom(new MathSpaceInset(",")));
743                 break;
744
745         case LFUN_INSET_ERT:
746                 // interpret this as if a backslash was typed
747                 recordUndo(cur, Undo::ATOMIC);
748                 interpret(cur, '\\');
749                 break;
750
751 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
752 // handling such that "self-insert" works on "arbitrary stuff" too, and
753 // math-insert only handles special math things like "matrix".
754         case LFUN_INSERT_MATH:
755                 recordUndo(cur, Undo::ATOMIC);
756                 cur.niceInsert(cmd.argument);
757                 break;
758
759         case LFUN_DIALOG_SHOW_NEW_INSET: {
760                 string const & name = cmd.argument;
761                 string data;
762 #if 0
763                 if (name == "ref") {
764                         RefInset tmp(name);
765                         data = tmp.createDialogStr(name);
766                 }
767 #endif
768                 cur.bv().owner()->getDialogs().show(name, data, 0);
769                 break;
770         }
771
772         case LFUN_INSET_APPLY: {
773                 string const name = cmd.getArg(0);
774                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
775
776                 if (base) {
777                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
778                         base->dispatch(cur, fr);
779                         break;
780                 }
781                 MathArray ar;
782                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
783                         cur.insert(ar);
784                         break;
785                 }
786                 cur.undispatched();
787                 break;
788         }
789
790         default:
791                 MathDimInset::priv_dispatch(cur, cmd);
792                 break;
793         }
794 }
795
796
797 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
798                 FuncStatus & flag) const
799 {
800         // the font related toggles
801         //string tc = "mathnormal";
802         bool ret = true;
803         switch (cmd.action) {
804 #if 0
805         case LFUN_TABULAR_FEATURE:
806                 // FIXME: check temporarily disabled
807                 // valign code
808                 char align = mathcursor::valign();
809                 if (align == '\0') {
810                         enable = false;
811                         break;
812                 }
813                 if (cmd.argument.empty()) {
814                         flag.clear();
815                         break;
816                 }
817                 if (!contains("tcb", cmd.argument[0])) {
818                         enable = false;
819                         break;
820                 }
821                 flag.setOnOff(cmd.argument[0] == align);
822                 break;
823         case LFUN_BOLD:
824                 flag.setOnOff(tc == "mathbf");
825                 break;
826         case LFUN_SANS:
827                 flag.setOnOff(tc == "mathsf");
828                 break;
829         case LFUN_EMPH:
830                 flag.setOnOff(tc == "mathcal");
831                 break;
832         case LFUN_ROMAN:
833                 flag.setOnOff(tc == "mathrm");
834                 break;
835         case LFUN_CODE:
836                 flag.setOnOff(tc == "mathtt");
837                 break;
838         case LFUN_NOUN:
839                 flag.setOnOff(tc == "mathbb");
840                 break;
841         case LFUN_DEFAULT:
842                 flag.setOnOff(tc == "mathnormal");
843                 break;
844 #endif
845         case LFUN_MATH_MUTATE:
846                 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
847                 flag.setOnOff(false);
848                 break;
849
850         // we just need to be in math mode to enable that
851         case LFUN_MATH_SIZE:
852         case LFUN_MATH_SPACE:
853         case LFUN_MATH_LIMITS:
854         case LFUN_MATH_NONUMBER:
855         case LFUN_MATH_NUMBER:
856         case LFUN_MATH_EXTERN:
857                 flag.enabled(true);
858                 break;
859
860         default:
861                 ret = false;
862                 break;
863         }
864         return ret;
865 }
866
867
868 void MathNestInset::edit(LCursor & cur, bool left)
869 {
870         cur.push(*this);
871         cur.idx() = left ? 0 : cur.lastidx();
872         cur.pos() = left ? 0 : cur.lastpos();
873         cur.resetAnchor();
874         lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
875 }
876
877
878 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y) const
879 {
880         int idx_min = 0;
881         int dist_min = 1000000;
882         for (idx_type i = 0; i < nargs(); ++i) {
883                 int d = cell(i).dist(x, y);
884                 if (d < dist_min) {
885                         dist_min = d;
886                         idx_min = i;
887                 }
888         }
889         MathArray const & ar = cell(idx_min);
890         cur.push(const_cast<MathNestInset&>(*this));
891         cur.idx() = idx_min;
892         cur.pos() = ar.x2pos(x - ar.xo());
893         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
894         if (dist_min == 0) {
895                 // hit inside cell
896                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
897                         if (ar[i]->covers(x, y))
898                                 return ar[i].nucleus()->editXY(cur, x, y);
899         }
900         return const_cast<MathNestInset*>(this);
901 }
902
903
904 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
905 {
906         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
907         if (cmd.button() == mouse_button::button1) {
908                 first_x = cmd.x;
909                 first_y = cmd.y;
910                 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
911                 cur.resetAnchor();
912                 cur.bv().cursor() = cur;
913         }
914
915         if (cmd.button() == mouse_button::button2) {
916                 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
917         }
918 }
919
920
921 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
922 {
923         // only select with button 1
924         if (cmd.button() != mouse_button::button1)
925                 return;
926
927         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
928                 return;
929
930         first_x = cmd.x;
931         first_y = cmd.y;
932
933         cur.bv().cursor().setCursor(cur, true);
934 }
935
936
937 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
938 {
939         lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
940
941         if (cmd.button() == mouse_button::button1) {
942                 // try to dispatch to enclosed insets first
943                 //cur.bv().stuffClipboard(cur.grabSelection());
944                 return;
945         }
946
947         if (cmd.button() == mouse_button::button2) {
948                 MathArray ar;
949                 asArray(cur.bv().getClipboard(), ar);
950                 cur.clearSelection();
951                 cur.setScreenPos(cmd.x, cmd.y);
952                 cur.insert(ar);
953                 cur.bv().update();
954                 return;
955         }
956
957         if (cmd.button() == mouse_button::button3) {
958                 // try to dispatch to enclosed insets first
959                 cur.bv().owner()->getDialogs().show("mathpanel");
960                 return;
961         }
962
963         cur.undispatched();
964 }
965
966
967 bool MathNestInset::interpret(LCursor & cur, char c)
968 {
969         lyxerr << "interpret 2: '" << c << "'" << endl;
970         cur.clearTargetX();
971
972         // handle macroMode
973         if (cur.inMacroMode()) {
974                 string name = cur.macroName();
975
976                 /// are we currently typing '#1' or '#2' or...?
977                 if (name == "\\#") {
978                         cur.backspace();
979                         int n = c - '0';
980                         if (n >= 1 && n <= 9)
981                                 cur.insert(new MathMacroArgument(n));
982                         return true;
983                 }
984
985                 if (isalpha(c)) {
986                         cur.activeMacro()->setName(name + c);
987                         return true;
988                 }
989
990                 // handle 'special char' macros
991                 if (name == "\\") {
992                         // remove the '\\'
993                         if (c == '\\') {
994                                 cur.backspace();
995                                 if (currentMode() == MathInset::TEXT_MODE)
996                                         cur.niceInsert(createMathInset("textbackslash"));
997                                 else
998                                         cur.niceInsert(createMathInset("backslash"));
999                         } else if (c == '{') {
1000                                 cur.backspace();
1001                                 cur.niceInsert(MathAtom(new MathBraceInset));
1002                         } else if (c == '%') {
1003                                 cur.backspace();
1004                                 cur.niceInsert(MathAtom(new MathCommentInset));
1005                         } else if (c == '#') {
1006                                 BOOST_ASSERT(cur.activeMacro());
1007                                 cur.activeMacro()->setName(name + c);
1008                         } else {
1009                                 cur.backspace();
1010                                 cur.niceInsert(createMathInset(string(1, c)));
1011                         }
1012                         return true;
1013                 }
1014
1015                 // leave macro mode and try again if necessary
1016                 cur.macroModeClose();
1017                 if (c == '{')
1018                         cur.niceInsert(MathAtom(new MathBraceInset));
1019                 else if (c != ' ')
1020                         interpret(cur, c);
1021                 return true;
1022         }
1023
1024         // This is annoying as one has to press <space> far too often.
1025         // Disable it.
1026
1027 #if 0
1028                 // leave autocorrect mode if necessary
1029                 if (autocorrect() && c == ' ') {
1030                         autocorrect() = false;
1031                         return true;
1032                 }
1033 #endif
1034
1035         // just clear selection on pressing the space bar
1036         if (cur.selection() && c == ' ') {
1037                 cur.selection() = false;
1038                 return true;
1039         }
1040
1041         selClearOrDel(cur);
1042
1043         if (c == '\\') {
1044                 //lyxerr << "starting with macro" << endl;
1045                 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1046                 return true;
1047         }
1048
1049         if (c == '\n') {
1050                 if (currentMode() == MathInset::TEXT_MODE)
1051                         cur.insert(c);
1052                 return true;
1053         }
1054
1055         if (c == ' ') {
1056                 if (currentMode() == MathInset::TEXT_MODE) {
1057                         // insert spaces in text mode,
1058                         // but suppress direct insertion of two spaces in a row
1059                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1060                         // it is better than nothing...
1061                         if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1062                                 cur.insert(c);
1063                         return true;
1064                 }
1065                 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1066                         cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1067                         return true;
1068                 }
1069                 if (cur.popRight())
1070                         return true;
1071                 // if are at the very end, leave the formula
1072                 return cur.pos() != cur.lastpos();
1073         }
1074
1075         if (c == '_') {
1076                 script(cur, false);
1077                 return true;
1078         }
1079
1080         if (c == '^') {
1081                 script(cur, true);
1082                 return true;
1083         }
1084
1085         if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%') {
1086                 cur.niceInsert(createMathInset(string(1, c)));
1087                 return true;
1088         }
1089
1090         if (c == '~') {
1091                 cur.niceInsert(createMathInset("sim"));
1092                 return true;
1093         }
1094
1095         // try auto-correction
1096         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1097         //      return true;
1098
1099         // no special circumstances, so insert the character without any fuss
1100         cur.insert(c);
1101         cur.autocorrect() = true;
1102         return true;
1103 }
1104
1105
1106 bool MathNestInset::script(LCursor & cur, bool up)
1107 {
1108         // Hack to get \^ and \_ working
1109         lyxerr << "handling script: up: " << up << endl;
1110         if (cur.inMacroMode() && cur.macroName() == "\\") {
1111                 if (up)
1112                         cur.niceInsert(createMathInset("mathcircumflex"));
1113                 else
1114                         interpret(cur, '_');
1115                 return true;
1116         }
1117
1118         cur.macroModeClose();
1119         string safe = grabAndEraseSelection(cur);
1120         if (asScriptInset() && cur.idx() == 0) {
1121                 // we are in a nucleus of a script inset, move to _our_ script
1122                 MathScriptInset * inset = asScriptInset();
1123                 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1124                 inset->ensure(up);
1125                 cur.idx() = inset->idxOfScript(up);
1126                 cur.pos() = 0;
1127         } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1128                 --cur.pos();
1129                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1130                 cur.push(*inset);
1131                 cur.idx() = inset->idxOfScript(up);
1132                 cur.pos() = cur.lastpos();
1133         } else {
1134                 // convert the thing to our left to a scriptinset or create a new
1135                 // one if in the very first position of the array
1136                 if (cur.pos() == 0) {
1137                         lyxerr << "new scriptinset" << endl;
1138                         cur.insert(new MathScriptInset(up));
1139                 } else {
1140                         lyxerr << "converting prev atom " << endl;
1141                         cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1142                 }
1143                 --cur.pos();
1144                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1145                 cur.push(*inset);
1146                 cur.idx() = 1;
1147                 cur.pos() = 0;
1148         }
1149         lyxerr << "pasting 1: safe:\n" << safe << endl;
1150         cur.paste(safe);
1151         cur.resetAnchor();
1152         lyxerr << "pasting 2: safe:\n" << safe << endl;
1153         return true;
1154 }