]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.C
Hopefully fix the problem with stateText() in lyxfont.C
[lyx.git] / src / mathed / math_cursor.C
index 7597f8046a724611410c9d78f428a33a3fbdf60d..fe467c34458ed8e50291035bdf8079a923c18d43 100644 (file)
@@ -7,7 +7,7 @@
  *
  *  Dependencies: Xlib, XForms
  *
- *  Copyright: (c) 1996, Alejandro Aguilar Sierra
+ *  Copyright: 1996, Alejandro Aguilar Sierra
  *
  *   Version: 0.8beta, Mathed & Lyx project.
  *
 #include "math_cursor.h"
 #include "math_macro.h"
 #include "math_root.h"
-#include "error.h"
+#include "support/lstrings.h"
+#include "debug.h"
+#include "LColor.h"
+#include "Painter.h"
 
-extern void mathed_set_font(short type, int style);
+static LyxArrayBase * selarray = 0;
 
-extern GC canvasGC, mathGC, latexGC, cursorGC, mathFrameGC;
+using std::endl;
 
-static LyxArrayBase *selarray=0;
-
-inline bool IsAlpha(char c)
+inline
+bool IsAlpha(char c)
 {
-   return ('A' <= c  && c<='Z' || 'a' <= c  && c<='z');
+   return ('A' <= c  && c <= 'Z' || 'a' <= c  && c <= 'z');
 }
 
 // This was very smaller, I'll change it later 
-inline bool IsMacro(short token, int id)
+inline
+bool IsMacro(short tok, int id)
 {
-   return (token!=LM_TK_STACK && token!=LM_TK_FRAC && token!=LM_TK_SQRT && token!=LM_TK_WIDE &&
-          token!=LM_TK_SPACE && token!=LM_TK_DOTS &&  token!=LM_TK_FUNCLIM &&
-          token!=LM_TK_BIGSYM && token!=LM_TK_ACCENT && 
-          !(token==LM_TK_SYM && id<255));
+   return (tok != LM_TK_STACK && tok != LM_TK_FRAC && tok != LM_TK_SQRT
+          && tok != LM_TK_WIDE
+          && tok != LM_TK_SPACE && tok != LM_TK_DOTS
+          && tok != LM_TK_FUNCLIM
+          && tok != LM_TK_BIGSYM && tok != LM_TK_ACCENT && 
+          !(tok == LM_TK_SYM && id < 255));
 }
 
-// Yes, mathed isn't using LString yet.
-inline char *strnew(char const* s)
+
+// Yes, mathed isn't using string yet.
+inline
+char * strnew(char const * s)
 {
-    char *s1 = new char[strlen(s)+1];
+    char * s1 = new char[strlen(s)+1];
     strcpy(s1, s);
     return s1;
 }
@@ -63,54 +70,54 @@ inline char *strnew(char const* s)
 struct MathStackXIter {
     
     int i, imax;
-    MathedXIter *item;
+    MathedXIter * item;
     
-    MathStackXIter(int n=MAX_STACK_ITEMS): imax(n) {
+    MathStackXIter(int n = MAX_STACK_ITEMS): imax(n) {
        item = new MathedXIter[imax];
        i = 0;
     }
     
-    MathStackXIter(MathStackXIter &stk);
+    MathStackXIter(MathStackXIter & stk);
     
     ~MathStackXIter() {
        delete[] item;
     }
    
-    void push(MathedXIter** a) {
+    void push(MathedXIter ** a) {
        *a = &item[i++];
     }
       
-    MathedXIter* pop() {
-       i--;
-       return &item[i-1];
+    MathedXIter * pop() {
+       --i;
+       return &item[i - 1];
     }
       
-    MathedXIter* Item(int idx) {
-       return (idx+1 <= i) ? &item[i-idx-1]: (MathedXIter*)NULL;
+    MathedXIter * Item(int idx) {
+       return (idx + 1 <= i) ? &item[i - idx - 1] : 0;
     }
 
     void Reset() {
        i = 0;
     }
    
-    int Full() {
-       return (i>=MAX_STACK_ITEMS);
+    bool Full() {
+       return i >= MAX_STACK_ITEMS;
     }
    
-    int Empty() {
-       return (i<=1);
+    bool Empty() {
+       return i <= 1;
     }
 
     int Level() { return i; } 
     
-} mathstk, *selstk=0;
+} mathstk, *selstk = 0;
 
 
-MathStackXIter::MathStackXIter(MathStackXIter &stk) {
+MathStackXIter::MathStackXIter(MathStackXIter & stk) {
     imax = stk.imax;
     item = new MathedXIter[imax];
     i = stk.i;
-    for (int k=0; k<i; k++) {
+    for (int k = 0; k < i; ++k) {
         item[k].SetData(stk.item[k].getPar());
         item[k].GoBegin();
         item[k].goPosAbs(stk.item[k].getPos());
@@ -120,19 +127,19 @@ MathStackXIter::MathStackXIter(MathStackXIter &stk) {
 
 /***----------------  Mathed Cursor  ---------------------------***/
   
-MathedCursor::MathedCursor(MathParInset *p) // : par(p)
+MathedCursor::MathedCursor(MathParInset * p) // : par(p)
 {
     accent = 0;
     anchor = 0;
     lastcode = LM_TC_MIN;
     SetPar(p);
-//    selarray = NULL;     
+//    selarray = 0;     
     if (!MathMacroTable::built)
        MathMacroTable::mathMTable.builtinMacros();
 }
 
 
-void MathedCursor::SetPar(MathParInset *p)
+void MathedCursor::SetPar(MathParInset * p)
 {
    win = 0;
    is_visible = False;
@@ -144,38 +151,39 @@ void MathedCursor::SetPar(MathParInset *p)
    cursor->SetData(par);
 }
 
-void MathedCursor::Draw(long unsigned pm, int x, int y)
+
+void MathedCursor::draw(Painter & pain, int x, int y)
 {
-//    fprintf(stderr, "Cursor[%d %d] ", x, y);
-    win = pm;    // win = (mathedCanvas) ? mathedCanvas: pm;
-    par->Metrics();
-    int w = par->Width()+2, a = par->Ascent()+1, h = par->Height()+1;
-    if (par->GetType()>LM_OT_PAR) { a += 4;  h += 8; }
-    
-   if (!canvasGC) mathed_set_font(LM_TC_VAR, 1);
-    //   XFillRectangle(fl_display,pm, canvasGC, x, y-a, w, h);
-    XDrawRectangle(fl_display,pm, mathFrameGC, x-1, y-a, w, h);
-    XFlush(fl_display);
-    MathParInset::pm = pm;
-    par->Draw(x, y);
-    cursor->Adjust();
+       //    lyxerr << "Cursor[" << x << " " << y << "] ";
+       //win = pm;    // win = (mathedCanvas) ? mathedCanvas: pm;
+       par->Metrics();
+       int w = par->Width() + 2;
+       int a = par->Ascent() + 1;
+       int h = par->Height() + 1;
+       if (par->GetType() > LM_OT_PAR) { a += 4;  h += 8; }
+       
+       pain.rectangle(x - 1, y - a, w, h, LColor::mathframe);
+       
+       par->draw(pain, x, y);
+       cursor->Adjust();
 }
 
 
-void MathedCursor::Redraw()
+void MathedCursor::Redraw(Painter & pain)
 {  
-   lyxerr.debug("Mathed: Redrawing!", Error::MATHED);
-   par->Metrics();
-   int w = par->Width(), h = par->Height();
-   int x, y;
-   par->GetXY(x, y);
-   mathed_set_font(LM_TC_VAR, 1);
-   XFillRectangle(fl_display, win,canvasGC,x, y-par->Ascent(), w, h);
-   XFlush(fl_display);
-    MathParInset::pm = win;
-   par->Draw(x, y);
+       lyxerr[Debug::MATHED] << "Mathed: Redrawing!" << endl;
+       par->Metrics();
+       int w = par->Width(), h = par->Height();
+       int x, y;
+       par->GetXY(x, y);
+       //mathed_set_font(LM_TC_VAR, 1);
+       pain.fillRectangle(x, y - par->Ascent(),
+                          x + w, y - par->Ascent() + h,
+                          LColor::mathbg);
+       par->draw(pain, x, y);
 }
 
+
 bool MathedCursor::Left(bool sel)
 {
    if (macro_mode) {
@@ -201,7 +209,7 @@ bool MathedCursor::Left(bool sel)
           return true;
        }
        if (!selection) {
-           MathParInset *p = cursor->GetActiveInset();
+           MathParInset * p = cursor->GetActiveInset();
            if (!p)
              return result;
                
@@ -214,6 +222,7 @@ bool MathedCursor::Left(bool sel)
    return result;  
 }
 
+
 // Leave the inset
 bool MathedCursor::Pop()
 {
@@ -225,11 +234,12 @@ bool MathedCursor::Pop()
    return false;
 }
 
+
 // Go to the inset 
 bool MathedCursor::Push()
 { 
    if (cursor->IsActive()) {
-      MathParInset *p = cursor->GetActiveInset();
+      MathParInset * p = cursor->GetActiveInset();
        if (!p) return false;
       mathstk.push(&cursor);
       cursor->SetData(p);
@@ -238,6 +248,7 @@ bool MathedCursor::Push()
    return false;
 }  
 
+
 bool MathedCursor::Right(bool sel)
 {  
    if (macro_mode) {
@@ -260,7 +271,7 @@ bool MathedCursor::Right(bool sel)
       if (!selection) { 
          MathParInset *p = cursor->GetActiveInset();
          if (!p) {
-             fprintf(stderr, "Math error: Inset expected.\n");
+                 lyxerr << "Math error: Inset expected." << endl;
              return cursor->Next();
          }
          p->setArgumentIdx(0);
@@ -270,7 +281,7 @@ bool MathedCursor::Right(bool sel)
       } else
         result = cursor->Next();
    } else {
-       if (cursor->GetChar()!=LM_TC_CR)
+       if (cursor->GetChar()!= LM_TC_CR)
         result = cursor->Next();
      if (!result && !mathstk.Empty()) {
        cursor = mathstk.pop();
@@ -296,7 +307,7 @@ void MathedCursor::SetPos(int x, int y)
     cursor->fitCoord(x, y);
     while (cursor->GetX()<x && cursor->OK()) {
        if (cursor->IsActive()) {
-           MathParInset *p = cursor->GetActiveInset();
+           MathParInset * p = cursor->GetActiveInset();
            if (p->Inside(x, y)) {
                p->SetFocus(x, y);
                mathstk.push(&cursor);
@@ -324,6 +335,7 @@ void MathedCursor::Home()
    cursor->GoBegin();
 }
 
+
 void MathedCursor::End()
 {
    if (macro_mode) MacroModeClose();
@@ -333,23 +345,24 @@ void MathedCursor::End()
    cursor->GoLast();
 }
 
+
 void MathedCursor::Insert(byte c, MathedTextCodes t)
 {  
    if (selection) SelDel();
    
-   if (t==LM_TC_MIN)
+   if (t == LM_TC_MIN)
       t = lastcode;
     
-   if (macro_mode && !(MathIsAlphaFont(t) || t==LM_TC_MIN))
+   if (macro_mode && !(MathIsAlphaFont(t) || t == LM_TC_MIN))
       MacroModeClose();
        
-   if (t==LM_TC_CR) {
-      MathParInset *p= cursor->p;
-      if (p==par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
-        MathMatrixInset* mt = new MathMatrixInset(3, 0);
+   if (t == LM_TC_CR) {
+      MathParInset * p = cursor->p;
+      if (p == par && p->GetType()<LM_OT_MPAR && p->GetType()>LM_OT_MIN) {
+        MathMatrixInset * mt = new MathMatrixInset(3, 0);
         mt->SetAlign(' ', "rcl");
         mt->SetStyle(LM_ST_DISPLAY);
-        mt->SetType((p->GetType()==LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
+        mt->SetType((p->GetType() == LM_OT_PARN) ? LM_OT_MPARN: LM_OT_MPAR);
         mt->SetData(p->GetData());
         p->SetData(0);//BUG duda
         delete p;
@@ -364,8 +377,8 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
          cursor->addRow();
       }
    } else
-   if (t==LM_TC_TAB) {
-      MathParInset *p = cursor->p;
+   if (t == LM_TC_TAB) {
+      MathParInset * p = cursor->p;
       if (p &&  p->Permit(LMPF_ALLOW_TAB)) {
          if (c) {
              cursor->Insert(c, t);
@@ -373,7 +386,7 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
          } else
            cursor->goNextColumn();
       } else // Navigate between arguments
-        if (p && p->GetType()==LM_OT_MACRO) {
+        if (p && p->GetType() == LM_OT_MACRO) {
             if (p->getArgumentIdx() < p->getMaxArgumentIdx()) {
                 p->setArgumentIdx(p->getArgumentIdx()+1);
                 cursor->SetData(p);
@@ -382,7 +395,7 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
         }       
    } else {
        if (macro_mode) {
-          if (MathIsAlphaFont(t) || t==LM_TC_MIN) {
+          if (MathIsAlphaFont(t) || t == LM_TC_MIN) {
               MacroModeInsert(c);
               return;
           }
@@ -398,18 +411,19 @@ void MathedCursor::Insert(byte c, MathedTextCodes t)
     clearLastCode();
 }
 
-void MathedCursor::Insert(MathedInset* p, int t)
+
+void MathedCursor::Insert(MathedInset * p, int t)
 {
    if (macro_mode) MacroModeClose();
    if (selection) {
       if (MathIsActive(t)) {
         SelCut();
-        ((MathParInset*)p)->SetData(selarray);
+        static_cast<MathParInset*>(p)->SetData(selarray);
       } else
        SelDel();
    }
          
-   if (mathstk.i<MAX_STACK_ITEMS-1) {
+   if (mathstk.i < MAX_STACK_ITEMS - 1) {
        
        if (accent && !MathIsActive(t)) {              
               doAccent(p);
@@ -423,9 +437,10 @@ void MathedCursor::Insert(MathedInset* p, int t)
        }
      
    } else
-     fprintf(stderr, "Math error: Full stack.\n");
+          lyxerr << "Math error: Full stack." << endl;
 }
 
+
 void MathedCursor::Delete() 
 {   
    if (macro_mode) return;
@@ -436,11 +451,12 @@ void MathedCursor::Delete()
    if (cursor->Empty() && !mathstk.Empty()) {
       cursor = mathstk.pop();
    } 
-//   if (cursor->GetChar()!=LM_TC_TAB)
+//   if (cursor->GetChar()!= LM_TC_TAB)
     cursor->Delete();
     cursor->checkTabs();
 }
 
+
 void MathedCursor::DelLine()
 {  
     if (macro_mode) MacroModeClose();
@@ -449,7 +465,7 @@ void MathedCursor::DelLine()
        return;
     }
     MathParInset *p= cursor->p;
-    if (p &&  (p->GetType()<=LM_OT_MATRIX && p->GetType()>=LM_OT_MPAR)) {
+    if (p &&  (p->GetType()<= LM_OT_MATRIX && p->GetType()>= LM_OT_MPAR)) {
        cursor->delRow();
     }
 }
@@ -464,7 +480,6 @@ bool MathedCursor::Up(bool sel)
     if (sel && !selection) SelStart();
     if (!sel && selection) SelClear();
     
-    MathParInset *p;
 
     if (cursor->IsScript()) {
        char cd = cursor->GetChar();
@@ -485,11 +500,11 @@ bool MathedCursor::Up(bool sel)
      
     result = cursor->Up();
     if (!result && cursor->p) {
-       p = cursor->p;   
+           MathParInset * p = cursor->p;   
    
-       if (p->GetType()==LM_OT_SCRIPT) {
-           MathedXIter *cx = mathstk.Item(1);
-           bool is_down = (cx->GetChar()==LM_TC_DOWN);
+       if (p->GetType() == LM_OT_SCRIPT) {
+           MathedXIter * cx = mathstk.Item(1);
+           bool is_down = (cx->GetChar() == LM_TC_DOWN);
            cursor = mathstk.pop();
            cursor->Next();
            result =  (is_down) ? true: Up();
@@ -519,8 +534,6 @@ bool MathedCursor::Down(bool sel)
     if (!sel && selection) SelClear();
 //    if (selection) SelClear();
 
-    MathParInset *p;
-
     if (cursor->IsScript()) {
        char cd = cursor->GetChar(); 
        if (MathIsDown(cd)) {
@@ -540,10 +553,10 @@ bool MathedCursor::Down(bool sel)
      
     result = cursor->Down();
     if (!result && cursor->p) {
-       p= cursor->p;   
-       if (p->GetType()==LM_OT_SCRIPT) {
-           MathedXIter *cx = mathstk.Item(1);
-           bool is_up = (cx->GetChar()==LM_TC_UP);
+       MathParInset * p= cursor->p;   
+       if (p->GetType() == LM_OT_SCRIPT) {
+           MathedXIter * cx = mathstk.Item(1);
+           bool is_up = (cx->GetChar() == LM_TC_UP);
            cursor = mathstk.pop();
            cursor->Next();
            result = (is_up) ? true: Down();
@@ -562,49 +575,51 @@ bool MathedCursor::Down(bool sel)
     return result;
 }
 
+
 bool MathedCursor::Limits()
 {
    if (cursor->IsInset()) {
-      MathedInset *p = cursor->GetInset();
+      MathedInset * p = cursor->GetInset();
       bool ol = p->GetLimits();
       p->SetLimits(!ol);
-      return (ol!=p->GetLimits());
+      return (ol!= p->GetLimits());
    }
    return false;
 }
 
+
 void MathedCursor::SetSize(short size)
 {
-    MathParInset *p = cursor->p;
+    MathParInset * p = cursor->p;
     p->UserSetSize(size);
     cursor->SetData(p);
 }
 
 
-void MathedCursor::setLabel(char const* label)
+void MathedCursor::setLabel(char const * label)
 {  // ugly hack and possible bug
     if (!cursor->setLabel(strnew(label)))
-      fprintf(stderr, "MathErr: Bad place to set labels.");
+           lyxerr << "MathErr: Bad place to set labels." << endl;
 }
 
 
 void MathedCursor::setNumbered()
 {  // another ugly hack
-    MathedRowSt *crow = cursor->crow;
+    MathedRowSt * crow = cursor->crow;
     if (!crow) return;    
     crow->setNumbered(!crow->isNumbered());
 }
 
 
-void MathedCursor::Interpret(char const *s)
+void MathedCursor::Interpret(char const * s)
 {
-    MathedInset *p = 0;
-    latexkeys *l = 0;   
+    MathedInset * p = 0;
+    latexkeys * l = 0;   
     MathedTextCodes tcode = LM_TC_INSET;
     
-    if (s[0]=='^' || s[0]=='_') {
+    if (s[0] == '^' || s[0] == '_') {
        char c = cursor->GetChar();
-       if (MathIsUp(c) && s[0]=='^' || MathIsDown(c) && s[0]=='_') {
+       if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
            Push();
            return;
        } else // A script may be followed by a script
@@ -612,18 +627,18 @@ void MathedCursor::Interpret(char const *s)
            cursor->ipush();
            cursor->Next();
            c = cursor->GetChar();
-           if (MathIsUp(c) && s[0]=='^' || MathIsDown(c) && s[0]=='_') {
+           if (MathIsUp(c) && s[0] == '^' || MathIsDown(c) && s[0] == '_') {
                Push();
                return;
            } else
              cursor->ipop();
        }
        p = new MathParInset(LM_ST_SCRIPT, "", LM_OT_SCRIPT);
-       Insert (p, (s[0]=='_') ? LM_TC_DOWN: LM_TC_UP); 
+       Insert (p, (s[0] == '_') ? LM_TC_DOWN: LM_TC_UP); 
        return;
     } else   
-      if (s[0]=='!' || s[0]==','  || s[0]==':' || s[0]==';') {
-         int sp = ((s[0]==',') ? 1:((s[0]==':') ? 2:((s[0]==';') ? 3: 0))); 
+      if (s[0] == '!' || s[0] == ','  || s[0] == ':' || s[0] == ';') {
+         int sp = ((s[0] == ',') ? 1:((s[0] == ':') ? 2:((s[0] == ';') ? 3: 0))); 
          p = new MathSpaceInset(sp);
          Insert(p);
          return;
@@ -633,16 +648,15 @@ void MathedCursor::Interpret(char const *s)
     if (!l) {       
        p = MathMacroTable::mathMTable.getMacro(s);
        if (!p) {
-           lyxerr.debug(LString("Macro2 ") + s + ' ' 
-                        + (long)tcode, Error::MATHED);
-           if (strcmp("root", s)==0) {
+           lyxerr[Debug::MATHED] << "Macro2 " << s << ' ' << tcode << endl;
+           if (strcmp("root", s) == 0) {
                p = new MathRootInset();
                tcode = LM_TC_ACTIVE_INSET;
            } else
              p = new MathFuncInset(s, LM_OT_UNDEF);
        } else {
-           tcode = ((MathMacro*)p)->getTCode();
-           fprintf(stderr, "Macro2 %s %d  ", s, tcode);
+           tcode = static_cast<MathMacro*>(p)->getTCode();
+           lyxerr << "Macro2 " << s << ' ' << tcode << "  " ;
        }
     } else {
        MathedInsetTypes fractype = LM_OT_FRAC;
@@ -655,7 +669,7 @@ void MathedCursor::Interpret(char const *s)
         case LM_TK_SYM:
         {                   
             if (l->id<255) {
-                Insert((byte)l->id, MathIsBOPS(l->id) ? 
+                Insert(static_cast<byte>(l->id), MathIsBOPS(l->id) ? 
                        LM_TC_BOPS: LM_TC_SYMB);            
             } else {
                 p = new MathFuncInset(l->name);
@@ -664,7 +678,7 @@ void MathedCursor::Interpret(char const *s)
         }  
         case LM_TK_STACK:
            fractype = LM_OT_STACKREL;
-           lyxerr.debug(" i:stackrel ", Error::MATHED);
+           lyxerr[Debug::MATHED] << " i:stackrel " << endl;
         case LM_TK_FRAC: 
         {       
             p = new MathFracInset(fractype);
@@ -703,9 +717,8 @@ void MathedCursor::Interpret(char const *s)
            break;
         case LM_TK_MACRO:
            p = MathMacroTable::mathMTable.getMacro(s);
-           tcode = ((MathMacro*)p)->getTCode();
-           lyxerr.debug(LString("Macro ") + s + ' ' 
-                        + (long)tcode, Error::MATHED);
+           tcode = static_cast<MathMacro*>(p)->getTCode();
+           lyxerr[Debug::MATHED] << "Macro " << s << ' ' << tcode << endl;
            break;
         default:
         {
@@ -724,11 +737,11 @@ void MathedCursor::Interpret(char const *s)
 bool MathedCursor::pullArg()
 { 
     if (cursor->IsActive()) {
-       MathParInset *p = cursor->GetActiveInset();
+       MathParInset * p = cursor->GetActiveInset();
        if (!p) { 
            return false;
        }
-       LyxArrayBase *a = p->GetData();
+       LyxArrayBase * a = p->GetData();
        p->SetData(0);
        Delete();
        if (a) {
@@ -751,15 +764,16 @@ void MathedCursor::MacroModeOpen()
       Insert (imacro);
       macro_mode = true;
    } else
-     fprintf(stderr, "Mathed Warning: Already in macro mode\n");
+          lyxerr << "Mathed Warning: Already in macro mode" << endl;
 }
 
+
 void MathedCursor::MacroModeClose()
 {
    if (macro_mode)  {
       macro_mode = false;
-      latexkeys *l = in_word_set(macrobf, macroln);
-      if (macroln>0 && (!l || (l && IsMacro(l->token, l->id))) && 
+      latexkeys * l = in_word_set(macrobf, macroln);
+      if (macroln > 0 && (!l || (l && IsMacro(l->token, l->id))) && 
          !MathMacroTable::mathMTable.getMacro(macrobf)) {
          if (!l) {
            imacro->SetName(strnew(macrobf));
@@ -769,19 +783,20 @@ void MathedCursor::MacroModeClose()
            imacro->SetName(l->name);
       } else {
          Left();
-        imacro->SetName(NULL);
-        if (cursor->GetInset()->GetType()==LM_OT_ACCENT) {
-            setAccent(((MathAccentInset*)cursor->GetInset())->getAccentCode());
+        imacro->SetName(0);
+        if (cursor->GetInset()->GetType() == LM_OT_ACCENT) {
+            setAccent(static_cast<MathAccentInset*>(cursor->GetInset())->getAccentCode());
         }
         cursor->Delete();
         if (l || MathMacroTable::mathMTable.getMacro(macrobf)) {
            Interpret(macrobf);
         }
       }
-      imacro = NULL;
+      imacro = 0;
    }  
 }
 
+
 void MathedCursor::MacroModeBack()
 {
    if (macro_mode) {
@@ -791,39 +806,40 @@ void MathedCursor::MacroModeBack()
      } else 
        MacroModeClose();
    } else
-     fprintf(stderr, "Mathed Warning: we are not in macro mode\n");
+          lyxerr << "Mathed Warning: we are not in macro mode" << endl;
 }
 
+
 void MathedCursor::MacroModeInsert(char c)
 {
    if (macro_mode) {
-      macrobf[macroln+1] = macrobf[macroln];
+      macrobf[macroln + 1] = macrobf[macroln];
       macrobf[macroln++] = c;
       imacro->Metrics();
    } else
-     fprintf(stderr, "Mathed Warning: we are not in macro mode\n");
+          lyxerr << "Mathed Warning: we are not in macro mode" << endl;
 }
 
+
 void MathedCursor::SelCopy()
 {
     if (selection) {
-       int p1, p2;
-       p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
-       p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
+       int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
+       int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
        selarray = cursor->Copy(p1, p2);
        cursor->Adjust();
        SelClear();
     }
 }
 
+
 void MathedCursor::SelCut()
 {   
     if (selection) {
-       if (cursor->pos==selpos) return;
+       if (cursor->pos == selpos) return;
        
-       int p1, p2;
-       p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
-       p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
+       int p1 = (cursor->pos < selpos) ? cursor->pos: selpos;
+       int p2 = (cursor->pos > selpos) ? cursor->pos: selpos;
        selarray = cursor->Copy(p1, p2);
        cursor->Clean(selpos);
        cursor->Adjust();
@@ -831,20 +847,22 @@ void MathedCursor::SelCut()
     }
 }
 
+
 void MathedCursor::SelDel()
 {
-//    fprintf(stderr, "Deleting sel ");
+//    lyxerr << "Deleting sel "
     if (selection) {    
-       if (cursor->pos==selpos) return;
+       if (cursor->pos == selpos) return;
        cursor->Clean(selpos);
        cursor->Adjust();
        SelClear();
     }
 }
 
+
 void MathedCursor::SelPaste()
 {
-//    fprintf(stderr, "paste %p %d ", selarray, cursor->pos);
+//    lyxerr << "paste " << selarray << " " << curor->pos;
     if (selection) SelDel();
     if (selarray) {
        cursor->Merge(selarray);
@@ -852,9 +870,10 @@ void MathedCursor::SelPaste()
     }
 }
 
+
 void MathedCursor::SelStart()
 {
-   lyxerr.debug("Starting sel ",Error::MATHED);
+       lyxerr[Debug::MATHED] << "Starting sel " << endl;
     if (!anchor) {
        selpos = cursor->pos;   
        selstk = new MathStackXIter(mathstk); 
@@ -867,9 +886,10 @@ void MathedCursor::SelStart()
     }
 }
 
+
 void MathedCursor::SelClear()
 {   
-   lyxerr.debug("Clearing sel ", Error::MATHED);
+       lyxerr[Debug::MATHED] << "Clearing sel " << endl;
     selection = false;
     delete selstk;
     selstk = 0;
@@ -885,13 +905,13 @@ void MathedCursor::SelBalance()
 
     // If unbalanced, balance them
     while (d != 0) {
-        if (d<0) {
-//            fprintf(stderr, "b[%d %d %d %d]", mathstk.Level(), selstk->Level(), anchor->GetX(), cursor->GetX());
+        if (d < 0) {
+//            lyxerr << "b[" << mathstk.Level() << " " << selstk->Level << " " << anchor->GetX() << " " << cursor->GetX() << "]";
             anchor = selstk->pop();
             if (anchor->GetX() >= cursor->GetX()) 
              anchor->Next();
         } else {
-//            fprintf(stderr, "a[%d %d]", mathstk.Level(), selstk->Level());
+//            lyxerr <<"a[" << mathstk.Level() << " " << selstk->Level() <<"]";
             Pop();
         }
         d = mathstk.Level() - selstk->Level();
@@ -902,68 +922,75 @@ void MathedCursor::SelBalance()
 } 
 
 
-XPoint *MathedCursor::SelGetArea(int& np)
+void MathedCursor::SelGetArea(int ** xp, int ** yp, int & np)
 {   
+    static int xpoint[10];
+    static int ypoint[10];
+
     if (!selection) {
        np = 0;
-       return 0;
+       xpoint[0] = 0;
+       ypoint[0] = 0;
+       *xp = &xpoint[0];
+       *yp = &ypoint[0];
+       return;
     }
     
-    static XPoint point[10];
-    
     // single row selection
-    int i = 0, x, y, a, d, w, xo, yo, x1, y1, a1, d1; //, p1, p2;
+    int i = 0, x, y, a, d, xo, yo, x1, y1, a1, d1;
 
     // Balance anchor and cursor
     SelBalance();
  
     cursor->p->GetXY(xo, yo);
-    w = cursor->p->Width();
+    int w = cursor->p->Width();
     cursor->GetPos(x1, y1);
     cursor->getAD(a1, d1);
     anchor->GetPos(x, y);
     anchor->getAD(a, d);
 
-    point[i].x = x;
-    point[i++].y = y+d;
-    point[i].x = x;
-    point[i++].y = y-a;
+    xpoint[i] = x;
+    ypoint[i++] = y + d;
+    xpoint[i] = x;
+    ypoint[i++] = y - a;
     
-    if (y!=y1) {
-       point[i].x = xo + w;
-       point[i++].y = y-a;
-       if (x1<xo+w) {
-           point[i].x = xo + w;
-           point[i++].y = y1-a;
-       }
+    if (y != y1) {
+           xpoint[i] = xo + w;
+           ypoint[i++] = y - a;
+
+           if (x1 < xo + w) {
+                   xpoint[i] = xo + w;
+                   ypoint[i++] = y1 - a;
+           }
     }
-       
-    point[i].x = x1;
-    point[i++].y = y1-a;
-    point[i].x = x1;
-    point[i++].y = y1+d;
+
+    xpoint[i] = x1;
+    ypoint[i++] = y1 - a;
+    xpoint[i] = x1;
+    ypoint[i++] = y1 + d;
     
-    if (y!=y1) {
-       point[i].x = xo;
-       point[i++].y = y1+d;
-       if (x>xo) {
-           point[i].x = xo;
-           point[i++].y = y+d;
-       }
+    if (y != y1) {
+           xpoint[i] = xo;
+           ypoint[i++] = y1 + d;
+           if (x > xo) {
+                   xpoint[i] = xo;
+                   ypoint[i++] = y + d;
+           }
     }
-    point[i].x = point[0].x;
-    point[i++].y = point[0].y;
+    xpoint[i] = xpoint[0];
+    ypoint[i++] = ypoint[0];
+
+    *xp = &xpoint[0];
+    *yp = &ypoint[0];
     np = i;
-//    fprintf(stderr, "AN[%d %d %d %d] ", x, y, x1, y1); 
-//    fprintf(stderr, "MT[%d %d %d %d] ", a, d, a1, d1);
-//    for (i=0; i<np; i++)
-//      fprintf(stderr, "XY[%d %d] ", point[i].x, point[i].y);
+//    lyxerr << "AN[" << x << " " << y << " " << x1 << " " << y1 << "] ";
+//    lyxerr << "MT[" << a << " " << d << " " << a1 << " " << d1 << "] ";
+//    for (i = 0; i < np; ++i)
+//      lyxerr << "XY[" << point[i].x << " " << point[i].y << "] ";
     
-    return &point[0];
 }
 
 
-
 void MathedCursor::setAccent(int ac)
 {
        if (ac > 0 && accent < 8) {
@@ -975,16 +1002,16 @@ void MathedCursor::setAccent(int ac)
  
 int MathedCursor::getAccent() const
 {
-       return (accent>0) ? nestaccent[accent-1]: 0;
+       return (accent > 0) ? nestaccent[accent - 1]: 0;
 }
 
 
 void MathedCursor::doAccent(byte c, MathedTextCodes t)
 {
-       MathedInset *ac = 0;
+       MathedInset * ac = 0;
        
-       for (int i=accent-1; i>=0; i--) {
-               if (i==accent-1)
+       for (int i = accent - 1; i >= 0; --i) {
+               if (i == accent - 1)
                  ac = new MathAccentInset(c, t, nestaccent[i]);
                else 
                  ac = new MathAccentInset(ac, nestaccent[i]);
@@ -996,12 +1023,12 @@ void MathedCursor::doAccent(byte c, MathedTextCodes t)
 }
 
 
-void MathedCursor::doAccent(MathedInset *p)
+void MathedCursor::doAccent(MathedInset * p)
 {
-       MathedInset *ac = 0;
+       MathedInset * ac = 0;
        
-       for (int i=accent-1; i>=0; i--) {
-               if (i==accent-1)
+       for (int i = accent - 1; i >= 0; --i) {
+               if (i == accent - 1)
                  ac = new MathAccentInset(p, nestaccent[i]);
                else 
                  ac = new MathAccentInset(ac, nestaccent[i]);