Recent

Author Topic: [Solved] Limit component property values to a subset of the standard enum'd list  (Read 6190 times)

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
This is probably a very simple question, but I can't seem to figure it out.

I'm trying to write a new component based upon TCustomMemo, but I want to limit the enumeration of certain properties to only selected values.

Specifically, in the Object Inspector, I want to be able to set:

property ScrollBars ONLY to ssNone, ssHorizontal, or ssAutoHorizontal

Is there a way to accomplish this? Do I need to somehow modify the ScrollBar property editor?

Thanks for any assistance you guys can provide.
« Last Edit: July 16, 2014, 12:40:42 am by jdlinke »
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1269
I've played with trying to create to subsets before.  I don't think you can do it, and it's nothing to do with the IDE Inspector.

I'd like to acheive something like the following, which is what I think you're after as well...

Code: [Select]
Type
    TOption = (oOne, oTwo, oThree, oFour, oFive);

    TOddOptions = Set of TOption = (oOne, oThree, oFive);

I'm not entirely convinced that Pascal supports this...

I actually needed this functionality for the patch in here...
http://bugs.freepascal.org/view.php?id=26404

I wanted to create a subset of Process options.  Instead I ended up having to create my own Set, and manually map my set in code.
Code: [Select]
  TRunOption = (roStderrToOutPut, roNoConsole, roNewConsole);
...
    If roStderrToOutPut in runoptions then
      p.Options := p.Options + [poStderrToOutPut];
    If roNoConsole in runoptions then
      p.Options := p.Options + [poNoConsole];
    If roNewConsole in runoptions then
      p.Options := p.Options + [poNewConsole];
You'll note I'm manually mapping the roXXX options (which are the subset) to their poXXX equivalents...

I think you'll have to do the same...  (Be *very* happy to be told otherwise though)
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

howardpc

  • Hero Member
  • *****
  • Posts: 4144
The difficulty arises from the current design of TCustomMemo, not from any inherent inability of Object Pascal to do what you want.
You would like to be able to override the TCustomMemo.SetScrollBars method to restrict the allowed options to a subset of TScrollStyle (and write a property editor which overrides the display of scrollbar options in the OI to show only the restricted subset).
Unfortunately this cannot be done, since SetScrollBars is not declared as virtual; so it cannot be overridden.

The only option I can see is to create an entirely new component, unrelated to TCustomMemo. It would have to be differently named, but have identical code (copied verbatim) except you would rewrite SetScrollBars to do what you wanted, (or make it virtual and then override it to do what you wanted).

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12770
  • FPC developer.
Afaik only by creating a custom property editor.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4709
  • I like bugs.
The only option I can see is to create an entirely new component, unrelated to TCustomMemo. It would have to be differently named, but have identical code (copied verbatim) except you would rewrite SetScrollBars to do what you wanted, (or make it virtual and then override it to do what you wanted).

Hey, the situation is not so desperate. See:

Code: [Select]
  TjdlinkeMemo = class(TCustomMemo)
  private
    function GetScrollBars: TScrollStyle;
    procedure SetScrollBars(const Value: TScrollStyle);
  published
    property Align;
    ... all published properties from TMemo except for ScrollBars ...
    property WordWrap;
    // and then the new ScrollBars
    property ScrollBars: TScrollStyle read GetScrollBars write SetScrollBars default ssNone;
  end;

implementation

function TjdlinkeMemo.GetScrollBars: TScrollStyle;
begin
  Result := inherited ScrollBars;
end;

procedure TjdlinkeMemo.SetScrollBars(const Value: TScrollStyle);
begin
  if Value in [ssHorizontal, ssAutoHorizontal] then
    inherited ScrollBars := Value
  else
    inherited ScrollBars := ssNone;
end;

It works but the object inspector would still show all values. It would only prevent the wrong value after user's selection.
A proper solution requires a custom property editor as Marcov noted. Thus making SetScrollBars virtual would not help much.
« Last Edit: July 15, 2014, 11:23:14 am by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
Thanks for the guidance everyone.

I think I'll stick with JuhaManninen's recommendation. Not 100% ideal, but it meets my requirements, and I can add a comment to be displayed in the Object Inspector to alert an end-user to the limited set of options.

Thanks again all!
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Based on the code Juha provided a package is attached which includes a custom property editor to get the OI display to correspond with the restricted scrollbar options.
The new component is placed on the Standard page (and has no custom icon), but this can of course be changed.

jdlinke

  • Jr. Member
  • **
  • Posts: 62
  • Just old me
howardpc: You're awesome! Exactly what I needed, and now I have a better idea of how to go about creating modified property editors.

Thanks howardpc, and again, thanks to all of you who responded!
Lazarus 1.2.4 32-bit version on Windows 8.1 64-bit, Windows 7 64-bit, and Windows XP 32-bit

Currently developing TimberLog and the VirtualDBScroll Component Package

 

TinyPortal © 2005-2018