]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
lowercase method names
[lyx.git] / src / mathed / math_cursor.C
1 /*
2  *  File:        math_cursor.C
3  *  Purpose:     Interaction for mathed
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5  *  Created:     January 1996
6  *  Description: Math interaction for a WYSIWYG math editor.
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta, Math & Lyx project.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include <config.h>
23 #include <algorithm>
24 #include <cctype>
25
26 #include "debug.h"
27 #include "LColor.h"
28 #include "Painter.h"
29 #include "mathed/support.h"
30 #include "formulabase.h"
31 #include "math_cursor.h"
32 #include "math_arrayinset.h"
33 #include "math_bigopinset.h"
34 #include "math_decorationinset.h"
35 #include "math_deliminset.h"
36 #include "math_dotsinset.h"
37 #include "math_fracinset.h"
38 #include "math_funcinset.h"
39 #include "math_gridinset.h"
40 #include "math_macro.h"
41 #include "math_macroarg.h"
42 #include "math_macrotable.h"
43 #include "math_macrotemplate.h"
44 #include "math_matrixinset.h"
45 #include "math_rootinset.h"
46 #include "math_spaceinset.h"
47 #include "math_sqrtinset.h"
48 #include "support/lstrings.h"
49 #include "math_scriptinset.h"
50 #include "math_parser.h"
51
52 using std::endl;
53 using std::min;
54 using std::max;
55 using std::isalnum;
56
57
58 namespace {
59
60 struct Selection
61 {
62         void grab(MathCursor const & cursor)
63         {
64                 data_.clear();
65                 MathCursorPos i1;
66                 MathCursorPos i2;
67                 cursor.getSelection(i1, i2); 
68                 if (i1.idx_ == i2.idx_)
69                         data_.push_back(MathArray(i1.cell(), i1.pos_, i2.pos_));
70                 else {
71                         std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
72                         for (unsigned i = 0; i < indices.size(); ++i)
73                                 data_.push_back(i1.cell(indices[i]));
74                 }
75         }
76
77         void erase(MathCursor & cursor)
78         {
79                 MathCursorPos i1;
80                 MathCursorPos i2;
81                 cursor.getSelection(i1, i2); 
82                 if (i1.idx_ == i2.idx_) {
83                         i1.cell().erase(i1.pos_, i2.pos_);
84                 } else {
85                         std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
86                         for (unsigned i = 0; i < indices.size(); ++i)
87                                 i1.cell(indices[i]).erase();
88                 }
89                 cursor.cursor() = i1;
90         }
91
92         void paste(MathCursor & cursor) const
93         {
94                 cursor.insert(glue());
95         }
96
97         // glues selection to one cell
98         MathArray glue() const
99         {
100                 MathArray ar;
101                 for (unsigned i = 0; i < data_.size(); ++i)
102                         ar.push_back(data_[i]);
103                 return ar;
104         }
105
106         void clear()
107         {
108                 data_.clear();
109         }
110
111         std::vector<MathArray> data_;
112 };
113
114
115 Selection theSelection;
116
117
118 std::ostream & operator<<(std::ostream & os, MathCursorPos const & p)
119 {
120         os << "(par: " << p.par_ << " idx: " << p.idx_
121            << " pos: " << p.pos_ << ")";
122         return os;
123 }
124
125 }
126
127
128 MathCursor::MathCursor(InsetFormulaBase * formula)
129         : formula_(formula), lastcode_(LM_TC_MIN), imacro_(0), selection_(false)
130 {
131         first();
132 }
133
134
135 void MathCursor::push(MathInset * par, bool first)
136 {
137         MathCursorPos p;
138         p.par_ = par;
139         if (first)
140                 par->idxFirst(p.idx_, p.pos_);
141         else
142                 par->idxLast(p.idx_, p.pos_);
143         Cursor_.push_back(p);
144 }
145
146
147 bool MathCursor::pop()
148 {
149         if (Cursor_.size() <= 1)
150                 return false;
151         Cursor_.pop_back();
152         return true;
153 }
154
155
156 MathInset * MathCursor::parInset(int i) const
157 {
158         return Cursor_[i].par_;
159 }
160
161
162 void MathCursor::dump(char const * what) const
163 {
164         return;
165
166         lyxerr << "MC: " << what << "\n";
167         for (unsigned i = 0; i < Cursor_.size(); ++i)
168                 lyxerr << "  i: " << i 
169                         << " pos: " << Cursor_[i].pos_
170                         << " idx: " << Cursor_[i].idx_
171                         << " par: " << Cursor_[i].par_ << "\n";
172
173         //lyxerr        << " sel: " << selection_ << " data: " << array() << "\n";
174 }
175
176
177 void MathCursor::seldump(char const *) const
178 {
179         //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
180         //dump("   Pos");
181         return;
182
183         //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv=======================   "
184         //      <<  str << "\ntheSelection: " << theSelection;
185         //for (unsigned int i = 0; i < Cursor_.size(); ++i) 
186         //      lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
187         //lyxerr << "\ncursor.pos_: " << cursor().pos_;
188         //lyxerr << "\nanchor.pos_: " << anchor().pos_;
189         //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
190 }
191
192
193 bool MathCursor::isInside(MathInset * p) const
194 {
195         for (unsigned i = 0; i < Cursor_.size(); ++i) 
196                 if (parInset(i) == p) 
197                         return true;
198         return false;
199 }
200
201
202 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
203 {
204         if (!p)
205                 return false;
206         if (!(p->isActive() || (useupdown && p->isScriptInset())))
207                 return false;
208
209         if (sel) {
210                 // we can't move into anything new during selection
211                 if (Cursor_.size() == Anchor_.size())
212                         return false;
213                 if (p != Anchor_[Cursor_.size()].par_)
214                         return false;
215         }
216         return true;
217 }
218
219
220 bool MathCursor::plainLeft()
221 {
222         return array().prev(cursor().pos_);
223 }
224
225
226 bool MathCursor::left(bool sel)
227 {
228         dump("Left 1");
229         if (imacro_) {
230                 // was MacroModeBack()
231                 if (!imacro_->name().empty()) {
232                         imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
233                         imacro_->metrics(imacro_->size());
234                 } else
235                         macroModeClose();
236                 return true;
237         }
238         selHandle(sel);
239         clearLastCode();
240
241         MathInset * p = prevInset();
242         if (openable(p, sel, false)) {
243                 plainLeft();
244                 push(p, false);
245                 return true;
246         } 
247         if (plainLeft())
248                 return true;
249         if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
250                 return true;
251         if (pop())
252                 return true;
253         return false;
254 }
255
256
257 bool MathCursor::plainRight()
258 {
259         return array().next(cursor().pos_);
260 }
261
262
263 bool MathCursor::right(bool sel)
264 {
265         dump("Right 1");
266         if (imacro_) {
267                 macroModeClose();
268                 return true;
269         }
270         selHandle(sel);
271         clearLastCode();
272
273         MathInset * p = nextInset();
274         if (openable(p, sel, false)) {
275                 push(p, true);
276                 return true;
277         }
278         if (array().next(cursor().pos_))
279                 return true;
280         if (cursor().par_->idxRight(cursor().idx_, cursor().pos_))
281                 return true;
282         if (!pop())
283                 return false;
284         array().next(cursor().pos_);
285         return true;
286 }
287
288
289 void MathCursor::first()
290 {
291         Cursor_.clear();
292         push(formula_->par(), true);
293 }
294
295
296 void MathCursor::last()
297 {
298         Cursor_.clear();
299         push(formula_->par(), false);
300 }
301
302
303 void MathCursor::setPos(int x, int y)
304 {
305         dump("setPos 1");
306         //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
307
308         macroModeClose();
309         lastcode_ = LM_TC_MIN;
310         first();
311
312         cursor().par_  = formula()->par();
313
314         while (1) {
315                 cursor().idx_ = -1;
316                 cursor().pos_ = -1;
317                 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_  << "\n";
318                 int distmin = 1 << 30; // large enough
319                 for (int i = 0; i < cursor().par_->nargs(); ++i) {
320                         MathXArray const & ar = cursor().par_->xcell(i);
321                         int x1 = x - ar.xo();
322                         int y1 = y - ar.yo();
323                         int c  = ar.x2pos(x1);
324                         int xx = abs(x1 - ar.pos2x(c));
325                         int yy = abs(y1);
326                         //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
327                         //      << " c: " << c  << " xo: " << ar.xo() << "\n";
328                         if (yy + xx <= distmin) {
329                                 distmin        = yy + xx;
330                                 cursor().idx_  = i;
331                                 cursor().pos_  = c;
332                         }
333                 }
334                 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
335                 //      << cursor().pos_  << "\n";
336                 MathInset * n = nextInset();
337                 MathInset * p = prevInset();
338                 if (openable(n, selection_, true) && n->covers(x, y))
339                         push(n, true);
340                 else if (openable(p, selection_, true) && p->covers(x, y)) {
341                         plainLeft();
342                         push(p, false);
343                 } else 
344                         break;
345         }
346         dump("setPos 2");
347 }
348
349
350 void MathCursor::home()
351 {
352         dump("home 1");
353         macroModeClose();
354         clearLastCode();
355         if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_)) 
356                 pop();
357         dump("home 2");
358 }
359
360
361 void MathCursor::end()
362 {
363         dump("end 1");
364         macroModeClose();
365         clearLastCode();
366         if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
367                 pop();
368                 array().next(cursor().pos_);
369         }
370         dump("end 2");
371 }
372
373
374 void MathCursor::plainErase()
375 {
376         array().erase(cursor().pos_);
377 }
378
379
380 void MathCursor::insert(char c, MathTextCodes t)
381 {
382         //lyxerr << "inserting '" << c << "'\n";
383         if (selection_)
384                 selDel();
385
386         if (t != LM_TC_MIN)
387                 lastcode_ = t;
388
389         if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
390                 macroModeClose();
391
392         if (imacro_) {
393                 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
394                         // was MacroModeinsert(c);
395                         imacro_->setName(imacro_->name() + c);
396                         return;
397                 }
398         }
399
400         array().insert(cursor().pos_, c, t);
401         array().next(cursor().pos_);
402 }
403
404
405 void MathCursor::insert(MathInset * p)
406 {
407         macroModeClose();
408
409         if (selection_) {
410                 if (p->nargs())
411                         selCut();
412                 else
413                         selDel();
414         }
415
416         array().insert(cursor().pos_, p);
417         array().next(cursor().pos_);
418 }
419
420
421 void MathCursor::insert(MathArray const & ar)
422 {
423         macroModeClose();
424         if (selection_)
425                 selCut();
426
427         array().insert(cursor().pos_, ar);
428         cursor().pos_ += ar.size();
429 }
430
431
432 void MathCursor::erase()
433 {
434         dump("erase 1");
435         if (imacro_)
436                 return;
437
438         if (selection_) {
439                 selDel();
440                 return;
441         }
442
443         // delete empty cells if necessary
444         if (cursor().pos_ == 0 && array().size() == 0) {
445                 bool popit;
446                 bool removeit;
447                 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
448                 if (popit && pop() && removeit)
449                         plainErase();
450                 return;
451         }
452
453         if (cursor().pos_ < array().size())
454                 plainErase();
455
456         dump("erase 2");
457 }
458
459
460 void MathCursor::delLine()
461 {
462         macroModeClose();
463
464         if (selection_) {
465                 selDel();
466                 return;
467         }
468
469         if (cursor().par_->nrows() > 1)
470                 cursor().par_->delRow(row());
471 }
472
473
474 bool MathCursor::up(bool sel)
475 {
476         dump("up 1");
477         macroModeClose();
478         selHandle(sel);
479
480         if (selection_) {
481                 int x = xarray().pos2x(cursor().pos_);
482                 if (cursor().idxDown()) {
483                         cursor().pos_ = xarray().x2pos(x);
484                         return true;
485                 }
486                 if (pop()) 
487                         return true;
488                 return false;
489         }
490
491         // check whether we could move into an inset on the right or on the left
492         MathInset * p = nextInset();
493         if (p) {
494                 int idx, pos;
495                 if (p->idxFirstUp(idx, pos)) {
496                         push(p, true);
497                         cursor().idx_ = idx;
498                         cursor().pos_ = pos;
499                         dump("up 3");
500                         return true;
501                 }
502         }
503
504         p = prevInset();
505         if (p) {
506                 int idx, pos;
507                 if (p->idxLastUp(idx, pos)) {
508                         plainLeft();
509                         push(p, false);
510                         cursor().idx_ = idx;
511                         cursor().pos_ = pos;
512                         dump("up 4");
513                         return true;
514                 }
515         }
516
517         int x = xarray().pos2x(cursor().pos_);
518         if (cursor().idxUp()) {
519                 cursor().pos_ = xarray().x2pos(x);
520                 return true;
521         }
522         if (pop())
523                 return true;
524         return false;
525 }
526
527
528 bool MathCursor::down(bool sel)
529 {
530         dump("down 1");
531         macroModeClose();
532         selHandle(sel);
533
534         if (selection_) {
535                 int x = xarray().pos2x(cursor().pos_);
536                 if (cursor().idxDown()) {
537                         cursor().pos_ = xarray().x2pos(x);
538                         return true;
539                 }
540                 if (pop()) 
541                         return true;
542                 return false;
543         }
544
545         // check whether we could move into an inset on the right or on the left
546         MathInset * p = nextInset();
547         if (p) {
548                 int idx, pos;
549                 if (p->idxFirstDown(idx, pos)) {
550                         push(p, true);
551                         cursor().idx_ = idx;
552                         cursor().pos_ = pos;
553                         dump("Down 3");
554                         return true;
555                 }
556         }
557
558         p = prevInset();
559         if (p) {
560                 int idx, pos;
561                 if (p->idxLastDown(idx, pos)) {
562                         plainLeft();
563                         push(p, false);
564                         cursor().idx_ = idx;
565                         cursor().pos_ = pos;
566                         dump("Down 4");
567                         return true;
568                 }
569         }
570
571         int x = xarray().pos2x(cursor().pos_);
572         if (cursor().idxDown()) {
573                 cursor().pos_ = xarray().x2pos(x);
574                 return true;
575         }
576         if (pop())
577                 return true;
578         return false;
579 }
580
581
582 bool MathCursor::toggleLimits()
583 {
584         if (!prevIsInset())
585                 return false;
586         MathInset * p = prevInset();
587         int old = p->limits();
588         p->limits(old < 0 ? 1 : -1);
589         return old != p->limits();
590 }
591
592
593 void MathCursor::setSize(MathStyles size)
594 {
595         cursor().par_->userSetSize(size);
596 }
597
598
599
600 void MathCursor::interpret(string const & s)
601 {
602         //lyxerr << "interpret: '" << s << "'\n";
603         //lyxerr << "in: " << in_word_set(s) << " \n";
604
605         if (s.size() && (s[0] == '^' || s[0] == '_')) {
606                 bool const up = (s[0] == '^');
607                 selCut();       
608                 MathScriptInset * p = prevScriptInset();
609                 if (!p) {
610                         MathInset * b = prevInset();
611                         if (b && b->isScriptable()) {
612                                 p = new MathScriptInset(up, !up, b->clone());
613                                 plainLeft();
614                                 erase();
615                         } else {
616                                 p = new MathScriptInset(up, !up);
617                         }
618                         insert(p);
619                         plainLeft();
620                 }
621                 push(p, true);
622                 if (up)
623                         p->up(true);
624                 else
625                         p->down(true);
626                 cursor().idx_ = up ? 0 : 1;
627                 cursor().pos_ = 0;
628                 selPaste();
629                 return;
630         }
631
632         if (s[0] == '!' || s[0] == ','  || s[0] == ':' || s[0] == ';') {
633                 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
634                 insert(new MathSpaceInset(sp));
635                 return;
636         }
637
638         MathInset * p = 0;
639         latexkeys const * l = in_word_set(s);
640
641         if (l == 0) {
642                 if (s == "root") 
643                         p = new MathRootInset;
644                 else if (MathMacroTable::hasTemplate(s))
645                         p = new MathMacro(MathMacroTable::provideTemplate(s));
646                 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
647                         int m = 1;
648                         int n = 1;
649                         string v_align;
650                         string h_align;
651                         istringstream is(s.substr(7).c_str());
652                         is >> m >> n >> v_align >> h_align;
653                         m = std::max(1, m);
654                         n = std::max(1, n);
655                         v_align += 'c';
656                         MathArrayInset * pp = new MathArrayInset(m, n);
657                         pp->valign(v_align[0]);
658                         pp->halign(h_align);
659                         p = pp;
660                 }
661                 else
662                         p = new MathFuncInset(s, LM_OT_UNDEF);
663         } else {
664                 switch (l->token) {
665                         case LM_TK_BIGSYM: 
666                                 p = new MathBigopInset(s, l->id);
667                                 break;
668                                 
669                         case LM_TK_SYM: {
670                                 MathTextCodes code = static_cast<MathTextCodes>(l->id);
671                                 if (code < 255)
672                                         insert(l->id, MathIsBOPS(code) ? LM_TC_BOPS : LM_TC_SYMB);
673                                 else
674                                         p = new MathFuncInset(l->name);
675                                 break;
676                         }
677
678                         case LM_TK_STACK:
679                                 p = new MathFracInset("stackrel");
680                                 break;
681
682                         case LM_TK_FRAC:
683                                 p = new MathFracInset("frac");
684                                 break;
685
686                         case LM_TK_SQRT:
687                                 p = new MathSqrtInset;
688                                 break;
689
690                         case LM_TK_DECORATION:
691                                 p = new MathDecorationInset(l->name, l->id);
692                                 break;
693
694                         case  LM_TK_FUNCLIM:
695                                 p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
696                                 break;
697
698                         case LM_TK_SPACE:
699                                 p = new MathSpaceInset(l->id);
700                                 break;
701
702                         case LM_TK_DOTS:
703                                 p = new MathDotsInset(l->name, l->id);
704                                 break;
705
706                         case LM_TK_MACRO:
707                                 p = new MathMacro(MathMacroTable::provideTemplate(s));
708                                 break;
709
710                         default:
711                                 p = new MathFuncInset(l->name);
712                                 break;
713                 }
714         }
715
716         if (p) {
717                 bool oldsel = selection_;
718                 if (oldsel) 
719                         selCut();
720                 insert(p);
721                 if (p->nargs()) {
722                         plainLeft();
723                         right();  // do not push for e.g. MathBigopInset
724                         if (oldsel) 
725                                 selPaste();
726                 }
727                 p->metrics(p->size());
728         }
729 }
730
731
732 void MathCursor::macroModeOpen()
733 {
734         if (!imacro_) {
735                 imacro_ = new MathFuncInset("");
736                 insert(imacro_);
737         } else
738                 lyxerr << "Math Warning: Already in macro mode" << endl;
739 }
740
741
742 void MathCursor::macroModeClose()
743 {
744         if (imacro_) {
745                 string name = imacro_->name();
746                 plainLeft();
747                 plainErase();
748                 imacro_ = 0;
749                 interpret(name);
750         }
751 }
752
753
754 void MathCursor::selCopy()
755 {
756         seldump("selCopy");
757         if (selection_) {
758                 theSelection.grab(*this);
759                 selClear();
760         }
761 }
762
763
764 void MathCursor::selCut()
765 {
766         seldump("selCut");
767         if (selection_) {
768                 theSelection.grab(*this);
769                 theSelection.erase(*this);
770                 selClear();
771         }
772 }
773
774
775 void MathCursor::selDel()
776 {
777         seldump("selDel");
778         if (selection_) {
779                 theSelection.erase(*this);
780                 selClear();
781         }
782 }
783
784
785 void MathCursor::selPaste()
786 {
787         seldump("selPaste");
788         theSelection.paste(*this);
789         selClear();
790 }
791
792
793 void MathCursor::selHandle(bool sel)
794 {
795         if (sel && !selection_)
796                 selStart();
797         if (!sel && selection_)
798                 selClear();
799 }
800
801
802 void MathCursor::selStart()
803 {
804         seldump("selStart");
805         if (selection_)
806                 return;
807
808         Anchor_ = Cursor_;
809         selection_ = true;
810 }
811
812
813 void MathCursor::selClear()
814 {
815         selection_ = false;
816 }
817
818
819 void MathCursor::drawSelection(Painter & pain) const
820 {
821         if (!selection_)
822                 return;
823
824         MathCursorPos i1;
825         MathCursorPos i2;
826         getSelection(i1, i2);
827
828         //lyxerr << "selection from: " << i1 << " to " << i2 << "\n";
829
830         if (i1.idx_ == i2.idx_) {
831                 MathXArray & c = i1.xcell();
832                 int x1 = c.xo() + c.pos2x(i1.pos_);
833                 int y1 = c.yo() - c.ascent();
834                 int x2 = c.xo() + c.pos2x(i2.pos_);
835                 int y2 = c.yo() + c.descent();
836                 pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
837         } else {
838
839                 std::vector<int> indices = i1.par_->idxBetween(i1.idx_, i2.idx_);
840                 for (unsigned i = 0; i < indices.size(); ++i) {
841                         MathXArray & c = i1.xcell(indices[i]);
842                         int x1 = c.xo();
843                         int y1 = c.yo() - c.ascent();
844                         int x2 = c.xo() + c.width();
845                         int y2 = c.yo() + c.descent();
846                         pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
847                 }
848         }
849 }
850
851
852 void MathCursor::handleFont(MathTextCodes t)
853 {
854         if (selection_) {
855                 MathCursorPos i1;
856                 MathCursorPos i2;
857                 getSelection(i1, i2); 
858                 if (i1.idx_ == i2.idx_) {
859                         MathArray & ar = i1.cell();
860                         for (int pos = i1.pos_; pos != i2.pos_; ar.next(pos))
861                                 if (!ar.isInset(pos) && isalnum(ar.getChar(pos))) { 
862                                         MathTextCodes c = ar.getCode(pos) == t ? LM_TC_VAR : t;
863                                         ar.setCode(pos, c);
864                                 }
865                 }
866         } else 
867                 lastcode_ = (lastcode_ == t) ? LM_TC_VAR : t;
868 }
869
870
871 void MathCursor::handleAccent(string const & name, int code)
872 {
873         MathDecorationInset * p = new MathDecorationInset(name, code);
874         if (selection_) {
875                 selCut();
876                 p->cell(0) = theSelection.glue();
877         }
878         insert(p);
879         push(p, true);
880 }
881
882
883 void MathCursor::handleDelim(int l, int r)
884 {
885         MathDelimInset * p = new MathDelimInset(l, r);
886         if (selection_) {
887                 selCut();
888                 p->cell(0) = theSelection.glue();
889         }
890         insert(p);
891         plainLeft();
892         push(p, true);
893 }
894
895
896 void MathCursor::getPos(int & x, int & y)
897 {
898         x = xarray().xo() + xarray().pos2x(cursor().pos_);
899         y = xarray().yo();
900 }
901
902
903 MathTextCodes MathCursor::nextCode() const
904 {
905         return array().getCode(cursor().pos_); 
906 }
907
908
909 MathTextCodes MathCursor::prevCode() const
910 {
911         return array().getCode(cursor().pos_ - 1); 
912 }
913
914
915 MathInset * MathCursor::par() const
916 {
917         return cursor().par_;
918 }
919
920
921 InsetFormulaBase const * MathCursor::formula()
922 {
923         return formula_;
924 }
925
926
927 int MathCursor::pos() const
928 {
929         return cursor().pos_;
930 }
931
932
933 bool MathCursor::inMacroMode() const
934 {
935         return imacro_;
936 }
937
938
939 bool MathCursor::selection() const
940 {
941         return selection_;
942 }
943
944
945 void MathCursor::clearLastCode()
946 {
947         lastcode_ = LM_TC_MIN;
948 }
949
950
951 void MathCursor::setLastCode(MathTextCodes t)
952 {
953         lastcode_ = t;
954 }
955
956
957 MathTextCodes MathCursor::getLastCode() const
958 {
959         return lastcode_;
960 }
961
962
963 MathInset * MathCursor::enclosing(MathInsetTypes t, int & idx) const
964 {
965         for (int i = Cursor_.size() - 1; i >= 0; --i) {
966                 //lyxerr << "checking level " << i << "\n";
967                 if (Cursor_[i].par_->getType() == t) {
968                         idx = Cursor_[i].idx_;
969                         return Cursor_[i].par_;
970                 }
971         }
972         return 0;
973 }
974
975
976 void MathCursor::pullArg(bool goright)
977 {
978         // pullArg
979         dump("pullarg");
980         MathArray a = array();
981         if (pop()) {
982                 plainErase();
983                 array().insert(cursor().pos_, a);
984                 if (goright) 
985                         cursor().pos_ += a.size();
986         }
987 }
988
989
990 MathStyles MathCursor::style() const
991 {
992         return xarray().style();
993 }
994
995
996 void MathCursor::normalize() const
997 {
998 #ifdef WITH_WARNINGS
999 #warning This is evil!
1000 #endif
1001         MathCursor * it = const_cast<MathCursor *>(this);
1002
1003         if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs())
1004                 lyxerr << "this should not really happen - 1\n";
1005         it->cursor().idx_    = max(cursor().idx_, 0);
1006         it->cursor().idx_    = min(cursor().idx_, cursor().par_->nargs());
1007
1008         if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1009                 lyxerr << "this should not really happen - 2\n";
1010         it->cursor().pos_ = max(cursor().pos_, 0);
1011         it->cursor().pos_ = min(cursor().pos_, array().size());
1012 }
1013
1014
1015 int MathCursor::col() const
1016 {
1017         return par()->col(cursor().idx_);
1018 }
1019
1020
1021 int MathCursor::row() const
1022 {
1023         return par()->row(cursor().idx_);
1024 }
1025
1026
1027 /*
1028 char MathCursorPos::getChar() const
1029 {
1030         return array().getChar(cursor().pos_);
1031 }
1032
1033
1034 string MathCursorPos::readString()
1035 {
1036         string s;
1037         int code = nextCode();
1038         for ( ; OK() && nextCode() == code; Next()) 
1039                 s += getChar();
1040
1041         return s;
1042 }
1043 */
1044
1045
1046 MathInset * MathCursor::prevInset() const
1047 {
1048         normalize();
1049         int c = cursor().pos_;
1050         if (!array().prev(c))
1051                 return 0;
1052         return array().nextInset(c);
1053 }
1054
1055
1056 MathInset * MathCursor::nextInset() const
1057 {
1058         normalize();
1059         return array().nextInset(cursor().pos_);
1060 }
1061
1062
1063 MathScriptInset * MathCursor::prevScriptInset() const
1064 {
1065         normalize();
1066         MathInset * p = array().prevInset(cursor().pos_);
1067         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1068 }
1069
1070
1071 MathSpaceInset * MathCursor::prevSpaceInset() const
1072 {
1073         normalize();
1074         MathInset * p = array().prevInset(cursor().pos_);
1075         return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1076 }
1077
1078
1079 MathArray & MathCursor::array() const
1080 {
1081         static MathArray dummy;
1082         if (!cursor().par_) {
1083                 lyxerr << "############  par_ not valid\n";
1084                 return dummy;
1085         }
1086
1087         if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1088                 lyxerr << "############  idx_ " << cursor().idx_ << " not valid\n";
1089                 return dummy;
1090         }
1091
1092         return cursor().cell();
1093 }
1094
1095
1096 MathXArray & MathCursor::xarray() const
1097 {
1098         return cursor().xcell();
1099 }
1100
1101
1102 bool MathCursor::nextIsInset() const
1103 {
1104         return cursor().pos_ < array().size() && MathIsInset(nextCode());
1105 }
1106
1107
1108 bool MathCursor::prevIsInset() const
1109 {
1110         return cursor().pos_ > 0 && MathIsInset(prevCode());
1111 }
1112
1113
1114 int MathCursor::xpos() const 
1115 {
1116         normalize();
1117         return xarray().pos2x(cursor().pos_);
1118 }
1119
1120
1121 void MathCursor::gotoX(int x)
1122 {
1123         cursor().pos_ = xarray().x2pos(x);      
1124 }
1125
1126
1127 void MathCursor::idxNext()
1128 {
1129         cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1130 }
1131
1132
1133 void MathCursor::idxPrev()
1134 {
1135         cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1136 }
1137
1138
1139 void MathCursor::splitCell()
1140 {
1141         if (cursor().idx_ == cursor().par_->nargs() - 1) 
1142                 return;
1143         MathArray ar = array();
1144         ar.erase(0, cursor().pos_);
1145         array().erase(cursor().pos_, array().size());
1146         ++cursor().idx_;
1147         cursor().pos_ = 0;
1148         array().insert(0, ar);
1149 }
1150
1151
1152 void MathCursor::breakLine()
1153 {
1154         MathMatrixInset * p = static_cast<MathMatrixInset *>(formula()->par());
1155         if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1156                 p->mutate(LM_OT_EQNARRAY);
1157                 p->addRow(0);
1158                 cursor().idx_ = p->nrows();
1159                 cursor().pos_ = 0;
1160         } else {
1161                 p->addRow(row());
1162
1163                 // split line
1164                 const int r = row();
1165                 for (int c = col() + 1; c < p->ncols(); ++c) {
1166                         const int i1 = p->index(r, c);
1167                         const int i2 = p->index(r + 1, c);      
1168                         lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1169                         p->cell(i1).swap(p->cell(i2));
1170                 }
1171
1172                 // split cell
1173                 splitCell();
1174                 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1175         }
1176 }
1177
1178
1179 char MathCursor::valign() const
1180 {
1181         int idx;
1182         MathGridInset * p =
1183                 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1184         return p ? p->valign() : 0;
1185 }
1186
1187
1188 char MathCursor::halign() const
1189 {
1190         int idx;
1191         MathGridInset * p =
1192                 static_cast<MathGridInset *>(enclosing(LM_OT_MATRIX, idx));
1193         return p ? p->halign(idx % p->ncols()) : 0;
1194 }
1195
1196
1197 MathCursorPos MathCursor::firstSelectionPos() const
1198 {
1199         MathCursorPos anc = normalAnchor();
1200         return anc < cursor() ? anc : cursor(); 
1201 }
1202
1203
1204 MathCursorPos MathCursor::lastSelectionPos() const
1205 {
1206         MathCursorPos anc = normalAnchor();
1207         return anc < cursor() ? cursor() : anc; 
1208 }
1209
1210
1211 void MathCursor::getSelection(MathCursorPos & i1, MathCursorPos & i2) const
1212 {
1213         MathCursorPos anc = normalAnchor();
1214         if (anc < cursor()) {
1215                 i1 = anc;
1216                 i2 = cursor();
1217         } else {
1218                 i1 = cursor();
1219                 i2 = anc;
1220         }
1221 }
1222
1223
1224 MathCursorPos & MathCursor::cursor()
1225 {
1226         return Cursor_.back();
1227 }
1228
1229
1230 MathCursorPos const & MathCursor::cursor() const
1231 {
1232         return Cursor_.back();
1233 }
1234
1235
1236
1237 ////////////////////////////////////////////////////////////////////////
1238
1239
1240 bool operator==(MathCursorPos const & ti, MathCursorPos const & it)
1241 {
1242         return ti.par_ == it.par_ && ti.idx_ == it.idx_ && ti.pos_ == it.pos_;
1243 }
1244
1245
1246 bool operator<(MathCursorPos const & ti, MathCursorPos const & it)
1247 {
1248         if (ti.par_ != it.par_) {
1249                 lyxerr << "can't compare cursor and anchor in different insets\n";
1250                 return true;
1251         }
1252         if (ti.idx_ != it.idx_)
1253                 return ti.idx_ < it.idx_;
1254         return ti.pos_ < it.pos_;
1255 }
1256
1257
1258 MathArray & MathCursorPos::cell(int idx) const
1259 {
1260         return par_->cell(idx);
1261 }
1262
1263 MathArray & MathCursorPos::cell() const
1264 {
1265         return par_->cell(idx_);
1266 }
1267
1268
1269 MathXArray & MathCursorPos::xcell(int idx) const
1270 {
1271         return par_->xcell(idx);
1272 }
1273
1274
1275 MathXArray & MathCursorPos::xcell() const
1276 {
1277         return par_->xcell(idx_);
1278 }
1279
1280
1281 MathCursorPos MathCursor::normalAnchor() const
1282 {
1283         // use Anchor on the same level as Cursor
1284         MathCursorPos normal = Anchor_[Cursor_.size() - 1];
1285         if (Cursor_.size() < Anchor_.size() && !(normal < cursor())) {
1286                 // anchor is behind cursor -> move anchor behind the inset
1287                 normal.cell().next(normal.pos_);
1288         }
1289         //lyxerr << "normalizing: from " << Anchor_[Anchor_.size() - 1] << " to "
1290         //      << normal << "\n";
1291         return normal;
1292 }
1293
1294
1295 bool MathCursorPos::idxUp()
1296 {
1297         return par_->idxUp(idx_, pos_);
1298 }
1299
1300
1301 bool MathCursorPos::idxDown()
1302 {
1303         return par_->idxDown(idx_, pos_);
1304 }
1305
1306
1307 bool MathCursorPos::idxLeft()
1308 {
1309         return par_->idxLeft(idx_, pos_);
1310 }
1311
1312
1313 bool MathCursorPos::idxRight()
1314 {
1315         return par_->idxRight(idx_, pos_);
1316 }