LCL: Refactor ClipBoard HTML functions (patch by WP, modified by me):

- Remove property AsHtml since it was ambigous. It's better to use GetAsHtml and set paramters as required.
- Add PlainText paramater for SetAsHtml method, that also sets the ClipBoards pain text content.
- Clear the clipboard before adding Html to it, otherwise external applications will only ever see the first copy.

git-svn-id: trunk@50953 -
This commit is contained in:
bart 2015-12-20 10:53:23 +00:00
parent 4477d86b14
commit 4630073fc9
2 changed files with 27 additions and 21 deletions

View File

@ -163,8 +163,6 @@ type
function AssignToGraphic(Dest: TGraphic; FormatID: TClipboardFormat): boolean; function AssignToGraphic(Dest: TGraphic; FormatID: TClipboardFormat): boolean;
//procedure AssignToMetafile(Dest: TMetafile); //procedure AssignToMetafile(Dest: TMetafile);
procedure AssignToPicture(Dest: TPicture); procedure AssignToPicture(Dest: TPicture);
function DoGetAsHtml(ExtractFragmentOnly: Boolean): String;
function GetAsHtml: String;
function GetAsText: string; function GetAsText: string;
function GetFormatCount: Integer; function GetFormatCount: Integer;
function GetFormats(Index: Integer): TClipboardFormat; function GetFormats(Index: Integer): TClipboardFormat;
@ -173,7 +171,6 @@ type
CreateIfNotExists: boolean): integer; CreateIfNotExists: boolean): integer;
procedure InternalOnRequest(const RequestedFormatID: TClipboardFormat; procedure InternalOnRequest(const RequestedFormatID: TClipboardFormat;
AStream: TStream); AStream: TStream);
procedure SetAsHtml(const Html: String);
procedure SetAsText(const Value: string); procedure SetAsText(const Value: string);
function SetBuffer(FormatID: TClipboardFormat; function SetBuffer(FormatID: TClipboardFormat;
var Buffer; Size: Integer): Boolean; var Buffer; Size: Integer): Boolean;
@ -198,6 +195,7 @@ type
function FindPictureFormatID: TClipboardFormat; function FindPictureFormatID: TClipboardFormat;
function FindFormatID(const FormatName: string): TClipboardFormat; function FindFormatID(const FormatName: string): TClipboardFormat;
//function GetAsHandle(Format: integer): THandle; //function GetAsHandle(Format: integer): THandle;
function GetAsHtml(ExtractFragmentOnly: Boolean): String;
function GetComponent(Owner, Parent: TComponent): TComponent; function GetComponent(Owner, Parent: TComponent): TComponent;
procedure GetComponent(var RootComponent: TComponent; procedure GetComponent(var RootComponent: TComponent;
OnFindComponentClass: TFindComponentClassEvent; OnFindComponentClass: TFindComponentClassEvent;
@ -212,12 +210,12 @@ type
procedure SupportedFormats(var AFormatCount: integer; procedure SupportedFormats(var AFormatCount: integer;
var FormatList: PClipboardFormat); var FormatList: PClipboardFormat);
function GetTextBuf(Buffer: PChar; BufSize: Integer): Integer; function GetTextBuf(Buffer: PChar; BufSize: Integer): Integer;
function GetAsHtmlFragment: String;
function HasFormat(FormatID: TClipboardFormat): Boolean; function HasFormat(FormatID: TClipboardFormat): Boolean;
function HasFormatName(const FormatName: string): Boolean; function HasFormatName(const FormatName: string): Boolean;
function HasPictureFormat: boolean; function HasPictureFormat: boolean;
procedure Open; procedure Open;
//procedure SetAsHandle(Format: integer; Value: THandle); //procedure SetAsHandle(Format: integer; Value: THandle);
procedure SetAsHtml(const Html: String; const PlainText: String; {%H-}AddWindowsHeader: Boolean);
function SetComponent(Component: TComponent): Boolean; function SetComponent(Component: TComponent): Boolean;
function SetComponentAsText(Component: TComponent): Boolean; function SetComponentAsText(Component: TComponent): Boolean;
function SetFormat(FormatID: TClipboardFormat; Stream: TStream): Boolean; function SetFormat(FormatID: TClipboardFormat; Stream: TStream): Boolean;
@ -225,7 +223,6 @@ type
FormatList: PClipboardFormat): Boolean; FormatList: PClipboardFormat): Boolean;
procedure SetTextBuf(Buffer: PChar); procedure SetTextBuf(Buffer: PChar);
property AsText: string read GetAsText write SetAsText; property AsText: string read GetAsText write SetAsText;
property AsHtml: String read GetAsHtml write SetAsHtml;
property ClipboardType: TClipboardType read FClipboardType; property ClipboardType: TClipboardType read FClipboardType;
property FormatCount: Integer read GetFormatCount; property FormatCount: Integer read GetFormatCount;
property Formats[Index: Integer]: TClipboardFormat read GetFormats; property Formats[Index: Integer]: TClipboardFormat read GetFormats;
@ -256,6 +253,7 @@ var
{$I clipbrd.inc} {$I clipbrd.inc}
function RegisterClipboardFormat(const Format: string): TClipboardFormat; function RegisterClipboardFormat(const Format: string): TClipboardFormat;
begin begin
Result:=ClipboardRegisterFormat(Format); Result:=ClipboardRegisterFormat(Format);

View File

@ -774,8 +774,10 @@ end;
{ Retrieves html formatted text from the clipboard. If ExtractFragmentOnly is { Retrieves html formatted text from the clipboard. If ExtractFragmentOnly is
true then only the relevant html fragment is returned, the rest of the html true then only the relevant html fragment is returned, the rest of the html
string is dropped. This features exists only for Windows. } string is dropped. The Office applications in Windows and Linux write the
function TClipboard.DoGetAsHtml(ExtractFragmentOnly: Boolean): String; full html code which can be retrieved with ExtractFragmentOnly = false.
In case of Windows, the MS header is automatically removed.}
function TClipboard.GetAsHtml(ExtractFragmentOnly: Boolean): String;
var var
stream: TMemoryStream; stream: TMemoryStream;
bom: TBOM; bom: TBOM;
@ -846,32 +848,38 @@ begin
end; end;
end; end;
function TClipboard.GetAsHtml: String; { Adds html-formatted text to the clipboard. The main Office applications in
begin Windows and Linux require a valid and complete html text (i.e. with <html>
Result := DoGetAsHtml(false); and <body> tags).
end; In case of Windows, a specific header must be added (AddWindowsHeader = true),
otherwise the format will not be recognized by the clipboard. }
function TClipboard.GetAsHtmlFragment: String; procedure TClipboard.SetAsHtml(const Html: String; const PlainText: String; {%H-}AddWindowsHeader: Boolean);
begin
Result := DoGetAsHtml(true);
end;
{ Adds html-formatted text to the clipboard. It must be valid html.
In case of Windows, a specific header is added. }
procedure TClipboard.SetAsHtml(const Html: String);
var var
stream: TStream; stream: TStream;
begin begin
if CF_HTML = 0 then if CF_HTML = 0 then
exit; exit;
{$IFDEF WINDOWS} {$IFDEF WINDOWS}
stream := TStringStream.Create(InsertClipHeader(Html)); if AddWindowsHeader then
stream := TStringStream.Create(InsertClipHeader(Html)) else
stream := TStringStream.Create(Html);
{$ELSE} {$ELSE}
stream := TStringStream.Create(Html); stream := TStringStream.Create(Html);
{$ENDIF} {$ENDIF}
try try
//Clear the clipboard before adding Html to it,
//otherwise external applications will only ever see the first copy.
ClipBoard.Clear;
stream.Position := 0; stream.Position := 0;
Clipboard.AddFormat(CF_HTML, stream); Clipboard.AddFormat(CF_HTML, stream);
if (PlainText <> '') then
begin
stream.Size := 0;
stream.Position := 0;
stream.WriteAnsiString(PlainText);
stream.Position := 0;
ClipBoard.AddFormat(CF_TEXT, stream);
end;
finally finally
stream.Free; stream.Free;
end; end;