/
JvGroupBox.pas
317 lines (284 loc) · 8.43 KB
/
JvGroupBox.pas
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
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvGroupBox.PAS, released on 2000-11-22.
The Initial Developer of the Original Code is Peter Below <100113 dott 1101 att compuserve dott com>
Portions created by Peter Below are Copyright (C) 2000 Peter Below.
All Rights Reserved.
Contributor(s):
Roman Ganz
Robert Marquardt
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Known Issues:
-----------------------------------------------------------------------------}
// $Id$
unit JvGroupBox;
{$I jvcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
SysUtils, Classes, Windows, Messages, Graphics, Controls, Forms, StdCtrls,
JvThemes, JvExControls, JvExStdCtrls, JvCheckBox, JvJCLUtils;
type
{$IFDEF RTL230_UP}
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
{$ENDIF RTL230_UP}
TJvGroupBox = class(TJvExGroupBox, IJvDenySubClassing)
private
FCheckBox: TJvCheckBox;
FOnHotKey: TNotifyEvent;
FPropagateEnable: Boolean;
FCheckable: Boolean;
FOnCheckBoxClick: TNotifyEvent;
procedure SetPropagateEnable(const Value: Boolean);
procedure SetCheckable(const Value: Boolean);
function GetCaption: TCaption;
procedure SetCaption(const Value: TCaption);
function GetChecked: Boolean;
procedure SetChecked(const Value: Boolean);
function StoredCheckable: Boolean;
procedure CheckBoxClick(Sender: TObject);
protected
function WantKey(Key: Integer; Shift: TShiftState): Boolean; override;
procedure EnabledChanged; override;
procedure DoHotKey; dynamic;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
property Canvas;
published
property HintColor;
{$IFDEF JVCLThemesEnabledD6}
property ParentBackground default True;
{$ENDIF JVCLThemesEnabledD6}
property Caption: TCaption read GetCaption write SetCaption;
property Checkable: Boolean read FCheckable write SetCheckable default False;
property Checked: Boolean read GetChecked write SetChecked stored StoredCheckable;
property PropagateEnable: Boolean read FPropagateEnable write SetPropagateEnable default False;
property OnMouseEnter;
property OnMouseLeave;
property OnParentColorChange;
property OnHotKey: TNotifyEvent read FOnHotKey write FOnHotKey;
property OnCheckBoxClick: TNotifyEvent read FOnCheckBoxClick write FOnCheckBoxClick;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
Math;
constructor TJvGroupBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPropagateEnable := False;
FCheckable := False;
ControlStyle := ControlStyle + [csAcceptsControls];
{$IFDEF JVCLThemesEnabledD6}
IncludeThemeStyle(Self, [csParentBackground]);
{$ENDIF JVCLThemesEnabledD6}
end;
procedure TJvGroupBox.Paint;
var
H: Integer;
R: TRect;
Flags: Longint;
{$IFDEF JVCLThemesEnabledD6}
Details: TThemedElementDetails;
CaptionRect: TRect;
{$ENDIF JVCLThemesEnabledD6}
LastBkMode: Integer;
begin
{$IFDEF JVCLThemesEnabled}
if StyleServices.Enabled then
begin
{$IFDEF COMPILER7_UP}
inherited Paint;
{$ELSE}
if Enabled then
Details := StyleServices.GetElementDetails(tbGroupBoxNormal)
else
Details := StyleServices.GetElementDetails(tbGroupBoxDisabled);
R := ClientRect;
Inc(R.Top, Canvas.TextHeight('0') div 2);
StyleServices.DrawElement(Canvas.Handle, Details, R);
CaptionRect := Rect(8, 0, Min(Canvas.TextWidth(Caption) + 8, ClientWidth - 8),
Canvas.TextHeight(Caption));
Canvas.Brush.Color := Self.Color;
DrawThemedBackground(Self, Canvas, CaptionRect);
StyleServices.DrawText(Canvas.Handle, Details, Caption, CaptionRect, DT_LEFT, 0);
{$ENDIF COMPILER7_UP}
Exit;
end;
{$ENDIF JVCLThemesEnabled}
with Canvas do
begin
LastBkMode := GetBkMode(Handle);
try
Font := Self.Font;
H := TextHeight('0');
R := Rect(0, H div 2 - 1, Width, Height);
if Ctl3D then
begin
Inc(R.Left);
Inc(R.Top);
Brush.Color := clBtnHighlight;
FrameRect( R);
OffsetRect(R, -1, -1);
Brush.Color := clBtnShadow;
end
else
Brush.Color := clWindowFrame;
FrameRect( R);
if Text <> '' then
begin
if not UseRightToLeftAlignment then
R := Rect(8, 0, 0, H)
else
R := Rect(R.Right - Canvas.TextWidth(Text) - 8, 0, 0, H);
Flags := DrawTextBiDiModeFlags(DT_SINGLELINE);
// calculate text rect
SetBkMode(Handle, OPAQUE);
DrawText(Handle, Text, Length(Text), R, Flags or DT_CALCRECT);
Brush.Color := Color;
if not Enabled then
begin
OffsetRect(R, 1, 1);
Font.Color := clBtnHighlight;
DrawText(Canvas, Text, Length(Text), R, Flags);
OffsetRect(R, -1, -1);
Font.Color := clBtnShadow;
SetBkMode(Handle, TRANSPARENT);
DrawText(Canvas, Text, Length(Text), R, Flags);
end
else
DrawText(Canvas, Text, Length(Text), R, Flags);
end;
finally
SetBkMode(Handle, LastBkMode);
end;
end;
end;
function TJvGroupBox.WantKey(Key: Integer; Shift: TShiftState): Boolean;
begin
Result := inherited WantKey(Key, Shift);
if Result then
DoHotKey;
end;
procedure TJvGroupBox.EnabledChanged;
var
I: Integer;
begin
inherited EnabledChanged;
if PropagateEnable then
for I := 0 to ControlCount - 1 do
if Checkable then
if Enabled then
if Controls[I] = FCheckBox then
Controls[I].Enabled := True
else
Controls[I].Enabled := Checked
else
Controls[I].Enabled := False
else
Controls[I].Enabled := Enabled;
Invalidate;
end;
procedure TJvGroupBox.DoHotKey;
begin
if Assigned(FOnHotKey) then
FOnHotKey(Self);
end;
function TJvGroupBox.GetCaption: TCaption;
begin
if FCheckable then
Result := FCheckBox.Caption
else
Result := inherited Caption;
end;
function TJvGroupBox.GetChecked: Boolean;
begin
if FCheckable then
Result := FCheckBox.Checked
else
Result := False;
end;
procedure TJvGroupBox.SetCaption(const Value: TCaption);
begin
if FCheckable then
FCheckBox.Caption := Value
else
inherited Caption := Value;
end;
procedure TJvGroupBox.SetCheckable(const Value: Boolean);
begin
if FCheckable <> Value then
begin
if Value then
begin
FCheckBox := TJvCheckBox.Create(Self);
FCheckBox.Parent := Self;
FCheckBox.Top := 0;
FCheckBox.Left := 8;
FCheckBox.Caption := Caption;
PropagateEnable := True;
FCheckBox.OnClick := CheckBoxClick;
FCheckBox.Checked := True;
inherited Caption := '';
end
else
begin
inherited Caption := FCheckBox.Caption;
FreeAndNil(FCheckBox);
end;
FCheckable := Value;
end;
end;
procedure TJvGroupBox.SetChecked(const Value: Boolean);
begin
if Checkable then
FCheckBox.Checked := Value;
end;
procedure TJvGroupBox.CheckBoxClick(Sender: TObject);
var
I: Integer;
begin
for I := 0 to ControlCount - 1 do
if Controls[I] <> FCheckBox then
Controls[I].Enabled := FCheckBox.Checked;
if Assigned(FOnCheckBoxClick) then
FOnCheckBoxClick(Self);
end;
procedure TJvGroupBox.SetPropagateEnable(const Value: Boolean);
var
I: Integer;
begin
FPropagateEnable := Value;
for I := 0 to ControlCount - 1 do
Controls[I].Enabled := Enabled;
end;
function TJvGroupBox.StoredCheckable: Boolean;
begin
{ Write "False" to the DFM file because the checkbox is initialized with "True" }
Result := FCheckable and not Checked;
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.