unit NxSlidesEditor6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, Menus, ComCtrls,
  { Next Standard 6 }
  NxControls6,
  { Next Standard 6 (Design) }
  NxDesignWindow6,
  { NextGrid 6 }
  NxGridView6, NxSlidesDesigner6, NxStdCtrls6, ImgList;

type
  { TNxSlidesEditorWindow }

  TNxSlidesEditorWindow = class(TNxDesignWindow)
    { Designer }
    SlidesDesigner: TNxSlidesDesigner6;
    StatusBar1: TStatusBar;
    Toolbar: TNxToolbar6;
    PopupMenu1: TPopupMenu;
    InspectColumn1: TMenuItem;
    ImageList1: TImageList;
    Delete1: TMenuItem;
    N1: TMenuItem;
    SetColumn1: TMenuItem;
    UnsetColumn1: TMenuItem;
    Add1: TMenuItem;
    AddAll1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    procedure SlidesDesignerSelectedChange(Sender: TObject);
    procedure ToolbarButtonClick(Sender: TObject; const Index: Integer);
    procedure InspectColumn1Click(Sender: TObject);
    procedure Delete1Click(Sender: TObject);
    procedure UnsetColumn1Click(Sender: TObject);
    procedure Add1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure AddAll1Click(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
  private
    FView: TNxSlidesGridView6;
    { Property Accessors }
    function GetSelected: TNxSlideBound6;
    procedure BuildPopupMenu;
    procedure SetView(const Value: TNxSlidesGridView6);
    procedure SetSelected(const Value: TNxSlideBound6);
  protected
    procedure DoMenuClick(Sender: TObject);
  public
    { Properties }
    property Selected: TNxSlideBound6 read GetSelected write SetSelected;
    property View: TNxSlidesGridView6 read FView write SetView;
  end;

var
  NxSlidesEditorWindow: TNxSlidesEditorWindow;

implementation

uses
  NxColumns6;

{$R *.dfm}

{ TForm1 }

procedure TNxSlidesEditorWindow.Add1Click(Sender: TObject);
begin
  Selected := View.Bounds.Add;
end;

procedure TNxSlidesEditorWindow.AddAll1Click(Sender: TObject);
var
  i, X, Y: Integer;
begin
  { Start }
  X := 0;
  Y := 8;

  { Add Bound for each Col. }
  for i := 0 to Pred(View.Columns.Count) do
  begin

    { Add & Set Bound }
    with View.Bounds.Add do
    begin
      Inc(X, 8);
      if (X + View.Columns[i].Width > View.ClientRect.Right) then
      begin
        X := 8;
        Inc(Y, View.Control.RowHeight + 8);
      end;
      Left := X;
      Top := Y;
      Width := View.Columns[i].Width;
      Height := View.Control.RowHeight;
      Column := View.Columns[i];

      Inc(X, Width);
    end;

  end;

  { Refresh }
  SlidesDesigner.Invalidate;
end;

procedure TNxSlidesEditorWindow.BuildPopupMenu;
var
  i: Integer;
  Item: TMenuItem;
begin
  SetColumn1.Clear;
  for i := 0 to View.Columns.Count - 1 do
  begin
    Item := TMenuItem.Create(Self);
    Item.Caption := View.Columns[i].Name;
    Item.RadioItem := True;
    Item.GroupIndex := 1;
    Item.Checked := Selected.Column = View.Columns[i];
    Item.OnClick := DoMenuClick;
    Item.Tag := i;
    SetColumn1.Add(Item);
  end;
end;

procedure TNxSlidesEditorWindow.Delete1Click(Sender: TObject);
begin
  if Assigned(SlidesDesigner.Selected) then
    if MessageDlg('Are you sure?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
    begin
      SlidesDesigner.Associate.Bounds.Delete(SlidesDesigner.Selected.Index);
      SlidesDesigner.Selected := nil;
    end;
end;

procedure TNxSlidesEditorWindow.DoMenuClick(Sender: TObject);
begin
  if Assigned(SlidesDesigner.Selected) then
  begin
    SlidesDesigner.Selected.Column := View.Columns[TMenuItem(Sender).Tag];
    SlidesDesigner.RefreshSlideBound(Selected);
    BuildPopupMenu;
  end;
end;

procedure TNxSlidesEditorWindow.FormCreate(Sender: TObject);
begin
  SlidesDesigner.DoubleBuffered := True;
  Toolbar.Buttons.EnableGroup(0, False);
  Toolbar.Buttons.EnableGroup(1, False);
end;

function TNxSlidesEditorWindow.GetSelected: TNxSlideBound6;
begin
  Result := SlidesDesigner.Selected;
end;

procedure TNxSlidesEditorWindow.InspectColumn1Click(Sender: TObject);
begin
  Designer.SelectComponent(SlidesDesigner.Selected.Column);
end;

procedure TNxSlidesEditorWindow.ToolbarButtonClick(Sender: TObject; const Index: Integer);
begin
  Designer.NoSelection;
  case Index of
    0: Add1Click(Sender);
    1: Delete1Click(Sender);
    3: AddAll1Click(Sender);
    5: SlidesDesigner.BoundLines := Toolbar.Buttons[Index].Checked;
    6: SlidesDesigner.GridLines := Toolbar.Buttons[Index].Checked;
  end;

  { SlideBound selected }
  if Assigned(SlidesDesigner.Selected) then
  begin

    case Index of
      12: SlidesDesigner.Selected.CaptionLocation := tlLeft;
      13: SlidesDesigner.Selected.CaptionLocation := tlTop;
    end;

    if Assigned(SlidesDesigner.Selected.Column) then
      case Index of
        8: SlidesDesigner.Selected.Column.Alignment := taLeftJustify;
        9: SlidesDesigner.Selected.Column.Alignment := taCenter;
        10: SlidesDesigner.Selected.Column.Alignment := taRightJustify;
      end;
  end;
end;

procedure TNxSlidesEditorWindow.PopupMenu1Popup(Sender: TObject);
begin
  InspectColumn1.Enabled := Assigned(SlidesDesigner.Selected);
  SetColumn1.Enabled := Assigned(SlidesDesigner.Selected);
  UnsetColumn1.Enabled := Assigned(SlidesDesigner.Selected);
end;

procedure TNxSlidesEditorWindow.SetSelected(const Value: TNxSlideBound6);
begin
  SlidesDesigner.Selected := Value;
end;

procedure TNxSlidesEditorWindow.SetView(const Value: TNxSlidesGridView6);
begin
  FView := Value;
  if Assigned(FView) then
  begin
    Caption := 'Slides Editor - ' + FView.Name;
    SlidesDesigner.Associate := FView;
  end;
end;

procedure TNxSlidesEditorWindow.SlidesDesignerSelectedChange(Sender: TObject);
var
  BtnIndex: Integer;
begin
  InspectColumn1.Enabled := Assigned(Selected) and Assigned(Selected.Column);
  if Assigned(Selected) then
  begin
    BuildPopupMenu;
    Designer.SelectComponent(TPersistent(Selected));

    { CaptionLocation }
    Toolbar.Buttons.EnableGroup(1, True);
    case Selected.CaptionLocation of
      tlLeft: BtnIndex := 12;
      else BtnIndex := 13;
    end;
    Toolbar.Buttons[BtnIndex].Checked := True;

    if Assigned(SlidesDesigner.Selected.Column) then
    begin
      Toolbar.Buttons.EnableGroup(0, True);
      StatusBar1.Panels[1].Text := Selected.Column.Name;
      case Selected.Column.Alignment of
        taLeftJustify: BtnIndex := 8;
        taCenter: BtnIndex := 9;
        taRightJustify: BtnIndex := 10;
      end;
      Toolbar.Buttons[BtnIndex].Checked := True;
    end else
    begin
      StatusBar1.Panels[1].Text := '';
      Toolbar.Buttons.EnableGroup(0, False);
    end;
  end else
  begin
    Designer.NoSelection;
    StatusBar1.Panels[1].Text := '';
    Toolbar.Buttons.EnableGroup(0, False);
    Toolbar.Buttons.EnableGroup(1, False);
  end;
end;

procedure TNxSlidesEditorWindow.UnsetColumn1Click(Sender: TObject);
begin
  if Assigned(Selected) then
  begin
    Selected.Column := nil;
    SlidesDesigner.RefreshSlideBound(Selected);
    BuildPopupMenu;
  end;
end;

end.
