unit fmaterialespesada;

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, Data.DB,  
  Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, Vcl.DBCtrls, 

  Vcl.Mask,
  Vcl.ComCtrls, Vcl.ExtCtrls, Excel2000,
  ComObj;

type
  Tmaterialespesadas = class(TForm)
    DBGrid1: TDBGrid;
    qmateriales: TFDQuery;
    dsqmateriales: TDataSource;
    gcrpagado: TDBCheckBox;
    qmateriales2: TFDQuery;
    qmaterialesid: TIntegerField;
    qmaterialescodigo: TWideStringField;
    qmaterialesdescripcion: TWideStringField;
    qmaterialeside: TIntegerField;
    qmaterialesidea: TIntegerField;
    qmaterialesidea2: TIntegerField;
    qmaterialesidaf: TIntegerField;
    qmaterialescantidad: TFloatField;
    qmaterialespeso: TFloatField;
    qmaterialeslote: TWideStringField;
    qmaterialesprecio: TFloatField;
    qmaterialesrecuperado: TIntegerField;
    qmateriales2ide: TIntegerField;
    qmateriales2idea: TIntegerField;
    qmateriales2idea2: TIntegerField;
    qmateriales2idaf: TIntegerField;
    qmateriales2cantidad: TFloatField;
    qmateriales2peso: TFloatField;
    qmateriales2lote: TWideStringField;
    qmateriales2precio: TFloatField;
    qmateriales2recuperado: TIntegerField;
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AppMessage(var Msg: Tmsg; var Handled:Boolean);
  end;

var
  materialespesadas: Tmaterialespesadas;

implementation

{$R *.dfm}
procedure Tmaterialespesadas.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 Tmaterialespesadas.DBGrid1CellClick(Column: TColumn);
begin
    if column.Field.FieldName ='recuperado' then
    begin
        if qmateriales2recuperado.asinteger=-1 then
         begin
           qmateriales2.Edit;
           qmateriales2recuperado.Value:=0;
           qmateriales2.Post;
           dbgrid1.selectedIndex:=1;
         end else
               begin
                 qmateriales2.Edit;
                 qmateriales2recuperado.Value:=-1;
                 qmateriales2.Post;
                 qmateriales2.Refresh;
                 dbgrid1.SelectedIndex:=1;
               end;
    end;
    qmateriales.Refresh;
end;

procedure Tmaterialespesadas.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 = 'recuperado') 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 = 'recuperado') 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];
         DBgrid1.Canvas.FillRect(Rect);
         DrawFrameControl(DBgrid1.Canvas.Handle, DrawRect,
         DFC_BUTTON, DrawState);
       end;
    end;
end;

procedure Tmaterialespesadas.FormCreate(Sender: TObject);
var
  I: Integer;
begin
   Application.OnMessage :=  AppMessage;
   gcrpagado.DataField := 'recuperado';
   gcrpagado.Visible := False;
   gcrpagado.SendToBack;
   gcrpagado.Color := DBGrid1.Color;
   gcrpagado.Caption := '';

   //explained later in the article
   gcrpagado.ValueChecked := '-1';
   gcrpagado.ValueUnChecked := '0';




end;

procedure Tmaterialespesadas.FormShow(Sender: TObject);
begin
   qmateriales.open;
   qmateriales2.open;
end;

end.
