www.pudn.com > ExtGrid.rar > ExtGrid.pas, change:2016-04-16,size:7122b


unit ExtGrid; 
 
interface 
 
uses 
  Windows, Classes, Grids, xmldom, XMLIntf, XMLDoc; 
 
type 
  TOnGetEditStyle = procedure(ACol, ARow: Integer; var EditStyle: TEditStyle) 
    of Object; 
  TOnGridEditButtonClick = procedure(Sender: TObject; ACol, ARow: Integer) 
    of Object; 
 
  TExtGrid = class(TStringGrid) 
  private 
    FDoc: TXMLDocument; 
    FOnDocChanged: TNotifyEvent; 
    FOnGetPickListitems: TOnGetPickListItems; 
    FOnGetEditStyle: TOnGetEditStyle; 
    FOnEditButtonClick: TOnGridEditButtonClick; 
    procedure SetOnDocChanged(const Value: TNotifyEvent); 
    procedure SetFOnEditButtonClick(const Value: TOnGridEditButtonClick); 
    procedure SetFOnGetPickListitems(const Value: TOnGetPickListItems); 
    procedure SetOnGetEditStyle(const Value: TOnGetEditStyle); 
 
    procedure ButtonClick(Sender: TObject); 
    procedure GetPickListitems(ACol, ARow: Integer; Items: TStrings); 
  protected 
    procedure SetEditText(ACol, ARow: Longint; const Value: string); override; 
    procedure KeyDown(var Key: Word; Shift: TShiftState); override; 
    procedure KeyUp(var Key: Word; Shift: TShiftState); override; 
 
    function CreateEditor: TInplaceEdit; override; 
    function GetEditStyle(ACol, ARow: Longint): TEditStyle; override; 
  public 
    property Doc: TXMLDocument read FDoc; 
    procedure DeleteRow(ARow: Longint); override; 
    procedure InsertColumn(ACol: Integer); 
    procedure InsertRow(ARow: Integer); 
    procedure AddRow; 
    procedure Fill; 
 
    constructor Create(AOwner: TComponent); override; 
    destructor Destroy; override; 
  published 
    property OnDocChanged: TNotifyEvent read FOnDocChanged 
      write SetOnDocChanged; 
    property OnEditButtonClick: TOnGridEditButtonClick read FOnEditButtonClick 
      write SetFOnEditButtonClick; 
    property OnGetPickListitems: TOnGetPickListItems read FOnGetPickListitems 
      write SetFOnGetPickListitems; 
    property OnGetEditStyle: TOnGetEditStyle read FOnGetEditStyle 
      write SetOnGetEditStyle; 
  end; 
 
procedure Register; 
 
implementation 
 
procedure Register; 
begin 
  RegisterComponents('Samples', [TExtGrid]); 
end; 
 
procedure TExtGrid.AddRow; 
begin 
  RowCount := RowCount + 1; 
  Row := RowCount - 1; 
end; 
 
procedure TExtGrid.ButtonClick(Sender: TObject); 
begin 
  if Assigned(FOnEditButtonClick) then 
    FOnEditButtonClick(Self, Col, Row); 
end; 
 
constructor TExtGrid.Create(AOwner: TComponent); 
begin 
  inherited; 
  Options := Options + [goEditing, goAlwaysShowEditor]; 
  FixedCols := 0; 
  DefaultRowHeight := 18; 
  FDoc := TXMLDocument.Create(Self); 
end; 
 
function TExtGrid.CreateEditor: TInplaceEdit; 
begin 
  inherited CreateEditor; 
  Result := TInplaceEditList.Create(Self); 
  Result.Parent := Self; 
  with TInplaceEditList(Result) do 
  begin 
    Font.Assign(Self.Font); 
    OnEditButtonClick := ButtonClick; 
    OnGetPickListitems := GetPickListitems; 
  end; 
end; 
 
procedure TExtGrid.DeleteRow(ARow: Integer); 
var 
  I: Integer; 
begin 
  for I := 0 to ColCount - 1 do 
  begin 
    Cells[I, ARow] := ''; 
  end; 
  inherited; 
end; 
 
destructor TExtGrid.Destroy; 
begin 
  if InplaceEditor <> nil then 
    if TInplaceEditList(InplaceEditor).PickListLoaded then 
      TInplaceEditList(InplaceEditor).PickList.Items.Clear; 
  FDoc.Free; 
  inherited; 
end; 
 
procedure TExtGrid.Fill; 
var 
  Rec: IXMLNode; 
  RecList: IXMLNodeList; 
  I, J: Integer; 
  FieldName: String; 
begin 
  Row := 1; 
  while RowCount > 2 do 
    DeleteRow(1); 
  ColCount := FDoc.DocumentElement.AttributeNodes.Count; 
  for I := 0 to FDoc.DocumentElement.AttributeNodes.Count - 1 do 
  begin 
    Cells[I, 0] := FDoc.DocumentElement.AttributeNodes[I].NodeName; 
  end; 
  RecList := FDoc.DocumentElement.ChildNodes; 
  if RecList.Count = 0 then 
    RowCount := 2 
  else 
    RowCount := 1 + RecList.Count; 
  for I := 0 to RecList.Count - 1 do 
  begin 
    Rec := RecList[I]; 
    for J := 0 to ColCount - 1 do 
    begin 
      FieldName := Cells[J, 0]; 
      if Rec.HasAttribute(FieldName) then 
        Cells[J, I + 1] := Rec.Attributes[FieldName]; 
    end; 
  end; 
end; 
 
function TExtGrid.GetEditStyle(ACol, ARow: Integer): TEditStyle; 
begin 
  Result := inherited GetEditStyle(ACol, ARow); 
  if Assigned(FOnGetEditStyle) then 
    FOnGetEditStyle(ACol, ARow, Result); 
end; 
 
procedure TExtGrid.GetPickListitems(ACol, ARow: Integer; Items: TStrings); 
begin 
  if Assigned(FOnGetPickListitems) then 
    FOnGetPickListitems(ACol, ARow, Items); 
end; 
 
procedure TExtGrid.InsertColumn(ACol: Integer); 
begin 
  ColCount := ColCount + 1; 
  MoveColumn(ColCount - 1, ACol); 
end; 
 
procedure TExtGrid.InsertRow(ARow: Integer); 
begin 
  RowCount := RowCount + 1; 
  MoveRow(RowCount - 1, ARow); 
end; 
 
procedure TExtGrid.KeyDown(var Key: Word; Shift: TShiftState); 
begin 
  if (Key = vk_down) and (Row = RowCount - 1) and (Shift = []) then 
  begin 
    if RowCount - 1 = FDoc.DocumentElement.ChildNodes.Count then 
      AddRow; 
  end 
  else 
    inherited; 
end; 
 
procedure TExtGrid.KeyUp(var Key: Word; Shift: TShiftState); 
var 
  R: Integer; 
begin 
  inherited; 
  if (Key = vk_delete) and (Shift = [ssCtrl]) then 
  begin 
    if FDoc.DocumentElement.ChildNodes.Count > 0 then 
    begin 
      R := Row; 
      if RowCount <= 2 then 
      begin 
        AddRow; 
      end; 
      HideEditor; 
      DeleteRow(R); 
      if FDoc.DocumentElement.ChildNodes.Count >= R then 
        FDoc.DocumentElement.ChildNodes.Delete(R - 1); 
      FDoc.SaveToFile('data.xml'); 
      if R <= Row then 
        Row := R 
      else 
        Row := R - 1; 
      ShowEditor; 
    end; 
  end; 
end; 
 
procedure TExtGrid.SetEditText(ACol, ARow: Integer; const Value: string); 
var 
  FieldName: String; 
  RecList: IXMLNodeList; 
  Rec: IXMLNode; 
begin 
  if Value <> Cells[ACol, ARow] then 
  begin 
    FieldName := Cells[ACol, 0]; 
    RecList := FDoc.DocumentElement.ChildNodes; 
    if (ARow = RowCount - 1) and (RecList.Count = RowCount - 2) then 
    begin 
      if Value = '' then 
        Exit; 
      //  
      Rec := FDoc.DocumentElement.AddChild('rec') 
    end 
    else 
    begin 
      //  
      Rec := RecList[ARow - 1]; 
      if Rec.HasAttribute(FieldName) then 
      begin 
        if Rec.Attributes[FieldName] = Value then 
          Exit; 
      end 
      else 
      begin 
        if Value = '' then 
          Exit; 
      end; 
    end; 
    if Rec <> nil then 
    begin 
      Rec.SetAttribute(FieldName, Value); 
      if Assigned(FOnDocChanged) then 
        FOnDocChanged(Self); 
    end; 
  end; 
  inherited; 
end; 
 
procedure TExtGrid.SetFOnEditButtonClick(const Value: TOnGridEditButtonClick); 
begin 
  FOnEditButtonClick := Value; 
end; 
 
procedure TExtGrid.SetFOnGetPickListitems(const Value: TOnGetPickListItems); 
begin 
  FOnGetPickListitems := Value; 
end; 
 
procedure TExtGrid.SetOnDocChanged(const Value: TNotifyEvent); 
begin 
  FOnDocChanged := Value; 
end; 
 
procedure TExtGrid.SetOnGetEditStyle(const Value: TOnGetEditStyle); 
begin 
  FOnGetEditStyle := Value; 
end; 
 
end.