public void MakePreUE(Keys key) { preUe = new UndoElem(); preUe.InsertLength = 0; if (key == Keys.Delete) { preUe.IsDelete = true; } if (SelectedText == "") { if (key == Keys.Delete) { if (SelectionStart < Text.Length) { preUe.Pos = SelectionStart; preUe.UndoData = Text.Substring(SelectionStart, 1); } } else if (key == Keys.Back) { if (SelectionStart > 0) { preUe.Pos = SelectionStart - 1; preUe.UndoData = Text.Substring(SelectionStart - 1, 1); } } } else { preUe.Pos = SelectionStart; preUe.UndoData = SelectedText; } SelectionChangedSkip = true; }
public new void Redo() { if (!CanRedo) { return; } UndoElem ue = undoBuffer[undoBufferIndex++]; string newText = Text; if (ue.UndoData != null) { newText = newText.Remove(ue.Pos, ue.UndoData.Length); } if (ue.RedoData != null) { newText = newText.Insert(ue.Pos, ue.RedoData); } UndoSelectionStart = ue.Pos + (ue.RedoData != null ? ue.RedoData.Length : 0); undoState = UndoState.Redo; EnableRedrawLock(true); Text = newText; undoState = UndoState.None; BeginInvoke((MethodInvoker) delegate() { EnableRedrawLock(false); SelectionStart = UndoSelectionStart; }); }
protected override void WndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case DecUBUpdatingMessage: if (undoBufferUpdatingCounter > 0) { --undoBufferUpdatingCounter; } if (undoBufferUpdatingCounter == 0) { preUe = null; SelectionChangedSkip = false; } break; case DelayedUpdatingMessage: PrevSelectionStart = DelayedPrevSelectionStart; PrevSelectedText = DelayedPrevSelectedText; PrevPosition = DelayedPrevPosition; break; case WM_IME_COMPOSITION: int hImc = ImmGetContext(Handle.ToInt32()); if ((m.LParam.ToInt32() & GCS_RESULTSTR) != 0) { //StringBuilder sb = new StringBuilder(512); //ImmGetCompositionString(hImc, GCS_RESULTSTR, sb, 512); imeSelectionStart = -1; } if ((m.LParam.ToInt32() & GCS_COMPSTR) != 0) { //StringBuilder sb = new StringBuilder(512); //ImmGetCompositionString(hImc, GCS_COMPSTR, sb, 512); imeSelectionStart = PrevSelectionStart; } if (m.LParam.ToInt32() == 0) { imeSelectionStart = -1; } ImmReleaseContext(Handle.ToInt32(), hImc); break; case ReraiseEventMessage: doingReraiseEvent = true; foreach (MethodBase mb in new List <MethodBase>(reraiseEventDic.Keys)) { reraiseEventDic[mb](); reraiseEventDic.Remove(mb); } doingReraiseEvent = false; break; } base.WndProc(ref m); }
protected override void OnTextChanged(EventArgs e) { if (TextChangedSkip || EventSkip || scrollingPos) { return; } if (undoState == UndoState.None && undoBufferUpdatingCounter > 0 && Text != PrevText) { UndoElem ue; if (preUe != null) { ue = preUe; } else { ue = new UndoElem(); ue.Pos = PrevSelectionStart; if (PrevSelectedText == "") { ue.InsertLength = Text.Length - PrevText.Length; } else { ue.InsertLength = Text.Length - (PrevText.Length - PrevSelectedText.Length); ue.UndoData = PrevSelectedText; } } if (undoBufferIndex < undoBuffer.Count) { undoBuffer.RemoveRange(undoBufferIndex, undoBuffer.Count - undoBufferIndex); } undoBuffer.Add(ue); ++undoBufferIndex; preUe = null; SelectionChangedSkip = false; --undoBufferUpdatingCounter; } base.OnTextChanged(e); }
public new void Undo() { if (!CanUndo) { return; } UndoElem ue = undoBuffer[--undoBufferIndex]; string newText = Text; if (ue.InsertLength > 0) { if (ue.RedoData == null) { //if (newText.Length < ue.Pos + ue.InsertLength) //{ // while (newText.Length < ue.Pos + ue.InsertLength) // --ue.Pos; //} ue.RedoData = newText.Substring(ue.Pos, ue.InsertLength); } newText = newText.Remove(ue.Pos, ue.InsertLength); } if (ue.UndoData != null) { newText = newText.Insert(ue.Pos, ue.UndoData); } UndoSelectionStart = ue.Pos + (!ue.IsDelete && ue.UndoData != null ? ue.UndoData.Length : 0); undoState = UndoState.Undo; EnableRedrawLock(true); Text = newText; undoState = UndoState.None; BeginInvoke((MethodInvoker) delegate() { EnableRedrawLock(false); SelectionStart = UndoSelectionStart; }); }
public void UpdateBreakPointLineDic(Dictionary <int, bool> bpLineDic) { if (undoBufferIndex == 0 && undoState != UndoState.Undo) { return; } try { UndoElem ue = null; int moveLine = 0; string deletedStr = null; if (undoState == UndoState.None) { ue = undoBuffer[undoBufferIndex - 1]; deletedStr = ue.UndoData; if (ue.UndoData != null) { moveLine -= ue.UndoData.Length - ue.UndoData.Replace("\n", "").Length; } if (ue.InsertLength > 0) { string insertStr = Text.Substring(ue.Pos, ue.InsertLength); moveLine += insertStr.Length - insertStr.Replace("\n", "").Length; } } else { if (undoState == UndoState.Undo) { ue = undoBuffer[undoBufferIndex]; deletedStr = ue.RedoData; if (ue.UndoData != null) { moveLine += ue.UndoData.Length - ue.UndoData.Replace("\n", "").Length; } if (ue.RedoData != null) { moveLine -= ue.RedoData.Length - ue.RedoData.Replace("\n", "").Length; } } else if (undoState == UndoState.Redo) { ue = undoBuffer[undoBufferIndex - 1]; deletedStr = ue.UndoData; if (ue.UndoData != null) { moveLine -= ue.UndoData.Length - ue.UndoData.Replace("\n", "").Length; } if (ue.RedoData != null) { moveLine += ue.RedoData.Length - ue.RedoData.Replace("\n", "").Length; } } } if (moveLine == 0) { return; } int orgOffset = deletedStr == null ? 0 : deletedStr.Length - deletedStr.Replace("\n", "").Length; int startLine = GetLineFromCharIndex(ue.Pos) + 1; int firstCharIndex = GetFirstCharIndexFromLine(startLine - 1); string preLineStr = Text.Substring(firstCharIndex, ue.Pos - firstCharIndex); //startOffset = preLineStr == "" ? 0 : 1; int startOffset = Regex.IsMatch(preLineStr, @"^[ \t]*$") ? 0 : 1; if (moveLine > 0) { // 空行のときはoffsetを0から1にする //if (startOffset == 0 && undoState != UndoState.Undo) // startOffset = Regex.IsMatch(Text.Substring(ue.Pos + ue.InsertLength), @"^[ \t]*(?:[\r\n]|$)") ? 1 : 0; } if (deletedStr != null) { int endLine = startLine + (deletedStr.Length - deletedStr.Replace("\n", "").Length); int endPos = ue.Pos;// +ue.InsertLength; int nextTermFromEndPos = Text.Substring(endPos).IndexOf('\n'); nextTermFromEndPos += nextTermFromEndPos == -1 ? Text.Length + 1 : endPos; string postLineStr = Text.Substring(endPos, nextTermFromEndPos - endPos); int endOffset = preLineStr != "" ? 1 : Regex.IsMatch(postLineStr, @"^[ \t]*$") ? 1 : 0; //if (startOffset == 1) // startOffset = Regex.IsMatch("", "") ? 0 : 1; for (int i = startLine + startOffset; i < endLine + endOffset; ++i) { if (bpLineDic.ContainsKey(i)) { bpLineDic.Remove(i); } } } List <int> lineList = new List <int>(bpLineDic.Keys); lineList.Sort(delegate(int a, int b) { return(Math.Sign(moveLine) * (a == b ? 0 : a < b ? 1 : -1)); }); foreach (int line in lineList) { if (line >= startLine + startOffset) { bpLineDic.Remove(line); bpLineDic[line + moveLine] = true; } } } catch { 0.ToString(); } }
public void DecrUserAction() { if (userActionDepth > 0) { --userActionDepth; } if (userActionDepth == 0 && userAction != null) { UndoStack.Push(userAction); userAction = null; } }
private void AddUndoElem(UndoElem v) { if (userAction != null) { if (userAction.MergeWith(v)) { return; } else { UndoStack.Push(userAction); userAction = null; } } if (userActionDepth > 0) { userAction = v; } else { UndoStack.Push(v); } }