[SOLVED] TCustomcontrol + Label lazarus / 31-LCL ---------------------------------------------------------------------- pcurtis · 2021-11-15 12:14 I am trying to create a simple component that has just a label. So far so good. How do I modify the code so that the label caption shows the component caption or the component name if the component caption is empty at design or run time? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyTestComponent; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, ComCtrls; type   { TMyTestComponent }   TMyTestComponent = class(TCustomControl)  private    fLabel : TLabel;  protected   public    constructor Create(AOwner : TComponent); override;  published    property Caption;  end; procedure Register; implementation constructor TMyTestComponent.Create(AOwner : TComponent);begin  inherited Create(AOwner);  Color := clLime;  fLabel := TLabel.Create(Self);  with fLabel do    begin      Parent := Self;      Left := 30;      Top := 30;    end;          SetInitialBounds(0, 0, 200,100);  ControlStyle := ControlStyle + [csAcceptsControls];end; procedure Register; begin  RegisterComponents('Misc',[TMyTestComponent]);end; end. I've tried to override RealGetText Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TMyTestComponent.RealGetText : TCaption;begin   inherited RealGetText;    Result := 'nil';    fLabel.Caption := Name;end;  and it works. But this fails Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TMyTestComponent.RealGetText : TCaption;begin   inherited RealGetText;  Result := 'nil';  if Caption = '' then  //fails here    fLabel.Caption := Name  else    fLabel.Caption := Caption;end; ---------------------------------------------------------------------- howardpc · 2021-11-15 12:56 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyTestComponent.Paint; begin   if (GetTextLen = 0) then     case Length(Name) of       0: FLabel.Caption := TMyTestComponent.ClassName;       otherwise FLabel.Caption := Name;   end;   inherited Paint; end; ---------------------------------------------------------------------- pcurtis · 2021-11-15 13:57 Doesn't work. Anyway  the logic is wrong. ---------------------------------------------------------------------- wp · 2021-11-15 14:22 Do you really need a TLabel inside the parent control to display the caption? Can't you just draw the text yourself in the Paint method. This is easy since every TCustomControl has a Canvas. ---------------------------------------------------------------------- pcurtis · 2021-11-15 14:31 Good point. I'll look into it. ---------------------------------------------------------------------- pcurtis · 2021-11-16 02:28 I changed the code to this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyGroupbox; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, ComCtrls; type   { TMyGroupbox }   TMyGroupbox = class(TCustomControl)  private    bLoaded : Boolean;  private    fCaption : String;  protected    procedure Paint; override;    function RealGetText : TCaption; override;    procedure RealSetText(const AValue: TCaption); override;  public    constructor Create(AOwner : TComponent); override;  published    property Caption;    property Enabled;    property Font;    property Visible;  end; procedure Register; implementation {$R mygroupbox.res} constructor TMyGroupbox.Create(AOwner : TComponent);begin  inherited Create(AOwner);  SetInitialBounds(0, 0, 200, 104);  ControlStyle := ControlStyle + [csAcceptsControls];end; function TMyGroupbox.RealGetText : TCaption;begin  inherited RealGetText;  Result := fCaption;end; procedure TMyGroupbox.RealSetText(const aValue: TCaption);begin  inherited;   if fCaption <> aValue then    begin    fCaption := aValue;    end;  Paint;end; procedure TMyGroupbox.Paint;var  iHeight : Integer;  iWidth : Integer;begin  inherited Paint;   Canvas.Clear;   iHeight := Canvas.TextHeight(fCaption);  iWidth := Canvas.TextWidth(fCaption);   if Enabled then    begin      Canvas.Pen.Color := clBlack;    end  else    begin      Canvas.Pen.Color := cl3DShadow;    end;   Canvas.Line(0, iHeight div 2, 5 ,iHeight div 2);  Canvas.Line(0, iHeight div 2, 0 ,Height-1);  Canvas.LineTo(Width - 1, Height - 1);  Canvas.LineTo(Width - 1, iHeight div 2);   if fCaption <> '' then    Canvas.LineTo(15 + iWidth, iHeight div 2)  else    Canvas.LineTo(0, iHeight div 2);   Canvas.TextOut(10, 1, fCaption);end; procedure Register; begin  RegisterComponents('Misc',[TMyGroupbox]);end; end. Now I get an error when I try to drop the component on a form "Error setting the name of a component" ---------------------------------------------------------------------- wp · 2021-11-16 08:42 Now that you paint the text yourself why do you need to call RealGetText and RealSetText at all? Simply write a setter for your FCaption field which sets the field and then invalidates the control: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyGroupbox = class(TCustomControl)  private    FCaptionText: String;    procedure SetCaption(const AValue: String);  ...  protected    property Caption: String read FCaptionText write SetCaption;  end; procedure TMyGroupbox.SetCaption(const AValue: String);begin  if AValue = FCaptionText then    exit;  FCaptionText := Value;  Invalidate;end;And, as you can see above, I'd rename "your" FCaption to something else, maybe FCaptionText, to distinguish it from the inherited field. In the Paint method, I'd recomment to avoid fixed rgb colors because dark mode is getting more and more popular, and what looks perfect on a bright screen looks like nothing on a dark screen. Always use the system color, e.g. clWindowText for text and clWindow for background. Most theming systems will render clWindowsText as bright and clWindow as dark for dark mode (not all, though...) ---------------------------------------------------------------------- pcurtis · 2021-11-16 09:23 OK, the code now looks like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyGroupbox; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, ComCtrls; type   { TMyGroupbox }   TMyGroupbox = class(TCustomControl)  private    bLoaded : Boolean;  private    fCaptionText: String;    procedure SetCaption(const AValue: String);  protected    procedure Paint; override;    property Caption: String read FCaptionText write SetCaption;  public    constructor Create(AOwner : TComponent); override;  published//    property Caption; Won't compile with this    property Enabled;    property Font;    property Visible;  end; procedure Register; implementation {$R mygroupbox.res} constructor TMyGroupbox.Create(AOwner : TComponent);begin  inherited Create(AOwner);  SetInitialBounds(0, 0, 200, 104);  ControlStyle := ControlStyle + [csAcceptsControls];end; procedure TMyGroupbox.SetCaption(const AValue: String);begin  if AValue = fCaptionText then    exit;  fCaptionText := aValue;  Invalidate;end; procedure TMyGroupbox.Paint;var  iHeight : Integer;  iWidth : Integer;begin  inherited Paint;   Canvas.Clear;   iHeight := Canvas.TextHeight(fCaptionText);  iWidth := Canvas.TextWidth(fCaptionText);   if Enabled then    begin      Canvas.Pen.Color := clBlack;    end  else    begin      Canvas.Pen.Color := cl3DShadow;    end;   Canvas.Line(0, iHeight div 2, 5 ,iHeight div 2);  Canvas.Line(0, iHeight div 2, 0 ,Height-1);  Canvas.LineTo(Width - 1, Height - 1);  Canvas.LineTo(Width - 1, iHeight div 2);   if fCaptionText <> '' then    Canvas.LineTo(15 + iWidth, iHeight div 2)  else    Canvas.LineTo(0, iHeight div 2);   Canvas.TextOut(10, 1, fCaptionText);end; procedure Register; begin  RegisterComponents('Misc',[TMyGroupbox]);end; end. It compiles but Ive lost the Caption property. That's why I was trying to use RealSet/Gettext so I could intercept changes to thr caption. ---------------------------------------------------------------------- wp · 2021-11-16 09:34 Quote from: pcurtis on November 16, 2021, 10:23:55 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyGroupbox = class(TCustomControl)  protected    property Caption: String read FCaptionText write SetCaption;  end; Me stupid. It should be in the "published" section, of course. ---------------------------------------------------------------------- pcurtis · 2021-11-16 10:13 @wp - Now we're getting somewhere. I didn't realize you can change the Setters Getters for an existing property. Just one last thing Where would I set the initial value (Name) of the Caption property? In Loaded override? ---------------------------------------------------------------------- wp · 2021-11-16 10:33 Quote from: pcurtis on November 16, 2021, 11:13:46 am@wp - Now we're getting somewhere. I didn't realize you can change the Setters Getters for an existing property. Just one last thing Where would I set the initial value (Name) of the Caption property? In Loaded override? Properties are initialized in the constructor of a class. The Name property, by default, is empty, but designtime code (property editors, designers, class editors, - I don't know exactly) gives it the class name plus an appended number at designtime. The Name also has the ugly requirement that it must be unique. - I never touch the Name and leave it empty. ---------------------------------------------------------------------- pcurtis · 2021-11-16 10:50 I don't want to change it, just read it when it's available. ---------------------------------------------------------------------- wp · 2021-11-16 11:36 You only want to use the Caption in the standard way so that it contains the component Name initially at designtime? If this is correct there's nothing you should do yourself regarding the caption and the name. Remove all the RealCaption stuff and remove the FCaptionText property that I introduced, and in Paint use the inherited Caption for output. If the component does not repaint itself automatically whenever the inherited Caption is changed you should override the inherited setter SetCaption so that it calls Invalidate for the component. Untested: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyGroupbox = class(TCustomControl)  private   protected    procedure Paint; override;    procedure SetCaption(const AValue: String); override;  // not necessarily needed  public    constructor Create(AOwner : TComponent); override;  published    property Caption;   // etc  end; procedure Register; implementation { Use this only when the component does not automatically repaint when the Caption is changed. }procedure TMyGroupbox.SetCaption(const AValue: String);begin  if AValue = inherited Caption then    exit;  inherited Caption := AValue;  Invalidate;end; procedure TMyGroupbox.Paint;var  iHeight : Integer;  iWidth : Integer;begin  inherited Paint;   Canvas.Clear;   iHeight := Canvas.TextHeight(Caption);  iWidth := Canvas.TextWidth(Caption);   if Enabled then    begin      Canvas.Pen.Color := clBlack;    end  else    begin      Canvas.Pen.Color := cl3DShadow;    end;   Canvas.Line(0, iHeight div 2, 5 ,iHeight div 2);  Canvas.Line(0, iHeight div 2, 0 ,Height-1);  Canvas.LineTo(Width - 1, Height - 1);  Canvas.LineTo(Width - 1, iHeight div 2);   if Caption <> '' then    Canvas.LineTo(15 + iWidth, iHeight div 2)  else    Canvas.LineTo(0, iHeight div 2);   Canvas.TextOut(10, 1, Caption);end; // etc. ---------------------------------------------------------------------- pcurtis · 2021-11-16 12:07 Thank you so much wp