{*******************************************************}
{                                                       }
{       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;

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

  TfEditorGridBase = class(TfEditorDBBase, IEditorGridBase)
    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;
    procedure tbxEditFiltroChange(Sender: TObject; const Text: String);
    procedure FormShow(Sender: TObject); override;
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actConfPaginaExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actQuitarFiltroExecute(Sender: TObject);
    procedure actDuplicarUpdate(Sender: TObject);
    procedure JvFormStorageSavePlacement(Sender: TObject);
    procedure JvFormStorageRestorePlacement(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
  private
    FViewGrid : IViewGridBase;
  protected
    procedure SetViewGrid(const Value : IViewGridBase); virtual;
    function GetViewGrid: IViewGridBase;
  public
    property ViewGrid: IViewGridBase read GetViewGrid write SetViewGrid;
    constructor Create(AOwner : TComponent); override;
    
    destructor Destroy; override;
  end;

implementation

uses
  uDataModuleBase, uCustomEditor, cxGridTableView;

{$R *.dfm}

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

function TfEditorGridBase.GetViewGrid: IViewGridBase;
begin
  Result := FViewGrid;
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
    ViewGrid.Filter := Text;
end;

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

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

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

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

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

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

procedure TfEditorGridBase.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 TfEditorGridBase.JvFormStorageSavePlacement(Sender: TObject);
begin
  inherited;
  if Assigned(ViewGrid) then
    ViewGrid.StoreToRegistry(JvAppRegistryStorage.Root);
end;

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

procedure TfEditorGridBase.actRefrescarExecute(Sender: TObject);
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;

initialization
  RegisterClass(TfEditorGridBase);

finalization
  UnRegisterClass(TfEditorGridBase);

end.

