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