SetupForm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. unit SetupForm;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2024 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. TSetupForm
  8. }
  9. interface
  10. {$I VERSION.INC}
  11. uses
  12. Windows, SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs,
  13. UIStateForm, MsgIDs;
  14. type
  15. TSetupForm = class(TUIStateForm)
  16. private
  17. FBaseUnitX, FBaseUnitY: Integer;
  18. FRightToLeft: Boolean;
  19. FFlipControlsOnShow: Boolean;
  20. FSizeAndCenterOnShow: Boolean;
  21. FControlsFlipped: Boolean;
  22. FKeepSizeY: Boolean;
  23. procedure WMQueryEndSession(var Message: TWMQueryEndSession); message WM_QUERYENDSESSION;
  24. protected
  25. procedure Center;
  26. procedure CenterInsideControl(const Ctl: TWinControl;
  27. const InsideClientArea: Boolean);
  28. procedure CenterInsideRect(const InsideRect: TRect);
  29. procedure CreateParams(var Params: TCreateParams); override;
  30. procedure CreateWnd; override;
  31. procedure FlipControlsIfNeeded;
  32. procedure SizeAndCenterIfNeeded(const ACenterInsideControl: Boolean;
  33. const CenterInsideControlCtl: TWinControl;
  34. const CenterInsideControlInsideClientArea: Boolean);
  35. procedure VisibleChanging; override;
  36. procedure WndProc(var Message: TMessage); override;
  37. public
  38. constructor Create(AOwner: TComponent); override;
  39. constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
  40. function CalculateButtonWidth(const ButtonCaptions: array of String): Integer;
  41. procedure InitializeFont;
  42. function ScalePixelsX(const N: Integer): Integer;
  43. function ScalePixelsY(const N: Integer): Integer;
  44. function ShouldSizeX: Boolean;
  45. function ShouldSizeY: Boolean;
  46. procedure FlipSizeAndCenterIfNeeded(const ACenterInsideControl: Boolean = False;
  47. const CenterInsideControlCtl: TWinControl = nil;
  48. const CenterInsideControlInsideClientArea: Boolean = False); virtual;
  49. property BaseUnitX: Integer read FBaseUnitX;
  50. property BaseUnitY: Integer read FBaseUnitY;
  51. published
  52. property ControlsFlipped: Boolean read FControlsFlipped;
  53. property FlipControlsOnShow: Boolean read FFlipControlsOnShow write FFlipControlsOnShow;
  54. property KeepSizeY: Boolean read FKeepSizeY write FKeepSizeY;
  55. property RightToLeft: Boolean read FRightToLeft;
  56. property SizeAndCenterOnShow: Boolean read FSizeAndCenterOnShow write FSizeAndCenterOnShow;
  57. end;
  58. procedure CalculateBaseUnitsFromFont(const Font: TFont; var X, Y: Integer);
  59. function GetRectOfPrimaryMonitor(const WorkArea: Boolean): TRect;
  60. function SetFontNameSize(const AFont: TFont; const AName: String;
  61. const ASize: Integer; const AFallbackName: String;
  62. const AFallbackSize: Integer): Boolean;
  63. const
  64. OrigBaseUnitX = 6;
  65. OrigBaseUnitY = 13;
  66. implementation
  67. uses
  68. Generics.Collections, UITypes,
  69. CmnFunc2, Main, Msgs, BidiUtils;
  70. var
  71. WM_QueryCancelAutoPlay: UINT;
  72. function GetRectOfPrimaryMonitor(const WorkArea: Boolean): TRect;
  73. begin
  74. if not WorkArea or
  75. not SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0) then
  76. Result := Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
  77. end;
  78. function GetRectOfMonitorContainingRect(const R: TRect): TRect;
  79. { Returns bounding rectangle of monitor containing or nearest to R }
  80. type
  81. HMONITOR = type THandle;
  82. TMonitorInfo = record
  83. cbSize: DWORD;
  84. rcMonitor: TRect;
  85. rcWork: TRect;
  86. dwFlags: DWORD;
  87. end;
  88. const
  89. MONITOR_DEFAULTTONEAREST = $00000002;
  90. var
  91. Module: HMODULE;
  92. MonitorFromRect: function(const lprc: TRect; dwFlags: DWORD): HMONITOR; stdcall;
  93. GetMonitorInfo: function(hMonitor: HMONITOR; var lpmi: TMonitorInfo): BOOL; stdcall;
  94. M: HMONITOR;
  95. Info: TMonitorInfo;
  96. begin
  97. Module := GetModuleHandle(user32);
  98. MonitorFromRect := GetProcAddress(Module, 'MonitorFromRect');
  99. GetMonitorInfo := GetProcAddress(Module, 'GetMonitorInfoA');
  100. if Assigned(MonitorFromRect) and Assigned(GetMonitorInfo) then begin
  101. M := MonitorFromRect(R, MONITOR_DEFAULTTONEAREST);
  102. Info.cbSize := SizeOf(Info);
  103. if GetMonitorInfo(M, Info) then begin
  104. Result := Info.rcWork;
  105. Exit;
  106. end;
  107. end;
  108. Result := GetRectOfPrimaryMonitor(True);
  109. end;
  110. function SetFontNameSize(const AFont: TFont; const AName: String;
  111. const ASize: Integer; const AFallbackName: String;
  112. const AFallbackSize: Integer): Boolean;
  113. { Returns True if AName <> '' and it used AName as the font name,
  114. False otherwise. }
  115. function SizeToHeight(const S: Integer): Integer;
  116. begin
  117. Result := MulDiv(-S, Screen.PixelsPerInch, 72);
  118. end;
  119. begin
  120. Result := False;
  121. if AName <> '' then begin
  122. if FontExists(AName) then begin
  123. AFont.Name := AName;
  124. AFont.Height := SizeToHeight(ASize);
  125. Result := True;
  126. Exit;
  127. end;
  128. { Note: AFallbackName is not used if the user specified an empty string for
  129. AName because in that case they want the default font used always }
  130. if (AFallbackName <> '') and FontExists(AFallbackName) then begin
  131. AFont.Name := AFallbackName;
  132. AFont.Height := SizeToHeight(AFallbackSize);
  133. Exit;
  134. end;
  135. end;
  136. AFont.Name := GetPreferredUIFont;
  137. AFont.Height := SizeToHeight(AFallbackSize);
  138. end;
  139. procedure CalculateBaseUnitsFromFont(const Font: TFont; var X, Y: Integer);
  140. var
  141. DC: HDC;
  142. Size: TSize;
  143. TM: TTextMetric;
  144. begin
  145. DC := GetDC(0);
  146. try
  147. SelectObject(DC, Font.Handle);
  148. { Based on code from Q145994: }
  149. GetTextExtentPoint(DC,
  150. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 52, Size);
  151. X := (Size.cx div 26 + 1) div 2;
  152. GetTextMetrics(DC, TM);
  153. Y := TM.tmHeight;
  154. finally
  155. ReleaseDC(0, DC);
  156. end;
  157. end;
  158. procedure NewChangeScale(const Ctl: TControl; const XM, XD, YM, YD: Integer);
  159. var
  160. X, Y, W, H: Integer;
  161. begin
  162. X := MulDiv(Ctl.Left, XM, XD);
  163. Y := MulDiv(Ctl.Top, YM, YD);
  164. if not(csFixedWidth in Ctl.ControlStyle) then
  165. W := MulDiv(Ctl.Width, XM, XD)
  166. else
  167. W := Ctl.Width;
  168. if not(csFixedHeight in Ctl.ControlStyle) then
  169. H := MulDiv(Ctl.Height, YM, YD)
  170. else
  171. H := Ctl.Height;
  172. Ctl.SetBounds(X, Y, W, H);
  173. end;
  174. procedure NewScaleControls(const Ctl: TWinControl; const XM, XD, YM, YD: Integer);
  175. { This is like TControl.ScaleControls, except it allows the width and height
  176. to be scaled independently }
  177. var
  178. I: Integer;
  179. C: TControl;
  180. begin
  181. for I := 0 to Ctl.ControlCount-1 do begin
  182. C := Ctl.Controls[I];
  183. if C is TWinControl then begin
  184. TWinControl(C).DisableAlign;
  185. try
  186. NewScaleControls(TWinControl(C), XM, XD, YM, YD);
  187. NewChangeScale(C, XM, XD, YM, YD);
  188. finally
  189. TWinControl(C).EnableAlign;
  190. end;
  191. end
  192. else
  193. NewChangeScale(C, XM, XD, YM, YD);
  194. end;
  195. end;
  196. function GetParentSetupForm(AControl: TControl): TSetupForm;
  197. begin
  198. { Note: Unlike GetParentForm, this checks all levels, not just the top }
  199. repeat
  200. if AControl is TSetupForm then begin
  201. Result := TSetupForm(AControl);
  202. Exit;
  203. end;
  204. AControl := AControl.Parent;
  205. until AControl = nil;
  206. Result := nil;
  207. end;
  208. function IsParentSetupFormFlipped(AControl: TControl): Boolean;
  209. var
  210. ParentForm: TSetupForm;
  211. begin
  212. ParentForm := GetParentSetupForm(AControl);
  213. if Assigned(ParentForm) then
  214. Result := ParentForm.ControlsFlipped
  215. else
  216. Result := False;
  217. end;
  218. function IsParentSetupFormRightToLeft(AControl: TControl): Boolean;
  219. var
  220. ParentForm: TSetupForm;
  221. begin
  222. ParentForm := GetParentSetupForm(AControl);
  223. if Assigned(ParentForm) then
  224. Result := ParentForm.RightToLeft
  225. else
  226. Result := False;
  227. end;
  228. type
  229. TControlAnchorsListItem = TPair<TControl, TAnchors>;
  230. TControlAnchorsList = TDictionary<TControl, TAnchors>;
  231. TControlAccess = class(TControl);
  232. procedure StripAndStoreCustomAnchors(const Ctl: TControl; const AnchorsList: TControlAnchorsList);
  233. var
  234. I: Integer;
  235. begin
  236. if Ctl.Anchors <> [akLeft, akTop] then begin
  237. AnchorsList.Add(Ctl, Ctl.Anchors);
  238. { Before we can set Anchors to [akLeft, akTop] (which has a special
  239. 'no anchors' meaning to VCL), we first need to update the Explicit*
  240. properties so the control doesn't get moved back to an old position. }
  241. TControlAccess(Ctl).UpdateExplicitBounds;
  242. Ctl.Anchors := [akLeft, akTop];
  243. end;
  244. if Ctl is TWinControl then
  245. for I := 0 to TWinControl(Ctl).ControlCount-1 do
  246. StripAndStoreCustomAnchors(TWinControl(Ctl).Controls[I], AnchorsList);
  247. end;
  248. procedure RestoreAnchors(const Ctl: TControl; const AnchorsList: TControlAnchorsList);
  249. var
  250. I: TControlAnchorsListItem;
  251. begin
  252. { The order in which we restore the anchors shouldn't matter, so just
  253. enumerate the list. }
  254. for I in AnchorsList do
  255. I.Key.Anchors := I.Value;
  256. end;
  257. { TSetupForm }
  258. constructor TSetupForm.Create(AOwner: TComponent);
  259. begin
  260. { Must initialize FRightToLeft here in addition to CreateNew because
  261. CreateNew isn't virtual on Delphi 2 and 3 }
  262. FRightToLeft := LangOptions.RightToLeft;
  263. FFlipControlsOnShow := FRightToLeft;
  264. FSizeAndCenterOnShow := True;
  265. inherited;
  266. { In Delphi 2005 and later, Position defaults to poDefaultPosOnly, but we
  267. don't want the form to be changing positions whenever its handle is
  268. recreated, so change it to the D7 and earlier default of poDesigned. }
  269. if Position = poDefaultPosOnly then
  270. Position := poDesigned;
  271. end;
  272. constructor TSetupForm.CreateNew(AOwner: TComponent; Dummy: Integer = 0);
  273. begin
  274. { Note: On Delphi 2 and 3, CreateNew isn't virtual, so this is only reached
  275. when TSetupForm.CreateNew is called explicitly }
  276. FRightToLeft := LangOptions.RightToLeft;
  277. FFlipControlsOnShow := FRightToLeft;
  278. FSizeAndCenterOnShow := True;
  279. inherited;
  280. end;
  281. function TSetupForm.CalculateButtonWidth(const ButtonCaptions: array of String): Integer;
  282. var
  283. DC: HDC;
  284. I, W: Integer;
  285. begin
  286. Result := ScalePixelsX(75);
  287. { Increase the button size if there are unusually long button captions }
  288. DC := GetDC(0);
  289. try
  290. SelectObject(DC, Font.Handle);
  291. for I := Low(ButtonCaptions) to High(ButtonCaptions) do begin
  292. W := GetTextWidth(DC, ButtonCaptions[I], True) + ScalePixelsX(20);
  293. if Result < W then
  294. Result := W;
  295. end;
  296. finally
  297. ReleaseDC(0, DC);
  298. end;
  299. end;
  300. procedure TSetupForm.CenterInsideControl(const Ctl: TWinControl;
  301. const InsideClientArea: Boolean);
  302. var
  303. R: TRect;
  304. begin
  305. if not InsideClientArea then begin
  306. if GetWindowRect(Ctl.Handle, R) then
  307. CenterInsideRect(R);
  308. end
  309. else begin
  310. R := Ctl.ClientRect;
  311. MapWindowPoints(Ctl.Handle, 0, R, 2);
  312. CenterInsideRect(R);
  313. end;
  314. end;
  315. procedure TSetupForm.CenterInsideRect(const InsideRect: TRect);
  316. var
  317. R, MR: TRect;
  318. begin
  319. R := Bounds(InsideRect.Left + ((InsideRect.Right - InsideRect.Left) - Width) div 2,
  320. InsideRect.Top + ((InsideRect.Bottom - InsideRect.Top) - Height) div 2,
  321. Width, Height);
  322. { Clip to nearest monitor }
  323. MR := GetRectOfMonitorContainingRect(R);
  324. if R.Right > MR.Right then
  325. OffsetRect(R, MR.Right - R.Right, 0);
  326. if R.Bottom > MR.Bottom then
  327. OffsetRect(R, 0, MR.Bottom - R.Bottom);
  328. if R.Left < MR.Left then
  329. OffsetRect(R, MR.Left - R.Left, 0);
  330. if R.Top < MR.Top then
  331. OffsetRect(R, 0, MR.Top - R.Top);
  332. BoundsRect := R;
  333. end;
  334. procedure TSetupForm.Center;
  335. begin
  336. CenterInsideRect(GetRectOfPrimaryMonitor(True));
  337. end;
  338. procedure TSetupForm.CreateParams(var Params: TCreateParams);
  339. begin
  340. inherited;
  341. if FRightToLeft then
  342. Params.ExStyle := Params.ExStyle or (WS_EX_RTLREADING or WS_EX_LEFTSCROLLBAR or WS_EX_RIGHT);
  343. end;
  344. procedure TSetupForm.CreateWnd;
  345. begin
  346. inherited;
  347. if WM_QueryCancelAutoPlay <> 0 then
  348. AddToWindowMessageFilterEx(Handle, WM_QueryCancelAutoPlay);
  349. end;
  350. procedure TSetupForm.FlipControlsIfNeeded;
  351. begin
  352. if FFlipControlsOnShow then begin
  353. FFlipControlsOnShow := False;
  354. FControlsFlipped := not FControlsFlipped;
  355. FlipControls(Self);
  356. end;
  357. end;
  358. procedure TSetupForm.SizeAndCenterIfNeeded(const ACenterInsideControl: Boolean; const CenterInsideControlCtl: TWinControl; const CenterInsideControlInsideClientArea: Boolean);
  359. begin
  360. if FSizeAndCenterOnShow then begin
  361. FSizeAndCenterOnShow := False;
  362. { Apply custom initial size from script - depends on Anchors being set on all the controls }
  363. if ShouldSizeX then
  364. ClientWidth := MulDiv(ClientWidth, SetupHeader.WizardSizePercentX, 100);
  365. if ShouldSizeY then
  366. ClientHeight := MulDiv(ClientHeight, SetupHeader.WizardSizePercentY, 100);
  367. { Center }
  368. if ACenterInsideControl then
  369. CenterInsideControl(CenterInsideControlCtl, CenterInsideControlInsideClientArea)
  370. else
  371. Center;
  372. end;
  373. end;
  374. function TSetupForm.ShouldSizeX: Boolean;
  375. begin
  376. Result := SetupHeader.WizardSizePercentX > 100;
  377. end;
  378. function TSetupForm.ShouldSizeY: Boolean;
  379. begin
  380. Result := not FKeepSizeY and (SetupHeader.WizardSizePercentY > 100);
  381. end;
  382. procedure TSetupForm.FlipSizeAndCenterIfNeeded(const ACenterInsideControl: Boolean;
  383. const CenterInsideControlCtl: TWinControl; const CenterInsideControlInsideClientArea: Boolean);
  384. begin
  385. { Flipping must be done first because when flipping after sizing the flipping might get old info for anchors that didn't do their work yet. }
  386. FlipControlsIfNeeded;
  387. SizeAndCenterIfNeeded(ACenterInsideControl, CenterInsideControlCtl, CenterInsideControlInsideClientArea);
  388. end;
  389. procedure TSetupForm.InitializeFont;
  390. var
  391. ControlAnchorsList: TControlAnchorsList;
  392. W, H: Integer;
  393. R: TRect;
  394. begin
  395. { Note: Must keep the following lines in synch with ScriptFunc_R's
  396. InitializeScaleBaseUnits }
  397. SetFontNameSize(Font, LangOptions.DialogFontName, LangOptions.DialogFontSize,
  398. '', 8);
  399. CalculateBaseUnitsFromFont(Font, FBaseUnitX, FBaseUnitY);
  400. if (FBaseUnitX <> OrigBaseUnitX) or (FBaseUnitY <> OrigBaseUnitY) then begin
  401. ControlAnchorsList := TControlAnchorsList.Create;
  402. try
  403. { Custom anchors interfere with our scaling code, so strip them and restore
  404. afterwards. }
  405. StripAndStoreCustomAnchors(Self, ControlAnchorsList);
  406. { Loosely based on scaling code from TForm.ReadState: }
  407. NewScaleControls(Self, BaseUnitX, OrigBaseUnitX, BaseUnitY, OrigBaseUnitY);
  408. R := ClientRect;
  409. W := MulDiv(R.Right, FBaseUnitX, OrigBaseUnitX);
  410. H := MulDiv(R.Bottom, FBaseUnitY, OrigBaseUnitY);
  411. SetBounds(Left, Top, W + (Width - R.Right), H + (Height - R.Bottom));
  412. finally
  413. RestoreAnchors(Self, ControlAnchorsList);
  414. end;
  415. end;
  416. end;
  417. function TSetupForm.ScalePixelsX(const N: Integer): Integer;
  418. begin
  419. Result := MulDiv(N, BaseUnitX, OrigBaseUnitX);
  420. end;
  421. function TSetupForm.ScalePixelsY(const N: Integer): Integer;
  422. begin
  423. Result := MulDiv(N, BaseUnitY, OrigBaseUnitY);
  424. end;
  425. procedure TSetupForm.VisibleChanging;
  426. begin
  427. inherited;
  428. { Note: Unlike DoShow, any exceptions raised in VisibleChanging will be
  429. propagated out, which is what we want }
  430. if not Visible then
  431. FlipSizeAndCenterIfNeeded;
  432. end;
  433. procedure TSetupForm.WMQueryEndSession(var Message: TWMQueryEndSession);
  434. begin
  435. { TDummyClass.AntiShutdownHook in Setup.dpr already denies shutdown attempts
  436. but we also need to catch WM_QUERYENDSESSION here to suppress the VCL's
  437. default handling which calls CloseQuery. We do not want to let TMainForm &
  438. TNewDiskForm display any 'Exit Setup?' message boxes since we're already
  439. denying shutdown attempts, and also we can't allow them to potentially be
  440. displayed on top of another dialog box that's already displayed. }
  441. { Return zero, except if RestartInitiatedByThisProcess is set (which means
  442. we called RestartComputer previously) }
  443. if RestartInitiatedByThisProcess then
  444. Message.Result := 1;
  445. end;
  446. procedure TSetupForm.WndProc(var Message: TMessage);
  447. begin
  448. { When we receive a 'QueryCancelAutoPlay' message as a result of a new CD
  449. being inserted, return 1 to prevent it from being 'autoplayed'.
  450. Note: According to the docs, this message is only sent on Shell version
  451. 4.70 and later. }
  452. if (WM_QueryCancelAutoPlay <> 0) and (Message.Msg = WM_QueryCancelAutoPlay) then
  453. Message.Result := 1
  454. else
  455. inherited;
  456. end;
  457. initialization
  458. BidiUtils.IsParentFlippedFunc := IsParentSetupFormFlipped;
  459. BidiUtils.IsParentRightToLeftFunc := IsParentSetupFormRightToLeft;
  460. WM_QueryCancelAutoPlay := RegisterWindowMessage('QueryCancelAutoPlay');
  461. end.