{ ------------------------------------------------------------- }
{ Purpose : Next Grid 6                                         }
{ By      : Bojan Nikolic                                       }
{ For     : BergSoft                                            }
{ ------------------------------------------------------------- }
{ Copyright (C) BergSoft.net 2012. All Rights Reserved.         }
{ ------------------------------------------------------------- }
unit NxGridReg6;

interface

uses
  { Standard }
  Classes, Types,
  { Standard.Design }
  DesignIntf, DesignEditors, VCLEditors,
  { Next Standard 6 }
  NxIntf6, NxConsts6, NxTypes6,
  { Next Standard 6 (Design) }
  NxStandardReg6, NxDesignWindow6,
  { NextGrid 6 }
  NxVirtualGrid6, NxGrid6, NxColumns6, NxGridView6,
  { NextGrid 6 (Design) }
  NxColumnsEditor6;

const
  Visibility: array[Boolean] of string = ('Show', 'Hide');
  State: array[Boolean] of string = ('Enable', 'Disable');

type
  { TNxGridComponentEditor6 }

  TNxGridComponentEditor6 = class(TNextSuiteComponentEditor6,
    INxDesignNotificationRecipient)
  private
    function GetSelectedView: TNxGridView6;
    procedure OpenEditor;
    procedure OpenSlidesEditor;
  protected
    function CreateReportView: TNxGridView6;
    procedure DesignNotification(Sender: INxDesignNotificationRecipient; Operation: TNxDesignOperation); virtual;
  public
    constructor Create(AComponent: TComponent; ADesigner: IDesigner); override;
    destructor Destroy; override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
    procedure Edit; override;
    procedure EditProperty(const Prop: IProperty; var Continue: Boolean); override;
    procedure ExecuteVerb (Index: Integer); override;
    property SelectedView: TNxGridView6 read GetSelectedView;
  end;

  { TNxViewsProperty6 }

  TNxViewsProperty6 = class(TPropertyEditor)
  private
    procedure OpenEditor;
  protected

  public
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
    procedure Edit; override;
  end;

  { TNxColumnsProperty6 }

  TNxColumnsProperty6 = class(TPropertyEditor)
  private
    procedure OpenEditor;
  protected
    function GetEditorClass: TNxEditorClass; virtual;
  public
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
    procedure Edit; override;
  end;

  { TNxInsertValueProperty6 }

  TNxInsertValueProperty6 = class(TPropertyEditor)
  public
		function GetValue: string; override;
		procedure SetValue(const Value: string); override;
  end;

procedure Register;

implementation

uses
  { Standard }
  Dialogs, Forms, SysUtils,
  { Next Standard 6 }
  NxStylePaint6,
  { NextGrid 6 }
  NxCustomGrid6, NxGridCtrls6,
  { NextGrid 6.Design }
  NxViewsEditor6, NxSlidesDesigner6, NxSlidesEditor6;

procedure Register;
begin
  { NextGrid 6 }
  RegisterComponents('Next Suite 6', [TNextGrid6, TNextVirtualGrid6,
    TNxSlidesDesigner6,
    TNxVirtualCellSource6, TNxGridHTMLExport6]);

  { Component Editors }
  RegisterComponentEditor(TNxCustomGrid6, TNxGridComponentEditor6);

  { Property Editors }
  RegisterPropertyEditor(TypeInfo(TNxColumns6), TNxCustomGrid6, 'Columns', TNxColumnsProperty6);
  RegisterPropertyEditor(TypeInfo(TNxGridViews6), TNxCustomGrid6, 'Views', TNxViewsProperty6);
  RegisterPropertyEditor(TypeInfo(INxBaseCell), TNxColumn6, 'InsertValue', TNxInsertValueProperty6);

//  RegisterPropertyEditor(TypeInfo(INxStylePaint), TNxCustomGrid6, 'StylePaint', TNxStylePaintProperty);

  { Multi-line }
  RegisterPropertyEditor(TypeInfo(WideString), TNxColumnPart, 'Caption', TNxMultilinePropertyEditor6);

  { Registering View Classes }
  RegisterClasses([TNxReportGridView6, TNxSlidesGridView6]);
  RegisterNoIcon([TNxReportGridView6, TNxSlidesGridView6]);

  { Required }
  RegisterClasses([
    TNxButtonColumn6,
    TNxCheckBoxColumn6,
    TNxColorColumn6,
    TNxComboBoxColumn6,
    TNxCheckComboBoxColumn6,
    TNxDateColumn6,
    TNxTimeColumn6,
    TNxGraphicColumn6,
    TNxHTMLColumn6,
    TNxIconColumn6,
    TNxIncrementColumn6,
    TNxListColumn6,
    TNxMemoColumn6,
    TNxNumberColumn6,
    TNxProgressColumn6,
    TNxTextColumn6,
    TNxToolbarColumn6,
    TNxTrackBarColumn6,
    TNxRateColumn6,
    TNxTreeColumn6,
    TNxChartColumn6
  ]);

  RegisterNoIcon([
    TNxButtonColumn6,
    TNxCheckBoxColumn6,
    TNxColorColumn6,
    TNxComboBoxColumn6,
    TNxCheckComboBoxColumn6,
    TNxDateColumn6,
    TNxTimeColumn6,
    TNxGraphicColumn6,
    TNxHTMLColumn6,
    TNxIconColumn6,
    TNxIncrementColumn6,
    TNxListColumn6,
    TNxMemoColumn6,
    TNxNumberColumn6,
    TNxProgressColumn6,
    TNxTextColumn6,
    TNxToolbarColumn6,
    TNxTrackBarColumn6,
    TNxRateColumn6,
    TNxTreeColumn6,
    TNxChartColumn6
  ]);
end;

{ TNxGridComponentEditor6 }

constructor TNxGridComponentEditor6.Create(AComponent: TComponent;
  ADesigner: IDesigner);
var
  Control: INxDesignControl;
begin
  inherited;
  if Supports(GetComponent, INxDesignControl, Control) then
  begin
    Control.SetDesignerDelegate(Self as INxDesignNotificationRecipient);
  end;
end;

function TNxGridComponentEditor6.CreateReportView: TNxGridView6;
begin
  Result := TNxReportGridView6.Create(GetComponent.Owner);
  with GetComponent as TNxCustomGrid6 do
  begin
    Views.Add(Result);
  end;
end;

procedure TNxGridComponentEditor6.DesignNotification(
  Sender: INxDesignNotificationRecipient; Operation: TNxDesignOperation);
var
  PersistentRef: INxPersistentDesignResponder;
  EntityRef: INxEntityDesignRespoder;
begin

  if Supports(Sender, INxPersistentDesignResponder, PersistentRef) then
  begin

    if PersistentRef.Instance = GetComponent then
    begin
      // Passtrough
    end
    else Designer.SelectComponent(PersistentRef.Instance);

    { Update painting }
    with INxDesignControl(GetComponent as TNxCustomGrid6) do
    begin
      SelectComponent(PersistentRef.Instance);
    end;
  end;

  { '...' Button }
  if Supports(Sender, INxEntityDesignRespoder, EntityRef) then
  begin
    OpenEditor;
  end;
end;

destructor TNxGridComponentEditor6.Destroy;
begin
  inherited;
end;

procedure TNxGridComponentEditor6.Edit;
begin
  inherited;
end;

procedure TNxGridComponentEditor6.EditProperty(const Prop: IProperty;
  var Continue: Boolean);
begin
  inherited;
  if Prop.GetName = 'Columns' then
  begin
    Continue := False;

    { Call TNxColumnsProperty6.Edit }
    Prop.Edit;
  end;

  if Prop.GetName = 'Views' then
  begin
    Continue := False;

    { Call TNxViewsProperty6.Edit }
    Prop.Edit;
  end;
end;

procedure TNxGridComponentEditor6.ExecuteVerb(Index: Integer);
begin
  with GetComponent as TNxCustomGrid6 do
    case Index of
      0: Edit;
      1: OpenEditor;
      3..11:
      begin
        case Index of
          3:
          begin
            MultiSelect := not MultiSelect;
            SelectFullRow := MultiSelect;
          end;
          4: MultiSort := not MultiSort;
          6..11:
          if Assigned(ActiveView) then
            case Index of
              6: ActiveView.GridLines := not ActiveView.GridLines;
              7: ActiveView.ShowHeader := not ActiveView.ShowHeader;
              8: ActiveView.ShowFooter := not ActiveView.ShowFooter;
              9: ActiveView.ShowInsertRow := not ActiveView.ShowInsertRow;
              10: ActiveView.ShowIndicator := not ActiveView.ShowIndicator;
              11: if ActiveView is TNxSlidesGridView6 then OpenSlidesEditor;
            end;
        end;

        Designer.Modified; { Update IDE }
      end;

    end;

  { TNextSuiteComponentEditor6 }
  inherited ExecuteVerb(Index);
end;

function TNxGridComponentEditor6.GetSelectedView: TNxGridView6;
var
  Selections: IDesignerSelections;
begin
  { Default }
  Result := nil;

  Selections := CreateSelectionList;
  Designer.GetSelections(Selections);

  if Selections.Count > 0 then
  begin
    Result := TNxGridView6(Selections[0]);
  end;

  Selections := nil;
end;

function TNxGridComponentEditor6.GetVerb(Index: Integer): string;
begin
  with GetComponent as TNxCustomGrid6 do
    case Index of
      0: Result := '&Columns Editor...';
      1: Result := '&Views Editor...';
      2: Result := '-';
      3: Result := State[SelectFullRow and MultiSelect] + ' &Multiselect';
      4: Result := State[MultiSort] + ' &Multisort';

      { ActiveView Prop. Set }
      5..11:
        if Assigned(ActiveView) then
          case Index of
            5: Result := '-';
            6: Result := Visibility[ActiveView.GridLines] + ' &Grid';
            7: Result := Visibility[ActiveView.ShowHeader] + ' &Header';
            8: Result := Visibility[ActiveView.ShowFooter] + ' &Footer';
            9: Result := Visibility[ActiveView.ShowInsertRow] + ' Insert &Row';
            10: Result := Visibility[ActiveView.ShowIndicator] + ' &Indicator';
            11: if ActiveView is TNxSlidesGridView6 then Result := 'Open Slides Editor...';
          end;

    end;

  { TNextSuiteComponentEditor6 }
  Result := inherited GetVerb(Index);
end;

function TNxGridComponentEditor6.GetVerbCount: Integer;
begin
  { ActiveView excluded }
  Result := inherited GetVerbCount + 5;

  { ActiveView is Set }
  with GetComponent as TNxCustomGrid6 do
  begin
    if Assigned(ActiveView) then
    begin
      Inc(Result, 6);
      { `Open Slides Editor...` }
      if ActiveView is TNxSlidesGridView6 then Inc(Result);
    end;
  end;
end;

procedure TNxGridComponentEditor6.OpenEditor;
var
  EditorForm: TViewsEditor;
begin
  inherited;

  if not ShowForm(GetComponent, TViewsEditor) then { Showing }
  begin
    EditorForm := TViewsEditor.Create(Application);

    try
      EditorForm.OwnerControl := GetComponent;
      EditorForm.Designer := Designer;
      EditorForm.WindowState := wsNormal;
      EditorForm.Show;
    except
      EditorForm.Free;
    end;
  end;

end;

procedure TNxGridComponentEditor6.OpenSlidesEditor;
var
  EditorWindow: TNxSlidesEditorWindow;
begin
  if not ShowForm(GetComponent, TNxSlidesEditorWindow) then { Showing }
  begin
    EditorWindow := TNxSlidesEditorWindow.Create(Application);

    try
      EditorWindow.OwnerControl := GetComponent;
      EditorWindow.View := TNxSlidesGridView6(TNxCustomGrid6(GetComponent).ActiveView);
      EditorWindow.Designer := Designer;
      EditorWindow.WindowState := wsNormal;
      EditorWindow.Show;
    except
      EditorWindow.Free;
    end;
  end;

end;

{ TNxViewsProperty6 }

procedure TNxViewsProperty6.Edit;
begin
  inherited;
  OpenEditor;
end;

function TNxViewsProperty6.GetAttributes: TPropertyAttributes;
begin
  Result := inherited GetAttributes + [paDialog, paReadOnly];
end;

function TNxViewsProperty6.GetValue: string;
begin
  Result := '(' + TNxCustomGrid6(GetComponent(0)).Views.ClassName +  ')';
end;

procedure TNxViewsProperty6.OpenEditor;
var
  EditorForm: TViewsEditor;
begin
  inherited;

  if not ShowForm(GetComponent(0), TViewsEditor) then { Showing }
  begin
    EditorForm := TViewsEditor.Create(Application);

    try
      EditorForm.OwnerControl := GetComponent(0);
      EditorForm.Designer := Designer;
      EditorForm.WindowState := wsNormal;
      EditorForm.Show;
    except
      EditorForm.Free;
    end;
  end;

end;

{ TNxColumnsProperty6 }

procedure TNxColumnsProperty6.Edit;
begin
  inherited;
  OpenEditor;
end;

function TNxColumnsProperty6.GetAttributes: TPropertyAttributes;
begin
  Result := inherited GetAttributes + [paDialog, paReadOnly];
end;

function TNxColumnsProperty6.GetEditorClass: TNxEditorClass;
begin
  Result := TNxColumnsEditor6;
end;

function TNxColumnsProperty6.GetValue: string;
begin
  Result := '(' + TNxCustomGrid6(GetComponent(0)).Columns.ClassName +  ')';
end;

procedure TNxColumnsProperty6.OpenEditor;
var
  EditorForm: TNxColumnsEditor6;
begin
  inherited;

  if not ShowForm(GetComponent(0), TNxColumnsEditor6) then { Showing }
  begin
    EditorForm := GetEditorClass.Create(Application);

    try
      EditorForm.OwnerControl := GetComponent(0);
      EditorForm.Designer := Designer;
      EditorForm.WindowState := wsNormal;
      EditorForm.Show;
    except
      EditorForm.Free;
    end;
  end;
end;

{ TNxInsertValueProperty6 }

function TNxInsertValueProperty6.GetValue: string;
begin
  Result := TNxColumn6(GetComponent(0)).InsertValue.AsString;
end;

procedure TNxInsertValueProperty6.SetValue(const Value: string);
begin
  TNxColumn6(GetComponent(0)).InsertValue.AsString := Value;
end;

end.
