NewCtrls.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. unit NewCtrls;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Previously this unit had RTL-capable versions of standard controls
  8. But now standard controls are RTL-capable already, and there's not much code left here
  9. Define VCLSTYLES to include an improved version TButtonStyleHook.DrawButton for command
  10. link buttons
  11. }
  12. interface
  13. uses
  14. Windows, SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs,
  15. {$IFDEF VCLSTYLES} Vcl.Themes, {$ELSE} Themes, {$ENDIF}
  16. StdCtrls, ExtCtrls;
  17. type
  18. TNewEdit = class(TEdit);
  19. TNewPathEdit = class(TNewEdit)
  20. protected
  21. procedure CreateWnd; override;
  22. public
  23. constructor Create(AOwner: TComponent); override;
  24. end;
  25. TNewMemo = class(TMemo);
  26. TNewComboBox = class(TComboBox);
  27. TNewListBox = class(TListBox);
  28. TNewButton = class(TButton)
  29. private
  30. class constructor Create;
  31. class destructor Destroy;
  32. protected
  33. procedure CreateParams(var Params: TCreateParams); override;
  34. public
  35. function AdjustHeightIfCommandLink: Integer;
  36. end;
  37. TNewButtonStyleHook = class(TButtonStyleHook)
  38. {$IFDEF VCLSTYLES}
  39. private
  40. class function DrawOrMeasureCommandLink(const Draw: Boolean;
  41. const Control: TNewButton; const LStyle: TCustomStyleServices; const FPressed: Boolean;
  42. const ACanvas: TCanvas; const AMouseInControl: Boolean): Integer;
  43. class function GetIdealHeightIfCommandLink(const Control: TNewButton; const Style: TCustomStyleServices): Integer;
  44. protected
  45. procedure DrawButton(ACanvas: TCanvas; AMouseInControl: Boolean); override;
  46. {$ENDIF}
  47. end;
  48. TNewCheckBox = class(TCheckBox);
  49. TNewRadioButton = class(TRadioButton);
  50. TNewLinkLabel = class(TLinkLabel)
  51. public
  52. function AdjustHeight: Integer;
  53. end;
  54. procedure Register;
  55. implementation
  56. uses
  57. CommCtrl, ShLwApi,
  58. Types,
  59. BidiUtils, UnsignedFunc;
  60. procedure Register;
  61. begin
  62. RegisterComponents('JR', [TNewEdit, TNewPathEdit, TNewMemo, TNewComboBox, TNewListBox,
  63. TNewButton, TNewCheckBox, TNewRadioButton]);
  64. end;
  65. { TNewPathEdit }
  66. constructor TNewPathEdit.Create(AOwner: TComponent);
  67. begin
  68. inherited;
  69. ParentBidiMode := False;
  70. BidiMode := bdLeftToRight;
  71. end;
  72. procedure TNewPathEdit.CreateWnd;
  73. begin
  74. inherited;
  75. SHAutoComplete(Handle, SHACF_FILESYSTEM);
  76. end;
  77. { TNewButton }
  78. class constructor TNewButton.Create;
  79. begin
  80. TCustomStyleEngine.RegisterStyleHook(TNewButton, TNewButtonStyleHook);
  81. end;
  82. procedure TNewButton.CreateParams(var Params: TCreateParams);
  83. begin
  84. inherited;
  85. if (Style = bsCommandLink) and IsRightToLeft then begin
  86. { Command link buttons need to have WS_EX_LAYOUTRTL enabled for full RTL, in addition to
  87. WS_EX_RTLREADING and WS_EX_LEFTSCROLLBAR, but not WS_EX_RIGHT. This can be confirmed by
  88. inspecting the style of a task dialog command link button. However, if VCL Styles is
  89. active, this should not be done since the style hook does not expect it at all. }
  90. if not IsCustomStyleActive then
  91. Params.ExStyle := Params.ExStyle or WS_EX_LAYOUTRTL;
  92. end;
  93. end;
  94. class destructor TNewButton.Destroy;
  95. begin
  96. TCustomStyleEngine.UnregisterStyleHook(TNewButton, TNewButtonStyleHook);
  97. end;
  98. function TNewButton.AdjustHeightIfCommandLink: Integer;
  99. begin
  100. Result := 0;
  101. if Style = bsCommandLink then begin
  102. var OldHeight := Height;
  103. {$IFDEF VCLSTYLES}
  104. var LStyle := StyleServices(Self);
  105. if not LStyle.Enabled or LStyle.IsSystemStyle then
  106. LStyle := nil;
  107. if LStyle <> nil then begin
  108. const IdealHeight = TNewButtonStyleHook.GetIdealHeightIfCommandLink(Self, LStyle);
  109. if IdealHeight <> 0 then begin
  110. Height := IdealHeight;
  111. Exit(Height- OldHeight);
  112. end;
  113. end;
  114. {$ENDIF}
  115. var IdealSize: TSize;
  116. IdealSize.cx := Width;
  117. IdealSize.cy := 0; { Not needed according to docs and tests, but clearing anyway }
  118. if SendMessage(Handle, BCM_GETIDEALSIZE, 0, LPARAM(@IdealSize)) <> 0 then begin
  119. Height := IdealSize.cy;
  120. Result := Height - OldHeight;
  121. end;
  122. end;
  123. end;
  124. { TNewLinkLabel }
  125. function TNewLinkLabel.AdjustHeight: Integer;
  126. begin
  127. var OldHeight := Height;
  128. var IdealSize: TSize;
  129. SendMessage(Handle, LM_GETIDEALSIZE, Width, LPARAM(@IdealSize));
  130. Height := IdealSize.cy;
  131. Result := Height - OldHeight;
  132. end;
  133. {$IFDEF VCLSTYLES}
  134. { TNewButtonStyleHook - same as Vcl.StdCtrls' TButtonStyleHook except that for command links it:
  135. -Adds support for measuring height
  136. -Fixes RTL support for CommandLinkHint
  137. -Actually flips the text and icons on RTL
  138. -Improves alignment of shield icons, especially at high dpi
  139. -Avoids drawing empty notes
  140. -Respects the font of the control
  141. -Properly centers glyphs vertically on the first text line
  142. For other button styles it just calls the original code, and the code for those styles is not copied here }
  143. class function TNewButtonStyleHook.DrawOrMeasureCommandLink(const Draw: Boolean;
  144. const Control: TNewButton; const LStyle: TCustomStyleServices; const FPressed: Boolean;
  145. const ACanvas: TCanvas; const AMouseInControl: Boolean): Integer;
  146. var
  147. Details: TThemedElementDetails;
  148. LParentRect, DrawRect, R, RSingleLine: TRect;
  149. LIsRightToLeft: Boolean;
  150. IL: BUTTON_IMAGELIST;
  151. IW, IH: Integer;
  152. TextFormat: TTextFormatFlags;
  153. ThemeTextColor: TColor;
  154. Buffer: string;
  155. BufferLength: Integer;
  156. ImgIndex: Integer;
  157. BCaption: String;
  158. IsDefault: Boolean;
  159. IsElevationRequired: Boolean;
  160. LPPI: Integer;
  161. begin
  162. const Handle = Control.Handle;
  163. LPPI := Control.CurrentPPI;
  164. LParentRect := Control.ClientRect;
  165. LIsRightToLeft := Control.IsRightToLeft;
  166. BCaption := Control.Caption;
  167. ImgIndex := 0;
  168. IsDefault := Control.Active;
  169. IsElevationRequired := CheckWin32Version(6, 0) and Control.ElevationRequired;
  170. if not Control.Enabled then
  171. begin
  172. Details := LStyle.GetElementDetails(tbPushButtonDisabled);
  173. ImgIndex := 3;
  174. end
  175. else
  176. if FPressed then
  177. begin
  178. Details := LStyle.GetElementDetails(tbPushButtonPressed);
  179. ImgIndex := 2;
  180. end
  181. else if AMouseInControl then
  182. begin
  183. Details := LStyle.GetElementDetails(tbPushButtonHot);
  184. ImgIndex := 1;
  185. end
  186. else if Control.Focused or IsDefault then
  187. begin
  188. Details := LStyle.GetElementDetails(tbPushButtonDefaulted);
  189. ImgIndex := 4;
  190. end
  191. else if Control.Enabled then
  192. Details := LStyle.GetElementDetails(tbPushButtonNormal);
  193. DrawRect := LParentRect;
  194. if Draw then
  195. LStyle.DrawElement(ACanvas.Handle, Details, DrawRect);
  196. IW := MulDiv(35, LPPI, Screen.DefaultPixelsPerInch);
  197. Inc(DrawRect.Left, IW);
  198. Inc(DrawRect.Top, 15);
  199. Inc(DrawRect.Left, 5);
  200. ACanvas.Font := Control.Font;
  201. R := DrawRect;
  202. TextFormat := TTextFormatFlags(UDrawTextBiDiModeFlags(Control, DT_LEFT or DT_WORDBREAK or DT_CALCRECT));
  203. LStyle.DrawText(ACanvas.Handle, Details, BCaption, R, TextFormat, ACanvas.Font.Color); { R is used directly below for measuring, and later also for the note }
  204. Result := R.Bottom;
  205. if Draw then begin
  206. RSingleLine := DrawRect;
  207. TextFormat := TTextFormatFlags(UDrawTextBiDiModeFlags(Control, DT_LEFT or DT_SINGLELINE or DT_CALCRECT));
  208. LStyle.DrawText(ACanvas.Handle, Details, BCaption, RSingleLine, TextFormat, ACanvas.Font.Color); { RSingleLine is used below for the glyphs }
  209. { Following does not use any DT_CALCRECT results }
  210. TextFormat := TTextFormatFlags(UDrawTextBiDiModeFlags(Control, DT_LEFT or DT_WORDBREAK));
  211. if (seFont in Control.StyleElements) and LStyle.GetElementColor(Details, ecTextColor, ThemeTextColor) then
  212. ACanvas.Font.Color := ThemeTextColor;
  213. var R2 := DrawRect;
  214. FlipRect(R2, LParentRect, LIsRightToLeft);
  215. LStyle.DrawText(ACanvas.Handle, Details, BCaption, R2, TextFormat, ACanvas.Font.Color);
  216. end;
  217. SetLength(Buffer, Button_GetNoteLength(Handle) + 1);
  218. if Length(Buffer) > 1 then
  219. begin
  220. BufferLength := Length(Buffer);
  221. if Button_GetNote(Handle, PChar(Buffer), BufferLength) then
  222. begin
  223. Inc(DrawRect.Top, R.Height + 2); { R is the DT_CALCRECT result }
  224. ACanvas.Font.Height := MulDiv(ACanvas.Font.Height, 2, 3);
  225. R := DrawRect;
  226. TextFormat := TTextFormatFlags(UDrawTextBiDiModeFlags(Control, DT_LEFT or DT_WORDBREAK or DT_CALCRECT));
  227. LStyle.DrawText(ACanvas.Handle, Details, Buffer, R, TextFormat, ACanvas.Font.Color); { R is used directly below for measuring }
  228. if R.Bottom > Result then
  229. Result := R.Bottom;
  230. if Draw then begin
  231. { Following does not use any DT_CALCRECT results }
  232. TextFormat := TTextFormatFlags(UDrawTextBiDiModeFlags(Control, DT_LEFT or DT_WORDBREAK));
  233. FlipRect(DrawRect, LParentRect, LIsRightToLeft);
  234. LStyle.DrawText(ACanvas.Handle, Details, Buffer, DrawRect, TextFormat, ACanvas.Font.Color);
  235. end;
  236. end;
  237. end;
  238. Inc(Result, 15);
  239. if not Draw then
  240. Exit;
  241. if Button_GetImageList(handle, IL) and (IL.himl <> 0) and
  242. ImageList_GetIconSize(IL.himl, IW, IH) then
  243. begin
  244. R.Left := 2;
  245. R.Top := RSingleLine.Top + (RSingleLine.Height - IH) div 2;
  246. if IsElevationRequired then
  247. begin
  248. ImgIndex := 0;
  249. Inc(R.Left, MulDiv(8, LPPI, Screen.DefaultPixelsPerInch));
  250. end;
  251. R.Right := R.Left + IW;
  252. R.Bottom := R.Top + IH;
  253. if Draw then begin
  254. FlipRect(R, LParentRect, LIsRightToLeft);
  255. ImageList_Draw(IL.himl, ImgIndex, ACanvas.Handle, R.Left, R.Top, ILD_NORMAL);
  256. end;
  257. end else begin
  258. if not Control.Enabled then
  259. Details := LStyle.GetElementDetails(tbCommandLinkGlyphDisabled)
  260. else
  261. if FPressed then
  262. Details := LStyle.GetElementDetails(tbCommandLinkGlyphPressed)
  263. else if Control.Focused then
  264. Details := LStyle.GetElementDetails(tbCommandLinkGlyphDefaulted)
  265. else if AMouseInControl then
  266. Details := LStyle.GetElementDetails(tbCommandLinkGlyphHot)
  267. else
  268. Details := LStyle.GetElementDetails(tbCommandLinkGlyphNormal);
  269. DrawRect.Right := IW;
  270. DrawRect.Left := 3;
  271. DrawRect.Top := RSingleLine.Top + (RSingleLine.Height - IW) div 2;
  272. DrawRect.Bottom := DrawRect.Top + IW;
  273. if Draw then begin
  274. if LIsRightToLeft then begin
  275. FlipRect(DrawRect, LParentRect, True);
  276. var FlipBitmap := TBitmap.Create;
  277. try
  278. FlipBitmap.Width := DrawRect.Width;
  279. FlipBitmap.Height := DrawRect.Height;
  280. BitBlt(FlipBitmap.Canvas.Handle, 0, 0, DrawRect.Width, DrawRect.Height, ACanvas.Handle, DrawRect.Left, DrawRect.Top, SRCCOPY);
  281. LStyle.DrawElement(FlipBitmap.Canvas.Handle, Details, Rect(0, 0, DrawRect.Width, DrawRect.Height), nil, LPPI);
  282. StretchBlt(ACanvas.Handle, DrawRect.Left, DrawRect.Top, DrawRect.Width, DrawRect.Height,
  283. FlipBitmap.Canvas.Handle, FlipBitmap.Width-1, 0, -FlipBitmap.Width, FlipBitmap.Height, SRCCOPY);
  284. finally
  285. FlipBitmap.Free;
  286. end;
  287. end else
  288. LStyle.DrawElement(ACanvas.Handle, Details, DrawRect, nil, LPPI);
  289. end;
  290. end;
  291. end;
  292. class function TNewButtonStyleHook.GetIdealHeightIfCommandLink(const Control: TNewButton; const Style: TCustomStyleServices): Integer;
  293. begin
  294. const Canvas = TCanvas.Create;
  295. try
  296. Canvas.Handle := GetDC(0);
  297. try
  298. Result := DrawOrMeasureCommandLink(False, Control, Style, False, Canvas, False);
  299. finally
  300. ReleaseDC(0, Canvas.Handle);
  301. end;
  302. finally
  303. Canvas.Handle := 0;
  304. Canvas.Free;
  305. end;
  306. end;
  307. procedure TNewButtonStyleHook.DrawButton(ACanvas: TCanvas; AMouseInControl: Boolean);
  308. begin
  309. const LControlStyle = GetWindowLong(Handle, GWL_STYLE);
  310. if (LControlStyle and BS_COMMANDLINK) <> BS_COMMANDLINK then begin
  311. inherited;
  312. Exit;
  313. end;
  314. DrawOrMeasureCommandLink(True, TNewButton(Control), StyleServices, FPressed, ACanvas, AMouseInControl);
  315. end;
  316. {$ENDIF}
  317. end.