SynEdit, Block/Selection: Clean-up/Refactor

git-svn-id: trunk@17808 -
This commit is contained in:
martin 2008-12-12 00:04:24 +00:00
parent 46fdc833da
commit ec853639b4
2 changed files with 35 additions and 105 deletions

View File

@ -568,7 +568,9 @@ type
procedure SetScrollBars(const Value: TScrollStyle); procedure SetScrollBars(const Value: TScrollStyle);
function GetSelectionMode : TSynSelectionMode; function GetSelectionMode : TSynSelectionMode;
procedure SetSelectionMode(const Value: TSynSelectionMode); procedure SetSelectionMode(const Value: TSynSelectionMode);
{$IFNDEF SYN_LAZARUS}
procedure SetSelText(const Value: string); procedure SetSelText(const Value: string);
{$ENDIF}
procedure SetSelTextExternal(const Value: string); procedure SetSelTextExternal(const Value: string);
procedure SetTabWidth(Value: integer); procedure SetTabWidth(Value: integer);
procedure SynSetText(const Value: string); procedure SynSetText(const Value: string);
@ -660,7 +662,8 @@ type
procedure SetName(const Value: TComponentName); override; procedure SetName(const Value: TComponentName); override;
procedure SetReadOnly(Value: boolean); virtual; procedure SetReadOnly(Value: boolean); virtual;
procedure SetSelTextPrimitive(PasteMode: TSynSelectionMode; Value: PChar; procedure SetSelTextPrimitive(PasteMode: TSynSelectionMode; Value: PChar;
ATag: PInteger); ATag: PInteger; AddToUndoList: Boolean = false;
ChangeReason: TSynChangeReason = crInsert);
procedure ShowCaret; procedure ShowCaret;
// If the translations requires Data, memory will be allocated for it via a // If the translations requires Data, memory will be allocated for it via a
// GetMem call. The client must call FreeMem on Data if it is not NIL. // GetMem call. The client must call FreeMem on Data if it is not NIL.
@ -3959,8 +3962,6 @@ end;
procedure TCustomSynEdit.PasteFromClipboard; procedure TCustomSynEdit.PasteFromClipboard;
var var
StartOfBlock: TPoint;
EndOfBlock: TPoint;
{$IFDEF SYN_LAZARUS} {$IFDEF SYN_LAZARUS}
MemStream: TMemoryStream; MemStream: TMemoryStream;
Buf: Pointer; Buf: Pointer;
@ -3995,39 +3996,12 @@ begin
P := GlobalLock(Mem); P := GlobalLock(Mem);
if P <> nil then begin if P <> nil then begin
{$ENDIF} {$ENDIF}
if SelAvail then begin
fUndoList.AddChange(crDelete, FBlockSelection.StartLineBytePos,
FBlockSelection.EndLineBytePos, SelText,
SelectionMode);
end;
// Our format: SelectionMode value followed by text. // Our format: SelectionMode value followed by text.
// See CopyToClipboard // See CopyToClipboard
PasteMode := PSynSelectionMode(P)^; PasteMode := PSynSelectionMode(P)^;
inc(P, SizeOf(TSynSelectionMode)); inc(P, SizeOf(TSynSelectionMode));
if SelAvail then begin
StartOfBlock := FBlockSelection.FirstLineBytePos;
EndOfBlock := FBlockSelection.LastLineBytePos;
FBlockSelection.StartLineBytePos := StartOfBlock;
FBlockSelection.EndLineBytePos := EndOfBlock;
if SelectionMode = smLine then
// Pasting always occurs at column 0 when current selection is
// smLine type
StartOfBlock.X := 1;
end else
StartOfBlock := PhysicalToLogicalPos(CaretXY);
DummyTag := 0; DummyTag := 0;
SetSelTextPrimitive(PasteMode, P, @DummyTag); SetSelTextPrimitive(PasteMode, P, @DummyTag, true, crPaste);
EndOfBlock := BlockEnd;
if PasteMode <> smLine then
fUndoList.AddChange(crPaste, StartOfBlock, EndOfBlock, SelText,
PasteMode)
else
if CaretX = 1 then
fUndoList.AddChange(crPaste, Point(1, StartOfBlock.y),
Point(CharsInWindow, EndOfBlock.y - 1), SelText, smLine)
else
fUndoList.AddChange(crPaste, Point(1, StartOfBlock.y),
EndOfBlock, SelText, smNormal);
end else end else
raise ESynEditError.Create('Clipboard paste operation failed.'); raise ESynEditError.Create('Clipboard paste operation failed.');
finally finally
@ -4041,21 +4015,7 @@ begin
// If our special format isn't there, check for regular text format. // If our special format isn't there, check for regular text format.
end else if Clipboard.HasFormat(CF_TEXT) then begin end else if Clipboard.HasFormat(CF_TEXT) then begin
// Normal text is much easier... // Normal text is much easier...
if SelAvail then begin
// fUndoList.AddChange(crSelDelete, fBlockBegin, fBlockEnd, SelText,
// SelectionMode);
fUndoList.AddChange(crDelete, FBlockSelection.StartLineBytePos,
FBlockSelection.EndLineBytePos, SelText, //mh 2000-11-20
SelectionMode);
end;
StartOfBlock := FBlockSelection.FirstLineBytePos;
EndOfBlock := FBlockSelection.LastLineBytePos;
FBlockSelection.StartLineBytePos := StartOfBlock;
FBlockSelection.EndLineBytePos := EndOfBlock;
LockUndo;
SelText := Clipboard.AsText; SelText := Clipboard.AsText;
UnlockUndo;
fUndoList.AddChange(crPaste, StartOfBlock, BlockEnd, SelText, smNormal);
end; end;
finally finally
EndUndoBlock; //mh 2000-11-20 EndUndoBlock; //mh 2000-11-20
@ -4354,20 +4314,24 @@ begin
end; end;
end; end;
{$IFNDEF SYN_LAZARUS}
procedure TCustomSynEdit.SetSelText(const Value: string); procedure TCustomSynEdit.SetSelText(const Value: string);
begin begin
// No undo entry added // No undo entry added
SetSelTextPrimitive(smNormal, PChar(Value), nil); SetSelTextPrimitive(smNormal, PChar(Value), nil);
end; end;
{$ENDIF}
procedure TCustomSynEdit.SetSelTextPrimitive(PasteMode: TSynSelectionMode; procedure TCustomSynEdit.SetSelTextPrimitive(PasteMode: TSynSelectionMode;
Value: PChar; ATag: PInteger); Value: PChar; ATag: PInteger; AddToUndoList: Boolean = false;
ChangeReason: TSynChangeReason = crInsert);
Begin Begin
// No undo entry added // No undo entry added
IncPaintLock; IncPaintLock;
TSynEditStringTrimmingList(fTrimLines).Lock; TSynEditStringTrimmingList(fTrimLines).Lock;
try try
FBlockSelection.SetSelTextPrimitive(PasteMode, Value, ATag); FBlockSelection.SetSelTextPrimitive(PasteMode, Value, ATag,
AddToUndoList, ChangeReason);
// Force caret reset // Force caret reset
CaretXY := CaretXY; CaretXY := CaretXY;
fLastCaretX := CaretX; fLastCaretX := CaretX;
@ -6218,7 +6182,6 @@ begin
if Source <> Self then if Source <> Self then
TCustomSynEdit(Source).SelText := '' TCustomSynEdit(Source).SelText := ''
else begin else begin
// fUndoList.AddChange(crDragDropDelete, fBlockBegin, fBlockEnd,
SetSelTextExternal(''); SetSelTextExternal('');
// adjust horizontal drop position // adjust horizontal drop position
if DropAfter and (NewCaret.Y = BE.Y) then begin if DropAfter and (NewCaret.Y = BE.Y) then begin
@ -6240,34 +6203,14 @@ begin
Include(fOptions, eoScrollPastEol); Include(fOptions, eoScrollPastEol);
CaretXY := NewCaret; CaretXY := NewCaret;
BlockBegin := NewCaret; BlockBegin := NewCaret;
LockUndo; if Source = Self then
try SetSelTextPrimitive(smNormal, PChar(DragDropText), nil, true, crDragDropInsert)
SelText := DragDropText; else
finally SetSelTextPrimitive(smNormal, PChar(DragDropText), nil, true, crInsert);
UnlockUndo;
end;
finally finally
if ChangeScrollPastEOL then if ChangeScrollPastEOL then
Exclude(fOptions, eoScrollPastEol); Exclude(fOptions, eoScrollPastEol);
end; end;
// save undo information
if Source = Self then begin
fUndoList.AddChange(crDragDropInsert,
{$IFDEF SYN_LAZARUS}
PhysicalToLogicalPos(NewCaret),
{$ELSE}
NewCaret,
{$ENDIF}
BlockEnd, SelText, SelectionMode);
end else begin
fUndoList.AddChange(crInsert,
{$IFDEF SYN_LAZARUS}
PhysicalToLogicalPos(NewCaret),
{$ELSE}
NewCaret,
{$ENDIF}
BlockEnd, SelText, SelectionMode);
end;
BlockBegin := {$IFDEF SYN_LAZARUS}PhysicalToLogicalPos(NewCaret) BlockBegin := {$IFDEF SYN_LAZARUS}PhysicalToLogicalPos(NewCaret)
{$ELSE}NewCaret{$ENDIF}; {$ELSE}NewCaret{$ENDIF};
BlockEnd := {$IFDEF SYN_LAZARUS}PhysicalToLogicalPos(CaretXY) BlockEnd := {$IFDEF SYN_LAZARUS}PhysicalToLogicalPos(CaretXY)
@ -7111,18 +7054,9 @@ begin
OldSelMode := FBlockSelection.SelectionMode; OldSelMode := FBlockSelection.SelectionMode;
try try
FBlockSelection.SelectionMode := smNormal; FBlockSelection.SelectionMode := smNormal;
SetBlockBegin({$IFDEF SYN_LAZARUS}PhysicalToLogicalPos(CaretXY) SetBlockBegin(PhysicalToLogicalPos(CaretXY));
{$ELSE}CaretXY{$ENDIF}); SetBlockEnd(PhysicalToLogicalPos(WP));
SetBlockEnd({$IFDEF SYN_LAZARUS}PhysicalToLogicalPos(WP) SetSelTextPrimitive(smNormal, nil, nil, true, crSilentDeleteAfterCursor)
{$ELSE}WP{$ENDIF});
fUndoList.AddChange(crSilentDeleteAfterCursor,
{$IFDEF SYN_LAZARUS}
PhysicalToLogicalPos(CaretXY), PhysicalToLogicalPos(WP),
{$ELSE}
CaretXY, WP,
{$ENDIF}
SelText, smNormal);
SetSelText('');
finally finally
FBlockSelection.SelectionMode := OldSelMode; FBlockSelection.SelectionMode := OldSelMode;
end; end;
@ -7139,21 +7073,9 @@ begin
OldSelMode := FBlockSelection.SelectionMode; OldSelMode := FBlockSelection.SelectionMode;
try try
FBlockSelection.SelectionMode := smNormal; FBlockSelection.SelectionMode := smNormal;
{$IFDEF SYN_LAZARUS} SetBlockBegin(PhysicalToLogicalPos(WP));
SetBlockBegin(PhysicalToLogicalPos(CaretXY)); SetBlockEnd(PhysicalToLogicalPos(CaretXY));
SetBlockEnd(PhysicalToLogicalPos(WP)); SetSelTextPrimitive(smNormal, nil, nil, true, crSilentDelete)
{$ELSE}
SetBlockBegin(CaretXY);
SetBlockEnd(WP);
{$ENDIF}
fUndoList.AddChange(crSilentDelete,
{$IFDEF SYN_LAZARUS}
PhysicalToLogicalPos(WP), PhysicalToLogicalPos(CaretXY),
{$ELSE}
WP, CaretXY,
{$ENDIF}
SelText, smNormal);
SetSelText('');
finally finally
FBlockSelection.SelectionMode := OldSelMode; FBlockSelection.SelectionMode := OldSelMode;
end; end;

View File

@ -94,7 +94,8 @@ type
constructor Create(ALines: TSynEditStrings); constructor Create(ALines: TSynEditStrings);
//destructor Destroy; override; //destructor Destroy; override;
procedure SetSelTextPrimitive(PasteMode: TSynSelectionMode; Value: PChar; procedure SetSelTextPrimitive(PasteMode: TSynSelectionMode; Value: PChar;
ATag: PInteger; AddToUndoList: Boolean = false); ATag: PInteger; AddToUndoList: Boolean = false;
ChangeReason: TSynChangeReason = crInsert);
function SelAvail: Boolean; function SelAvail: Boolean;
function IsBackwardSel: Boolean; // SelStart < SelEnd ? function IsBackwardSel: Boolean; // SelStart < SelEnd ?
property Enabled: Boolean read FEnabled write SetEnabled; property Enabled: Boolean read FEnabled write SetEnabled;
@ -444,7 +445,8 @@ begin
end; end;
procedure TSynEditSelection.SetSelTextPrimitive(PasteMode : TSynSelectionMode; procedure TSynEditSelection.SetSelTextPrimitive(PasteMode : TSynSelectionMode;
Value : PChar; ATag : PInteger; AddToUndoList: Boolean = false); Value : PChar; ATag : PInteger; AddToUndoList: Boolean = false;
ChangeReason: TSynChangeReason = crInsert);
var var
BB, BE: TPoint; BB, BE: TPoint;
TempString: string; TempString: string;
@ -739,7 +741,7 @@ var
end; end;
var var
StartInsert: TPoint; StartInsert, EndInsert: TPoint;
begin begin
FLines.BeginUpdate; FLines.BeginUpdate;
try try
@ -762,10 +764,16 @@ begin
StartInsert := FCaret.LineBytePos; StartInsert := FCaret.LineBytePos;
InsertText; InsertText;
if AddToUndoList then begin if AddToUndoList then begin
if SelectionMode = smLine then EndInsert := FCaret.LineBytePos;
StartInsert.X := 1; if SelectionMode = smLine then begin // The SelectionMode of the deleted block
StartInsert.x := 1;
if EndInsert.x = 1 then begin
dec(EndInsert.y);
EndInsert.x := Length(FLines[EndInsert.y - 1]);
end;
end;
if length(Value) > 0 then if length(Value) > 0 then
fUndoList.AddChange(crInsert, StartInsert, FCaret.LineBytePos, '', smNormal); fUndoList.AddChange(ChangeReason, StartInsert, EndInsert, '', PasteMode);
end; end;
end; end;
finally finally