unit DataEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,
Buttons, StdCtrls, Grids;
type
TDataEditorKind = (ekBtnEdit,ekCheckBox,ekComboBox);
{ TDataEdit }
TDataEdit =
class(TPanel)
private
{ Private declarations }
FButton: TSpeedButton;
FEdit: TEdit;
FCheckBox: TCheckBox;
FComboBox: TComboBox;
FEditorKind : TDataEditorKind;
FOnClick: TNotifyEvent;
FOnDblClick: TNotifyEvent;
FTable: TStringGrid;
function GetText:
String;
procedure SetEditorKind(value: TDataEditorKind);
procedure SetTable(value: TStringGrid);
procedure SetText(value:
String);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent);
destructor Destroy;
procedure Click;
procedure DblClick;
function focused: Boolean;
override;
function IsDesignmode: Boolean;
procedure SetRect(Rec: TRect);
property Edit: TEdit
read FEdit;
property CheckBox: TCheckBox
read FCheckBox;
property ComboBox: TComboBox
read FComboBox;
published
{ Published declarations }
property EditorKind: TDataEditorKind
read FEditorKind
write SetEditorKind;
property OnClick: TNotifyEvent
read FOnClick
write FOnClick;
property OnDblClick: TNotifyEvent
read FOnDblClick
write FOnDblClick;
property Table: TStringGrid
read FTable
write SetTable;
property Text:
String read GetText
write SetText;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('
Samples', [TDataEdit]);
end;
function TDataEdit.GetText:
String;
begin
Result := '
';
case FEditorKind
of
ekBtnEdit:
begin
if Assigned(FEdit)
then Result := FEdit.Text;
end;
ekCheckBox:
begin
if Assigned(FCheckBox)
then Result := FCheckBox.Caption;
end;
ekComboBox:
begin
if Assigned(FComboBox)
then Result := FComboBox.Text;
end;
end;
end;
procedure TDataEdit.SetEditorKind(value: TDataEditorKind);
begin
if FEditorKind <> value
then FEditorKind := value;
case value
of
ekBtnEdit:
begin
if FCheckBox <>
nil then FreeAndNil(FCheckBox);
if FComboBox <>
nil then FreeAndNil(FComboBox);
FEdit := TEdit.Create(self);
FEdit.Parent := self;
FButton := TSpeedButton.Create(self);
FButton.Parent := self;
FButton.Width := Height;
FButton.Height := Height;
FEdit.Width := Width - FButton.Width;
FButton.Left := FEdit.Width + 1;
FEdit.Top := 0;
FButton.Top := 0;
end;
ekCheckBox:
begin
if FButton <>
nil then FreeAndNil(FButton);
if FEdit <>
nil then FreeAndNil(FEdit);
FCheckBox := TCheckBox.Create(self);
FCheckBox.Parent := self;
FCheckBox.Align := alClient;
//FCheckBox.Height := Height;
//FCheckBox.Width := Width;
end;
ekComboBox:
begin
if FButton <>
nil then FreeAndNil(FButton);
if FEdit <>
nil then FreeAndNil(FEdit);
FComboBox := TComboBox.Create(self);
FComboBox.Parent := self;
FComboBox.Align := alClient;
//FComboBox.Height := Height;
//FComboBox.Width := Width;
end;
end;
end;
procedure TDataEdit.SetRect(Rec: TRect);
var r: TRect;
begin
case FEditorKind
of
ekBtnEdit:
with FEdit
do begin
CopyRect(r, Rec);
// rec => r (Eck-Koordinaten an Hilfsrechteck übergeben)
// Umrechnung der Eck-Koordinaten des Hilfsrechtecks:
r.BottomRight := Parent.ScreenToClient(FTable.ClientToScreen(r.BottomRight));
r.TopLeft := Parent.ScreenToClient(FTable.ClientToScreen(r.TopLeft));
// Positionierung der Combobox (Left, Top, Width und Height werden zugewiesen):
SetBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top);
BringToFront
// Combobox in Vordergrund bringen
end;
ekCheckBox:
with FCheckBox
do begin
CopyRect(r, Rec);
// rec => r (Eck-Koordinaten an Hilfsrechteck übergeben)
// Umrechnung der Eck-Koordinaten des Hilfsrechtecks:
r.BottomRight := Parent.ScreenToClient(FTable.ClientToScreen(r.BottomRight));
r.TopLeft := Parent.ScreenToClient(FTable.ClientToScreen(r.TopLeft));
// Positionierung der Combobox (Left, Top, Width und Height werden zugewiesen):
SetBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top);
BringToFront
// Combobox in Vordergrund bringen
end;
ekComboBox:
with FComboBox
do begin
CopyRect(r, Rec);
// rec => r (Eck-Koordinaten an Hilfsrechteck übergeben)
// Umrechnung der Eck-Koordinaten des Hilfsrechtecks:
r.BottomRight := Parent.ScreenToClient(FTable.ClientToScreen(r.BottomRight));
r.TopLeft := Parent.ScreenToClient(FTable.ClientToScreen(r.TopLeft));
// Positionierung der Combobox (Left, Top, Width und Height werden zugewiesen):
SetBounds(r.Left, r.Top, r.Right-r.Left, r.Bottom-r.Top);
BringToFront
// Combobox in Vordergrund bringen
end;
end;
end;
procedure TDataEdit.SetTable(value: TStringGrid);
begin
if FTable =
nil then FTable := value
else
if FTable <> value
then
begin
FreeandNil(FTable);
FTable := value;
Width := FTable.DefaultColWidth;
Height := FTable.DefaultRowHeight;
end;
end;
procedure TDataEdit.SetText(value:
String);
begin
case FEditorKind
of
ekBtnEdit:
begin
FEdit.Text := value;
end;
ekCheckBox:
begin
FCheckBox.Caption := value;
end;
ekComboBox:
begin
FComboBox.Text := value;
end;
end;
end;
constructor TDataEdit.Create(AOwner: TComponent);
var Grid: TStringGrid;
begin
inherited Create(AOwner);
//Grid := TStringGrid.Create(AOwner);
//Grid.DefaultColWidth := 80;
//Grid.DefaultColHeight := 20;
//SetTable(Grid);
//Height := Grid.DefautRowHeight;
//Width := Grid.DefaultColWidth;
end;
destructor TDataEdit.Destroy;
begin
if FButton <>
nil then FreeAndNil(FButton);
if FCheckBox <>
nil then FreeAndNil(FCheckBox);
if FComboBox <>
nil then FreeAndNil(FComboBox);
if FEdit <>
nil then FreeAndNil(FEdit);
inherited Destroy;
end;
procedure TDataEdit.Click;
begin
case FEditorKind
of
ekBtnEdit:
begin
if Assigned(FEdit)
then
begin
//inherited FEdit.Click;
FEdit.OnClick := FOnClick;
end;
if Assigned(FOnClick)
then FOnClick(FEdit);
end;
ekCheckBox:
begin
if Assigned(FCheckBox)
then
begin
//inherited FCheckBox.Click;
FCheckBox.OnClick := FOnClick;
end;
if Assigned(FOnClick)
then FOnClick(FCheckBox);
end;
ekComboBox:
begin
if Assigned(FComboBox)
then
begin
//inherited FComboBox.Click;
FComboBox.OnClick := FOnClick;
end;
if Assigned(FOnClick)
then FOnClick(FComboBox);
end;
end;
end;
procedure TDataEdit.DblClick;
begin
case FEditorKind
of
ekBtnEdit:
begin
if Assigned(FEdit)
then
begin
//inherited FEdit.DblClick;
FEdit.OnDblClick := FOnDblClick;
end;
if Assigned(FOnDblClick)
then FOnDblClick(FEdit);
end;
ekCheckBox:
begin
if Assigned(FCheckBox)
then
begin
//inherited FCheckBox.DblClick;
//FCheckBox.OnClick := FOnDblClick(FCheckBox);
end;
if Assigned(FOnDblClick)
then FOnDblClick(FCheckBox);
end;
ekComboBox:
begin
if Assigned(FComboBox)
then
begin
//inherited FComboBox.DblClick;
FComboBox.OnDblClick := FOnDblClick;
end;
if Assigned(FOnDblClick)
then FOnDblClick(FComboBox);
end;
end;
end;
function TDataEdit.focused: Boolean;
var HasFocus: Boolean;
begin
HasFocus :=
inherited focused;
Result := HasFocus;
if HasFocus
then
case FEditorKind
of
ekBtnEdit:
begin
FEdit.SetFocus;
Result := true;
end;
ekCheckBox:
begin
FCheckBox.SetFocus;
Result := true;
end;
ekComboBox:
begin
FComboBox.SetFocus;
Result := true;
end;
end;
end;
function TDataEdit.IsDesignmode: Boolean;
begin
Result := csDesigning
IN ComponentState;
end;
end.