{

  Copyright (c) 1997-2003 by CoolDev.Com. Part of CoolControls package.

}

{$I versions.inc}
unit CoolCtrlsReg;

interface

uses
  Windows, Messages, Classes, Controls, Graphics, Dialogs, Forms,
  SysUtils, TypInfo,
  {$IFNDEF DELPHI6_OR_ABOVE}
  DsgnIntf
  {$ELSE}
  DesignIntf,
  DesignEditors,
  VCLEditors
  {$ENDIF}
  ;

type
  {$IFNDEF VER90}
  {$IFDEF DELPHI4_OR_ABOVE}
  {TCoolButtonPanelCompEditor}
  TCoolButtonPanelCompEditor = class(TComponentEditor)
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;
  {$ENDIF}

  {TCoolTrayIconEditor}
  TCoolTrayIconEditor = class(TComponentEditor)
  private
    FTimeToPreview: Cardinal;
  public
    {$IFDEF DELPHI4_OR_ABOVE}
    {$IFDEF DELPHI6_OR_ABOVE}
    constructor Create(AComponent: TComponent; ADesigner: IDesigner); override;
    {$ELSE}
    constructor Create(AComponent: TComponent; ADesigner: IFormDesigner); override;
    {$ENDIF}
    {$ELSE}
    constructor Create(AComponent: TComponent; ADesigner: TFormDesigner); override;
    {$ENDIF}
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;

  {TCopyModeProperty}
  TCopyModeProperty = class(TIntegerProperty)
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
    procedure GetValues(Proc: TGetStrProc); override;
    procedure SetValue(const Value: string); override;
  end;

  {TCoolImagesEditor}
  TCoolImagesEditor = class(TDefaultEditor)
  protected
    {$IFNDEF DELPHI6_OR_ABOVE}
    procedure EditProperty(PropertyEditor: TPropertyEditor;
      var Continue, FreeEditor: Boolean); override;
    {$ELSE}
    procedure EditProperty(const Prop: IProperty; var Continue: Boolean); override;
    {$ENDIF}
  public
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;
  {$ENDIF}

  {TCCAboutProperty}
  TCCAboutProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
    function GetValue: string; override;
    {$IFDEF DELPHI5_OR_ABOVE}
    {$IFNDEF DELPHI6_OR_ABOVE}
    procedure PropDrawName(ACanvas: TCanvas; const ARect: TRect;
      ASelected: Boolean); override;
    {$ENDIF}
    {$ENDIF}
  end;

  {TCaptionPropertyEx}
  TCaptionPropertyEx = class(TCaptionProperty)
    procedure SetValue(const Value: string); override;
  end;

  {$IFDEF PRO}
  {TDBCoolListBoxEditor}
  TDBCoolListBoxEditor = class(TComponentEditor)
  public
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;
  {$ENDIF}

  {TPointListProperty}
  TPointListProperty = class(TClassProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    procedure Edit; override;
  end;

  {TColorListProperty}
  TColorListProperty = class(TClassProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    procedure Edit; override;
  end;

  {TPolygonesProperty}
  TPolygonesProperty = class(TClassProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    procedure Edit; override;
  end;

  {TCoolImageShapeEditor}
  TCoolImageShapeEditor = class(TDefaultEditor)
  protected
    {$IFNDEF DELPHI6_OR_ABOVE}
    procedure EditProperty(PropertyEditor: TPropertyEditor;
      var Continue, FreeEditor: Boolean); override;
    {$ELSE}
    procedure EditProperty(const Prop: IProperty; var Continue: Boolean); override;
    {$ENDIF}
  end;

  {TCoolPageControlEditor}
  TCoolPageControlEditor = class(TComponentEditor)
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;

procedure Register;

implementation

uses
  CoolCtrls,
  CoolTools,
  CoolUtils,
  CoolPolygons,
  CoolLists,
  CoolCtrlsEx,
  PointListUnit,
  ColorListUnit,
  PolygonesUnit,
  CoolAboutUnit,
  CoolScrollBar,
  CoolAppLock,
  CoolWinDock,
  CoolSplitter
  {$IFDEF PRO}, CoolCtrlsDB {$ENDIF};

type
  TPropInfoList = class
  private
    FList: PPropList;
    FCount: Integer;
    FSize: Integer;
    function Get(Index: Integer): PPropInfo;
  public
    constructor Create(Instance: TPersistent; Filter: TTypeKinds);
    destructor Destroy; override;
    function Contains(P: PPropInfo): Boolean;
    function ContainsByName(N: string): Boolean;
    procedure Delete(Index: Integer);
    procedure Intersect(List: TPropInfoList);
    property Count: Integer read FCount;
    property Items[Index: Integer]: PPropInfo read Get; default;
  end;

constructor TPropInfoList.Create(Instance: TPersistent; Filter: TTypeKinds);
begin
  FCount := GetPropList(Instance.ClassInfo, Filter, nil);
  FSize := FCount * SizeOf(Pointer);
  GetMem(FList, FSize);
  GetPropList(Instance.ClassInfo, Filter, FList);
end;

destructor TPropInfoList.Destroy;
begin
  if FList <> nil then FreeMem(FList, FSize);
end;

function TPropInfoList.Contains(P: PPropInfo): Boolean;
var
  I: Integer;
begin
 {$IFNDEF VER90}
  for I := 0 to FCount - 1 do
    with FList^[I]^ do
      if (PropType^ = P^.PropType^) and (CompareText(Name, P^.Name) = 0) then
      begin
        Result := True;
        Exit;
      end;
  {$ENDIF}
  Result := False;
end;

function TPropInfoList.ContainsByName(N: string): Boolean;
var
  I: Integer;
begin
  for I := 0 to FCount - 1 do
    with FList^[I]^ do
      if (CompareText(Name, N) = 0) then
      begin
        Result := True;
        Exit;
      end;
  Result := False;
end;

procedure TPropInfoList.Delete(Index: Integer);
begin
  Dec(FCount);
  if Index < FCount then
    Move(FList^[Index + 1], FList^[Index],
      (FCount - Index) * SizeOf(Pointer));
end;

function TPropInfoList.Get(Index: Integer): PPropInfo;
begin
  Result := FList^[Index];
end;

procedure TPropInfoList.Intersect(List: TPropInfoList);
var
  I: Integer;
begin
  for I := FCount - 1 downto 0 do
    if not List.Contains(FList^[I]) then Delete(I);
end;

{$IFNDEF VER90}
{$IFDEF DELPHI4_OR_ABOVE}
{TCoolButtonPanelCompEditor}
procedure TCoolButtonPanelCompEditor.ExecuteVerb(Index: Integer);
var
  Button: TCoolSpeedButton;
begin
  case Index of
    0: begin
         Button := TCoolButtonPanel(Component).CreateButton;
         Button.Name := Designer.UniqueName(Button.ClassName);
         Designer.Modified;
       end;
    1: TCoolButtonPanel(Component).ReorderButtons;
  end;
end;

function TCoolButtonPanelCompEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := 'A&dd button';
    1: Result := 'Re&order buttons';
  end;
end;

function TCoolButtonPanelCompEditor.GetVerbCount: Integer;
begin
  Result := 2;
end;
{$ENDIF}

{TCoolTrayIconEditor}
{$IFDEF DELPHI4_OR_ABOVE}
{$IFDEF DELPHI6_OR_ABOVE}
constructor TCoolTrayIconEditor.Create(AComponent: TComponent; ADesigner: IDesigner);
{$ELSE}
constructor TCoolTrayIconEditor.Create(AComponent: TComponent; ADesigner: IFormDesigner);
{$ENDIF}
{$ELSE}
constructor TCoolTrayIconEditor.Create(AComponent: TComponent; ADesigner: TFormDesigner); 
{$ENDIF}
begin
  inherited Create(AComponent, ADesigner);
  FTimeToPreview := 5;
end;

procedure TCoolTrayIconEditor.ExecuteVerb(Index: Integer);
var
  I: Integer;
begin
  case Index of
    0: begin
         TCoolTrayIcon(Component).Preview := True;
         for I := Pred(FTimeToPreview) downto 0 do Delay(1000);
         TCoolTrayIcon(Component).Preview := False;
       end;
    1: try
         FTimeToPreview := StrToInt(InputBox('Preview', 'Time to Preview', IntToStr(FTimeToPreview)));
       except
         FTimeToPreview := 5;
       end;
  end;
end;

function TCoolTrayIconEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := '&Preview';
    1: Result := '&Time to preview...';
  end;
end;

function TCoolTrayIconEditor.GetVerbCount: Integer;
begin
  Result := 2;
end;

{TCopyModeProperty}
function TCopyModeProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paMultiSelect, paValueList, paSortList, paRevertable];
end;

function TCopyModeProperty.GetValue: string;
begin
  IntToIdent(GetOrdValue, Result, CopyModes);
end;

procedure TCopyModeProperty.GetValues(Proc: TGetStrProc);
begin
  GetCopyModeValues(Proc);
end;

procedure TCopyModeProperty.SetValue(const Value: string);
var
  NewValue: Longint;
begin
  if IdentToInt(Value, NewValue, CopyModes) then
    SetOrdValue(NewValue)
  else
    raise Exception.Create('''' + Value + ''' is not a valid TCopyMode value');
end;

{TCoolImagesEditor}
{$IFNDEF DELPHI6_OR_ABOVE}
procedure TCoolImagesEditor.EditProperty(PropertyEditor: TPropertyEditor;
  var Continue, FreeEditor: Boolean);
{$ELSE}
procedure TCoolImagesEditor.EditProperty(const Prop: IProperty; var Continue: Boolean);
{$ENDIF}
var
  PropName: string;
begin
  {$IFNDEF DELPHI6_OR_ABOVE}
  PropName := PropertyEditor.GetName;
  {$ELSE}
  PropName := Prop.GetName;
  {$ENDIF}
  if (CompareText(PropName, 'Images') = 0) then
  begin
    {$IFNDEF DELPHI6_OR_ABOVE}
    PropertyEditor.Edit;
    {$ELSE}
    Prop.Edit;
    {$ENDIF}
    Continue := False;
  end;
end;

procedure TCoolImagesEditor.ExecuteVerb(Index: Integer);
begin
  case Index of 
    0: Edit;
  end;
end;

function TCoolImagesEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := '&Images Editor...';
  end;
end;

function TCoolImagesEditor.GetVerbCount: Integer;
begin
  Result := 1
end;
{$ENDIF}

{TCCAboutProperty}
function TCCAboutProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog, paReadOnly {$IFDEF DELPHI5_OR_ABOVE}, paFullWidthName {$ENDIF}]
end {GetAttributes};

procedure TCCAboutProperty.Edit;
begin
  with TFormAbout.Create(Application) do
  try
    LabelVersion.Caption := 'Version 3.04h';
    LabelText.Caption := GetComponent(0).ClassName + ' is one out of more than 70 components ' +
                         'from award winning CoolControls package.';

    ShowModal;
  finally
    Free;
  end;
end;

{$IFDEF DELPHI5_OR_ABOVE}
{$IFNDEF DELPHI6_OR_ABOVE}
procedure TCCAboutProperty.PropDrawName(ACanvas: TCanvas; const ARect: TRect;
      ASelected: Boolean);
begin
  ACanvas.Font.Style := [fsBold];
  inherited PropDrawName(ACanvas, ARect, ASelected);
end;
{$ENDIF}
{$ENDIF}

function TCCAboutProperty.GetValue: string;
begin
  Result := '(About)';
end;

{TCaptionPropertyEx}
procedure TCaptionPropertyEx.SetValue(const Value: string);
var
  S: string;
  I: Integer;
begin
  if Pos('|', Value) = 0 then
    inherited SetValue(Value) else
    begin
      S := Value;
      for I := 0 to Length(S) do
        if S[I] = '|' then
        begin
          Delete(S, I, 1);
          Insert(#13, S, I);
        end;

      inherited SetValue(S);
    end;
end;

{$IFDEF PRO}
{TDBCoolListBoxEditor}
procedure TDBCoolListBoxEditor.ExecuteVerb(Index: Integer); 
begin
  case Index of
    0: TDBCoolListBox(Component).BuildItems;
  end;
end;

function TDBCoolListBoxEditor.GetVerb(Index: Integer): string; 
begin
  case Index of
    0: Result := '&Build from DataSet...';
  end;
end;

function TDBCoolListBoxEditor.GetVerbCount: Integer;
begin
  Result := 1;
end;
{$ENDIF}

{TPointListProperty}
function TPointListProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog, paReadOnly]
end;

procedure TPointListProperty.Edit;
begin
  with TPointListForm.Create(Application) do
  try
    PointList := TPointList(GetOrdValue);
    if ShowModal = mrOk then
      SetOrdValue(LongInt(PointList));
  finally
    Free;
  end;
end;

{TPolygonesProperty}
function TPolygonesProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog, paReadOnly]
end;

procedure TPolygonesProperty.Edit;
begin
  with TPolygonesForm.Create(Application) do
  try
    CoolPolygones.Width := TControl(GetComponent(0)).Width;
    CoolPolygones.Height := TControl(GetComponent(0)).Height;

    Polygones := TPolygones(GetOrdValue);
    if ShowModal = mrOk then
      SetOrdValue(LongInt(Polygones));
  finally
    Free;
  end;
end;

{TPointListProperty}
function TColorListProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog, paReadOnly]
end;

{$IFNDEF DELPHI5_OR_ABOVE}
function GetObjectProp(Instance: TObject; const PropName: string; MinClass: TClass): TObject;
var
  PropInfo: PPropInfo;
begin
  PropInfo := TypInfo.GetPropInfo(PTypeInfo(Instance.ClassInfo), PropName);
  Result := TObject(TypInfo.GetOrdProp(Instance, PropInfo));
  if (Result <> nil) and
     (MinClass <> nil) and
     not (Result is MinClass) then
    Result := nil;
end;
{$ENDIF}

procedure TColorListProperty.Edit;
var
  InfoList: TPropInfoList;
  Bitmap: TBitmap;
  Images: TImageList;
begin
  with TColorListForm.Create(Application) do
  try
    InfoList := TPropInfoList.Create(GetComponent(0), [tkClass]);
    try
      if InfoList.ContainsByName('Bitmap') then
      begin
        {$IFDEF DELPHI5_OR_ABOVE}
        TObject(Bitmap) := TypInfo.GetObjectProp(GetComponent(0), 'Bitmap', TBitmap);
        {$ELSE}
        TObject(Bitmap) := GetObjectProp(GetComponent(0), 'Bitmap', TBitmap);
        {$ENDIF}
        if Assigned(Bitmap) then
          Image.Picture.Bitmap.Assign(Bitmap);
      end else
      if InfoList.ContainsByName('Images') then
      begin
        {$IFDEF DELPHI5_OR_ABOVE}
        TObject(Images) := TypInfo.GetObjectProp(GetComponent(0), 'Images', TImageList);
        {$ELSE}
        TObject(Images) := GetObjectProp(GetComponent(0), 'Images', TImageList);
        {$ENDIF}
        if Assigned(Images) and (Images.Count >= 0) then
          Images.GetBitmap(0, Image.Picture.Bitmap);
      end;
    finally
      InfoList.Free;
    end;
 
    ColorList := TColorList(GetOrdValue);

    if ShowModal = mrOk then
      SetOrdValue(LongInt(ColorList));
  finally
    Free;    
  end;
end;

{TCoolImageShapeEditor}
{$IFNDEF DELPHI6_OR_ABOVE}
procedure TCoolImageShapeEditor.EditProperty(PropertyEditor: TPropertyEditor;
  var Continue, FreeEditor: Boolean);
{$ELSE}
procedure TCoolImageShapeEditor.EditProperty(const Prop: IProperty; var Continue: Boolean);
{$ENDIF}
var
  PropName: string;
begin
  {$IFNDEF DELPHI6_OR_ABOVE}
  PropName := PropertyEditor.GetName;
  {$ELSE}
  PropName := Prop.GetName;
  {$ENDIF}
  if (CompareText(PropName, 'Bitmap') = 0) then
  begin
    {$IFNDEF DELPHI6_OR_ABOVE}
    PropertyEditor.Edit;
    {$ELSE}
    Prop.Edit;
    {$ENDIF}
    Continue := False;
  end;
end;

{TCoolPageControlEditor}
procedure TCoolPageControlEditor.ExecuteVerb(Index: Integer);
var
  Page: TCoolTabSheet;
  PageIndex: Integer;
begin
  case Index of
    0: with TCoolPageControl(Component) do
       begin
         Page := NewPage;
         Page.Name := Designer.UniqueName(Page.ClassName);
         Designer.Modified;
       end;
    1: with TCoolPageControl(Component) do
       begin
         PageIndex := GetActivePageIndex;
         if PageIndex < Pred(GetPageCount) then
           SetActivePageIndex(Succ(PageIndex));
       end;
    2: with TCoolPageControl(Component) do
       begin
         PageIndex := GetActivePageIndex;
         if PageIndex > 0 then
           SetActivePageIndex(Pred(PageIndex));
       end;
    3: with TCoolPageControl(Component) do
       begin
         PageIndex := GetActivePageIndex;
         if (PageIndex >= 0) and (PageIndex <= Pred(GetPageCount)) then
         begin
           Page := GetPage(PageIndex);
           RemovePage(Page);
           Page.Free;
         end; 
       end;    
  end;
end;

function TCoolPageControlEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := 'Ne&w Page';
    1: Result := 'Ne&xt Page';
    2: Result := 'Pre&vious Page';
    3: Result := 'De&lete Page';
  end;
end;

function TCoolPageControlEditor.GetVerbCount: Integer;
begin
  Result := 4;
end;

procedure Register;
begin
  RegisterComponents('CoolControls', [TCoolListBox,
                                      TCoolComboBox,
                                      TCoolEdit,
                                      TCoolEditBtn,
                                      TCoolMemo,
                                      TCoolLabel,
                                      TCoolScrollingText,
                                      TCoolCheckRadioBox,
                                      TCoolGroupBox,
                                      {$IFNDEF VER90}
                                      TCoolPanel,
                                      TCoolButtonPanel,
                                      {$ENDIF}
                                      TCoolBtn,
		                      TCoolCaptionBtn,
                                      TCoolThumbTab,
                                      TCoolBevel,
                                      TCoolCheckRadioGroup,
                                      TCoolPopupButton,
                                      TCoolPopupBox,
                  		      TCoolPopupButton2,
                                      TCoolPopupBox2,
	                              TCoolScrollBox,
                                      TCoolScrollbar,
                                      TCoolSpotSplitter]);

  RegisterComponents('CoolControls Extended', [{$IFDEF DELPHI4_OR_ABOVE}
                                               TCoolListView,
 	       				       TCoolExplorerListView,
                                               {$ENDIF}
                                               TCoolPageControl,
                                               TCoolTrackBar,
                                               TCoolBitmapButton,
                                               TCoolManyBitmapsButton,
                                               TCoolThermomitor,
                                               TCoolStringGrid]);

  RegisterComponents('CoolControls Tools', [TCoolFormHook,
	                                    TMDICoolFormHook,
                                            TCoolLocker,
                                            TCoolDesktop,
		                            TCoolWindowsInformant,
                                            TCoolTrayIcon,
					    TCoolCapture,
                                            TCoolImages,
                                            TCoolLoupe,
                                            TCoolLoupePolygones,
                                            TCoolAnimate,
					    TCoolAnimateEffects,
					    TCoolInstancesChecker,
				            TCoolDisksInformant,
 					    TCoolFileFinder,
                                            TCoolClipboardComponent,
                                            TCoolClipboardViewer,
                                            TCoolPolygones,
                                            TCoolPolyRegions,
                                            TCoolImageShape,
                                            TCoolTransparentArea,
                                            TCoolCaptionEffects,
                                            TCoolShadow,
                                            TCoolKeyboardSpy,
                                            TCoolProgrLock,
                                            TCoolWinDock]);

  {$IFDEF PRO}
  RegisterComponents('CoolControls Data', [TDBCoolListBox,
                                           TDBCoolComboBox,
                                           TDBCoolEdit,
                                           TDBCoolEditBtn,
                                           TDBCoolMemo,
                                           TDBCoolText,
                                           TDBCoolScrollingText,
                                           TDBCoolCheckRadioBox,
                                           {$IFDEF DELPHI4_OR_ABOVE}
				           TDBCoolLookupListBox,
                                           TDBCoolLookupComboBox,
                                           {$ENDIF}
                                           TDBCoolGrid,
                                           TDBCoolCheckRadioGroup,
                                           TDBCoolScrollbar]);
  {$ENDIF}


  {$IFDEF PRO}
  RegisterComponentEditor(TDBCoolListBox, TDBCoolListBoxEditor);
  {$ENDIF}
  RegisterPropertyEditor(TypeInfo(TCCAboutBox), nil, '', TCCAboutProperty);
  RegisterPropertyEditor(TypeInfo(TCaption), nil, '', TCaptionPropertyEx);
  {$IFNDEF VER90}
  RegisterComponentEditor(TCoolImages, TCoolImagesEditor);
  RegisterPropertyEditor(TypeInfo(TCopyMode), TCoolCapture, 'CopyMode', TCopyModeProperty);
  RegisterPropertyEditor(TypeInfo(TCopyMode), TCoolLoupe, 'CopyMode', TCopyModeProperty);
  RegisterPropertyEditor(TypeInfo(TCopyMode), TCoolLoupePolygones, 'CopyMode', TCopyModeProperty);
  RegisterClasses([TCoolSpeedButton]);
  RegisterNoIcon([TCoolSpeedButton]);
  RegisterComponentEditor(TCoolTrayIcon, TCoolTrayIconEditor);
  {$IFDEF DELPHI4_OR_ABOVE}
  RegisterComponentEditor(TCoolButtonPanel, TCoolButtonPanelCompEditor);
  {$ENDIF}
  {$ENDIF}
  RegisterClasses([TCoolTabSheet]);
  RegisterComponentEditor(TCoolPageControl, TCoolPageControlEditor);
  RegisterPropertyEditor(TypeInfo(TPointList), nil, '', TPointListProperty);
  RegisterPropertyEditor(TypeInfo(TPolygones), TCoolLoupePolygones, 'Polygones', TPolygonesProperty);
  RegisterPropertyEditor(TypeInfo(TPolygones), TCoolTransparentArea, 'Polygones', TPolygonesProperty);
  RegisterPropertyEditor(TypeInfo(TColorList), nil, '', TColorListProperty);
  RegisterComponentEditor(TCoolImageShape, TCoolImageShapeEditor);
end;

end.
