unit dbtest;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, DBGrids, ExtCtrls, DBCtrls,  StdCtrls, Mask, ComCtrls, Grids,
  sncCurrency, sncDBCurrency, ToolWin;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    DBGrid1: TDBGrid;
    sncDBCurrencyInplaceEdit: TsncDBCurrencyEdit;
    Table1OrderNo: TFloatField;
    Table1CustNo: TFloatField;
    Table1ItemsTotal: TCurrencyField;
    Table1Freight: TCurrencyField;
    Table1AmountPaid: TCurrencyField;
    ToolBar1: TToolBar;
    DBNavigator1: TDBNavigator;
    procedure SetHighLightColor;
    procedure SetNormalColor;
    procedure DBGrid1ColEnter(Sender: TObject);
    procedure DBGrid1ColExit(Sender: TObject);
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
    procedure sncDBCurrencyInplaceEditEnter(Sender: TObject);
    procedure sncDBCurrencyInplaceEditExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure sncDBCurrencyInplaceEditMoveOut(Sender: TObject;
      var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  CurrencyFieldSet: array[0..2] of string=('ItemsTotal', 'Freight', 'AmountPaid');

var
  Form1: TForm1;

implementation

{$R *.DFM}

function CheckCurrencyField(FieldName: String): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I:=0 to Length(CurrencyFieldSet)-1 do
  begin
    if (FieldName=CurrencyFieldSet[I]) then
    begin
      Result := True;
      Break;
    end;
  end;
end;

procedure TForm1.SetHighLightColor;
begin
  sncDBCurrencyInplaceEdit.Color := clHighLight;
  sncDBCurrencyInplaceEdit.Font.Color := clWhite;
end;

procedure TForm1.SetNormalColor;
begin
  sncDBCurrencyInplaceEdit.Color := DBGrid1.Color;
  sncDBCurrencyInplaceEdit.Font.Color := DBGrid1.Font.Color;
end;

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
  if CheckCurrencyField(DBGrid1.SelectedField.FieldName) then
  begin
    sncDBCurrencyInplaceEdit.DataField := DBGrid1.SelectedField.FieldName;
    sncDBCurrencyInplaceEdit.Tag := 1;
  end;
end;

procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
  if CheckCurrencyField(DBGrid1.SelectedField.FieldName) then
  begin
    sncDBCurrencyInplaceEdit.Visible := false;
    sncDBCurrencyInplaceEdit.Tag := 0;
  end;
end;

procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  if (key<>chr(9)) and CheckCurrencyField(DBGrid1.SelectedField.FieldName) then
    begin
      sncDBCurrencyInplaceEdit.SetFocus;
      SendMessage(sncDBCurrencyInplaceEdit.Handle, WM_Char, WORD(Key), 0);
    end;
end;

procedure TForm1.sncDBCurrencyInplaceEditEnter(Sender: TObject);
begin
  SetNormalColor;
end;

procedure TForm1.sncDBCurrencyInplaceEditExit(Sender: TObject);
begin
  SetHighLightColor;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  sncDBCurrencyInplaceEdit.Color := DBGrid1.Color;
  sncDBCurrencyInplaceEdit.Font.Assign(DBGrid1.Font);
  sncDBCurrencyInplaceEdit.Tag := 0;
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  OldColor, OldFontColor: TColor;
begin
  if CheckCurrencyField(Column.FieldName) then
  begin
    if (gdFocused in State) then
    begin
      sncDBCurrencyInplaceEdit.Left := Rect.Left + DBGrid1.Left+2;
      sncDBCurrencyInplaceEdit.Top := Rect.Top + DBGrid1.top+2;
      sncDBCurrencyInplaceEdit.Width := Rect.Right-Rect.Left;
      sncDBCurrencyInplaceEdit.Height := Rect.Bottom- Rect.Top;
      SetHighLightColor;
      if (sncDBCurrencyInplaceEdit.Tag=1) and not sncDBCurrencyInplaceEdit.Visible then
        sncDBCurrencyInplaceEdit.Visible := True;
    end
    else
      begin
        OldColor := sncDBCurrencyInplaceEdit.Color;
        OldFontColor := sncDBCurrencyInplaceEdit.Font.Color;
        SetNormalColor;
        DrawCurrencyFrame(DBGrid1.Canvas, Rect, sncDBCurrencyInplaceEdit, Column.Field.AsFloat);
        sncDBCurrencyInplaceEdit.Color := OldColor;
        sncDBCurrencyInplaceEdit.Font.Color := OldFontColor;
      end;
  end;
end;


procedure TForm1.sncDBCurrencyInplaceEditMoveOut(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  case Key of
    VK_UP,VK_DOWN,VK_LEFT,VK_RIGHT:
      begin
        DBGrid1.SetFocus;
        SendMessage(DBGrid1.Handle, WM_KEYDOWN, WORD(Key), 0);
      end;
  end;
end;

end.
