Subversion Repositories decoder

Rev

Blame | Last modification | View Log | RSS feed

  1. unit DragDropText;
  2.  
  3. // -----------------------------------------------------------------------------
  4. // Project:         Drag and Drop Component Suite.
  5. // Module:          DragDropText
  6. // Description:     Implements Dragging and Dropping of different text formats.
  7. // Version:         4.0
  8. // Date:            18-MAY-2001
  9. // Target:          Win32, Delphi 5-6
  10. // Authors:         Anders Melander, anders@melander.dk, http://www.melander.dk
  11. // Copyright        © 1997-2001 Angus Johnson & Anders Melander
  12. // -----------------------------------------------------------------------------
  13. interface
  14.  
  15. uses
  16.   DragDrop,
  17.   DropTarget,
  18.   DropSource,
  19.   DragDropFormats,
  20.   ActiveX,
  21.   Windows,
  22.   Classes;
  23.  
  24. type
  25. ////////////////////////////////////////////////////////////////////////////////
  26. //
  27. //              TRichTextClipboardFormat
  28. //
  29. ////////////////////////////////////////////////////////////////////////////////
  30.   TRichTextClipboardFormat = class(TCustomTextClipboardFormat)
  31.   public
  32.     function GetClipboardFormat: TClipFormat; override;
  33.     function HasData: boolean; override;
  34.     function Assign(Source: TCustomDataFormat): boolean; override;
  35.     function AssignTo(Dest: TCustomDataFormat): boolean; override;
  36.     property Text;
  37.   end;
  38.  
  39. ////////////////////////////////////////////////////////////////////////////////
  40. //
  41. //              TUnicodeTextClipboardFormat
  42. //
  43. ////////////////////////////////////////////////////////////////////////////////
  44.   TUnicodeTextClipboardFormat = class(TCustomWideTextClipboardFormat)
  45.   public
  46.     function GetClipboardFormat: TClipFormat; override;
  47.     function Assign(Source: TCustomDataFormat): boolean; override;
  48.     function AssignTo(Dest: TCustomDataFormat): boolean; override;
  49.     property Text;
  50.   end;
  51.  
  52. ////////////////////////////////////////////////////////////////////////////////
  53. //
  54. //              TOEMTextClipboardFormat
  55. //
  56. ////////////////////////////////////////////////////////////////////////////////
  57.   TOEMTextClipboardFormat = class(TCustomTextClipboardFormat)
  58.   public
  59.     function GetClipboardFormat: TClipFormat; override;
  60.     function Assign(Source: TCustomDataFormat): boolean; override;
  61.     function AssignTo(Dest: TCustomDataFormat): boolean; override;
  62.     property Text;
  63.   end;
  64.  
  65. ////////////////////////////////////////////////////////////////////////////////
  66. //
  67. //              TCSVClipboardFormat
  68. //
  69. ////////////////////////////////////////////////////////////////////////////////
  70.   TCSVClipboardFormat = class(TCustomStringListClipboardFormat)
  71.   public
  72.     function GetClipboardFormat: TClipFormat; override;
  73.     function Assign(Source: TCustomDataFormat): boolean; override;
  74.     function AssignTo(Dest: TCustomDataFormat): boolean; override;
  75.     property Lines;
  76.   end;
  77.  
  78. ////////////////////////////////////////////////////////////////////////////////
  79. //
  80. //              TLocaleClipboardFormat
  81. //
  82. ////////////////////////////////////////////////////////////////////////////////
  83.   TLocaleClipboardFormat = class(TCustomDWORDClipboardFormat)
  84.   public
  85.     function GetClipboardFormat: TClipFormat; override;
  86.     function HasData: boolean; override;
  87.     function Assign(Source: TCustomDataFormat): boolean; override;
  88.     function AssignTo(Dest: TCustomDataFormat): boolean; override;
  89.     property Locale: DWORD read GetValueDWORD;
  90.   end;
  91.  
  92. ////////////////////////////////////////////////////////////////////////////////
  93. //
  94. //              TDropTextTarget
  95. //
  96. ////////////////////////////////////////////////////////////////////////////////
  97.   TDropTextTarget = class(TCustomDropMultiTarget)
  98.   private
  99.     FTextFormat         : TTextDataFormat;
  100.   protected
  101.     function GetText: string;
  102.   public
  103.     constructor Create(AOwner: TComponent); override;
  104.     destructor Destroy; override;
  105.     property Text: string read GetText;
  106.   end;
  107.  
  108. ////////////////////////////////////////////////////////////////////////////////
  109. //
  110. //              TDropTextSource
  111. //
  112. ////////////////////////////////////////////////////////////////////////////////
  113.   TDropTextSource = class(TCustomDropMultiSource)
  114.   private
  115.     FTextFormat         : TTextDataFormat;
  116.   protected
  117.     function GetText: string;
  118.     procedure SetText(const Value: string);
  119.   public
  120.     constructor Create(aOwner: TComponent); override;
  121.     destructor Destroy; override;
  122.   published
  123.     property Text: string read GetText write SetText;
  124.   end;
  125.  
  126.  
  127. ////////////////////////////////////////////////////////////////////////////////
  128. //
  129. //              Component registration
  130. //
  131. ////////////////////////////////////////////////////////////////////////////////
  132. procedure Register;
  133.  
  134.  
  135. ////////////////////////////////////////////////////////////////////////////////
  136. //
  137. //              Misc.
  138. //
  139. ////////////////////////////////////////////////////////////////////////////////
  140. function IsRTF(const s: string): boolean;
  141. function MakeRTF(const s: string): string;
  142.  
  143. ////////////////////////////////////////////////////////////////////////////////
  144. ////////////////////////////////////////////////////////////////////////////////
  145. //
  146. //                      IMPLEMENTATION
  147. //
  148. ////////////////////////////////////////////////////////////////////////////////
  149. ////////////////////////////////////////////////////////////////////////////////
  150. implementation
  151.  
  152. uses
  153.   SysUtils;
  154.  
  155. ////////////////////////////////////////////////////////////////////////////////
  156. //
  157. //              Component registration
  158. //
  159. ////////////////////////////////////////////////////////////////////////////////
  160. procedure Register;
  161. begin
  162.   RegisterComponents(DragDropComponentPalettePage, [TDropTextTarget,
  163.     TDropTextSource]);
  164. end;
  165.  
  166. ////////////////////////////////////////////////////////////////////////////////
  167. //
  168. //              Utilities
  169. //
  170. ////////////////////////////////////////////////////////////////////////////////
  171. function IsRTF(const s: string): boolean;
  172. begin
  173.   // This probably isn't a valid test, but it will have to do until I have
  174.   // time to research the RTF specifications.
  175.   { TODO -oanme -cImprovement : Need a solid test for RTF format. }
  176.   Result := (AnsiStrLIComp(PChar(s), '{\rtf', 5) = 0);
  177. end;
  178.  
  179. { TODO -oanme -cImprovement : Needs RTF to text conversion. Maybe ITextDocument can be used. }
  180. function MakeRTF(const s: string): string;
  181. begin
  182.   { TODO -oanme -cImprovement : Needs to escape \ in text to RTF conversion. }
  183.   { TODO -oanme -cImprovement : Needs better text to RTF conversion. }
  184.   if (not IsRTF(s)) then
  185.     Result := '{\rtf1\ansi ' + s + '}'
  186.   else
  187.     Result := s;
  188. end;
  189.  
  190.  
  191. ////////////////////////////////////////////////////////////////////////////////
  192. //
  193. //              TRichTextClipboardFormat
  194. //
  195. ////////////////////////////////////////////////////////////////////////////////
  196. var
  197.   CF_RTF: TClipFormat = 0;
  198.  
  199. function TRichTextClipboardFormat.GetClipboardFormat: TClipFormat;
  200. begin
  201.   // Note: The string 'Rich Text Format', is also defined in the RichEdit
  202.   // unit as CF_RTF
  203.   if (CF_RTF = 0) then
  204.     CF_RTF := RegisterClipboardFormat('Rich Text Format'); // *** DO NOT LOCALIZE ***
  205.   Result := CF_RTF;
  206. end;
  207.  
  208. function TRichTextClipboardFormat.HasData: boolean;
  209. begin
  210.   Result := inherited HasData and IsRTF(Text);
  211. end;
  212.  
  213. function TRichTextClipboardFormat.Assign(Source: TCustomDataFormat): boolean;
  214. begin
  215.   if (Source is TTextDataFormat) then
  216.   begin
  217.     Text := MakeRTF(TTextDataFormat(Source).Text);
  218.     Result := True;
  219.   end else
  220.     Result := inherited Assign(Source);
  221. end;
  222.  
  223. function TRichTextClipboardFormat.AssignTo(Dest: TCustomDataFormat): boolean;
  224. begin
  225.   if (Dest is TTextDataFormat) then
  226.   begin
  227.     TTextDataFormat(Dest).Text := Text;
  228.     Result := True;
  229.   end else
  230.     Result := inherited AssignTo(Dest);
  231. end;
  232.  
  233.  
  234. ////////////////////////////////////////////////////////////////////////////////
  235. //
  236. //              TUnicodeTextClipboardFormat
  237. //
  238. ////////////////////////////////////////////////////////////////////////////////
  239. function TUnicodeTextClipboardFormat.GetClipboardFormat: TClipFormat;
  240. begin
  241.   Result := CF_UNICODETEXT;
  242. end;
  243.  
  244. function TUnicodeTextClipboardFormat.Assign(Source: TCustomDataFormat): boolean;
  245. begin
  246.   if (Source is TTextDataFormat) then
  247.   begin
  248.     Text := TTextDataFormat(Source).Text;
  249.     Result := True;
  250.   end else
  251.     Result := inherited Assign(Source);
  252. end;
  253.  
  254. function TUnicodeTextClipboardFormat.AssignTo(Dest: TCustomDataFormat): boolean;
  255. begin
  256.   if (Dest is TTextDataFormat) then
  257.   begin
  258.     TTextDataFormat(Dest).Text := Text;
  259.     Result := True;
  260.   end else
  261.     Result := inherited AssignTo(Dest);
  262. end;
  263.  
  264.  
  265. ////////////////////////////////////////////////////////////////////////////////
  266. //
  267. //              TOEMTextClipboardFormat
  268. //
  269. ////////////////////////////////////////////////////////////////////////////////
  270. function TOEMTextClipboardFormat.GetClipboardFormat: TClipFormat;
  271. begin
  272.   Result := CF_OEMTEXT;
  273. end;
  274.  
  275. function TOEMTextClipboardFormat.Assign(Source: TCustomDataFormat): boolean;
  276. var
  277.   OEMText               : string;
  278. begin
  279.   if (Source is TTextDataFormat) then
  280.   begin
  281.     // First convert ANSI string to OEM string...
  282.     SetLength(OEMText, Length(TTextDataFormat(Source).Text));
  283.     CharToOemBuff(PChar(TTextDataFormat(Source).Text), PChar(OEMText),
  284.       Length(TTextDataFormat(Source).Text));
  285.     // ...then assign OEM string
  286.     Text := OEMText;
  287.     Result := True;
  288.   end else
  289.     Result := inherited Assign(Source);
  290. end;
  291.  
  292. function TOEMTextClipboardFormat.AssignTo(Dest: TCustomDataFormat): boolean;
  293. var
  294.   AnsiText              : string;
  295. begin
  296.   if (Dest is TTextDataFormat) then
  297.   begin
  298.     // First convert OEM string to ANSI string...
  299.     SetLength(AnsiText, Length(Text));
  300.     OemToCharBuff(PChar(Text), PChar(AnsiText), Length(Text));
  301.     // ...then assign ANSI string
  302.     TTextDataFormat(Dest).Text := AnsiText;
  303.     Result := True;
  304.   end else
  305.     Result := inherited AssignTo(Dest);
  306. end;
  307.  
  308.  
  309. ////////////////////////////////////////////////////////////////////////////////
  310. //
  311. //              TCSVClipboardFormat
  312. //
  313. ////////////////////////////////////////////////////////////////////////////////
  314. var
  315.   CF_CSV: TClipFormat = 0;
  316.  
  317. function TCSVClipboardFormat.GetClipboardFormat: TClipFormat;
  318. begin
  319.   if (CF_CSV = 0) then
  320.     CF_CSV := RegisterClipboardFormat('CSV'); // *** DO NOT LOCALIZE ***
  321.   Result := CF_CSV;
  322. end;
  323.  
  324. function TCSVClipboardFormat.Assign(Source: TCustomDataFormat): boolean;
  325. begin
  326.   if (Source is TTextDataFormat) then
  327.   begin
  328.     Lines.Text := TTextDataFormat(Source).Text;
  329.     Result := True;
  330.   end else
  331.     Result := inherited AssignTo(Source);
  332. end;
  333.  
  334. function TCSVClipboardFormat.AssignTo(Dest: TCustomDataFormat): boolean;
  335. begin
  336.   if (Dest is TTextDataFormat) then
  337.   begin
  338.     TTextDataFormat(Dest).Text := Lines.Text;
  339.     Result := True;
  340.   end else
  341.     Result := inherited AssignTo(Dest);
  342. end;
  343.  
  344.  
  345. ////////////////////////////////////////////////////////////////////////////////
  346. //
  347. //              TLocaleClipboardFormat
  348. //
  349. ////////////////////////////////////////////////////////////////////////////////
  350. function TLocaleClipboardFormat.GetClipboardFormat: TClipFormat;
  351. begin
  352.   Result := CF_LOCALE;
  353. end;
  354.  
  355. function TLocaleClipboardFormat.HasData: boolean;
  356. begin
  357.   Result := (Locale <> 0);
  358. end;
  359.  
  360. function TLocaleClipboardFormat.Assign(Source: TCustomDataFormat): boolean;
  361. begin
  362.   // So far we have no one to play with...
  363.   Result := inherited Assign(Source);
  364. end;
  365.  
  366. function TLocaleClipboardFormat.AssignTo(Dest: TCustomDataFormat): boolean;
  367. begin
  368.   // So far we have no one to play with...
  369.   Result := inherited AssignTo(Dest);
  370. end;
  371.  
  372.  
  373. ////////////////////////////////////////////////////////////////////////////////
  374. //
  375. //              TDropTextTarget
  376. //
  377. ////////////////////////////////////////////////////////////////////////////////
  378. constructor TDropTextTarget.Create(AOwner: TComponent);
  379. begin
  380.   inherited Create(AOwner);
  381.   FTextFormat := TTextDataFormat.Create(Self);
  382. end;
  383.  
  384. destructor TDropTextTarget.Destroy;
  385. begin
  386.   FTextFormat.Free;
  387.   inherited Destroy;
  388. end;
  389.  
  390. function TDropTextTarget.GetText: string;
  391. begin
  392.   Result := FTextFormat.Text;
  393. end;
  394.  
  395.  
  396. ////////////////////////////////////////////////////////////////////////////////
  397. //
  398. //              TDropTextSource
  399. //
  400. ////////////////////////////////////////////////////////////////////////////////
  401. constructor TDropTextSource.Create(aOwner: TComponent);
  402. begin
  403.   inherited Create(aOwner);
  404.   FTextFormat := TTextDataFormat.Create(Self);
  405. end;
  406.  
  407. destructor TDropTextSource.Destroy;
  408. begin
  409.   FTextFormat.Free;
  410.   inherited Destroy;
  411. end;
  412.  
  413. function TDropTextSource.GetText: string;
  414. begin
  415.   Result := FTextFormat.Text;
  416. end;
  417.  
  418. procedure TDropTextSource.SetText(const Value: string);
  419. begin
  420.   FTextFormat.Text := Value;
  421. end;
  422.  
  423. ////////////////////////////////////////////////////////////////////////////////
  424. //
  425. //              Initialization/Finalization
  426. //
  427. ////////////////////////////////////////////////////////////////////////////////
  428.  
  429. initialization
  430.   // Clipboard format registration
  431.   TTextDataFormat.RegisterCompatibleFormat(TUnicodeTextClipboardFormat, 1, csSourceTarget, [ddRead]);
  432.   TTextDataFormat.RegisterCompatibleFormat(TRichTextClipboardFormat, 2, csSourceTarget, [ddRead]);
  433.   TTextDataFormat.RegisterCompatibleFormat(TOEMTextClipboardFormat, 2, csSourceTarget, [ddRead]);
  434.   TTextDataFormat.RegisterCompatibleFormat(TCSVClipboardFormat, 3, csSourceTarget, [ddRead]);
  435.  
  436. finalization
  437.   // Clipboard format unregistration
  438.   TUnicodeTextClipboardFormat.UnregisterClipboardFormat;
  439.   TRichTextClipboardFormat.UnregisterClipboardFormat;
  440.   TOEMTextClipboardFormat.UnregisterClipboardFormat;
  441.   TCSVClipboardFormat.UnregisterClipboardFormat;
  442. end.
  443.