{*******************************************************}
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uViewGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, uDADataTable, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, Menus, ActnList, Grids,
  DBGrids, JvComponent, JvFormAutoSize, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPrnDlg,
  cxIntlPrintSys3, dxPSPrvwAdv, uViewGridBase, cxGridCustomPopupMenu,
  cxGridPopupMenu, uViewFiltroBase, ComCtrls, cxPC, ImgList, PngImageList,
  TB2Item, TBX, TB2Dock, TB2Toolbar, TBXDkPanels, dxPgsDlg, uDAInterfaces,
  dxPScxGrid6Lnk, uCustomView;

type
  IViewGrid = interface(IViewGridBase)
    ['{D6D79E4F-15DD-439E-B1DF-7EC05300A307}']
  end;

  TfrViewGrid = class(TfrViewGridBase, IViewGrid)
    cxGrid: TcxGrid;
    cxGridLevel: TcxGridLevel;
    cxGridView: TcxGridDBTableView;
    dxComponentPrinter: TdxComponentPrinter;
    dxPSEngineController1: TdxPSEngineController;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleSelection: TcxStyle;
    dxComponentPrinterLink: TdxGridReportLink;
    frViewFiltroBase1: TfrViewFiltroBase;
    ActionList1: TActionList;
    actQuitarAgrupaciones: TAction;
    pnlAgrupaciones: TTBXDockablePanel;
    TBXAlignmentPanel1: TTBXAlignmentPanel;
    TBXToolbar1: TTBXToolbar;
    TBXItem1: TTBXItem;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxStyle_NORMAL_PAR: TcxStyle;
    cxStyle_NORMAL_IMPAR: TcxStyle;
    cxStyle_ORDENADO_PAR: TcxStyle;
    cxStyle_ORDENADO_IMPAR: TcxStyle;
    cxStyle_FILTRADO_PAR: TcxStyle;
    cxStyle_FILTRADO_IMPAR: TcxStyle;
    cxStyle_FILTRADO_ORDENADO_PAR: TcxStyle;
    cxStyle_FILTRADO_ORDENADO_IMPAR: TcxStyle;
    GridPNGImageList: TPngImageList;
    cxViewGridPopupMenu: TcxGridPopupMenu;
    procedure cxGridViewStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridViewDblClick(Sender: TObject);
    procedure actQuitarAgrupacionesExecute(Sender: TObject);
    procedure actQuitarAgrupacionesUpdate(Sender: TObject);
    procedure cxGridViewICONOCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure cxGridViewICONOCustomDrawHeader(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo;
      var ADone: Boolean);
  protected
    function GetGrid : TcxGrid; override;
    function GetFocusedView : TcxGridDBTableView; override;
    procedure SetPopupMenu(const Value: TPopupMenu); override;
    procedure OnChangeValoresFiltro(Sender: TObject);
    procedure SetViewFiltros(const Value: IViewFiltroBase); override;
    function AddFilterGrid(const Operacion: tcxFilterBoolOperatorKind): TcxFilterCriteriaItemList;
    function GetAgrupacionesVisibles: Boolean; override;
  public
    function EsSeleccionCeldaDatos: Boolean; override;
    procedure Preview; override;
    procedure Print; override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  uDMBase, uDBSelectionListUtils, dxOffice11, cxGridCommon;

{$R *.dfm}

{
********************************* TfrViewGrid **********************************
}

{ TfrViewGrid }
function TfrViewGrid.GetAgrupacionesVisibles: Boolean;
begin
  Result := pnlAgrupaciones.Visible;
end;

function TfrViewGrid.GetFocusedView: TcxGridDBTableView;
begin
  Result := cxGridView;
end;

function TfrViewGrid.GetGrid: TcxGrid;
begin
  Result := cxGrid;
end;

procedure TfrViewGrid.OnChangeValoresFiltro(Sender: TObject);
begin
  cxGridView.DataController.Filter.BeginUpdate;
  try
    RefrescarFiltro;
  finally
    cxGridView.DataController.Filter.EndUpdate;
  end;
end;

procedure TfrViewGrid.Preview;
begin
  inherited;
  dxComponentPrinter.Preview;
end;

procedure TfrViewGrid.Print;
begin
  inherited;
  dxComponentPrinter.Print(True, nil, nil);
end;

procedure TfrViewGrid.SetPopupMenu(const Value: TPopupMenu);
begin
  inherited;
  cxViewGridPopupMenu.PopupMenus[0].PopupMenu := FPopupMenu;
end;

procedure TfrViewGrid.SetViewFiltros(const Value: IViewFiltroBase);
begin
  inherited;
  if Assigned(ViewFiltros) then
     ViewFiltros.OnFiltrosChange := OnChangeValoresFiltro
end;

procedure TfrViewGrid.actQuitarAgrupacionesExecute(Sender: TObject);
var
  Columna: TcxGridDBColumn;
  i,j: Integer;
begin
  inherited;
  for j := 0 to cxGrid.ViewCount - 1 do
  begin
    for i := 0 to  (cxGrid.Views[j] as tcxGridDBTableView).ColumnCount - 1 do
    begin
      Columna := (cxGrid.Views[j] as TcxGridDBTableView).Columns[i];
      if not (Columna.GroupIndex < 0) then
      begin
        Columna.GroupIndex := -1;
        Columna.Visible := True;
      end;
    end;
  end;
end;

procedure TfrViewGrid.actQuitarAgrupacionesUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (cxGridView.GroupedColumnCount > 0);
end;

function TfrViewGrid.AddFilterGrid(const Operacion: tcxFilterBoolOperatorKind): TcxFilterCriteriaItemList;
var
  AItemList: TcxFilterCriteriaItemList;
begin
  AItemList := cxGridView.DataController.Filter.Root;
  Result := AItemList.AddItemList(Operacion);
end;

constructor TfrViewGrid.Create(AOwner: TComponent);
begin
  inherited;
  ViewFiltros := frViewFiltroBase1;
end;

procedure TfrViewGrid.cxGridViewDblClick(Sender: TObject);
begin
  inherited;
  if Assigned(FOnDblClick) then
    FOnDblClick(Sender);
end;

procedure TfrViewGrid.cxGridViewICONOCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  R : TRect;
begin
  inherited;
  R := AViewInfo.ContentBounds;
  ACanvas.FillRect(R);
  ACanvas.DrawImage(GridPNGImageList, R.Left + 2, R.Top + 2, 1);
  ADone := True;
end;

procedure TfrViewGrid.cxGridViewICONOCustomDrawHeader(Sender: TcxGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo;
  var ADone: Boolean);
var
  R : TRect;
begin
  inherited;
  with AViewInfo do
  begin
    LookAndFeelPainter.DrawHeader(ACanvas, Bounds, TextAreaBounds, Neighbors,
      Borders, ButtonState, AlignmentHorz, AlignmentVert, MultiLine,
      False, '', Params.Font, Params.TextColor, Params.Color);

    R := AViewInfo.ContentBounds;
    ACanvas.FillRect(R);
    ACanvas.DrawImage(GridPNGImageList, R.Left + 2, R.Top + 2, 0);
  end;

  ADone := True;
end;

procedure TfrViewGrid.cxGridViewStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  AFiltrado : Boolean;
  AOrdenado : Boolean;
  AFilaPar : Boolean;
begin
  inherited;
  if Assigned(AItem) then
  begin
    AFilaPar := Odd(ARecord.Index);
    AFiltrado := Filtered;
    AOrdenado := AItem.SortOrder <> soNone;


    if AFilaPar then
    begin
      if AFiltrado then
      begin
        if AOrdenado then
          AStyle := cxStyle_FILTRADO_ORDENADO_PAR
        else
          AStyle := cxStyle_FILTRADO_PAR
      end
      else // No filtrado
        if AOrdenado then
          AStyle := cxStyle_ORDENADO_PAR
        else
          AStyle := cxStyle_NORMAL_PAR
    end
    else begin
      if AFiltrado then
      begin
        if AOrdenado then
          AStyle := cxStyle_FILTRADO_ORDENADO_IMPAR
        else
          AStyle := cxStyle_FILTRADO_IMPAR
      end
      else // No filtrado
        if AOrdenado then
          AStyle := cxStyle_ORDENADO_IMPAR
        else
          AStyle := cxStyle_NORMAL_IMPAR
    end;
  end;
end;

destructor TfrViewGrid.Destroy;
begin
  ViewFiltros := Nil;
  inherited;
end;

function TfrViewGrid.esSeleccionCeldaDatos: Boolean;
begin
  Result := not (cxGridView.Controller.FocusedRecord is TcxGridGroupRow);
end;

end.

