]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
Fix a leak; cosmetics
[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::push(MathInset * par, bool first)
143 {
144         MathCursorPos p;
145         p.par_ = par;
146         if (first)
147                 par->idxFirst(p.idx_, p.pos_);
148         else
149                 par->idxLast(p.idx_, p.pos_);
150         Cursor_.push_back(p);
151 }
152
153
154 bool MathCursor::pop()
155 {
156         if (Cursor_.size() <= 1)
157                 return false;
158         Cursor_.pop_back();
159         return true;
160 }
161
162
163 MathInset * MathCursor::parInset(int i) const
164 {
165         return Cursor_[i].par_;
166 }
167
168
169 void MathCursor::dump(char const * what) const
170 {
171         return;
172
173         lyxerr << "MC: " << what << "\n";
174         for (unsigned i = 0; i < Cursor_.size(); ++i)
175                 lyxerr << "  i: " << i 
176                         << " pos: " << Cursor_[i].pos_
177                         << " idx: " << Cursor_[i].idx_
178                         << " par: " << Cursor_[i].par_ << "\n";
179
180         //lyxerr        << " sel: " << selection_ << " data: " << array() << "\n";
181 }
182
183
184 void MathCursor::seldump(char const *) const
185 {
186         //lyxerr << "SEL: " << str << ": '" << theSelection << "'\n";
187         //dump("   Pos");
188         return;
189
190         //lyxerr << "\n\n\\n=================vvvvvvvvvvvvv=======================   "
191         //      <<  str << "\ntheSelection: " << theSelection;
192         //for (unsigned int i = 0; i < Cursor_.size(); ++i) 
193         //      lyxerr << Cursor_[i].par_ << "\n'" << Cursor_[i].cell() << "'\n";
194         //lyxerr << "\ncursor.pos_: " << cursor().pos_;
195         //lyxerr << "\nanchor.pos_: " << anchor().pos_;
196         //lyxerr << "\n===================^^^^^^^^^^^^=====================\n\n\n";
197 }
198
199
200 bool MathCursor::isInside(MathInset const * p) const
201 {
202         for (unsigned i = 0; i < Cursor_.size(); ++i) 
203                 if (parInset(i) == p) 
204                         return true;
205         return false;
206 }
207
208
209 bool MathCursor::openable(MathInset * p, bool sel, bool useupdown) const
210 {
211         if (!p)
212                 return false;
213
214         if (!(p->isActive() || (useupdown && p->isScriptInset())))
215                 return false;
216
217         if (sel) {
218                 // we can't move into anything new during selection
219                 if (Cursor_.size() == Anchor_.size())
220                         return false;
221                 if (p != Anchor_[Cursor_.size()].par_)
222                         return false;
223         }
224         return true;
225 }
226
227
228 void MathCursor::plainLeft()
229 {
230         --cursor().pos_;
231 }
232
233
234 void MathCursor::plainRight()
235 {
236         ++cursor().pos_;
237 }
238
239
240 bool MathCursor::left(bool sel)
241 {
242         dump("Left 1");
243         if (imacro_) {
244                 // was MacroModeBack()
245                 if (!imacro_->name().empty()) {
246                         imacro_->setName(imacro_->name().substr(0, imacro_->name().length()-1));
247                         imacro_->metrics(imacro_->size());
248                 } else
249                         macroModeClose();
250                 return true;
251         }
252         selHandle(sel);
253         clearLastCode();
254
255         MathInset * p = prevInset();
256         if (openable(p, sel, false)) {
257                 plainLeft();
258                 push(p, false);
259                 return true;
260         } 
261         if (cursor().pos_) {
262                 plainLeft();
263                 return true;
264         }
265         if (cursor().par_->idxLeft(cursor().idx_, cursor().pos_))
266                 return true;
267         if (pop())
268                 return true;
269         return false;
270 }
271
272
273 bool MathCursor::right(bool sel)
274 {
275         dump("Right 1");
276         if (imacro_) {
277                 macroModeClose();
278                 return true;
279         }
280         selHandle(sel);
281         clearLastCode();
282
283         MathInset * p = nextInset();
284         if (openable(p, sel, false)) {
285                 push(p, true);
286                 return true;
287         }
288         if (cursor().pos_ != array().size()) {
289                 plainRight();
290                 return true;
291         }
292         if (cursor().par_->idxRight(cursor().idx_, cursor().pos_)) {
293                 return true;
294         }
295         if (pop()) {
296                 plainRight();
297                 return true;
298         }
299         return false;
300 }
301
302
303 void MathCursor::first()
304 {
305         Cursor_.clear();
306         push(outerPar(), true);
307 }
308
309
310 void MathCursor::last()
311 {
312         Cursor_.clear();
313         push(outerPar(), false);
314 }
315
316
317 void MathCursor::setPos(int x, int y)
318 {
319         dump("setPos 1");
320         //lyxerr << "MathCursor::setPos x: " << x << " y: " << y << "\n";
321
322         macroModeClose();
323         lastcode_ = LM_TC_MIN;
324         first();
325
326         cursor().par_  = outerPar();
327
328         while (1) {
329                 cursor().idx_ = -1;
330                 cursor().pos_ = -1;
331                 //lyxerr << "found idx: " << idx_ << " cursor: " << cursor().pos_  << "\n";
332                 int distmin = 1 << 30; // large enough
333                 for (int i = 0; i < cursor().par_->nargs(); ++i) {
334                         MathXArray const & ar = cursor().par_->xcell(i);
335                         int x1 = x - ar.xo();
336                         int y1 = y - ar.yo();
337                         int c  = ar.x2pos(x1);
338                         int xx = abs(x1 - ar.pos2x(c));
339                         int yy = abs(y1);
340                         //lyxerr << "idx: " << i << " xx: " << xx << " yy: " << yy
341                         //      << " c: " << c  << " xo: " << ar.xo() << "\n";
342                         if (yy + xx <= distmin) {
343                                 distmin        = yy + xx;
344                                 cursor().idx_  = i;
345                                 cursor().pos_  = c;
346                         }
347                 }
348                 //lyxerr << "found idx: " << cursor().idx_ << " cursor: "
349                 //      << cursor().pos_  << "\n";
350                 MathInset * n = nextInset();
351                 MathInset * p = prevInset();
352                 if (openable(n, selection_, true) && n->covers(x, y))
353                         push(n, true);
354                 else if (openable(p, selection_, true) && p->covers(x, y)) {
355                         plainLeft();
356                         push(p, false);
357                 } else 
358                         break;
359         }
360         dump("setPos 2");
361 }
362
363
364 void MathCursor::home()
365 {
366         dump("home 1");
367         macroModeClose();
368         clearLastCode();
369         if (!cursor().par_->idxHome(cursor().idx_, cursor().pos_)) 
370                 pop();
371         dump("home 2");
372 }
373
374
375 void MathCursor::end()
376 {
377         dump("end 1");
378         macroModeClose();
379         clearLastCode();
380         if (!cursor().par_->idxEnd(cursor().idx_, cursor().pos_)) {
381                 pop();
382                 ++cursor().pos_;
383         }
384         dump("end 2");
385 }
386
387
388 void MathCursor::plainErase()
389 {
390         array().erase(cursor().pos_);
391 }
392
393
394 void MathCursor::insert(char c, MathTextCodes t)
395 {
396         //lyxerr << "inserting '" << c << "'\n";
397         if (selection_)
398                 selDel();
399
400         if (t != LM_TC_MIN)
401                 lastcode_ = t;
402
403         if (imacro_ && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
404                 macroModeClose();
405
406         if (imacro_) {
407                 if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
408                         // was MacroModeinsert(c);
409                         imacro_->setName(imacro_->name() + c);
410                         return;
411                 }
412         }
413
414         array().insert(cursor().pos_, c, t);
415         ++cursor().pos_;
416 }
417
418
419 void MathCursor::insert(MathInset * p)
420 {
421         macroModeClose();
422
423         if (selection_) {
424                 if (p->nargs())
425                         selCut();
426                 else
427                         selDel();
428         }
429
430         array().insert(cursor().pos_, p);
431         ++cursor().pos_;
432 }
433
434
435 void MathCursor::insert(MathArray const & ar)
436 {
437         macroModeClose();
438         if (selection_)
439                 selCut();
440
441         array().insert(cursor().pos_, ar);
442         cursor().pos_ += ar.size();
443 }
444
445
446 void MathCursor::erase()
447 {
448         dump("erase 1");
449         if (imacro_)
450                 return;
451
452         if (selection_) {
453                 selDel();
454                 return;
455         }
456
457         // delete empty cells if necessary
458         if (cursor().pos_ == 0 && array().empty()) {
459                 bool popit;
460                 bool removeit;
461                 cursor().par_->idxDelete(cursor().idx_, popit, removeit);
462                 if (popit && pop() && removeit)
463                         plainErase();
464                 return;
465         }
466
467         if (cursor().pos_ < array().size())
468                 plainErase();
469
470         dump("erase 2");
471 }
472
473
474 void MathCursor::delLine()
475 {
476         macroModeClose();
477
478         if (selection_) {
479                 selDel();
480                 return;
481         }
482
483         if (cursor().par_->nrows() > 1)
484                 cursor().par_->delRow(row());
485 }
486
487
488 bool MathCursor::up(bool sel)
489 {
490         dump("up 1");
491         macroModeClose();
492         selHandle(sel);
493
494         if (selection_) {
495                 int x = xarray().pos2x(cursor().pos_);
496                 if (cursor().idxDown()) {
497                         cursor().pos_ = xarray().x2pos(x);
498                         return true;
499                 }
500                 if (pop()) 
501                         return true;
502                 return false;
503         }
504
505         // check whether we could move into an inset on the right or on the left
506         MathInset * p = nextInset();
507         if (p) {
508                 int idx, pos;
509                 if (p->idxFirstUp(idx, pos)) {
510                         push(p, true);
511                         cursor().idx_ = idx;
512                         cursor().pos_ = pos;
513                         dump("up 3");
514                         return true;
515                 }
516         }
517
518         p = prevInset();
519         if (p) {
520                 int idx, pos;
521                 if (p->idxLastUp(idx, pos)) {
522                         plainLeft();
523                         push(p, false);
524                         cursor().idx_ = idx;
525                         cursor().pos_ = pos;
526                         dump("up 4");
527                         return true;
528                 }
529         }
530
531         int x = xarray().pos2x(cursor().pos_);
532         if (cursor().idxUp()) {
533                 cursor().pos_ = xarray().x2pos(x);
534                 return true;
535         }
536         if (pop())
537                 return true;
538         return false;
539 }
540
541
542 bool MathCursor::down(bool sel)
543 {
544         dump("down 1");
545         macroModeClose();
546         selHandle(sel);
547
548         if (selection_) {
549                 int x = xarray().pos2x(cursor().pos_);
550                 if (cursor().idxDown()) {
551                         cursor().pos_ = xarray().x2pos(x);
552                         return true;
553                 }
554                 if (pop()) 
555                         return true;
556                 return false;
557         }
558
559         // check whether we could move into an inset on the right or on the left
560         MathInset * p = nextInset();
561         if (p) {
562                 int idx, pos;
563                 if (p->idxFirstDown(idx, pos)) {
564                         push(p, true);
565                         cursor().idx_ = idx;
566                         cursor().pos_ = pos;
567                         dump("Down 3");
568                         return true;
569                 }
570         }
571
572         p = prevInset();
573         if (p) {
574                 int idx, pos;
575                 if (p->idxLastDown(idx, pos)) {
576                         plainLeft();
577                         push(p, false);
578                         cursor().idx_ = idx;
579                         cursor().pos_ = pos;
580                         dump("Down 4");
581                         return true;
582                 }
583         }
584
585         int x = xarray().pos2x(cursor().pos_);
586         if (cursor().idxDown()) {
587                 cursor().pos_ = xarray().x2pos(x);
588                 return true;
589         }
590         if (pop())
591                 return true;
592         return false;
593 }
594
595
596 bool MathCursor::toggleLimits()
597 {
598         MathScriptInset * p = prevScriptInset();
599         if (!p)
600                 return false;
601         int old = p->limits();
602         p->limits(old < 0 ? 1 : -1);
603         return old != p->limits();
604 }
605
606
607 void MathCursor::setSize(MathStyles size)
608 {
609         cursor().par_->userSetSize(size);
610 }
611
612
613
614 void MathCursor::interpret(string const & s)
615 {
616         //lyxerr << "interpret: '" << s << "'\n";
617         //lyxerr << "in: " << in_word_set(s) << " \n";
618
619         if (s.empty())
620                 return;
621
622         if (s[0] == '^' || s[0] == '_') {
623                 bool const up = (s[0] == '^');
624                 selCut();       
625                 MathScriptInset * p = prevScriptInset();
626                 if (!p) {
627                         MathInset * b = prevInset();
628                         if (b && b->isScriptable()) {
629                                 p = new MathScriptInset(up, !up, b->clone());
630                                 plainLeft();
631                                 erase();
632                         } else {
633                                 p = new MathScriptInset(up, !up);
634                         }
635                         insert(p);
636                         plainLeft();
637                 }
638                 push(p, true);
639                 if (up)
640                         p->up(true);
641                 else
642                         p->down(true);
643                 cursor().idx_ = up ? 0 : 1;
644                 cursor().pos_ = 0;
645                 selPaste();
646                 return;
647         }
648
649         if (s[0] == '!' || s[0] == ','  || s[0] == ':' || s[0] == ';') {
650                 int sp = (s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0));
651                 insert(new MathSpaceInset(sp));
652                 return;
653         }
654
655         MathInset * p = 0;
656         latexkeys const * l = in_word_set(s);
657
658         if (l == 0) {
659                 if (s == "root") 
660                         p = new MathRootInset;
661                 else if (MathMacroTable::hasTemplate(s))
662                         p = new MathMacro(MathMacroTable::provideTemplate(s));
663                 else if (s.size() > 7 && s.substr(0, 7) == "matrix ") {
664                         int m = 1;
665                         int n = 1;
666                         string v_align;
667                         string h_align;
668                         istringstream is(s.substr(7).c_str());
669                         is >> m >> n >> v_align >> h_align;
670                         m = std::max(1, m);
671                         n = std::max(1, n);
672                         v_align += 'c';
673                         MathArrayInset * pp = new MathArrayInset(m, n);
674                         pp->valign(v_align[0]);
675                         pp->halign(h_align);
676                         p = pp;
677                 }
678                 else
679                         p = new MathFuncInset(s);
680         } else {
681                 switch (l->token) {
682                         case LM_TK_BIGSYM:
683                                 p = new MathBigopInset(l);
684                                 break;
685
686                         case LM_TK_FUNCLIM:
687                                 p = new MathFuncLimInset(l);
688                                 break;
689
690                         case LM_TK_SYM: 
691                                 p = new MathSymbolInset(l);
692                                 break;
693
694                         case LM_TK_STACK:
695                                 p = new MathFracInset("stackrel");
696                                 break;
697
698                         case LM_TK_FRAC:
699                                 p = new MathFracInset("frac");
700                                 break;
701
702                         case LM_TK_SQRT:
703                                 p = new MathSqrtInset;
704                                 break;
705
706                         case LM_TK_DECORATION:
707                                 p = new MathDecorationInset(l);
708                                 break;
709
710                         case LM_TK_SPACE:
711                                 p = new MathSpaceInset(l->id);
712                                 break;
713
714                         case LM_TK_DOTS:
715                                 p = new MathDotsInset(l);
716                                 break;
717
718                         case LM_TK_MACRO:
719                                 p = new MathMacro(MathMacroTable::provideTemplate(s));
720                                 break;
721
722                         default:
723                                 p = new MathFuncInset(l->name);
724                                 break;
725                 }
726         }
727
728         if (p) {
729                 selCut();
730                 insert(p);
731                 if (p->nargs()) {
732                         plainLeft();
733                         right();  // do not push for e.g. MathSymbolInset
734                         selPaste();
735                 }
736                 p->metrics(p->size());
737         }
738 }
739
740
741 void MathCursor::macroModeOpen()
742 {
743         if (!imacro_) {
744                 imacro_ = new MathFuncInset("");
745                 array().insert(cursor().pos_, imacro_);
746                 ++cursor().pos_;
747                 //insert(imacro_);
748         } else
749                 lyxerr << "Math Warning: Already in macro mode" << endl;
750 }
751
752
753 void MathCursor::macroModeClose()
754 {
755         if (imacro_) {
756                 string name = imacro_->name();
757                 plainLeft();
758                 plainErase();
759                 imacro_ = 0;
760                 interpret(name);
761         }
762 }
763
764
765 void MathCursor::selCopy()
766 {
767         seldump("selCopy");
768         if (selection_) {
769                 theSelection.grab(*this);
770                 selClear();
771         }
772 }
773
774
775 void MathCursor::selCut()
776 {
777         seldump("selCut");
778         if (selection_) {
779                 theSelection.grab(*this);
780                 theSelection.erase(*this);
781         }
782         selClear();
783 }
784
785
786 void MathCursor::selDel()
787 {
788         seldump("selDel");
789         if (selection_) {
790                 theSelection.erase(*this);
791                 selClear();
792         }
793 }
794
795
796 void MathCursor::selPaste()
797 {
798         seldump("selPaste");
799         theSelection.paste(*this);
800         selClear();
801 }
802
803
804 void MathCursor::selHandle(bool sel)
805 {
806         if (sel && !selection_)
807                 selStart();
808         if (!sel && selection_)
809                 selClear();
810 }
811
812
813 void MathCursor::selStart()
814 {
815         seldump("selStart");
816         if (selection_)
817                 return;
818
819         Anchor_ = Cursor_;
820         selection_ = true;
821 }
822
823
824 void MathCursor::selClear()
825 {
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                 return;
887
888         MathDecorationInset * p = new MathDecorationInset(l);
889         if (selection_) {
890                 selCut();
891                 p->cell(0) = theSelection.glue();
892         }
893         insert(p);
894         push(p, true);
895 }
896
897
898 void MathCursor::handleDelim(int l, int r)
899 {
900         MathDelimInset * p = new MathDelimInset(l, r);
901         if (selection_) {
902                 selCut();
903                 p->cell(0) = theSelection.glue();
904         }
905         insert(p);
906         plainLeft();
907         push(p, true);
908 }
909
910
911 void MathCursor::getPos(int & x, int & y)
912 {
913         x = xarray().xo() + xarray().pos2x(cursor().pos_);
914         y = xarray().yo();
915 }
916
917
918 MathTextCodes MathCursor::nextCode() const
919 {
920         return array().getCode(cursor().pos_); 
921 }
922
923
924 MathTextCodes MathCursor::prevCode() const
925 {
926         return array().getCode(cursor().pos_ - 1); 
927 }
928
929
930 MathInset * MathCursor::par() const
931 {
932         return cursor().par_;
933 }
934
935
936 InsetFormulaBase const * MathCursor::formula()
937 {
938         return formula_;
939 }
940
941
942 int MathCursor::pos() const
943 {
944         return cursor().pos_;
945 }
946
947
948 bool MathCursor::inMacroMode() const
949 {
950         return imacro_;
951 }
952
953
954 bool MathCursor::selection() const
955 {
956         return selection_;
957 }
958
959
960 void MathCursor::clearLastCode()
961 {
962         lastcode_ = LM_TC_MIN;
963 }
964
965
966 void MathCursor::setLastCode(MathTextCodes t)
967 {
968         lastcode_ = t;
969 }
970
971
972 MathTextCodes MathCursor::getLastCode() const
973 {
974         return lastcode_;
975 }
976
977
978 MathArrayInset * MathCursor::enclosingArray(int & idx) const
979 {
980         for (int i = Cursor_.size() - 1; i >= 0; --i) {
981                 if (Cursor_[i].par_->isArray()) {
982                         idx = Cursor_[i].idx_;
983                         return static_cast<MathArrayInset *>(Cursor_[i].par_);
984                 }
985         }
986         return 0;
987 }
988
989
990 void MathCursor::pullArg(bool goright)
991 {
992         // pullArg
993         dump("pullarg");
994         MathArray a = array();
995         if (pop()) {
996                 plainErase();
997                 array().insert(cursor().pos_, a);
998                 if (goright) 
999                         cursor().pos_ += a.size();
1000         }
1001 }
1002
1003
1004 MathStyles MathCursor::style() const
1005 {
1006         return xarray().style();
1007 }
1008
1009
1010 void MathCursor::normalize() const
1011 {
1012 #ifdef WITH_WARNINGS
1013 #warning This is evil!
1014 #endif
1015         MathCursor * it = const_cast<MathCursor *>(this);
1016
1017         if (cursor().idx_ < 0 || cursor().idx_ > cursor().par_->nargs() - 1)
1018                 lyxerr << "this should not really happen - 1\n";
1019         it->cursor().idx_    = max(cursor().idx_, 0);
1020         it->cursor().idx_    = min(cursor().idx_, cursor().par_->nargs() - 1);
1021
1022         if (cursor().pos_ < 0 || cursor().pos_ > array().size())
1023                 lyxerr << "this should not really happen - 2\n";
1024         it->cursor().pos_ = max(cursor().pos_, 0);
1025         it->cursor().pos_ = min(cursor().pos_, array().size());
1026 }
1027
1028
1029 int MathCursor::col() const
1030 {
1031         return par()->col(cursor().idx_);
1032 }
1033
1034
1035 int MathCursor::row() const
1036 {
1037         return par()->row(cursor().idx_);
1038 }
1039
1040
1041 /*
1042 char MathCursorPos::getChar() const
1043 {
1044         return array().getChar(cursor().pos_);
1045 }
1046
1047
1048 string MathCursorPos::readString()
1049 {
1050         string s;
1051         int code = nextCode();
1052         for ( ; OK() && nextCode() == code; Next()) 
1053                 s += getChar();
1054
1055         return s;
1056 }
1057 */
1058
1059
1060 MathInset * MathCursor::prevInset() const
1061 {
1062         normalize();
1063         int c = cursor().pos_;
1064         if (!c)
1065                 return 0;
1066         return array().nextInset(c);
1067 }
1068
1069
1070 MathInset * MathCursor::nextInset() const
1071 {
1072         normalize();
1073         return array().nextInset(cursor().pos_);
1074 }
1075
1076
1077 MathScriptInset * MathCursor::prevScriptInset() const
1078 {
1079         normalize();
1080         MathInset * p = prevInset();
1081         return (p && p->isScriptInset()) ? static_cast<MathScriptInset *>(p) : 0;
1082 }
1083
1084
1085 MathSpaceInset * MathCursor::prevSpaceInset() const
1086 {
1087         normalize();
1088         MathInset * p = prevInset();
1089         return (p && p->isSpaceInset()) ? static_cast<MathSpaceInset *>(p) : 0;
1090 }
1091
1092
1093 MathArray & MathCursor::array() const
1094 {
1095         static MathArray dummy;
1096         if (!cursor().par_) {
1097                 lyxerr << "############  par_ not valid\n";
1098                 return dummy;
1099         }
1100
1101         if (cursor().idx_ < 0 || cursor().idx_ >= cursor().par_->nargs()) {
1102                 lyxerr << "############  idx_ " << cursor().idx_ << " not valid\n";
1103                 return dummy;
1104         }
1105
1106         return cursor().cell();
1107 }
1108
1109
1110 MathXArray & MathCursor::xarray() const
1111 {
1112         return cursor().xcell();
1113 }
1114
1115
1116 int MathCursor::xpos() const 
1117 {
1118         normalize();
1119         return xarray().pos2x(cursor().pos_);
1120 }
1121
1122
1123 void MathCursor::gotoX(int x)
1124 {
1125         cursor().pos_ = xarray().x2pos(x);      
1126 }
1127
1128
1129 void MathCursor::idxNext()
1130 {
1131         cursor().par_->idxNext(cursor().idx_, cursor().pos_);
1132 }
1133
1134
1135 void MathCursor::idxPrev()
1136 {
1137         cursor().par_->idxPrev(cursor().idx_, cursor().pos_);
1138 }
1139
1140
1141 void MathCursor::splitCell()
1142 {
1143         if (cursor().idx_ == cursor().par_->nargs() - 1) 
1144                 return;
1145         MathArray ar = array();
1146         ar.erase(0, cursor().pos_);
1147         array().erase(cursor().pos_, array().size());
1148         ++cursor().idx_;
1149         cursor().pos_ = 0;
1150         array().insert(0, ar);
1151 }
1152
1153
1154 void MathCursor::breakLine()
1155 {
1156         MathMatrixInset * p = outerPar();
1157         if (p->getType() == LM_OT_SIMPLE || p->getType() == LM_OT_EQUATION) {
1158                 p->mutate(LM_OT_EQNARRAY);
1159                 p->addRow(0);
1160                 cursor().idx_ = p->nrows();
1161                 cursor().pos_ = 0;
1162         } else {
1163                 p->addRow(row());
1164
1165                 // split line
1166                 const int r = row();
1167                 for (int c = col() + 1; c < p->ncols(); ++c) {
1168                         const int i1 = p->index(r, c);
1169                         const int i2 = p->index(r + 1, c);      
1170                         lyxerr << "swapping cells " << i1 << " and " << i2 << "\n";
1171                         p->cell(i1).swap(p->cell(i2));
1172                 }
1173
1174                 // split cell
1175                 splitCell();
1176                 p->cell(cursor().idx_).swap(p->cell(cursor().idx_ + p->ncols() - 1));
1177         }
1178 }
1179
1180
1181 char MathCursor::valign() const
1182 {
1183         int idx;
1184         MathArrayInset * p = enclosingArray(idx);
1185         return p ? p->valign() : 0;
1186 }
1187
1188
1189 char MathCursor::halign() const
1190 {
1191         int idx;
1192         MathArrayInset * p = enclosingArray(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.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 }
1317
1318
1319 MathMatrixInset * MathCursor::outerPar() const
1320 {
1321         return
1322                 static_cast<MathMatrixInset *>(const_cast<MathInset *>(formula_->par()));
1323 }