bcmdbuttonfocus.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {******************************* CONTRIBUTOR(S) ******************************
  3. - Edivando S. Santos Brasil | [email protected]
  4. (Compatibility with delphi VCL 11/2018)
  5. ***************************** END CONTRIBUTOR(S) *****************************}
  6. unit BCMDButtonFocus;
  7. {$I bgracontrols.inc}
  8. // Set this to show number of repaint in each MDBUTTON
  9. { $DEFINE MDBUTTON_DEBUG}
  10. // Set this to animate only a MDBUTTON at a time
  11. { $DEFINE MDBUTTON_ANIMATEONLYONE}
  12. interface
  13. uses
  14. Classes, SysUtils, Types, {$IFDEF FPC}LCLType, LResources, LMessages,{$ENDIF}
  15. Forms, Controls, Graphics, Dialogs,
  16. {$IFNDEF FPC}Windows, Messages, BGRAGraphics, GraphType, FPImage, {$ENDIF}
  17. BCBaseCtrls, BGRABitmap, BGRABitmapTypes, ExtCtrls, Math, BGRABlend, BCMDButton;
  18. type
  19. { TCustomBCMDButtonFocus }
  20. TCustomBCMDButtonFocus = class(TBGRACustomCtrl)
  21. private
  22. FChecked: boolean;
  23. FKind: TBCMDButtonKind;
  24. {$IFDEF INDEBUG}
  25. FCount: integer;
  26. {$ENDIF}
  27. FRounding: integer;
  28. FTextAutoSize: boolean;
  29. FTextProportional: boolean;
  30. FTextProportionalRatio: single;
  31. FTimer: TTimer;
  32. FPercent: double;
  33. FCircleSize: double;
  34. FCX, FCY: integer;
  35. FAlphaPercent: double;
  36. FAlignment: TAlignment;
  37. FAnimation: boolean;
  38. FState: TBCMDButtonState;
  39. FStyleActive: TBCMDButtonStyle;
  40. FStyleDisabled: TBCMDButtonStyle;
  41. FStyleHover: TBCMDButtonStyle;
  42. FStyleNormal: TBCMDButtonStyle;
  43. FTextLayout: TTextLayout;
  44. procedure OnChangeStyle(Sender: TObject);
  45. procedure SetFAlignment(AValue: TAlignment);
  46. procedure SetFAnimation(AValue: boolean);
  47. procedure SetFChecked(AValue: boolean);
  48. procedure SetFKind(AValue: TBCMDButtonKind);
  49. procedure SetFStyleActive(AValue: TBCMDButtonStyle);
  50. procedure SetFStyleDisabled(AValue: TBCMDButtonStyle);
  51. procedure SetFStyleHover(AValue: TBCMDButtonStyle);
  52. procedure SetFStyleNormal(AValue: TBCMDButtonStyle);
  53. procedure SetFTextAutoSize(AValue: boolean);
  54. procedure SetFTextLayout(AValue: TTextLayout);
  55. procedure SetFTextProportional(AValue: boolean);
  56. procedure SetFTextProportionalRatio(AValue: single);
  57. protected
  58. // START / MDBUTTONFOCUS ONLY
  59. procedure WMSetFocus(var Message: {$IFDEF FPC}TLMSetFocus{$ELSE}TWMKillFocus{$ENDIF}); message {$IFDEF FPC}LM_SETFOCUS{$ELSE}WM_SETFOCUS{$ENDIF};
  60. procedure WMKillFocus(var Message: {$IFDEF FPC}TLMKillFocus{$ELSE}TWMKillFocus{$ENDIF}); message {$IFDEF FPC}LM_KILLFOCUS{$ELSE}WM_KILLFOCUS{$ENDIF};
  61. procedure UpdateFocus(AFocused: boolean);
  62. procedure KeyDown(var Key: word; Shift: TShiftState); override;
  63. procedure KeyUp(var Key: word; Shift: TShiftState); override;
  64. // END / MDBUTTONFOCUS ONLY
  65. procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
  66. {%H-}WithThemeSpace: boolean); override;
  67. procedure Paint; override;
  68. procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  69. X, Y: integer); override;
  70. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
  71. procedure MouseEnter; override;
  72. procedure MouseLeave; override;
  73. procedure RealSetText(const Value: TCaption); override;
  74. procedure OnTimer(Sender: TObject);
  75. procedure OnStartTimer(Sender: TObject);
  76. procedure OnStopTimer(Sender: TObject);
  77. function easeInOutQuad(t: double): double;
  78. function easeOutQuad(t: double): double;
  79. procedure UncheckOthers;
  80. class function GetControlClassDefaultSize: TSize;override;
  81. public
  82. constructor Create(AOwner: TComponent); override;
  83. destructor Destroy; override;
  84. procedure SelectAll;
  85. procedure UnselectAll;
  86. procedure InvertSelection;
  87. function GetSelected: TStringList;
  88. published
  89. property Animation: boolean read FAnimation write SetFAnimation default False;
  90. property Alignment: TAlignment read FAlignment write SetFAlignment default taCenter;
  91. property TextLayout: TTextLayout
  92. read FTextLayout write SetFTextLayout default tlCenter;
  93. property StyleNormal: TBCMDButtonStyle read FStyleNormal write SetFStyleNormal;
  94. property StyleHover: TBCMDButtonStyle read FStyleHover write SetFStyleHover;
  95. property StyleActive: TBCMDButtonStyle read FStyleActive write SetFStyleActive;
  96. property StyleDisabled: TBCMDButtonStyle read FStyleDisabled write SetFStyleDisabled;
  97. property Checked: boolean read FChecked write SetFChecked default False;
  98. property Kind: TBCMDButtonKind read FKind write SetFKind default mdbkNormal;
  99. // If text size is used to measure buttons
  100. // Disable it if you use the buttons in a grid, for example
  101. property TextAutoSize: boolean read FTextAutoSize write SetFTextAutoSize;
  102. // Enable it if you want that text size grows with height
  103. property TextProportional: boolean read FTextProportional write SetFTextProportional;
  104. // Each character font height proportional to height of control
  105. // Set it in conjunction with TextProportional, values recommended between 0...1
  106. property TextProportionalRatio: single read FTextProportionalRatio
  107. write SetFTextProportionalRatio;
  108. end;
  109. TBCMDButtonFocus = class(TCustomBCMDButtonFocus)
  110. property Action;
  111. property Align;
  112. property Anchors;
  113. property AutoSize;
  114. property BidiMode;
  115. property BorderSpacing;
  116. {$IFDEF FPC} //#
  117. property OnChangeBounds;
  118. {$ENDIF}
  119. //property Cancel;
  120. property Caption;
  121. property Color;
  122. property Constraints;
  123. //property Default;
  124. property DragCursor;
  125. property DragKind;
  126. property DragMode;
  127. property Enabled;
  128. property Font;
  129. property ParentBidiMode;
  130. //property ModalResult;
  131. property OnClick;
  132. property OnContextPopup;
  133. property OnDragDrop;
  134. property OnDragOver;
  135. property OnEndDrag;
  136. //property OnEnter;
  137. //property OnExit;
  138. //property OnKeyDown;
  139. //property OnKeyPress;
  140. //property OnKeyUp;
  141. property OnMouseDown;
  142. property OnMouseEnter;
  143. property OnMouseLeave;
  144. property OnMouseMove;
  145. property OnMouseUp;
  146. property OnMouseWheel;
  147. property OnMouseWheelDown;
  148. property OnMouseWheelUp;
  149. property OnResize;
  150. property OnStartDrag;
  151. //property OnUTF8KeyPress;
  152. property ParentFont;
  153. property ParentShowHint;
  154. property PopupMenu;
  155. property ShowHint;
  156. //property TabOrder;
  157. //property TabStop;
  158. property Visible;
  159. end;
  160. {$IFDEF FPC}procedure Register;{$ENDIF}
  161. implementation
  162. {$IFDEF MDBUTTON_ANIMATEONLYONE}
  163. var
  164. MDAnimating: TCustomMDButtonFocus;
  165. {$ENDIF}
  166. {$IFDEF FPC}
  167. procedure Register;
  168. begin
  169. RegisterComponents('BGRA Button Controls', [TBCMDButtonFocus]);
  170. end;
  171. {$ENDIF}
  172. { TCustomBCMDButtonFocus }
  173. procedure TCustomBCMDButtonFocus.SetFStyleActive(AValue: TBCMDButtonStyle);
  174. begin
  175. if FStyleActive = AValue then
  176. Exit;
  177. FStyleActive := AValue;
  178. end;
  179. procedure TCustomBCMDButtonFocus.SetFAlignment(AValue: TAlignment);
  180. begin
  181. if FAlignment = AValue then
  182. Exit;
  183. FAlignment := AValue;
  184. Invalidate;
  185. end;
  186. procedure TCustomBCMDButtonFocus.SetFAnimation(AValue: boolean);
  187. begin
  188. if FAnimation = AValue then
  189. Exit;
  190. FAnimation := AValue;
  191. Invalidate;
  192. end;
  193. procedure TCustomBCMDButtonFocus.SetFChecked(AValue: boolean);
  194. begin
  195. if FChecked = AValue then
  196. Exit;
  197. FChecked := AValue;
  198. if FChecked and (FKind in [mdbkToggleGroup, mdbkRadioButton, mdbkTab]) then
  199. UncheckOthers;
  200. Invalidate;
  201. end;
  202. procedure TCustomBCMDButtonFocus.SetFKind(AValue: TBCMDButtonKind);
  203. begin
  204. if FKind = AValue then
  205. Exit;
  206. FKind := AValue;
  207. Invalidate;
  208. end;
  209. procedure TCustomBCMDButtonFocus.OnChangeStyle(Sender: TObject);
  210. begin
  211. Invalidate;
  212. end;
  213. procedure TCustomBCMDButtonFocus.SetFStyleDisabled(AValue: TBCMDButtonStyle);
  214. begin
  215. if FStyleDisabled = AValue then
  216. Exit;
  217. FStyleDisabled := AValue;
  218. end;
  219. procedure TCustomBCMDButtonFocus.SetFStyleHover(AValue: TBCMDButtonStyle);
  220. begin
  221. if FStyleHover = AValue then
  222. Exit;
  223. FStyleHover := AValue;
  224. end;
  225. procedure TCustomBCMDButtonFocus.SetFStyleNormal(AValue: TBCMDButtonStyle);
  226. begin
  227. if FStyleNormal = AValue then
  228. Exit;
  229. FStyleNormal := AValue;
  230. end;
  231. procedure TCustomBCMDButtonFocus.SetFTextAutoSize(AValue: boolean);
  232. begin
  233. if FTextAutoSize = AValue then
  234. Exit;
  235. FTextAutoSize := AValue;
  236. end;
  237. procedure TCustomBCMDButtonFocus.SetFTextLayout(AValue: TTextLayout);
  238. begin
  239. if FTextLayout = AValue then
  240. Exit;
  241. FTextLayout := AValue;
  242. Invalidate;
  243. end;
  244. procedure TCustomBCMDButtonFocus.SetFTextProportional(AValue: boolean);
  245. begin
  246. if FTextProportional = AValue then
  247. Exit;
  248. FTextProportional := AValue;
  249. Invalidate;
  250. end;
  251. procedure TCustomBCMDButtonFocus.SetFTextProportionalRatio(AValue: single);
  252. begin
  253. if FTextProportionalRatio = AValue then
  254. Exit;
  255. FTextProportionalRatio := AValue;
  256. Invalidate;
  257. end;
  258. procedure TCustomBCMDButtonFocus.WMSetFocus(var Message: {$IFDEF FPC}TLMSetFocus{$ELSE}TWMKillFocus{$ENDIF});
  259. begin
  260. inherited;
  261. UpdateFocus(True);
  262. end;
  263. procedure TCustomBCMDButtonFocus.WMKillFocus(var Message: {$IFDEF FPC}TLMKillFocus{$ELSE}TWMKillFocus{$ENDIF});
  264. begin
  265. inherited;
  266. if Message.FocusedWnd <> Handle then
  267. UpdateFocus(False);
  268. end;
  269. procedure TCustomBCMDButtonFocus.UpdateFocus(AFocused: boolean);
  270. var
  271. lForm: TCustomForm;
  272. begin
  273. lForm := GetParentForm(Self);
  274. if lForm = nil then
  275. exit;
  276. {$IFDEF FPC}//#
  277. if AFocused then
  278. ActiveDefaultControlChanged(lForm.ActiveControl)
  279. else
  280. ActiveDefaultControlChanged(nil);
  281. {$ENDIF}
  282. Invalidate;
  283. end;
  284. procedure TCustomBCMDButtonFocus.KeyDown(var Key: word; Shift: TShiftState);
  285. begin
  286. inherited KeyDown(Key, Shift);
  287. if (Key = VK_SPACE) or (Key = VK_RETURN) then
  288. MouseDown(mbLeft, [], Width div 2, Height div 2);
  289. end;
  290. procedure TCustomBCMDButtonFocus.KeyUp(var Key: word; Shift: TShiftState);
  291. begin
  292. if (Key = VK_SPACE) or (Key = VK_RETURN) then
  293. begin
  294. MouseLeave;
  295. Self.Click;
  296. end;
  297. inherited KeyUp(Key, Shift);
  298. end;
  299. procedure TCustomBCMDButtonFocus.CalculatePreferredSize(
  300. var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
  301. var
  302. bmp: TBGRABitmap;
  303. s: TSize;
  304. begin
  305. bmp := TBGRABitmap.Create;
  306. bmp.FontName := Font.Name;
  307. if FTextProportional then
  308. bmp.FontHeight := Round(Height * FTextProportionalRatio)
  309. else
  310. bmp.FontHeight := 0;
  311. bmp.FontAntialias := True;
  312. bmp.FontQuality := fqSystemClearType;
  313. bmp.FontStyle := Font.Style;
  314. s := bmp.TextSize(Caption);
  315. if FTextAutoSize then
  316. begin
  317. PreferredWidth := s.Width + 26 {$IFDEF FPC}+ BorderSpacing.InnerBorder{$ENDIF};
  318. PreferredHeight := s.Height + 10 {$IFDEF FPC}+ BorderSpacing.InnerBorder{$ENDIF};
  319. end
  320. else
  321. begin
  322. {$IFDEF FPC}//#
  323. PreferredWidth := BorderSpacing.InnerBorder;
  324. PreferredHeight := BorderSpacing.InnerBorder;
  325. {$ENDIF}
  326. end;
  327. bmp.Free;
  328. end;
  329. procedure TCustomBCMDButtonFocus.Paint;
  330. var
  331. bmp: TBGRABitmap;
  332. iTemp: integer;
  333. alpha: byte;
  334. tempState: TBCMDButtonState;
  335. tempText: string;
  336. tempRounding: integer;
  337. tempColor, hoverColor: TBGRAPixel;
  338. begin
  339. bmp := TBGRABitmap.Create(Width, Height);
  340. bmp.FontName := Font.Name;
  341. if FTextProportional then
  342. bmp.FontHeight := Round(Height * FTextProportionalRatio)
  343. else
  344. bmp.FontHeight := 0;
  345. bmp.FontAntialias := True;
  346. bmp.FontQuality := fqSystemClearType;
  347. bmp.FontStyle := Font.Style;
  348. tempState := FState;
  349. if Kind = mdbkTab then
  350. tempRounding := 0
  351. else
  352. tempRounding := FRounding;
  353. if FChecked then
  354. tempState := mdbsActive
  355. else
  356. tempState := FState;
  357. // START / MDBUTTONFOCUS ONLY
  358. if Focused and (tempState = mdbsNormal) then
  359. tempState := mdbsHover;
  360. // END / MDBUTTONFOCUS ONLY
  361. tempText := Caption;
  362. case FKind of
  363. mdbkCheckBox:
  364. begin
  365. if Length(Caption) > 0 then
  366. tempText := ' ' + Caption;
  367. if FChecked then
  368. tempText := BCMDBUTTONBALLOTBOXWITHCHECK + tempText
  369. else
  370. tempText := BCMDBUTTONBALLOTBOX + tempText;
  371. end;
  372. mdbkRadioButton:
  373. begin
  374. if Length(Caption) > 0 then
  375. tempText := ' ' + Caption;
  376. if FChecked then
  377. tempText := BCMDBUTTONRADIOBUTTON + tempText
  378. else
  379. tempText := BCMDBUTTONRADIOBUTTONCIRCLE + tempText;
  380. end;
  381. end;
  382. // Enabled
  383. if Enabled then
  384. begin
  385. if not FTimer.Enabled then
  386. begin
  387. case tempState of
  388. mdbsNormal:
  389. begin
  390. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  391. FStyleNormal.Color,
  392. FStyleNormal.Color);
  393. {$IFDEF FPC}
  394. bmp.TextRect(Rect(BorderSpacing.InnerBorder, BorderSpacing.InnerBorder,
  395. Width - BorderSpacing.InnerBorder, Height - BorderSpacing.InnerBorder),
  396. tempText, Alignment,
  397. TextLayout, FStyleNormal.TextColor);
  398. {$ELSE}
  399. bmp.TextRect(Rect(0, 0, Width, Height), tempText, Alignment, TextLayout, FStyleNormal.TextColor);
  400. {$ENDIF}
  401. end;
  402. mdbsHover:
  403. begin
  404. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  405. FStyleHover.Color, FStyleHover.Color);
  406. {$IFDEF FPC}
  407. bmp.TextRect(Rect(BorderSpacing.InnerBorder, BorderSpacing.InnerBorder,
  408. Width - BorderSpacing.InnerBorder, Height - BorderSpacing.InnerBorder),
  409. tempText, Alignment,
  410. TextLayout, FStyleHover.TextColor);
  411. {$ELSE}
  412. bmp.TextRect(Rect(0, 0, Width, Height), tempText, Alignment, TextLayout, FStyleHover.TextColor);
  413. {$ENDIF}
  414. end;
  415. mdbsActive:
  416. begin
  417. if not FAnimation then
  418. begin
  419. if FKind in [mdbkNormal] then
  420. bmp.RoundRect(0, 0, Width, Height, tempRounding,
  421. tempRounding, FStyleActive.Color,
  422. FStyleActive.Color)
  423. else
  424. bmp.RoundRect(0, 0, Width, Height, tempRounding,
  425. tempRounding, FStyleHover.Color,
  426. FStyleHover.Color);
  427. end
  428. else
  429. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  430. FStyleHover.Color,
  431. FStyleHover.Color);
  432. {$IFDEF FPC}
  433. bmp.TextRect(Rect(BorderSpacing.InnerBorder, BorderSpacing.InnerBorder,
  434. Width - BorderSpacing.InnerBorder, Height - BorderSpacing.InnerBorder),
  435. tempText, Alignment,
  436. TextLayout, FStyleActive.TextColor);
  437. {$ELSE}
  438. bmp.TextRect(Rect(0, 0, Width, Height), tempText, Alignment, TextLayout, FStyleActive.TextColor);
  439. {$ENDIF}
  440. end;
  441. end;
  442. end
  443. else
  444. begin
  445. iTemp := round(FCircleSize * easeOutQuad(FPercent));
  446. alpha := round(easeInOutQuad(FAlphaPercent) * 255);
  447. case tempState of
  448. mdbsNormal:
  449. begin
  450. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  451. FStyleNormal.Color,
  452. FStyleNormal.Color);
  453. if FPercent < 1 then
  454. tempColor := FStyleHover.Color
  455. else
  456. begin
  457. tempColor := FStyleNormal.Color;
  458. hoverColor := ColorToBGRA(FStyleHover.Color, alpha);
  459. PutPixels(@tempColor, @hoverColor, 1, dmDrawWithTransparency, 255);
  460. end;
  461. bmp.FillEllipseAntialias(FCX, FCY, iTemp,
  462. iTemp, tempColor);
  463. {$IFDEF FPC}
  464. bmp.TextRect(Rect(BorderSpacing.InnerBorder, BorderSpacing.InnerBorder,
  465. Width - BorderSpacing.InnerBorder, Height - BorderSpacing.InnerBorder),
  466. tempText, Alignment,
  467. TextLayout, FStyleNormal.TextColor);
  468. {$ELSE}
  469. bmp.TextRect(Rect(0, 0, Width, Height), tempText, Alignment, TextLayout, FStyleNormal.TextColor);
  470. {$ENDIF}
  471. end;
  472. mdbsHover, mdbsActive:
  473. begin
  474. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  475. FStyleHover.Color, FStyleHover.Color);
  476. if FPercent < 1 then
  477. tempColor := FStyleActive.Color
  478. else
  479. begin
  480. tempColor := FStyleHover.Color;
  481. hoverColor := ColorToBGRA(FStyleActive.Color, alpha);
  482. PutPixels(@tempColor, @hoverColor, 1, dmDrawWithTransparency, 255);
  483. end;
  484. bmp.FillEllipseAntialias(FCX, FCY, iTemp,
  485. iTemp, tempColor);
  486. {$IFDEF FPC}
  487. bmp.TextRect(Rect(BorderSpacing.InnerBorder, BorderSpacing.InnerBorder,
  488. Width - BorderSpacing.InnerBorder, Height - BorderSpacing.InnerBorder),
  489. tempText, Alignment,
  490. TextLayout, FStyleHover.TextColor);
  491. {$ELSE}
  492. bmp.TextRect(Rect(0, 0, Width, Height), tempText, Alignment, TextLayout, FStyleHover.TextColor);
  493. {$ENDIF}
  494. end;
  495. end;
  496. end;
  497. end
  498. // Disabled
  499. else
  500. begin
  501. if FChecked then
  502. begin
  503. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  504. FStyleHover.Color, FStyleHover.Color);
  505. end
  506. else
  507. bmp.RoundRect(0, 0, Width, Height, tempRounding, tempRounding,
  508. FStyleDisabled.Color, FStyleDisabled.Color);
  509. {$IFDEF FPC}
  510. bmp.TextRect(Rect(BorderSpacing.InnerBorder, BorderSpacing.InnerBorder,
  511. Width - BorderSpacing.InnerBorder, Height - BorderSpacing.InnerBorder),
  512. tempText, Alignment,
  513. TextLayout, FStyleDisabled.TextColor);
  514. {$ELSE}
  515. bmp.TextRect(Rect(0, 0, Width, Height), tempText, Alignment, TextLayout, FStyleDisabled.TextColor);
  516. {$ENDIF}
  517. end;
  518. // Tab
  519. if Kind = mdbkTab then
  520. begin
  521. if FTimer.Enabled then
  522. begin
  523. iTemp := round((bmp.Width div 2) * easeInOutQuad(FPercent));
  524. bmp.Rectangle((bmp.Width div 2) - iTemp, bmp.Height - 2,
  525. (bmp.Width div 2) + iTemp, bmp.Height, $00BB513F, dmSet);
  526. end
  527. else
  528. begin
  529. if FChecked then
  530. bmp.Rectangle(0, bmp.Height - 2, bmp.Width, bmp.Height, $00BB513F, dmSet);
  531. end;
  532. end;
  533. {$IFDEF MDBUTTON_DEBUG}
  534. bmp.FontHeight := 10;
  535. bmp.TextOut(0, 0, FCount.ToString, BGRA(255, 0, 0, 255));
  536. FCount += 1;
  537. {$ENDIF}
  538. bmp.Draw(Canvas, 0, 0, False);
  539. bmp.Free;
  540. inherited Paint;
  541. end;
  542. procedure TCustomBCMDButtonFocus.MouseDown(Button: TMouseButton;
  543. Shift: TShiftState; X, Y: integer);
  544. begin
  545. inherited MouseDown(Button, Shift, X, Y);
  546. FState := mdbsActive;
  547. if FAnimation and BCMDBUTTONANIMATION then
  548. begin
  549. FCircleSize := max(round(Width / 1.5) + abs((Width div 2) - X),
  550. round(Height / 1.5) + abs((Height div 2) - Y));
  551. FCX := X;
  552. FCY := Y;
  553. FTimer.Enabled := False;
  554. FTimer.Enabled := True;
  555. {$IFDEF MDBUTTON_ANIMATEONLYONE}
  556. MDAnimating := Self;
  557. {$ENDIF}
  558. end;
  559. if FKind in [mdbkToggle, mdbkToggleGroup, mdbkCheckBox, mdbkRadioButton, mdbkTab] then
  560. begin
  561. FChecked := not FChecked;
  562. if FKind in [mdbkToggleGroup, mdbkRadioButton, mdbkTab] then
  563. begin
  564. FChecked := True;
  565. UncheckOthers;
  566. end;
  567. end;
  568. Invalidate;
  569. end;
  570. procedure TCustomBCMDButtonFocus.MouseUp(Button: TMouseButton;
  571. Shift: TShiftState; X, Y: integer);
  572. begin
  573. inherited MouseUp(Button, Shift, X, Y);
  574. if (x > 0) and (x < Width) and (y > 0) and (y < Height) and (FState = mdbsActive) then
  575. FState := mdbsHover
  576. else
  577. FState := mdbsNormal;
  578. Invalidate;
  579. end;
  580. procedure TCustomBCMDButtonFocus.MouseEnter;
  581. begin
  582. inherited MouseEnter;
  583. FState := mdbsHover;
  584. Invalidate;
  585. end;
  586. procedure TCustomBCMDButtonFocus.MouseLeave;
  587. begin
  588. inherited MouseLeave;
  589. FState := mdbsNormal;
  590. Invalidate;
  591. end;
  592. procedure TCustomBCMDButtonFocus.RealSetText(const Value: TCaption);
  593. begin
  594. inherited RealSetText(Value);
  595. InvalidatePreferredSize;
  596. Invalidate;
  597. end;
  598. procedure TCustomBCMDButtonFocus.OnTimer(Sender: TObject);
  599. begin
  600. {$IFDEF MDBUTTON_ANIMATEONLYONE}
  601. if MDAnimating = Self then
  602. begin
  603. {$ENDIF}
  604. FPercent := FPercent + BCMDBUTTONANIMATIONSPEED;
  605. if FPercent < 0 then
  606. FPercent := 0
  607. else if FPercent > 1 then
  608. FPercent := 1;
  609. if FPercent = 1 then
  610. begin
  611. FAlphaPercent := FAlphaPercent - BCMDBUTTONANIMATIONSPEED;
  612. if FAlphaPercent < 0 then
  613. FAlphaPercent := 0
  614. else if FAlphaPercent > 1 then
  615. FAlphaPercent := 1;
  616. end;
  617. {$IFDEF MDBUTTON_ANIMATEONLYONE}
  618. end
  619. else
  620. FTimer.Enabled := False;
  621. {$ENDIF}
  622. Invalidate;
  623. if (FPercent >= 1) and (FAlphaPercent <= 0) then
  624. FTimer.Enabled := False;
  625. end;
  626. procedure TCustomBCMDButtonFocus.OnStartTimer(Sender: TObject);
  627. begin
  628. FPercent := 0;
  629. FAlphaPercent := 1;
  630. end;
  631. procedure TCustomBCMDButtonFocus.OnStopTimer(Sender: TObject);
  632. begin
  633. end;
  634. function TCustomBCMDButtonFocus.easeInOutQuad(t: double): double;
  635. begin
  636. if t < 0.5 then
  637. Result := 2 * t * t
  638. else
  639. Result := -1 + (4 - 2 * t) * t;
  640. end;
  641. function TCustomBCMDButtonFocus.easeOutQuad(t: double): double;
  642. begin
  643. Result := t * (2 - t);
  644. end;
  645. procedure TCustomBCMDButtonFocus.UncheckOthers;
  646. var
  647. i: integer;
  648. control: TWinControl;
  649. begin
  650. if Parent is TWinControl then
  651. begin
  652. control := TWinControl(Parent);
  653. for i := 0 to control.ControlCount - 1 do
  654. if (control.Controls[i] <> Self) and (control.Controls[i] is
  655. TCustomBCMDButtonFocus) then
  656. if (TCustomBCMDButtonFocus(control.Controls[i]).Kind in
  657. [mdbkToggleGroup, mdbkRadioButton, mdbkTab]) then
  658. TCustomBCMDButtonFocus(control.Controls[i]).Checked := False;
  659. end;
  660. end;
  661. class function TCustomBCMDButtonFocus.GetControlClassDefaultSize: TSize;
  662. begin
  663. Result.CX := 75;
  664. Result.CY := 25;
  665. end;
  666. constructor TCustomBCMDButtonFocus.Create(AOwner: TComponent);
  667. begin
  668. inherited Create(AOwner);
  669. // START / MDBUTTONFOCUS ONLY
  670. TabStop := True;
  671. ControlStyle := ControlStyle + [csAcceptsControls, csParentBackground];
  672. DoubleBuffered := True;
  673. // END / MDBUTTONFOCUS ONLY
  674. {$IFDEF INDEBUG}
  675. FCount := 0;
  676. {$ENDIF}
  677. // State
  678. FState := mdbsNormal;
  679. FChecked := False;
  680. FKind := mdbkNormal;
  681. // Text
  682. FTextAutoSize := True;
  683. FAlignment := taCenter;
  684. FTextLayout := tlCenter;
  685. FTextProportional := False;
  686. FTextProportionalRatio := 0.5;
  687. // Style
  688. FRounding := 6;
  689. FStyleNormal := TBCMDButtonStyle.Create;
  690. FStyleNormal.OnChange := OnChangeStyle;
  691. FStyleHover := TBCMDButtonStyle.Create;
  692. FStyleHover.OnChange := OnChangeStyle;
  693. FStyleActive := TBCMDButtonStyle.Create;
  694. FStyleActive.OnChange := OnChangeStyle;
  695. FStyleDisabled := TBCMDButtonStyle.Create;
  696. FStyleDisabled.OnChange := OnChangeStyle;
  697. // Default Style
  698. FStyleHover.Color := RGBToColor(220, 220, 220);
  699. FStyleActive.Color := RGBToColor(198, 198, 198);
  700. FStyleDisabled.TextColor := RGBToColor(163, 163, 163);
  701. // Animation
  702. FAnimation := False;
  703. FTimer := TTimer.Create(Self);
  704. FTimer.Enabled := False;
  705. FTimer.Interval := BCMDBUTTONTIMERSPEED;
  706. FTimer.OnTimer := OnTimer;
  707. {$IFDEF FPC}//#
  708. FTimer.OnStartTimer := OnStartTimer;
  709. FTimer.OnStopTimer := OnStopTimer;
  710. {$ENDIF}
  711. // Setup default sizes
  712. with GetControlClassDefaultSize do
  713. SetInitialBounds(0, 0, CX, CY);
  714. end;
  715. destructor TCustomBCMDButtonFocus.Destroy;
  716. begin
  717. FTimer.OnTimer := nil;
  718. {$IFDEF FPC}//#
  719. FTimer.OnStartTimer := nil;
  720. FTimer.OnStopTimer := nil;
  721. {$ENDIF}
  722. FTimer.Enabled := False;
  723. FStyleNormal.Free;
  724. FStyleHover.Free;
  725. FStyleActive.Free;
  726. FStyleDisabled.Free;
  727. inherited Destroy;
  728. end;
  729. procedure TCustomBCMDButtonFocus.SelectAll;
  730. var
  731. i: integer;
  732. control: TWinControl;
  733. begin
  734. if Parent is TWinControl then
  735. begin
  736. control := TWinControl(Parent);
  737. for i := 0 to control.ControlCount - 1 do
  738. if (control.Controls[i] is TCustomBCMDButtonFocus) then
  739. if (TCustomBCMDButtonFocus(control.Controls[i]).Kind in
  740. [mdbkToggle, mdbkCheckBox]) then
  741. TCustomBCMDButtonFocus(control.Controls[i]).Checked := True;
  742. end;
  743. end;
  744. procedure TCustomBCMDButtonFocus.UnselectAll;
  745. var
  746. i: integer;
  747. control: TWinControl;
  748. begin
  749. if Parent is TWinControl then
  750. begin
  751. control := TWinControl(Parent);
  752. for i := 0 to control.ControlCount - 1 do
  753. if (control.Controls[i] is TCustomBCMDButtonFocus) then
  754. if (TCustomBCMDButtonFocus(control.Controls[i]).Kind in
  755. [mdbkToggle, mdbkCheckBox]) then
  756. TCustomBCMDButtonFocus(control.Controls[i]).Checked := False;
  757. end;
  758. end;
  759. procedure TCustomBCMDButtonFocus.InvertSelection;
  760. var
  761. i: integer;
  762. control: TWinControl;
  763. begin
  764. if Parent is TWinControl then
  765. begin
  766. control := TWinControl(Parent);
  767. for i := 0 to control.ControlCount - 1 do
  768. if (control.Controls[i] is TCustomBCMDButtonFocus) then
  769. if (TCustomBCMDButtonFocus(control.Controls[i]).Kind in
  770. [mdbkToggle, mdbkCheckBox]) then
  771. TCustomBCMDButtonFocus(control.Controls[i]).Checked :=
  772. not TCustomBCMDButtonFocus(control.Controls[i]).Checked;
  773. end;
  774. end;
  775. function TCustomBCMDButtonFocus.GetSelected: TStringList;
  776. var
  777. i: integer;
  778. control: TWinControl;
  779. begin
  780. Result := TStringList.Create;
  781. if Parent is TWinControl then
  782. begin
  783. control := TWinControl(Parent);
  784. for i := 0 to control.ControlCount - 1 do
  785. if (control.Controls[i] is TCustomBCMDButtonFocus) then
  786. if TCustomBCMDButtonFocus(control.Controls[i]).Checked then
  787. Result.AddObject(TCustomBCMDButtonFocus(control.Controls[i]).Caption,
  788. TCustomBCMDButtonFocus(control.Controls[i]));
  789. end;
  790. end;
  791. end.