PasTools.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. unit PasTools;
  2. interface
  3. uses
  4. Windows, Types, Classes, ComCtrls, ExtCtrls, Controls, Dialogs, Forms, Messages;
  5. function Construct(ComponentClass: TComponentClass; Owner: TComponent): TComponent;
  6. function IsVistaHard: Boolean;
  7. function IsVista: Boolean;
  8. // Prevent name conflict with C++ IsWin8.
  9. {$HPPEMIT '#define IsWin7 IsWin7Pas'}
  10. {$HPPEMIT END '#undef IsWin7'}
  11. function IsWin7: Boolean;
  12. // Prevent name conflict with C++ IsWin8.
  13. {$HPPEMIT '#define IsWin8 IsWin8Pas'}
  14. {$HPPEMIT END '#undef IsWin8'}
  15. function IsWin8: Boolean;
  16. // Prevent name conflict with C++ CutToChar.
  17. {$HPPEMIT '#define CutToChar CutToCharPas'}
  18. {$HPPEMIT END '#undef CutToChar'}
  19. function CutToChar(var Str: string; Ch: Char; Trim: Boolean): string;
  20. procedure FilterToFileTypes(Filter: string; FileTypes: TFileTypeItems);
  21. // Note that while we based our scaling on pixels-per-inch,
  22. // VCL actually scales based on font size
  23. const
  24. CM_DPICHANGED = WM_USER + $2000 + 10;
  25. WM_DPICHANGED_BEFOREPARENT = $02E2;
  26. WM_DPICHANGED_AFTERPARENT = $02E3;
  27. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  28. function SystemParametersInfoForPixelsPerInch(
  29. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  30. procedure GetFormScaleRatio(Form: TForm; var M, D: Integer);
  31. function GetMonitorFromControl(Control: TControl): TMonitor;
  32. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  33. function GetControlPixelsPerInch(Control: TControl): Integer;
  34. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  35. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  36. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  37. function SaveDimension(Dimension: Integer): Integer;
  38. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  39. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer; overload;
  40. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer; overload;
  41. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  42. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  43. function SavePixelsPerInch(Control: TControl): string;
  44. function SaveDefaultPixelsPerInch: string;
  45. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  46. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  47. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  48. type
  49. TImageListSize = (ilsSmall, ilsLarge);
  50. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  51. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  52. function IsAppIconic: Boolean;
  53. procedure SetAppIconic(Value: Boolean);
  54. procedure SetAppMainForm(Value: TForm);
  55. procedure SetAppTerminated(Value: Boolean);
  56. procedure ForceColorChange(Control: TWinControl);
  57. function IsUncPath(Path: string): Boolean;
  58. type
  59. TApiPathEvent = function(Path: string): string;
  60. var
  61. OnApiPath: TApiPathEvent = nil;
  62. // Prevent name conflict with C++ ApiPath.
  63. // We would not want to call this implementation in any case anyway.
  64. {$HPPEMIT '#define ApiPath ApiPathPas'}
  65. {$HPPEMIT END '#undef ApiPath'}
  66. function ApiPath(Path: string): string;
  67. type
  68. TControlScrollBeforeUpdate = procedure(ObjectToValidate: TObject) of object;
  69. TControlScrollAfterUpdate = procedure of object;
  70. TCustomControlScrollOnDragOver = class
  71. private
  72. FOnBeforeUpdate: TControlScrollBeforeUpdate;
  73. FOnAfterUpdate: TControlScrollAfterUpdate;
  74. FDragOverTimer: TTimer;
  75. FControl: TControl;
  76. FDragOverTime: FILETIME;
  77. FLastVScrollTime: FILETIME;
  78. FVScrollCount: Integer;
  79. procedure DragOverTimer(Sender: TObject);
  80. procedure BeforeUpdate(ObjectToValidate: TObject);
  81. procedure AfterUpdate;
  82. public
  83. constructor Create(Control: TControl; ScheduleDragOver: Boolean);
  84. destructor Destroy; override;
  85. procedure StartDrag; virtual;
  86. procedure EndDrag; virtual;
  87. procedure DragOver(Point: TPoint); virtual; abstract;
  88. property OnBeforeUpdate: TControlScrollBeforeUpdate read FOnBeforeUpdate write FOnBeforeUpdate;
  89. property OnAfterUpdate: TControlScrollAfterUpdate read FOnAfterUpdate write FOnAfterUpdate;
  90. end;
  91. TTreeViewScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  92. private
  93. FLastDragNode: TTreeNode;
  94. FLastHScrollTime: FILETIME;
  95. public
  96. procedure StartDrag; override;
  97. procedure DragOver(Point: TPoint); override;
  98. end;
  99. TListViewScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  100. public
  101. procedure DragOver(Point: TPoint); override;
  102. end;
  103. TListBoxScrollOnDragOver = class(TCustomControlScrollOnDragOver)
  104. public
  105. procedure DragOver(Point: TPoint); override;
  106. end;
  107. implementation
  108. uses
  109. SysUtils, StdCtrls, Graphics, MultiMon, ShellAPI, Generics.Collections, CommCtrl, ImgList;
  110. const
  111. DDExpandDelay = 15000000;
  112. DDMaxSlowCount = 3;
  113. DDVScrollDelay = 2000000;
  114. DDHScrollDelay = 2000000;
  115. DDDragStartDelay = 500000;
  116. function Construct(ComponentClass: TComponentClass; Owner: TComponent): TComponent;
  117. begin
  118. Result := ComponentClass.Create(Owner);
  119. end;
  120. // detects vista, even in compatibility mode
  121. // (GetLocaleInfoEx is available since Vista only)
  122. function IsVistaHard: Boolean;
  123. begin
  124. Result := (GetProcAddress(GetModuleHandle(Kernel32), 'GetLocaleInfoEx') <> nil);
  125. end;
  126. function IsVista: Boolean;
  127. begin
  128. Result := CheckWin32Version(6, 0);
  129. end;
  130. function IsWin7: Boolean;
  131. begin
  132. Result := CheckWin32Version(6, 1);
  133. end;
  134. function IsWin8: Boolean;
  135. begin
  136. Result := CheckWin32Version(6, 2);
  137. end;
  138. function CutToChar(var Str: string; Ch: Char; Trim: Boolean): string;
  139. var
  140. P: Integer;
  141. begin
  142. P := Pos(Ch, Str);
  143. if P > 0 then
  144. begin
  145. Result := Copy(Str, 1, P-1);
  146. Delete(Str, 1, P);
  147. end
  148. else
  149. begin
  150. Result := Str;
  151. Str := '';
  152. end;
  153. if Trim then Result := SysUtils.Trim(Result);
  154. end;
  155. procedure FilterToFileTypes(Filter: string; FileTypes: TFileTypeItems);
  156. var
  157. Item: TFileTypeItem;
  158. begin
  159. while Filter <> '' do
  160. begin
  161. Item := FileTypes.Add();
  162. Item.DisplayName := CutToChar(Filter, '|', True);
  163. Item.FileMask := CutToChar(Filter, '|', True);
  164. end;
  165. end;
  166. type
  167. TGetDpiForMonitorFunc =
  168. function (hMonitor: HMONITOR; MonitorType: Integer; out DpiX, DpiY: Cardinal): HRESULT; stdcall;
  169. TGetSystemMetricsForDpiFunc =
  170. function (nIndex: Integer; Dpi: Cardinal): Integer; stdcall;
  171. TSystemParametersInfoForDpiFunc =
  172. function (uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL; stdcall;
  173. const
  174. MDT_EFFECTIVE_DPI = 0;
  175. var
  176. GetDpiForMonitor: TGetDpiForMonitorFunc = nil;
  177. GetSystemMetricsForDpi: TGetSystemMetricsForDpiFunc = nil;
  178. SystemParametersInfoForDpi: TSystemParametersInfoForDpiFunc = nil;
  179. function HasSystemParametersInfoForPixelsPerInch: Boolean;
  180. begin
  181. Result := Assigned(SystemParametersInfoForDpi);
  182. end;
  183. function SystemParametersInfoForPixelsPerInch(
  184. uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT; dpi: UINT): BOOL;
  185. begin
  186. if HasSystemParametersInfoForPixelsPerInch then
  187. begin
  188. Result := SystemParametersInfoForDpi(uiAction, uiParam, pvParam, fWinIni, dpi);
  189. end
  190. else
  191. begin
  192. Result := SystemParametersInfo(uiAction, uiParam, pvParam, fWinIni);
  193. end;
  194. end;
  195. function GetMonitorPixelsPerInch(Monitor: TMonitor): Integer;
  196. var
  197. DpiX, DpiY: Cardinal;
  198. begin
  199. if Assigned(GetDpiForMonitor) and
  200. (GetDpiForMonitor(Monitor.Handle, MDT_EFFECTIVE_DPI, DpiX, DpiY) = S_OK) then
  201. begin
  202. Result := DpiX;
  203. end
  204. else
  205. begin
  206. Result := Screen.PixelsPerInch;
  207. end;
  208. end;
  209. function GetMonitorFromControl(Control: TControl): TMonitor;
  210. begin
  211. if Control.Parent <> nil then
  212. begin
  213. Result := GetMonitorFromControl(Control.Parent);
  214. end
  215. else
  216. if Control is TCustomForm then
  217. begin
  218. Result := TCustomForm(Control).Monitor;
  219. end
  220. else
  221. if (Control is TWinControl) and TWinControl(Control).HandleAllocated then
  222. begin
  223. Result := Screen.MonitorFromWindow(TWinControl(Control).Handle);
  224. end
  225. else
  226. begin
  227. Result := nil;
  228. end;
  229. end;
  230. function GetControlPixelsPerInch(Control: TControl): Integer;
  231. var
  232. Form: TCustomForm;
  233. Monitor: TMonitor;
  234. begin
  235. if Assigned(GetDpiForMonitor) then // optimization
  236. begin
  237. Form := GetParentForm(Control);
  238. if Assigned(Form) then
  239. begin
  240. // By default, scale according to what the form is so far rendered on.
  241. // If the monitor perceived DPI does not match its monitor DPI, it's because the WM_DPICHANGED is still pending.
  242. Result := TForm(Form).PixelsPerInch;
  243. end
  244. else
  245. begin
  246. Monitor := GetMonitorFromControl(Control);
  247. if Monitor = nil then
  248. begin
  249. Assert(False);
  250. Monitor := Screen.PrimaryMonitor;
  251. end;
  252. Result := GetMonitorPixelsPerInch(Monitor);
  253. end;
  254. end
  255. else
  256. begin
  257. Result := Screen.PixelsPerInch;
  258. end;
  259. end;
  260. function GetComponentPixelsPerInch(Component: TComponent): Integer;
  261. begin
  262. Result := GetControlPixelsPerInch(TControl(Component.Owner));
  263. end;
  264. function LoadDimension(Dimension: Integer; PixelsPerInch: Integer; Control: TControl): Integer;
  265. begin
  266. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), PixelsPerInch);
  267. end;
  268. function StrToDimensionDef(Str: string; PixelsPerInch: Integer; Control: TControl; Default: Integer): Integer;
  269. begin
  270. if TryStrToInt(Str, Result) then
  271. begin
  272. Result := LoadDimension(Result, PixelsPerInch, Control);
  273. end
  274. else
  275. begin
  276. Result := Default;
  277. end;
  278. end;
  279. function SaveDimension(Dimension: Integer): Integer;
  280. begin
  281. // noop
  282. Result := Dimension;
  283. end;
  284. function DimensionToDefaultPixelsPerInch(Dimension: Integer): Integer;
  285. begin
  286. Result := MulDiv(Dimension, USER_DEFAULT_SCREEN_DPI, Screen.PixelsPerInch);
  287. end;
  288. function ScaleByPixelsPerInch(Dimension: Integer; Monitor: TMonitor): Integer;
  289. begin
  290. Result := MulDiv(Dimension, GetMonitorPixelsPerInch(Monitor), USER_DEFAULT_SCREEN_DPI);
  291. end;
  292. function ScaleByPixelsPerInch(Dimension: Integer; Control: TControl): Integer;
  293. begin
  294. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), USER_DEFAULT_SCREEN_DPI);
  295. end;
  296. function ScaleByPixelsPerInchFromSystem(Dimension: Integer; Control: TControl): Integer;
  297. begin
  298. Result := MulDiv(Dimension, GetControlPixelsPerInch(Control), Screen.PixelsPerInch);
  299. end;
  300. function LoadPixelsPerInch(S: string; Control: TControl): Integer;
  301. begin
  302. // for backward compatibility with version that did not save the DPI,
  303. // make reasonable assumption that the configuration was saved with
  304. // the same DPI as we run now
  305. Result := StrToIntDef(S, GetControlPixelsPerInch(Control));
  306. end;
  307. function SavePixelsPerInch(Control: TControl): string;
  308. begin
  309. Result := IntToStr(GetControlPixelsPerInch(Control));
  310. end;
  311. function SaveDefaultPixelsPerInch: string;
  312. begin
  313. Result := IntToStr(USER_DEFAULT_SCREEN_DPI);
  314. end;
  315. // WORKAROUND
  316. // https://stackoverflow.com/q/9410485/850848
  317. type
  318. TFormHelper = class helper for TCustomForm
  319. public
  320. function RetrieveTextHeight: Integer;
  321. function CalculateTextHeight: Integer;
  322. end;
  323. function TFormHelper.RetrieveTextHeight: Integer;
  324. begin
  325. Result := Self.FTextHeight;
  326. end;
  327. function TFormHelper.CalculateTextHeight: Integer;
  328. begin
  329. Result := Self.GetTextHeight;
  330. end;
  331. function ScaleByTextHeightImpl(Control: TControl; Dimension: Integer; TextHeight: Integer): Integer;
  332. var
  333. Form: TCustomForm;
  334. NewTextHeight: Integer;
  335. begin
  336. // RTL_COPY (TCustomForm.ReadState)
  337. Form := ValidParentForm(Control);
  338. NewTextHeight := Form.CalculateTextHeight;
  339. if TextHeight <> NewTextHeight then
  340. begin
  341. Dimension := MulDiv(Dimension, NewTextHeight, TextHeight);
  342. end;
  343. Result := Dimension;
  344. end;
  345. const
  346. OurDesignTimeTextHeight = 13;
  347. function ScaleByTextHeight(Control: TControl; Dimension: Integer): Integer;
  348. var
  349. Form: TCustomForm;
  350. TextHeight: Integer;
  351. begin
  352. // RTL_COPY (TCustomForm.ReadState)
  353. Form := GetParentForm(Control);
  354. if Form = nil then
  355. begin
  356. // This should happen only for screen tip over dropped down menu
  357. Assert(Control.ClassName = 'TTBXPopupWindow');
  358. Result := ScaleByPixelsPerInch(Dimension, Control);
  359. end
  360. else
  361. begin
  362. TextHeight := Form.RetrieveTextHeight;
  363. // runtime form (such as TTBFloatingWindowParent)
  364. if TextHeight = 0 then
  365. begin
  366. Result := ScaleByTextHeightRunTime(Control, Dimension);
  367. end
  368. else
  369. begin
  370. // that's our design text-size, we do not expect any other value
  371. Assert(TextHeight = OurDesignTimeTextHeight);
  372. Result := ScaleByTextHeightImpl(Control, Dimension, TextHeight);
  373. end;
  374. end;
  375. end;
  376. procedure GetFormScaleRatio(Form: TForm; var M, D: Integer);
  377. begin
  378. M := Form.CalculateTextHeight;
  379. D := Form.RetrieveTextHeight;
  380. end;
  381. // this differs from ScaleByTextHeight only by enforcing
  382. // constant design-time text height
  383. function ScaleByTextHeightRunTime(Control: TControl; Dimension: Integer): Integer;
  384. begin
  385. Result := ScaleByTextHeightImpl(Control, Dimension, OurDesignTimeTextHeight);
  386. end;
  387. function GetSystemMetricsForControl(Control: TControl; nIndex: Integer): Integer;
  388. begin
  389. if Assigned(GetSystemMetricsForDpi) then
  390. begin
  391. Result := GetSystemMetricsForDpi(nIndex, GetControlPixelsPerInch(Control))
  392. end
  393. else
  394. begin
  395. Result := GetSystemMetrics(nIndex);
  396. end;
  397. end;
  398. var
  399. ShellImageLists: TDictionary<Integer, TImageList> = nil;
  400. procedure InitializeShellImageLists;
  401. type
  402. TSHGetImageList = function (iImageList: integer; const riid: TGUID; var ppv: Pointer): hResult; stdcall;
  403. const
  404. IID_IImageList: TGUID = '{46EB5926-582E-4017-9FDF-E8998DAA0950}';
  405. var
  406. Lib: THandle;
  407. ImageList: Integer;
  408. Handle: THandle;
  409. Height, Width: Integer;
  410. ShellImageList: TImageList;
  411. SHGetImageList: TSHGetImageList;
  412. begin
  413. Lib := LoadLibrary('shell32');
  414. SHGetImageList := GetProcAddress(Lib, 'SHGetImageList');
  415. ShellImageLists := TDictionary<Integer, TImageList>.Create;
  416. for ImageList := 0 to SHIL_LAST do
  417. begin
  418. // VCL have declaration for SHGetImageList in ShellAPI, but it does not link
  419. if (SHGetImageList(ImageList, IID_IImageList, Pointer(Handle)) = S_OK) and
  420. ImageList_GetIconSize(Handle, Width, Height) then
  421. begin
  422. // We could use AddOrSetValue instead, but to be on a safe siz, we prefer e.g. SHIL_SMALL over SHIL_SYSSMALL,
  423. // while they actually can be the same
  424. if not ShellImageLists.ContainsKey(Width) then
  425. begin
  426. ShellImageList := TImageList.Create(Application);
  427. ShellImageList.Handle := Handle;
  428. ShellImageList.ShareImages := True;
  429. ShellImageList.DrawingStyle := dsTransparent;
  430. ShellImageLists.Add(Width, ShellImageList);
  431. end;
  432. end;
  433. end;
  434. end;
  435. function ShellImageListForControl(Control: TControl; Size: TImageListSize): TImageList;
  436. var
  437. ImageListPair: TPair<Integer, TImageList>;
  438. Width, ImageListWidth: Integer;
  439. Diff, BestDiff: Integer;
  440. begin
  441. // Delay load image lists, not to waste resources in console/scripting mode
  442. if ShellImageLists = nil then
  443. begin
  444. InitializeShellImageLists;
  445. end;
  446. case Size of
  447. ilsSmall: Width := 16;
  448. ilsLarge: Width := 32;
  449. else Width := 0; Assert(False);
  450. end;
  451. Width := ScaleByPixelsPerInch(Width, Control);
  452. Result := nil;
  453. BestDiff := -1;
  454. for ImageListPair in ShellImageLists do
  455. begin
  456. ImageListWidth := ImageListPair.Key;
  457. if ImageListWidth <= Width then
  458. begin
  459. Diff := Width - ImageListWidth;
  460. end
  461. else
  462. begin
  463. // Prefer smaller images over larger, so for 150%, we use 100% images, not 200%
  464. Diff := ImageListWidth - Width + 1;
  465. end;
  466. if (BestDiff < 0) or (BestDiff > Diff) then
  467. begin
  468. BestDiff := Diff;
  469. Result := ImageListPair.Value;
  470. end;
  471. end;
  472. end;
  473. type
  474. TListViewHelper = class helper for TCustomListView
  475. public
  476. function HasMemStream: Boolean;
  477. end;
  478. function TListViewHelper.HasMemStream: Boolean;
  479. begin
  480. Result := Assigned(Self.FMemStream);
  481. end;
  482. type
  483. TTreeViewHelper = class helper for TCustomTreeView
  484. public
  485. function HasMemStream: Boolean;
  486. end;
  487. function TTreeViewHelper.HasMemStream: Boolean;
  488. begin
  489. Result := Assigned(Self.FMemStream);
  490. end;
  491. type
  492. TRichEditHelper = class helper for TCustomRichEdit
  493. public
  494. function HasMemStream: Boolean;
  495. end;
  496. function TRichEditHelper.HasMemStream: Boolean;
  497. begin
  498. Result := Assigned(Self.FMemStream);
  499. end;
  500. function ControlHasRecreationPersistenceData(Control: TControl): Boolean;
  501. begin
  502. // not implemented for this class as we do not use it as of now
  503. Assert(not (Control is TCustomComboBoxEx));
  504. Result :=
  505. ((Control is TCustomListView) and (Control as TCustomListView).HasMemStream) or
  506. ((Control is TCustomTreeView) and (Control as TCustomTreeView).HasMemStream) or
  507. ((Control is TCustomRichEdit) and (Control as TCustomRichEdit).HasMemStream);
  508. end;
  509. type
  510. TApplicationHelper = class helper for TApplication
  511. public
  512. function IsAppIconic: Boolean;
  513. procedure SetAppIconic(Value: Boolean);
  514. procedure SetMainForm(Value: TForm);
  515. procedure SetTerminated(Value: Boolean);
  516. end;
  517. function TApplicationHelper.IsAppIconic: Boolean;
  518. begin
  519. Result := Self.FAppIconic;
  520. end;
  521. procedure TApplicationHelper.SetAppIconic(Value: Boolean);
  522. begin
  523. Self.FAppIconic := Value;
  524. end;
  525. procedure TApplicationHelper.SetMainForm(Value: TForm);
  526. begin
  527. Self.FMainForm := Value;
  528. end;
  529. procedure TApplicationHelper.SetTerminated(Value: Boolean);
  530. begin
  531. Self.FTerminate := Value;
  532. end;
  533. function IsAppIconic: Boolean;
  534. begin
  535. Result := Application.IsAppIconic;
  536. end;
  537. procedure SetAppIconic(Value: Boolean);
  538. begin
  539. Application.SetAppIconic(Value);
  540. end;
  541. procedure SetAppMainForm(Value: TForm);
  542. begin
  543. Application.SetMainForm(Value);
  544. end;
  545. procedure SetAppTerminated(Value: Boolean);
  546. begin
  547. Application.SetTerminated(Value);
  548. end;
  549. function ApiPath(Path: string): string;
  550. begin
  551. Result := Path;
  552. if Assigned(OnApiPath) then
  553. begin
  554. Result := OnApiPath(Result);
  555. end;
  556. end;
  557. procedure ForceColorChange(Control: TWinControl);
  558. begin
  559. // particularly when changing color back to default (clWindow),
  560. // non-client area (border line) is not redrawn,
  561. // keeping previous color. force redraw here
  562. if Control.HandleAllocated then
  563. begin
  564. RedrawWindow(Control.Handle, nil, 0, RDW_INVALIDATE or RDW_FRAME);
  565. end;
  566. end;
  567. { TCustomControlScrollOnDragOver }
  568. constructor TCustomControlScrollOnDragOver.Create(Control: TControl;
  569. ScheduleDragOver: Boolean);
  570. begin
  571. FControl := Control;
  572. FOnBeforeUpdate := nil;
  573. FOnAfterUpdate := nil;
  574. if ScheduleDragOver then
  575. begin
  576. FDragOverTimer := TTimer.Create(Control);
  577. FDragOverTimer.Enabled := False;
  578. FDragOverTimer.Interval := 50;
  579. FDragOverTimer.OnTimer := DragOverTimer;
  580. end
  581. else FDragOverTimer := nil;
  582. end;
  583. destructor TCustomControlScrollOnDragOver.Destroy;
  584. begin
  585. FreeAndNil(FDragOverTimer);
  586. end;
  587. procedure TCustomControlScrollOnDragOver.DragOverTimer(Sender: TObject);
  588. var
  589. P: TPoint;
  590. begin
  591. P := FControl.ScreenToClient(Mouse.CursorPos);
  592. if (P.X >= 0) and (P.X < FControl.Width) and
  593. (P.Y >= 0) and (P.Y < FControl.Height) then
  594. begin
  595. DragOver(P);
  596. end;
  597. end;
  598. procedure TCustomControlScrollOnDragOver.StartDrag;
  599. begin
  600. GetSystemTimeAsFileTime(FDragOverTime);
  601. GetSystemTimeAsFileTime(FLastVScrollTime);
  602. FVScrollCount := 0;
  603. if Assigned(FDragOverTimer) then
  604. FDragOverTimer.Enabled := True;
  605. end;
  606. procedure TCustomControlScrollOnDragOver.EndDrag;
  607. begin
  608. if Assigned(FDragOverTimer) then
  609. FDragOverTimer.Enabled := False;
  610. end;
  611. type
  612. TPublicControl = class(TControl);
  613. procedure TCustomControlScrollOnDragOver.BeforeUpdate(ObjectToValidate: TObject);
  614. var
  615. DragImages: TDragImageList;
  616. begin
  617. if Assigned(FOnBeforeUpdate) then
  618. FOnBeforeUpdate(ObjectToValidate);
  619. DragImages := TPublicControl(FControl).GetDragImages;
  620. if Assigned(DragImages) then
  621. DragImages.HideDragImage;
  622. end;
  623. procedure TCustomControlScrollOnDragOver.AfterUpdate;
  624. var
  625. DragImages: TDragImageList;
  626. begin
  627. if Assigned(FOnAfterUpdate) then
  628. FOnAfterUpdate;
  629. DragImages := TPublicControl(FControl).GetDragImages;
  630. if Assigned(DragImages) then
  631. DragImages.ShowDragImage;
  632. end;
  633. procedure TTreeViewScrollOnDragOver.StartDrag;
  634. var
  635. KeyBoardState : TKeyBoardState;
  636. begin
  637. inherited;
  638. FLastDragNode := nil;
  639. if (GetKeyState(VK_SPACE) <> 0) and GetKeyboardState(KeyBoardState) then
  640. begin
  641. KeyBoardState[VK_SPACE] := 0;
  642. SetKeyBoardState(KeyBoardState);
  643. end;
  644. GetSystemTimeAsFileTime(FLastHScrollTime);
  645. end;
  646. { TTreeViewScrollOnDragOver }
  647. procedure TTreeViewScrollOnDragOver.DragOver(Point: TPoint);
  648. var
  649. TreeView: TCustomTreeView;
  650. NbPixels: Integer;
  651. KnowTime: TFileTime;
  652. Node: TTreeNode;
  653. TempTopItem: TTreeNode;
  654. ScrollInfo: TScrollInfo;
  655. KeyBoardState : TKeyBoardState;
  656. begin
  657. TreeView := (FControl as TCustomTreeView);
  658. Node := TreeView.GetNodeAt(Point.X, Point.Y);
  659. if Assigned(Node) then
  660. begin
  661. GetSystemTimeAsFileTime(KnowTime);
  662. if GetKeyState(VK_SPACE) = 0 then
  663. begin
  664. {Expand node after 2.5 seconds: }
  665. if not Assigned(FLastDragNode) or (FLastDragNode <> Node) then
  666. begin
  667. {not previous droptarget: start timer}
  668. GetSystemTimeAsFileTime(FDragOverTime);
  669. FLastDragNode := Node
  670. end
  671. else
  672. begin
  673. if ((Int64(KnowTime) - Int64(FDragOverTime)) > DDExpandDelay) then
  674. begin
  675. TempTopItem := TreeView.TopItem;
  676. BeforeUpdate(nil);
  677. Node.Expand(False);
  678. TreeView.TopItem := TempTopItem;
  679. TreeView.Update;
  680. AfterUpdate;
  681. FDragOverTime := KnowTime;
  682. end;
  683. end;
  684. end
  685. else
  686. begin
  687. {restart timer}
  688. GetSystemTimeAsFileTime(FDragOverTime);
  689. if GetKeyboardState(KeyBoardState) then
  690. begin
  691. KeyBoardState[VK_Space] := 0;
  692. SetKeyBoardState(KeyBoardState);
  693. end;
  694. TempTopItem := TreeView.TopItem;
  695. BeforeUpdate(Node);
  696. if Node.Expanded then
  697. begin
  698. if not TreeView.Selected.HasAsParent(Node) then
  699. Node.Collapse(False);
  700. end
  701. else Node.Expand(False);
  702. TreeView.TopItem := TempTopItem;
  703. TreeView.Update;
  704. AfterUpdate;
  705. end;
  706. NbPixels := Abs(TTreeView(FControl).Font.Height);
  707. {Vertical treescrolling:}
  708. if ((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  709. ((FVScrollCount > 3) and
  710. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay Div 4))) then
  711. begin
  712. {Scroll tree up, if droptarget is topitem:}
  713. if Node = TreeView.TopItem then
  714. begin
  715. BeforeUpdate(nil);
  716. TreeView.Perform(WM_VSCROLL, SB_LINEUP, 0);
  717. AfterUpdate;
  718. GetSystemTimeAsFileTime(FLastVScrollTime);
  719. Inc(FVScrollCount);
  720. end
  721. else
  722. {Scroll tree down, if next visible item of droptarget is not visible:}
  723. begin
  724. if Point.Y + 3 * nbPixels > TreeView.Height then
  725. begin
  726. BeforeUpdate(nil);
  727. TreeView.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  728. AfterUpdate;
  729. GetSystemTimeAsFileTime(FLastVScrollTime);
  730. Inc(FVScrollCount);
  731. end
  732. else
  733. begin
  734. FVScrollCount := 0;
  735. end;
  736. end;
  737. end; {VScrollDelay}
  738. {Horizontal treescrolling:}
  739. {Scroll tree Left}
  740. if ((Int64(KnowTime) - Int64(FLastHScrollTime)) > DDHScrollDelay) then
  741. begin
  742. GetSystemTimeAsFileTime(FLastHScrollTime);
  743. ScrollInfo.cbSize := SizeOf(ScrollInfo);
  744. ScrollInfo.FMask := SIF_ALL;
  745. GetScrollInfo(TreeView.Handle, SB_HORZ, ScrollInfo);
  746. if ScrollInfo.nMin <> ScrollInfo.nMax then
  747. begin
  748. if Point.X < 50 then
  749. begin
  750. if Node.DisplayRect(True).Right + 50 < TreeView.Width then
  751. begin
  752. BeforeUpdate(nil);
  753. TreeView.Perform(WM_HSCROLL, SB_LINELEFT, 0);
  754. AfterUpdate;
  755. end;
  756. end
  757. else
  758. if Point.X > (TreeView.Width - 50) then
  759. begin
  760. if Node.DisplayRect(True).Left > 50 then
  761. begin
  762. BeforeUpdate(nil);
  763. TreeView.Perform(WM_HSCROLL, SB_LINERIGHT, 0);
  764. AfterUpdate;
  765. end;
  766. end;
  767. end;
  768. end;
  769. end;
  770. end;
  771. { TListViewScrollOnDragOver }
  772. procedure TListViewScrollOnDragOver.DragOver(Point: TPoint);
  773. var
  774. ListView: TCustomListView;
  775. KnowTime: TFileTime;
  776. NbPixels: Integer;
  777. WParam: LongInt;
  778. begin
  779. ListView := (FControl as TCustomListView);
  780. GetSystemTimeAsFileTime(KnowTime);
  781. NbPixels := Abs(TListView(ListView).Font.Height);
  782. {Vertical scrolling, if viewstyle = vsReport:}
  783. if (TListView(ListView).ViewStyle = vsReport) and Assigned(ListView.TopItem) and
  784. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  785. ((FVScrollCount > DDMaxSlowCount) and
  786. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  787. begin
  788. if (Point.Y - 3 * nbPixels <= 0) and (ListView.TopItem.Index > 0) then WParam := SB_LINEUP
  789. else
  790. if (Point.Y + 3 * nbPixels > ListView.Height) then WParam := SB_LINEDOWN
  791. else WParam := -1;
  792. if WParam >= 0 then
  793. begin
  794. BeforeUpdate(nil);
  795. ListView.Perform(WM_VSCROLL, WParam, 0);
  796. if FVScrollCount > DDMaxSlowCount then
  797. ListView.Perform(WM_VSCROLL, WParam, 0);
  798. if FVScrollCount > DDMaxSlowCount * 3 then
  799. ListView.Perform(WM_VSCROLL, WParam, 0);
  800. ListView.Update;
  801. AfterUpdate;
  802. GetSystemTimeAsFileTime(FLastVScrollTime);
  803. Inc(FVScrollCount);
  804. end
  805. else FVScrollCount := 0;
  806. end;
  807. end;
  808. { TListBoxScrollOnDragOver }
  809. procedure TListBoxScrollOnDragOver.DragOver(Point: TPoint);
  810. var
  811. ListBox: TListBox;
  812. KnowTime: TFileTime;
  813. NbPixels: Integer;
  814. WParam: LongInt;
  815. begin
  816. ListBox := (FControl as TListBox);
  817. GetSystemTimeAsFileTime(KnowTime);
  818. NbPixels := Abs(ListBox.Font.Height);
  819. {Vertical scrolling, if viewstyle = vsReport:}
  820. if (ListBox.Items.Count > 0) and
  821. (((Int64(KnowTime) - Int64(FLastVScrollTime)) > DDVScrollDelay) or
  822. ((FVScrollCount > DDMaxSlowCount) and
  823. ((Int64(KnowTime) - Int64(FLastVScrollTime)) > (DDVScrollDelay div 4)))) then
  824. begin
  825. if (Point.Y - 3 * nbPixels <= 0) and (ListBox.TopIndex > 0) then WParam := SB_LINEUP
  826. else
  827. if (Point.Y + 3 * nbPixels > ListBox.Height) then WParam := SB_LINEDOWN
  828. else WParam := -1;
  829. if WParam >= 0 then
  830. begin
  831. BeforeUpdate(nil);
  832. ListBox.Perform(WM_VSCROLL, WParam, 0);
  833. if FVScrollCount > DDMaxSlowCount then
  834. ListBox.Perform(WM_VSCROLL, WParam, 0);
  835. if FVScrollCount > DDMaxSlowCount * 3 then
  836. ListBox.Perform(WM_VSCROLL, WParam, 0);
  837. ListBox.Update;
  838. AfterUpdate;
  839. GetSystemTimeAsFileTime(FLastVScrollTime);
  840. Inc(FVScrollCount);
  841. end
  842. else FVScrollCount := 0;
  843. end;
  844. end;
  845. function IsUncPath(Path: string): Boolean;
  846. begin
  847. Result := (Copy(Path, 1, 2) = '\\') or (Copy(Path, 1, 2) = '//');
  848. end;
  849. var
  850. Lib: THandle;
  851. initialization
  852. Lib := LoadLibrary('shcore');
  853. if Lib <> 0 then
  854. begin
  855. GetDpiForMonitor := GetProcAddress(Lib, 'GetDpiForMonitor');
  856. end;
  857. Lib := LoadLibrary('user32');
  858. if Lib <> 0 then
  859. begin
  860. GetSystemMetricsForDpi := GetProcAddress(Lib, 'GetSystemMetricsForDpi');
  861. SystemParametersInfoForDpi := GetProcAddress(Lib, 'SystemParametersInfoForDpi');
  862. end;
  863. finalization
  864. // No need to release individual image lists as they are owned by Application object.
  865. FreeAndNil(ShellImageLists);
  866. end.