Skip to content

Commit

Permalink
ecere/gui/EditBox; ide: (#1065) Support for chaining undo events as one
Browse files Browse the repository at this point in the history
- Used to undo designer changes as one event
- Used for clearing trailing spaces as one event
- Used for EditBox text drag & drop (#1065)
- Used for avoiding to create trailing spaces
  • Loading branch information
jerstlouis committed Mar 26, 2014
1 parent e17fb2a commit 931660e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
61 changes: 43 additions & 18 deletions ecere/src/gui/controls/EditBox.ec
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public class UndoAction : struct
{
public:
subclass(UndoAction) type;
bool continued;

virtual void Undo(void * data) { type.Undo(this, data); }
virtual void Redo(void * data) { type.Redo(this, data); }
#ifdef _DEBUG
Expand All @@ -200,6 +202,8 @@ public:
void * data;
int dontRecord;
bool insideRedo;
bool recordAsOne;
bool firstEvent;

dontRecord = 0;

Expand All @@ -210,35 +214,43 @@ public:

void Undo()
{
dontRecord++;
if(curAction > 0)
bool continued = true;
while(curAction > 0 && continued)
{
UndoAction action = actions[--curAction];
dontRecord++;

#ifdef _DEBUG
/*Print("Undoing: ");
action.Print(data);*/
#endif
action.Undo(data);
dontRecord--;

continued = curAction > 0 && actions[curAction-1].continued;
}
dontRecord--;
}

void Redo()
{
dontRecord++;
insideRedo = true;
if(curAction < count)
bool continued = true;
while(curAction < count && continued)
{
UndoAction action = actions[curAction];
continued = action.continued;
dontRecord++;
insideRedo = true;

curAction++;
#ifdef _DEBUG
/*Print("Redoing: ");
action.Print(data);*/
#endif
action.Redo(data);

insideRedo = false;
dontRecord--;
}
insideRedo = false;
dontRecord--;
}

void Record(UndoAction action)
Expand All @@ -261,6 +273,12 @@ public:
/*Print("Recording: ");
action.Print(data);*/
#endif
if(recordAsOne)
{
if(!firstEvent && count > 0)
actions[count-1].continued = true;
firstEvent = false;
}
actions[count++] = action;
curAction = count;

Expand Down Expand Up @@ -878,6 +896,7 @@ public:
property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
property bool recordUndoEvent { set { undoBuffer.recordAsOne = value; undoBuffer.firstEvent = true; } get { return undoBuffer.recordAsOne; } };

// selectionStart.line, selectionStart.column (With Set)
// selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
Expand Down Expand Up @@ -3312,12 +3331,16 @@ private:
moveX = this.selX - this.x;
}
}

recordUndoEvent = true;
DelSel(null);
this.dropX -= moveX;
this.selX = this.x = this.dropX;
this.selY = this.y = this.dropY;
this.selLine = this.line = this.dropLine;
AddS(text);
recordUndoEvent = false;

SetViewToCursor(true);
delete text;
Modified();
Expand Down Expand Up @@ -3694,8 +3717,8 @@ private:
bool stuffAfter = false;
char * addString;
int len = 0;
bool resetX = false;
int backX;
/*bool resetX = false;
int backX;*/

if(style.stuckCaret) GoToEnd(true);
if(style.readOnly) break;
Expand All @@ -3712,14 +3735,14 @@ private:
}

// Prevent adding trailing spaces if at the head of a line
if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
/*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
{
position = 0;
backX = this.x;
this.x = 0;
this.selX = 0;
resetX = true;
}
}*/

if(!line.count)
position = x;
Expand Down Expand Up @@ -3765,9 +3788,10 @@ private:
}
addString[len] = '\0';
}
recordUndoEvent = true;
if(AddS(addString))
{
/*EditLine prevLine = this.line.prev;
EditLine prevLine = this.line.prev;
if(prevLine)
{
// Nuke spaces if that is all that is left on previous line
Expand All @@ -3778,13 +3802,13 @@ private:
break;
if(i == prevLine.count)
DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
}*/
if(resetX)
}
/*if(resetX)
{
this.x = this.selX = backX;
ComputeColumn();
}
else if(!stuffAfter && style.freeCaret)
else */if(!stuffAfter && style.freeCaret)
{
this.x = this.selX = position;
ComputeColumn();
Expand All @@ -3793,6 +3817,7 @@ private:
SetViewToCursor(true);
Modified();
}
recordUndoEvent = false;
delete addString;
return false;
}
Expand Down Expand Up @@ -5121,7 +5146,7 @@ public:
void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
{
Deselect();
DelCh(line1, y1, x1, line2, y2, x2, false);
_DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
SetViewToCursor(true);
UpdateDirty();
Modified();
Expand Down Expand Up @@ -6573,7 +6598,7 @@ public:
start.AdjustDelete(pos, end);
sel.AdjustDelete(pos, end);

editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);
editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);
}
}
};
6 changes: 6 additions & 0 deletions ide/src/designer/CodeEditor.ec
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,7 @@ class CodeEditor : Window
// Nuke trailing spaces
EditLine line;
int y = 0;
editBox.recordUndoEvent = true;
for(line = editBox.firstLine; line; line = line.next, y++)
{
String buffer = line.text;
Expand All @@ -1978,6 +1979,7 @@ class CodeEditor : Window
if(i < count - 1)
editBox.Delete(line, y, i + 1, line, y, count);
}
editBox.recordUndoEvent = false;
return true;
}
};
Expand Down Expand Up @@ -4552,6 +4554,8 @@ class CodeEditor : Window

updatingCode++;

editBox.recordUndoEvent = true;

if(moveAttached)
{
movedFuncId = GetDeclId(function.declarator);
Expand Down Expand Up @@ -4970,6 +4974,8 @@ class CodeEditor : Window
delete test;
}

editBox.recordUndoEvent = false;

updatingCode--;
delete f;

Expand Down

0 comments on commit 931660e

Please sign in to comment.