unit uagenda;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBCtrls, Grids, DBGrids, ExtCtrls, Mask, ActnList,
  Buttons, DB, DBTables;

type
  Tfagenda = class(TForm)
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    Panel2: TPanel;
    DBMemo1: TDBMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    MaskEdit1: TMaskEdit;
    MaskEdit2: TMaskEdit;
    Bevel1: TBevel;
    Bevel2: TBevel;
    MaskEdit3: TMaskEdit;
    MaskEdit4: TMaskEdit;
    Label4: TLabel;
    Edit1: TEdit;
    Bevel3: TBevel;
    ActionList1: TActionList;
    focus: TAction;
    data: TAction;
    hora: TAction;
    SpeedButton1: TSpeedButton;
    formatar: TAction;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure DBGrid1Enter(Sender: TObject);
    procedure DBGrid1Exit(Sender: TObject);
    procedure focusExecute(Sender: TObject);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBMemo1Exit(Sender: TObject);
    procedure DBMemo1Enter(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure dataExecute(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MaskEdit2Exit(Sender: TObject);
    procedure horaExecute(Sender: TObject);
    procedure MaskEdit4Exit(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure formatarExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fagenda: Tfagenda;

implementation

uses udm;

{$R *.dfm}

procedure Tfagenda.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#13 then begin
  key := #0;
       Perform(WM_nextdlgctl,0,0);
 end Else if Key = #27 then
   Perform(WM_nextdlgctl,1,0);
end;

procedure Tfagenda.DBGrid1Enter(Sender: TObject);
begin
        keypreview := false
end;

procedure Tfagenda.DBGrid1Exit(Sender: TObject);
begin
        keypreview := true;
end;

procedure Tfagenda.focusExecute(Sender: TObject);
begin
        if dbgrid1.Focused = true then
         dbmemo1.SetFocus
        else if dbmemo1.Focused = true then
         maskedit1.SetFocus
        else
        dbgrid1.SetFocus;  
end;

procedure Tfagenda.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = 13 then
         dbgrid1.SelectedIndex := dbgrid1.SelectedIndex + 1;

         if key = 27 then
          dbgrid1.SelectedIndex := dbgrid1.SelectedIndex - 1;

end;

procedure Tfagenda.DBMemo1Exit(Sender: TObject);
begin
        keypreview := true;
end;

procedure Tfagenda.DBMemo1Enter(Sender: TObject);
begin
        keypreview := false;
end;

procedure Tfagenda.Edit1Change(Sender: TObject);
begin
        fdm.tbagenda.findnearest([edit1.text]);
end;

procedure Tfagenda.dataExecute(Sender: TObject);
begin
       if (maskedit1.text <>'  /  /    ') and (maskedit2.text <>'  /  /    ') then begin
        fdm.tbagenda.filter := 'data >=''' + maskedit1.text + ''' and data<=''' +  maskedit2.text + '''';
         maskedit3.Clear;
          maskedit4.Clear;
           end;

end;

procedure Tfagenda.FormClose(Sender: TObject; var Action: TCloseAction);
begin
       fdm.tbagenda.Filtered := false;
        fdm.tbagenda.edit;
         fdm.tbagenda.post;
end;

procedure Tfagenda.MaskEdit2Exit(Sender: TObject);
begin
        dataexecute(self);
end;

procedure Tfagenda.horaExecute(Sender: TObject);
begin
      if (maskedit3.text <>'  :  ') and (maskedit4.text <>'  :  ') then begin

       if (maskedit1.text <>'  /  /    ') and (maskedit2.text <>'  /  /    ') then
        fdm.tbagenda.filter := 'data >=''' + maskedit1.text + ''' and data<=''' +  maskedit2.text + ''' and hora >=''' +
         maskedit3.text + ''' and hora<=''' +  maskedit4.text + ''''

         else

        fdm.tbagenda.filter := 'hora >=''' + maskedit3.text + ''' and hora<=''' +  maskedit4.text + '''';
       fdm.tbagenda.filtered := true;

           end;
end;

procedure Tfagenda.MaskEdit4Exit(Sender: TObject);
begin
        horaexecute(self);
end;

procedure Tfagenda.SpeedButton1Click(Sender: TObject);
begin
       fdm.tbagenda.Filtered := false;
        maskedit1.Clear;
         maskedit2.Clear;
          maskedit3.Clear;
           maskedit4.Clear;
end;

procedure Tfagenda.formatarExecute(Sender: TObject);
begin
         (fdm.tbagenda.fieldbyname ('data') as tdatetimefield).editmask := '99/99/9999';
         (fdm.tbagenda.fieldbyname ('hora') as tdatetimefield).editmask := '99:99';
         (fdm.tbagenda.fieldbyname ('fone') as tstringfield).editmask := '(99)9999-9999';         
end;

end.
