File: dbcheckbox.inc

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (232 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download | duplicates (3)
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
{%MainUnit ../dbctrls.pp}
{
 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}

{ 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 AValue=DataSource then exit;
  if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
    ChangeDataSource(Self,FDataLink,AValue);
end;

procedure TDBCheckBox.SetReadOnly(const AValue: Boolean);
begin
  FDataLink.ReadOnly:=AValue;
end;

procedure TDBCheckBox.SetValueChecked(const AValue: string);
begin
  if FValueChecked=AValue then exit;
  FValueChecked:=AValue;
  DataChange(Self);
end;

procedure TDBCheckBox.SetValueUnchecked(const AValue: string);
begin
  if FValueUnchecked=AValue then exit;
  FValueUnchecked:=AValue;
  DataChange(Self);
end;

//check if Word is equal to S or is one of the ; delimitted words in s
//whitespace between Word and delimiter is ignored (Delphi behavior)
function FindWord(const Word, S: String): Boolean;
var
  I, J, L: Integer;
  C: Char;
begin
  I := Pos(Word, S);
  Result := I > 0;
  if Result then
  begin
    //forward
    J := I + Length(Word);
    L := Length(S);
    while Result and (J < L) do
    begin
      C := S[J];
      if C = ';' then
        Break;
      Result := C = ' ';
      Inc(J);
    end;
    //backward
    Dec(I);
    while Result and (I > 0) do
    begin
      C := S[I];
      if C = ';' then
        Break;
      Result := C = ' ';
      Dec(I);
    end;
  end;
end;

function TDBCheckBox.GetFieldCheckState: TCheckBoxState;
var
  FieldText: string;
  DataLinkField: TField;
begin
  DataLinkField := FDataLink.Field;
  if DatalinkField=nil then begin
    Result:=cbUnchecked;
    exit;
  end;
  if DataLinkField.IsNull then
    Result:=cbGrayed
  else if DataLinkField.DataType = ftBoolean then begin
    if DataLinkField.AsBoolean then
      Result:=cbChecked
    else
      Result:=cbUnchecked;
  end else begin
    FieldText:=UpperCase(DatalinkField.AsString);
    if FindWord(FieldText,UpperCase(FValueChecked)) then
      Result:=cbChecked
    else if FindWord(FieldText,UpperCase(FValueUnchecked)) then
      Result:=cbUnchecked
    else
      Result:=cbGrayed;
  end;
end;

procedure TDBCheckBox.DataChange(Sender: TObject);
begin
  // avoid DoOnChange circle #33573
  FDataLink.OnDataChange := nil;
  try
    State := GetFieldCheckState;
  finally
    FDataLink.OnDataChange := @DataChange;
  end;
end;

procedure TDBCheckBox.DoOnChange;
begin
  // avoid DoOnChange circle #33573
  if FDataLink.OnDataChange <> nil then
  try
    //avoid reseting value when state changes
    FDataLink.OnDataChange := nil;
    if FDatalink.Edit then begin
      FDatalink.Modified;
      FDataLink.UpdateRecord;
    end else
      State := GetFieldCheckState;
  finally
    FDataLink.OnDataChange := @DataChange;
  end;

  inherited DoOnChange;
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:=FValueChecked
      else
        NewFieldText:=FValueUnchecked;
      // ToDo: use Field.Text
      FDataLink.Field.AsString:=Trim(NewFieldText);
    end;
end;

procedure TDBCheckBox.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation=opRemove) then begin
    if (FDataLink<>nil) and (AComponent=DataSource) then
      DataSource:=nil;
  end;
end;

procedure TDBCheckBox.CMGetDataLink(var Message: TLMessage);
begin
  Message.Result := PtrUInt(FDataLink);
end;

function TDBCheckBox.NonDefaultValueChecked: Boolean;
begin
  Result := not AnsiSameText(FValueChecked, BoolToStr(True));
end;

function TDBCheckBox.NonDefaultValueUnchecked: Boolean;
begin
  Result := not AnsiSameText(FValueChecked, BoolToStr(False));
end;

constructor TDBCheckBox.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  FValueChecked := BoolToStr(True);
  FValueUnchecked := BoolToStr(False);

  ControlStyle:=ControlStyle+[csReplicatable];
  State:=cbUnchecked;
  FDataLink:=TFieldDataLink.Create;
  FDataLink.Control:=Self;
  FDataLink.OnDataChange:=@DataChange;
  FDataLink.OnUpdateData:=@UpdateData;
end;

destructor TDBCheckBox.Destroy;
begin
  FDataLink.Destroy;
  inherited Destroy;
end;

function TDBCheckBox.ExecuteAction(AAction: TBasicAction): Boolean;
begin
  Result := inherited ExecuteAction(AAction) or
            (FDataLink <> nil) and FDataLink.ExecuteAction(AAction);
end;

function TDBCheckBox.UpdateAction(AAction: TBasicAction): Boolean;
begin
  Result := inherited UpdateAction(AAction) or
            (FDataLink <> nil) and FDataLink.UpdateAction(AAction);
end;

// included by dbctrls.pp