summaryrefslogtreecommitdiff
path: root/gui/db/fpgui_db.pas
blob: 10f0e5a67baaced84d4e03044e99667ff8d5d5f3 (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
{
    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.

    fgGUI database support

    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.

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


unit fpGUI_DB;

{$IFDEF Debug}
{$ASSERTIONS On}
{$ENDIF}

interface

uses Classes, fpGUI, DB;

type
  TFieldDataLink = class(TDataLink)
  private
    FWidget: TWidget;
    FField: TField;
    FFieldName: String;
    FOnDataChange: TNotifyEvent;
    procedure SetFieldName(const AFieldName: String);
    procedure UpdateField;
  protected
    procedure ActiveChanged; override;
    procedure RecordChanged(AField: TField); override;
  public
    constructor Create(AWidget: TWidget);
    property Field: TField read FField;
    property FieldName: String read FFieldName write SetFieldName;
    property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange;
  end;

  TDBText = class(TCustomLabel)
  private
    FDataLink: TFieldDataLink;
    function GetDataField: String;
    procedure SetDataField(const ADataField: String);
    function GetDataSource: TDataSource;
    procedure SetDataSource(ADataSource: TDataSource);
    procedure DataChange(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Text;
    property DataField: String read GetDataField write SetDataField;
    property DataSource: TDataSource read GetDataSource write SetDataSource;
  end;


// ===================================================================
// ===================================================================

implementation


constructor TFieldDataLink.Create(AWidget: TWidget);
begin
  inherited Create;
  FWidget := AWidget;
end;

procedure TFieldDataLink.ActiveChanged;
begin
  UpdateField;
end;

procedure TFieldDataLink.RecordChanged(AField: TField);
begin
  if Assigned(OnDataChange) then
    OnDataChange(Self);
end;

procedure TFieldDataLink.SetFieldName(const AFieldName: String);
begin
  if AFieldName <> FieldName then
  begin
    FFieldName := AFieldName;
    UpdateField;
  end;
end;

procedure TFieldDataLink.UpdateField;
begin
WriteLn('##############UpdateField. DataSet: ', DataSource.DataSet.ClassName);
  FField := DataSource.DataSet.FindField(FieldName);
  if Assigned(OnDataChange) then
    OnDataChange(Self);
end;


constructor TDBText.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FDataLink := TFieldDataLink.Create(Self);
  FDataLink.OnDataChange := @DataChange;
end;

destructor TDBText.Destroy;
begin
  FDataLink.Free;
  inherited Destroy;
end;

function TDBText.GetDataField: String;
begin
  Result := FDataLink.FieldName;
end;

procedure TDBText.SetDataField(const ADataField: String);
begin
  FDataLink.FieldName := ADataField;
end;

function TDBText.GetDataSource: TDataSource;
begin
  Result := FDataLink.DataSource;
end;

procedure TDBText.SetDataSource(ADataSource: TDataSource);
begin
  FDataLink.DataSource := ADataSource;
end;

procedure TDBText.DataChange(Sender: TObject);
begin
Write('TDBText.DataChange');
  if Assigned(FDataLink.Field) then
  begin
    Text := FDataLink.Field.DisplayText;
    WriteLn(' new text: "', Text, '"');
  end else
  begin
    Text := '';
    WriteLn('DataLink has no data');
  end;
end;


end.


{
  $Log: fpgui_db.pp,v $
  Revision 1.2  2001/01/17 21:36:26  sg
  * Updating fixes

  Revision 1.1  2000/12/23 23:20:16  sg
  * First public CVS version...

}