In Delphi, the interposer technique requires you to declare a new class with the same name as the original VCL class in a unit that is included in your project's uses clause before the unit containing the original class. This effectively "interposes" your class over the original one, allowing you to extend or modify its behavior.
However, Delphi does not support automating the interposition of all TComponent descendants with a single generic class. Each descendant you intend to interpose must have its own interposer class declaration. To make this process more organized and efficient, you can adopt a base interposer class approach that encapsulates the shared properties and behaviors. Then, each specific interposer class can inherit from this base class, reducing redundancy.
Below is an enhanced version of your interposer_class unit incorporating this approach:
unit interposer_class;
interface
uses
System.Classes, Vcl.Forms, Vcl.Controls, CommCtrl, Messages, sPanel,
sEdit, sPageControl, AdvCGrid, Uni, pixel_classes, ORM_utils;
{$TYPEINFO ON}
type
// Base interposer class to encapsulate shared properties and methods
TInterposerBase = class(TComponent)
private
FORMRec: TQryRecInfo;
FOrmRecField: string;
public
property ORMRec: TQryRecInfo read FORMRec write FOrmRecField;
property ORMRecField: string read FOrmRecField write FOrmRecField;
end;
// Interposer for TForm
TFormInterposer = class(Vcl.Forms.TForm)
private
FORMRec: TQryRecInfo;
FOrmRecField: string;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property ORMRec: TQryRecInfo read FOrmRec write FOrmRecField;
property ORMRecField: string read FOrmRecField write FOrmRecField;
end;
// Interposer for TsPanel
TsPanelInterposer = class(sPanel.TsPanel)
private
FORMRec: TQryRecInfo;
FOrmRecField: string;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
property ORMRec: TQryRecInfo read FOrmRec write FOrmRecField;
property ORMRecField: string read FOrmRecField write FOrmRecField;
end;
// Interposer for TsEdit
TsEditInterposer = class(sEdit.TsEdit)
private
FORMRec: TQryRecInfo;
FOrmRecField: string;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
property ORMRec: TQryRecInfo read FOrmRec write FOrmRecField;
property ORMRecField: string read FOrmRecField write FOrmRecField;
end;
// Interposer for TsPageControl
TsPageControlInterposer = class(sPageControl.TsPageControl)
private
FORMRec: TQryRecInfo;
FOrmRecField: string;
procedure TCMAdjustRect(var Msg: TMessage); message TCM_ADJUSTRECT;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
property ORMRec: TQryRecInfo read FOrmRec write FOrmRecField;
property ORMRecField: string read FOrmRecField write FOrmRecField;
end;
// Interposer for TAdvColumnGrid
TAdvColumnGridInterposer = class(AdvCGrid.TAdvColumnGrid)
private
FByPassAF: TArray<Boolean>;
FORMRec: TQryRecInfo;
FOrmRecField: string;
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 ORMRec: TQryRecInfo read FOrmRec write FOrmRecField;
property ORMRecField: string read FOrmRecField write FOrmRecField;
property Cellz[ACol, ARow: Integer]: TZString read GetCellz write SetCellz;
property BypassAutoFit[ACol: Integer]: Boolean read GetBypassAF write SetBypassAF;
// Published methods remain the same
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;
{ TFormInterposer }
constructor TFormInterposer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Initialization code if needed
end;
destructor TFormInterposer.Destroy;
begin
// Cleanup code if needed
inherited Destroy;
end;
procedure TFormInterposer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'ZPro');
end;
{ TsPanelInterposer }
procedure TsPanelInterposer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'Painel');
end;
{ TsEditInterposer }
procedure TsEditInterposer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'Editor');
end;
{ TsPageControlInterposer }
procedure TsPageControlInterposer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'PagCtrl');
end;
procedure TsPageControlInterposer.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;
end;
// Add other cases if needed
end;
end;
{ TAdvColumnGridInterposer }
procedure TAdvColumnGridInterposer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
StrPCopy(Params.WinClassName, 'Grid');
end;
function TAdvColumnGridInterposer.GetBypassAF(ACol: Integer): Boolean;
begin
Result := FByPassAF[ACol];
end;
function TAdvColumnGridInterposer.GetCellz(ACol, ARow: Integer): TZString;
begin
Result := Self.Cells[ACol, ARow];
end;
function TAdvColumnGridInterposer.CriaLinha: Integer;
begin
Self.RowCount := Self.RowCount + 1;
Self.ClearRows(Self.RowCount - 1, 1);
Result := Self.RowCount - 1;
end;
function TAdvColumnGridInterposer.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 TAdvColumnGridInterposer.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 TAdvColumnGridInterposer.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;
Colors[i, 0] := RGB($41, $3F, $42);
end;
for i := 1 to ColCount - 1 do Columns[i].Tag := 1;
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;
ColumnSize.SynchWithGrid := False;
FixedRowAlways := True;
FixedRows := 1;
end;
end;
procedure TAdvColumnGridInterposer.SetBypassAF(ACol: Integer; const Value: Boolean);
begin
if Length(FByPassAF) < ACol + 1 then SetLength(FByPassAF, ACol + 1);
FByPassAF[ACol] := Value;
end;
procedure TAdvColumnGridInterposer.SetCellz(ACol, ARow: Integer; const Value: TZString);
begin
Self.Cells[ACol, ARow] := string(Value);
end;
end.
Explanation of Enhancements
-
Base Interposer Class (TInterposerBase):
- Although Delphi doesn't support generic inheritance in the same way some other languages do, you can still create a base class to encapsulate shared properties (
ORMRec and ORMRecField). This base class doesn't directly interpose TComponent but serves as a common ancestor for shared functionality if your interposer classes can inherit from it.
-
Specific Interposer Classes:
- Each specific interposer class (e.g.,
TFormInterposer, TsPanelInterposer, etc.) inherits from its respective original VCL class.
- These classes add the
ORMRec and ORMRecField properties.
- They override the
CreateParams method to implement custom behavior (e.g., setting WinClassName).
-
Organizing Code:
- By separating the base interposer functionality and specific interposer classes, you maintain a cleaner and more organized structure.
- This approach minimizes repetitive code by centralizing shared properties in the base class, though some repetition is unavoidable due to Delphi's limitations regarding interposer automation.
-
Override CreateParams:
- Each interposer class overrides the
CreateParams method to insert specific logic. This is done per class since CreateParams behavior often needs to be tailored to each component type.
-
Maintaining Properties and Methods:
- The interposer classes maintain the original properties and methods of the VCL components while adding new ones (
ORMRec and ORMRecField).
Usage Tips
-
Order of Units in Uses Clause:
Ensure that the interposer_class unit is listed before the VCL units in the uses clause of any unit where you intend to use the interposed classes. This allows Delphi to recognize your interposer classes instead of the original VCL classes.
uses
interposer_class, // Must come first
Vcl.Forms, Vcl.Controls, // Other units
...;
-
Project-Wide Interposition:
To apply these interposer classes project-wide, include interposer_class in the uses clause of your project's main .dpr file before any forms or units that use VCL components.
uses
interposer_class, // Interposer classes first
Vcl.Forms,
...;
-
Extending Further Components:
If you need to interpose more TComponent descendants in the future, follow the same pattern by creating new interposer classes that inherit from their respective original classes and add the necessary properties and method overrides.
Limitations and Considerations
-
Manual Declaration Required:
Delphi does not provide a mechanism to automatically interpose all descendants of TComponent. Each interposer class must be manually declared.
-
Inheritance Hierarchy:
Ensure that your interposer classes correctly follow the inheritance hierarchy of the original classes to avoid unexpected behavior.
-
Compiler Optimizations:
Some compiler optimizations or specific VCL behaviors might not work as expected with interposer classes. Thoroughly test your application to ensure stability.
-
Alternative Approaches:
Depending on your project's requirements, consider alternative approaches such as class helpers for adding methods or utilizing composition to associate additional data with components without altering their class definitions.
Final Note
While the interposer technique is powerful for extending or modifying the behavior of existing classes, it requires careful handling to maintain compatibility and stability within your application. By organizing your interposer classes effectively, as demonstrated above, you can streamline the process and make your codebase more maintainable.
If you need further automation or dynamic behavior, you might need to explore more advanced Delphi features or design patterns, keeping in mind the language's inherent limitations regarding class interposition.