PasTools.pas 28 KB

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