DropSource.pas 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. unit DropSource;
  2. // -----------------------------------------------------------------------------
  3. // Project: Drag and Drop Component Suite
  4. // Module: DropSource
  5. // Description: Implements Dragging & Dropping of data
  6. // FROM your application to another.
  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. // General changes:
  14. // - Some component glyphs has changed.
  15. //
  16. // TDropSource changes:
  17. // - CutToClipboard and CopyToClipboard now uses OleSetClipboard.
  18. // This means that descendant classes no longer needs to override the
  19. // CutOrCopyToClipboard method.
  20. // - New OnGetData event.
  21. // - Changed to use new V4 architecture:
  22. // * All clipboard format support has been removed from TDropSource, it has
  23. // been renamed to TCustomDropSource and the old TDropSource has been
  24. // modified to descend from TCustomDropSource and has moved to the
  25. // DropSource3 unit. TDropSource is now supported for backwards
  26. // compatibility only and will be removed in a future version.
  27. // * A new TCustomDropMultiSource, derived from TCustomDropSource, uses the
  28. // new architecture (with TClipboardFormat and TDataFormat) and is the new
  29. // base class for all the drop source components.
  30. // - TInterfacedComponent moved to DragDrop unit.
  31. // -----------------------------------------------------------------------------
  32. // TODO -oanme -cCheckItOut : OleQueryLinkFromData
  33. // TODO -oanme -cDocumentation : CutToClipboard and CopyToClipboard alters the value of PreferredDropEffect.
  34. // TODO -oanme -cDocumentation : Clipboard must be flushed or emptied manually after CutToClipboard and CopyToClipboard. Automatic flush is not guaranteed.
  35. // TODO -oanme -cDocumentation : Delete-on-paste. Why and How.
  36. // TODO -oanme -cDocumentation : Optimized move. Why and How.
  37. // TODO -oanme -cDocumentation : OnPaste event is only fired if target sets the "Paste Succeeded" clipboard format. Explorer does this for delete-on-paste move operations.
  38. // TODO -oanme -cDocumentation : DragDetectPlus. Why and How.
  39. // -----------------------------------------------------------------------------
  40. interface
  41. uses
  42. DragDrop,
  43. DragDropFormats,
  44. ActiveX,
  45. Controls,
  46. Windows,
  47. Classes;
  48. {$include DragDrop.inc}
  49. type
  50. TDragResult = (drDropCopy, drDropMove, drDropLink, drCancel,
  51. drOutMemory, drAsync, drUnknown);
  52. TDropEvent = procedure(Sender: TObject; DragType: TDragType;
  53. var ContinueDrop: Boolean) of object;
  54. //: TAfterDropEvent is fired after the target has finished processing a
  55. // successfull drop.
  56. // The Optimized parameter is True if the target either performed an operation
  57. // other than a move or performed an "optimized move". In either cases, the
  58. // source isn't required to delete the source data.
  59. // If the Optimized parameter is False, the target performed an "unoptimized
  60. // move" operation and the source is required to delete the source data to
  61. // complete the move operation.
  62. TAfterDropEvent = procedure(Sender: TObject; DragResult: TDragResult;
  63. Optimized: Boolean) of object;
  64. TFeedbackEvent = procedure(Sender: TObject; Effect: LongInt;
  65. var UseDefaultCursors: Boolean) of object;
  66. //: The TDropDataEvent event is fired when the target requests data from the
  67. // drop source or offers data to the drop source.
  68. // The Handled flag should be set if the event handler satisfied the request.
  69. TDropDataEvent = procedure(Sender: TObject; const FormatEtc: TFormatEtc;
  70. out Medium: TStgMedium; var Handled: Boolean) of object;
  71. //: TPasteEvent is fired when the target sends a "Paste Succeeded" value
  72. // back to the drop source after a clipboard transfer.
  73. // The DeleteOnPaste parameter is True if the source is required to delete
  74. // the source data. This will only occur after a CutToClipboard operation
  75. // (corresponds to a move drag/drop).
  76. TPasteEvent = procedure(Sender: TObject; Action: TDragResult;
  77. DeleteOnPaste: boolean) of object;
  78. ////////////////////////////////////////////////////////////////////////////////
  79. //
  80. // TCustomDropSource
  81. //
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // Abstract base class for all Drop Source components.
  84. // Implements the IDropSource and IDataObject interfaces.
  85. ////////////////////////////////////////////////////////////////////////////////
  86. TCustomDropSource = class(TDragDropComponent, IDropSource, IDataObject,
  87. IAsyncOperation)
  88. private
  89. FDragTypes: TDragTypes;
  90. FFeedbackEffect: LongInt;
  91. // Events...
  92. FOnDrop: TDropEvent;
  93. FOnAfterDrop: TAfterDropEvent;
  94. FOnFeedback: TFeedBackEvent;
  95. FOnGetData: TDropDataEvent;
  96. FOnSetData: TDropDataEvent;
  97. FOnPaste: TPasteEvent;
  98. // Drag images...
  99. FImages: TImageList;
  100. FShowImage: boolean;
  101. FImageIndex: integer;
  102. FImageHotSpot: TPoint;
  103. FDragSourceHelper: IDragSourceHelper;
  104. // Async transfer...
  105. FAllowAsync: boolean;
  106. FRequestAsync: boolean;
  107. FIsAsync: boolean;
  108. protected
  109. property FeedbackEffect: LongInt read FFeedbackEffect write FFeedbackEffect;
  110. // IDropSource implementation
  111. function QueryContinueDrag(fEscapePressed: bool;
  112. grfKeyState: LongInt): HRESULT; stdcall;
  113. function GiveFeedback(dwEffect: LongInt): HRESULT; stdcall;
  114. // IDataObject implementation
  115. function GetData(const FormatEtcIn: TFormatEtc;
  116. out Medium: TStgMedium):HRESULT; stdcall;
  117. function GetDataHere(const FormatEtc: TFormatEtc;
  118. out Medium: TStgMedium):HRESULT; stdcall;
  119. function QueryGetData(const FormatEtc: TFormatEtc): HRESULT; stdcall;
  120. function GetCanonicalFormatEtc(const FormatEtc: TFormatEtc;
  121. out FormatEtcout: TFormatEtc): HRESULT; stdcall;
  122. function SetData(const FormatEtc: TFormatEtc; var Medium: TStgMedium;
  123. fRelease: Bool): HRESULT; stdcall;
  124. function EnumFormatEtc(dwDirection: LongInt;
  125. out EnumFormatEtc: IEnumFormatEtc): HRESULT; stdcall;
  126. function dAdvise(const FormatEtc: TFormatEtc; advf: LongInt;
  127. const advsink: IAdviseSink; out dwConnection: LongInt): HRESULT; stdcall;
  128. function dUnadvise(dwConnection: LongInt): HRESULT; stdcall;
  129. function EnumdAdvise(out EnumAdvise: IEnumStatData): HRESULT; stdcall;
  130. // IAsyncOperation implementation
  131. function EndOperation(hResult: HRESULT; const pbcReserved: IBindCtx;
  132. dwEffects: Cardinal): HRESULT; stdcall;
  133. function GetAsyncMode(out fDoOpAsync: LongBool): HRESULT; stdcall;
  134. function InOperation(out pfInAsyncOp: LongBool): HRESULT; stdcall;
  135. function SetAsyncMode(fDoOpAsync: LongBool): HRESULT; stdcall;
  136. function StartOperation(const pbcReserved: IBindCtx): HRESULT; stdcall;
  137. // Abstract methods
  138. function DoGetData(const FormatEtcIn: TFormatEtc;
  139. out Medium: TStgMedium): HRESULT; virtual; abstract;
  140. function DoSetData(const FormatEtc: TFormatEtc;
  141. var Medium: TStgMedium): HRESULT; virtual;
  142. function HasFormat(const FormatEtc: TFormatEtc): boolean; virtual; abstract;
  143. function GetEnumFormatEtc(dwDirection: LongInt): IEnumFormatEtc; virtual; abstract;
  144. // Data format event sink
  145. procedure DataChanging(Sender: TObject); virtual;
  146. // Clipboard
  147. function CutOrCopyToClipboard: boolean; virtual;
  148. procedure DoOnPaste(Action: TDragResult; DeleteOnPaste: boolean); virtual;
  149. // Property access
  150. procedure SetShowImage(Value: boolean);
  151. procedure SetImages(const Value: TImageList);
  152. procedure SetImageIndex(const Value: integer);
  153. procedure SetPoint(Index: integer; Value: integer);
  154. function GetPoint(Index: integer): integer;
  155. function GetPerformedDropEffect: longInt; virtual;
  156. function GetLogicalPerformedDropEffect: longInt; virtual;
  157. procedure SetPerformedDropEffect(const Value: longInt); virtual;
  158. function GetPreferredDropEffect: longInt; virtual;
  159. procedure SetPreferredDropEffect(const Value: longInt); virtual;
  160. function GetInShellDragLoop: boolean; virtual;
  161. function GetTargetCLSID: TCLSID; virtual;
  162. procedure SetInShellDragLoop(const Value: boolean); virtual;
  163. function GetLiveDataOnClipboard: boolean;
  164. procedure SetAllowAsync(const Value: boolean);
  165. // Component management
  166. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  167. property DragSourceHelper: IDragSourceHelper read FDragSourceHelper;
  168. public
  169. constructor Create(AOwner: TComponent); override;
  170. destructor Destroy; override;
  171. function Execute: TDragResult; virtual;
  172. function CutToClipboard: boolean; virtual;
  173. function CopyToClipboard: boolean; virtual;
  174. procedure FlushClipboard; virtual;
  175. procedure EmptyClipboard; virtual;
  176. property PreferredDropEffect: longInt read GetPreferredDropEffect
  177. write SetPreferredDropEffect;
  178. property PerformedDropEffect: longInt read GetPerformedDropEffect
  179. write SetPerformedDropEffect;
  180. property LogicalPerformedDropEffect: longInt read GetLogicalPerformedDropEffect;
  181. property InShellDragLoop: boolean read GetInShellDragLoop
  182. write SetInShellDragLoop;
  183. property TargetCLSID: TCLSID read GetTargetCLSID;
  184. property LiveDataOnClipboard: boolean read GetLiveDataOnClipboard;
  185. property AsyncTransfer: boolean read FIsAsync;
  186. published
  187. property DragTypes: TDragTypes read FDragTypes write FDragTypes;
  188. // Events
  189. property OnFeedback: TFeedbackEvent read FOnFeedback write FOnFeedback;
  190. property OnDrop: TDropEvent read FOnDrop write FOnDrop;
  191. property OnAfterDrop: TAfterDropEvent read FOnAfterDrop write FOnAfterDrop;
  192. property OnGetData: TDropDataEvent read FOnGetData write FOnGetData;
  193. property OnSetData: TDropDataEvent read FOnSetData write FOnSetData;
  194. property OnPaste: TPasteEvent read FOnPaste write FOnPaste;
  195. // Drag Images...
  196. property Images: TImageList read FImages write SetImages;
  197. property ImageIndex: integer read FImageIndex write SetImageIndex;
  198. property ShowImage: boolean read FShowImage write SetShowImage;
  199. property ImageHotSpotX: integer index 1 read GetPoint write SetPoint;
  200. property ImageHotSpotY: integer index 2 read GetPoint write SetPoint;
  201. // Async transfer...
  202. property AllowAsyncTransfer: boolean read FAllowAsync write SetAllowAsync;
  203. end;
  204. ////////////////////////////////////////////////////////////////////////////////
  205. //
  206. // TCustomDropMultiSource
  207. //
  208. ////////////////////////////////////////////////////////////////////////////////
  209. // Drop target base class which can accept multiple formats.
  210. ////////////////////////////////////////////////////////////////////////////////
  211. TCustomDropMultiSource = class(TCustomDropSource)
  212. private
  213. FFeedbackDataFormat: TFeedbackDataFormat;
  214. FRawDataFormat: TRawDataFormat;
  215. protected
  216. function DoGetData(const FormatEtcIn: TFormatEtc;
  217. out Medium: TStgMedium):HRESULT; override;
  218. function DoSetData(const FormatEtc: TFormatEtc;
  219. var Medium: TStgMedium): HRESULT; override;
  220. function HasFormat(const FormatEtc: TFormatEtc): boolean; override;
  221. function GetEnumFormatEtc(dwDirection: LongInt): IEnumFormatEtc; override;
  222. function GetPerformedDropEffect: longInt; override;
  223. function GetLogicalPerformedDropEffect: longInt; override;
  224. function GetPreferredDropEffect: longInt; override;
  225. procedure SetPerformedDropEffect(const Value: longInt); override;
  226. procedure SetPreferredDropEffect(const Value: longInt); override;
  227. function GetInShellDragLoop: boolean; override;
  228. procedure SetInShellDragLoop(const Value: boolean); override;
  229. function GetTargetCLSID: TCLSID; override;
  230. procedure DoOnSetData(DataFormat: TCustomDataFormat;
  231. ClipboardFormat: TClipboardFormat);
  232. public
  233. constructor Create(AOwner: TComponent); override;
  234. destructor Destroy; override;
  235. property DataFormats;
  236. // TODO : Add support for delayed rendering with OnRenderData event.
  237. published
  238. end;
  239. ////////////////////////////////////////////////////////////////////////////////
  240. //
  241. // TDropEmptySource
  242. //
  243. ////////////////////////////////////////////////////////////////////////////////
  244. // Do-nothing source for use with TDataFormatAdapter and such
  245. ////////////////////////////////////////////////////////////////////////////////
  246. TDropEmptySource = class(TCustomDropMultiSource);
  247. ////////////////////////////////////////////////////////////////////////////////
  248. //
  249. // TDropSourceThread
  250. //
  251. ////////////////////////////////////////////////////////////////////////////////
  252. // Executes a drop source operation from a thread.
  253. // TDropSourceThread is an alternative to the Windows 2000 Asynchronous Data
  254. // Transfer support.
  255. ////////////////////////////////////////////////////////////////////////////////
  256. type
  257. TDropSourceThread = class(TThread)
  258. private
  259. FDropSource: TCustomDropSource;
  260. FDragResult: TDragResult;
  261. protected
  262. procedure Execute; override;
  263. public
  264. constructor Create(ADropSource: TCustomDropSource; AFreeOnTerminate: Boolean);
  265. property DragResult: TDragResult read FDragResult;
  266. property Terminated;
  267. end;
  268. ////////////////////////////////////////////////////////////////////////////////
  269. //
  270. // Utility functions
  271. //
  272. ////////////////////////////////////////////////////////////////////////////////
  273. function DropEffectToDragResult(DropEffect: longInt): TDragResult;
  274. ////////////////////////////////////////////////////////////////////////////////
  275. //
  276. // Component registration
  277. //
  278. ////////////////////////////////////////////////////////////////////////////////
  279. procedure Register;
  280. (*******************************************************************************
  281. **
  282. ** IMPLEMENTATION
  283. **
  284. *******************************************************************************)
  285. implementation
  286. uses
  287. CommCtrl,
  288. ComObj,
  289. Graphics;
  290. ////////////////////////////////////////////////////////////////////////////////
  291. //
  292. // Component registration
  293. //
  294. ////////////////////////////////////////////////////////////////////////////////
  295. procedure Register;
  296. begin
  297. RegisterComponents(DragDropComponentPalettePage, [TDropEmptySource]);
  298. end;
  299. ////////////////////////////////////////////////////////////////////////////////
  300. //
  301. // Utility functions
  302. //
  303. ////////////////////////////////////////////////////////////////////////////////
  304. function DropEffectToDragResult(DropEffect: longInt): TDragResult;
  305. begin
  306. case DropEffect of
  307. DROPEFFECT_NONE:
  308. Result := drCancel;
  309. DROPEFFECT_COPY:
  310. Result := drDropCopy;
  311. DROPEFFECT_MOVE:
  312. Result := drDropMove;
  313. DROPEFFECT_LINK:
  314. Result := drDropLink;
  315. else
  316. Result := drUnknown; // This is probably an error condition
  317. end;
  318. end;
  319. // -----------------------------------------------------------------------------
  320. // TCustomDropSource
  321. // -----------------------------------------------------------------------------
  322. constructor TCustomDropSource.Create(AOwner: TComponent);
  323. begin
  324. inherited Create(AOwner);
  325. DragTypes := [dtCopy]; //default to Copy.
  326. // Note: Normally we would call _AddRef or coLockObjectExternal(Self) here to
  327. // make sure that the component wasn't deleted prematurely (e.g. after a call
  328. // to RegisterDragDrop), but since our ancestor class TInterfacedComponent
  329. // disables reference counting, we do not need to do so.
  330. FImageHotSpot := Point(16,16);
  331. FImages := nil;
  332. end;
  333. destructor TCustomDropSource.Destroy;
  334. begin
  335. // TODO -oanme -cImprovement : Maybe FlushClipboard would be more appropiate?
  336. EmptyClipboard;
  337. inherited Destroy;
  338. end;
  339. // -----------------------------------------------------------------------------
  340. function TCustomDropSource.GetCanonicalFormatEtc(const FormatEtc: TFormatEtc;
  341. out FormatEtcout: TFormatEtc): HRESULT;
  342. begin
  343. Result := DATA_S_SAMEFORMATETC;
  344. end;
  345. // -----------------------------------------------------------------------------
  346. function TCustomDropSource.SetData(const FormatEtc: TFormatEtc;
  347. var Medium: TStgMedium; fRelease: Bool): HRESULT;
  348. begin
  349. // Warning: Ordinarily it would be much more efficient to just call
  350. // HasFormat(FormatEtc) to determine if we support the given format, but
  351. // because we have to able to accept *all* data formats, even unknown ones, in
  352. // order to support the Windows 2000 drag helper functionality, we can't
  353. // reject any formats here. Instead we pass the request on to DoSetData and
  354. // let it worry about the details.
  355. // if (HasFormat(FormatEtc)) then
  356. // begin
  357. try
  358. Result := DoSetData(FormatEtc, Medium);
  359. finally
  360. if (fRelease) then
  361. ReleaseStgMedium(Medium);
  362. end;
  363. // end else
  364. // Result:= DV_E_FORMATETC;
  365. end;
  366. // -----------------------------------------------------------------------------
  367. function TCustomDropSource.DAdvise(const FormatEtc: TFormatEtc; advf: LongInt;
  368. const advSink: IAdviseSink; out dwConnection: LongInt): HRESULT;
  369. begin
  370. Result := OLE_E_ADVISENOTSUPPORTED;
  371. end;
  372. // -----------------------------------------------------------------------------
  373. function TCustomDropSource.DUnadvise(dwConnection: LongInt): HRESULT;
  374. begin
  375. Result := OLE_E_ADVISENOTSUPPORTED;
  376. end;
  377. // -----------------------------------------------------------------------------
  378. function TCustomDropSource.EnumDAdvise(out EnumAdvise: IEnumStatData): HRESULT;
  379. begin
  380. Result := OLE_E_ADVISENOTSUPPORTED;
  381. end;
  382. // -----------------------------------------------------------------------------
  383. function TCustomDropSource.GetData(const FormatEtcIn: TFormatEtc;
  384. out Medium: TStgMedium):HRESULT; stdcall;
  385. var
  386. Handled: boolean;
  387. begin
  388. Handled := False;
  389. if (Assigned(FOnGetData)) then
  390. // Fire event to ask user for data.
  391. FOnGetData(Self, FormatEtcIn, Medium, Handled);
  392. // If user provided data, there is no need to call descendant for it.
  393. if (Handled) then
  394. Result := S_OK
  395. else if (HasFormat(FormatEtcIn)) then
  396. // Call descendant class to get data.
  397. Result := DoGetData(FormatEtcIn, Medium)
  398. else
  399. Result:= DV_E_FORMATETC;
  400. end;
  401. // -----------------------------------------------------------------------------
  402. function TCustomDropSource.GetDataHere(const FormatEtc: TFormatEtc;
  403. out Medium: TStgMedium):HRESULT; stdcall;
  404. begin
  405. Result := E_NOTIMPL;
  406. end;
  407. // -----------------------------------------------------------------------------
  408. function TCustomDropSource.QueryGetData(const FormatEtc: TFormatEtc): HRESULT; stdcall;
  409. begin
  410. if (HasFormat(FormatEtc)) then
  411. Result:= S_OK
  412. else
  413. Result:= DV_E_FORMATETC;
  414. end;
  415. // -----------------------------------------------------------------------------
  416. function TCustomDropSource.EnumFormatEtc(dwDirection: LongInt;
  417. out EnumFormatEtc:IEnumFormatEtc): HRESULT; stdcall;
  418. begin
  419. EnumFormatEtc := GetEnumFormatEtc(dwDirection);
  420. if (EnumFormatEtc <> nil) then
  421. Result := S_OK
  422. else
  423. Result := E_NOTIMPL;
  424. end;
  425. // -----------------------------------------------------------------------------
  426. // Implements IDropSource.QueryContinueDrag
  427. function TCustomDropSource.QueryContinueDrag(fEscapePressed: bool;
  428. grfKeyState: LongInt): HRESULT; stdcall;
  429. var
  430. ContinueDrop : Boolean;
  431. DragType : TDragType;
  432. begin
  433. if FEscapePressed then
  434. Result := DRAGDROP_S_CANCEL
  435. // Allow drag and drop with either mouse buttons.
  436. else if (grfKeyState and (MK_LBUTTON or MK_RBUTTON) = 0) then
  437. begin
  438. ContinueDrop := DropEffectToDragType(FeedbackEffect, DragType) and
  439. (DragType in DragTypes);
  440. InShellDragLoop := False;
  441. // If a valid drop then do OnDrop event if assigned...
  442. if ContinueDrop and Assigned(OnDrop) then
  443. OnDrop(Self, DragType, ContinueDrop);
  444. if ContinueDrop then
  445. Result := DRAGDROP_S_DROP
  446. else
  447. Result := DRAGDROP_S_CANCEL;
  448. end else
  449. Result := S_OK;
  450. end;
  451. // -----------------------------------------------------------------------------
  452. // Implements IDropSource.GiveFeedback
  453. function TCustomDropSource.GiveFeedback(dwEffect: LongInt): HRESULT; stdcall;
  454. var
  455. UseDefaultCursors: Boolean;
  456. begin
  457. UseDefaultCursors := True;
  458. FeedbackEffect := dwEffect;
  459. if Assigned(OnFeedback) then
  460. OnFeedback(Self, dwEffect, UseDefaultCursors);
  461. if UseDefaultCursors then
  462. Result := DRAGDROP_S_USEDEFAULTCURSORS
  463. else
  464. Result := S_OK;
  465. end;
  466. // -----------------------------------------------------------------------------
  467. function TCustomDropSource.DoSetData(const FormatEtc: TFormatEtc;
  468. var Medium: TStgMedium): HRESULT;
  469. var
  470. Handled: boolean;
  471. begin
  472. Result := E_NOTIMPL;
  473. if (Assigned(FOnSetData)) then
  474. begin
  475. Handled := False;
  476. // Fire event to ask user to handle data.
  477. FOnSetData(Self, FormatEtc, Medium, Handled);
  478. if (Handled) then
  479. Result := S_OK;
  480. end;
  481. end;
  482. // -----------------------------------------------------------------------------
  483. procedure TCustomDropSource.SetAllowAsync(const Value: boolean);
  484. begin
  485. if (FAllowAsync <> Value) then
  486. begin
  487. FAllowAsync := Value;
  488. if (not FAllowAsync) then
  489. begin
  490. FRequestAsync := False;
  491. FIsAsync := False;
  492. end;
  493. end;
  494. end;
  495. function TCustomDropSource.GetAsyncMode(out fDoOpAsync: LongBool): HRESULT;
  496. begin
  497. fDoOpAsync := FRequestAsync;
  498. Result := S_OK;
  499. end;
  500. function TCustomDropSource.SetAsyncMode(fDoOpAsync: LongBool): HRESULT;
  501. begin
  502. if (FAllowAsync) then
  503. begin
  504. FRequestAsync := fDoOpAsync;
  505. Result := S_OK;
  506. end else
  507. Result := E_NOTIMPL;
  508. end;
  509. function TCustomDropSource.InOperation(out pfInAsyncOp: LongBool): HRESULT;
  510. begin
  511. pfInAsyncOp := FIsAsync;
  512. Result := S_OK;
  513. end;
  514. function TCustomDropSource.StartOperation(const pbcReserved: IBindCtx): HRESULT;
  515. begin
  516. if (FRequestAsync) then
  517. begin
  518. FIsAsync := True;
  519. Result := S_OK;
  520. end else
  521. Result := E_NOTIMPL;
  522. end;
  523. function TCustomDropSource.EndOperation(hResult: HRESULT;
  524. const pbcReserved: IBindCtx; dwEffects: Cardinal): HRESULT;
  525. var
  526. DropResult: TDragResult;
  527. begin
  528. if (FIsAsync) then
  529. begin
  530. FIsAsync := False;
  531. if (Assigned(FOnAfterDrop)) then
  532. begin
  533. if (Succeeded(hResult)) then
  534. DropResult := DropEffectToDragResult(dwEffects and DragTypesToDropEffect(FDragTypes))
  535. else
  536. DropResult := drUnknown;
  537. FOnAfterDrop(Self, DropResult,
  538. (DropResult <> drDropMove) or (PerformedDropEffect <> DROPEFFECT_MOVE));
  539. end;
  540. Result := S_OK;
  541. end else
  542. Result := E_FAIL;
  543. end;
  544. function TCustomDropSource.Execute: TDragResult;
  545. function GetRGBColor(Value: TColor): DWORD;
  546. begin
  547. Result := ColorToRGB(Value);
  548. case Result of
  549. clNone: Result := CLR_NONE;
  550. clDefault: Result := CLR_DEFAULT;
  551. end;
  552. end;
  553. var
  554. DropResult: HRESULT;
  555. AllowedEffects,
  556. DropEffect: longint;
  557. IsDraggingImage: boolean;
  558. shDragImage: TSHDRAGIMAGE;
  559. shDragBitmap: TBitmap;
  560. begin
  561. shDragBitmap := nil;
  562. AllowedEffects := DragTypesToDropEffect(FDragTypes);
  563. // Reset the "Performed Drop Effect" value. If it is supported by the target,
  564. // the target will set it to the desired value when the drop occurs.
  565. PerformedDropEffect := -1;
  566. if (FShowImage) then
  567. begin
  568. // Attempt to create Drag Drop helper object.
  569. // At present this is only supported on Windows 2000. If the object can't be
  570. // created, we fall back to the old image list based method (which only
  571. // works within the application).
  572. CoCreateInstance(CLSID_DragDropHelper, nil, CLSCTX_INPROC_SERVER,
  573. IDragSourceHelper, FDragSourceHelper);
  574. // Display drag image.
  575. if (FDragSourceHelper <> nil) then
  576. begin
  577. IsDraggingImage := True;
  578. shDragBitmap := TBitmap.Create;
  579. shDragBitmap.PixelFormat := pfDevice;
  580. FImages.GetBitmap(ImageIndex, shDragBitmap);
  581. shDragImage.hbmpDragImage := shDragBitmap.Handle;
  582. shDragImage.sizeDragImage.cx := shDragBitmap.Width;
  583. shDragImage.sizeDragImage.cy := shDragBitmap.Height;
  584. shDragImage.crColorKey := GetRGBColor(FImages.BkColor);
  585. shDragImage.ptOffset.x := ImageHotSpotX;
  586. shDragImage.ptOffset.y := ImageHotSpotY;
  587. if Failed(FDragSourceHelper.InitializeFromBitmap(shDragImage, Self)) then
  588. begin
  589. FDragSourceHelper := nil;
  590. shDragBitmap.Free;
  591. shDragBitmap := nil;
  592. end;
  593. end else
  594. IsDraggingImage := False;
  595. // Fall back to image list drag image if platform doesn't support
  596. // IDragSourceHelper or if we "just" failed to initialize properly.
  597. if (FDragSourceHelper = nil) then
  598. begin
  599. IsDraggingImage := ImageList_BeginDrag(FImages.Handle, FImageIndex,
  600. FImageHotSpot.X, FImageHotSpot.Y);
  601. end;
  602. end else
  603. IsDraggingImage := False;
  604. if (AllowAsyncTransfer) then
  605. SetAsyncMode(True);
  606. try
  607. InShellDragLoop := True;
  608. try
  609. DropResult := DoDragDrop(Self, Self, AllowedEffects, DropEffect);
  610. finally
  611. // InShellDragLoop is also reset in TCustomDropSource.QueryContinueDrag.
  612. // This is just to make absolutely sure that it is reset (actually no big
  613. // deal if it isn't).
  614. InShellDragLoop := False;
  615. end;
  616. finally
  617. if IsDraggingImage then
  618. begin
  619. if (FDragSourceHelper <> nil) then
  620. begin
  621. FDragSourceHelper := nil;
  622. shDragBitmap.Free;
  623. end else
  624. ImageList_EndDrag;
  625. end;
  626. end;
  627. case DropResult of
  628. DRAGDROP_S_DROP:
  629. (*
  630. ** Special handling of "optimized move".
  631. ** If PerformedDropEffect has been set by the target to DROPEFFECT_MOVE
  632. ** and the drop effect returned from DoDragDrop is different from
  633. ** DROPEFFECT_MOVE, then an optimized move was performed.
  634. ** Note: This is different from how MSDN states that an optimized move is
  635. ** signalled, but matches how Windows 2000 signals an optimized move.
  636. **
  637. ** On Windows 2000 an optimized move is signalled by:
  638. ** 1) Returning DRAGDROP_S_DROP from DoDragDrop.
  639. ** 2) Setting drop effect to DROPEFFECT_NONE.
  640. ** 3) Setting the "Performed Dropeffect" format to DROPEFFECT_MOVE.
  641. **
  642. ** On previous version of Windows, an optimized move is signalled by:
  643. ** 1) Returning DRAGDROP_S_DROP from DoDragDrop.
  644. ** 2) Setting drop effect to DROPEFFECT_MOVE.
  645. ** 3) Setting the "Performed Dropeffect" format to DROPEFFECT_NONE.
  646. **
  647. ** The documentation states that an optimized move is signalled by:
  648. ** 1) Returning DRAGDROP_S_DROP from DoDragDrop.
  649. ** 2) Setting drop effect to DROPEFFECT_NONE or DROPEFFECT_COPY.
  650. ** 3) Setting the "Performed Dropeffect" format to DROPEFFECT_NONE.
  651. *)
  652. if (LogicalPerformedDropEffect = DROPEFFECT_MOVE) or
  653. ((DropEffect <> DROPEFFECT_MOVE) and (PerformedDropEffect = DROPEFFECT_MOVE)) then
  654. Result := drDropMove
  655. else
  656. Result := DropEffectToDragResult(DropEffect and AllowedEffects);
  657. DRAGDROP_S_CANCEL:
  658. Result := drCancel;
  659. E_OUTOFMEMORY:
  660. Result := drOutMemory;
  661. else
  662. // This should never happen!
  663. Result := drUnknown;
  664. end;
  665. // Reset PerformedDropEffect if the target didn't set it.
  666. if (PerformedDropEffect = -1) then
  667. PerformedDropEffect := DROPEFFECT_NONE;
  668. // Fire OnAfterDrop event unless we are in the middle of an async data
  669. // transfer.
  670. if (not AsyncTransfer) and (Assigned(FOnAfterDrop)) then
  671. FOnAfterDrop(Self, Result,
  672. (Result = drDropMove) and
  673. ((DropEffect <> DROPEFFECT_MOVE) or (PerformedDropEffect <> DROPEFFECT_MOVE)));
  674. end;
  675. // -----------------------------------------------------------------------------
  676. function TCustomDropSource.GetPerformedDropEffect: longInt;
  677. begin
  678. Result := DROPEFFECT_NONE;
  679. end;
  680. function TCustomDropSource.GetLogicalPerformedDropEffect: longInt;
  681. begin
  682. Result := DROPEFFECT_NONE;
  683. end;
  684. procedure TCustomDropSource.SetPerformedDropEffect(const Value: longInt);
  685. begin
  686. // Not implemented in base class
  687. end;
  688. function TCustomDropSource.GetPreferredDropEffect: longInt;
  689. begin
  690. Result := DROPEFFECT_NONE;
  691. end;
  692. procedure TCustomDropSource.SetPreferredDropEffect(const Value: longInt);
  693. begin
  694. // Not implemented in base class
  695. end;
  696. function TCustomDropSource.GetInShellDragLoop: boolean;
  697. begin
  698. Result := False;
  699. end;
  700. function TCustomDropSource.GetTargetCLSID: TCLSID;
  701. begin
  702. Result := GUID_NULL;
  703. end;
  704. procedure TCustomDropSource.SetInShellDragLoop(const Value: boolean);
  705. begin
  706. // Not implemented in base class
  707. end;
  708. procedure TCustomDropSource.DataChanging(Sender: TObject);
  709. begin
  710. // Data is changing - Flush clipboard to freeze the contents
  711. FlushClipboard;
  712. end;
  713. procedure TCustomDropSource.FlushClipboard;
  714. begin
  715. // If we have live data on the clipboard...
  716. if (LiveDataOnClipboard) then
  717. // ...we force the clipboard to make a static copy of the data
  718. // before the data changes.
  719. OleCheck(OleFlushClipboard);
  720. end;
  721. procedure TCustomDropSource.EmptyClipboard;
  722. begin
  723. // If we have live data on the clipboard...
  724. if (LiveDataOnClipboard) then
  725. // ...we empty the clipboard.
  726. OleCheck(OleSetClipboard(nil));
  727. end;
  728. function TCustomDropSource.CutToClipboard: boolean;
  729. begin
  730. PreferredDropEffect := DROPEFFECT_MOVE;
  731. // Copy data to clipboard
  732. Result := CutOrCopyToClipboard;
  733. end;
  734. // -----------------------------------------------------------------------------
  735. function TCustomDropSource.CopyToClipboard: boolean;
  736. begin
  737. PreferredDropEffect := DROPEFFECT_COPY;
  738. // Copy data to clipboard
  739. Result := CutOrCopyToClipboard;
  740. end;
  741. // -----------------------------------------------------------------------------
  742. function TCustomDropSource.CutOrCopyToClipboard: boolean;
  743. begin
  744. Result := (OleSetClipboard(Self as IDataObject) = S_OK);
  745. end;
  746. procedure TCustomDropSource.DoOnPaste(Action: TDragResult; DeleteOnPaste: boolean);
  747. begin
  748. if (Assigned(FOnPaste)) then
  749. FOnPaste(Self, Action, DeleteOnPaste);
  750. end;
  751. function TCustomDropSource.GetLiveDataOnClipboard: boolean;
  752. begin
  753. Result := (OleIsCurrentClipboard(Self as IDataObject) = S_OK);
  754. end;
  755. // -----------------------------------------------------------------------------
  756. procedure TCustomDropSource.SetImages(const Value: TImageList);
  757. begin
  758. if (FImages = Value) then
  759. exit;
  760. FImages := Value;
  761. if (csLoading in ComponentState) then
  762. exit;
  763. { DONE -oanme : Shouldn't FShowImage and FImageIndex only be reset if FImages = nil? }
  764. if (FImages = nil) or (FImageIndex >= FImages.Count) then
  765. FImageIndex := 0;
  766. FShowImage := FShowImage and (FImages <> nil) and (FImages.Count > 0);
  767. end;
  768. // -----------------------------------------------------------------------------
  769. procedure TCustomDropSource.SetImageIndex(const Value: integer);
  770. begin
  771. if (csLoading in ComponentState) then
  772. begin
  773. FImageIndex := Value;
  774. exit;
  775. end;
  776. if (Value < 0) or (FImages.Count = 0) or (FImages = nil) then
  777. begin
  778. FImageIndex := 0;
  779. FShowImage := False;
  780. end else
  781. if (Value < FImages.Count) then
  782. FImageIndex := Value;
  783. end;
  784. // -----------------------------------------------------------------------------
  785. procedure TCustomDropSource.SetPoint(Index: integer; Value: integer);
  786. begin
  787. if (Index = 1) then
  788. FImageHotSpot.x := Value
  789. else
  790. FImageHotSpot.y := Value;
  791. end;
  792. // -----------------------------------------------------------------------------
  793. function TCustomDropSource.GetPoint(Index: integer): integer;
  794. begin
  795. if (Index = 1) then
  796. Result := FImageHotSpot.x
  797. else
  798. Result := FImageHotSpot.y;
  799. end;
  800. // -----------------------------------------------------------------------------
  801. procedure TCustomDropSource.SetShowImage(Value: boolean);
  802. begin
  803. FShowImage := Value;
  804. if (csLoading in ComponentState) then
  805. exit;
  806. if (FImages = nil) then
  807. FShowImage := False;
  808. end;
  809. // -----------------------------------------------------------------------------
  810. procedure TCustomDropSource.Notification(AComponent: TComponent;
  811. Operation: TOperation);
  812. begin
  813. inherited Notification(AComponent, Operation);
  814. if (Operation = opRemove) and (AComponent = FImages) then
  815. Images := nil;
  816. end;
  817. ////////////////////////////////////////////////////////////////////////////////
  818. //
  819. // TEnumFormatEtc
  820. //
  821. ////////////////////////////////////////////////////////////////////////////////
  822. // Format enumerator used by TCustomDropMultiTarget.
  823. ////////////////////////////////////////////////////////////////////////////////
  824. type
  825. TEnumFormatEtc = class(TInterfacedObject, IEnumFormatEtc)
  826. private
  827. FFormats : TClipboardFormats;
  828. FIndex : integer;
  829. protected
  830. constructor CreateClone(AFormats: TClipboardFormats; AIndex: Integer);
  831. public
  832. constructor Create(AFormats: TDataFormats; Direction: TDataDirection);
  833. { IEnumFormatEtc implentation }
  834. function Next(Celt: LongInt; out Elt; pCeltFetched: pLongInt): HRESULT; stdcall;
  835. function Skip(Celt: LongInt): HRESULT; stdcall;
  836. function Reset: HRESULT; stdcall;
  837. function Clone(out Enum: IEnumFormatEtc): HRESULT; stdcall;
  838. end;
  839. constructor TEnumFormatEtc.Create(AFormats: TDataFormats; Direction: TDataDirection);
  840. var
  841. i, j : integer;
  842. begin
  843. inherited Create;
  844. FFormats := TClipboardFormats.Create(nil, False);
  845. FIndex := 0;
  846. for i := 0 to AFormats.Count-1 do
  847. for j := 0 to AFormats[i].CompatibleFormats.Count-1 do
  848. if (Direction in AFormats[i].CompatibleFormats[j].DataDirections) and
  849. (not FFormats.Contain(TClipboardFormatClass(AFormats[i].CompatibleFormats[j].ClassType))) then
  850. FFormats.Add(AFormats[i].CompatibleFormats[j]);
  851. end;
  852. constructor TEnumFormatEtc.CreateClone(AFormats: TClipboardFormats; AIndex: Integer);
  853. var
  854. i : integer;
  855. begin
  856. inherited Create;
  857. FFormats := TClipboardFormats.Create(nil, False);
  858. FIndex := AIndex;
  859. for i := 0 to AFormats.Count-1 do
  860. FFormats.Add(AFormats[i]);
  861. end;
  862. function TEnumFormatEtc.Next(Celt: LongInt; out Elt;
  863. pCeltFetched: pLongInt): HRESULT;
  864. var
  865. i : integer;
  866. FormatEtc : PFormatEtc;
  867. begin
  868. i := 0;
  869. FormatEtc := PFormatEtc(@Elt);
  870. while (i < Celt) and (FIndex < FFormats.Count) do
  871. begin
  872. FormatEtc^ := FFormats[FIndex].FormatEtc;
  873. Inc(FormatEtc);
  874. Inc(i);
  875. Inc(FIndex);
  876. end;
  877. if (pCeltFetched <> nil) then
  878. pCeltFetched^ := i;
  879. if (i = Celt) then
  880. Result := S_OK
  881. else
  882. Result := S_FALSE;
  883. end;
  884. function TEnumFormatEtc.Skip(Celt: LongInt): HRESULT;
  885. begin
  886. if (FIndex + Celt <= FFormats.Count) then
  887. begin
  888. inc(FIndex, Celt);
  889. Result := S_OK;
  890. end else
  891. begin
  892. FIndex := FFormats.Count;
  893. Result := S_FALSE;
  894. end;
  895. end;
  896. function TEnumFormatEtc.Reset: HRESULT;
  897. begin
  898. FIndex := 0;
  899. Result := S_OK;
  900. end;
  901. function TEnumFormatEtc.Clone(out Enum: IEnumFormatEtc): HRESULT;
  902. begin
  903. Enum := TEnumFormatEtc.CreateClone(FFormats, FIndex);
  904. Result := S_OK;
  905. end;
  906. ////////////////////////////////////////////////////////////////////////////////
  907. //
  908. // TCustomDropMultiSource
  909. //
  910. ////////////////////////////////////////////////////////////////////////////////
  911. type
  912. TSourceDataFormats = class(TDataFormats)
  913. public
  914. function Add(DataFormat: TCustomDataFormat): integer; override;
  915. end;
  916. function TSourceDataFormats.Add(DataFormat: TCustomDataFormat): integer;
  917. begin
  918. Result := inherited Add(DataFormat);
  919. // Set up change notification so drop source can flush clipboard if data changes.
  920. DataFormat.OnChanging := TCustomDropMultiSource(DataFormat.Owner).DataChanging;
  921. end;
  922. constructor TCustomDropMultiSource.Create(AOwner: TComponent);
  923. begin
  924. inherited Create(AOwner);
  925. FDataFormats := TSourceDataFormats.Create;
  926. FFeedbackDataFormat := TFeedbackDataFormat.Create(Self);
  927. FRawDataFormat := TRawDataFormat.Create(Self);
  928. end;
  929. destructor TCustomDropMultiSource.Destroy;
  930. var
  931. i : integer;
  932. begin
  933. EmptyClipboard;
  934. // Delete all target formats owned by the object
  935. for i := FDataFormats.Count-1 downto 0 do
  936. FDataFormats[i].Free;
  937. FDataFormats.Free;
  938. inherited Destroy;
  939. end;
  940. function TCustomDropMultiSource.DoGetData(const FormatEtcIn: TFormatEtc;
  941. out Medium: TStgMedium): HRESULT;
  942. var
  943. i, j: integer;
  944. DF: TCustomDataFormat;
  945. CF: TClipboardFormat;
  946. begin
  947. // TODO : Add support for delayed rendering with OnRenderData event.
  948. Medium.tymed := 0;
  949. Medium.UnkForRelease := nil;
  950. Medium.hGlobal := 0;
  951. Result := DV_E_FORMATETC;
  952. (*
  953. ** Loop through all data formats associated with this drop source to find one
  954. ** which can offer the clipboard format requested by the target.
  955. *)
  956. for i := 0 to DataFormats.Count-1 do
  957. begin
  958. DF := DataFormats[i];
  959. // Ignore empty data formats.
  960. if (not DF.HasData) then
  961. continue;
  962. (*
  963. ** Loop through all the data format's supported clipboard formats to find
  964. ** one which contains data and can provide it in the format requested by the
  965. ** target.
  966. *)
  967. for j := 0 to DF.CompatibleFormats.Count-1 do
  968. begin
  969. CF := DF.CompatibleFormats[j];
  970. (*
  971. ** 1) Determine if the clipboard format supports the format requested by
  972. ** the target.
  973. ** 2) Transfer data from the data format object to the clipboard format
  974. ** object.
  975. ** 3) Determine if the clipboard format object now has data to offer.
  976. ** 4) Transfer the data from the clipboard format object to the medium.
  977. *)
  978. if (CF.AcceptFormat(FormatEtcIn)) and
  979. (DataFormats[i].AssignTo(CF)) and
  980. (CF.HasData) and
  981. (CF.SetDataToMedium(FormatEtcIn, Medium)) then
  982. begin
  983. // Once data has been sucessfully transfered to the medium, we clear
  984. // the data in the TClipboardFormat object in order to conserve
  985. // resources.
  986. CF.Clear;
  987. Result := S_OK;
  988. exit;
  989. end;
  990. end;
  991. end;
  992. end;
  993. function TCustomDropMultiSource.DoSetData(const FormatEtc: TFormatEtc;
  994. var Medium: TStgMedium): HRESULT;
  995. var
  996. i, j : integer;
  997. GenericClipboardFormat: TRawClipboardFormat;
  998. begin
  999. Result := E_NOTIMPL;
  1000. // Get data for requested source format.
  1001. for i := 0 to DataFormats.Count-1 do
  1002. for j := 0 to DataFormats[i].CompatibleFormats.Count-1 do
  1003. if (DataFormats[i].CompatibleFormats[j].AcceptFormat(FormatEtc)) and
  1004. (DataFormats[i].CompatibleFormats[j].GetDataFromMedium(Self, Medium)) and
  1005. (DataFormats[i].Assign(DataFormats[i].CompatibleFormats[j])) then
  1006. begin
  1007. DoOnSetData(DataFormats[i], DataFormats[i].CompatibleFormats[j]);
  1008. // Once data has been sucessfully transfered to the medium, we clear
  1009. // the data in the TClipboardFormat object in order to conserve
  1010. // resources.
  1011. DataFormats[i].CompatibleFormats[j].Clear;
  1012. Result := S_OK;
  1013. exit;
  1014. end;
  1015. // The requested data format wasn't supported by any of the registered
  1016. // clipboard formats, but in order to support the Windows 2000 drag drop helper
  1017. // object we have to accept any data which is written to the IDataObject.
  1018. // To do this we create a new clipboard format object, initialize it with the
  1019. // format information passed to us and copy the data.
  1020. GenericClipboardFormat := TRawClipboardFormat.CreateFormatEtc(FormatEtc);
  1021. FRawDataFormat.CompatibleFormats.Add(GenericClipboardFormat);
  1022. if (GenericClipboardFormat.GetDataFromMedium(Self, Medium)) and
  1023. (FRawDataFormat.Assign(GenericClipboardFormat)) then
  1024. Result := S_OK;
  1025. end;
  1026. function TCustomDropMultiSource.GetEnumFormatEtc(dwDirection: Integer): IEnumFormatEtc;
  1027. begin
  1028. if (dwDirection = DATADIR_GET) then
  1029. Result := TEnumFormatEtc.Create(FDataFormats, ddRead)
  1030. else if (dwDirection = DATADIR_SET) then
  1031. Result := TEnumFormatEtc.Create(FDataFormats, ddWrite)
  1032. else
  1033. Result := nil;
  1034. end;
  1035. function TCustomDropMultiSource.HasFormat(const FormatEtc: TFormatEtc): boolean;
  1036. var
  1037. i ,
  1038. j : integer;
  1039. begin
  1040. Result := False;
  1041. for i := 0 to DataFormats.Count-1 do
  1042. for j := 0 to DataFormats[i].CompatibleFormats.Count-1 do
  1043. if (DataFormats[i].CompatibleFormats[j].AcceptFormat(FormatEtc)) then
  1044. begin
  1045. Result := True;
  1046. exit;
  1047. end;
  1048. end;
  1049. function TCustomDropMultiSource.GetPerformedDropEffect: longInt;
  1050. begin
  1051. Result := FFeedbackDataFormat.PerformedDropEffect;
  1052. end;
  1053. function TCustomDropMultiSource.GetLogicalPerformedDropEffect: longInt;
  1054. begin
  1055. Result := FFeedbackDataFormat.LogicalPerformedDropEffect;
  1056. end;
  1057. function TCustomDropMultiSource.GetPreferredDropEffect: longInt;
  1058. begin
  1059. Result := FFeedbackDataFormat.PreferredDropEffect;
  1060. end;
  1061. procedure TCustomDropMultiSource.SetPerformedDropEffect(const Value: longInt);
  1062. begin
  1063. FFeedbackDataFormat.PerformedDropEffect := Value;
  1064. end;
  1065. procedure TCustomDropMultiSource.SetPreferredDropEffect(const Value: longInt);
  1066. begin
  1067. FFeedbackDataFormat.PreferredDropEffect := Value;
  1068. end;
  1069. function TCustomDropMultiSource.GetInShellDragLoop: boolean;
  1070. begin
  1071. Result := FFeedbackDataFormat.InShellDragLoop;
  1072. end;
  1073. procedure TCustomDropMultiSource.SetInShellDragLoop(const Value: boolean);
  1074. begin
  1075. FFeedbackDataFormat.InShellDragLoop := Value;
  1076. end;
  1077. function TCustomDropMultiSource.GetTargetCLSID: TCLSID;
  1078. begin
  1079. Result := FFeedbackDataFormat.TargetCLSID;
  1080. end;
  1081. procedure TCustomDropMultiSource.DoOnSetData(DataFormat: TCustomDataFormat;
  1082. ClipboardFormat: TClipboardFormat);
  1083. var
  1084. DropEffect : longInt;
  1085. begin
  1086. if (ClipboardFormat is TPasteSuccededClipboardFormat) then
  1087. begin
  1088. DropEffect := TPasteSuccededClipboardFormat(ClipboardFormat).Value;
  1089. DoOnPaste(DropEffectToDragResult(DropEffect),
  1090. (DropEffect = DROPEFFECT_MOVE) and (PerformedDropEffect = DROPEFFECT_MOVE));
  1091. end;
  1092. end;
  1093. ////////////////////////////////////////////////////////////////////////////////
  1094. //
  1095. // TDropSourceThread
  1096. //
  1097. ////////////////////////////////////////////////////////////////////////////////
  1098. constructor TDropSourceThread.Create(ADropSource: TCustomDropSource;
  1099. AFreeOnTerminate: Boolean);
  1100. begin
  1101. inherited Create(True);
  1102. FreeOnTerminate := AFreeOnTerminate;
  1103. FDropSource := ADropSource;
  1104. FDragResult := drAsync;
  1105. end;
  1106. procedure TDropSourceThread.Execute;
  1107. var
  1108. pt: TPoint;
  1109. hwndAttach: HWND;
  1110. dwAttachThreadID, dwCurrentThreadID : DWORD;
  1111. begin
  1112. (*
  1113. ** See Microsoft Knowledgebase Article Q139408 for an explanation of the
  1114. ** AttachThreadInput stuff.
  1115. ** http://support.microsoft.com/support/kb/articles/Q139/4/08.asp
  1116. *)
  1117. // Get handle of window under mouse-cursor.
  1118. GetCursorPos(pt);
  1119. hwndAttach := WindowFromPoint(pt);
  1120. ASSERT(hwndAttach<>0, 'Can''t find window with drag-object');
  1121. // Get thread IDs.
  1122. dwAttachThreadID := GetWindowThreadProcessId(hwndAttach, nil);
  1123. dwCurrentThreadID := GetCurrentThreadId();
  1124. // Attach input queues if necessary.
  1125. if (dwAttachThreadID <> dwCurrentThreadID) then
  1126. AttachThreadInput(dwAttachThreadID, dwCurrentThreadID, True);
  1127. try
  1128. // Initialize OLE for this thread.
  1129. OleInitialize(nil);
  1130. try
  1131. // Start drag & drop.
  1132. FDragResult := FDropSource.Execute;
  1133. finally
  1134. OleUninitialize;
  1135. end;
  1136. finally
  1137. // Restore input queue settings.
  1138. if (dwAttachThreadID <> dwCurrentThreadID) then
  1139. AttachThreadInput(dwAttachThreadID, dwCurrentThreadID, False);
  1140. // Set Terminated flag so owner knows that drag has finished.
  1141. Terminate;
  1142. end;
  1143. end;
  1144. end.