unit KsSkinReg;

interface

{$I KsSkinVer.Inc}

uses
  Dialogs, Forms, Classes, SysUtils,
  {$IFDEF SE_DELPHI6} DesignIntf, DesignEditors {$ELSE} DsgnIntf {$ENDIF};

procedure Register;

implementation {===============================================================}

uses KsMenuDesigner, KsSkinForms, KsSkinEngine, KsSkinItems, KsSkinMenus,
  KsSkinButtons, KsSkinCheckBoxs, KsSkinProgress, KsSkinTrackBars, KsSkinPanels,
  KsSkinGroupBoxs, KsSkinScrollBars, KsSkinTabs, KsSkinComboBoxs, KsSkinEdits,
  KsSkinHints, KsSkinMessages, KsSkinListBoxs, KsSkinSpeedButtons,
  KsSkinSpinButtons, KsSkinConvert, KsConvertForm, KsSkinGrids, KsSkinControlBars,
  KsSkinToolBars, KsSpeedButtons, Controls, KsSkinSource;

{$R *.DCR}

type

{ TSeSkinStoreProperty }

  TSeSkinStoreProperty = class(TPropertyEditor)
  private
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
  end;

procedure TSeSkinStoreProperty.Edit;
var
  OpenDialog: TOpenDialog;
begin
  { Execute editor }
  OpenDialog := TOpenDialog.Create(Application);
  OpenDialog.Filter := GetDialogFilter;
  try
    if OpenDialog.Execute then
    begin
      TSeSkinEngine(GetComponent(0)).SkinSource.LoadFromFile(OpenDialog.FileName);
      TSeSkinEngine(GetComponent(0)).SkinFile := '';
    end;
    Modified;
  finally
    OpenDialog.Free;
  end;
end;

function TSeSkinStoreProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog];
end;

function TSeSkinStoreProperty.GetValue: string;
begin
  if TSeSkinEngine(GetComponent(0)).SkinSource.Count > 0 then
    Result := '('+TSeSkinEngine(GetComponent(0)).SkinSource.SkinName+')'
  else
    Result := '(Emtpy)'
end;

{ Converter }

type

  TSeCompConverter = class(TDefaultEditor)
  public
    procedure Edit; override;
    procedure ExecuteVerb (Index: Integer); override;
    function GetVerb (Index: Integer): String; override;
    function GetVerbCount: Integer; override;
  end;

procedure TSeCompConverter.Edit;
var
  C: TfrmConvertForm;
  SkinEngine: TSeSkinEngine;
begin
  if Assigned(Component) then
  begin
    C := TfrmConvertForm.Create(Application);
    try
      { Initialization }
      C.ConvertClass := TSeConverter;
      C.ConvertForm := TCustomForm(Component.Owner);
      C.ConvertName := 'SkinEngine';
      C.InitConverter;
      { Set SkinEngine }
      SkinEngine := nil;
      if Component is TSeSkinEngine then
        SkinEngine := TSeSkinEngine(Component);
      if Component is TSeSkinForm then
        SkinEngine := TSeSkinForm(Component).SkinEngine;

      if (SkinEngine <> nil) and (C.Converter <> nil) and (C.Converter is TSeConverter) then
        TSeConverter(C.Converter).SkinEngine := SkinEngine;
      { Show }
      C.ShowModal;
    finally
      { Free }
      C.Free;
    end;
  end;
end;

procedure TSeCompConverter.ExecuteVerb (Index: Integer);
begin
  if Index = 0 then
    Edit;
end;

function TSeCompConverter.GetVerbCount: Integer;
begin
  Result := 1;
end;

function TSeCompConverter.GetVerb(Index: Integer): String;
begin
  if Index = 0 then
    Result := 'Component Converter...'
  else
    Result := '';
end;

type

{ Toolbar editor }

  TKsToolbarEditor = class(TComponentEditor)
  public
    function GetVerbCount: Integer; override;
    function GetVerb( Index: Integer ): string; override;
    procedure ExecuteVerb( Index: Integer ); override;
  end;

function TKsToolbarEditor.GetVerbCount: Integer;
begin
  Result := 3;
end;

function TKsToolbarEditor.GetVerb(Index: Integer): string;
begin
  with Component as TSeSkinToolbar do
  begin
    case Index of
      0: Result := 'Add Button';
      1: Result := 'Add Divider';
      2: Result := 'Add Container';
    end; { case }
  end;
end;

procedure TKsToolbarEditor.ExecuteVerb(Index: Integer);
var
  btnButton: TSeSkinSpeedButton;
  btnDiv: TKsSpeedDivider;
  btnCont: TKsSpeedContainer;
begin
  case Index of
    0: { Speedbutton }
    begin
      btnButton := TSeSkinSpeedButton.Create(Designer.GetRoot);
      btnButton.Name := Designer.UniqueName(btnButton.ClassName);
      btnButton.Parent := TWinControl(Component);
      btnButton.Left := 1000;
      btnButton.Top := 1000;

      Designer.SelectComponent(btnButton);
    end;
    1: { Divider }
    begin
      btnDiv := TKsSpeedDivider.Create(Designer.GetRoot);
      btnDiv.Name := Designer.UniqueName(btnDiv.ClassName);
      btnDiv.Parent := TWinControl(Component);
      btnDiv.Left := 1000;
      btnDiv.Top := 1000;

      Designer.SelectComponent(btnDiv);
    end;
    2: { Container }
    begin
      btnCont := TKsSpeedContainer.Create(Designer.GetRoot);
      btnCont.Name := Designer.UniqueName(btnCont.ClassName);
      btnCont.Parent := TWinControl(Component);
      btnCont.Left := 1000;
      btnCont.Top := 1000;

      Designer.SelectComponent(btnCont);
    end;
  end;

  if Component is TSeSkinToolbar then
    TSeSkinToolbar(Component).Realign;

  Designer.Modified;
end;

procedure Register;
begin
  RegisterComponents('SkinEngine', [TSeSkinEngine, TSeSkinForm,  
    TSeSkinMenuBar, TSeSkinPopupMenu, TSeSkinButton, TSeSkinCheckBox,
    TSeSkinRadioButton, TSeSkinProgressBar, TSeSkinTrackBar, TSeSkinPanel,
    TSeSkinGroupBox, TSeSkinScrollBar, TSeSkinTabControl, TSeSkinComboBox,
    TSeSkinEdit, TSeSkinListBox, TSeSkinSpeedButton, TSeSkinSpinButton,
    TSeSkinHint, TSeSkinMessage, TSeSkinRadioGroup, TSeSkinControlBar,
    TSeSkinToolBar, TSeSkinDrawGrid, TSeSkinStringGrid]);

  RegisterNoIcon([TSeSkinItem]);

  { Converter }
  RegisterComponentEditor(TSeSkinForm, TSeCompConverter);
  RegisterComponentEditor(TSeSkinEngine, TSeCompConverter);
  { Menus Editors }
  RegisterComponentEditor(TSeSkinPopupMenu, TKsPopupMenuEditor);
  RegisterComponentEditor(TSeSkinMenuBar, TKsMenuBarEditor);
  { Toolbar Editors }
  RegisterComponentEditor(TSeSkinToolbar, TKsToolbarEditor);

  RegisterPropertyEditor(TypeInfo(TSeSkinItem), nil, '', TKsItemsPropertyEditor);
  RegisterPropertyEditor(TypeInfo(String), TSeSkinEngine, 'SkinStore', TSeSkinStoreProperty);
end;

end.

