Best method for making a small modification to a gambas builtin class file

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Best method for making a small modification to a gambas builtin class file

Post by JumpyVB »

I want to use Richt Text in a ListBox control.

To achieve this I have copied the contents of /home/user/gambas-3.18.2/comp/src/gb.gui.base/.src/ListBox/ListBox.class to my project with a new class name RichListBox. I only changed one line of code in "Public Sub GridView_Data" like so:

Public Sub GridView_Data(Row As Integer, (Column) As Integer)
  With $hView.Data
    Try .RichText = $aText[Row] ' This is the one and only line I changed
    .WordWrap = $bWrap
    If $hView.Rows[Row].Selected Then
      .Background = Color.SelectedBackground
      .Foreground = Color.SelectedForeground
    Endif
  End With
End


Now I have the desired behaviour. But I was wondering is there a better way to make such a small change to an existing class - one that doesn't involve copy pasting all the code from the original class?
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Best method for making a small modification to a gambas builtin class file

Post by vuott »

Uhmmm... :? ...probably creating a specific exportable Class that you can call up in an application.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: Best method for making a small modification to a gambas builtin class file

Post by thatbruce »

Somewhere in the help is the description of how to override classes in your application. Basically, just create a class in your project with the same name as the one you want to "extend" and add the relevant stuff. I frequently use it to add additional properties to a native class. There are some things that you need to understand though, so I wish I could give you that wiki reference, but damned if I can find it. For simple things it is the quickest, easiest and most efficient way to do things to native classes that just "lack something I need". I need to tell you that if you want to override event handlers you may need to understand STOP EVENT.
p.s. It also depends if you just have a local single project that needs to "add something", overriding the class internally in that project is the simplest (believe me!) way to do it. Otherwise if you need that additional feature then vuott's, somewhat cryptic reference to creating custom components is better.
Have you ever noticed that software is never advertised using the adjective "spreadable".
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Re: Best method for making a small modification to a gambas builtin class file

Post by JumpyVB »

thatbruce wrote: Saturday 28th October 2023 6:49amSomewhere in the help is the description of how to override classes in your application.
Yeas, extend and override sound like something I am after.

I like to use a google search for the gambas wiki with target site specified like this:

Code: Select all

class override site:https://gambaswiki.org
I was able to find this https://gambaswiki.org/wiki/doc/object-model#t17 And this https://gambaswiki.org/wiki/lang/inherits And this https://gambaswiki.org/wiki/lang/super

Then I came up with this for my RichListBox class:
Export
Inherits ListBox

Public Sub GridView_Data(Row As Integer, (Column) As Integer)
  With Super.$hView.Data
    Try .RichText = Super.$aText[Row]
    .WordWrap = Super.$bWrap
    If Super.$hView.Rows[Row].Selected Then
      .Background = Color.SelectedBackground
      .Foreground = Color.SelectedForeground
    Endif
  End With
End
But I get the following error message:
Unknown symbol '$hView' in class 'Container'
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: Best method for making a small modification to a gambas builtin class file

Post by thatbruce »

ON this line?
With Super.$hView.Data

I doubt that "$hView" is a public attribute of the ListBox. Seek another attribute that provides what yoe need.
Have you ever noticed that software is never advertised using the adjective "spreadable".
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Best method for making a small modification to a gambas builtin class file

Post by BruceSteers »

Inheritance becomes tricky when you want to access internal private data.

$hView is Private to ListBox.class so your inheriting class does not see it.
Sometimes the inherited class may have a hidden function that accesses what you want like this..
Public Sub _GetGrid() As GridView

  Return $hView

End


ListBox does not
but examining the code we see that $hView points to the GridView that is it's Proxy
Public Sub _new()
  
  $hView = New GridView(Me) As "GridView"
  $hView.Columns.Count = 1
  $hView.Mode = Select.Single
  $hView.Grid = False
  $hView._DoNotDrawSelection = True
  $hView.ScrollBar = Scroll.Vertical
  
  Me.Proxy = $hView
  
  UpdateLayout
  
End
  



So we can see that $hView is the first child of the ListBox and also the Proxy so we can access it like this...


' Gambas class file

Export

Inherits ListBox

Public Sub GridView_Data(Row As Integer, (Column) As Integer)

  Super.GridView_Data(Row, Column)  ' Run the parent GridView_Data() event

  Dim $hView As GridView = Me.Proxy  ' get the gridview object

  $hView.Data.RichText = $hView.Data.Text  ' move Text to RichText
  $hView.Data.Text = ""

End
If at first you don't succeed , try doing something differently.
BruceS
JumpyVB
Posts: 75
Joined: Friday 11th September 2020 9:09am

Re: Best method for making a small modification to a gambas builtin class file

Post by JumpyVB »

BruceSteers wrote: Sunday 29th October 2023 11:47amInheritance becomes tricky when you want to access internal private data.
Good to know. Thank you for this information.
BruceSteers wrote: Sunday 29th October 2023 11:47am
' Gambas class file
Export
Inherits ListBox
Public Sub GridView_Data(Row As Integer, (Column) As Integer)
  Super.GridView_Data(Row, Column)  ' Run the parent GridView_Data() event
  Dim $hView As GridView = Me.Proxy  ' get the gridview object
  $hView.Data.RichText = $hView.Data.Text  ' move Text to RichText
  $hView.Data.Text = ""
End
Nice work around.
Post Reply