ecere/gui/EditBox: Fixed selection menu items after undo
[sdk] / ecere / src / gui / controls / EditBox.ec
index 5755d61..c9408fe 100644 (file)
@@ -11,6 +11,16 @@ import "FindDialog"
 import "GoToDialog"
 import "Array"
 
+char * strchrmax(const char * s, int c, int max)
+{
+   int i;
+   char ch;
+   for(i = 0; i < max && (ch = s[i]); i++)
+      if(ch == c)
+         return (char *)s + i;
+   return null;
+}
+
 public class SyntaxColorScheme
 {
 public:
@@ -528,7 +538,7 @@ public class EditLine : struct
    int length;
    EditBox editBox;
 public:
-   property char * text
+   property const char * text
    {
       set
       {
@@ -658,7 +668,7 @@ public struct BufferLocation
 
 public enum EditBoxFindResult { notFound, found, wrapped };
 
-static char * keyWords1[] =
+static const char * keyWords1[] =
 {
    // C
    "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
@@ -696,12 +706,14 @@ static char * keyWords1[] =
    null
 };
 
-static char * keyWords2[] =
+static const char * keyWords2[] =
 {
-   "defined", "warning", null
+   "defined", "warning",
+   "include", "pragma", "elif", "ifdef", "ifndef", "endif", "undef", "line",
+   null
 };
 
-static char ** keyWords[] = { keyWords1, keyWords2 };
+static const char ** keyWords[] = { keyWords1, keyWords2 };
 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
 //static int * keyLen[NUM_KEYWORD_GROUPS];
 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
@@ -794,7 +806,7 @@ public:
    property EditLine firstLine { get { return lines.first; } };      // Change these to a List<EditLine>... (this.lines[10].text)
    property EditLine lastLine  { get { return lines.last; } };
    property EditLine line { get { return this.line; } }; // TODO: Add Set   this.line = this.lines[10]
-   property char * contents
+   property const char * contents
    {
       property_category $"Data"
       set
@@ -886,7 +898,7 @@ public:
       return null;
    }
 
-   void SetLineText(char * text)
+   void SetLineText(const char * text)
    {
       if(this)
       {
@@ -963,7 +975,7 @@ private:
 
    bool modified;
 
-   void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
+   void (* FontExtent)(Display display, Font font, const char * text, int len, int * width, int * height);
 
    Color backColor;
    bool rightButtonDown;
@@ -1130,7 +1142,7 @@ private:
             void NotifyDestroyed(Window window, DialogResult result)
             {
                ReplaceDialog dialog = (ReplaceDialog)window;
-               char * replace = dialog.replaceString;
+               const char * replace = dialog.replaceString;
                if(replace)
                   strcpy(replaceString, replace);
                strcpy(searchString, dialog.searchString);
@@ -1198,7 +1210,7 @@ private:
 
       FontExtent = Display::FontExtent;
       font = fontObject;
-      lines.offset = (uint)&((EditLine)0).prev;
+      lines.offset = (uint)(uintptr)&((EditLine)0).prev;
 
       style = EditBoxBits { hScroll = true };
 
@@ -1283,8 +1295,8 @@ private:
       }
    }
 
-   int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
-                   Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
+   bool CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
+                    Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, int *overwrite)
    {
       bool flush = false;
 
@@ -1308,7 +1320,7 @@ private:
          if((style.stuckCaret && wc == line.count && !line.next) ||
             (!mouseMove && line == this.line && wc == editX))
          {
-            *overwrite = true;
+            *overwrite = 1;
             flush = true;
          }
       }
@@ -1480,9 +1492,9 @@ private:
       bool opacity;
 
       // Overwrite Caret Stuff
-      bool overWrite = false;
-      int overWriteX, overWriteY;
-      byte overWriteCh;
+      int overWrite = 0;
+      int overWriteX = 0, overWriteY = 0;
+      char overWriteCh;
 
       // ****** SYNTAX STATES ******
       bool inMultiLineComment = style.inMultiLineComment;
@@ -1775,26 +1787,29 @@ private:
                            if(!wasEscaped)
                               escaped = true;
                         }
-                        else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
+                        else if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
                         {
-                           char * dot = strchr(word, '.');
-                           bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
-                           char * exponent;
-                           bool isReal;
+                           char * dot = word[wordLen] == '.' ? word + wordLen : (word[0] == '.' && (word == line.buffer || word[-1] == '-' || isspace(word[-1])) ? word : null);
+                           bool isReal = dot != null;
                            char * s = null;
-                           if(isHex)
-                           {
-                              exponent = strchr(word, 'p');
-                              if(!exponent) exponent = strchr(word, 'P');
-                           }
+                           if(dot)
+                              isReal = true;
                            else
                            {
-                              exponent = strchr(word, 'e');
-                              if(!exponent) exponent = strchr(word, 'E');
+                              char * exponent;
+                              bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
+                              if(isHex)
+                              {
+                                 exponent = strchrmax(word, 'p', wordLen);
+                                 if(!exponent) exponent = strchrmax(word, 'P', wordLen);
+                              }
+                              else
+                              {
+                                 exponent = strchrmax(word, 'e', wordLen);
+                                 if(!exponent) exponent = strchrmax(word, 'E', wordLen);
+                              }
+                              isReal = exponent != null;
                            }
-                           if(exponent && exponent > word + wordLen) exponent = null;
-                           if(dot && dot > word + wordLen) dot = null;
-                           isReal = dot || exponent;
                            if(isReal)
                               strtod(word, &s);      // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
                            else
@@ -1856,14 +1871,14 @@ private:
                               if(firstWord)
                               {
                                  inPrep = true;
-                                 newTextColor = colorScheme.preprocessorColor;
+                                 newTextColor = wordLen == 1 ? colorScheme.keywordColors[1] : colorScheme.preprocessorColor;
                               }
                            }
-                           if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
+                           if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
                            {
                               for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
                               {
-                                 char ** keys = keyWords[g];
+                                 const char ** keys = keyWords[g];
                                  int * len = keyLen[g];
                                  for(ccc = 0; keys[ccc]; ccc++)
                                  {
@@ -1954,7 +1969,7 @@ private:
                {
                   flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
                      selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
-                  if(overWrite == true)
+                  if(overWrite == 1)
                   {
                      overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
                      if(overWriteCh == '\t') overWriteCh = ' ';
@@ -1965,7 +1980,7 @@ private:
                      flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
                      if(flagTrailingSpace) surface.SetBackground(red);
                      FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
-                     if(overWrite == true)
+                     if(overWrite == 1)
                      {
                         overWriteX = x;
                         overWriteY = y;
@@ -2002,7 +2017,7 @@ private:
          if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
                         selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
          {
-            if(overWrite == true)
+            if(overWrite == 1)
             {
                overWriteX = x;
                overWriteY = y;
@@ -2085,8 +2100,6 @@ private:
    void ComputeLength(EditLine line)
    {
       int c;
-      int tabOccur = 0;
-      int tabWidth;
       int x = 0;
 
       for(c = 0; c < line.count; )
@@ -2468,7 +2481,7 @@ private:
       return false;
    }
 
-   bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
+   bool AddToLine(const char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
    {
       bool hadComment = false;
       // Add the line here
@@ -2494,7 +2507,7 @@ private:
          {
             int w;
             int numBytes = 1;
-            char * string;
+            const char * string;
             if(c < Min(this.x, line.count))
                string = line.buffer + c;
             else if(c < endX)
@@ -2692,9 +2705,8 @@ private:
    // Returns true if it needs scrolling
    bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
    {
-      int w;
       int c;
-      int x, y;
+      int y;
       EditLine line;
       bool needHScroll = false;
 
@@ -2957,7 +2969,6 @@ private:
       while(true)
       {
          int start = c;
-         int numBytes = 1;
          int len = 1;
          int w;
          if(c < Min(max, line.count))
@@ -3102,7 +3113,7 @@ private:
    }
 
    /*
-   bool SaveFile(char * fileName)
+   bool SaveFile(const char * fileName)
    {
       File f = eFile_Open(fileName, FO_WRITE);
       if(f)
@@ -3597,7 +3608,7 @@ private:
                int y;
                bool done = false;
                EditLine line = this.line;
-               int c;
+               int c = 0;
                for(y = this.y; y>= 0; y--)
                {
                   c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
@@ -3678,7 +3689,6 @@ private:
                      if(key.ctrl)
                      {
                         int i;
-                        int length;
                         char * buffer = line1.buffer;
                         for(i = x1; i < line1.count; i++)
                         {
@@ -3758,12 +3768,13 @@ private:
                   stuffAfter = true;
 
                //If last character is a { indent one tab
-               if(this.line.buffer[this.x - 1] == '{')
+               c = Min(x, line.count);
+               if(c > 0 && line.buffer[c - 1] == '{')
                {
                   //Except if the next non space character is a }
                   bool indent = false;
                   int i;
-                  for(i = this.x; i < this.line.size; i++)
+                  for(i = c; i <= this.line.count; i++)   // indent will be set to true on nul terminating char
                      if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
                      {
                         if(this.line.buffer[i] != '}')
@@ -3845,8 +3856,8 @@ private:
                bool foundAlpha = false;
                bool found = false;
                int y = this.y;
-               EditLine line, lastLine;
-               int lastC, lastY;
+               EditLine line, lastLine = null;
+               int lastC = 0, lastY = 0;
 
                for(line = this.line; (line && !found); line = line.prev, y--)
                {
@@ -3884,7 +3895,7 @@ private:
                            break;
                         }
                      }
-                     while(--c)
+                     while(--c >= 0)
                      {
                         byte ch = line.buffer[c];
                         if(UTF8_IS_FIRST(ch)) break;
@@ -3917,7 +3928,7 @@ private:
                {
                   if(x <= line.count)
                   {
-                     byte * buffer = line.buffer;
+                     byte * buffer = (byte *)line.buffer;
                      while(--x)
                      {
                         byte ch = buffer[x];
@@ -3967,9 +3978,9 @@ private:
                {
                   bool foundAlpha = false;
                   bool found = false;
-                  EditLine line, lastLine;
+                  EditLine line = null, lastLine = null;
                   int y = this.y;
-                  int lastC, lastY, lastNumBytes;
+                  int lastC = 0, lastY = 0, lastNumBytes = 0;
 
                   for(line = this.line; (line && !found); line = line.next, y++)
                   {
@@ -4063,7 +4074,7 @@ private:
                {
                   if(x < line.count)
                   {
-                     byte * buffer = line.buffer;
+                     byte * buffer = (byte *)line.buffer;
                      while(++x)
                      {
                         byte ch = buffer[x];
@@ -4274,11 +4285,13 @@ private:
             {
                if(style.stuckCaret) break;
                {
+                  /*
                   int th = space.h;
                   int textPos = 0;
                   int sx = 0, sy = this.y * this.space.h;
                   int maxW = clientSize.w - sx;
                   char * text = line.buffer;
+                  */
 
                   if(!shift) SelDirty();
                   DirtyLine(this.y);
@@ -4809,12 +4822,12 @@ private:
                   {
                      //Only indent back if you are exactly at one tab.
                      {
-                        bool whitespace = true;
+                        //bool whitespace = true;
                         int i;
                         char * newline;
                         int putsize;
 
-                        int indentwidth;
+                        int indentwidth = 0;
                         EditLine line = this.line;
 
                         //Only remove one tab if there is nothing else on the line.
@@ -4971,7 +4984,7 @@ private:
    bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
    {
       EditLine line;
-      int length, endX;
+      int length, endX = 0;
       bool result;
       ReplaceTextAction replaceAction = null;
       AddCharAction addCharAction = null;
@@ -5167,6 +5180,11 @@ public:
       undoBuffer.Undo();
       itemEditUndo.disabled = undoBuffer.curAction == 0;
       itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
+
+      UpdateDirty();
+      SetSelectCursor();
+      SelectionEnables();
+
       if(savedAction == undoBuffer.curAction)
       {
          modifiedDocument = false;
@@ -5180,6 +5198,11 @@ public:
       undoBuffer.Redo();
       itemEditUndo.disabled = undoBuffer.curAction == 0;
       itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
+
+      UpdateDirty();
+      SetSelectCursor();
+      SelectionEnables();
+
       if(savedAction == undoBuffer.curAction)
       {
          modifiedDocument = false;
@@ -5232,12 +5255,12 @@ public:
    }
 
    // BASIC OUTPUT
-   bool AddS(char * string)
+   bool AddS(const char * string)
    {
       if(this)
       {
          bool ret = true;
-         char * line;
+         const char * line;
          int c, count;
          int addedSpaces = 0, addedTabs = 0;
          AddTextAction action = null;
@@ -5312,7 +5335,7 @@ public:
          {
             if(string[c] == '\n' || string[c] == '\r')
             {
-               if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
+               if(!AddToLine(line, count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
                {
                   ret = false;
                   break;
@@ -5484,7 +5507,7 @@ public:
       }
    }
 
-   void PutS(char * string)
+   void PutS(const char * string)
    {
       if(this)
       {
@@ -5494,7 +5517,7 @@ public:
       }
    }
 
-   void Printf(char * format, ...)
+   void Printf(const char * format, ...)
    {
       if(this)
       {
@@ -5508,7 +5531,7 @@ public:
       }
    }
 
-   void SetContents(char * format, ...)
+   void SetContents(const char * format, ...)
    {
       if(this)
       {
@@ -5657,8 +5680,7 @@ public:
    {
       if(created)
       {
-         int w;
-         int c, numLines;
+         int numLines;
          EditLine line;
          int x;
          int checkX, checkY;
@@ -5821,7 +5843,7 @@ public:
       }
       else
       {
-         EditLine oldLine = this.line;
+         //EditLine oldLine = this.line;
          bool lastOne = false;
          EditLine oldViewLine = this.viewLine;
          bool figureSyntax = false;
@@ -5869,8 +5891,6 @@ public:
       }
       else
       {
-         EditLine oldLine = this.line;
-
          for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
          {
             this.line = line;
@@ -6235,7 +6255,7 @@ public:
       itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
    }
 
-   EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
+   EditBoxFindResult Find(const char * text, bool matchWord, bool matchCase, bool isSearchDown)
    {
       EditLine line;
       int num;
@@ -6293,7 +6313,7 @@ public:
       return notFound;
    }
 
-   EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
+   EditBoxFindResult FindInSelection(const char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
    {
       EditLine line;
       int y;
@@ -6528,7 +6548,7 @@ public:
       return result;
    }
 
-   bool Puts(char * string)
+   bool Puts(const char * string)
    {
       EditBox editBox = this.editBox;
       BufferLocation start { editBox.line, editBox.y, editBox.x };
@@ -6558,7 +6578,7 @@ public:
       {
          utf8Bytes[numBytes++] = ch;
          utf8Bytes[numBytes] = 0;
-         if(UTF8Validate(utf8Bytes))
+         if(UTF8Validate((char *)utf8Bytes))
          {
             editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
             numBytes = 0;