]> git.lyx.org Git - lyx.git/blob - src/mathed/math_cursor.C
my changes during the holidyas...i expect some compilers to have some problems, but...
[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: (c) 1996, Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta, Mathed & 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 FORMS_H_LOCATION
24 #include "math_inset.h"
25 #include "math_parser.h"
26 #include "math_cursor.h"
27 #include "math_macro.h"
28 #include "math_root.h"
29 #include "support/lstrings.h"
30 #include "debug.h"
31
32 extern void mathed_set_font(short type, int style);
33
34 extern GC canvasGC, mathGC, latexGC, cursorGC, mathFrameGC;
35
36 static LyxArrayBase * selarray = 0;
37
38 inline bool IsAlpha(char c)
39 {
40    return ('A' <= c  && c <= 'Z' || 'a' <= c  && c <= 'z');
41 }
42
43 // This was very smaller, I'll change it later 
44 inline bool IsMacro(short tok, int id)
45 {
46    return (tok != LM_TK_STACK && tok != LM_TK_FRAC && tok != LM_TK_SQRT
47            && tok != LM_TK_WIDE
48            && tok != LM_TK_SPACE && tok != LM_TK_DOTS
49            && tok != LM_TK_FUNCLIM
50            && tok != LM_TK_BIGSYM && tok != LM_TK_ACCENT && 
51            !(tok == LM_TK_SYM && id < 255));
52 }
53
54
55 // Yes, mathed isn't using string yet.
56 inline char * strnew(char const * s)
57 {
58     char * s1 = new char[strlen(s)+1];
59     strcpy(s1, s);
60     return s1;
61 }
62
63
64
65 #define MAX_STACK_ITEMS 32
66
67 struct MathStackXIter {
68     
69     int i, imax;
70     MathedXIter * item;
71     
72     MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
73         item = new MathedXIter[imax];
74         i = 0;
75     }
76     
77     MathStackXIter(MathStackXIter & stk);
78     
79     ~MathStackXIter() {
80         delete[] item;
81     }
82    
83     void push(MathedXIter ** a) {
84         *a = &item[i++];
85     }
86       
87     MathedXIter * pop() {
88         --i;
89         return &item[i - 1];
90     }
91       
92     MathedXIter * Item(int idx) {
93        return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
94     }
95
96     void Reset() {
97         i = 0;
98     }
99    
100     bool Full() {
101         return i >= MAX_STACK_ITEMS;
102     }
103    
104     bool Empty() {
105         return i <= 1;
106     }
107
108     int Level() { return i; } 
109     
110 } mathstk, *selstk = 0;
111
112
113 MathStackXIter::MathStackXIter(MathStackXIter & stk) {
114     imax = stk.imax;
115     item = new MathedXIter[imax];
116     i = stk.i;
117     for (int k = 0; k < i; ++k) {
118         item[k].SetData(stk.item[k].getPar());
119         item[k].GoBegin();
120         item[k].goPosAbs(stk.item[k].getPos());
121     }
122 }
123
124
125 /***----------------  Mathed Cursor  ---------------------------***/
126   
127 MathedCursor::MathedCursor(MathParInset * p) // : par(p)
128 {
129     accent = 0;
130     anchor = 0;
131     lastcode = LM_TC_MIN;
132     SetPar(p);
133 //    selarray = 0;     
134     if (!MathMacroTable::built)
135         MathMacroTable::mathMTable.builtinMacros();
136 }
137
138
139 void MathedCursor::SetPar(MathParInset * p)
140 {
141    win = 0;
142    is_visible = False;
143    macro_mode = false;
144    selection = false; // not SelClear() ?
145    mathstk.Reset();
146    mathstk.push(&cursor);
147    par = p;
148    cursor->SetData(par);
149 }
150
151
152 void MathedCursor::Draw(long unsigned pm, int x, int y)
153 {
154 //    lyxerr << "Cursor[" << x << " " << y << "] ";
155     win = pm;    // win = (mathedCanvas) ? mathedCanvas: pm;
156     par->Metrics();
157     int w = par->Width()+2, a = par->Ascent()+1, h = par->Height()+1;
158     if (par->GetType() > LM_OT_PAR) { a += 4;  h += 8; }
159     
160    if (!canvasGC) mathed_set_font(LM_TC_VAR, 1);
161     //   XFillRectangle(fl_display, pm, canvasGC, x, y-a, w, h);
162     XDrawRectangle(fl_display, pm, mathFrameGC, x - 1, y - a, w, h);
163     XFlush(fl_display);
164     MathParInset::pm = pm;
165     par->Draw(x, y);
166     cursor->Adjust();
167 }
168
169
170 void MathedCursor::Redraw()
171 {  
172         lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
173    par->Metrics();
174    int w = par->Width(), h = par->Height();
175    int x, y;
176    par->GetXY(x, y);
177    mathed_set_font(LM_TC_VAR, 1);
178    XFillRectangle(fl_display, win, canvasGC, x, y-par->Ascent(), w, h);
179    XFlush(fl_display);
180     MathParInset::pm = win;
181    par->Draw(x, y);
182 }
183
184
185 bool MathedCursor::Left(bool sel)
186 {
187    if (macro_mode) {
188       MacroModeBack();
189       return true;
190    }
191     clearLastCode();
192    if (sel && !selection) SelStart();
193    if (!sel && selection) SelClear();
194    bool result = cursor->Prev();
195    if (!result && !mathstk.Empty()) {
196       cursor = mathstk.pop();
197       cursor->Adjust();
198       result = true;
199       if (selection) SelClear();
200    } else  
201      if (result && cursor->IsActive()) {
202         if (cursor->IsScript()) {
203            cursor->Prev();
204            if (!cursor->IsScript())
205              cursor->Next();
206            cursor->Adjust(); 
207            return true;
208         }
209         if (!selection) {
210             MathParInset * p = cursor->GetActiveInset();
211             if (!p)
212               return result;
213                 
214             p->setArgumentIdx(p->getMaxArgumentIdx());
215             mathstk.push(&cursor);
216             cursor->SetData(p);
217             cursor->GoLast();
218         }
219      } 
220    return result;  
221 }
222
223
224 // Leave the inset
225 bool MathedCursor::Pop()
226 {
227    if (!mathstk.Empty()) {
228       cursor = mathstk.pop();
229       cursor->Next(); 
230       return true;
231    }
232    return false;
233 }
234
235
236 // Go to the inset 
237 bool MathedCursor::Push()
238
239    if (cursor->IsActive()) {
240       MathParInset * p = cursor->GetActiveInset();
241        if (!p) return false;
242       mathstk.push(&cursor);
243       cursor->SetData(p);
244       return true;
245    }
246    return false;
247 }  
248
249
250 bool MathedCursor::Right(bool sel)
251 {  
252    if (macro_mode) {
253       MacroModeClose();
254       return true;
255    } 
256     clearLastCode();
257    if (sel && !selection) SelStart();
258    if (!sel && selection) SelClear();
259    bool result = false;
260  
261    if (cursor->IsActive()) {
262       if (cursor->IsScript()) {
263          cursor->Next();
264          // A script may be followed by another script
265          if (cursor->IsScript()) 
266            cursor->Next();
267          return true;
268       }
269       if (!selection) { 
270           MathParInset *p = cursor->GetActiveInset();
271           if (!p) {
272                   lyxerr << "Math error: Inset expected." << endl;
273               return cursor->Next();
274           }
275           p->setArgumentIdx(0);
276           mathstk.push(&cursor);
277           cursor->SetData(p);
278           result = true;
279       } else
280          result = cursor->Next();
281    } else {
282        if (cursor->GetChar()!= LM_TC_CR)
283          result = cursor->Next();
284      if (!result && !mathstk.Empty()) {
285         cursor = mathstk.pop();
286         cursor->Next();
287          cursor->Adjust();
288         result = true;
289         if (selection) SelClear();
290      }
291    }
292    return result;
293 }
294
295
296 void MathedCursor::SetPos(int x, int y)
297 {
298     int xp = 0;
299     
300     if (macro_mode) MacroModeClose();
301     lastcode = LM_TC_MIN;
302     mathstk.Reset();
303     mathstk.push(&cursor);
304     cursor->SetData(par);
305     cursor->fitCoord(x, y);
306     while (cursor->GetX()<x && cursor->OK()) {
307         if (cursor->IsActive()) {
308             MathParInset * p = cursor->GetActiveInset();
309             if (p->Inside(x, y)) {
310                 p->SetFocus(x, y);
311                 mathstk.push(&cursor);
312                 cursor->SetData(p);
313                 cursor->fitCoord(x, y);
314                 continue;
315             }
316         }
317         xp = cursor->GetX();
318         cursor->ipush();
319         if (!cursor->Next() && !Pop()) 
320           break;
321     }
322     if (x-xp < cursor->GetX()-x) cursor->ipop();
323     cursor->Adjust();
324 }
325    
326
327 void MathedCursor::Home()
328 {
329    if (macro_mode) MacroModeClose();
330     clearLastCode();
331    mathstk.Reset();
332    mathstk.push(&cursor);
333    cursor->GoBegin();
334 }
335
336
337 void MathedCursor::End()
338 {
339    if (macro_mode) MacroModeClose();
340     clearLastCode();
341    mathstk.Reset();
342    mathstk.push(&cursor);
343    cursor->GoLast();
344 }
345
346
347 void MathedCursor::Insert(byte c, MathedTextCodes t)
348 {  
349    if (selection) SelDel();
350    
351    if (t == LM_TC_MIN)
352       t = lastcode;
353     
354    if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
355       MacroModeClose();
356         
357    if (t == LM_TC_CR) {
358       MathParInset * p = cursor->p;
359       if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
360          MathMatrixInset * mt = new MathMatrixInset(3, 0);
361          mt->SetAlign(' ', "rcl");
362          mt->SetStyle(LM_ST_DISPLAY);
363          mt->SetType((p->GetType() == LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
364          mt->SetData(p->GetData());
365          p->SetData(0);//BUG duda
366          delete p;
367          par = mt;
368          p = mt;
369          p->Metrics();
370          int pos = cursor->getPos();
371          cursor->SetData(par);
372          cursor->goPosAbs(pos);
373       }      
374       if (p &&  p->Permit(LMPF_ALLOW_CR)) {
375           cursor->addRow();
376       }
377    } else
378    if (t == LM_TC_TAB) {
379       MathParInset * p = cursor->p;
380       if (p &&  p->Permit(LMPF_ALLOW_TAB)) {
381           if (c) {
382               cursor->Insert(c, t);
383               cursor->checkTabs();
384           } else
385             cursor->goNextColumn();
386       } else // Navigate between arguments
387          if (p && p->GetType() == LM_OT_MACRO) {
388              if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
389                  p->setArgumentIdx(p->getArgumentIdx()+1);
390                  cursor->SetData(p);
391                  return;
392              }
393          }       
394    } else {
395        if (macro_mode) {
396            if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
397                MacroModeInsert(c);
398                return;
399            }
400        } 
401        if (accent) {
402                doAccent(c, t);
403        } else 
404          cursor->Insert(c, t);
405        lastcode = t;
406        
407        return;
408    }
409     clearLastCode();
410 }
411
412
413 void MathedCursor::Insert(MathedInset * p, int t)
414 {
415    if (macro_mode) MacroModeClose();
416    if (selection) {
417       if (MathIsActive(t)) {
418          SelCut();
419          static_cast<MathParInset*>(p)->SetData(selarray);
420       } else
421         SelDel();
422    }
423          
424    if (mathstk.i < MAX_STACK_ITEMS - 1) {
425        
426        if (accent && !MathIsActive(t)) {               
427                doAccent(p);
428        } else {
429            cursor->Insert(p, t);
430        
431            if (MathIsActive(t)) {
432                cursor->Prev();
433                Push();
434            }
435        }
436      
437    } else
438            lyxerr << "Math error: Full stack." << endl;
439 }
440
441
442 void MathedCursor::Delete() 
443 {   
444    if (macro_mode) return;
445    if (selection) {
446       SelDel();
447       return;
448    }
449    if (cursor->Empty() && !mathstk.Empty()) {
450       cursor = mathstk.pop();
451    } 
452 //   if (cursor->GetChar()!= LM_TC_TAB)
453     cursor->Delete();
454     cursor->checkTabs();
455 }
456
457
458 void MathedCursor::DelLine()
459 {  
460     if (macro_mode) MacroModeClose();
461     if (selection) {
462         SelDel();
463         return;
464     }
465     MathParInset *p= cursor->p;
466     if (p &&  (p->GetType()<= LM_OT_MATRIX && p->GetType()>= LM_OT_MPAR)) {
467         cursor->delRow();
468     }
469 }
470
471
472 bool MathedCursor::Up(bool sel)
473 {
474     bool result = false;
475    
476     if (macro_mode) MacroModeClose();
477     
478     if (sel && !selection) SelStart();
479     if (!sel && selection) SelClear();
480     
481
482     if (cursor->IsScript()) {
483         char cd = cursor->GetChar();
484         if (MathIsUp(cd)) {
485             Push();
486             return true;
487         } else {
488             // A subscript may be followed by a superscript
489             cursor->ipush();
490             cursor->Next();
491             if (MathIsUp(cursor->GetChar())) {
492                 Push();
493                 return true;
494             } else  // return to the previous state
495               cursor->ipop();
496       }
497    }    
498      
499     result = cursor->Up();
500     if (!result && cursor->p) {
501             MathParInset * p = cursor->p;   
502    
503         if (p->GetType() == LM_OT_SCRIPT) {
504             MathedXIter * cx = mathstk.Item(1);
505             bool is_down = (cx->GetChar() == LM_TC_DOWN);
506             cursor = mathstk.pop();
507             cursor->Next();
508             result =  (is_down) ? true: Up();
509         } else {
510             result = (p->getArgumentIdx() > 0);
511             if (result) {
512                 p->setArgumentIdx(p->getArgumentIdx()-1);
513                 cursor->SetData(p);
514             }
515         }
516         if (!result && !mathstk.Empty()) {
517             cursor = mathstk.pop();
518             return Up();
519         }     
520     }
521     return result;
522 }
523
524
525 bool MathedCursor::Down(bool sel)
526 {
527     bool result = false;
528    
529     if (macro_mode) MacroModeClose();
530     
531     if (sel && !selection) SelStart();
532     if (!sel && selection) SelClear();
533 //    if (selection) SelClear();
534
535     if (cursor->IsScript()) {
536         char cd = cursor->GetChar(); 
537         if (MathIsDown(cd)) {
538             Push();
539             return true;
540         } else {
541             // A superscript may be followed by a subscript
542             cursor->ipush();
543             cursor->Next();
544             if (MathIsDown(cursor->GetChar())) {
545                 Push();
546                 return true;
547             } else
548               cursor->ipop();
549       }
550    }
551      
552     result = cursor->Down();
553     if (!result && cursor->p) {
554         MathParInset * p= cursor->p;   
555         if (p->GetType() == LM_OT_SCRIPT) {
556             MathedXIter * cx = mathstk.Item(1);
557             bool is_up = (cx->GetChar() == LM_TC_UP);
558             cursor = mathstk.pop();
559             cursor->Next();
560             result = (is_up) ? true: Down();
561         } else {
562             result = (p->getArgumentIdx() < p->getMaxArgumentIdx());
563             if (result) {
564                 p->setArgumentIdx(p->getArgumentIdx()+1);
565                 cursor->SetData(p);
566             }
567         }
568         if (!result && !mathstk.Empty()) {
569             cursor = mathstk.pop();
570             return Down(sel);
571         }    
572     }
573     return result;
574 }
575
576
577 bool MathedCursor::Limits()
578 {
579    if (cursor->IsInset()) {
580       MathedInset * p = cursor->GetInset();
581       bool ol = p->GetLimits();
582       p->SetLimits(!ol);
583       return (ol!= p->GetLimits());
584    }
585    return false;
586 }
587
588
589 void MathedCursor::SetSize(short size)
590 {
591     MathParInset * p = cursor->p;
592     p->UserSetSize(size);
593     cursor->SetData(p);
594 }
595
596
597 void MathedCursor::setLabel(char const * label)
598 {  // ugly hack and possible bug
599     if (!cursor->setLabel(strnew(label)))
600             lyxerr << "MathErr: Bad place to set labels." << endl;
601 }
602
603
604 void MathedCursor::setNumbered()
605 {  // another ugly hack
606     MathedRowSt * crow = cursor->crow;
607     if (!crow) return;    
608     crow->setNumbered(!crow->isNumbered());
609 }
610
611
612 void MathedCursor::Interpret(char const * s)
613 {
614     MathedInset * p = 0;
615     latexkeys * l = 0;   
616     MathedTextCodes tcode = LM_TC_INSET;
617     
618     if (s[0] == '^' || s[0] == '_') {
619         char c = cursor->GetChar();
620         if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
621             Push();
622             return;
623         } else // A script may be followed by a script
624           if (MathIsUp(c)  || MathIsDown(c)) { 
625             cursor->ipush();
626             cursor->Next();
627             c = cursor->GetChar();
628             if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
629                 Push();
630                 return;
631             } else
632               cursor->ipop();
633         }
634         p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
635         Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP); 
636         return;
637     } else   
638       if (s[0] == '!' || s[0] == ','  || s[0] == ':' || s[0] == ';') {
639           int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0))); 
640           p = new MathSpaceInset(sp);
641           Insert(p);
642           return;
643       } else  
644       l = in_word_set (s, strlen(s));
645     
646     if (!l) {       
647         p = MathMacroTable::mathMTable.getMacro(s);
648         if (!p) {
649             lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
650             if (strcmp("root", s) == 0) {
651                 p = new MathRootInset();
652                 tcode = LM_TC_ACTIVE_INSET;
653             } else
654               p = new MathFuncInset(s, LM_OT_UNDEF);
655         } else {
656             tcode = static_cast<MathMacro*>(p)->getTCode();
657             lyxerr << "Macro2 " << s << ' ' << tcode << "  " ;
658         }
659     } else {
660         MathedInsetTypes fractype = LM_OT_FRAC;
661         switch (l->token) {
662          case LM_TK_BIGSYM:
663          {
664              p = new MathBigopInset(l->name, l->id);
665              break;
666          }
667          case LM_TK_SYM:
668          {                   
669              if (l->id<255) {
670                  Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ? 
671                         LM_TC_BOPS: LM_TC_SYMB);            
672              } else {
673                  p = new MathFuncInset(l->name);
674              }
675              break;
676          }  
677          case LM_TK_STACK:
678             fractype = LM_OT_STACKREL;
679             lyxerr[Debug::MATHED] << " i:stackrel " << endl;
680          case LM_TK_FRAC: 
681          {       
682              p = new MathFracInset(fractype);
683              tcode = LM_TC_ACTIVE_INSET;
684              break;
685          }
686          case LM_TK_SQRT: 
687          {       
688              p = new MathSqrtInset; 
689              tcode = LM_TC_ACTIVE_INSET;
690              break;
691          }
692          case LM_TK_WIDE: 
693          {       
694              p = new MathDecorationInset(l->id); 
695              tcode = LM_TC_ACTIVE_INSET;
696              break;
697          } 
698          case  LM_TK_FUNCLIM:
699          {
700              p = new MathFuncInset(l->name, LM_OT_FUNCLIM);
701              break;
702          }
703          case LM_TK_SPACE:
704          {
705              p = new MathSpaceInset(l->id);
706              break;
707          }         
708          case LM_TK_DOTS: 
709          {
710              p = new MathDotsInset(l->name, l->id);
711              break;
712          }         
713          case LM_TK_ACCENT:
714             setAccent(l->id);
715             break;
716          case LM_TK_MACRO:
717             p = MathMacroTable::mathMTable.getMacro(s);
718             tcode = static_cast<MathMacro*>(p)->getTCode();
719             lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
720             break;
721          default:
722          {
723              p = new MathFuncInset(l->name);
724              break;
725          }
726         }
727     }
728     if (p) {
729         Insert(p, tcode);
730         par->Metrics();
731     }
732 }
733
734
735 bool MathedCursor::pullArg()
736
737     if (cursor->IsActive()) {
738         MathParInset * p = cursor->GetActiveInset();
739         if (!p) { 
740             return false;
741         }
742         LyxArrayBase * a = p->GetData();
743         p->SetData(0);
744         Delete();
745         if (a) {
746             cursor->Merge(a);
747             cursor->Adjust();
748         }
749         
750         return true;
751     }
752     return false;
753 }  
754  
755
756 void MathedCursor::MacroModeOpen()
757 {
758    if (!macro_mode)  {
759       macroln = 0;
760       macrobf[0] = '\0';
761       imacro = new MathFuncInset(&macrobf[0]);
762       Insert (imacro);
763       macro_mode = true;
764    } else
765            lyxerr << "Mathed Warning: Already in macro mode" << endl;
766 }
767
768
769 void MathedCursor::MacroModeClose()
770 {
771    if (macro_mode)  {
772       macro_mode = false;
773       latexkeys * l = in_word_set(macrobf, macroln);
774       if (macroln > 0 && (!l || (l && IsMacro(l->token, l->id))) && 
775           !MathMacroTable::mathMTable.getMacro(macrobf)) {
776           if (!l) {
777             imacro->SetName(strnew(macrobf));
778               // This guarantees that the string will be removed by destructor
779             imacro->SetType(LM_OT_UNDEF);
780           } else 
781             imacro->SetName(l->name);
782       } else {
783          Left();
784          imacro->SetName(0);
785          if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
786              setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
787          }
788          cursor->Delete();
789          if (l || MathMacroTable::mathMTable.getMacro(macrobf)) {
790             Interpret(macrobf);
791          }
792       }
793       imacro = 0;
794    }  
795 }
796
797
798 void MathedCursor::MacroModeBack()
799 {
800    if (macro_mode) {
801      if (macroln>0) {
802         macrobf[--macroln] = '\0';
803         imacro->Metrics();
804      } else 
805         MacroModeClose();
806    } else
807            lyxerr << "Mathed Warning: we are not in macro mode" << endl;
808 }
809
810
811 void MathedCursor::MacroModeInsert(char c)
812 {
813    if (macro_mode) {
814       macrobf[macroln + 1] = macrobf[macroln];
815       macrobf[macroln++] = c;
816       imacro->Metrics();
817    } else
818            lyxerr << "Mathed Warning: we are not in macro mode" << endl;
819 }
820
821
822 void MathedCursor::SelCopy()
823 {
824     if (selection) {
825         int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
826         int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
827         selarray = cursor->Copy(p1, p2);
828         cursor->Adjust();
829         SelClear();
830     }
831 }
832
833
834 void MathedCursor::SelCut()
835 {   
836     if (selection) {
837         if (cursor->pos == selpos) return;
838         
839         int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
840         int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
841         selarray = cursor->Copy(p1, p2);
842         cursor->Clean(selpos);
843         cursor->Adjust();
844         SelClear();
845     }
846 }
847
848
849 void MathedCursor::SelDel()
850 {
851 //    lyxerr << "Deleting sel "
852     if (selection) {    
853         if (cursor->pos == selpos) return;
854         cursor->Clean(selpos);
855         cursor->Adjust();
856         SelClear();
857     }
858 }
859
860
861 void MathedCursor::SelPaste()
862 {
863 //    lyxerr << "paste " << selarray << " " << curor->pos;
864     if (selection) SelDel();
865     if (selarray) {
866         cursor->Merge(selarray);
867         cursor->Adjust();
868     }
869 }
870
871
872 void MathedCursor::SelStart()
873 {
874         lyxerr[Debug::MATHED] << "Starting sel " << endl;
875     if (!anchor) {
876         selpos = cursor->pos;   
877         selstk = new MathStackXIter(mathstk); 
878         anchor = selstk->Item(-1); 
879         anchor->SetData(cursor->p);
880         anchor->GoBegin();
881         anchor->goPosAbs(selpos);
882         selection = true;
883         
884     }
885 }
886
887
888 void MathedCursor::SelClear()
889 {   
890         lyxerr[Debug::MATHED] << "Clearing sel " << endl;
891     selection = false;
892     delete selstk;
893     selstk = 0;
894     anchor = 0;
895 }
896
897
898
899 // Anchor position must be at the same level that stack.
900 void MathedCursor::SelBalance()
901 {
902     int d = mathstk.Level() - selstk->Level();
903
904     // If unbalanced, balance them
905     while (d != 0) {
906         if (d < 0) {
907 //            lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
908             anchor = selstk->pop();
909             if (anchor->GetX() >= cursor->GetX()) 
910               anchor->Next();
911         } else {
912 //            lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
913             Pop();
914         }
915         d = mathstk.Level() - selstk->Level();
916     }
917
918     // Once balanced the levels, check that they are at the same paragraph
919     selpos = anchor->pos;
920
921
922
923 XPoint * MathedCursor::SelGetArea(int & np)
924 {   
925     if (!selection) {
926         np = 0;
927         return 0;
928     }
929     
930     static XPoint point[10];
931     
932     // single row selection
933     int i = 0, x, y, a, d, xo, yo, x1, y1, a1, d1; //, p1, p2;
934
935     // Balance anchor and cursor
936     SelBalance();
937  
938     cursor->p->GetXY(xo, yo);
939     int w = cursor->p->Width();
940     cursor->GetPos(x1, y1);
941     cursor->getAD(a1, d1);
942     anchor->GetPos(x, y);
943     anchor->getAD(a, d);
944
945     point[i].x = x;
946     point[i++].y = y+d;
947     point[i].x = x;
948     point[i++].y = y-a;
949     
950     if (y != y1) {
951         point[i].x = xo + w;
952         point[i++].y = y - a;
953         if (x1 < xo + w) {
954             point[i].x = xo + w;
955             point[i++].y = y1 - a;
956         }
957     }
958         
959     point[i].x = x1;
960     point[i++].y = y1 - a;
961     point[i].x = x1;
962     point[i++].y = y1 + d;
963     
964     if (y != y1) {
965         point[i].x = xo;
966         point[i++].y = y1 + d;
967         if (x > xo) {
968             point[i].x = xo;
969             point[i++].y = y + d;
970         }
971     }
972     point[i].x = point[0].x;
973     point[i++].y = point[0].y;
974     np = i;
975 //    lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
976 //    lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
977 //    for (i= 0; i<np; i++)
978 //      lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
979     
980     return &point[0];
981 }
982
983
984
985 void MathedCursor::setAccent(int ac)
986 {
987         if (ac > 0 && accent < 8) {
988                 nestaccent[accent++] = ac;
989         } else
990           accent = 0;  // consumed!
991 }
992
993  
994 int MathedCursor::getAccent() const
995 {
996         return (accent > 0) ? nestaccent[accent - 1]: 0;
997 }
998
999
1000 void MathedCursor::doAccent(byte c, MathedTextCodes t)
1001 {
1002         MathedInset * ac = 0;
1003         
1004         for (int i = accent - 1; i >= 0; --i) {
1005                 if (i == accent - 1)
1006                   ac = new MathAccentInset(c, t, nestaccent[i]);
1007                 else 
1008                   ac = new MathAccentInset(ac, nestaccent[i]);
1009         }
1010         if (ac) 
1011           cursor->Insert(ac);
1012         
1013         accent = 0;  // consumed!
1014 }
1015
1016
1017 void MathedCursor::doAccent(MathedInset * p)
1018 {
1019         MathedInset * ac = 0;
1020         
1021         for (int i = accent - 1; i >= 0; --i) {
1022                 if (i == accent - 1)
1023                   ac = new MathAccentInset(p, nestaccent[i]);
1024                 else 
1025                   ac = new MathAccentInset(ac, nestaccent[i]);
1026         }
1027         if (ac) 
1028           cursor->Insert(ac);
1029         
1030         accent = 0;  // consumed!
1031 }
1032