Recent

Author Topic: [SOLVED] Issues with subclass creation  (Read 6368 times)

pusuni

  • Jr. Member
  • **
  • Posts: 72
[SOLVED] Issues with subclass creation
« on: September 23, 2014, 03:57:54 pm »
Hi! I'm trying to make a new  control (class) derivated from TCustomPanel class. A easy and simplified code  version of this class   is:

Code: [Select]
unit Test;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, LMessages;

type
  TTest = class(TCustomPanel)
  private
    { Private declarations }
    ControlOne              : TEdit;
    ControlTwo              : TLabel;

  protected
    { Protected declarations }
  public
    { Public declarations }
      constructor Create (AOwner : TComponent) ;   override;
      destructor  Destroy;                         override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Standard',[TTest]);
end;


constructor TTest.Create (AOwner : TComponent) ;
begin
  inherited Create (AOwner);
  Height := 136;
  Width := 176;
  ClientHeight := 132;
  ClientWidth := 172;
  Constraints.MaxHeight:=136;
  Constraints.MaxWidth :=176;
  Constraints.MinHeight:=136;
  Constraints.MinWidth :=176;
  CAPTION :='';

  ControlOne:= TEdit.Create(Self);
  ControlOne.Parent:= Self;
  ControlOne.SetSubComponent(true);
  ControlOne.ControlStyle := ControlOne.ControlStyle - [csNoDesignSelectable];
  ControlOne.Name := 'Input1';
  ControlOne.Text := '1';
  ControlOne.Left := 36;
  ControlOne.Height := 23;
  ControlOne.Top := 20;
  ControlOne.Width := 130;

   ControlTwo := TLabel.Create(Self);
   ControlTwo.Parent:= Self;
   ControlTwo.SetSubComponent(true);
   ControlTwo.ControlStyle := ControlTwo.ControlStyle - [csNoDesignSelectable];
   ControlTwo.Name := 'Label1';
   ControlTwo.Caption := 'Label1';

end;

destructor  TTest.Destroy;
begin
   ControlTwo.Free;
   ControlOne.Free;
   inherited Destroy;
end;

end.

That code compile OK and I can create a new package with this new class: TTest. I can create a new form with this new control, but, I get a problem.

TCustomPanel has a default "Caption text" , I don't want that text. Inside  the constructor,  I added the following code to delete that text:

  CAPTION :='';

but the text is always on . How can I delete that text?

Thanks in advanced
« Last Edit: September 25, 2014, 09:51:30 am by pusuni »

Blaazen

  • Hero Member
  • *****
  • Posts: 3239
  • POKE 54296,15
    • Eye-Candy Controls
Re: Issues with subclass creation
« Reply #1 on: September 23, 2014, 04:26:29 pm »
It works well on Linux (both GTk2 and Qt4). Also Wine is OK. With or without the line:
Code: [Select]
CAPTION :='';
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Issues with subclass creation
« Reply #2 on: September 23, 2014, 04:27:27 pm »
I'm guessing that at the point in the creation scheme the underlying widgetset instance of TPanel hasn't been created, so TControl.SetText (which calls TControl.RealSetText) has no handle to send the new caption message to.  (only wildly guessing).

For your reference, I found these notes in Controls.pp
Code: [Select]
{* Note on TControl.Caption
 * The VCL implementation relies on the virtual Get/SetTextBuf to
 * exchange text between widgets and VCL. This means a lot of
 * (unnecessary) text copies.
 * The LCL uses strings for exchanging text (more efficient).
 * To maintain VCL compatibility, the virtual RealGet/SetText is
 * introduced. These functions interface with the LCLInterface. The
 * default Get/SetTextbuf implementation calls the RealGet/SetText.
 * As long as the Get/SetTextBuf isn't overridden Get/SetText
 * calls RealGet/SetText to avoid PChar copying.
 * To keep things optimal, LCL implementations should always
 * override RealGet/SetText. Get/SetTextBuf is only kept for
 * compatibility.
 }     
   

I simply cannot find where Caption is defined.  So for now, try just overriding RealSetText, and always set ''
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Issues with subclass creation
« Reply #3 on: September 23, 2014, 04:37:06 pm »
I'm no pro in making designtime components, but:

1. make sure that after the constructor change your recompile everything (package and thus Lazarus)
2. existing dropped ttest components will already have the caption in their streaming, and are thus unchanged. Drop new ones to test.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Issues with subclass creation
« Reply #4 on: September 23, 2014, 04:42:05 pm »
@Mike.Cornflake

Or the default value caption is set after the control creation (which is usually the case for any registered control, during the streaming). So emptying it in the constructor doesn't have any visible effect.

Anyway, overriding the set text method seems also to be the correct approach to me.
« Last Edit: September 23, 2014, 05:21:05 pm by ChrisF »

Blaazen

  • Hero Member
  • *****
  • Posts: 3239
  • POKE 54296,15
    • Eye-Candy Controls
Re: Issues with subclass creation
« Reply #5 on: September 23, 2014, 04:51:25 pm »
You can try:
Code: [Select]
  ControlStyle := ControlStyle - [csSetCaption];
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Issues with subclass creation
« Reply #6 on: September 23, 2014, 06:05:41 pm »
AFAIK csSetCaption applies to design-time behaviour, not runtime behaviour.
Setting Caption to EmptyStr in the constructor or overriding RealSetText and doing the same should both achieve the desired effect. If not, I think Marco has explained why already dropped components show 'old' behaviour.
The TTest as written shows no Caption here (Win32).

Blaazen

  • Hero Member
  • *****
  • Posts: 3239
  • POKE 54296,15
    • Eye-Candy Controls
Re: Issues with subclass creation
« Reply #7 on: September 23, 2014, 07:01:37 pm »
Quote
AFAIK csSetCaption applies to design-time behaviour, not runtime behaviour.
It's not clear from OP if it is design-time or run-time issue. He wrote:
Quote
That code compile OK and I can create a new package with this new class: TTest. I can create a new form with this new control, but, I get a problem.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

pusuni

  • Jr. Member
  • **
  • Posts: 72
Re: Issues with subclass creation
« Reply #8 on: September 24, 2014, 11:31:20 am »
Hi everyone!!

Hi Howard!!

Sorry for not giving all the information: the problem it is design-time and

You can try:
Code: [Select]
  ControlStyle := ControlStyle - [csSetCaption];

that fixed my issue.

Thanks at all




pusuni

  • Jr. Member
  • **
  • Posts: 72
Re: Issues with subclass creation
« Reply #9 on: September 24, 2014, 11:37:08 am »
the default value caption is set after the control creation (which is usually the case for any registered control, during the streaming). So emptying it in the constructor doesn't have any visible effect.


I didn't know it but that's the point!!

Thanks, ChrisF
« Last Edit: September 24, 2014, 11:39:34 am by pusuni »

pusuni

  • Jr. Member
  • **
  • Posts: 72
Re: Issues with subclass creation
« Reply #10 on: September 24, 2014, 11:47:08 am »
I simply cannot find where Caption is defined.  So for now, try just overriding RealSetText, and always set ''

You are right, but if you compile and run, there is a caption in design time (look at the picture): http://subefotos.com/ver/?9cb641d81c449363ae6ff661cf5f3541o.jpg

Anyway, the Blaazen point has fixed my problem

Thanks

 

TinyPortal © 2005-2018