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