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