bgrathemecheckbox.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. unit BGRAThemeCheckBox;
  3. {$mode delphi}
  4. interface
  5. uses
  6. Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs,
  7. BGRATheme, Types, LMessages, LCLType;
  8. type
  9. { TBGRAThemeCheckBox }
  10. TBGRAThemeCheckBox = class(TBGRAThemeControl)
  11. private
  12. FChecked: boolean;
  13. FOnChange: TNotifyEvent;
  14. FState: TBGRAThemeButtonState;
  15. procedure SetChecked(AValue: boolean);
  16. protected
  17. procedure KeyDown(var Key: word; Shift: TShiftState); override;
  18. procedure KeyUp(var Key: word; Shift: TShiftState); override;
  19. procedure WMSetFocus(var Message: {$IFDEF FPC}TLMSetFocus{$ELSE}TWMSetFocus{$ENDIF}); message {$IFDEF FPC}LM_SETFOCUS{$ELSE}WM_SETFOCUS{$ENDIF};
  20. procedure WMKillFocus(var Message: {$IFDEF FPC}TLMKillFocus{$ELSE}TWMKillFocus{$ENDIF}); message {$IFDEF FPC}LM_KILLFOCUS{$ELSE}WM_KILLFOCUS{$ENDIF};
  21. procedure UpdateFocus(AFocused: boolean);
  22. class function GetControlClassDefaultSize: TSize; override;
  23. procedure MouseEnter; override;
  24. procedure MouseLeave; override;
  25. procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  26. X, Y: integer); override;
  27. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
  28. procedure Click; override;
  29. procedure SetEnabled(Value: boolean); override;
  30. procedure TextChanged; override;
  31. procedure Paint; override;
  32. procedure Resize; override;
  33. public
  34. constructor Create(AOwner: TComponent); override;
  35. published
  36. property Align;
  37. property Anchors;
  38. property BorderSpacing;
  39. property Caption;
  40. property Checked: boolean read FChecked write SetChecked;
  41. property Font;
  42. property Enabled;
  43. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  44. property TabStop;
  45. property TabOrder;
  46. end;
  47. procedure Register;
  48. implementation
  49. uses BGRABitmapTypes;
  50. procedure Register;
  51. begin
  52. RegisterComponents('BGRA Themes', [TBGRAThemeCheckBox]);
  53. end;
  54. { TBGRAThemeCheckBox }
  55. procedure TBGRAThemeCheckBox.SetChecked(AValue: boolean);
  56. begin
  57. if FChecked = AValue then
  58. Exit;
  59. FChecked := AValue;
  60. Invalidate;
  61. if Assigned(FOnChange) then FOnChange(Self);
  62. end;
  63. procedure TBGRAThemeCheckBox.KeyDown(var Key: word; Shift: TShiftState);
  64. begin
  65. inherited KeyDown(Key, Shift);
  66. if (Key = VK_SPACE) or (Key = VK_RETURN) then
  67. MouseDown(mbLeft, [], 0, 0);
  68. end;
  69. procedure TBGRAThemeCheckBox.KeyUp(var Key: word; Shift: TShiftState);
  70. begin
  71. if (Key = VK_SPACE) or (Key = VK_RETURN) then
  72. begin
  73. MouseUp(mbLeft, [], 0, 0);
  74. MouseLeave;
  75. end;
  76. inherited KeyUp(Key, Shift);
  77. end;
  78. procedure TBGRAThemeCheckBox.WMSetFocus(var Message: TLMSetFocus);
  79. begin
  80. inherited;
  81. UpdateFocus(True);
  82. end;
  83. procedure TBGRAThemeCheckBox.WMKillFocus(var Message: TLMKillFocus);
  84. begin
  85. inherited;
  86. if Message.FocusedWnd <> Handle then
  87. UpdateFocus(False);
  88. end;
  89. procedure TBGRAThemeCheckBox.UpdateFocus(AFocused: boolean);
  90. var
  91. lForm: TCustomForm;
  92. begin
  93. lForm := GetParentForm(Self);
  94. if lForm = nil then
  95. exit;
  96. {$IFDEF FPC}//#
  97. if AFocused then
  98. ActiveDefaultControlChanged(lForm.ActiveControl)
  99. else
  100. ActiveDefaultControlChanged(nil);
  101. {$ENDIF}
  102. Invalidate;
  103. end;
  104. class function TBGRAThemeCheckBox.GetControlClassDefaultSize: TSize;
  105. begin
  106. Result.CX := 165;
  107. Result.CY := 19;
  108. end;
  109. procedure TBGRAThemeCheckBox.MouseEnter;
  110. begin
  111. inherited MouseEnter;
  112. FState := btbsHover;
  113. Invalidate;
  114. end;
  115. procedure TBGRAThemeCheckBox.MouseLeave;
  116. begin
  117. inherited MouseLeave;
  118. FState := btbsNormal;
  119. Invalidate;
  120. end;
  121. procedure TBGRAThemeCheckBox.MouseDown(Button: TMouseButton;
  122. Shift: TShiftState; X, Y: integer);
  123. begin
  124. inherited MouseDown(Button, Shift, X, Y);
  125. FState := btbsActive;
  126. Invalidate;
  127. end;
  128. procedure TBGRAThemeCheckBox.MouseUp(Button: TMouseButton;
  129. Shift: TShiftState; X, Y: integer);
  130. begin
  131. inherited MouseUp(Button, Shift, X, Y);
  132. if ClientRect.Contains(Point(X, Y)) then
  133. FState := btbsHover
  134. else
  135. FState := btbsNormal;
  136. if ClientRect.Contains(Point(X, Y)) then
  137. Checked := not FChecked
  138. else
  139. Invalidate;
  140. end;
  141. procedure TBGRAThemeCheckBox.Click;
  142. begin
  143. inherited Click;
  144. end;
  145. procedure TBGRAThemeCheckBox.SetEnabled(Value: boolean);
  146. begin
  147. inherited SetEnabled(Value);
  148. if Value then
  149. FState := btbsNormal
  150. else
  151. FState := btbsDisabled;
  152. Invalidate;
  153. end;
  154. procedure TBGRAThemeCheckBox.TextChanged;
  155. begin
  156. inherited TextChanged;
  157. Invalidate;
  158. end;
  159. procedure TBGRAThemeCheckBox.Paint;
  160. var
  161. surface: TBGRAThemeSurface;
  162. begin
  163. surface := TBGRAThemeSurface.Create(self);
  164. try
  165. if Assigned(Theme) then
  166. Theme.DrawCheckBox(Caption, FState, Focused, Checked, ClientRect, surface)
  167. else
  168. BGRADefaultTheme.DrawCheckBox(Caption, FState, Focused, Checked, ClientRect, surface);
  169. finally
  170. surface.Free;
  171. end;
  172. end;
  173. procedure TBGRAThemeCheckBox.Resize;
  174. begin
  175. Invalidate;
  176. inherited Resize;
  177. end;
  178. constructor TBGRAThemeCheckBox.Create(AOwner: TComponent);
  179. begin
  180. inherited Create(AOwner);
  181. FState := btbsNormal;
  182. ControlStyle := ControlStyle + [csParentBackground, csAcceptsControls];
  183. with GetControlClassDefaultSize do
  184. SetInitialBounds(0, 0, CX, CY);
  185. end;
  186. end.