delphiaccessibilityc++buildervirtualtreeview

Implementing VirtualTreeView TVTDefaultAccessibleProvider in C++ Builder


When using VirtualStringTree to add accessibility support it is required to include the unit VirtualTrees.Accessibility in the uses section. This works in Delphi.

The equivalent of this in C++ Builder would be to include the #include "VirtualTrees.Accessibility.hpp". But including the include file doesn't have any effect.

I've traced the problem to the VirtualTrees.Accessibility.pas file and it executes a few lines of code to register the default accessibility provider in Delphi while this code is absent from the hpp file. So including the hpp does include the class but not the code which registers the provider.

The code in question (at the end of VirtualTrees.Accessibility.pas is this):

var
  DefaultAccessibleProvider: TVTDefaultAccessibleProvider;
  DefaultAccessibleItemProvider: TVTDefaultAccessibleItemProvider;
  MultiColumnAccessibleProvider: TVTMultiColumnAccessibleItemProvider;

initialization
  if DefaultAccessibleProvider = nil then
  begin
    DefaultAccessibleProvider := TVTDefaultAccessibleProvider.Create;
    GetAccessibilityFactory.RegisterAccessibleProvider(DefaultAccessibleProvider);
  end;
  if DefaultAccessibleItemProvider = nil then
  begin
    DefaultAccessibleItemProvider := TVTDefaultAccessibleItemProvider.Create;
    GetAccessibilityFactory.RegisterAccessibleProvider(DefaultAccessibleItemProvider);
  end;
  if MultiColumnAccessibleProvider = nil then
  begin
    MultiColumnAccessibleProvider := TVTMultiColumnAccessibleItemProvider.Create;
    GetAccessibilityFactory.RegisterAccessibleProvider(MultiColumnAccessibleProvider);
  end;
finalization
  GetAccessibilityFactory.UnRegisterAccessibleProvider(MultiColumnAccessibleProvider);
  MultiColumnAccessibleProvider := nil;
  GetAccessibilityFactory.UnRegisterAccessibleProvider(DefaultAccessibleItemProvider);
  DefaultAccessibleItemProvider := nil;
  GetAccessibilityFactory.UnRegisterAccessibleProvider(DefaultAccessibleProvider);
  DefaultAccessibleProvider := nil;

end.

My question - how do I translate or use the above so I can have accessibility support from C++ Builder as well after including the VirtualTrees.Accessibility.hpp file? A translation of the above into C++ would be very appreciated.


Solution

  • Based on research from this link and answer by Remy Lebeau:

    The initialization part is not called

    And the opened issue on VirtualTree GitHub:

    https://github.com/Virtual-TreeView/Virtual-TreeView/issues/951

    I am sharing a solution here, how this might be achieved by either one of the following:

    Method A

    By calling:

    TVirtualTreeAccessibility::RegisterDefaultAccessibleProviders();

    somewhere in the cpp file, as suggested by Joachim Marder

    Method B

    By adding:

    #pragma link "VirtualTrees.Accessibility"

    in the cpp source file

    Method C

    By adding something like:

    {$HPPEMIT '#pragma link "VirtualTrees.Accessibility"'}

    in the VirtualTrees.Accessibility.pas source code