summaryrefslogtreecommitdiff
path: root/gui/buttons.inc
blob: 4ae69f5d98dff004f4f22385cb196c48c07851db (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
{
    fpGUI  -  Free Pascal Graphical User Interface
    Copyright (C) 2000 - 2001 by
      Areca Systems GmbH / Sebastian Guenther
    Copyright (C) 2006 by Graeme Geldenhuys 
      member of the fpGUI development team.

    Button class declarations

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

    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.

 **********************************************************************}


{%mainunit fpgui.pp}


{$IFDEF read_interface}

  TGenericButton = class(TWidget)
  protected
    FEmbedded: Boolean;
    procedure   Paint(Canvas: TFCanvas); override;
  public
    constructor Create(AOwner: TComponent); override;
    property    Embedded: Boolean read FEmbedded write FEmbedded default False;
  end;


  TCustomButton = class(TGenericButton)
  protected
    procedure   Paint(Canvas: TFCanvas); override;
    procedure   CalcSizes; override;
  public
    constructor Create(const pText: string; pOwner: TComponent); overload;
  end;


  TButton = class(TCustomButton)
  published
    property    CanExpandWidth;
    property    Enabled;
    property    Text;
    property    OnClick;
  end;

{
  TCustomImageButton = class(TGenericButton)
  protected
    procedure Paint(Canvas: TGfxCanvas); override;
    procedure EvRecalcLayout; override;
  end;

  TImageButton = class(TCustomImageButton)
  published
    property Enabled;
    property Image;
    property OnClick;
  end;
}

{$ENDIF read_interface}



{$IFDEF read_implementation}

// ===================================================================
//   TGenericButton
// ===================================================================

constructor TGenericButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
end;


procedure TGenericButton.Paint(Canvas: TFCanvas);
var
  Flags: TButtonFlags;
begin
  if Embedded then
    Flags := [btnIsEmbedded]
  else
    Flags := [];
  if (wsClicked in WidgetState) and (wsMouseInside in WidgetState) then
    Include(Flags, btnIsPressed);
  if (wsHasFocus in WidgetState) and not Embedded then
  begin
    Include(Flags, btnIsSelected);
    if FindForm.IsActive then
      Include(Flags, btnHasFocus);
  end;

  Style.DrawButtonFace(Canvas, Rect(0, 0, BoundsSize.cx, BoundsSize.cy), Flags);
end;


// ===================================================================
//   TCustomButton
// ===================================================================

procedure TCustomButton.Paint(Canvas: TFCanvas);
var
  Pt: TPoint;
  Borders: TRect;
begin
  inherited Paint(Canvas);

  Borders := Style.GetButtonBorders;
  Canvas.IntersectClipRect(Rect(Borders.Left, Borders.Top,
    BoundsSize.cx - Borders.Right, BoundsSize.cy - Borders.Bottom));

  Canvas.SetColor(Style.GetUIColor(clBtnText));
  Pt.x := (BoundsSize.cx - Canvas.TextWidth(Text)) div 2;
  Pt.y := (BoundsSize.cy - Canvas.FontCellHeight) div 2;
  if (wsClicked in WidgetState) and (wsMouseInside in WidgetState) then
    Pt := Pt + Point(1, 1);
  Style.DrawText(Canvas, Pt, Text, WidgetState);
end;


procedure TCustomButton.CalcSizes;
var
  Borders: TRect;
begin
  LAYOUTTRACE('TCustomButton.CalcSizes for %s:%s', [Name, ClassName]);
  Borders := Style.GetButtonBorders;
  with FindForm.Wnd.Canvas do
  begin
    FMinSize.cx := Borders.Left + Borders.Right + TextWidth(Text);
    if FMinSize.cx < 75 then
      FMinSize.cx := 75;    // apply default button width
    FMinSize.cy := Borders.Left + Borders.Right + FontCellHeight;
  end;
  FDefSize := FMinSize + gfxbase.Size(20, 2);
end;


constructor TCustomButton.Create(const pText: string; pOwner: TComponent);
begin
  Create(pOwner);
  Text := pText;
end;


{$ENDIF read_implementation}