4

I created a Custom components SkinMgr and SkinPanel in delphi2009. i wanted the two component automatically link together even if the SkinMgr is in a DataModule or anywhere in the other form.

any help, sample or suggestion.

thank you in advance.

XBasic3000
  • 3,418
  • 5
  • 46
  • 91
  • I'm afraid without some Open Tools API magic this won't be possible. Connect two controls on the same form should be easy. You can get a parent form of the just placed component, iterate over all of its components and when you'll find one (the first) that can be paired, you'll just assign it, but I can't imagine how the control placed on a form would guess the form or datamodule names from the other units. But maybe I'm wrong and it's easy... :-) – TLama Sep 03 '12 at 03:42
  • I presume that you want this linking to occur at design-time, not run-time; and also that only when a new instance of the SkinPanel is dropped on a form, as opposed to the form being loaded by the designer? – Sean B. Durkin Sep 03 '12 at 04:48
  • @TLama, Yes, it can paired automatically if they drop in same form. I have have AlphaControls which has that feature. I wonder how they do that. – XBasic3000 Sep 03 '12 at 07:23
  • @Sean B. Durkin, yes, designed time only so that the skin can be rendered at designtime just like the AlphaControls. – XBasic3000 Sep 03 '12 at 07:25

1 Answers1

3

Here's a quick test with a TActionList descendant and TCustomActionManager: when an instance of TActionListEx component is dropped on a form at designtime, it enumerates all current project's modules and try to find a form/datamodule with an instance of TCustomActionManager. If it finds one, it adds the newly created TActionListEx to its LinkedActionLists collection.

Runtime package:

unit TestComponents;

interface

uses
  System.Classes, Vcl.ActnList;

type
  TActionListEx = class(TActionList)
  public
    constructor Create(AOwner: TComponent); override;
  end;
  TNotifyProc = procedure(Sender: TObject);

var
  CreateNotify: TNotifyProc = nil;

implementation

{ TActionListEx }

constructor TActionListEx.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if (csDesigning in ComponentState) and Assigned(CreateNotify) then
    CreateNotify(Self);
end;

end.

Design time package:

unit TestComponentsDesign;

interface

procedure Register;

implementation

uses
  System.Classes, System.SysUtils,
  Vcl.ActnMan,
  ToolsAPI,
  TestComponents;

procedure CreateNotifyProc(Sender: TObject);
var
  ActionList: TActionListEx absolute Sender;
  ModuleServices: IOTAModuleServices;
  ActiveProject: IOTAProject;
  I, J: Integer;
  ModuleInfo: IOTAModuleInfo;
  Module: IOTAModule;
  Editor: IOTAFormEditor;
  RootComponent: IOTAComponent;
  Component: INTAComponent;
  ActionManager: TCustomActionManager;
  ActionListItem: TActionListItem;
begin
  if not (Sender is TActionListEx) or not (csDesigning in ActionList.ComponentState) then
    Exit;

  if not Supports(BorlandIDEServices, IOTAModuleServices, ModuleServices) then
    Exit;

  ActiveProject := ModuleServices.GetActiveProject;
  if not Assigned(ActiveProject) then
    Exit;

  for I := 0 to ActiveProject.GetModuleCount - 1 do
  begin
    Module := nil;
    Editor := nil;
    RootComponent := nil;

    ModuleInfo := ActiveProject.GetModule(I);
    if Assigned(ModuleInfo) and (ModuleInfo.FormName <> '') then
      Module := ModuleInfo.OpenModule;

    if Assigned(Module) then
      for J := 0 to Module.ModuleFileCount - 1 do
        if Supports(Module.ModuleFileEditors[J], IOTAFormEditor, Editor) then
          Break;

    if Assigned(Editor) then
      RootComponent := Editor.GetRootComponent;

    if Assigned(RootComponent) then
      for J := 0 to RootComponent.GetComponentCount - 1 do
        if Supports(RootComponent.GetComponent(J), INTAComponent, Component) and (Component.GetComponent is TCustomActionManager) then
        begin
          ActionManager := TCustomActionManager(Component.GetComponent);
          ActionListItem := ActionManager.LinkedActionLists.Add as TActionListItem;
          try
            ActionListItem.ActionList := ActionList;
            Editor.MarkModified;
            Exit;
          except
            ActionListItem.Free;
            raise;
          end;
        end;
  end;
end;

procedure Register;
begin
  RegisterComponents('Test', [TActionListEx]);
  CreateNotify := CreateNotifyProc;
end;

initialization

finalization
  CreateNotify := nil;

end.
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128