diff options
author | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2007-10-28 22:12:15 +0000 |
---|---|---|
committer | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2007-10-28 22:12:15 +0000 |
commit | 475482d47ccc2b9fb74e0cc933716bc5f4a573cf (patch) | |
tree | 19f9ad8e0ef55908668f1211e637ef2efaa28aae /src/gui | |
parent | 3092f78b128cfcb01bd49165e1dd0f4ca8e5e621 (diff) | |
download | fpGUI-475482d47ccc2b9fb74e0cc933716bc5f4a573cf.tar.xz |
* Refactored the TfpgLabel component.
* Implemented a TfpgDBLabel (data-aware) label component.
* Created a new DB test example to show how TfpgDBLabel works.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/db/fpgui_db.pas | 214 | ||||
-rw-r--r-- | src/gui/fpgui_package.lpk | 10 | ||||
-rw-r--r-- | src/gui/fpgui_package.pas | 2 | ||||
-rw-r--r-- | src/gui/gui_button.pas | 4 | ||||
-rw-r--r-- | src/gui/gui_label.pas | 64 |
5 files changed, 266 insertions, 28 deletions
diff --git a/src/gui/db/fpgui_db.pas b/src/gui/db/fpgui_db.pas new file mode 100644 index 00000000..170f9877 --- /dev/null +++ b/src/gui/db/fpgui_db.pas @@ -0,0 +1,214 @@ +{ + fpGUI - Free Pascal GUI Library + + Copyright (C) 2006 - 2007 See the file AUTHORS.txt, included in this + distribution, for details of the copyright. + + See the file COPYING.modifiedLGPL, included in this distribution, + for details about redistributing fpGUI. + + 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. + + Description: + Database support classes. +} + +unit fpgui_db; + +{$mode objfpc}{$H+} + +// If enabled it outputs debug information for this unit +{.$Define DEBUG} + +interface + +uses + Classes, + db, + gfx_widget, + gui_label{, gui_edit}; + +type + + TfpgFieldDataLink = class(TDataLink) + private + FWidget: TfpgWidget; + FField: TField; + FFieldName: string; + FOnDataChange: TNotifyEvent; + function GetCanModify: Boolean; + procedure SetFieldName(const AFieldName: string); + procedure UpdateField; + protected + procedure ActiveChanged; override; + procedure RecordChanged(AField: TField); override; + public + constructor Create(AWidget: TfpgWidget); + property CanModify: Boolean read GetCanModify; + property Field: TField read FField; + property FieldName: string read FFieldName write SetFieldName; + property Widget: TfpgWidget read FWidget write FWidget; + property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange; + end; + + + TfpgDBLabel = class(TfpgCustomLabel) + private + FDataLink: TfpgFieldDataLink; + function GetDataField: String; + function GetField: TField; + 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; + property Field: TField read GetField; + published + property DataField: string read GetDataField write SetDataField; + property DataSource: TDataSource read GetDataSource write SetDataSource; + property Enabled; + property BackgroundColor; + property Color; + property FontDesc; + property Text; + end; + +{ + // TfpgEdit needs to be refactor some more first!! + TfpgDBEdit = class(TfpgCustomEdit) + private + FDataLink: TfpgFieldDataLink; + function GetDataField: string; + function GetDataSource: TDataSource; + function GetField: TField; + function GetReadOnly: Boolean; + procedure SetDataField(const ADataField: string); + procedure SetDataSource(const ADataSource: TDataSource); + procedure DataChange(Sender: TObject); + procedure SetReadOnly(const AValue: Boolean); + protected + public + constructor Create(AOwner: TComponent); override; + destructor Destroy; override; + property Field: TField read GetField; + published + property DataField: string read GetDataField write SetDataField; + property DataSource: TDataSource read GetDataSource write SetDataSource; + property Enabled; + property BackgroundColor; + property Color; + property FontDesc; + property Text; + property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False; + end; +} + + +implementation + + +{ TfpgFieldDataLink } + +function TfpgFieldDataLink.GetCanModify: Boolean; +begin + Result := not ReadOnly and (Field <> nil) and Field.CanModify; +end; + +procedure TfpgFieldDataLink.SetFieldName(const AFieldName: string); +begin + if AFieldName <> FieldName then + begin + FFieldName := AFieldName; + UpdateField; + end; +end; + +procedure TfpgFieldDataLink.UpdateField; +begin + {$IFDEF DEBUG} WriteLn('## UpdateField. DataSet: ', DataSource.DataSet.ClassName); {$ENDIF} + FField := DataSource.DataSet.FindField(FieldName); + if Assigned(OnDataChange) then + OnDataChange(Self); +end; + +procedure TfpgFieldDataLink.ActiveChanged; +begin + inherited ActiveChanged; + UpdateField; +end; + +procedure TfpgFieldDataLink.RecordChanged(AField: TField); +begin + inherited RecordChanged(AField); + if Assigned(OnDataChange) then + OnDataChange(Self); +end; + +constructor TfpgFieldDataLink.Create(AWidget: TfpgWidget); +begin + inherited Create; + FWidget := AWidget; +end; + + +{ TfpgDBLabel } + +function TfpgDBLabel.GetDataField: String; +begin + Result := FDataLink.FieldName; +end; + +function TfpgDBLabel.GetField: TField; +begin + Result := FDataLink.Field; +end; + +procedure TfpgDBLabel.SetDataField(const ADataField: String); +begin + FDataLink.FieldName := ADataField; +end; + +function TfpgDBLabel.GetDataSource: TDataSource; +begin + Result := FDataLink.DataSource; +end; + +procedure TfpgDBLabel.SetDataSource(ADataSource: TDataSource); +begin + FDataLink.DataSource := ADataSource; +end; + +procedure TfpgDBLabel.DataChange(Sender: TObject); +begin + {$IFDEF DEBUG} Write(Classname + '.DataChange'); {$ENDIF} + if Assigned(FDataLink.Field) then + begin + Text := FDataLink.Field.DisplayText; + {$IFDEF DEBUG} WriteLn(' new text: "', Text, '"'); {$ENDIF} + end + else + begin + Text := ''; + {$IFDEF DEBUG} WriteLn('DataLink has no data'); {$ENDIF} + end; +end; + +constructor TfpgDBLabel.Create(AOwner: TComponent); +begin + inherited Create(AOwner); + FDataLink := TfpgFieldDataLink.Create(Self); + FDataLink.OnDataChange := @DataChange; +end; + +destructor TfpgDBLabel.Destroy; +begin + FDataLink.Free; + inherited Destroy; +end; + +end. + diff --git a/src/gui/fpgui_package.lpk b/src/gui/fpgui_package.lpk index 901eacc9..e002da0d 100644 --- a/src/gui/fpgui_package.lpk +++ b/src/gui/fpgui_package.lpk @@ -1,12 +1,12 @@ <?xml version="1.0"?> <CONFIG> - <Package Version="2"> + <Package Version="3"> <Name Value="fpgui_package"/> <Author Value="Graeme Geldenhuys"/> - <AutoUpdate Value="OnRebuildingAll"/> <CompilerOptions> <Version Value="5"/> <SearchPaths> + <OtherUnitFiles Value="db/"/> <UnitOutputDirectory Value="../../lib"/> </SearchPaths> <Parsing> @@ -26,7 +26,7 @@ <License Value="Modified LGPL "/> <Version Minor="5" Release="1"/> - <Files Count="24"> + <Files Count="25"> <Item1> <Filename Value="gui_button.pas"/> <UnitName Value="gui_button"/> @@ -123,6 +123,10 @@ <Filename Value="gui_mru.pas"/> <UnitName Value="gui_mru"/> </Item24> + <Item25> + <Filename Value="db/fpgui_db.pas"/> + <UnitName Value="fpgui_db"/> + </Item25> </Files> <RequiredPkgs Count="2"> <Item1> diff --git a/src/gui/fpgui_package.pas b/src/gui/fpgui_package.pas index 662408ba..d26bd4e1 100644 --- a/src/gui/fpgui_package.pas +++ b/src/gui/fpgui_package.pas @@ -11,7 +11,7 @@ uses gui_listbox, gui_memo, gui_scrollbar, gui_bevel, gui_checkbox, gui_radiobutton, gui_trackbar, gui_tab, gui_basegrid, gui_listview, gui_customgrid, gui_progressbar, gui_menu, gui_style, gui_grid, gui_tree, - gui_iniutils, gui_mru; + gui_iniutils, gui_mru, fpgui_db; implementation diff --git a/src/gui/gui_button.pas b/src/gui/gui_button.pas index ff29a385..52f608ed 100644 --- a/src/gui/gui_button.pas +++ b/src/gui/gui_button.pas @@ -90,11 +90,15 @@ type property ModalResult: integer read FModalResult write FModalResult default 0; property Embedded: Boolean read FEmbedded write SetEmbedded default False; property ShowImage: Boolean read FShowImage write SetShowImage default True; + property OnMouseExit; + property OnMouseEnter; end; + function CreateButton(AOwner: TComponent; x, y, w: TfpgCoord; AText: string; AOnClickEvent: TNotifyEvent): TfpgButton; + implementation uses diff --git a/src/gui/gui_label.pas b/src/gui/gui_label.pas index 85caadda..efdb9bdb 100644 --- a/src/gui/gui_label.pas +++ b/src/gui/gui_label.pas @@ -30,7 +30,7 @@ uses type - TfpgLabel = class(TfpgWidget) + TfpgCustomLabel = class(TfpgWidget) private FAutoSize: boolean; FBackgroundColor: TfpgColor; @@ -46,25 +46,40 @@ type FText: string; FFont: TfpgFont; procedure HandlePaint; override; - public - constructor Create(AOwner: TComponent); override; - destructor Destroy; override; - property Font: TfpgFont read FFont; - published property AutoSize: boolean read FAutoSize write SetAutoSize default False; property Text: string read FText write SetText; property FontDesc: string read GetFontDesc write SetFontDesc; property Color: TfpgColor read FColor write SetColor; property BackgroundColor: TfpgColor read FBackgroundColor write SetBackgroundColor; + public + constructor Create(AOwner: TComponent); override; + destructor Destroy; override; + property Font: TfpgFont read FFont; + end; + + + TfpgLabel = class(TfpgCustomLabel) + published + property AutoSize; + property BackgroundColor; + property Color; + property FontDesc; + property Text; + property OnMouseEnter; + property OnMouseExit; + property OnMouseDown; + property OnMouseUp; property OnMouseMove; end; - TLabelClass = class of TfpgLabel; +// A convenience function to create a TfpgLabel instance function CreateLabel(AOwner: TComponent; x, y: TfpgCoord; AText: string): TfpgLabel; + implementation + function CreateLabel(AOwner: TComponent; x, y: TfpgCoord; AText: string): TfpgLabel; begin Result := TfpgLabel.Create(AOwner); @@ -74,22 +89,15 @@ begin Result.Width := Result.Font.TextWidth(Result.Text); end; -{ TfpgLabel } -procedure TfpgLabel.SetColor(const AValue: TfpgColor); -begin - if FColor = AValue then - Exit; - FColor := AValue; - RePaint; -end; +{ TfpgCustomLabel } -function TfpgLabel.GetFontDesc: string; +function TfpgCustomLabel.GetFontDesc: string; begin Result := FFont.FontDesc; end; -procedure TfpgLabel.SetAutoSize(const AValue: boolean); +procedure TfpgCustomLabel.SetAutoSize(const AValue: boolean); begin if FAutoSize = AValue then Exit; //==> @@ -101,7 +109,7 @@ begin end; end; -procedure TfpgLabel.SetBackgroundColor(const AValue: TfpgColor); +procedure TfpgCustomLabel.SetBackgroundColor(const AValue: TfpgColor); begin if FBackgroundColor = AValue then Exit; //==> @@ -109,7 +117,7 @@ begin RePaint; end; -procedure TfpgLabel.SetFontDesc(const AValue: string); +procedure TfpgCustomLabel.SetFontDesc(const AValue: string); begin FFont.Free; FFont := fpgGetFont(AValue); @@ -118,7 +126,15 @@ begin RePaint; end; -procedure TfpgLabel.SetText(const AValue: string); +procedure TfpgCustomLabel.SetColor(const AValue: TfpgColor); +begin + if FColor = AValue then + Exit; + FColor := AValue; + RePaint; +end; + +procedure TfpgCustomLabel.SetText(const AValue: string); begin if FText = AValue then Exit; //==> @@ -128,14 +144,14 @@ begin RePaint; end; -procedure TfpgLabel.ResizeLabel; +procedure TfpgCustomLabel.ResizeLabel; begin Width := FFont.TextWidth(FText); Height := FFont.Height; SetPosition(Left, Top, Width, Height); end; -constructor TfpgLabel.Create(AOwner: TComponent); +constructor TfpgCustomLabel.Create(AOwner: TComponent); begin inherited Create(AOwner); FText := 'Label'; @@ -147,14 +163,14 @@ begin FAutoSize := False; end; -destructor TfpgLabel.Destroy; +destructor TfpgCustomLabel.Destroy; begin FText := ''; FFont.Free; inherited Destroy; end; -procedure TfpgLabel.HandlePaint; +procedure TfpgCustomLabel.HandlePaint; begin Canvas.BeginDraw; inherited; |