unit puestos_trabajo;

interface

uses
  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,
  ZAbstractDataset, ZDataset, Data.DB, ZAbstractRODataset, ZAbstractTable,Excel2000,
  ComObj;

type
  Tfpuestos_trabajo = class(TForm)
    Panel7: TPanel;
    btAcancelar: TButton;
    btAmodificar: TButton;
    btAborrar: TButton;
    btAnuevo: TButton;
    btAguardar: TButton;
    qaux: TZQuery;
    Panel1: TPanel;
    Panel2: TPanel;
    DBGrid1: TDBGrid;
    dsqimpuestos: TDataSource;
    qpuestos_trabajo: TZQuery;
    gcrpagado: TDBCheckBox;
    qpuestos_trabajoid: TIntegerField;
    qpuestos_trabajodescripcion: TWideStringField;
    qpuestos_trabajoactivo: TIntegerField;
    procedure btAnuevoClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure dsqimpuestosStateChange(Sender: TObject);
    procedure btAborrarClick(Sender: TObject);
    procedure btAmodificarClick(Sender: TObject);
    procedure btAguardarClick(Sender: TObject);
    procedure btAcancelarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AppMessage(var Msg: Tmsg; var Handled:Boolean);
  end;

var
  fpuestos_trabajo: Tfpuestos_trabajo;

implementation

{$R *.dfm}
procedure Tfpuestos_trabajo.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 añadir 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 pondrían todos los controles que usamos en la
       // aplicación y necesitamos cambiar el tab por intro.
       // de esta forma te despreocupas de cuantos añades y es más
       // limpio el código

       end;
    end;
 end;

procedure Tfpuestos_trabajo.btAborrarClick(Sender: TObject);
begin
   qpuestos_trabajo.Delete;
end;

procedure Tfpuestos_trabajo.btAcancelarClick(Sender: TObject);
begin
    qpuestos_trabajo.cancel;
end;

procedure Tfpuestos_trabajo.btAguardarClick(Sender: TObject);
begin
    qpuestos_trabajo.Post;
end;

procedure Tfpuestos_trabajo.btAmodificarClick(Sender: TObject);
begin
    qpuestos_trabajo.Edit;
end;

procedure Tfpuestos_trabajo.btAnuevoClick(Sender: TObject);
begin
   qaux.Close;
   qaux.sql.clear;
   qaux.SQL.Add('select max(id)+1 from puestos_trabajo');
   qaux.Open;
   qpuestos_trabajo.append;
   if qaux.Fields[0].AsInteger<0 then qpuestos_trabajoid.Value:=0 else qpuestos_trabajoid.Value:=qaux.Fields[0].AsInteger;
   dbgrid1.setfocus;
end;

procedure Tfpuestos_trabajo.DBGrid1CellClick(Column: TColumn);
begin
    if column.Field.FieldName ='activo' then
    begin
        if qpuestos_trabajoactivo.asinteger=1 then
         begin
           qpuestos_trabajo.Edit;
           qpuestos_trabajoactivo.Value:=0;
           qpuestos_trabajo.Post;
           dbgrid1.selectedIndex:=1;
         end else
               begin
                 qpuestos_trabajo.Edit;
                 qpuestos_trabajoactivo.Value:=1;
                 qpuestos_trabajo.Post;
                 qpuestos_trabajo.Refresh;
                 dbgrid1.SelectedIndex:=1;
               end;
    end;
end;

procedure Tfpuestos_trabajo.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 = 'activo') 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 = 'activo') 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 Tfpuestos_trabajo.dsqimpuestosStateChange(Sender: TObject);
begin
  if qpuestos_trabajo.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 Tfpuestos_trabajo.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if (activecontrol is tdbgrid) then
    begin
        if Key = 13 then Key :=9;
    end;
end;

procedure Tfpuestos_trabajo.FormKeyPress(Sender: TObject; var Key: Char);
var formatos:tformatsettings;
punto:string;
begin
    GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatos);
    punto:=formatos.DecimalSeparator;
    if punto=',' then
    begin
        key:=upcase(key);
        if (key='.') and (activecontrol is tdbgrid) then
        begin
            key:=','
        end;
    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 Tfpuestos_trabajo.FormShow(Sender: TObject);
begin
    qpuestos_trabajo.Close;
    qpuestos_trabajo.Open;
end;

end.
