|
@@ -1,12 +1,17 @@
|
|
|
unit UClipboard;
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
-{ $DEFINE DEBUG_CLIPBOARD}
|
|
|
+{$DEFINE DEBUG_CLIPBOARD}
|
|
|
|
|
|
+{$DEFINE HTML_CLIPBOARD_FORMAT}
|
|
|
{$IFDEF DARWIN}
|
|
|
{$DEFINE TIFF_CLIPBOARD_FORMAT}
|
|
|
{$ELSE}
|
|
|
{$DEFINE BMP_CLIPBOARD_FORMAT}
|
|
|
+ {$DEFINE PNG_CLIPBOARD_FORMAT}
|
|
|
+{$ENDIF}
|
|
|
+{$IFDEF WINDOWS}
|
|
|
+ {$DEFINE PDN_CLIPBOARD_FORMAT}
|
|
|
{$ENDIF}
|
|
|
|
|
|
interface
|
|
@@ -20,8 +25,10 @@ function ClipboardContainsBitmap: boolean;
|
|
|
|
|
|
implementation
|
|
|
|
|
|
-uses Dialogs, BGRABitmapTypes, Clipbrd, Graphics, LCLIntf, LCLType,
|
|
|
- BGRADNetDeserial, math, GraphType, fphttpclient, FPWriteBMP;
|
|
|
+uses Dialogs, BGRABitmapTypes, Clipbrd, Graphics, LCLIntf, LCLType, GraphType
|
|
|
+ {$IFDEF PDN_CLIPBOARD_FORMAT}, math, BGRADNetDeserial{$ENDIF}
|
|
|
+ {$IFDEF BMP_CLIPBOARD_FORMAT}, FPWriteBMP{$ENDIF}
|
|
|
+ {$IFDEF HTML_CLIPBOARD_FORMAT}, fphttpclient{$ENDIF};
|
|
|
|
|
|
{$IFDEF DEBUG_CLIPBOARD}
|
|
|
const
|
|
@@ -33,9 +40,13 @@ const
|
|
|
var
|
|
|
tiffClipboardFormat: TClipboardFormat;
|
|
|
{$ENDIF}
|
|
|
-
|
|
|
+{$IFDEF PNG_CLIPBOARD_FORMAT}
|
|
|
+var
|
|
|
+ pngClipboardFormat: TClipboardFormat;
|
|
|
+{$ENDIF}
|
|
|
+{$IFDEF PDN_CLIPBOARD_FORMAT}
|
|
|
var
|
|
|
- bgraClipboardFormat: TClipboardFormat;
|
|
|
+ pdnClipboardFormat: TClipboardFormat;
|
|
|
|
|
|
function GetBitmapFromPaintDotNetMaskedSurface(deserial: TDotNetDeserialization): TBGRABitmap;
|
|
|
var width,height: integer;
|
|
@@ -123,6 +134,11 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
end;
|
|
|
+{$ENDIF}
|
|
|
+
|
|
|
+{$IFDEF HTML_CLIPBOARD_FORMAT}
|
|
|
+var
|
|
|
+ htmlClipboardFormat: TClipboardFormat;
|
|
|
|
|
|
function WideStringToStr(data: string): string;
|
|
|
var
|
|
@@ -332,38 +348,31 @@ begin
|
|
|
end;
|
|
|
tagTokens.Free;
|
|
|
end;
|
|
|
-
|
|
|
-function SafeClipboardFormatToMimeType(FormatID: TClipboardFormat): string;
|
|
|
-begin
|
|
|
- try
|
|
|
- result := ClipboardFormatToMimeType(FormatID);
|
|
|
- except
|
|
|
- on ex: Exception do
|
|
|
- result := '';
|
|
|
- end;
|
|
|
-end;
|
|
|
+{$ENDIF}
|
|
|
|
|
|
function GetBitmapFromClipboard: TBGRABitmap;
|
|
|
var i: integer;
|
|
|
Stream: TMemoryStream;
|
|
|
+ data: string;
|
|
|
|
|
|
{$IFDEF DEBUG_CLIPBOARD}
|
|
|
j: integer;
|
|
|
pcf: TPredefinedClipboardFormat;
|
|
|
- str: string;
|
|
|
+ mime, str: string;
|
|
|
|
|
|
c: char;
|
|
|
prevCok: boolean;
|
|
|
{$ENDIF}
|
|
|
|
|
|
+{$IFDEF PDN_CLIPBOARD_FORMAT}
|
|
|
deserial: TDotNetDeserialization;
|
|
|
- mime, data: string;
|
|
|
+{$ENDIF}
|
|
|
|
|
|
begin
|
|
|
result := nil;
|
|
|
|
|
|
{$IFDEF DEBUG_CLIPBOARD}
|
|
|
- str := '';
|
|
|
+ str := 'clipboard.FormatCount = '+inttostr(clipboard.FormatCount)+lineending;
|
|
|
for i := 0 to clipboard.FormatCount-1 do
|
|
|
begin
|
|
|
if str <> '' then str += ', ';
|
|
@@ -379,7 +388,7 @@ begin
|
|
|
mime := moreMimeTypes[j];
|
|
|
str += mime;
|
|
|
|
|
|
- stream := TMemoryStream.Create;
|
|
|
+{ stream := TMemoryStream.Create;
|
|
|
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
|
|
|
|
str += '('+inttostr(stream.Size)+' bytes)';
|
|
@@ -409,16 +418,14 @@ begin
|
|
|
end;
|
|
|
str += ']'+lineending;
|
|
|
end;
|
|
|
- stream.Free;
|
|
|
-
|
|
|
+ stream.Free; }
|
|
|
end;
|
|
|
ShowMessage(str);
|
|
|
{$ENDIF}
|
|
|
|
|
|
+ {$IFDEF PDN_CLIPBOARD_FORMAT}
|
|
|
for i := 0 to clipboard.FormatCount-1 do
|
|
|
- begin
|
|
|
- mime := SafeClipboardFormatToMimeType(Clipboard.Formats[i]);
|
|
|
- if mime = 'PaintDotNet.MaskedSurface' then
|
|
|
+ if Clipboard.Formats[i] = pdnClipboardFormat then
|
|
|
begin
|
|
|
Stream := TMemoryStream.Create;
|
|
|
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
@@ -432,47 +439,35 @@ begin
|
|
|
end;
|
|
|
deserial.Free;
|
|
|
if result <> nil then exit;
|
|
|
- end else
|
|
|
- if mime = 'text/html' then
|
|
|
- begin
|
|
|
- Stream := TMemoryStream.Create;
|
|
|
- Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
|
- if stream.Size > 65536 then
|
|
|
- setlength(data,65536) else
|
|
|
- setlength(data,stream.size);
|
|
|
- stream.Position:= 0;
|
|
|
- stream.read(data[1],length(data));
|
|
|
- Stream.Free;
|
|
|
- try
|
|
|
- result := GetBitmapFromHtml(data);
|
|
|
- except
|
|
|
- end;
|
|
|
- if result <> nil then exit;
|
|
|
end;
|
|
|
end;
|
|
|
+ {$ENDIF}
|
|
|
|
|
|
+ {$IFDEF TIFF_CLIPBOARD_FORMAT}
|
|
|
for i := 0 to clipboard.FormatCount-1 do
|
|
|
- if Clipboard.Formats[i] = bgraClipboardFormat then
|
|
|
+ if Clipboard.Formats[i] = tiffClipboardFormat then
|
|
|
begin
|
|
|
- Stream := TMemoryStream.Create;
|
|
|
- Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
|
- stream.Position := 0;
|
|
|
- result := TBGRABitmap.Create;
|
|
|
- try
|
|
|
- result.Deserialize(Stream);
|
|
|
- except
|
|
|
- on ex:exception do
|
|
|
- begin
|
|
|
- FreeAndNil(result);
|
|
|
- end;
|
|
|
- end;
|
|
|
- Stream.Free;
|
|
|
- if result <> nil then exit;
|
|
|
+ Stream := TMemoryStream.Create;
|
|
|
+ Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
|
+ Stream.Position := 0;
|
|
|
+ try
|
|
|
+ result := TBGRABitmap.Create;
|
|
|
+ result.LoadFromStream(Stream);
|
|
|
+ if result.Empty then result.AlphaFill(255);
|
|
|
+ except
|
|
|
+ on ex:exception do
|
|
|
+ begin
|
|
|
+ result := nil;
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ Stream.Free;
|
|
|
+ if result <> nil then exit;
|
|
|
end;
|
|
|
+ {$ENDIF}
|
|
|
|
|
|
- {$IFDEF TIFF_CLIPBOARD_FORMAT}
|
|
|
+ {$IFDEF PNG_CLIPBOARD_FORMAT}
|
|
|
for i := 0 to clipboard.FormatCount-1 do
|
|
|
- if Clipboard.Formats[i] = tiffClipboardFormat then
|
|
|
+ if Clipboard.Formats[i] = pngClipboardFormat then
|
|
|
begin
|
|
|
Stream := TMemoryStream.Create;
|
|
|
Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
@@ -492,6 +487,24 @@ begin
|
|
|
end;
|
|
|
{$ENDIF}
|
|
|
|
|
|
+ for i := 0 to clipboard.FormatCount-1 do
|
|
|
+ if Clipboard.Formats[i] = htmlClipboardFormat then
|
|
|
+ begin
|
|
|
+ Stream := TMemoryStream.Create;
|
|
|
+ Clipboard.GetFormat(Clipboard.Formats[i],Stream);
|
|
|
+ if stream.Size > 65536 then
|
|
|
+ setlength(data,65536) else
|
|
|
+ setlength(data,stream.size);
|
|
|
+ stream.Position:= 0;
|
|
|
+ stream.read(data[1],length(data));
|
|
|
+ Stream.Free;
|
|
|
+ try
|
|
|
+ result := GetBitmapFromHtml(data);
|
|
|
+ except
|
|
|
+ end;
|
|
|
+ if result <> nil then exit;
|
|
|
+ end;
|
|
|
+
|
|
|
for i := 0 to clipboard.FormatCount-1 do
|
|
|
if (Clipboard.Formats[i] = PredefinedClipboardFormat(pcfBitmap)) then
|
|
|
begin
|
|
@@ -540,26 +553,36 @@ begin
|
|
|
stream.Free;
|
|
|
{$ENDIF}
|
|
|
|
|
|
- stream := TMemoryStream.Create;
|
|
|
- bmp.Serialize(stream);
|
|
|
- Clipboard.AddFormat(bgraClipboardFormat, stream);
|
|
|
- stream.Free;
|
|
|
-
|
|
|
{$IFDEF TIFF_CLIPBOARD_FORMAT}
|
|
|
stream := TMemoryStream.Create;
|
|
|
bmp.SaveToStreamAs(stream, ifTiff);
|
|
|
Clipboard.AddFormat(tiffClipboardFormat, stream);
|
|
|
stream.Free;
|
|
|
{$ENDIF}
|
|
|
+
|
|
|
+ {$IFDEF PNG_CLIPBOARD_FORMAT}
|
|
|
+ stream := TMemoryStream.Create;
|
|
|
+ bmp.SaveToStreamAs(stream, ifPng);
|
|
|
+ Clipboard.AddFormat(pngClipboardFormat, stream);
|
|
|
+ stream.Free;
|
|
|
+ {$ENDIF}
|
|
|
end;
|
|
|
|
|
|
initialization
|
|
|
|
|
|
- bgraClipboardFormat := RegisterClipboardFormat('TBGRABitmap');
|
|
|
-
|
|
|
{$IFDEF TIFF_CLIPBOARD_FORMAT}
|
|
|
tiffClipboardFormat := RegisterClipboardFormat({$IFDEF DARWIN}'public.tiff'{$ELSE}'image/tiff'{$ENDIF});
|
|
|
{$ENDIF}
|
|
|
+{$IFDEF PNG_CLIPBOARD_FORMAT}
|
|
|
+ pngClipboardFormat := RegisterClipboardFormat({$IFDEF DARWIN}'public.png'{$ELSE}'image/png'{$ENDIF});
|
|
|
+{$ENDIF}
|
|
|
+{$IFDEF HTML_CLIPBOARD_FORMAT}
|
|
|
+ htmlClipboardFormat := RegisterClipboardFormat({$IFDEF DARWIN}'public.html'{$ELSE}'text/html'{$ENDIF});
|
|
|
+{$ENDIF}
|
|
|
+{$IFDEF PDN_CLIPBOARD_FORMAT}
|
|
|
+ pdnClipboardFormat := RegisterClipboardFormat('PaintDotNet.MaskedSurface');
|
|
|
+{$ENDIF}
|
|
|
+
|
|
|
|
|
|
end.
|
|
|
|