1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit UserInterface.Control.Button;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Drawing.Canvas,
Event,
Geometry._2d.Integer32.Rectangle,
Geometry._2d.Integer32.Size,
Timer,
UserInterface.Control,
UserInterface.Control.Text.SingleLine,
UserInterface.Keyboard.Event,
UserInterface.Mouse.Event;
type
TButtonBehavior = (PushButton, ToggleButton, RepeatingButton);
TAbstractButton = class(TControl)
private
FDown: TBoolean;
FHot: TBoolean;
FBehavior: TButtonBehavior;
FirstTimer: TLowResolutionSynchronousTimer;
RepeatTimer: TLowResolutionSynchronousTimer;
protected
procedure DownChanged(); virtual;
procedure HotChanged(); virtual;
procedure HandleButtonDown();
procedure HandleButtonUp();
procedure SetDown(const Value: TBoolean);
procedure SetHot(const Value: TBoolean);
procedure SetBehavior(const Value: TButtonBehavior);
procedure AdjustFocusability();
procedure AdjustTimers();
public
constructor Create(); override;
destructor Destroy(); override;
function HandleEvent(Event: TEvent): TBoolean; override;
function KeyDown(Event: TKeyDownEvent): TBoolean; override;
function KeyUp(Event: TKeyUpEvent): TBoolean; override;
function MouseLeftDown(Event: TMouseButtonEvent): TBoolean; override;
function MouseLeftUp(Event: TMouseButtonEvent): TBoolean; override;
function MouseMove(Event: TMouseMoveEvent): TBoolean; override;
function MouseLeave(Event: TMouseLeaveEvent): TBoolean; override;
property Down: TBoolean read FDown write SetDown;
property Hot: TBoolean read FHot write SetHot;
property Behavior: TButtonBehavior read FBehavior write SetBehavior;
end;
TButtonState = (Normal, Pressed, Hot);
TButton = class(TAbstractButton)
private
FState: TButtonState;
FLabelControl: TSingleLineTextControl;
procedure SetState(const Value: TButtonState);
protected
procedure HasBeenBoundToLayout(); override;
procedure CaptionChanged(); override;
procedure BoundsChanged(const OldBounds: TInteger32Rectangle; const NewBounds: TInteger32Rectangle); override;
procedure DownChanged(); override;
procedure HotChanged(); override;
procedure Layout();
function DetermineState(): TButtonState;
function GetCaptionInsets(): TInteger32Rectangle;
function GetPreferredExtraSize(): TInteger32Size;
function CalculateCaptionRectangle(): TInteger32Rectangle;
procedure PaintButton(Canvas: TCanvas; const Bounds: TInteger32Rectangle);
public
constructor Create(); override;
procedure Paint(Canvas: TCanvas); override;
function GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size; override;
property State: TButtonState read FState write SetState;
property LabelControl: TSingleLineTextControl read FLabelControl;
end;
implementation
uses
Alignment,
Color.Constants,
Drawing.Fill,
Drawing.Stroke,
FloatingPoint._64.Interval,
Font.Purpose,
Geometry._2d.Dimension,
Geometry._2d.FloatingPoint64.Rectangle,
Geometry._2d.FloatingPoint64.Rectangle.Rounded,
Geometry._2d.FloatingPoint64.Size,
Geometry._2d.Integer32.Point,
UserInterface.Control.Mode,
UserInterface.Keyboard.Key;
{ TAbstractButton }
procedure TAbstractButton.AdjustFocusability();
begin
if Behavior = TButtonBehavior.RepeatingButton then
Focusability := TFocusability.None
else
Focusability := TFocusability.Focusable;
end;
procedure TAbstractButton.AdjustTimers;
begin
if Behavior = TButtonBehavior.RepeatingButton then
begin
FirstTimer := TLowResolutionSynchronousTimer.Create();
FirstTimer.Delay_Milliseconds := 500;
RepeatTimer := TLowResolutionSynchronousTimer.Create();
RepeatTimer.Delay_Milliseconds := 50;
end
else
begin
FirstTimer.Free();
RepeatTimer.Free();
FirstTimer := nil;
RepeatTimer := nil;
end;
end;
constructor TAbstractButton.Create();
begin
inherited Create();
AllowedModes := [TControlMode.Display];
Behavior := TButtonBehavior.PushButton;
end;
destructor TAbstractButton.Destroy();
begin
FirstTimer.Free();
RepeatTimer.Free();
inherited Destroy();
end;
procedure TAbstractButton.DownChanged();
begin
end;
procedure TAbstractButton.HandleButtonDown();
begin
case Behavior of
TButtonBehavior.PushButton:
Down := True;
TButtonBehavior.ToggleButton:
begin
Down := not Down;
FireDefaultActionExecuted();
end;
TButtonBehavior.RepeatingButton:
begin
Down := True;
FireDefaultActionExecuted();
FirstTimer.Schedule(Self);
end;
end;
end;
procedure TAbstractButton.HandleButtonUp();
begin
case Behavior of
TButtonBehavior.PushButton:
if Down then
begin
Down := False;
FireDefaultActionExecuted();
end;
TButtonBehavior.RepeatingButton:
begin
Down := False;
FirstTimer.Kill();
RepeatTimer.Kill();
end;
end;
end;
function TAbstractButton.HandleEvent(Event: TEvent): TBoolean;
begin
if Event is TTimerFired then
begin
Result := True;
if Down then
FireDefaultActionExecuted();
RepeatTimer.Schedule(Self);
end
else
Result := inherited HandleEvent(Event);
end;
procedure TAbstractButton.HotChanged();
begin
end;
function TAbstractButton.KeyDown(Event: TKeyDownEvent): TBoolean;
begin
Result := Event.Key = TKey.Space;
if Result then
HandleButtonDown();
end;
function TAbstractButton.KeyUp(Event: TKeyUpEvent): TBoolean;
begin
Result := True;
if Event.Key = TKey.Space then
HandleButtonUp();
end;
function TAbstractButton.MouseLeave(Event: TMouseLeaveEvent): TBoolean;
begin
Result := True;
Hot := False;
end;
function TAbstractButton.MouseLeftDown(Event: TMouseButtonEvent): TBoolean;
begin
Result := True;
GetMouseManager().ControlWithCapture := Self;
HandleButtonDown();
end;
function TAbstractButton.MouseLeftUp(Event: TMouseButtonEvent): TBoolean;
begin
Result := True;
HandleButtonUp();
end;
function TAbstractButton.MouseMove(Event: TMouseMoveEvent): TBoolean;
var
SizeRectangle: TInteger32Rectangle;
begin
Result := False;
if IsMouseLeftDown then
begin
SizeRectangle := Bounds.Size;
Down := SizeRectangle.Contains(Event.Position);
end
else
Hot := True;
end;
procedure TAbstractButton.SetBehavior(const Value: TButtonBehavior);
begin
if Value <> Behavior then
begin
FBehavior := Value;
AdjustFocusability();
AdjustTimers();
end;
end;
procedure TAbstractButton.SetDown(const Value: TBoolean);
begin
if Down <> Value then
begin
FDown := Value;
DownChanged();
Invalidate();
end;
end;
procedure TAbstractButton.SetHot(const Value: TBoolean);
begin
if Value <> Hot then
begin
FHot := Value;
HotChanged();
Invalidate();
end;
end;
{ TButton }
procedure TButton.Paint(Canvas: TCanvas);
var
CaptionRectangle: TInteger32Rectangle;
begin
PaintButton(Canvas, Bounds.Size);
CaptionRectangle := CalculateCaptionRectangle();
if ReceivesKeyboardInput then
PaintFocusRectangle(Canvas, CaptionRectangle.Outset(TInteger32Rectangle.New(1, 1, 1, 1)));
end;
procedure TButton.PaintButton(Canvas: TCanvas; const Bounds: TInteger32Rectangle);
begin
if State = TButtonState.Pressed then
Canvas.Fill := TFill.Create(TPurposeColor.Button.Pressed)
else
Canvas.Fill := TFill.Create(TNamedColor.White);
Canvas.FillRectangle(Bounds);
if State = TButtonState.Hot then
begin
Canvas.Stroke := TStroke.Create(TPurposeColor.Hot, 1);
Canvas.DrawRectangle(TInteger32Rectangle.New(Bounds.Left + 1, Bounds.Top + 1, Bounds.Right - 1, Bounds.Bottom - 1));
Canvas.DrawRectangle(TInteger32Rectangle.New(Bounds.Left + 2, Bounds.Top + 2, Bounds.Right - 2, Bounds.Bottom - 2));
end;
Canvas.Stroke := TStroke.Create(TPurposeColor.DarkControlBorder, 1);
Canvas.DrawRoundedRectangle(T2dFloatingPoint64RoundedRectangle.New(T2dFloatingPoint64Rectangle.New(Bounds), T2dFloatingPoint64Size.NewSquare(1)));
Canvas.OverlayGradient(
TInteger32Rectangle.New(0, Height div 2, Width - 1, Height - 1),
TNamedColor.LightGray,
TFloatingPoint64Interval.New(0.4, 0.05),
T2dDimension.Vertical
);
end;
procedure TButton.SetState(const Value: TButtonState);
begin
if Value <> State then
begin
FState := Value;
Layout();
Invalidate();
end;
end;
procedure TButton.BoundsChanged(const OldBounds: TInteger32Rectangle; const NewBounds: TInteger32Rectangle);
begin
inherited BoundsChanged(OldBounds, NewBounds);
if OldBounds.Size <> NewBounds.Size then
Layout();
end;
function TButton.CalculateCaptionRectangle(): TInteger32Rectangle;
begin
Result := Size;
Result := Result.Inset(GetCaptionInsets());
if State = TButtonState.Pressed then
Result := Result.Offset(TInteger32Point.New(1, 1));
end;
procedure TButton.CaptionChanged();
begin
inherited CaptionChanged();
LabelControl.Text := Caption;
end;
constructor TButton.Create();
begin
inherited Create();
FLabelControl := TSingleLineTextControl.Create();
FLabelControl.AllowedModes := [TControlMode.Display];
FLabelControl.Focusability := TFocusability.None;
FLabelControl.IsEditor := True;
FLabelControl.HorizontalAlignment := THorizontalAlignment.Middle;
FLabelControl.Font := TPurposeFont.Caption;
ChildControls.Add(LabelControl);
end;
function TButton.DetermineState(): TButtonState;
begin
if Down then
Result := TButtonState.Pressed
else if Hot then
Result := TButtonState.Hot
else
Result := TButtonState.Normal;
end;
procedure TButton.DownChanged();
begin
State := DetermineState();
end;
function TButton.GetCaptionInsets(): TInteger32Rectangle;
begin
Result := TInteger32Rectangle.New(4, 4, 5, 5);
end;
function TButton.GetPreferredExtraSize(): TInteger32Size;
begin
Result := TInteger32Size.New(6, 0);
end;
function TButton.GetPreferredSize(AvailableSize: TInteger32Size): TInteger32Size;
var
Insets: TInteger32Rectangle;
InsetsSize: TInteger32Size;
begin
Insets := GetCaptionInsets();
InsetsSize := TInteger32Size.New(Insets.Left + Insets.Right, Insets.Top + Insets.Bottom) + GetPreferredExtraSize();
Result := LabelControl.GetPreferredSize(AvailableSize - InsetsSize) + InsetsSize;
end;
procedure TButton.HasBeenBoundToLayout();
begin
inherited HasBeenBoundToLayout();
Layout();
end;
procedure TButton.HotChanged();
begin
State := DetermineState();
end;
procedure TButton.Layout();
begin
if IsBoundToLayout() then
LabelControl.Bounds := CalculateCaptionRectangle();
end;
end.
|