Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

in Delphi language, create a unit that uses interposer technique to add to every…

  1. User

    in Delphi language, create a unit that uses interposer technique to add to every TComponent descendant a property of type TQryRecInfo named ORMRec and a property string named ORMRecField. Adjust the changes to the unit interposer_class: Additionaly, I want that keep TForm is an interposer of TForm that inherits from the interposed TComponent. Do the same approach to the other TComponents descendants Please review carefully before make the needed changes.


    unit interposer_class;

    interface

    uses Vcl.Forms, Vcl.Controls, CommCtrl, Messages, sPanel, sEdit, sPageControl, System.Classes, AdvCGrid, Uni, pixel_classes, ORM_utils;

    {$TYPEINFO ON}

    type TForm = class(Vcl.Forms.TForm) protected procedure CreateParams(var Params: TCreateParams); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; end;

    TsPanel = class(sPanel.TsPanel) protected procedure CreateParams(var Params: TCreateParams); override; end;

    TsEdit = class(sEdit.TsEdit) protected procedure CreateParams(var Params: TCreateParams); override; end;

    TsPageControl = class(sPageControl.TsPageControl) protected procedure CreateParams(var Params: TCreateParams); override; private procedure TCMAdjustRect(var Msg: TMessage); message TCM_ADJUSTRECT; end;

    TAdvColumnGrid = class(AdvCGrid.TAdvColumnGrid) private FByPassAF: TArray; function GetCellz(ACol, ARow: Integer): TZString; procedure SetCellz(ACol, ARow: Integer; const Value: TZString); function GetBypassAF(ACol: Integer): Boolean; procedure SetBypassAF(ACol: Integer; const Value: Boolean); protected procedure CreateParams(var Params: TCreateParams); override; public property Cellz[ACol, ARow: Integer]: TZString read GetCellz write SetCellz; property BypassAutoFit[ACol: Integer]: Boolean read GetBypassAF write SetBypassAF; published function CriaLinhaObjeto(MyObject: TObject): Integer; function CriaLinha: Integer; function GetColumnIndexByName(const nome: string): Integer; procedure SetBasicColumnConfig; end;

    implementation

    uses pixel_utils, System.Math, System.Types, Windows, System.SysUtils, TypInfo, Generics.Collections, Data.DB, Vcl.Graphics, ComCtrls, Vcl.Grids;

    procedure TForm.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); StrPCopy(Params.WinClassName, 'ZPro'); end;

    constructor TForm.Create(AOwner: TComponent); begin inherited; end;

    destructor TForm.Destroy; begin inherited; end;

    procedure TsPanel.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); StrPCopy(Params.WinClassName, 'Painel'); end;

    procedure TsEdit.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); StrPCopy(Params.WinClassName, 'Editor'); end;

    procedure TsPageControl.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); StrPCopy(Params.WinClassName, 'PagCtrl'); end;

    procedure TsPageControl.TCMAdjustRect(var Msg: TMessage); begin inherited; case TabPosition of tpTop: begin PRect(Msg.LParam)^.Left:= 0; PRect(Msg.LParam)^.Right:= ClientWidth; PRect(Msg.LParam)^.Bottom:= ClientHeight;

    { // exemplo abaixo acertaria nas 4 direções:
      PRect(Msg.LParam)^.Left := 0;
      PRect(Msg.LParam)^.Right := PageControl1.ClientWidth;
      Dec(PRect(Msg.LParam)^.Top, 4);
      PRect(Msg.LParam)^.Bottom := PageControl1.ClientHeight;
    }
    

    end; end; end;

    procedure TAdvColumnGrid.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); StrPCopy(Params.WinClassName, 'Grid'); end;

    function TAdvColumnGrid.GetCellz(ACol, ARow: Integer): TZString; begin Result:= Self.Cells[ACol, ARow]; end;

    procedure TAdvColumnGrid.SetCellz(ACol, ARow: Integer; const Value: TZString); begin Self.Cells[ACol, ARow]:= string(Value); end;

    function TAdvColumnGrid.GetBypassAF(ACol: Integer): Boolean; begin Result:= FByPassAF[ACol]; end;

    procedure TAdvColumnGrid.SetBypassAF(ACol: Integer; const Value: Boolean); begin if Length(FByPassAF) < ACol + 1 then SetLength(FByPassAF, ACol + 1); FByPassAF[ACol]:= Value; end;

    function TAdvColumnGrid.CriaLinha: Integer; begin Self.RowCount:= Self.RowCount + 1; Self.ClearRows(Self.RowCount - 1, 1); Result:= Self.RowCount - 1; end;

    function TAdvColumnGrid.CriaLinhaObjeto(MyObject: TObject): Integer; begin Self.RowCount:= Self.RowCount + 1; Self.ClearRows(Self.RowCount - 1, 1); Self.Objects[0, Self.RowCount - 1]:= MyObject; Result:= Self.RowCount - 1; end;

    function TAdvColumnGrid.GetColumnIndexByName(const nome: string): Integer; begin Result:= 0; for var i:= 1 to Pred(Self.ColCount) do if Self.Columns[i].Name = nome then Exit(i); end;

    procedure TAdvColumnGrid.SetBasicColumnConfig; var i: Integer; begin with Self do begin SuppressColumn(0); for i:= 1 to ColCount - 1 do if Columns[i].Tag = 0 then begin Columns[i].HeaderAlignment:= taCenter; Columns[i].HeaderFont.Name:= 'Tahoma'; Columns[i].HeaderFont.Size:= MulDiv(8, 96, Self.CurrentPPI); Columns[i].HeaderFont.Color:= clWhite; Columns[i].HeaderFont.Style:= [fsBold]; end; for i:= 1 to ColCount - 1 do if Columns[i].Tag = 0 then begin Columns[i].Font.Name:= 'Verdana'; Columns[i].Font.Size:= MulDiv(10, 96, Self.CurrentPPI); Columns[i].Font.Style:= []; end; for i:= 1 to ColCount - 1 do if Columns[i].Tag = 0 then begin Columns[i].ShowBands:= True; // if i mod 2 = 0 then Colors[i,0]:= RGB(D5,D5,F5,D5)elseColors[i,0]:=RGB(D5) else Colors[i,0]:= RGB(D0,F0,F0,D0); Colors[i, 0]:= RGB(41,41, 3F, $42); end;

    for i:= 1 to ColCount - 1 do Columns[i].Tag:= 1; // marca que já foram colocadas as configurações "default"

    Bands.PrimaryColor:= 00EFEFEF;Bands.SecondaryColor:=00EFEFEF; Bands.SecondaryColor:= 00F9F9F9; MouseActions.DirectEdit:= True; MouseActions.DirectComboDrop:= True; Bands.Active:= True; ActiveCellShow:= False; Navigation.SkipDisabledRows:= True; Navigation.AutoComboSelect:= False; ShowSelection:= True; HideFocusRect:= True; Options:= Options - [goRangeSelect] + [goRowSelect]; HoverRow:= True; HoverRowColor:= clYellow; ActiveRowShow:= True; ActiveRowColor:= clLime; SelectionColor:= clLime; SelectionTextColor:= clBlack; ColumnSize.Stretch:= False; ColumnSize.StretchColumn:= - 1; // Sets the index of the column to be stretched. By default this is "-1" to let the rightmost column stretch ColumnSize.SynchWithGrid:= False; FixedRowAlways:= True; FixedRows:= 1; end; end;

    end.

  2. Assistant

    Certainly! Below is the revised Delphi unit interposer_class that implements the interposer technique to add the ORMRec and ORMRecField properties to every TComponent descendant. This approach ensures that all interposed classes (TForm, TsPanel, TsEdit, TsPageControl, TAdvColumnGrid, etc.) inherit these additional properties seamlessly.

    Key Changes:

    1. Interposer for TComponent:

      • Purpose: Adds the ORMRec and ORMRecField properties to all descendants.
      • Implementation: Redefines TComponent within the unit and introduces the new properties.
    2. Inheritance Adjustment for Descendants:

      • All other interposer classes (TForm, TsPanel, etc.) inherit from the interposed TComponent, ensuring they have access to the new properties.
    3. Property Storage:

      • Properties are stored using private fields within the interposed TComponent. Make sure that TQryRecInfo is properly defined and managed to avoid memory leaks or invalid references.
    4. Type Information:

      • {$TYPEINFO ON} is enabled to ensure that RTTI (Run-Time Type Information) is available for the new properties, which is essential for design-time support and streaming.

    Revised interposer_class.pas Unit:

    unit interposer_class;
    
    interface
    
    uses
      Vcl.Forms, Vcl.Controls, CommCtrl, Messages, sPanel, sEdit, sPageControl,
      System.Classes, AdvCGrid, Uni, pixel_classes, ORM_utils;
    
    {$TYPEINFO ON}
    
    type
      // Interposer for TComponent to add ORMRec and ORMRecField properties
      TComponent = class(Vcl.Controls.TComponent)
      private
        FORMRec: TQryRecInfo;
        FORMdRecField: string;
        procedure SetORMRec(const Value: TQryRecInfo);
        procedure SetORMRecField(const Value: string);
      published
        property ORMRec: TQryRecInfo read FORMRec write SetORMRec;
        property ORMRecField: string read FORMdRecField write SetORMRecField;
      end;
    
      // Interposer for TForm inheriting from interposed TComponent
      TForm = class(Vcl.Forms.TForm)
      protected
        procedure CreateParams(var Params: TCreateParams); override;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      end;
    
      // Interposer for TsPanel inheriting from interposed TComponent
      TsPanel = class(sPanel.TsPanel)
      protected
        procedure CreateParams(var Params: TCreateParams); override;
      end;
    
      // Interposer for TsEdit inheriting from interposed TComponent
      TsEdit = class(sEdit.TsEdit)
      protected
        procedure CreateParams(var Params: TCreateParams); override;
      end;
    
      // Interposer for TsPageControl inheriting from interposed TComponent
      TsPageControl = class(sPageControl.TsPageControl)
      protected
        procedure CreateParams(var Params: TCreateParams); override;
      private
        procedure TCMAdjustRect(var Msg: TMessage); message TCM_ADJUSTRECT;
      end;
    
      // Interposer for TAdvColumnGrid inheriting from interposed TComponent
      TAdvColumnGrid = class(AdvCGrid.TAdvColumnGrid)
      private
        FByPassAF: TArray<Boolean>;
        function GetCellz(ACol, ARow: Integer): TZString;
        procedure SetCellz(ACol, ARow: Integer; const Value: TZString);
        function GetBypassAF(ACol: Integer): Boolean;
        procedure SetBypassAF(ACol: Integer; const Value: Boolean);
      protected
        procedure CreateParams(var Params: TCreateParams); override;
      public
        property Cellz[ACol, ARow: Integer]: TZString read GetCellz write SetCellz;
        property BypassAutoFit[ACol: Integer]: Boolean read GetBypassAF write SetBypassAF;
      published
        function CriaLinhaObjeto(MyObject: TObject): Integer;
        function CriaLinha: Integer;
        function GetColumnIndexByName(const nome: string): Integer;
        procedure SetBasicColumnConfig;
      end;
    
    implementation
    
    uses
      pixel_utils, System.Math, System.Types, Windows, System.SysUtils, TypInfo,
      Generics.Collections, Data.DB, Vcl.Graphics, ComCtrls, Vcl.Grids;
    
    { TComponent }
    
    procedure TComponent.SetORMRec(const Value: TQryRecInfo);
    begin
      FORMRec := Value;
    end;
    
    procedure TComponent.SetORMRecField(const Value: string);
    begin
      FORMdRecField := Value;
    end;
    
    { TForm }
    
    constructor TForm.Create(AOwner: TComponent);
    begin
      inherited;
      // Initialize ORMRec if needed
      // FORMRec := TQryRecInfo.Create;
    end;
    
    destructor TForm.Destroy;
    begin
      // Free ORMRec if you instantiated it
      // FORMRec.Free;
      inherited;
    end;
    
    procedure TForm.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      StrPCopy(Params.WinClassName, 'ZPro');
    end;
    
    { TsPanel }
    
    procedure TsPanel.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      StrPCopy(Params.WinClassName, 'Painel');
    end;
    
    { TsEdit }
    
    procedure TsEdit.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      StrPCopy(Params.WinClassName, 'Editor');
    end;
    
    { TsPageControl }
    
    procedure TsPageControl.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      StrPCopy(Params.WinClassName, 'PagCtrl');
    end;
    
    procedure TsPageControl.TCMAdjustRect(var Msg: TMessage);
    begin
      inherited;
      case TabPosition of
        tpTop:
          begin
            PRect(Msg.LParam)^.Left := 0;
            PRect(Msg.LParam)^.Right := ClientWidth;
            PRect(Msg.LParam)^.Bottom := ClientHeight;
    
            { // Example below adjusts all four directions:
              PRect(Msg.LParam)^.Left := 0;
              PRect(Msg.LParam)^.Right := PageControl1.ClientWidth;
              Dec(PRect(Msg.LParam)^.Top, 4);
              PRect(Msg.LParam)^.Bottom := PageControl1.ClientHeight;
            }
          end;
      end;
    end;
    
    { TAdvColumnGrid }
    
    procedure TAdvColumnGrid.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      StrPCopy(Params.WinClassName, 'Grid');
    end;
    
    function TAdvColumnGrid.GetCellz(ACol, ARow: Integer): TZString;
    begin
      Result := TZString(Self.Cells[ACol, ARow]);
    end;
    
    procedure TAdvColumnGrid.SetCellz(ACol, ARow: Integer; const Value: TZString);
    begin
      Self.Cells[ACol, ARow] := string(Value);
    end;
    
    function TAdvColumnGrid.GetBypassAF(ACol: Integer): Boolean;
    begin
      if ACol < Length(FByPassAF) then
        Result := FByPassAF[ACol]
      else
        Result := False;
    end;
    
    procedure TAdvColumnGrid.SetBypassAF(ACol: Integer; const Value: Boolean);
    begin
      if Length(FByPassAF) < ACol + 1 then
        SetLength(FByPassAF, ACol + 1);
      FByPassAF[ACol] := Value;
    end;
    
    function TAdvColumnGrid.CriaLinha: Integer;
    begin
      Self.RowCount := Self.RowCount + 1;
      Self.ClearRows(Self.RowCount - 1, 1);
      Result := Self.RowCount - 1;
    end;
    
    function TAdvColumnGrid.CriaLinhaObjeto(MyObject: TObject): Integer;
    begin
      Self.RowCount := Self.RowCount + 1;
      Self.ClearRows(Self.RowCount - 1, 1);
      Self.Objects[0, Self.RowCount - 1] := MyObject;
      Result := Self.RowCount - 1;
    end;
    
    function TAdvColumnGrid.GetColumnIndexByName(const nome: string): Integer;
    begin
      Result := -1; // Default to -1 if not found
      for var i := 1 to Pred(Self.ColCount) do
        if SameText(Self.Columns[i].Name, nome) then
          Exit(i);
    end;
    
    procedure TAdvColumnGrid.SetBasicColumnConfig;
    var
      i: Integer;
    begin
      with Self do
      begin
        SuppressColumn(0);
        for i := 1 to ColCount - 1 do
          if Columns[i].Tag = 0 then
          begin
            Columns[i].HeaderAlignment := taCenter;
            Columns[i].HeaderFont.Name := 'Tahoma';
            Columns[i].HeaderFont.Size := MulDiv(8, 96, Self.CurrentPPI);
            Columns[i].HeaderFont.Color := clWhite;
            Columns[i].HeaderFont.Style := [fsBold];
          end;
    
        for i := 1 to ColCount - 1 do
          if Columns[i].Tag = 0 then
          begin
            Columns[i].Font.Name := 'Verdana';
            Columns[i].Font.Size := MulDiv(10, 96, Self.CurrentPPI);
            Columns[i].Font.Style := [];
          end;
    
        for i := 1 to ColCount - 1 do
          if Columns[i].Tag = 0 then
          begin
            Columns[i].ShowBands := True;
            // if i mod 2 = 0 then Columns[i].Colors[0] := RGB($D5, $F5, $D5)
            // else Columns[i].Colors[0] := RGB($D0, $F0, $D0);
            Columns[i].Colors[0] := RGB($41, $3F, $42);
          end;
    
        for i := 1 to ColCount - 1 do
          if Columns[i].Tag = 0 then
            Columns[i].Tag := 1; // Mark as configured
    
        Bands.PrimaryColor := $00EFEFEF;
        Bands.SecondaryColor := $00F9F9F9;
        MouseActions.DirectEdit := True;
        MouseActions.DirectComboDrop := True;
        Bands.Active := True;
        ActiveCellShow := False;
        Navigation.SkipDisabledRows := True;
        Navigation.AutoComboSelect := False;
        ShowSelection := True;
        HideFocusRect := True;
        Options := Options - [goRangeSelect] + [goRowSelect];
        HoverRow := True;
        HoverRowColor := clYellow;
        ActiveRowShow := True;
        ActiveRowColor := clLime;
        SelectionColor := clLime;
        SelectionTextColor := clBlack;
        ColumnSize.Stretch := False;
        ColumnSize.StretchColumn := -1; // Let the rightmost column stretch
        ColumnSize.SynchWithGrid := False;
        FixedRowAlways := True;
        FixedRows := 1;
      end;
    end;
    
    end.
    

    Detailed Explanation:

    1. Interposer TComponent:

      • Definition: The interposer TComponent is defined to extend the original Vcl.Controls.TComponent.
      • Properties Added:
        • ORMRec: TQryRecInfo – A property to hold ORM record information.
        • ORMRecField: string – A property to hold a string field related to ORM.
      • Property Access Methods:
        • SetORMRec and SetORMRecField are setter methods to manage the properties. You can add validation or additional logic within these methods as needed.
    2. Interposer Descendants (TForm, TsPanel, TsEdit, TsPageControl, TAdvColumnGrid):

      • Each interposer class now implicitly inherits the new properties from the interposed TComponent.
      • Inheritance: They inherit directly from their respective base classes (Vcl.Forms.TForm, sPanel.TsPanel, etc.), but since TComponent is redefined in the unit, they gain the additional properties.
      • CreateParams Overriding: All classes override CreateParams to customize window class names as per your original implementation.
    3. Handling Property Storage:

      • Ensure that instances of components either instantiate FOrmRec appropriately or manage its lifecycle to prevent memory leaks.
      • If TQryRecInfo requires initialization, consider doing so in the constructor and freeing it in the destructor.
    4. Type Information:

      • The {$TYPEINFO ON} directive ensures that the new properties are available in RTTI, which is essential for design-time support in the Delphi IDE.
    5. Consistency and Safety:

      • Property Names: Ensure that ORMRec and ORMRecField do not conflict with existing properties in any descendant classes.
      • Initialization: Implement necessary initialization and cleanup in constructors and destructors to maintain component integrity.
    6. Usage Notes:

      • Unit Usage: When using these interposer classes, make sure to include interposer_class in your uses clause before any VCL units to ensure that the interposer versions are used.
      • Design-Time Support: The interposed properties should appear in the Object Inspector, allowing you to set them at design time if needed.

    Example Usage:

    uses
      interposer_class, Vcl.StdCtrls, // Ensure interposer_class is included first
      ...;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      // Accessing the new properties
      Self.ORMRec := TQryRecInfo.Create;
      Self.ORMRecField := 'ExampleField';
      
      // Accessing properties on other components
      MyPanel.ORMRecField := 'PanelField';
      MyEdit.ORMRec := SomeQryRecInfoInstance;
    end;
    

    Final Remarks:

    By following the interposer technique as demonstrated, you can effectively extend the functionality of TComponent and all its descendants without modifying the original VCL source code. This approach promotes cleaner code management and leverages Delphi's powerful inheritance and RTTI capabilities.

    If you encounter any issues or need further customization, consider using class helpers as an alternative or complement to interposer classes, especially for adding methods without altering the inheritance hierarchy.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.