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