summaryrefslogtreecommitdiff
path: root/src/gui/fpg_checkbox.pas
blob: cd0e99201abe1043aba043f7d0119a61098c207d (plain)
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
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2014 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      Defines a CheckBox control. Also known as a Check Button control.
}

unit fpg_checkbox;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main,
  fpg_widget;
  
type

  TfpgBaseCheckBox = class(TfpgWidget)
  private
    FChecked: boolean;
    FOnChange: TNotifyEvent;
    FReadOnly: Boolean;
    FText: string;
    FFont: TfpgFont;
    FBoxLayout: TBoxLayout;
    FBoxSize: integer;
    FImgTextSpacing: integer;
    FIsPressed: boolean;
    function    GetBoxLayout: TBoxLayout;
    function    GetFontDesc: string;
    procedure   SetBoxLayout(const AValue: TBoxLayout);
    procedure   SetChecked(const AValue: boolean);
    procedure   SetFontDesc(const AValue: string);
    procedure   SetReadOnly(const AValue: Boolean);
    procedure   SetText(const AValue: string);
    procedure   DoOnChange;
  protected
    procedure   HandleCheckChanged; virtual;
    procedure   HandlePaint; override;
    procedure   HandleLMouseDown(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
    procedure   HandleKeyRelease(var keycode: word; var shiftstate: TShiftState; var consumed: boolean); override;
    property    Checked: boolean read FChecked write SetChecked default False;
    property    FontDesc: string read GetFontDesc write SetFontDesc;
    property    BoxLayout: TBoxLayout read GetBoxLayout write SetBoxLayout default tbLeftBox;
    property    ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
    property    Text: string read FText write SetText;
    property    OnChange: TNotifyEvent read FOnChange write FOnChange;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    property    Font: TfpgFont read FFont;
  end;


  TfpgCheckBox = class(TfpgBaseCheckBox)
  published
    property    Align;
    property    BackgroundColor;
    property    BoxLayout;
    property    Checked;
    property    Enabled;
    property    FontDesc;
    property    Height;
    property    Hint;
    property    Left;
    property    MaxHeight;
    property    MaxWidth;
    property    MinHeight;
    property    MinWidth;
    property    ParentShowHint;
    property    ReadOnly;
    property    ShowHint;
    property    TabOrder;
    property    Text;
    property    TextColor;
    property    Top;
    property    Width;
    property    OnChange;
    property    OnEnter;
    property    OnExit;
    property    OnShowHint;
  end;


function CreateCheckBox(AOwner: TComponent; x, y: TfpgCoord; AText: string): TfpgCheckBox;


implementation


function CreateCheckBox(AOwner: TComponent; x, y: TfpgCoord; AText: string): TfpgCheckBox;
begin
  Result := TfpgCheckBox.Create(AOwner);
  Result.Top    := y;
  Result.Left   := x;
  Result.Text   := AText;
  Result.Width  := Result.Font.TextWidth(Result.Text) + 24;
end;

{ TfpgBaseCheckBox }

procedure TfpgBaseCheckBox.SetChecked(const AValue: boolean);
begin
  if ReadOnly then
    Exit; //==>
  if FChecked = AValue then
    Exit; //==>
  FChecked := AValue;
  HandleCheckChanged;
  RePaint;
  if not (csDesigning in ComponentState) then
    DoOnChange;
end;

function TfpgBaseCheckBox.GetBoxLayout: TBoxLayout;
begin
  Result := FBoxLayout;
end;

function TfpgBaseCheckBox.GetFontDesc: string;
begin
  Result := FFont.FontDesc;
end;

procedure TfpgBaseCheckBox.SetBoxLayout(const AValue: TBoxLayout);
begin
  if FBoxLayout = AValue then
    Exit; //==>
  FBoxLayout := AValue;
  RePaint;
end;

procedure TfpgBaseCheckBox.SetFontDesc(const AValue: string);
begin
  FFont.Free;
  FFont := fpgGetFont(AValue);
  { TODO: Implement AutoSize property, then adjust width here if True }
  RePaint;
end;

procedure TfpgBaseCheckBox.SetReadOnly(const AValue: Boolean);
begin
  if FReadOnly=AValue then exit;
  FReadOnly:=AValue;
  RePaint;
end;

procedure TfpgBaseCheckBox.SetText(const AValue: string);
begin
  if FText = AValue then
    Exit; //==>
  FText := AValue;
  RePaint;
end;

procedure TfpgBaseCheckBox.DoOnChange;
begin
  if Assigned(FOnChange) then
    FOnChange(self);
end;

procedure TfpgBaseCheckBox.HandleCheckChanged;
begin
  // nothing here for us
end;

procedure TfpgBaseCheckBox.HandlePaint;
var
  r: TfpgRect;
  ix: integer;
  img: TfpgImage;
  LFlags: TfpgTextFlags;
begin
  inherited HandlePaint;
  Canvas.ClearClipRect;
  Canvas.SetColor(FBackgroundColor);
  Canvas.FillRectangle(0, 0, Width, Height);
  Canvas.SetFont(Font);
  Canvas.SetLineStyle(1, lsSolid);

  if FBoxLayout = tbLeftBox then
    r.SetRect(2, ((Height - FBoxSize) div 2), FBoxSize, FBoxSize)
  else
    r.SetRect(Width - FBoxSize - 2, ((Height - FBoxSize) div 2), FBoxSize, FBoxSize);
  if r.top < 0 then
    r.top := 0;

  // calculate which image to paint.
  if Enabled then
  begin
    if ReadOnly then
      ix := (2 + (Ord(FChecked) * 2)) - Ord(FChecked)
    else
    begin
      ix := Ord(FChecked);
      if FIsPressed then
        Inc(ix, 2);
    end;
  end
  else
    ix := (2 + (Ord(FChecked) * 2)) - Ord(FChecked);

  // paint the check (in this case a X)
  fpgStyle.DrawCheckbox(Canvas, r.Left, r.Top, ix*FBoxSize, 0);

  r := GetClientRect;
  { max focus rectangle and text boundry }
  InflateRect(r, -1, -1);
  { exclude the checkbox image and spacing from rectangle }
  if FBoxLayout = tbLeftBox then
  begin
    r.Left  := r.Left + FBoxSize + FImgTextSpacing;
    r.Width := r.Width - FBoxSize - FImgTextSpacing;
  end
  else
    r.Width := r.Width - FBoxSize - FImgTextSpacing;

  Canvas.SetTextColor(FTextColor);
  Canvas.SetClipRect(r);

  if Enabled then
    LFlags := [txtLeft, txtVCenter]
  else
    LFlags := [txtLeft, txtVCenter, txtDisabled];
  Canvas.DrawText(r, FText, LFlags);   { internally this still calls fpgStyle.DrawString(), so theming will be applied }

  if FFocused then
  begin
    Canvas.ClearClipRect;
    { adjust focusrect-to-text margin }
    if FBoxLayout = tbLeftBox then
    begin
      r.Left := r.Left - 2;
      r.Width := r.Width + 2;
    end
    else
    begin
      r.Width := r.Width + 2;
    end;
    { undo the 2px focusrect-to-text margin, so we simply use the clip rect }
    fpgStyle.DrawFocusRect(Canvas, r);
  end;
end;

procedure TfpgBaseCheckBox.HandleLMouseDown(x, y: integer; shiftstate: TShiftState);
begin
  inherited HandleLMouseDown(x, y, shiftstate);
  FIsPressed := True;
  Repaint;
end;

procedure TfpgBaseCheckBox.HandleLMouseUp(x, y: integer; shiftstate: TShiftState);
begin
  inherited HandleLMouseUp(x, y, shiftstate);
  FIsPressed := False;
  Checked := not FChecked;
end;

procedure TfpgBaseCheckBox.HandleKeyRelease(var keycode: word;
  var shiftstate: TShiftState; var consumed: boolean);
begin
  if (keycode = keySpace) or (keycode = keyReturn) or (keycode = keyPEnter) then
  begin
    consumed := True;
    Checked := not FChecked;
  end;

  if consumed then
    Exit; //==>

  inherited HandleKeyRelease(keycode, shiftstate, consumed);
end;

constructor TfpgBaseCheckBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FText       := 'CheckBox';
  FFont       := fpgGetFont('#Label1');
  FHeight     := FFont.Height + 4;
  FWidth      := 120;
  FTextColor  := Parent.TextColor;
  FBackgroundColor := Parent.BackgroundColor;
  FFocusable  := True;
  FBoxSize    := fpgStyle.GetCheckBoxSize;
  FImgTextSpacing := 6;
  FChecked    := False;
  FIsPressed  := False;
  FOnChange   := nil;
  FBoxLayout  := tbLeftBox;
  FReadOnly   := False;
end;

destructor TfpgBaseCheckBox.Destroy;
begin
  FFont.Free;
  inherited Destroy;
end;

end.