GR32.ImageFormats.TClipboard.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. unit GR32.ImageFormats.TClipboard;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is image format support for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Anders Melander <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2008-2022
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$include GR32.inc}
  35. implementation
  36. uses
  37. Classes,
  38. SysUtils,
  39. ClipBrd,
  40. GR32,
  41. GR32_Clipboard,
  42. GR32.ImageFormats;
  43. //------------------------------------------------------------------------------
  44. //
  45. // TImageFormatAdapterTClipboard
  46. //
  47. //------------------------------------------------------------------------------
  48. // Implements IImageFormatAdapter for the TClipboard class.
  49. //------------------------------------------------------------------------------
  50. type
  51. TImageFormatAdapterTClipboard = class(TCustomImageFormatAdapter,
  52. IImageFormatAdapter,
  53. IImageFormatWriteNotification)
  54. strict protected
  55. // IImageFormatAdapter
  56. function CanAssignFrom(Source: TPersistent): boolean; override;
  57. function AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean; override;
  58. function CanAssignTo(Dest: TPersistent): boolean; override;
  59. function AssignTo(Source: TCustomBitmap32; Dest: TPersistent): boolean; override;
  60. private
  61. // IImageFormatWriteNotification
  62. procedure BeginWriting(Source: TCustomBitmap32; Dest: TPersistent);
  63. procedure EndWriting(Source: TCustomBitmap32; Dest: TPersistent);
  64. end;
  65. //------------------------------------------------------------------------------
  66. // IImageFormatAdapter
  67. //------------------------------------------------------------------------------
  68. function TImageFormatAdapterTClipboard.CanAssignFrom(Source: TPersistent): boolean;
  69. begin
  70. Result := (Source is TClipboard) and (ImageFormatManager.ClipboardFormats.CanPasteFromClipboard);
  71. end;
  72. function TImageFormatAdapterTClipboard.AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean;
  73. begin
  74. if (not (Source is TClipboard)) then
  75. Exit(False);
  76. Result := ImageFormatManager.ClipboardFormats.PasteFromClipboard(Dest);
  77. end;
  78. //------------------------------------------------------------------------------
  79. function TImageFormatAdapterTClipboard.CanAssignTo(Dest: TPersistent): boolean;
  80. begin
  81. Result := (Dest is TClipboard);
  82. end;
  83. function TImageFormatAdapterTClipboard.AssignTo(Source: TCustomBitmap32; Dest: TPersistent): boolean;
  84. begin
  85. if (Dest is TClipboard) then
  86. begin
  87. TClipboard(Dest).Open;
  88. try
  89. Result := CopyBitmap32ToClipboard(Source);
  90. // Note that, if PNG format is enabled, we also place a copy of the bitmap
  91. // in PNG format on the clipboard.
  92. // See comment in CopyBitmap32ToClipboard.
  93. finally
  94. TClipboard(Dest).Close;
  95. end;
  96. end else
  97. Result := False;
  98. end;
  99. //------------------------------------------------------------------------------
  100. // IImageFormatWriteNotification
  101. //------------------------------------------------------------------------------
  102. procedure TImageFormatAdapterTClipboard.BeginWriting(Source: TCustomBitmap32; Dest: TPersistent);
  103. begin
  104. if (Dest is TClipboard) then
  105. TClipboard(Dest).Open;
  106. end;
  107. procedure TImageFormatAdapterTClipboard.EndWriting(Source: TCustomBitmap32; Dest: TPersistent);
  108. begin
  109. if (Dest is TClipboard) then
  110. TClipboard(Dest).Close;
  111. end;
  112. //------------------------------------------------------------------------------
  113. //------------------------------------------------------------------------------
  114. //------------------------------------------------------------------------------
  115. var
  116. ImageFormatHandle: integer = 0;
  117. initialization
  118. ImageFormatHandle := ImageFormatManager.RegisterImageFormat(TImageFormatAdapterTClipboard.Create, ImageFormatPriorityNormal);
  119. finalization
  120. ImageFormatManager.UnregisterImageFormat(ImageFormatHandle);
  121. end.