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