unit uEditorGrid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, ActnList, TB2Dock, TB2Toolbar, TBX, TB2Item,
  ImgList, PngImageList, StdActns, TB2ExtItems, TBXExtItems, uViewGrid,
  uEditorDBBase, DB, uDADataTable, Menus, JvFormAutoSize,
  uDAScriptingProvider, uDACDSDataTable, AppEvnts, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, pngimage, ExtCtrls, uCustomView,
  uViewBase, uViewBarraSeleccion, dxLayoutLookAndFeels, JvComponentBase;

type
  IEditorGrid = interface(IEditorDBBase)
    ['{CB8CDE00-B225-4A1D-9A5C-EC6FBE2C854B}']
  end;

  TfEditorGrid = class(TfEditorDBBase, IEditorGrid)
    tbxEditFiltro: TTBXEditItem;
    tbxFiltro: TTBXToolbar;
    TBXItem7: TTBXItem;
    GridPopupMenu: TPopupMenu;
    Modificar1: TMenuItem;
    Eliminar1: TMenuItem;
    N1: TMenuItem;
    Nuevo1: TMenuItem;
    TBXLabelItem1: TTBXLabelItem;
    actQuitarFiltro: TAction;
    N2: TMenuItem;
    Previsualizar1: TMenuItem;
    Imprimir1: TMenuItem;
    TBXSeparatorItem14: TTBXSeparatorItem;
    TBXItem33: TTBXItem;
    actDuplicar: TAction;
    TBXSeparatorItem15: TTBXSeparatorItem;
    TBXItem34: TTBXItem;
    Duplicar1: TMenuItem;
    frViewBarraSeleccion: TfrViewBarraSeleccion;
    procedure tbxEditFiltroChange(Sender: TObject; const Text: String);
    procedure FormShow(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actConfPaginaExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actQuitarFiltroExecute(Sender: TObject);
    procedure actModificarExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actDuplicarUpdate(Sender: TObject);
    procedure actDuplicarExecute(Sender: TObject);
    procedure JvFormStorageSavePlacement(Sender: TObject);
    procedure JvFormStorageRestorePlacement(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
  private
    FViewGrid : IViewGrid;
  protected
    procedure SetViewGrid(const Value : IViewGrid); virtual;
    function GetViewGrid: IViewGrid;
    function GetSelectionBarVisible: Boolean;
    procedure SetSelectionBarVisible(const Value: Boolean); Virtual;
    function GetMultiSelection: Boolean;
    procedure SetMultiSelection(const Value: Boolean);    
  public
    property ViewGrid: IViewGrid read GetViewGrid write SetViewGrid;
    constructor Create(AOwner : TComponent); override;    
    destructor Destroy; override;
    property MultiSelection : Boolean read GetMultiSelection write SetMultiSelection;
    property SelectionBarVisible : Boolean read GetSelectionBarVisible write SetSelectionBarVisible;    
  end;

implementation

uses
  uDataModuleBase, uCustomEditor, cxGridTableView;

{$R *.dfm}

{
********************************* TfEditorGrid *********************************
}
destructor TfEditorGrid.Destroy;
begin
  FViewGrid := NIL;
  inherited;
end;

function TfEditorGrid.GetViewGrid: IViewGrid;
begin
  Result := FViewGrid;
end;

procedure TfEditorGrid.SetViewGrid(const Value: IViewGrid);
begin
  FViewGrid := Value;
  if Assigned(FViewGrid) then
  begin
    FViewGrid.PopupMenu := GridPopupMenu;
    FViewGrid.OnDblClick := actModificar.OnExecute;
  end;
end;

procedure TfEditorGrid.tbxEditFiltroChange(Sender: TObject; const Text: String);
begin
  if Assigned(ViewGrid) then
    ViewGrid.Filter := Text;
end;

procedure TfEditorGrid.FormShow(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
     ViewGrid.ShowEmbedded(Self);
end;

procedure TfEditorGrid.actPrevisualizarExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.Preview;
end;

procedure TfEditorGrid.actConfPaginaExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.PrintSetup;
end;

procedure TfEditorGrid.actImprimirExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.Print;
end;

procedure TfEditorGrid.actQuitarFiltroExecute(Sender: TObject);
begin
  if Assigned(ViewGrid) then
  begin
    tbxEditFiltro.Text := '';
    ViewGrid.Filter := '';
  end;
end;

procedure TfEditorGrid.actModificarExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.SyncFocusedRecordsFromGrid;
end;

procedure TfEditorGrid.actEliminarExecute(Sender: TObject);
begin
  ViewGrid.SyncFocusedRecordsFromGrid;  // Esto va ANTES del inherited. No cambiar.
  inherited;
end;

constructor TfEditorGrid.Create(AOwner: TComponent);
begin
  inherited;
  actModificar.ShortCut := ShortCut(VK_RETURN, []);
end;

procedure TfEditorGrid.actDuplicarUpdate(Sender: TObject);
begin
  if Assigned(dsDataTable.DataTable) then
    (Sender as TAction).Enabled := (not dsDataTable.DataTable.IsEmpty)
      and not (dsDataTable.DataTable.State = dsInsert)
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfEditorGrid.actDuplicarExecute(Sender: TObject);
begin
  ViewGrid.SyncFocusedRecordsFromGrid;  // Esto va ANTES del inherited. No cambiar.
  inherited;
end;

procedure TfEditorGrid.JvFormStorageSavePlacement(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.StoreToRegistry;
end;

procedure TfEditorGrid.JvFormStorageRestorePlacement(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.RestoreFromRegistry;
end;

procedure TfEditorGrid.actRefrescarExecute(Sender: TObject);
var
  FocusedRow, TopRow : Integer;
begin
  ViewGrid.SyncFocusedRecordsFromGrid;

  TopRow := (ViewGrid.Grid.ActiveView.Controller as TcxGridTableController).TopRowIndex;
  FocusedRow := ViewGrid.Grid.ActiveView.DataController.FocusedRowIndex;

  inherited;

  ViewGrid.Grid.ActiveView.DataController.FocusedRowIndex := FocusedRow;
  (ViewGrid.Grid.ActiveView.Controller as TcxGridTableController).TopRowIndex := TopRow;
  //  ViewGrid.Grid.ActiveView.Controller.TopRowIndex := TopRow;

//  ViewGrid.SyncFocusedRecordsFromDataSet;
end;

function TfEditorGrid.GetMultiSelection: Boolean;
begin
  Result := ViewGrid.MultiSelection;
end;

function TfEditorGrid.GetSelectionBarVisible: Boolean;
begin
  Result := frViewBarraSeleccion.Visible;
end;

procedure TfEditorGrid.SetMultiSelection(const Value: Boolean);
begin
  ViewGrid.MultiSelection := Value;
end;

procedure TfEditorGrid.SetSelectionBarVisible(const Value: Boolean);
begin
  frViewBarraSeleccion.Visible := Value;
end;

initialization
  RegisterClass(TfEditorGrid);

finalization
  UnRegisterClass(TfEditorGrid);

end.

