unit uViewControlesBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxGraphics, cxCustomData, cxStyles, cxTL,
  cxImageComboBox, cxMaskEdit, cxTextEdit, cxInplaceContainer, cxDBTL,
  cxControls, cxTLData, Menus, uBizControlesBase, DB, uDADataTable,
  ImgList, PngImageList, EasyLVCommonObjects, EasyListview,
  EasyLVCommonUtilities;

type
  IViewControlesBase = interface(IViewBase)
  ['{D991FCEC-08DB-4F77-BD57-FA04B3C88788}']
    function GetDblClick: TNotifyEvent;
    procedure SetDblClick(const Value: TNotifyEvent);
    property OnDblClick: TNotifyEvent read GetDblClick write SetDblClick;

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

    function GetControl: IBizControlBase;
    procedure SetControl(const Value: IBizControlBase);
    property Control: IBizControlBase read GetControl write SetControl;

    procedure Refresh;
  end;

  TfrViewControlesBase = class(TfrViewBase, IViewControlesBase)
    ReportImages: TPngImageList;
    dsDataTable: TDADataSource;
    EasyListview: TEasyListview;
    procedure EasyListviewItemDblClick(Sender: TCustomEasyListview;
      Button: TEasyMouseButton; MousePos: TPoint;
      HitInfo: TEasyHitInfoItem);
    procedure EasyListviewItemContextMenu(Sender: TCustomEasyListview;
      HitInfo: TEasyHitInfoItem; WindowPoint: TPoint; var Menu: TPopupMenu;
      var Handled: Boolean);
    procedure EasyListviewItemSelectionChanged(Sender: TCustomEasyListview;
      Item: TEasyItem);
  protected
    FControl : IBizControlBase;
    FOnDblClick: TNotifyEvent;
    FPopupMenu: TPopupMenu;
    
    function GetDblClick: TNotifyEvent; virtual;
    procedure SetDblClick(const Value: TNotifyEvent); virtual;

    function GetPopupMenu: TPopupMenu; virtual;
    procedure SetPopupMenu(const Value: TPopupMenu); virtual;

    function GetControl: IBizControlBase; virtual;
    procedure SetControl(const Value: IBizControlBase); virtual;
  public
    procedure Refresh; override;  
    property Control: IBizControlBase read GetControl write SetControl;
    property OnDblClick: TNotifyEvent read GetDblClick write SetDblClick;
    property PopupMenu: TPopupMenu read GetPopupMenu write SetPopupMenu;
  end;

implementation

{$R *.dfm}

{ TfrViewControlesBase }

function TfrViewControlesBase.GetControl: IBizControlBase;
begin
  Result := FControl;
end;

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

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

procedure TfrViewControlesBase.Refresh;
var
  GroupNoModificable: TEasyGroup;
  GroupModificable: TEasyGroup;
  AItem : TEasyItem;
  i: Integer;
  IDSelected : string;
begin
  inherited;

  if not Assigned(FControl) then
    Exit;

  EasyListview.BeginUpdate(False);
  try
    IDSelected := '';
    if EasyListview.Groups.Count > 0 then
    begin
      for i := 0 to EasyListview.Items.Count - 1 do
        if EasyListview.Items[i].Selected then
          IDSelected := EasyListview.Items[i].Captions[3];   // Columna ID
      EasyListview.Groups.Clear;
    end;

    GroupNoModificable := EasyListview.Groups.Add;
    GroupNoModificable.Caption := 'Controles generales';
    GroupNoModificable.Bold := True;

    GroupModificable := EasyListview.Groups.Add;
    GroupModificable.Caption := 'Controles personalizados';
    GroupModificable.Bold := True;

    with FControl  do
    begin
      FControl.First;
      for i := 0 to (FControl.RecordCount - 1) do
      begin
        if EsModificable then
          AItem := GroupModificable.Items.Add(NIL)
        else
          AItem := GroupNoModificable.Items.Add(NIL);

        AItem.ImageIndex := ICONO;
        AItem.Captions[0] := NOMBRE;
        AItem.Captions[1] := DESCRIPCION;
        AItem.Captions[3] := IntToStr(ID);
        AItem.Captions[4] := MODIFICABLE;
        AItem.DetailCount := 2;
        AItem.Details[0] := 0;  // Columna NOMBRE;
        AItem.Details[1] := 1;  // Columna DESCRIPCION;

        AItem.Selected := (IDSelected = AItem.Captions[3]);
        FControl.Next;
      end;
    end;
  finally
    EasyListview.Sort.SortAll;
    EasyListview.EndUpdate;

    if (Length(IDSelected) = 0) and (EasyListview.Items.Count > 0) then
      EasyListview.Items[0].Selected := True;

    for i := 0 to EasyListview.Items.Count - 1 do
      if EasyListview.Items[i].Selected then
      begin
        FControl.First;
        FControl.Locate('ID', StrToInt(EasyListview.Items[i].Captions[3]), []);
        Break;
      end;

    EasyListview.ShowGroupMargins := True;
  end
end;

procedure TfrViewControlesBase.SetControl(const Value: IBizControlBase);
begin
  FControl := Value;
  dsDataTable.DataTable := Value.DataTable;
end;

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

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

procedure TfrViewControlesBase.EasyListviewItemDblClick(
  Sender: TCustomEasyListview; Button: TEasyMouseButton; MousePos: TPoint;
  HitInfo: TEasyHitInfoItem);
begin
  inherited;
  if Assigned(FOnDblClick) then
    FOnDblClick(Self);
end;

procedure TfrViewControlesBase.EasyListviewItemContextMenu(
  Sender: TCustomEasyListview; HitInfo: TEasyHitInfoItem;
  WindowPoint: TPoint; var Menu: TPopupMenu; var Handled: Boolean);
begin
  inherited;
  if Assigned(FPopupMenu) then
    Menu := FPopupMenu;
end;

procedure TfrViewControlesBase.EasyListviewItemSelectionChanged(
  Sender: TCustomEasyListview; Item: TEasyItem);
begin
  inherited;
  if not Item.Destroying then
  begin
    FControl.First;
    FControl.Locate('ID', StrToInt(Item.Captions[3]), []);
  end;
end;

end.

