unit udevise;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DB, Grids, DBGrids, ExtCtrls, Menus, Mask,
  ToolEdit, dxCore, dxButton;

type
  TFrmdevise = class(TForm)
    Panel1: TPanel;
    Dsirepertoire: TDataSource;
    BtnOk: TBitBtn;
    PopupMenuGrid: TPopupMenu;
    Ajouter1: TMenuItem;
    Modifier1: TMenuItem;
    Supprimer1: TMenuItem;
    Panel7: TPanel;
    LabChpRech: TLabel;
    eValChampRech: TEdit;
    DBGrid1: TDBGrid;
    dxButton1: TdxButton;
    dxButton4: TdxButton;
    dxButton5: TdxButton;
    dxButton2: TdxButton;
    procedure FiltrerAffichage();
    procedure eCodeChange(Sender: TObject);
     
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnOkClick(Sender: TObject);

    procedure BtnSupprimerClick(Sender: TObject);
    procedure eCodeKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure Ajouter1Click(Sender: TObject);
    procedure Modifier1Click(Sender: TObject);
    procedure eValChampRechChange(Sender: TObject);
    
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DBGrid1Enter(Sender: TObject);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormCreate(Sender: TObject);
    procedure DBGridNewWindowProc(var Msg: TMessage);
    procedure dxButton2Click(Sender: TObject);
  private
   OldWindowProc: TWndMethod;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Frmdevise: TFrmdevise;
  aNomChampRech :String;
implementation

{$R *.dfm}
uses udataprix;

procedure TFrmdevise.FiltrerAffichage();
var asFiltre :String;
begin
if LabChpRech.Caption= 'Devise' then
 begin
asFiltre := '';
if(eValChampRech.Text <> '') then
   asFiltre := 'upper(Devise) LIKE ' + QuotedStr('%'+uppercase(eValChampRech.Text) +'%');

dataprix.devise.Close;
dataprix.devise.SQL.Clear;
if asfiltre <>'' then
dataprix.devise.SQL.Add('select * from Devise where '+asFiltre
                                  + ' order by devise')
else
dataprix.devise.SQL.Add('select * from Devise '
                               + ' order by devise');
dataprix.devise.open;
end;

if LabChpRech.Caption= 'Libelle' then
 begin
asFiltre := '';
if(eValChampRech.Text <> '') then
   asFiltre := 'upper(Libelle) LIKE ' + QuotedStr('%'+uppercase(eValChampRech.Text) +'%');

dataprix.devise.Close;
dataprix.devise.SQL.Clear;
if asfiltre <>'' then
dataprix.devise.SQL.Add('select * from Devise where '+asFiltre
                                  + ' order by libelle')
else
dataprix.devise.SQL.Add('select * from Devise '
                               + ' order by Libelle');
dataprix.devise.open;
end;

end;

procedure TFrmdevise.eCodeChange(Sender: TObject);
begin
FiltrerAffichage();
if(dataprix.devise.RecordCount = 0)then
  ActiveControl := eValChampRech;
end;


procedure TFrmdevise.FormClose(Sender: TObject;
  var Action: TCloseAction);
  begin
Action := caFree;
end;

procedure TFrmdevise.BtnOkClick(Sender: TObject);
begin
ModalResult := mrOk;
end;

procedure TFrmdevise.BtnSupprimerClick(Sender: TObject);
begin
if(Application.MessageBox('Voulez-vous supprimer la ligne en cours ?', 'Confirmation',MB_YESNO)= IDYES) then
   DsIrepertoire.DataSet.Delete;
end;

procedure TFrmdevise.eCodeKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if(Key = VK_DOWN) then
    DBGrid1.SetFocus;
end;

procedure TFrmdevise.FormShow(Sender: TObject);
begin
dataprix.devise.Close;
dataprix.devise.OPEN;
 
end;

procedure TFrmdevise.Ajouter1Click(Sender: TObject);
begin
 dataprix.devise.Append;
 DBGrid1.ReadOnly:=False;
 DBGrid1.SetFocus;
end;

procedure TFrmdevise.Modifier1Click(Sender: TObject);
begin
 dataprix.devise.Edit;
 DBGrid1.ReadOnly:=False;
end;

procedure TFrmdevise.eValChampRechChange(Sender: TObject);
begin
FiltrerAffichage();
ActiveControl := eValChampRech;
end;

procedure TFrmdevise.DBGrid1DblClick(Sender: TObject);
begin
// dataprix.devise.Edit;
end;

procedure TFrmdevise.DBGrid1Enter(Sender: TObject);
begin

if  dataprix.devise.State in [dsedit,dsinsert] then
    begin
    dataprix.devise.post;
    dataprix.devise.Refresh;
    end;
dataprix.devise.Edit;
DBGrid1.SetFocus;
end;

procedure TFrmdevise.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if(Key in [VK_F10	]) then
    ModalResult := mrOk;  //F10
if (key in [vk_up]) and (dataprix.deviseDEVISE.Value='') then
dataprix.devise.Cancel;

if(Key in [VK_RETURN]) then
case DBGrid1.SelectedIndex of
     0:DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex+1;

     1:begin

             if dataprix.devise.State in [dsbrowse] then
                dataprix.devise.Edit;
             if dataprix.devise.State in [dsinsert,dsedit] then
                begin
                 dataprix.devise.Post;
                 DBGrid1.ReadOnly:=True;

                end;
              DBGrid1.SelectedIndex:=0;
            end;

end;//end case
end;

procedure TFrmdevise.DBGrid1TitleClick(Column: TColumn);
begin
DBGrid1.Columns[0].Color := $00FFFFF4 ;
DBGrid1.Columns[1].Color := $00FFFFF4 ;

   dataprix.devise.Close;
   dataprix.devise.SQL.Clear;
   dataprix.devise.SQL.Add('select * from devise'
                                     + ' Order by ' +  Column.FieldName);
   aNomChampRech := Column.FieldName;
   LabChpRech.Caption := Column.Title.Caption;
   dataprix.devise.Open;
   Column.Color :=  $00B3D9FF;
end;

procedure TFrmdevise.DBGrid1CellClick(Column: TColumn);
begin
if dataprix.devise.State in [dsbrowse] then
   dataprix.devise.Edit;
end;

procedure TFrmdevise.FormCreate(Sender: TObject);
begin
 OldWindowProc := Dbgrid1.WindowProc;
 Dbgrid1.WindowProc := DBGridNewWindowProc;
end;

procedure TFrmdevise.DBGridNewWindowProc(var Msg: TMessage);
begin
  //.Interception de l'évènement WM_MOUSEWHEEL. 
  if Msg.Msg = WM_MOUSEWHEEL then 
  begin 
    if (Dbgrid1.DataSource.DataSet.Active) then
    begin 
      if SmallInt(Msg.WParamHi) < 0 then
        Dbgrid1.DataSource.DataSet.Next
      else
        Dbgrid1.DataSource.DataSet.Prior;
    end; 
    Exit; 
  end; 
  
  //.Traitement normal des autres message. 
  OldWindowProc(Msg); 
end;

procedure TFrmdevise.dxButton2Click(Sender: TObject);
begin
  close;
end;

end.
