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

unit uEditorGridBase;

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, uViewGridBase,
  uEditorDBBase, DB, uDADataTable, Menus, JvFormAutoSize,
  uDAScriptingProvider, uDACDSDataTable, AppEvnts, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, pngimage, ExtCtrls,
  JvComponentBase, dxLayoutLookAndFeels, TBXStatusBars, JvExComCtrls,
  JvStatusBar;

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

  TfEditorGridBase = class(TfEditorDBBase, IEditorGridBase)
    tbxEditFiltro: TTBXEditItem;
    tbxFiltro: TTBXToolbar;
    GridPopupMenu: TPopupMenu;
    Modificar1: TMenuItem;
    Eliminar1: TMenuItem;
    N1: TMenuItem;
    Nuevo1: TMenuItem;
    TBXLabelItem1: TTBXLabelItem;
    actQuitarFiltro: TAction;
    N2: TMenuItem;
    Previsualizar1: TMenuItem;
    Imprimir1: TMenuItem;
    TBXSeparatorItem6: TTBXSeparatorItem;
    TBXItem7: TTBXItem;
    TBXSeparatorItem14: TTBXSeparatorItem;
    TBXItem33: TTBXItem;
    N3: TMenuItem;
    Actualizar1: TMenuItem;
    TBXItem34: TTBXItem;
    actAnchoAuto: TAction;
    TBXSeparatorItem16: TTBXSeparatorItem;
    TBXItem35: TTBXItem;
    TBXItem36: TTBXItem;
    procedure tbxEditFiltroChange(Sender: TObject; const Text: String);
    procedure FormShow(Sender: TObject); override;
    procedure actQuitarFiltroExecute(Sender: TObject);
    procedure actDuplicarUpdate(Sender: TObject);
    procedure JvFormStorageSavePlacement(Sender: TObject);
    procedure JvFormStorageRestorePlacement(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actNuevoUpdate(Sender: TObject);
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure actAnchoAutoUpdate(Sender: TObject);
    procedure actRefrescarUpdate(Sender: TObject);
  private
    FViewGrid : IViewGridBase;
  protected
    procedure SetViewGrid(const Value : IViewGridBase); virtual;
    function GetViewGrid: IViewGridBase;

    procedure PrevisualizarInterno; override;
    procedure ConfPaginaInterno; override;
    procedure ImprimirInterno; override;
    procedure RefrescarInterno; override;
  public
    property ViewGrid: IViewGridBase read GetViewGrid write SetViewGrid;
    constructor Create(AOwner : TComponent); override;

    destructor Destroy; override;
  end;

implementation

uses
  uDataModuleBase, uCustomEditor, cxGridTableView, uViewGrid;

{$R *.dfm}

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

function TfEditorGridBase.GetViewGrid: IViewGridBase;
begin
  Result := FViewGrid;
end;

procedure TfEditorGridBase.ImprimirInterno;
begin
  inherited;
  (ViewGrid as IViewGrid).Print;
end;

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

procedure TfEditorGridBase.tbxEditFiltroChange(Sender: TObject; const Text: String);
begin
  if Assigned(ViewGrid) then
  begin
    if Length(Text) > 0 then
      ViewGrid.Filter := Text
    else
      actQuitarFiltro.Execute;
  end;
end;

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

procedure TfEditorGridBase.actEliminarUpdate(Sender: TObject);
begin
  inherited;
  if HayDatos and Assigned(ViewGrid) then
    (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes)
      and not ViewGrid.IsEmpty
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfEditorGridBase.actModificarUpdate(Sender: TObject);
begin
  inherited;
  if HayDatos and Assigned(ViewGrid) then
    (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes)
      and not ViewGrid.IsEmpty
  else
    (Sender as TAction).Enabled := False;
end;

procedure TfEditorGridBase.actNuevoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(dsDataTable.DataTable);
end;

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

procedure TfEditorGridBase.actRefrescarUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := Assigned(dsDataTable.DataTable);
end;

procedure TfEditorGridBase.ConfPaginaInterno;
begin
  inherited;
  (ViewGrid as IViewGrid).PrintSetup;
end;

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

procedure TfEditorGridBase.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.AjustarAncho;
end;

procedure TfEditorGridBase.actAnchoAutoUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := HayDatos;
end;

procedure TfEditorGridBase.actDuplicarUpdate(Sender: TObject);
begin
  inherited;
  if HayDatos and Assigned(ViewGrid) then
    (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes)
      and not ViewGrid.IsEmpty
  else
    (Sender as TAction).Enabled := False;
end;

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

procedure TfEditorGridBase.PrevisualizarInterno;
begin
  inherited;
  (ViewGrid as IViewGrid).Preview;
end;

procedure TfEditorGridBase.RefrescarInterno;
var
  FocusedRow, TopRow : Integer;
begin
  TopRow := ViewGrid._FocusedView.Controller.TopRowIndex;
  FocusedRow := ViewGrid._FocusedView.DataController.FocusedRowIndex;
  ViewGrid._FocusedView.BeginUpdate;
  try
    inherited;
  finally
    ViewGrid._FocusedView.EndUpdate;
    ViewGrid._FocusedView.DataController.FocusedRowIndex := FocusedRow;
    ViewGrid._FocusedView.Controller.TopRowIndex := TopRow;
  end;
end;

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

initialization
  RegisterClass(TfEditorGridBase);

finalization
  UnRegisterClass(TfEditorGridBase);

end.

