In Delphi, interposer classes allow you to override or extend the behavior of existing classes without modifying the original class definitions. However, Delphi's static type system requires that each interposer class be explicitly defined for each component you wish to interpose. Unfortunately, there isn't a built-in mechanism to automatically interpose all TComponent descendants dynamically. Nevertheless, you can enhance your interposer_class unit to make it more organized and efficient by following these best practices:
-
Create a Base Interposer Class:
Define a base interposer class that encapsulates the additional properties (ORMRec and ORMRecField). This base class can then be inherited by all other interposer classes, ensuring consistency and reducing code duplication.
-
Use a Common Method for CreateParams:
Implement a common method in the base interposer class to handle the CreateParams modification. Each descendant interposer class can call this method within their overridden CreateParams, ensuring a uniform approach while allowing for class-specific adjustments if necessary.
-
Organize Interposer Classes:
Structure your unit in a way that groups related interposer classes together, making the codebase easier to navigate and maintain.
Below is an enhanced version of your interposer_class unit incorporating these improvements:
unit InterposerClass;
interface
uses
Vcl.Forms, Vcl.Controls, CommCtrl, Messages, sPanel, sEdit, sPageControl,
System.Classes, AdvCGrid, Uni, pixel_classes, ORM_utils;
{$TYPEINFO ON}
type
// Base Interposer Class for TComponent descendants
TInterposerBase = class(TComponent)
private
FORMRec: TQryRecInfo;
FORMRecField: string;
public
property ORMRec: TQryRecInfo read FORMRec write FORMRec;
property ORMRecField: string read FORMRecField write FORMRecField;
protected
// Common method to adjust CreateParams
procedure AdjustCreateParams(var Params: TCreateParams; const ClassNameOverride: string); virtual;
end;
// Implement the base interposer method
procedure TInterposerBase.AdjustCreateParams(var Params: TCreateParams; const ClassNameOverride: string);
begin
inherited CreateParams(Params);
if ClassNameOverride <> '' then
StrPCopy(Params.WinClassName, PChar(ClassNameOverride));
end;
// Interpose TForm with the additional properties and overridden CreateParams
TForm = class(Vcl.Forms.TForm)
private
// Inherit properties from TInterposerBase without duplicating fields
function GetORMRec: TQryRecInfo;
procedure SetORMRec(const Value: TQryRecInfo);
function GetORMRecField: string;
procedure SetORMRecField(const Value: string);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property ORMRec: TQryRecInfo read GetORMRec write SetORMRec;
property ORMRecField: string read GetORMRecField write SetORMRecField;
end;
// Repeat similar structure for other components
TsPanel = class(sPanel.TsPanel)
private
function GetORMRec: TQryRecInfo;
procedure SetORMRec(const Value: TQryRecInfo);
function GetORMRecField: string;
procedure SetORMRecField(const Value: string);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
property ORMRec: TQryRecInfo read GetORMRec write SetORMRec;
property ORMRecField: string read GetORMRecField write SetORMRecField;
end;
TsEdit = class(sEdit.TsEdit)
private
function GetORMRec: TQryRecInfo;
procedure SetORMRec(const Value: TQryRecInfo);
function GetORMRecField: string;
procedure SetORMRecField(const Value: string);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
property ORMRec: TQryRecInfo read GetORMRec write SetORMRec;
property ORMRecField: string read GetORMRecField write SetORMRecField;
end;
TsPageControl = class(sPageControl.TsPageControl)
private
function GetORMRec: TQryRecInfo;
procedure SetORMRec(const Value: TQryRecInfo);
function GetORMRecField: string;
procedure SetORMRecField(const Value: string);
public
property ORMRec: TQryRecInfo read GetORMRec write SetORMRec;
property ORMRecField: string read GetORMRecField write SetORMRecField;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure TCMAdjustRect(var Msg: TMessage); message TCM_ADJUSTRECT;
end;
TAdvColumnGrid = class(AdvCGrid.TAdvColumnGrid)
private
FByPassAF: TArray<Boolean>;
function GetORMRec: TQryRecInfo;
procedure SetORMRec(const Value: TQryRecInfo);
function GetORMRecField: string;
procedure SetORMRecField(const Value: 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 GetORMRec write SetORMRec;
property ORMRecField: string read GetORMRecField write SetORMRecField;
property Cellz[ACol, ARow: Integer]: TZString read GetCellz write SetCellz;
property BypassAutoFit[ACol: Integer]: Boolean read GetBypassAF write SetBypassAF;
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;
{ TInterposerBase }
procedure TInterposerBase.AdjustCreateParams(var Params: TCreateParams; const ClassNameOverride: string);
begin
StrPCopy(Params.WinClassName, PChar(ClassNameOverride));
end;
{ TForm }
function TForm.GetORMRec: TQryRecInfo;
begin
// Implement a mechanism to store and retrieve ORMRec, possibly using a TInterposerBase instance
// For simplicity, directly accessing the fields is shown here
Result := TInterposerBase(Self).ORMRec;
end;
procedure TForm.SetORMRec(const Value: TQryRecInfo);
begin
TInterposerBase(Self).ORMRec := Value;
end;
function TForm.GetORMRecField: string;
begin
Result := TInterposerBase(Self).ORMRecField;
end;
procedure TForm.SetORMRecField(const Value: string);
begin
TInterposerBase(Self).ORMRecField := Value;
end;
constructor TForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Initialize ORMRec or other properties if needed
end;
destructor TForm.Destroy;
begin
// Clean up ORMRec or other properties if needed
inherited Destroy;
end;
procedure TForm.CreateParams(var Params: TCreateParams);
begin
AdjustCreateParams(Params, 'ZPro');
end;
{ TsPanel }
function TsPanel.GetORMRec: TQryRecInfo;
begin
Result := TInterposerBase(Self).ORMRec;
end;
procedure TsPanel.SetORMRec(const Value: TQryRecInfo);
begin
TInterposerBase(Self).ORMRec := Value;
end;
function TsPanel.GetORMRecField: string;
begin
Result := TInterposerBase(Self).ORMRecField;
end;
procedure TsPanel.SetORMRecField(const Value: string);
begin
TInterposerBase(Self).ORMRecField := Value;
end;
procedure TsPanel.CreateParams(var Params: TCreateParams);
begin
AdjustCreateParams(Params, 'Painel');
end;
{ TsEdit }
function TsEdit.GetORMRec: TQryRecInfo;
begin
Result := TInterposerBase(Self).ORMRec;
end;
procedure TsEdit.SetORMRec(const Value: TQryRecInfo);
begin
TInterposerBase(Self).ORMRec := Value;
end;
function TsEdit.GetORMRecField: string;
begin
Result := TInterposerBase(Self).ORMRecField;
end;
procedure TsEdit.SetORMRecField(const Value: string);
begin
TInterposerBase(Self).ORMRecField := Value;
end;
procedure TsEdit.CreateParams(var Params: TCreateParams);
begin
AdjustCreateParams(Params, 'Editor');
end;
{ TsPageControl }
function TsPageControl.GetORMRec: TQryRecInfo;
begin
Result := TInterposerBase(Self).ORMRec;
end;
procedure TsPageControl.SetORMRec(const Value: TQryRecInfo);
begin
TInterposerBase(Self).ORMRec := Value;
end;
function TsPageControl.GetORMRecField: string;
begin
Result := TInterposerBase(Self).ORMRecField;
end;
procedure TsPageControl.SetORMRecField(const Value: string);
begin
TInterposerBase(Self).ORMRecField := Value;
end;
procedure TsPageControl.CreateParams(var Params: TCreateParams);
begin
AdjustCreateParams(Params, '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;
end;
end;
end;
{ TAdvColumnGrid }
function TAdvColumnGrid.GetORMRec: TQryRecInfo;
begin
Result := TInterposerBase(Self).ORMRec;
end;
procedure TAdvColumnGrid.SetORMRec(const Value: TQryRecInfo);
begin
TInterposerBase(Self).ORMRec := Value;
end;
function TAdvColumnGrid.GetORMRecField: string;
begin
Result := TInterposerBase(Self).ORMRecField;
end;
procedure TAdvColumnGrid.SetORMRecField(const Value: string);
begin
TInterposerBase(Self).ORMRecField := Value;
end;
procedure TAdvColumnGrid.CreateParams(var Params: TCreateParams);
begin
AdjustCreateParams(Params, '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
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;
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;
end.
Explanation of Enhancements
-
Base Interposer Class (TInterposerBase):
- Purpose: Encapsulates the additional properties
ORMRec and ORMRecField to avoid duplicating them across all interposer classes.
- Method
AdjustCreateParams: Provides a common method to modify the WinClassName in CreateParams. This ensures consistency and reduces code duplication.
-
Interposer Classes (TForm, TsPanel, TsEdit, TsPageControl, TAdvColumnGrid):
- Property Implementation: Instead of declaring separate fields for
ORMRec and ORMRecField in each interposer class, the properties delegate to the TInterposerBase's fields. This approach centralizes property management.
- Overridden
CreateParams: Each interposer class calls the AdjustCreateParams method from the base class, passing the specific WinClassName. This ensures that all interposer classes modify their window class names uniformly.
- Constructor and Destructor: Provided as overrides to facilitate any future initialization or cleanup related to the interposed properties. Currently, they call
inherited, but you can extend them as needed.
-
Organized Code Structure:
- Grouping Related Code: Each interposer class's implementation is grouped together, making the unit easier to read and maintain.
- Consistent Naming Conventions: Ensures that properties and methods follow a uniform naming pattern, enhancing code clarity.
Important Considerations
-
Manual Interposer Class Definitions: Despite the organized structure, you still need to manually define an interposer class for each TComponent descendant you wish to modify. Delphi doesn't support automatic interposing for all subclasses.
-
Limitations of Interposer Classes:
- Name Clashes: Ensure that the
interposer_class unit is placed after the original unit in the uses clause wherever you intend to use these interposer classes. This ensures that your interposer classes are the ones being instantiated.
- Design-Time Support: Some IDE features might not recognize interposer classes seamlessly, potentially affecting form designers or other tools.
-
Alternative Approaches:
- Class Helpers: While class helpers can extend classes with new methods, they cannot add new fields or override existing methods like
CreateParams. Hence, they aren't suitable for this particular use case.
- Composition Over Inheritance: In scenarios where inheritance becomes cumbersome, consider using composition to include additional functionality. However, this would require changes in how you use your components throughout the application.
Automating Interposer Class Creation
To further streamline the process of creating interposer classes, consider using code generation tools or Delphi macros. While Delphi doesn't natively support macros, you can leverage external scripting languages (like Python or Perl) to generate the necessary boilerplate code. This approach reduces manual effort and minimizes the risk of inconsistencies across interposer classes.
Final Thoughts
While Delphi's static type system imposes certain limitations on dynamically interposing classes, thoughtful structuring of your interposer classes can lead to a maintainable and organized codebase. By centralizing shared functionalities and minimizing code duplication, you enhance both the readability and scalability of your application.