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

Post

Posted
Rating:
#1 (In Topic #1119)
Regular
JumpyVB is in the usergroup ‘Regular’
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:

Code (gambas)

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

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?
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
Uhmmm… :? …probably creating a specific exportable Class that you can call up in an application.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 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.

Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
JumpyVB is in the usergroup ‘Regular’

thatbruce said

Somewhere 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

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

Then I came up with this for my RichListBox class:

Code (gambas)

  1.  
  2. Public Sub GridView_Data(Row As Integer, (Column) As Integer)
  3.   With Super.$hView.Data
  4.     Try .RichText = Super.$aText[Row]
  5.     .WordWrap = Super.$bWrap
  6.     If Super.$hView.Rows[Row].Selected Then
  7.       .Background = Color.SelectedBackground
  8.       .Foreground = Color.SelectedForeground
  9.     Endif
But I get the following error message:
Unknown symbol '$hView' in class 'Container'
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 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.

Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
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..

Code (gambas)

  1. Public Sub _GetGrid() As GridView
  2.  
  3.   Return $hView
  4.  
  5.  

ListBox does not
but examining the code we see that $hView points to the GridView that is it's Proxy

Code (gambas)

  1. Public Sub _new()
  2.  
  3.   $hView = New GridView(Me) As "GridView"
  4.   $hView.Columns.Count = 1
  5.   $hView.Mode = Select.Single
  6.   $hView.Grid = False
  7.   $hView._DoNotDrawSelection = True
  8.   $hView.ScrollBar = Scroll.Vertical
  9.  
  10.   Me.Proxy = $hView
  11.  
  12.   UpdateLayout
  13.  
  14.  
  15.  


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


Code (gambas)

  1. ' Gambas class file
  2.  
  3.  
  4.  
  5. Public Sub GridView_Data(Row As Integer, (Column) As Integer)
  6.  
  7.   Super.GridView_Data(Row, Column)  ' Run the parent GridView_Data() event
  8.  
  9.   Dim $hView As GridView = Me.Proxy  ' get the gridview object
  10.  
  11.   $hView.Data.RichText = $hView.Data.Text  ' move Text to RichText
  12.   $hView.Data.Text = ""
  13.  
  14.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
JumpyVB is in the usergroup ‘Regular’

BruceSteers said

Inheritance becomes tricky when you want to access internal private data.
Good to know. Thank you for this information.

BruceSteers said

Code (gambas)

  1. ' Gambas class file
  2. Public Sub GridView_Data(Row As Integer, (Column) As Integer)
  3.   Super.GridView_Data(Row, Column)  ' Run the parent GridView_Data() event
  4.   Dim $hView As GridView = Me.Proxy  ' get the gridview object
  5.   $hView.Data.RichText = $hView.Data.Text  ' move Text to RichText
  6.   $hView.Data.Text = ""
Nice work around.
Online now: No Back to the top
1 guest and 0 members have just viewed this.