Recent

Author Topic: Subclassing Widgets  (Read 7169 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Subclassing Widgets
« on: February 26, 2011, 04:43:10 pm »
I am migrating from REALbasic where I had a small library of widgets that I subclassed to modify their behavior. These could be imported and used in the layout.

In Lazarus I don't see a mechanism to do this. Instead I have started to write wrapper (if that is the correct term) classes that are initialized with widgets in TForm.Create. A simple example is adding a method to select all rows in a TListView. Something like:

Code: [Select]
constructor TListForm1.Create(AOwner: TComponent);
begin
  inherited;
  fListViewWrapper := TListViewWrapper.Create(ListView1);
end;

procedure TListForm1.DoSelectAll;
begin
   fListViewWrapper.SelectAll;
end;

This works fine, but I'm wondering if I'm overlooking a more obvious solution. Any comments appreciated.

Cheers,
Frederick
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11137
  • Debugger - SynEdit - and more
    • wiki
Re: Subclassing Widgets
« Reply #1 on: February 26, 2011, 05:25:17 pm »
Create a new Package.

then in the package window, press "Add" and choose "new component"

JD

  • Hero Member
  • *****
  • Posts: 1896
Re: Subclassing Widgets
« Reply #2 on: February 26, 2011, 05:50:53 pm »
I am migrating from REALbasic where I had a small library of widgets that I subclassed to modify their behavior. These could be imported and used in the layout.
        ........................
        ........................

Cheers,
Frederick

Hi Frederick, I'm interested to know why you are migrating from RealBasic to Lazarus. Is it a "cost" thing or is it a functionality thing or maybe even something else? I played a little with RealBasic but I had already discovered Lazarus so I did not take it any further.

Cheers,

JD
Linux Mint - Lazarus 4.0/FPC 3.2.2,
Windows - Lazarus 4.0/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Subclassing Widgets
« Reply #3 on: February 26, 2011, 06:22:53 pm »
Create a new Package.

then in the package window, press "Add" and choose "new component"

Wow! That is exactly what I was looking for.

I have a lot to learn, thanks Martin.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Subclassing Widgets
« Reply #4 on: February 26, 2011, 07:07:02 pm »
Quote
Hi Frederick, I'm interested to know why you are migrating from RealBasic to Lazarus. Is it a "cost" thing or is it a functionality thing or maybe even something else? I played a little with RealBasic but I had already discovered Lazarus so I did not take it any further.

Cheers,

JD

I used to use TurboPascal way back, and then migrated through various C++ packages on both Windows and Mac, but really needed a simple cross-platform tool. I use Mac exclusively myself, but need to support Windows. REALbasic is honestly exceptional for cross-platform development, and I have been using it pretty much since it first came out.

Money is not really the issue, as I can use an academic license, but the cost did get me looking for alternatives. They become more expensive all the time, and they went to a subscription model. You can only install it on two computers which have to be connected to the internet, so if I have it installed on two computers, I can't put it on my laptop when I travel. Then there is the question of plugins, which can be the only way to optimize some functions. For most things you don't notice, but FPC's code is much more highly optimized when I need speed. Plugins are a pain to write, and require frequent updates.

I like the idea of using open source code, and since Lazarus is based on Dephi it has a large potential user base, and a large and active community of developers. It terms of code availability, there seem to be a large number of sources. If I look hard enough, I seem to be able to find just about anything I need. I am very impressed, although I have yet to attempt cross-compiling.

I did try Python for a few months, and thought that seemed promising, but getting working code to my users in a simple package seemed extremely difficult.

Last, I'm always a little embarrassed to admit I code in BASIC, never cared much for C++, and have always had a soft spot for Pascal.

Cheers
« Last Edit: February 26, 2011, 07:09:35 pm by Frederick »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Subclassing Widgets
« Reply #5 on: February 26, 2011, 08:13:16 pm »
A somewhat related question:

When I select an item in a TListView in code is there a simple way to prevent OnClick from being called?

For example if I write a SelectAll procedure I don't want it to fire for every item. A flag works, but then I have to remember to check it each time.

“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

vvzh

  • Jr. Member
  • **
  • Posts: 58
Re: Subclassing Widgets
« Reply #6 on: February 26, 2011, 09:58:17 pm »
You can remove and then restore event handler:
Code: [Select]
procedure TListViewWrapper.SelectAll;
var
  ClickEventHandler: TNotifyEvent;
begin
  ClickEventHandler := @FActualListView.OnClick;
  { "@" above is required in objfpc mode, omit it if you are in delphi mode }
  FActualListView.OnClick := nil;

  // your code here

  FActualListView.OnClick := @ClickEventHandler;
end;
If you have a single event handler you can even get rid of temporary variable.
« Last Edit: February 26, 2011, 10:06:40 pm by vvzh »

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Subclassing Widgets
« Reply #7 on: February 26, 2011, 11:28:19 pm »
Excellent, works like a charm.

Thanks vvzh!
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

 

TinyPortal © 2005-2018