My FMX TClipboard and TMacPreferencesIniFile implementations now compiling in XE4

A bit belatedly, but my FireMonkey TClipboard and TMacPreferencesIniFile/TApplePreferencesIniFile (*) implementations are now compiling with XE4 (thanks goes to Ken Schafer for prodding me in the case of the former). See my post from nearly a year ago for the details:

https://delphihaven.wordpress.com/2012/07/27/fmx-tclipboard-and-tmacpreferencesinifile/

With respect to TClipboard, I’ve also done the following:

  • Added a cfPNG TClipboardFormat identifier (this is the same as cfBitmap on OS X).
  • Fixed a bad assumption about bitmap pitches on Windows that was causing issues for some people.
  • Switched to using CF_DIBV5 internally when reading or writing bitmaps on Windows.
  • When assigning to a bitmap, cfPNG is now looked for first (this makes things works better with MS Word), and correspondingly, HasFormat(cfBitmap) now also checks for cfPNG as a special case.
  • On Windows again, when a source bitmap includes transparency, the outputted DIB has this transparency removed, however at the same time a PNG representation is added that maintains the original alpha channel.
  • For greater flexibility (e.g. when there is only PDF data on the clipboard), the Mac implementation may now use NSImage as an intermediary when assigning to a TBitmap.
  • For both Windows and OS X, added GetFormats and GetFormatName methods. Where the former returns an array of TClipboardFormat, the latter converts a TClipboardFormat to a string:
procedure TfrmClipboardDemo.btnListClick(Sender: TObject);
var
  Format: TClipboardFormat;
  S: string;
begin
  for Format in Clipboard.GetFormats do
    S := S + sLineBreak + Clipboard.GetFormatName(Format);
  if S = '' then
    S := 'Nothing is currently on the clipboard.'
  else
    S := 'The following formats are on the clipboard:' + S;
  MessageDlg(S, TMsgDlgType.mtInformation, [TMsgDlgBtn.mbOK], 0)
end;

This is the result when I copy a word on a webpage in Safari:

One word copied from Safari

As this example hints at, it’s better to use Apple’s ‘UTI’ format for custom clipboard identifiers on OS X (e.g. com.mycompany.formatname, though ultimately just something.something, or something.something.something, etc.). While things still work if you don’t, failing to do so will lead the OS to allocate a parallel ‘dynamic’ UTI for you (e.g. dyn.a1oad0fg1bber13hthat600s0nand0nand0n). As such, I’ve tweaked the demo accordingly – click the ‘Copy as Custom Clipboard Format’ button then the ‘List’ one, and you get this:

Custom format list

(*) Update (17/9/13): as the code actually worked for iOS too, I’ve renamed the class to TApplePreferencesIniFile and its unit to CCR.PrefsIniFile.Apple.pas; the CreateUserPreferencesIniFile helper function has also now moved to CCR.PrefsIniFile.pas. For XE5, I’ve also written a TAndroidPreferencesIniFile class – see here.

Advertisements

3 thoughts on “My FMX TClipboard and TMacPreferencesIniFile implementations now compiling in XE4

  1. Hey Chris — do you know what the Mac format string is for RTF–e.g. what name does it expect to be passed to RegisterFormat?

  2. Pingback: A few XE5-related bits | Delphi Haven

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s