Add code for TClipboardHelper.TryAsText, missing in previous commit

This commit is contained in:
Ansgar Becker
2023-10-10 17:23:46 +02:00
parent 86a8840cc0
commit 62f27dbf0a

View File

@ -157,6 +157,14 @@ type
class function CreateTable(SQL: String; SourceDb, TargetDb: TDBConnection): String; class function CreateTable(SQL: String; SourceDb, TargetDb: TDBConnection): String;
end; end;
TClipboardHelper = class helper for TClipboard
private
function GetTryAsText: String;
procedure SetTryAsText(AValue: String);
public
property TryAsText: String read GetTryAsText write SetTryAsText;
end;
TAppSettingDataType = (adInt, adBool, adString); TAppSettingDataType = (adInt, adBool, adString);
TAppSettingIndex = (asHiddenColumns, asFilter, asSort, asDisplayedColumnsSorted, asLastSessions, TAppSettingIndex = (asHiddenColumns, asFilter, asSort, asDisplayedColumnsSorted, asLastSessions,
asLastActiveSession, asAutoReconnect, asRestoreLastUsedDB, asLastUsedDB, asTreeBackground, asIgnoreDatabasePattern, asLogFileDdl, asLogFileDml, asLogFilePath, asLastActiveSession, asAutoReconnect, asRestoreLastUsedDB, asLastUsedDB, asTreeBackground, asIgnoreDatabasePattern, asLogFileDdl, asLogFileDml, asLogFilePath,
@ -408,7 +416,6 @@ type
procedure FindComponentInstances(BaseForm: TComponent; ClassType: TClass; var List: TObjectList); procedure FindComponentInstances(BaseForm: TComponent; ClassType: TClass; var List: TObjectList);
function WebColorStrToColorDef(WebColor: string; Default: TColor): TColor; function WebColorStrToColorDef(WebColor: string; Default: TColor): TColor;
function UserAgent(OwnerComponent: TComponent): String; function UserAgent(OwnerComponent: TComponent): String;
function GetClipboardAsText: String;
var var
AppSettings: TAppSettings; AppSettings: TAppSettings;
@ -1338,7 +1345,7 @@ begin
SetLength(TextContent, Text.Size); SetLength(TextContent, Text.Size);
Text.Position := 0; Text.Position := 0;
Text.Read(PAnsiChar(TextContent)^, Text.Size); Text.Read(PAnsiChar(TextContent)^, Text.Size);
Clipboard.AsText := Utf8ToString(TextContent); Clipboard.TryAsText := Utf8ToString(TextContent);
SetString(TextContent, nil, 0); SetString(TextContent, nil, 0);
end; end;
@ -3021,34 +3028,6 @@ begin
end; end;
function GetClipboardAsText: String;
var
AttemptsLeft: Integer;
Success: Boolean;
LastError: String;
begin
AttemptsLeft := 5;
Result := '';
Success := False;
while AttemptsLeft > 0 do begin
Dec(AttemptsLeft);
try
Result := Clipboard.AsText;
Success := True;
Break;
except
// We could also just catch EClipboardException
on E:Exception do begin
LastError := E.Message;
Sleep(100);
end;
end;
end;
if not Success then
MainForm.LogSQL(LastError, lcError);
end;
{ Get SID of current Windows user, probably useful in the future { Get SID of current Windows user, probably useful in the future
function GetCurrentUserSID: string; function GetCurrentUserSID: string;
type type
@ -3534,6 +3513,61 @@ begin
end; end;
{ TClipboardHelper }
function TClipboardHelper.GetTryAsText: String;
var
AttemptsLeft: Integer;
Success: Boolean;
LastError: String;
begin
AttemptsLeft := 5;
Result := '';
Success := False;
while AttemptsLeft > 0 do begin
Dec(AttemptsLeft);
try
Result := AsText;
Success := True;
Break;
except
// We could also just catch EClipboardException
on E:Exception do begin
LastError := E.Message;
Sleep(100);
end;
end;
end;
if not Success then
MainForm.LogSQL(LastError, lcError);
end;
procedure TClipboardHelper.SetTryAsText(AValue: String);
var
AttemptsLeft: Integer;
Success: Boolean;
LastError: String;
begin
AttemptsLeft := 5;
Success := False;
while AttemptsLeft > 0 do begin
Dec(AttemptsLeft);
try
AsText := AValue;
Success := True;
Break;
except
// We could also just catch EClipboardException
on E:Exception do begin
LastError := E.Message;
Sleep(100);
end;
end;
end;
if not Success then
MainForm.LogSQL(LastError, lcError);
end;
{ TAppSettings } { TAppSettings }