PasTools.pas 30 KB

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