lazarus/lcl/include/dbcheckbox.inc
mattias 55955882c7 started TDBCheckBox
git-svn-id: trunk@4631 -
2003-09-16 11:35:14 +00:00

150 lines
3.8 KiB
PHP

// included by dbctrls.pas
{
*****************************************************************************
* *
* This file is part of the Lazarus Component Library (LCL) *
* *
* See the file COPYING.LCL, 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. *
* *
*****************************************************************************
}
{ TDBCheckBox }
function TDBCheckBox.GetDataField: string;
begin
Result:=FDataLink.FieldName;
end;
function TDBCheckBox.GetDataSource: TDataSource;
begin
Result:=FDataLink.DataSource;
end;
function TDBCheckBox.GetField: TField;
begin
Result:=FDataLink.Field;
end;
function TDBCheckBox.GetReadOnly: Boolean;
begin
Result:=FDataLink.ReadOnly;
end;
procedure TDBCheckBox.SetDataField(const AValue: string);
begin
FDataLink.FieldName:=AValue;
end;
procedure TDBCheckBox.SetDataSource(const AValue: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource:=AValue;
if AValue<>nil then
AValue.FreeNotification(Self);
end;
procedure TDBCheckBox.SetReadOnly(const AValue: Boolean);
begin
FDataLink.ReadOnly:=AValue;
end;
procedure TDBCheckBox.SetValueCheck(const AValue: string);
begin
if FValueCheck=AValue then exit;
FValueCheck:=AValue;
DataChange(Self);
end;
procedure TDBCheckBox.SetValueUncheck(const AValue: string);
begin
if FValueUncheck=AValue then exit;
FValueUncheck:=AValue;
DataChange(Self);
end;
function TDBCheckBox.GetFieldCheckState: TCheckBoxState;
var
FieldText: string;
begin
if FDatalink.Field=nil then begin
Result:=cbUnchecked;
exit;
end;
if FDataLink.Field.IsNull then
Result:=cbGrayed
else if FDataLink.Field.DataType = ftBoolean then begin
if FDataLink.Field.AsBoolean then
Result:=cbChecked
else
Result:=cbUnchecked;
end else begin
Result:=cbGrayed;
// ToDo: use Field.Text
FieldText:=FDataLink.Field.DisplayText;
if ValueEqualsField(FValueCheck,FieldText) then
Result:=cbChecked
else if ValueEqualsField(FValueUncheck,FieldText) then
Result:=cbUnchecked;
end;
end;
procedure TDBCheckBox.DataChange(Sender: TObject);
begin
State:=GetFieldCheckState;
end;
procedure TDBCheckBox.UpdateData(Sender: TObject);
var
NewFieldText: string;
begin
if State = cbGrayed then
FDataLink.Field.Clear
else
if FDataLink.Field.DataType = ftBoolean then
FDataLink.Field.AsBoolean:=Checked
else begin
if Checked then
NewFieldText:=FValueCheck
else
NewFieldText:=FValueUncheck;
// ToDo: use Field.Text
FDataLink.Field.AsString:=Trim(NewFieldText);
end;
end;
function TDBCheckBox.ValueEqualsField(const AValue, AFieldText: string
): boolean;
begin
Result:=AnsiCompareText(AValue,AFieldText)=0;
end;
constructor TDBCheckBox.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FValueCheck:='True';
FValueUncheck:='False';
ControlStyle:=ControlStyle+[csReplicatable];
State:=cbUnchecked;
FDataLink:=TFieldDataLink.Create;
FDataLink.Control:=Self;
FDataLink.OnDataChange:=@DataChange;
FDataLink.OnUpdateData:=@UpdateData;
end;
destructor TDBCheckBox.Destroy;
begin
FDataLink.Free;
FDataLink:=nil;
inherited Destroy;
end;
// included by dbctrls.pas