2

When a property is a simple component of any class, the IDE's property editor is able to drop down a list of all compatible components in all the project's forms.

I want to do some equivalent task, but with some filtering based on acceptable component classes for the property; these classes common ancestor is only TComponent and they have custom interfaces.

Currently I have a working property editor that uses a paValueList attribute and some filtering in the GetValues procedure, based on checking the supported interfaces, but it is limited to the current form :-(.

How to browse all the forms like the IDE does?

8
  • 1
    You'll have to parse the uses clause in the interface section.
    – Johan
    Sep 11, 2015 at 14:33
  • If you are filtering for only 1 interface, you can change the property to be that interface instead of TComponent and then let the default editor filter the components for you automatically. Sep 11, 2015 at 19:27
  • @Johan: and to gain access to the uses clause in a property editor, you would have to query BorlandIDEServices for the IOTAModuleServices interface and enumerate its modules until you find the IOTAModule interface for the unit containing the component being edited, then obtain the module's IOTASourceEditor interface and call its CreateReader() method to get an IOTAEditReader interface for reading the unit's source code, then finally you can parse out the uses clause. That is a lot of work. Sep 11, 2015 at 20:05
  • IDE does not browse all forms but only forms whose units are added to uses clause of your current form. As far as I know the only way to filter shown components that you want is to make sure that all those components inherits from one common class which should not have any other non-wanted classes/components derived from it. So in your case you would have to inject additional common class into inheritance tree somewhere between TComponent and your components classes which might not be possible if your are inheriting from existing VCL components. Sep 11, 2015 at 20:25
  • Another option might be to use interfaces and multi-inheritance to make sure that all of your components interfaces share one common interface. But I haven't tried this myself since I personally don't like working with interfaces unless I'm forced to. I prefer using plain old classes more. Sep 11, 2015 at 20:28

2 Answers 2

4

I want to do some equivalent task, but with some filtering based on acceptable component classes for the property; these classes common ancestor is only TComponent and they have custom interfaces.

If you are filtering for only 1 interface, you should change the property in question to accept that interface type instead of a TComponent, and then the default property editor for interface properties (TInterfaceProperty) will filter the components automatically for you:

property MyProperty: IMyInterface read ... write ...;

Currently I have a working property editor that uses a paValueList attribute and some filtering in the GetValues procedure, based on checking the supported interfaces, but it is limited to the current form :-(.

How to browse all the forms like the IDE does?

To manually filter the components in a custom property editor, you need to do the same thing that the default component property editor (TComponentProperty) does to obtain the compatible components, and then you can filter them further as needed.

Internally, TComponentProperty.GetValues() simply calls Designer.GetComponentNames(), passing it the PTypeData of the property type that is being edited:

procedure TComponentProperty.GetValues(Proc: TGetStrProc);
begin
  Designer.GetComponentNames(GetTypeData(GetPropType), Proc);
end;

So, if your property accepts a TComponent (since that is the only common ancestor of your intended components):

property MyProperty: TComponent read ... write ...;

Then GetPropType() in this case would return TypeInfo(TComponent).

GetComponentNames() (whose implementation is in the IDE and not available in the VCL source code) enumerates the components of the Root (Form, DataModule, or Frame) that owns the component being edited, as well as all linked Root objects that are accessible in other units specified in the edited Root's uses clause. This is documented behavior:

DesignIntf.IDesigner60.GetComponentNames

Executes a callback for every component that can be assigned a property of a specified type.

Use GetComponentNames to call the procedure specified by the Proc parameter for every component that can be assigned a property that matches the TypeData parameter. For each component, Proc is called with its S parameter set to the name of the component. This parameter can be used to obtain a reference to the component by calling the GetComponent method.

Note: GetComponentNames calls Proc for components in units that are in the uses clause of the current root object's unit (Delphi) or included by that unit (C++), as well as the entity that is the value of Root.

So, in your GetValues() implementation, call Designer.GetComponentNames() specifying the PTypeData for TComponent and let the IDE enumerate all available units and provide you with a list of each component's Name. Then you can loop through that list calling Designer.GetComponent() to get the actual TComponent objects and query them for your desired interface(s):

procedure TMyComponentProperty.GetValues(Proc: TGetStrProc);
var
  Names: TStringList;
  I: Integer;
begin
  Names := TStringList.Create;
  try
    Designer.GetComponentNames(GetTypeData(TypInfo(TComponent)), Names.Append);
    for I := 0 to Names.Count-1 do
    begin
      if Supports(Designer.GetComponent(Names[I]), IMyInterface) then
        Proc(Names[I]);
    end;
  finally
    Names.Free;
  end;
end;

In fact, this is very similar to what the default TInterfaceProperty.GetValues() implementation does:

procedure TInterfaceProperty.ReceiveComponentNames(const S: string);
var
  Temp: TComponent;
  Intf: IInterface;
begin
  Temp := Designer.GetComponent(S);
  if Assigned(FGetValuesStrProc) and
     Assigned(Temp) and
     Supports(TObject(Temp), GetTypeData(GetPropType)^.Guid, Intf) then
    FGetValuesStrProc(S);
end;

procedure TInterfaceProperty.GetValues(Proc: TGetStrProc);
begin
  FGetValuesStrProc := Proc;
  try
    Designer.GetComponentNames(GetTypeData(TypeInfo(TComponent)), ReceiveComponentNames);
  finally
    FGetValuesStrProc := nil;
  end;
end;

The only difference is that TInterfaceProperty does not waste memory collecting the names into a temp TStringList. It filters them in real-time as they are being enumerated.

3
  • @Remy: Thanks a lot; I will explore this solution.asap; it seems the perfect solution. I missed GetComponentNames. To others: Thanks for your tries.
    – Philippe
    Sep 12, 2015 at 8:23
  • @Remy, that is a great answer. I came across this while looking for another question. Do you know if there is a way that I can have components created via code, in a data module for example, and be able to link those components to other components in other forms? We've tried putting the declarations in public and creating published properties for them, but they aren't available for linking to a component property on another form. Please let me know if you think this warrants a separate question. Mar 8, 2017 at 21:16
  • @JonRobertson: since you are creating the objects in code to begin with, why not just assign them to the desired properties in code, too? It doesn't really make sense to do this at design-time. But, as long as the objects are created with the DM as their Owner, and they have a Name assigned, and published properties to access them, I don't see why they would not be available to a property editor, as long as the DM unit is in the uses clause. Mar 8, 2017 at 21:32
0

Remy's solution works perfectly for my needs. Nevertheless I've "simplified" a bit the filtering procedure:

procedure TMyComponentProperty.ReceiveComponentNames(const S: string);
var
  Temp: TComponent;
  Intf: IInterface;
begin
  if Assigned(FGetValuesStrProc) then
    begin
      Temp := Designer.GetComponent(S);
      if Assigned(Temp) then
        if Temp.GetInterface(IMyInterface, IntF) then
          FGetValuesStrProc(S);
      // May add other interfaces checks here   
    end;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.