{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit ProxyDatabaseCollectionObjectFind; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, DB, DBTables, Base, DatabaseObjects, ProxyDatabaseObjectCollectionUnit; type TProxyDatabaseCollectionObjectFindForm = class(TBaseForm) StringGrid: TStringGrid; procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure StringGridDblClick(Sender: TObject); procedure StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); private { Private declarations } FDatabaseObjectClass : TDatabaseObjectClass; FProxyDatabaseObjectCollection : TProxyDatabaseObjectCollection; FDatabaseObject : TDatabaseObject; SavedWidth : integer; SavedHeight : integer; public { Public declarations } function Find (DatabaseObjectClass : TDatabaseObjectClass; ProxyDatabaseObjectCollection : TProxyDatabaseObjectCollection) : TDatabaseObject; end; var ProxyDatabaseCollectionObjectFindForm: TProxyDatabaseCollectionObjectFindForm; implementation uses DatabaseManager, Globals; {$R *.dfm} function TProxyDatabaseCollectionObjectFindForm.Find (DatabaseObjectClass : TDatabaseObjectClass; ProxyDatabaseObjectCollection : TProxyDatabaseObjectCollection) : TDatabaseObject; var i : integer; begin // record width and height on first call and restore on subsequent if (SavedWidth = 0) and (SavedHeight = 0) then begin SavedWidth := Width; SavedHeight := Height; end else begin Width := SavedWidth; Height := SavedHeight; end; FDatabaseObject := nil; FDatabaseObjectClass := DatabaseObjectClass; FProxyDatabaseObjectCollection := ProxyDatabaseObjectCollection; Caption := FDatabaseObjectClass.FindFormCaption; FDatabaseObjectClass.SetupFindStringGrid(StringGrid); if FProxyDatabaseObjectCollection.Count = 0 then begin StringGrid.RowCount := 2; // clear details on last row for i := 0 to StringGrid.ColCount - 1 do StringGrid.Cells[i,1] := ''; end else StringGrid.RowCount := FProxyDatabaseObjectCollection.Count + 1; StringGrid.Row := 1; ShowModal; Result := FDatabaseObject; end; procedure TProxyDatabaseCollectionObjectFindForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); var DatabaseObject : TDatabaseObject; begin if Key = VK_ESCAPE then begin Close; Key := 0; end else if Key = VK_RETURN then begin DatabaseObject := FProxyDatabaseObjectCollection[StringGrid.Row - 1]; if DatabaseObject <> nil then begin // create copy of object to return to caller FDatabaseObject := FDatabaseObjectClass.Create; FDatabaseObject.Assign(DatabaseObject); ModalResult := mrOk; Key := 0; end; end; end; procedure TProxyDatabaseCollectionObjectFindForm.StringGridDblClick( Sender: TObject); var DatabaseObject : TDatabaseObject; begin DatabaseObject := FProxyDatabaseObjectCollection[StringGrid.Row - 1]; if DatabaseObject <> nil then begin // create copy of object to return to caller FDatabaseObject := FDatabaseObjectClass.Create; FDatabaseObject.Assign(DatabaseObject); ModalResult := mrOk; end; end; procedure TProxyDatabaseCollectionObjectFindForm.StringGridDrawCell( Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var DatabaseObject : TDatabaseObject; Text : string; begin DatabaseObject := TDatabaseObject(FProxyDatabaseObjectCollection[ARow - 1]); if DatabaseObject <> nil then begin // set the brush color if gdSelected in State then StringGrid.Canvas.Brush.Color := clHighlight else StringGrid.Canvas.Brush.Color := StringGrid.Color; // set the font color if gdSelected in State then StringGrid.Canvas.Font.Color := clWhite else StringGrid.Canvas.Font.Color := StringGrid.Font.Color; // format the text Text := DatabaseObject.FindStringGridText(ACol); // output the text StringGrid.Canvas.TextRect(Rect,Rect.Left+2,Rect.Top+2,Text); end; end; end.