| 1 |
{ |
| 2 |
Double Commander |
| 3 |
------------------------------------------------------------------------- |
| 4 |
Control like TButton which does not steal focus on click |
| 5 |
|
| 6 |
Copyright (C) 2021-2023 Alexander Koblov ([email protected]) |
| 7 |
|
| 8 |
This library is free software; you can redistribute it and/or |
| 9 |
modify it under the terms of the GNU Lesser General Public |
| 10 |
License as published by the Free Software Foundation; either |
| 11 |
version 2.1 of the License, or (at your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
} |
| 21 |
|
| 22 |
unit KASButton; |
| 23 |
|
| 24 |
{$mode delphi} |
| 25 |
|
| 26 |
interface |
| 27 |
|
| 28 |
uses |
| 29 |
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls, |
| 30 |
Buttons, Themes, Types; |
| 31 |
|
| 32 |
type |
| 33 |
|
| 34 |
{ TKASButton } |
| 35 |
|
| 36 |
TKASButton = class(TPanel) |
| 37 |
private |
| 38 |
FState: TButtonState; |
| 39 |
FShowCaption: Boolean; |
| 40 |
FButtonGlyph: TButtonGlyph; |
| 41 |
function GetGlyph: TBitmap; |
| 42 |
function IsGlyphStored: Boolean; |
| 43 |
procedure SetGlyph(AValue: TBitmap); |
| 44 |
procedure SetShowCaption(AValue: Boolean); |
| 45 |
function GetDrawDetails: TThemedElementDetails; |
| 46 |
protected |
| 47 |
procedure Paint; override; |
| 48 |
procedure DoExit; override; |
| 49 |
procedure DoEnter; override; |
| 50 |
procedure MouseEnter; override; |
| 51 |
procedure MouseLeave; override; |
| 52 |
procedure KeyUp(var Key: Word; Shift: TShiftState); override; |
| 53 |
procedure KeyDown(var Key: Word; Shift: TShiftState); override; |
| 54 |
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 55 |
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 56 |
protected |
| 57 |
procedure GlyphChanged(Sender: TObject); |
| 58 |
class function GetControlClassDefaultSize: TSize; override; |
| 59 |
procedure ActionChange(Sender: TObject; CheckDefaults: Boolean); override; |
| 60 |
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer; WithThemeSpace: Boolean); override; |
| 61 |
public |
| 62 |
constructor Create(TheOwner: TComponent); override; |
| 63 |
destructor Destroy; override; |
| 64 |
published |
| 65 |
property Action; |
| 66 |
property Glyph: TBitmap read GetGlyph write SetGlyph stored IsGlyphStored; |
| 67 |
property ShowCaption: Boolean read FShowCaption write SetShowCaption default True; |
| 68 |
end; |
| 69 |
|
| 70 |
procedure Register; |
| 71 |
|
| 72 |
implementation |
| 73 |
|
| 74 |
uses |
| 75 |
LCLType, LCLProc, LCLIntf, ActnList; |
| 76 |
|
| 77 |
procedure Register; |
| 78 |
begin |
| 79 |
RegisterComponents('KASComponents',[TKASButton]); |
| 80 |
end; |
| 81 |
|
| 82 |
{ TKASButton } |
| 83 |
|
| 84 |
procedure TKASButton.DoEnter; |
| 85 |
begin |
| 86 |
inherited DoEnter; |
| 87 |
FState:= bsExclusive; |
| 88 |
Invalidate; |
| 89 |
end; |
| 90 |
|
| 91 |
procedure TKASButton.DoExit; |
| 92 |
begin |
| 93 |
inherited DoExit; |
| 94 |
FState:= bsUp; |
| 95 |
Invalidate; |
| 96 |
end; |
| 97 |
|
| 98 |
function TKASButton.GetDrawDetails: TThemedElementDetails; |
| 99 |
var |
| 100 |
Detail: TThemedButton; |
| 101 |
begin |
| 102 |
if not IsEnabled then |
| 103 |
Detail := tbPushButtonDisabled |
| 104 |
else if FState = bsDown then |
| 105 |
Detail := tbPushButtonPressed |
| 106 |
else if FState = bsHot then |
| 107 |
Detail := tbPushButtonHot |
| 108 |
else if FState = bsExclusive then |
| 109 |
Detail := tbPushButtonDefaulted |
| 110 |
else begin |
| 111 |
Detail := tbPushButtonNormal; |
| 112 |
end; |
| 113 |
Result := ThemeServices.GetElementDetails(Detail) |
| 114 |
end; |
| 115 |
|
| 116 |
procedure TKASButton.SetShowCaption(AValue: Boolean); |
| 117 |
begin |
| 118 |
if FShowCaption = AValue then Exit; |
| 119 |
FShowCaption:= AValue; |
| 120 |
Invalidate; |
| 121 |
end; |
| 122 |
|
| 123 |
function TKASButton.GetGlyph: TBitmap; |
| 124 |
begin |
| 125 |
Result:= FButtonGlyph.Glyph; |
| 126 |
end; |
| 127 |
|
| 128 |
function TKASButton.IsGlyphStored: Boolean; |
| 129 |
var |
| 130 |
Act: TCustomAction; |
| 131 |
begin |
| 132 |
if Action <> nil then |
| 133 |
begin |
| 134 |
Result:= True; |
| 135 |
Act:= TCustomAction(Action); |
| 136 |
if (Act.ActionList <> nil) and (Act.ActionList.Images <> nil) and |
| 137 |
(Act.ImageIndex >= 0) and (Act.ImageIndex < Act.ActionList.Images.Count) then |
| 138 |
Result := False; |
| 139 |
end |
| 140 |
else Result:= (FButtonGlyph.Glyph <> nil) and (not FButtonGlyph.Glyph.Empty) and |
| 141 |
(FButtonGlyph.Glyph.Width > 0) and (FButtonGlyph.Glyph.Height > 0); |
| 142 |
end; |
| 143 |
|
| 144 |
procedure TKASButton.SetGlyph(AValue: TBitmap); |
| 145 |
begin |
| 146 |
FButtonGlyph.Glyph := AValue; |
| 147 |
InvalidatePreferredSize; |
| 148 |
AdjustSize; |
| 149 |
end; |
| 150 |
|
| 151 |
procedure TKASButton.Paint; |
| 152 |
var |
| 153 |
APoint: TPoint; |
| 154 |
SysFont: TFont; |
| 155 |
PaintRect: TRect; |
| 156 |
TextFlags: Integer; |
| 157 |
Details: TThemedElementDetails; |
| 158 |
begin |
| 159 |
PaintRect:= ClientRect; |
| 160 |
Details:= GetDrawDetails; |
| 161 |
ThemeServices.DrawElement(Canvas.Handle, Details, PaintRect); |
| 162 |
PaintRect := ThemeServices.ContentRect(Canvas.Handle, Details, PaintRect); |
| 163 |
|
| 164 |
if FShowCaption and (Caption <> EmptyStr) then |
| 165 |
begin |
| 166 |
TextFlags := DT_CENTER or DT_VCENTER; |
| 167 |
if UseRightToLeftReading then begin |
| 168 |
TextFlags := TextFlags or DT_RTLREADING; |
| 169 |
end; |
| 170 |
|
| 171 |
SysFont := Screen.SystemFont; |
| 172 |
|
| 173 |
if (SysFont.Color = Font.Color) and |
| 174 |
((SysFont.Name = Font.Name) or IsFontNameDefault(Font.Name)) and |
| 175 |
(SysFont.Pitch = Font.Pitch) and (SysFont.Style = Font.Style) then |
| 176 |
begin |
| 177 |
ThemeServices.DrawText(Canvas, Details, Caption, PaintRect, TextFlags, 0); |
| 178 |
end |
| 179 |
else begin |
| 180 |
Canvas.Brush.Style := bsClear; |
| 181 |
DrawText(Canvas.Handle, PChar(Caption), Length(Caption), PaintRect, TextFlags); |
| 182 |
end; |
| 183 |
end |
| 184 |
else if not FButtonGlyph.Glyph.Empty then |
| 185 |
begin |
| 186 |
APoint.X:= (PaintRect.Width - FButtonGlyph.Width) div 2; |
| 187 |
APoint.Y:= (PaintRect.Height - FButtonGlyph.Height) div 2; |
| 188 |
FButtonGlyph.Draw(Canvas, PaintRect, APoint, FState, True, 0); |
| 189 |
end; |
| 190 |
end; |
| 191 |
|
| 192 |
procedure TKASButton.MouseEnter; |
| 193 |
begin |
| 194 |
inherited MouseEnter; |
| 195 |
FState:= bsHot; |
| 196 |
Invalidate; |
| 197 |
end; |
| 198 |
|
| 199 |
procedure TKASButton.MouseLeave; |
| 200 |
begin |
| 201 |
inherited MouseLeave; |
| 202 |
FState:= bsUp; |
| 203 |
Invalidate; |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TKASButton.KeyUp(var Key: Word; Shift: TShiftState); |
| 207 |
begin |
| 208 |
inherited KeyUp(Key, Shift); |
| 209 |
if (Key in [VK_SPACE, VK_RETURN]) and (Shift = []) then |
| 210 |
begin |
| 211 |
FState:= bsUp; |
| 212 |
Invalidate; |
| 213 |
Click; |
| 214 |
end; |
| 215 |
end; |
| 216 |
|
| 217 |
procedure TKASButton.KeyDown(var Key: Word; Shift: TShiftState); |
| 218 |
begin |
| 219 |
inherited KeyDown(Key, Shift); |
| 220 |
if (Key in [VK_SPACE, VK_RETURN]) and (Shift = []) then |
| 221 |
begin |
| 222 |
FState:= bsDown; |
| 223 |
Invalidate; |
| 224 |
end; |
| 225 |
end; |
| 226 |
|
| 227 |
procedure TKASButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X, |
| 228 |
Y: Integer); |
| 229 |
begin |
| 230 |
inherited MouseUp(Button, Shift, X, Y); |
| 231 |
FState:= bsUp; |
| 232 |
Invalidate; |
| 233 |
end; |
| 234 |
|
| 235 |
procedure TKASButton.MouseDown(Button: TMouseButton; Shift: TShiftState; |
| 236 |
X, Y: Integer); |
| 237 |
begin |
| 238 |
inherited MouseDown(Button, Shift, X, Y); |
| 239 |
FState:= bsDown; |
| 240 |
Invalidate; |
| 241 |
end; |
| 242 |
|
| 243 |
procedure TKASButton.GlyphChanged(Sender: TObject); |
| 244 |
begin |
| 245 |
InvalidatePreferredSize; |
| 246 |
AdjustSize; |
| 247 |
end; |
| 248 |
|
| 249 |
class function TKASButton.GetControlClassDefaultSize: TSize; |
| 250 |
begin |
| 251 |
Result.CX := 23; |
| 252 |
Result.CY := 22; |
| 253 |
end; |
| 254 |
|
| 255 |
procedure TKASButton.ActionChange(Sender: TObject; CheckDefaults: Boolean); |
| 256 |
begin |
| 257 |
inherited ActionChange(Sender, CheckDefaults); |
| 258 |
|
| 259 |
if Sender is TCustomAction then |
| 260 |
begin |
| 261 |
with TCustomAction(Sender) do |
| 262 |
begin |
| 263 |
if (Glyph.Empty) and (ActionList <> nil) and (ActionList.Images <> nil) and |
| 264 |
(ImageIndex >= 0) and (ImageIndex < ActionList.Images.Count) then |
| 265 |
ActionList.Images.GetBitmap(ImageIndex, Glyph); |
| 266 |
end; |
| 267 |
end; |
| 268 |
end; |
| 269 |
|
| 270 |
procedure TKASButton.CalculatePreferredSize(var PreferredWidth, |
| 271 |
PreferredHeight: Integer; WithThemeSpace: Boolean); |
| 272 |
var |
| 273 |
PaintRect: TRect; |
| 274 |
ClientRect: TRect; |
| 275 |
Details: TThemedElementDetails; |
| 276 |
begin |
| 277 |
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight, WithThemeSpace); |
| 278 |
|
| 279 |
if (not FButtonGlyph.Glyph.Empty) then |
| 280 |
begin |
| 281 |
Details:= GetDrawDetails; |
| 282 |
PaintRect:= TRect.Create(0, 0, 32, 32); |
| 283 |
ClientRect:= ThemeServices.ContentRect(Canvas.Handle, Details, PaintRect); |
| 284 |
|
| 285 |
PreferredWidth:= Abs(PaintRect.Width - ClientRect.Width) + FButtonGlyph.Width; |
| 286 |
PreferredHeight:= Abs(PaintRect.Height - ClientRect.Height) + FButtonGlyph.Height; |
| 287 |
end; |
| 288 |
end; |
| 289 |
|
| 290 |
constructor TKASButton.Create(TheOwner: TComponent); |
| 291 |
begin |
| 292 |
inherited Create(TheOwner); |
| 293 |
|
| 294 |
FButtonGlyph := TButtonGlyph.Create; |
| 295 |
FButtonGlyph.NumGlyphs := 1; |
| 296 |
FButtonGlyph.OnChange := GlyphChanged; |
| 297 |
FButtonGlyph.IsDesigning := csDesigning in ComponentState; |
| 298 |
|
| 299 |
FShowCaption:= True; |
| 300 |
TabStop:= True; |
| 301 |
end; |
| 302 |
|
| 303 |
destructor TKASButton.Destroy; |
| 304 |
begin |
| 305 |
FreeAndNil(FButtonGlyph); |
| 306 |
inherited Destroy; |
| 307 |
end; |
| 308 |
|
| 309 |
end. |