bcmdbutton.pas 27 KB

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