{ ------------------------------------------------------------- }
{ Purpose : NextLayout 6                                        }
{ By      : Bojan Nikolic                                       }
{ For     : BergSoft                                            }
{ ------------------------------------------------------------- }
{ Copyright (C) BergSoft.net 2018. All Rights Reserved.         }
{ ------------------------------------------------------------- }
{$R 'Nodes.res'}
unit NxNodesEditor6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms, Dialogs, ImgList,
  ComCtrls, StdCtrls, ExtCtrls,
  { Design }
  DesignIntf,
  { Next Standard 6 (Design) }
  NxDesignWindow6, NxStdCtrls6, NxAssociates6, NxControls6,
  { Next Layout 6 }
  NxLayout6, NxLayoutNodes6;

type
  TNodesEditor = class(TNxDesignWindow)
    Palette: TNxToolPalette6;
    Icons: TImageList;
    procedure PaletteButtonClick(Sender: TObject; Index: Integer);
    procedure FormCreate(Sender: TObject);
  private
    function GetLayoutControl: TNextLayout6;
    { Private declarations }
  protected
    procedure AddButton(Palette: string; ComponentClass: TComponentClass;
      ImageIndex: Integer); virtual;
    procedure AddButtons; virtual;
    procedure AddIcon(Res: string);
    procedure AddIcons; virtual;
    function CreateNode(const NodeClass: TNxLayoutNodeClass): TNxLayoutNode6;
  public
    constructor Create(AOwner: TComponent); override;
    property LayoutControl: TNextLayout6 read GetLayoutControl;
  end;

var
  NodesEditor: TNodesEditor;

implementation

{$R *.dfm}

{ TForm1 }

procedure TNodesEditor.AddButton(Palette: string; ComponentClass: TComponentClass; ImageIndex: Integer);
begin
  with Self.Palette.Add(Palette, ComponentClass, ImageIndex) do
  begin
    ShowHint := True;
    Hint := ComponentClass.ClassName;
  end;
end;

procedure TNodesEditor.AddButtons;
begin
  AddButton('Standard', TNxLabelLayoutNode6, 0);
  AddButton('Standard', TNxEditLayoutNode6, 1);
  AddButton('Standard', TNxSpinLayoutNode6, 2);
  AddButton('Standard', TNxCheckBoxLayoutNode6, 3);
  AddButton('Standard', TNxButtonLayoutNode6, 4);

  AddButton('Additional', TNxSeparatorLayoutNode6, 5);
  AddButton('Additional', TNxToolbarLayoutNode6, 6);
  AddButton('Additional', TNxTrackBarLayoutNode6, 7);
end;

procedure TNodesEditor.AddIcon(Res: string);
var
  Bitmap: TBitmap;
begin
  Bitmap := TBitmap.Create;

  try
    Bitmap.LoadFromResourceName(HInstance, Res);

    { Add to list }
    Icons.AddMasked(Bitmap, Bitmap.Canvas.Pixels[0, Bitmap.Height - 1]);
  finally

  end;
end;

procedure TNodesEditor.AddIcons;
begin
  AddIcon('LABELNODE');
  AddIcon('EDITNODE');
  AddIcon('SPINNODE');
  AddIcon('CHECKBOXNODE');
  AddIcon('BUTTONNODE');

  AddIcon('SEPARATORNODE');
  AddIcon('TOOLBARNODE');
  AddIcon('TRACKBARNODE');
end;

constructor TNodesEditor.Create(AOwner: TComponent);
begin
  inherited;
  AddIcons;
  AddButtons;
end;

function TNodesEditor.CreateNode(const NodeClass: TNxLayoutNodeClass): TNxLayoutNode6;
var
  NewName: TComponentName;
  ParentNode: TNxLayoutNode6;
  ParentNodeList: TNxLayoutNodeList;
  Selected: TPersistent;
  Selections: IDesignerSelections;
begin
  Selected := nil;
  ParentNodeList := nil;

  { Create Instance }
  Result := NodeClass.Create(LayoutControl.Owner);

  { Set Unique Name to Node }
  NewName := Designer.UniqueName(Result.ClassName);

  Result.Name := NewName;
  Result.Width := 72;

  { Selections }
  Selections := TDesignerSelections.Create;
  Designer.GetSelections(Selections);

  if Assigned(Selections) then
  begin

    if Selections.Count = 1 then
    begin
      Selected := Selections.Items[0];
    end else Exit;

  end;

  if Selected is TNextLayout6 then
  begin
    ParentNodeList := TNextLayout6(Selected).Nodes;
  end else if Selected is TNxLayoutNode6 then
  begin
    ParentNode := TNxLayoutNode6(Selected);
    ParentNodeList := ParentNode.ChildNodes;
    if ParentNode.IsTopLevel then
    begin
      Result.Padding.Left := 48;
    end;
  end;

  if Assigned(ParentNodeList) then
  begin
    ParentNodeList.Add(Result);

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

procedure TNodesEditor.FormCreate(Sender: TObject);
begin
  ClientHeight := Palette.Height;
end;

function TNodesEditor.GetLayoutControl: TNextLayout6;
begin
  Result := TNextLayout6(OwnerControl);
end;

procedure TNodesEditor.PaletteButtonClick(Sender: TObject; Index: Integer);
var
  ComponentClass: TNxLayoutNodeClass;
begin
  ComponentClass := TNxLayoutNodeClass(Palette.Palette[Palette.ItemIndex].Button[Index].ReferenceType);
  CreateNode(ComponentClass);
end;

end.
