]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
fix compile problem
[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         case LFUN_BEGINNINGBUFSEL:
546         case LFUN_BEGINNINGBUF:
547                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
548                 break;
549
550         case LFUN_NEXTSEL:
551         case LFUN_NEXT:
552         case LFUN_ENDBUFSEL:
553         case LFUN_ENDBUF:
554                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
555                 break;
556
557         case LFUN_CELL_FORWARD:
558                 cur.inset().idxNext(cur);
559                 break;
560
561         case LFUN_CELL_BACKWARD:
562                 cur.inset().idxPrev(cur);
563                 break;
564
565         case LFUN_DELETE_WORD_BACKWARD:
566         case LFUN_BACKSPACE:
567                 recordUndo(cur, Undo::ATOMIC);
568                 cur.backspace();
569                 break;
570
571         case LFUN_DELETE_WORD_FORWARD:
572         case LFUN_DELETE:
573                 recordUndo(cur);
574                 cur.erase();
575                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
576                 break;
577
578         case LFUN_ESCAPE:
579                 if (cur.selection())
580                         cur.clearSelection();
581                 else
582                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
583                 break;
584
585         case LFUN_INSET_TOGGLE:
586                 recordUndo(cur);
587                 //lockToggle();
588                 if (cur.pos() != cur.lastpos()) {
589                         // toggle previous inset ...
590                         cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
591                 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
592                         // ... or enclosing inset if we are in the last inset position
593                         cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
594                         ++cur.pos();
595                 }
596                 break;
597
598         case LFUN_SELFINSERT:
599                 recordUndo(cur);
600                 if (cmd.argument.size() != 1) {
601                         cur.insert(cmd.argument);
602                         break;
603                 }
604                 if (!interpret(cur, cmd.argument[0]))
605                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
606                 break;
607
608         //case LFUN_GETXY:
609         //      sprintf(dispatch_buffer, "%d %d",);
610         //      break;
611
612         case LFUN_SETXY: {
613                 lyxerr << "LFUN_SETXY broken!" << endl;
614                 int x = 0;
615                 int y = 0;
616                 istringstream is(cmd.argument.c_str());
617                 is >> x >> y;
618                 cur.setScreenPos(x, y);
619                 break;
620         }
621
622         // Special casing for superscript in case of LyX handling
623         // dead-keys:
624         case LFUN_CIRCUMFLEX:
625                 if (cmd.argument.empty()) {
626                         // do superscript if LyX handles
627                         // deadkeys
628                         recordUndo(cur, Undo::ATOMIC);
629                         script(cur, true);
630                 }
631                 break;
632
633         case LFUN_UMLAUT:
634         case LFUN_ACUTE:
635         case LFUN_GRAVE:
636         case LFUN_BREVE:
637         case LFUN_DOT:
638         case LFUN_MACRON:
639         case LFUN_CARON:
640         case LFUN_TILDE:
641         case LFUN_CEDILLA:
642         case LFUN_CIRCLE:
643         case LFUN_UNDERDOT:
644         case LFUN_TIE:
645         case LFUN_OGONEK:
646         case LFUN_HUNG_UMLAUT:
647                 break;
648
649         //  Math fonts
650         case LFUN_FREEFONT_APPLY:
651         case LFUN_FREEFONT_UPDATE:
652                 handleFont2(cur, cmd.argument);
653                 break;
654
655         case LFUN_BOLD:
656                 handleFont(cur, cmd.argument, "mathbf");
657                 break;
658         case LFUN_SANS:
659                 handleFont(cur, cmd.argument, "mathsf");
660                 break;
661         case LFUN_EMPH:
662                 handleFont(cur, cmd.argument, "mathcal");
663                 break;
664         case LFUN_ROMAN:
665                 handleFont(cur, cmd.argument, "mathrm");
666                 break;
667         case LFUN_CODE:
668                 handleFont(cur, cmd.argument, "texttt");
669                 break;
670         case LFUN_FRAK:
671                 handleFont(cur, cmd.argument, "mathfrak");
672                 break;
673         case LFUN_ITAL:
674                 handleFont(cur, cmd.argument, "mathit");
675                 break;
676         case LFUN_NOUN:
677                 handleFont(cur, cmd.argument, "mathbb");
678                 break;
679         //case LFUN_FREEFONT_APPLY:
680                 handleFont(cur, cmd.argument, "textrm");
681                 break;
682         case LFUN_DEFAULT:
683                 handleFont(cur, cmd.argument, "textnormal");
684                 break;
685
686         case LFUN_MATH_MODE:
687 #if 1
688                 cur.macroModeClose();
689                 selClearOrDel(cur);
690                 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
691                 cur.posLeft();
692                 cur.pushLeft(*cur.nextInset());
693 #else
694                 if (currentMode() == InsetBase::TEXT_MODE) {
695                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
696                         cur.message(_("create new math text environment ($...$)"));
697                 } else {
698                         handleFont(cur, cmd.argument, "textrm");
699                         cur.message(_("entered math text mode (textrm)"));
700                 }
701 #endif
702                 break;
703
704         case LFUN_MATH_SIZE:
705 #if 0
706                 recordUndo(cur);
707                 cur.setSize(arg);
708 #endif
709                 break;
710
711         case LFUN_INSERT_MATRIX: {
712                 recordUndo(cur, Undo::ATOMIC);
713                 unsigned int m = 1;
714                 unsigned int n = 1;
715                 string v_align;
716                 string h_align;
717                 istringstream is(cmd.argument);
718                 is >> m >> n >> v_align >> h_align;
719                 if (m < 1)
720                         m = 1;
721                 if (n < 1)
722                         n = 1;
723                 v_align += 'c';
724                 cur.niceInsert(
725                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
726                 break;
727         }
728
729         case LFUN_MATH_DELIM: {
730                 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
731                 string ls;
732                 string rs = lyx::support::split(cmd.argument, ls, ' ');
733                 // Reasonable default values
734                 if (ls.empty())
735                         ls = '(';
736                 if (rs.empty())
737                         rs = ')';
738                 recordUndo(cur, Undo::ATOMIC);
739                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
740                 break;
741         }
742
743         case LFUN_SPACE_INSERT:
744         case LFUN_MATH_SPACE:
745                 recordUndo(cur, Undo::ATOMIC);
746                 cur.insert(MathAtom(new MathSpaceInset(",")));
747                 break;
748
749         case LFUN_INSET_ERT:
750                 // interpret this as if a backslash was typed
751                 recordUndo(cur, Undo::ATOMIC);
752                 interpret(cur, '\\');
753                 break;
754
755 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
756 // handling such that "self-insert" works on "arbitrary stuff" too, and
757 // math-insert only handles special math things like "matrix".
758         case LFUN_INSERT_MATH:
759                 recordUndo(cur, Undo::ATOMIC);
760                 cur.niceInsert(cmd.argument);
761                 break;
762
763         case LFUN_DIALOG_SHOW_NEW_INSET: {
764                 string const & name = cmd.argument;
765                 string data;
766 #if 0
767                 if (name == "ref") {
768                         RefInset tmp(name);
769                         data = tmp.createDialogStr(name);
770                 }
771 #endif
772                 cur.bv().owner()->getDialogs().show(name, data, 0);
773                 break;
774         }
775
776         case LFUN_INSET_APPLY: {
777                 string const name = cmd.getArg(0);
778                 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
779
780                 if (base) {
781                         FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
782                         base->dispatch(cur, fr);
783                         break;
784                 }
785                 MathArray ar;
786                 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
787                         cur.insert(ar);
788                         break;
789                 }
790                 cur.undispatched();
791                 break;
792         }
793
794         default:
795                 MathDimInset::priv_dispatch(cur, cmd);
796                 break;
797         }
798 }
799
800
801 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
802                 FuncStatus & flag) const
803 {
804         // the font related toggles
805         //string tc = "mathnormal";
806         bool ret = true;
807         switch (cmd.action) {
808 #if 0
809         case LFUN_TABULAR_FEATURE:
810                 // FIXME: check temporarily disabled
811                 // valign code
812                 char align = mathcursor::valign();
813                 if (align == '\0') {
814                         enable = false;
815                         break;
816                 }
817                 if (cmd.argument.empty()) {
818                         flag.clear();
819                         break;
820                 }
821                 if (!contains("tcb", cmd.argument[0])) {
822                         enable = false;
823                         break;
824                 }
825                 flag.setOnOff(cmd.argument[0] == align);
826                 break;
827         case LFUN_BOLD:
828                 flag.setOnOff(tc == "mathbf");
829                 break;
830         case LFUN_SANS:
831                 flag.setOnOff(tc == "mathsf");
832                 break;
833         case LFUN_EMPH:
834                 flag.setOnOff(tc == "mathcal");
835                 break;
836         case LFUN_ROMAN:
837                 flag.setOnOff(tc == "mathrm");
838                 break;
839         case LFUN_CODE:
840                 flag.setOnOff(tc == "mathtt");
841                 break;
842         case LFUN_NOUN:
843                 flag.setOnOff(tc == "mathbb");
844                 break;
845         case LFUN_DEFAULT:
846                 flag.setOnOff(tc == "mathnormal");
847                 break;
848 #endif
849         case LFUN_MATH_MUTATE:
850                 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
851                 flag.setOnOff(false);
852                 break;
853
854         // we just need to be in math mode to enable that
855         case LFUN_MATH_SIZE:
856         case LFUN_MATH_SPACE:
857         case LFUN_MATH_LIMITS:
858         case LFUN_MATH_NONUMBER:
859         case LFUN_MATH_NUMBER:
860         case LFUN_MATH_EXTERN:
861                 flag.enabled(true);
862                 break;
863
864         default:
865                 ret = false;
866                 break;
867         }
868         return ret;
869 }
870
871
872 void MathNestInset::edit(LCursor & cur, bool left)
873 {
874         cur.push(*this);
875         cur.idx() = left ? 0 : cur.lastidx();
876         cur.pos() = left ? 0 : cur.lastpos();
877         cur.resetAnchor();
878         lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
879 }
880
881
882 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
883 {
884         int idx_min = 0;
885         int dist_min = 1000000;
886         for (idx_type i = 0; i < nargs(); ++i) {
887                 int d = cell(i).dist(x, y);
888                 if (d < dist_min) {
889                         dist_min = d;
890                         idx_min = i;
891                 }
892         }
893         MathArray & ar = cell(idx_min);
894         cur.push(*this);
895         cur.idx() = idx_min;
896         cur.pos() = ar.x2pos(x - ar.xo());
897         lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
898         if (dist_min == 0) {
899                 // hit inside cell
900                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
901                         if (ar[i]->covers(x, y))
902                                 return ar[i].nucleus()->editXY(cur, x, y);
903         }
904         return this;
905 }
906
907
908 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
909 {
910         //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
911
912         if (cmd.button() == mouse_button::button1) {
913                 // try to dispatch to enclosed insets first
914                 //cur.bv().stuffClipboard(cur.grabSelection());
915                 return;
916         }
917
918         if (cmd.button() == mouse_button::button2) {
919                 MathArray ar;
920                 asArray(cur.bv().getClipboard(), ar);
921                 cur.clearSelection();
922                 cur.setScreenPos(cmd.x, cmd.y);
923                 cur.insert(ar);
924                 cur.bv().update();
925                 return;
926         }
927
928         if (cmd.button() == mouse_button::button3) {
929                 // try to dispatch to enclosed insets first
930                 cur.bv().owner()->getDialogs().show("mathpanel");
931                 return;
932         }
933
934         cur.undispatched();
935 }
936
937
938 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
939 {
940         lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
941         if (cmd.button() == mouse_button::button1) {
942                 first_x = cmd.x;
943                 first_y = cmd.y;
944                 //cur.setScreenPos(cmd.x + xo_, cmd.y + yo_);
945                 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
946                 cur.resetAnchor();
947                 cur.bv().cursor() = cur;
948         }
949
950         if (cmd.button() == mouse_button::button2) {
951                 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
952         }
953 }
954
955
956 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
957 {
958         // only select with button 1
959         if (cmd.button() != mouse_button::button1)
960                 return;
961
962         if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
963                 return;
964
965         first_x = cmd.x;
966         first_y = cmd.y;
967
968         cur.bv().cursor().setCursor(cur, true);
969 }
970
971
972 bool MathNestInset::interpret(LCursor & cur, char c)
973 {
974         lyxerr << "interpret 2: '" << c << "'" << endl;
975         cur.clearTargetX();
976
977         // handle macroMode
978         if (cur.inMacroMode()) {
979                 string name = cur.macroName();
980                 lyxerr << "interpret macro name: '" << name << "'" << endl;
981
982                 /// are we currently typing '#1' or '#2' or...?
983                 if (name == "\\#") {
984                         cur.backspace();
985                         int n = c - '0';
986                         if (n >= 1 && n <= 9)
987                                 cur.insert(new MathMacroArgument(n));
988                         return true;
989                 }
990
991                 if (isalpha(c)) {
992                         cur.activeMacro()->setName(name + c);
993                         return true;
994                 }
995
996                 // handle 'special char' macros
997                 if (name == "\\") {
998                         // remove the '\\'
999                         if (c == '\\') {
1000                                 cur.backspace();
1001                                 if (currentMode() == MathInset::TEXT_MODE)
1002                                         cur.niceInsert(createMathInset("textbackslash"));
1003                                 else
1004                                         cur.niceInsert(createMathInset("backslash"));
1005                         } else if (c == '{') {
1006                                 cur.backspace();
1007                                 cur.niceInsert(MathAtom(new MathBraceInset));
1008                         } else if (c == '%') {
1009                                 cur.backspace();
1010                                 cur.niceInsert(MathAtom(new MathCommentInset));
1011                         } else if (c == '#') {
1012                                 lyxerr << "setting name to " << name + c << endl;
1013                                 BOOST_ASSERT(cur.activeMacro());
1014                                 cur.activeMacro()->setName(name + c);
1015                                 lyxerr << "set name to " << name + c << endl;
1016                         } else {
1017                                 cur.backspace();
1018                                 cur.niceInsert(createMathInset(string(1, c)));
1019                         }
1020                         return true;
1021                 }
1022
1023                 // leave macro mode and try again if necessary
1024                 cur.macroModeClose();
1025                 if (c == '{')
1026                         cur.niceInsert(MathAtom(new MathBraceInset));
1027                 else if (c != ' ')
1028                         interpret(cur, c);
1029                 return true;
1030         }
1031
1032         // This is annoying as one has to press <space> far too often.
1033         // Disable it.
1034
1035 #if 0
1036                 // leave autocorrect mode if necessary
1037                 if (autocorrect() && c == ' ') {
1038                         autocorrect() = false;
1039                         return true;
1040                 }
1041 #endif
1042
1043         // just clear selection on pressing the space bar
1044         if (cur.selection() && c == ' ') {
1045                 cur.selection() = false;
1046                 return true;
1047         }
1048
1049         selClearOrDel(cur);
1050
1051         if (c == '\\') {
1052                 //lyxerr << "starting with macro" << endl;
1053                 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1054                 return true;
1055         }
1056
1057         if (c == '\n') {
1058                 if (currentMode() == MathInset::TEXT_MODE)
1059                         cur.insert(c);
1060                 return true;
1061         }
1062
1063         if (c == ' ') {
1064                 if (currentMode() == MathInset::TEXT_MODE) {
1065                         // insert spaces in text mode,
1066                         // but suppress direct insertion of two spaces in a row
1067                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1068                         // it is better than nothing...
1069                         if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1070                                 cur.insert(c);
1071                         return true;
1072                 }
1073                 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1074                         cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1075                         return true;
1076                 }
1077                 if (cur.popRight())
1078                         return true;
1079                 // if are at the very end, leave the formula
1080                 return cur.pos() != cur.lastpos();
1081         }
1082
1083         if (c == '_') {
1084                 script(cur, false);
1085                 return true;
1086         }
1087
1088         if (c == '^') {
1089                 script(cur, true);
1090                 return true;
1091         }
1092
1093         if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%') {
1094                 cur.niceInsert(createMathInset(string(1, c)));
1095                 return true;
1096         }
1097
1098         if (c == '~') {
1099                 cur.niceInsert(createMathInset("sim"));
1100                 return true;
1101         }
1102
1103         // try auto-correction
1104         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1105         //      return true;
1106
1107         // no special circumstances, so insert the character without any fuss
1108         cur.insert(c);
1109         cur.autocorrect() = true;
1110         return true;
1111 }
1112
1113
1114 bool MathNestInset::script(LCursor & cur, bool up)
1115 {
1116         // Hack to get \^ and \_ working
1117         lyxerr << "handling script: up: " << up << endl;
1118         if (cur.inMacroMode() && cur.macroName() == "\\") {
1119                 if (up)
1120                         cur.niceInsert(createMathInset("mathcircumflex"));
1121                 else
1122                         interpret(cur, '_');
1123                 return true;
1124         }
1125
1126         cur.macroModeClose();
1127         string safe = grabAndEraseSelection(cur);
1128         if (asScriptInset() && cur.idx() == 0) {
1129                 // we are in a nucleus of a script inset, move to _our_ script
1130                 MathScriptInset * inset = asScriptInset();
1131                 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1132                 inset->ensure(up);
1133                 cur.idx() = inset->idxOfScript(up);
1134                 cur.pos() = 0;
1135         } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1136                 --cur.pos();
1137                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1138                 cur.push(*inset);
1139                 cur.idx() = inset->idxOfScript(up);
1140                 cur.pos() = cur.lastpos();
1141         } else {
1142                 // convert the thing to our left to a scriptinset or create a new
1143                 // one if in the very first position of the array
1144                 if (cur.pos() == 0) {
1145                         lyxerr << "new scriptinset" << endl;
1146                         cur.insert(new MathScriptInset(up));
1147                 } else {
1148                         lyxerr << "converting prev atom " << endl;
1149                         cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1150                 }
1151                 lyxerr << "new scriptinset 2: cur:\n" << cur << endl;
1152                 --cur.pos();
1153                 lyxerr << "new scriptinset 3: cur:\n" << cur << endl;
1154                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1155                 lyxerr << "new scriptinset 3: inset:\n" << inset << endl;
1156                 cur.push(*inset);
1157                 cur.idx() = 1;
1158                 cur.pos() = 0;
1159         }
1160         lyxerr << "pasting 1: safe:\n" << safe << endl;
1161         cur.paste(safe);
1162         cur.resetAnchor();
1163         lyxerr << "pasting 2: safe:\n" << safe << endl;
1164         return true;
1165 }