unit fformularios;

interface

uses
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Client, uFireDacHelper,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Vcl.StdCtrls, Vcl.DBCtrls, Vcl.Mask,
  Vcl.ComCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls,datos,
    Data.DB,  Excel2000,
  ComObj;

type
  Tformularios = class(TForm)
    qformularios: TFDQuery;
    DBGrid1: TDBGrid;
    dsqformularios: TDataSource;
    qformulariosid: TIntegerField;
    qformulariosformulario: TWideStringField;
    qformulariosdescripcion: TWideStringField;
    qformulariosaplicar_a: TWideStringField;
    qformularioside: TIntegerField;
    Panel7: TPanel;
    btAcancelar: TButton;
    btAmodificar: TButton;
    btAborrar: TButton;
    btAnuevo: TButton;
    btAguardar: TButton;
    qaux: TFDQuery;
    gcrpagado: TDBCheckBox;
    qformulariospordefecto: TIntegerField;
    procedure btAnuevoClick(Sender: TObject);
    procedure btAborrarClick(Sender: TObject);
    procedure btAmodificarClick(Sender: TObject);
    procedure btAguardarClick(Sender: TObject);
    procedure btAcancelarClick(Sender: TObject);
    procedure dsqformulariosStateChange(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure qformulariosBeforePost(DataSet: TDataSet);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AppMessage(var Msg: Tmsg; var Handled:Boolean);
  end;

var
  formularios: Tformularios;

implementation

{$R *.dfm}

procedure Tformularios.AppMessage(var Msg: TMSg; Var Handled:Boolean);
 var
  actual : TWincontrol;
 begin
 if Msg.message = WM_KEYDOWN then
    begin

    // esto es para controlar que con la flecha abajo se desplegen las
    // listas

    if Msg.wParam = VK_DOWN then
      begin
      Actual := Screen.ActiveControl;
      if Actual is TDBLookupComboBox then
            if not TDBLookupComboBox(Screen.ActiveForm.ActiveControl).listVisible then
               TDBLookupComboBox(Screen.ActiveForm.ActiveControl).DropDown;

      // si utilizas las RX
   {   if Actual is TRxDBLookupCombo then
            if not TRxDBLookupCombo(Screen.ActiveForm.ActiveControl).listVisible then
               TRxDBLookupCombo(Screen.ActiveForm.ActiveControl).DropDown;

    }
      if Actual is TComboBox then
         SendMessage(TComboBox(Screen.ActiveForm.ActiveControl).Handle,CB_SHOWDROPDOWN,-1,0);

      if (Actual is TCustomEdit) then
          Msg.wParam := VK_TAB;
      // se pueden aadir mas controles....

      end;

    // y aqui es el control del intro.
    if Msg.wParam = VK_RETURN THEN
       begin
       Actual := Screen.ActiveControl;
       // para los edit.
       if (Actual is TCustomEdit) and
          not (Actual is TCustomMemo) then
          Msg.wParam := VK_TAB;

       // para los memo. Con ctrl + enter se salta de linea dentro del
       // memo, si solo es intro salta al siguiente control
       if (Actual is TCustomMemo) and
          (HiWord(GetKeyState(VK_CONTROL)) = 0)then
          Msg.wParam := VK_TAB;

       // para los lookup
       if Actual is TDBLookupComboBox then
          if not TDBLookupComboBox(Actual).listVisible then
             Msg.wParam := VK_TAB;

       // Si utilizas las RX
{       if Actual is TRxDBLookupCombo then
          if not TRxDBLookupCombo(Actual).listVisible then
             Msg.wParam := VK_TAB;
 }
       // Los combobox
       if Actual is TCustomComboBox then
          if not TCustomComboBox(Actual).DroppedDown then
             Msg.wParam := VK_TAB;

       // el radiobutton
       if Actual is TRadioButton then
          Msg.wParam := VK_TAB;

       // en el grid
       if Actual is TDBGrid then
          if (TDBGrid(Actual).ReadOnly) or
             (HiWord(GetKeyState(VK_CONTROL)) = 1)then
             Msg.wParam := VK_TAB;

       // o esto otro Con Ctrl + intro siguiente celda con
       // ctrl + shift + intro celda anterior
       // es complicado pero es por no liar con el intro solo
       // que siempre pasa de un control a otro.
       if Actual is TDBGrid then
          begin
          if (TDBGrid(Actual).ReadOnly) or
             (HiWord(GetKeyState(VK_CONTROL)) = 0) then
             Msg.wParam := VK_TAB
          else
             if not(HiWord(GetKeyState(VK_SHIFT)) = 0) then
                begin
                if TDBGrid(Actual).selectedindex > 0 then             { increment the field }
                   TDBGrid(Actual).selectedindex := TDBGrid(Actual).selectedindex -1
                else
                   TDBGrid(Actual).selectedindex := TDBGrid(Actual).fieldcount -1;
                end
             else
                begin
                if TDBGrid(Actual).selectedindex < (TDBGrid(Actual).fieldcount -1) then             { increment the field }
                   TDBGrid(Actual).selectedindex := TDBGrid(Actual).selectedindex +1
                else
                   TDBGrid(Actual).selectedindex := 0;
                end;
          end;


       // ListBox
       if Actual is TCustomListBox then
          Msg.wParam := VK_TAB;

       // TabControl
//       if Actual is TCustomTabControl then
//          Msg.wParam := VK_TAB;

       // Aqui se pondran todos los controles que usamos en la
       // aplicacin y necesitamos cambiar el tab por intro.
       // de esta forma te despreocupas de cuantos aades y es ms
       // limpio el cdigo

       end;
    end;
 end;


procedure Tformularios.btAborrarClick(Sender: TObject);
begin
    qformularios.Delete;
end;

procedure Tformularios.btAcancelarClick(Sender: TObject);
begin
    qformularios.cancel;
end;

procedure Tformularios.btAguardarClick(Sender: TObject);
begin
    qformularios.post;
end;

procedure Tformularios.btAmodificarClick(Sender: TObject);
begin
    qformularios.edit;
end;

procedure Tformularios.btAnuevoClick(Sender: TObject);
begin
   qaux.sql.clear;
   qaux.SQL.Add('select max(id)+1 from formatosimpresion');
   qaux.Open;
   qformularios.append;
   dbgrid1.setfocus;
   qformulariosid.Value:=qaux.Fields[0].AsInteger;
end;

procedure Tformularios.DBGrid1CellClick(Column: TColumn);
begin
    if column.Field.FieldName ='pordefecto' then
    begin
        if qformulariospordefecto.asinteger=1 then
         begin
           qformularios.Edit;
           qformulariospordefecto.Value:=0;
           qformularios.Post;
           dbgrid1.selectedIndex:=1;
         end else
               begin
                 qformularios.Edit;
                 qformulariospordefecto.Value:=1;
                 qformularios.Post;
                 qformularios.Refresh;
                 dbgrid1.SelectedIndex:=1;
               end;
    end;
end;

procedure Tformularios.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
 (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
  DrawState: Integer;
  DrawRect: TRect;
begin
if (gdFocused in State) then
begin

    if (Column.Field.FieldName = 'pordefecto') then
     begin
       gcrpagado.Left := Rect.Left + DBgrid1.Left + 2;
       gcrpagado.Top := Rect.Top + DBgrid1.top + 2;
       gcrpagado.Width := Rect.Right - Rect.Left;
       gcrpagado.Height := Rect.Bottom - Rect.Top;
       gcrpagado.Visible := True;
     end;


end else
     begin

       if (Column.Field.FieldName = 'pordefecto') then
       begin
         DrawRect:=Rect;
         InflateRect(DrawRect,-1,-1);
         if column.Field.Asinteger=0  then DrawState := ischecked[false];
         if column.Field.Asinteger=1  then DrawState := ischecked[true];
      //   DrawState := ISChecked[Column.Field.Asboolean];
         DBgrid1.Canvas.FillRect(Rect);
         DrawFrameControl(DBgrid1.Canvas.Handle, DrawRect,
         DFC_BUTTON, DrawState);
       end;


    end;


end;

procedure Tformularios.dsqformulariosStateChange(Sender: TObject);
begin
    if qformularios.State in [dsedit,dsinsert] then
    begin
        btanuevo.Enabled:=false;
        btamodificar.Enabled:=false;
        btaborrar.Enabled:=false;
        btacancelar.Enabled:=true;
        btaguardar.Enabled:=true;
    end else
    begin
        btanuevo.Enabled:=true;
        btamodificar.Enabled:=true;
        btaborrar.Enabled:=true;
        btacancelar.Enabled:=false;
        btaguardar.Enabled:=false;
    end;
end;

procedure Tformularios.FormCreate(Sender: TObject);
var
  I: Integer;
begin
    Application.OnMessage :=  AppMessage;
   gcrpagado.DataField := 'pagado';
   gcrpagado.Visible := False;
   gcrpagado.SendToBack;
   gcrpagado.Color := DBGrid1.Color;
   gcrpagado.Caption := '';
   gcrpagado.ValueChecked := '0';
   gcrpagado.ValueUnChecked := '1';


end;

procedure Tformularios.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if (activecontrol is tdbgrid) then
    begin
        if Key = 13 then Key :=9;

    end;
end;

procedure Tformularios.FormKeyPress(Sender: TObject; var Key: Char);
begin
   key:=upcase(key);
    if (key='.') and (activecontrol is tdbedit) then
    begin
       if  (activecontrol as tdbedit).Field.DataType in [ftfloat,ftCurrency,ftbcd,ftfmtbcd] then key:=','
    end;
    if (key='.') and (activecontrol is tdbgrid) then
    begin
        key:=','
    end;
    if (activecontrol is tdbgrid) then
    begin
        if Key = #13 then Key :=#9;
    end;

    if (key=#13) then
    begin
        Perform(WM_NextDLGCtl,0,0);
        key:=#0;
    end;
end;

procedure Tformularios.FormShow(Sender: TObject);
begin
    dm.tempresa.Open;
    qformularios.Open;
end;

procedure Tformularios.qformulariosBeforePost(DataSet: TDataSet);
begin
    qformularioside.Value:=dm.tempresaid.asinteger;
end;

end.
