unit uViewColumnas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, dxLayoutControl, cxControls, cxContainer, cxListBox,
  TBXDkPanels, ActnList, cxCustomData,  cxGridDBTableView, cxGridTableView,
  JvExControls, JvComponent, JvxCheckListBox, uViewControlGrid,
  cxCheckListBox, cxCheckBox, uViewParametrosControlGrid;

type
  IViewColumnas = interface(IViewParametrosControlGrid)
  ['{FD33275B-6875-421A-ABAC-595982ACDDDB}']
    function GetCheckedColumns : TStringList;
  end;

  TfrViewColumnas = class(TfrViewParametrosControlGrid, IViewColumnas)
    lbColumnas: TcxCheckListBox;
    TBXLabel1: TTBXLabel;
    procedure lbColumnasClickCheck(Sender: TObject; AIndex: Integer;
      APrevState, ANewState: TcxCheckBoxState);
  protected
    procedure RellenarListaColumnas;
    procedure SetModificable(const Value : Boolean); override;
  public
    procedure Refresh; override;
    procedure Execute; override;
    function GetCheckedColumns : TStringList;
  end;

implementation

{$R *.dfm}

{ TfrViewColumnas }

procedure TfrViewColumnas.Refresh;
begin
  inherited;
  RellenarListaColumnas;
end;

procedure TfrViewColumnas.RellenarListaColumnas;
var
  i : integer;
  AIndex : Integer;
begin
  with lbColumnas do
  begin
    Items.BeginUpdate;
    Items.Clear;
    try
      for i := 0 to ViewControlGrid.ColumnCount - 1 do
      begin
        AddItem(ViewControlGrid.Columns[i].Caption);
        AIndex := Items.Count - 1;
        Items[AIndex].Checked := ViewControlGrid.Columns[i].Visible;
        Items[AIndex].ItemObject := ViewControlGrid.Columns[i];
      end;
    finally
      Items.EndUpdate;
    end;
  end;
end;

procedure TfrViewColumnas.lbColumnasClickCheck(Sender: TObject;
  AIndex: Integer; APrevState, ANewState: TcxCheckBoxState);
var
  i : integer;
begin
  inherited;
  { No hacer nada por que el cambio en las columnas se debe ver cuando
    el usuario pulse sobre el botón 'Refrescar'. }
{  with lbColumnas.Items[AIndex] do
    if Assigned(ItemObject) then
      (ItemObject as TcxGridDBColumn).Visible := (ANewState = cbsChecked);}
end;

procedure TfrViewColumnas.Execute;
var
  AIndex : integer;
begin
  inherited;
  for AIndex := 0 to lbColumnas.Items.Count - 1 do
    with lbColumnas.Items[AIndex] do
      if Assigned(ItemObject) then
        (ItemObject as TcxGridDBColumn).Visible := (State = cbsChecked);
end;

function TfrViewColumnas.GetCheckedColumns: TStringList;
var
  AIndex : integer;
begin
  result := TStringList.Create;
  for AIndex := 0 to lbColumnas.Items.Count - 1 do
    if (lbColumnas.Items[AIndex].State = cbsChecked) then
      with lbColumnas.Items[AIndex] do
        if Assigned(ItemObject) then
          Result.Add((ItemObject as TcxGridDBColumn).DataBinding.FieldName);
end;

procedure TfrViewColumnas.SetModificable(const Value: Boolean);
begin
  inherited;
  lbColumnas.ReadOnly := not Value;
  if lbColumnas.ReadOnly then
    lbColumnas.Style.TextColor := clBtnShadow
  else
    lbColumnas.Style.TextColor := clWindowText;
end;

end.
