{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uViewGridBase;

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, dxPScxGridLnk, dxPrnDlg,
  cxIntlPrintSys3, dxPSPrvwAdv, uGridUtils;

type
  IViewGridBase = interface(IViewBase)
  ['{D5B9B017-2A2E-44AC-8223-E54664C6BC66}']
    procedure ExpandirTodo;
    procedure ContraerTodo;
    procedure AjustarAncho;

    procedure Preview;
    procedure Print;
    procedure PrintSetup;

    function IsEmpty : Boolean;

    procedure SaveGridStatus;
    procedure RestoreGridStatus;

    procedure GotoFirst;
    procedure GotoLast;

    function GetFocusedView : TcxGridDBTableView;
    property _FocusedView : TcxGridDBTableView read GetFocusedView;

    function GetGrid : TcxGrid;
    property _Grid : TcxGrid read GetGrid;

    procedure StoreToRegistry (const Path : String);
    procedure RestoreFromRegistry (const Path : String);

    procedure SetDblClick(const Value: TNotifyEvent);
    function GetDblClick: TNotifyEvent;
    property OnDblClick: TNotifyEvent read GetDblClick write SetDblClick;

    procedure SetPopupMenu(const Value: TPopupMenu);
    function GetPopupMenu: TPopupMenu;
    property PopupMenu: TPopupMenu read GetPopupMenu write SetPopupMenu;

    function GetMultiSelect: Boolean;
    procedure SetMultiSelect(const Value: Boolean);
    property MultiSelect : Boolean read GetMultiSelect write SetMultiSelect;

    procedure SetFilter(const Value: string);
    function GetFilter: string;
    property Filter: string read GetFilter write SetFilter;

    function GetFiltered: Boolean;     
    property Filtered : Boolean read GetFiltered;
  end;


  TfrViewGridBase = class(TfrViewBase, IViewGridBase)
    dsDataSource: TDADataSource;
  private
    FFilter: string;
    FOnFilterChanged : TNotifyEvent;
    procedure FiltrarGrid(TextoFiltro : String);
  protected
    FGridStatus : TcxGridStatus;
    FOnDblClick: TNotifyEvent;
    FPopupMenu: TPopupMenu;
    function GetMultiSelect: Boolean; virtual;
    procedure SetMultiSelect(const Value: Boolean); virtual;
    procedure SetPopupMenu(const Value: TPopupMenu); virtual;
    function GetPopupMenu: TPopupMenu; virtual;
    procedure SetDblClick(const Value: TNotifyEvent); virtual;
    function GetDblClick: TNotifyEvent; virtual;
    function GetGrid : TcxGrid; virtual; abstract;
    function GetFocusedView : TcxGridDBTableView; virtual; abstract;
    procedure SetFilter(const Value: string); virtual;
    function GetFilter: string; virtual;
    function GetFiltered: Boolean; virtual;
    procedure FilterChanged(Sender : TObject); virtual;
  public
    constructor Create(AOwner: TComponent); override;

    procedure ShowEmbedded(const AParent : TWinControl); override;

    procedure ExpandirTodo;
    procedure ContraerTodo;
    procedure AjustarAncho;

    procedure Preview; virtual;
    procedure Print; virtual;
    procedure PrintSetup; virtual;

    function IsEmpty : Boolean;

    procedure SaveGridStatus;
    procedure RestoreGridStatus;

    procedure GotoFirst;
    procedure GotoLast;

    procedure StoreToRegistry (const Path : String);
    procedure RestoreFromRegistry (const Path : String);

    property Filter: string read GetFilter write SetFilter;
    property Filtered : Boolean read GetFiltered;
    property _FocusedView : TcxGridDBTableView read GetFocusedView;
    property _Grid : TcxGrid read GetGrid;
    property OnDblClick: TNotifyEvent read GetDblClick write SetDblClick;
    property PopupMenu: TPopupMenu read GetPopupMenu write SetPopupMenu;
    property MultiSelect : Boolean read GetMultiSelect write SetMultiSelect;
    destructor Destroy; override;
  end;

procedure Register;

implementation

uses
  CCReg, uDataModuleBase, uDBSelectionListUtils;

{$R *.dfm}

procedure Register;
begin
  RegisterCustomContainer(TfrViewGridBase);
end;

{ TfrViewGrid }

procedure TfrViewGridBase.AjustarAncho;
begin
  if Assigned(_FocusedView) then
    _FocusedView.ApplyBestFit;
end;

procedure TfrViewGridBase.ContraerTodo;
begin
  if Assigned(_FocusedView) then
    _FocusedView.ViewData.Collapse(True);
end;

constructor TfrViewGridBase.Create(AOwner: TComponent);
begin
  inherited;
  FFilter := '';
  FOnFilterChanged := FilterChanged;
  FPopupMenu := nil;
  FOnDblClick := nil;
  FGridStatus := NIL;
end;

procedure TfrViewGridBase.ExpandirTodo;
begin
  if Assigned(_FocusedView) then
    _FocusedView.ViewData.Expand(True);
end;

function TfrViewGridBase.GetDblClick: TNotifyEvent;
begin
  Result := FOnDblClick;
end;

function TfrViewGridBase.GetFilter: string;
begin
  Result := FFilter;
end;

function TfrViewGridBase.GetFiltered: Boolean;
begin
  Result := (_FocusedView.DataController.Filter.Root.Count > 0);  
end;

function TfrViewGridBase.GetMultiSelect: Boolean;
begin
  Result := _FocusedView.OptionsSelection.MultiSelect;
end;

function TfrViewGridBase.GetPopupMenu: TPopupMenu;
begin
  Result := FPopupMenu;
end;

procedure TfrViewGridBase.GotoFirst;
begin
  if Assigned(_FocusedView) then
    _FocusedView.DataController.GotoFirst;
end;

procedure TfrViewGridBase.GotoLast;
begin
  if Assigned(_FocusedView) then
    _FocusedView.DataController.GotoLast;
end;

function TfrViewGridBase.IsEmpty: Boolean;
begin
  Result := (_FocusedView.ViewData.RowCount < 1);
end;

procedure TfrViewGridBase.Preview;
begin

end;

procedure TfrViewGridBase.Print;
begin

end;

procedure TfrViewGridBase.PrintSetup;
begin

end;

procedure TfrViewGridBase.RestoreFromRegistry(const Path : String);
begin
  if Assigned(_FocusedView) then
    _FocusedView.RestoreFromRegistry(Path + '\\GridSettings\\' + Self.Name, False, False, []);
end;

procedure TfrViewGridBase.RestoreGridStatus;
begin
  if Assigned(FGridStatus) then
    FGridStatus.Restore(_FocusedView);
end;

procedure TfrViewGridBase.SaveGridStatus;
begin
  FreeAndNil(FGridStatus);
  FGridStatus := TcxGridStatus.Create(_FocusedView);
end;

procedure TfrViewGridBase.SetDblClick(const Value: TNotifyEvent);
begin
  FOnDblClick := Value;
end;

procedure TfrViewGridBase.SetFilter(const Value: string);
begin
  if FFilter <> Value then
  begin
    FFilter := Value;
    FiltrarGrid(FFilter);
    if Assigned(FOnFilterChanged) then
      FOnFilterChanged(Self);
  end;
end;

procedure TfrViewGridBase.SetMultiSelect(const Value: Boolean);
begin
  _FocusedView.OptionsSelection.MultiSelect := Value;
//  _FocusedView..OnSelectionChanged := SelectionChanged;
end;

procedure TfrViewGridBase.SetPopupMenu(const Value: TPopupMenu);
begin
  FPopupMenu := Value;
end;

procedure TfrViewGridBase.ShowEmbedded(const AParent: TWinControl);
begin
  inherited;

  // No activar la tabla ya por si acaso tuviera parámetros
{  if not DADataSource.DataTable.Active then
    DADataSource.DataTable.Active := True;}

  GotoFirst;
  _FocusedView.Focused := True;
  if _FocusedView.ViewData.RecordCount > 0 then
  begin
    _FocusedView.ViewData.Records[0].Selected := True;
    _FocusedView.ViewData.Records[0].Focused := True;
  end;
end;

procedure TfrViewGridBase.StoreToRegistry(const Path : String);
begin
  if Assigned(_FocusedView) then
    _FocusedView.StoreToRegistry(Path + '\\GridSettings\\' + Self.Name, False, []);
end;

procedure TfrViewGridBase.FiltrarGrid(TextoFiltro : String);
var
  Columna: TcxGridDBColumn;
  i: Integer;
  AItemList: TcxFilterCriteriaItemList;
begin
  with _FocusedView.DataController.Filter do
  begin
    BeginUpdate;
    try
      Options := [fcoCaseInsensitive, fcoSoftCompare];
      Root.Clear;
      if Length(TextoFiltro) > 0 then
      begin
        AItemList := Root.AddItemList(fboAnd);
        AItemList.BoolOperatorKind := fboOr;
        for i:=0 to (_FocusedView as TcxGridDBTableView).ColumnCount - 1 do
        begin
          Columna := (_FocusedView as TcxGridDBTableView).Columns[i];
          if (Length(Columna.Caption) > 0) and (Columna.Caption <> 'RecID') then
             AItemList.AddItem(Columna, foLike, '%'+TextoFiltro+'%', IntToStr(i));
        end;
        Active := True;
      end
      else
        Active := False;
    finally
      EndUpdate;
    end;
  end;
end;


procedure TfrViewGridBase.FilterChanged(Sender: TObject);
begin
//
end;

destructor TfrViewGridBase.Destroy;
begin
  FOnFilterChanged := Nil;
  if Assigned(FGridStatus) then
    FreeAndNil(FGridStatus);
  inherited;
end;

end.

