synedit: improve synedit copy to clipboard error handling (more informative), simplify code (by Alexander S. Klenin, mantis #0011548)

git-svn-id: trunk@18106 -
This commit is contained in:
paul 2009-01-04 17:28:07 +00:00
parent e3b2307040
commit b24b71e52a

View File

@ -1296,95 +1296,86 @@ var
SLen: integer; SLen: integer;
Failed: boolean; Failed: boolean;
begin begin
if SText <> '' then begin if SText = '' then exit;
Failed := TRUE; // assume the worst. SLen := Length(SText);
SLen := Length(SText); {$IFDEF SYN_LAZARUS}
{$IFDEF SYN_LAZARUS} Clipboard.Clear;
try Clipboard.AsText:=SText;
Clipboard.Clear; if not Clipboard.HasFormat(CF_TEXT) then
Clipboard.AsText:=SText; raise ESynEditError.Create('Clipboard copy operation failed: HasFormat');
Failed:=not Clipboard.HasFormat(CF_TEXT); // Copy it in our custom format so we know what kind of block it is.
except // That effects how it is pasted in.
BufSize:=SLen+SizeOf(TSynSelectionMode)+1;
GetMem(Buf,BufSize);
if Buf = nil then
raise ESynEditError.Create('Clipboard copy operation failed: GetMem');
try
P:=PChar(Buf);
// Our format: TSynSelectionMode value followed by text.
PSynSelectionMode(P)^ := SelectionMode;
inc(P, SizeOf(TSynSelectionMode));
if SLen>0 then begin
Move(SText[1], P^, SLen);
inc(P,SLen);
end; end;
if not Failed then begin P[0]:=#0;
Failed:=true; if not Clipboard.AddFormat(SynEditClipboardFormat,Buf^,BufSize) then
// Copy it in our custom format so we know what kind of block it is. raise ESynEditError.Create('Clipboard copy operation failed: AddFormat');
// That effects how it is pasted in. finally
BufSize:=SLen+SizeOf(TSynSelectionMode)+1; FreeMem(Buf);
GetMem(Buf,BufSize); end;
if Buf<>nil then {$ELSE}
Failed := true; // assume the worst.
// Open and Close are the only TClipboard methods we use because TClipboard
// is very hard (impossible) to work with if you want to put more than one
// format on it at a time.
Clipboard.Open;
try
// Clear anything already on the clipboard.
EmptyClipboard;
// Put it on the clipboard as normal text format so it can be pasted into
// things like notepad or Delphi.
Mem := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, SLen + 1);
if Mem <> 0 then begin
P := GlobalLock(Mem);
try try
P:=PChar(Buf); if P <> nil then begin
// Our format: TSynSelectionMode value followed by text. Move(PChar(SText)^, P^, SLen + 1);
PSynSelectionMode(P)^ := SelectionMode; // Put it on the clipboard in text format
inc(P, SizeOf(TSynSelectionMode)); Failed := SetClipboardData(CF_TEXT, Mem) = 0;
if SLen>0 then begin
Move(SText[1], P^, SLen);
inc(P,SLen);
end;
P[0]:=#0;
try
Failed:=not Clipboard.AddFormat(SynEditClipboardFormat,Buf^,BufSize);
except
end; end;
finally finally
FreeMem(Buf); GlobalUnlock(Mem);
end; end;
end; end;
if Failed then // Don't free Mem! It belongs to the clipboard now, and it will free it
raise ESynEditError.Create('Clipboard copy operation failed'); // when it is done with it.
{$ELSE} if not Failed then begin
// Open and Close are the only TClipboard methods we use because TClipboard // Copy it in our custom format so we know what kind of block it is.
// is very hard (impossible) to work with if you want to put more than one // That effects how it is pasted in.
// format on it at a time. Mem := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, SLen +
Clipboard.Open; SizeOf(TSynSelectionMode) + 1);
try P := GlobalLock(Mem);
// Clear anything already on the clipboard. try
EmptyClipboard; if P <> nil then begin
// Put it on the clipboard as normal text format so it can be pasted into // Our format: TSynSelectionMode value followed by text.
// things like notepad or Delphi. PSynSelectionMode(P)^ := SelectionMode;
Mem := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, SLen + 1); inc(P, SizeOf(TSynSelectionMode));
if Mem <> 0 then begin Move(PChar(SText)^, P^, SLen + 1);
P := GlobalLock(Mem); Failed := SetClipboardData(SynEditClipboardFormat, Mem) = 0;
try
if P <> nil then begin
Move(PChar(SText)^, P^, SLen + 1);
// Put it on the clipboard in text format
Failed := SetClipboardData(CF_TEXT, Mem) = 0;
end;
finally
GlobalUnlock(Mem);
end; end;
finally
GlobalUnlock(Mem);
end; end;
// Don't free Mem! It belongs to the clipboard now, and it will free it // Don't free Mem! It belongs to the clipboard now, and it will free it
// when it is done with it. // when it is done with it.
if not Failed then begin
// Copy it in our custom format so we know what kind of block it is.
// That effects how it is pasted in.
Mem := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, SLen +
SizeOf(TSynSelectionMode) + 1);
P := GlobalLock(Mem);
try
if P <> nil then begin
// Our format: TSynSelectionMode value followed by text.
PSynSelectionMode(P)^ := SelectionMode;
inc(P, SizeOf(TSynSelectionMode));
Move(PChar(SText)^, P^, SLen + 1);
Failed := SetClipboardData(SynEditClipboardFormat, Mem) = 0;
end;
finally
GlobalUnlock(Mem);
end;
// Don't free Mem! It belongs to the clipboard now, and it will free it
// when it is done with it.
end;
finally
Clipboard.Close;
if Failed then
raise ESynEditError.Create('Clipboard copy operation failed');
end; end;
{$ENDIF} finally
Clipboard.Close;
if Failed then
raise ESynEditError.Create('Clipboard copy operation failed');
end; end;
{$ENDIF}
end; end;
procedure TCustomSynEdit.CopyToClipboard; procedure TCustomSynEdit.CopyToClipboard;