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

interface

uses
  { Standard }
  Classes, Types, Graphics,
  { Standard.Design }
  DesignIntf, DesignEditors, VCLEditors,
  { Standard.Controls }
  Controls, StdCtrls, Buttons, ImgList,
  { Next Standard 6 }
  NxTypes6, NxConsts6, NxAssociates6, NxClasses6,
  { Next Standard 6 (Design) }
  NxStandardReg6, NxDesignWindow6,
  { NextGrid 6 }
  NxCustomGrid6, NxGridView6, NxControls6, NxCells6,
  { NextLayout 6 }
  NxLayoutIntf6, NxLayoutNodes6, NxLayout6, NxColumns6, NxGrid6, NxStdCtrls6, NxVirtualGrid6;

const
  strActivateView = 'Do you want to set this view as ActiveView?';

type
  { TNxGridComponentEditor6 }

  TViewsEditor = class(TNxDesignWindow)
    frProperties: TNextLayout6;
    grEditor: TNextGrid6;
    NxReportGridView61: TNxReportGridView6;
    NxTextColumn61: TNxTextColumn6;
    NxTextColumn62: TNxTextColumn6;
    NxToolbar61: TNxToolbar6;
    ImageList1: TImageList;
    NxTextColumn63: TNxIconColumn6;
    ImageList2: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure grEditorSelect(Sender: TObject; ACol, ARow: Integer);
    procedure NxToolbar61ButtonClick(Sender: TObject; const Index: Integer);
    procedure NxTextColumn63ButtonClick(Sender: TObject);
    procedure NxTextColumn63DrawContent(Sender: TObject; ACol, ARow: Integer;
      CellRect: TRect; State: TNxCellPaintingState);
  private
    function GetGrid: TNxCustomGrid6;
    function GetSelectedInstance: TObject;
  protected
    procedure Add(View: TNxGridView6);
    procedure AddRow(View: TNxGridView6);
    procedure RebuildList;
    procedure ReadProperties;
  public
    property Grid: TNxCustomGrid6 read GetGrid;
    property SelectedInstance: TObject read GetSelectedInstance;
  end;

implementation

uses
  { Standard }
  SysUtils, Dialogs;

{$R *.dfm}

function TViewsEditor.GetGrid: TNxCustomGrid6;
begin
  Result := TNxCustomGrid6(OwnerControl);
end;

function TViewsEditor.GetSelectedInstance: TObject;
begin
  if grEditor.IsSelectionValid then
  begin
    Result := Grid.Views[grEditor.SelectedRow];
  end
  else Result := nil;
end;

procedure TViewsEditor.grEditorSelect(Sender: TObject; ACol, ARow: Integer);
begin
  Designer.SelectComponent(TPersistent(SelectedInstance));

  { Update controler }
  if Assigned(SelectedInstance)
    then ReadProperties;
end;

procedure TViewsEditor.NxTextColumn63ButtonClick(Sender: TObject);
begin
  { No longer active }
  grEditor.RefreshCell(0, Grid.ActiveViewIndex);

  Grid.ActiveViewIndex := grEditor.SelectedRow;
end;

procedure TViewsEditor.NxTextColumn63DrawContent(Sender: TObject; ACol,
  ARow: Integer; CellRect: TRect; State: TNxCellPaintingState);
var
  Location: TPoint;
begin
  with grEditor.Canvas do
  begin

    if Assigned(Grid.ActiveView) and
      (Grid.ActiveView.Index = ARow) then
    begin

      Location := CenterInRect(CellRect, 7, 7);

      with Location do
      begin
        Pen.Color := clWindowText;
        Polyline([Point(X, Y + 1), Point(X + 2, Y + 3), Point(X + 7, Y - 2)]);
        Polyline([Point(X, Y + 2), Point(X + 2, Y + 4), Point(X + 7, Y - 1)]);
        Polyline([Point(X, Y + 3), Point(X + 2, Y + 5), Point(X + 7, Y)]);
      end;

    end;

  end;

end;

procedure TViewsEditor.NxToolbar61ButtonClick(Sender: TObject;
  const Index: Integer);
var
  ReportGridView: TNxReportGridView6;
  SlideGridView: TNxSlidesGridView6;
begin
  case Index of
    0:
    begin
      ReportGridView := TNxReportGridView6.Create(Grid.Owner);

      { Add & set Name }
      Add(ReportGridView);
    end;

    1:
    begin
      SlideGridView := TNxSlidesGridView6.Create(Grid.Owner);

      { Add & set Name }
      Add(SlideGridView);
    end;

    3:
    begin
      Designer.NoSelection;

      Grid.Views[grEditor.SelectedRow].Free;

      grEditor.DeleteSelected;

      { Notify Delphi }
      Designer.Modified;
    end;

    4:
    begin
      Designer.NoSelection;

      Grid.Views.Clear;

      grEditor.ClearRows;

      { Notify Delphi }
      Designer.Modified;
    end;
  end;
end;

procedure TViewsEditor.ReadProperties;
begin
  { Assign previous editing }
  frProperties.EndEditing;

  { Erase first }
  frProperties.Nodes.Clear;

  frProperties.Associates.Clear;

  frProperties.Associates.Add(TPersistent(SelectedInstance));

  { Add root }
  with frProperties.Nodes.Add(TNxLabelLayoutNode6) do
  begin
    TextBefore := 'Properties';
    Padding.Left := 16;
    Padding.Top := 4;

    with ChildNodes.Add(TNxLabelLayoutNode6) do
    begin
      TextBefore := 'Common';
      Width := 80;
      Padding.Bottom := 8;
      ExpandLock := True;

      with ChildNodes.Add(TNxSpinLayoutNode6) as TNxSpinLayoutNode6 do
      begin
        BindProperty := 'HeaderHeight';
        TextBefore := 'HeaderHeight';
        TextBeforeWidth := 80;
        Min := 0;
        Max := MaxInt;
        ClientWidth := 50;
        Clearing := [clRight];
        Precision := 0;
      end;

      with ChildNodes.Add(TNxCheckBoxLayoutNode6) do
      begin
        BindProperty := 'GridLines';
        TextBefore := 'GridLines';
        TextBeforeWidth := 80;
        ClientWidth := 60;
        Clearing := [clRight];
      end;
    end;

    with ChildNodes.Add(TNxLabelLayoutNode6) do
    begin
      TextBefore := 'Columns';
      Width := 80;
      Padding.Bottom := 8;
      ExpandLock := True;

      with ChildNodes.Add(TNxCheckBoxLayoutNode6) as TNxCheckBoxLayoutNode6 do
      begin
        Button := True;
        ButtonImageIndex := 0;
        BindProperty := 'ColumnMoving';
        Hint := 'ColumnMoving';
        Width := 22;
        Clearing := [clRight];
      end;

    end;

    with ChildNodes.Add(TNxLabelLayoutNode6) do
    begin
      TextBefore := 'Rows';
      Width := 80;
      Padding.Bottom := 8;
      ExpandLock := True;

      with ChildNodes.Add(TNxCheckBoxLayoutNode6) as TNxCheckBoxLayoutNode6 do
      begin
        Button := True;
        ButtonImageIndex := 1;
        BindProperty := 'RowMoving';
        Hint := 'RowMoving';
        Width := 22;
        Clearing := [];
        Margins.Right := 0;
      end;

      with ChildNodes.Add(TNxCheckBoxLayoutNode6) as TNxCheckBoxLayoutNode6 do
      begin
        Button := True;
        ButtonImageIndex := 2;
        BindProperty := 'RowResizing';
        Hint := 'RowResizing';
        Margins.Left := 0;
        Width := 22;
        Clearing := [clRight];
      end;
    end;
  end;

  frProperties.Invalidate;
  frProperties.ReadProperties(nil);
end;

procedure TViewsEditor.RebuildList;
var
  i: Integer;
begin
  grEditor.ClearRows;

  { Add Views }
  for i := 0 to Pred(Grid.Views.Count) do AddRow(Grid.Views[i]);
end;

procedure TViewsEditor.Add(View: TNxGridView6);
var
  NewName: TComponentName;
begin
  { Set Unique Name to View }
  NewName := Designer.UniqueName(View.ClassName);

  View.Name := NewName;

  { Add Object into List }
  Grid.Views.Add(View);

  AddRow(View);

  grEditor.SelectedRow := grEditor.LastAddedRow;

  { Set as default }
  if Grid.ActiveView = nil then
    if MessageDlg(strActivateView, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    begin
      Grid.ActiveView := View;
      grEditor.RefreshCell(NxTextColumn63.Index, grEditor.LastAddedRow);
    end;

  Grid.Invalidate;

  { Enable Save Btn. }
  Designer.Modified;
end;

procedure TViewsEditor.AddRow(View: TNxGridView6);
begin
  with grEditor.AddRow.Cells do
  begin
    Item[1].AsString := View.Name;
    Item[2].AsString := View.ClassName;
  end;
end;

procedure TViewsEditor.FormCreate(Sender: TObject);
begin
  grEditor.SelectionColor := ColorOf(seHighlight);
  grEditor.SelectionTextColor := clWindowText;
  grEditor.ClearRows;
end;

procedure TViewsEditor.FormShow(Sender: TObject);
begin
  RebuildList;
end;

end.
