Automatic change of Row Height with WordWrap ?

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Automatic change of Row Height with WordWrap ?

Post by cogier »

1/. The reason the panel you wanted did not work is that you needed to 'Arrange' it so the GridView could expand.
2/. It took some experimentation, but I think you will find that the cells will now auto adjust.
3/. The .Font.Underline works for me.

Image

Here is the modified code: -
' Gambas class file

Public MyPanel As Panel     'Something for later ...
Public MyGrid As GridView
Public MyButton As Button
Public Text$ As String
Public Edit$ As New String[20]
Public Number As Integer

Public Sub Form_Open()

  With Me
    .Center
    .Height = 500
    .Width = 400
    .Arrangement = Arrange.Vertical
    .Padding = 50
  End With

  With MyPanel = New Panel(Me) As "MyPanel"
    'Trying to use a panel and placing MyGrid on it
    'to create a border around MyGrid. Didn't work ...
    .Border = Border.Etched     ''***************************
    .Expand = True
    .padding = 2
    .Arrangement = Arrange.Fill ''***************************
    .Background = &H00FF00FF&
  End With

  With MyGrid = New GridView(MyPanel) As "MyGrid"  'New GridView(Me.MyPanel) ???
    .rows.Count = 20
    .columns.Count = 1
    .ScrollBar = False
    .NoTabFocus = True
    .expand = True
    .Background = &HFFFFFFFF&
    .border = True
    .Grid = False
    .Padding = 6
  End With

  With MyButton = New Button(Me) As "MyButton"
    .NoTabFocus = True
    .width = 60
    .height = 20
    .text = "Click to activate grid"

  End With

End

Public Sub MyButton_click()

  With MyGrid
    Number = 0
    .[0, 0].Alignment = Align.Center  'This takes care of the 'no center' HTML problem
    .[0, 0].Font = Font["Arial,16,Bold"]
    .[0, 0].RichText = GetText()    'RichText can be used, even if there's no HTML markup
    .Rows[0].Height = -1           ''***************************

    Number = 1
    .[1, 0].Font = Font["Comic sans,8"]
    .[1, 0].WordWrap = True
    .[1, 0].RichText = GetText()
    .Rows[1].Height = -1           ''***************************

    Number = 2
    .[2, 0].Font = Font["Arial,12"]
    .[2, 0].Font.Underline = True   'This doesn't seem to work, but doesn't produce an error either
    .[2, 0].WordWrap = True
    .[2, 0].RichText = GetText()
    .Rows[2].Height = -1           ''***************************

    Number = 3
    .[3, 0].Font = Font["Times,9"]
    .[3, 0].WordWrap = True
    .[3, 0].RichText = GetText()
    .Rows[3].Height = -1           ''***************************
  End With

End

Public Sub GetText() As String

  'The data to be used in the GridView will be read from an external file.

  Edit$[0] = "Welcome"
  Edit$[1] = "<b><i>This line uses HTML Bold & Italic</i></b><br>and switches them of in the next one</br>"
  Edit$[2] = "Lorem ipsum ... Yeah, I know ..."
  Edit$[3] = "<i>And now let's see what happens with some very long text.</i><br>" "</br><br><b>WordWrap does work, but it would be ideal if the row's height could change with it.</b></br>" "</br><br><b>WordWrap does work, but it would be ideal if the row's height could change with it.</b></br>" "</br><br><b>WordWrap does work, but it would be ideal if the row's height could change with it.</b></br>"

  Text$ = Edit$[Number]
  Return Text$

End

Public Sub MyGrid_Click()

  MyGrid.Grid = Not MyGrid.Grid

  ' If MyGrid.Grid = False Then
  '   MyGrid.Grid = True
  ' Else
  '   MyGrid.Grid = False
  ' End If

End
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Automatic change of Row Height with WordWrap ?

Post by Doctor Watson »

Well I’ll be damned! All hail Cougier!
Let’s go back to the very beginning of this topic:
The solution would be that Rows[x].Height changes automatically when any cell in that row ‘WordWraps’ it’s content.
Either I don’t find it, or it’s not possible (yet). I found a topic about this here:
https://gambas-user.narkive.com/CSTpFWV ... e-question
where Benoit Minisini writes:
“In Gambas 3, WordWrap is a property attached to the cell. And to adjust the
width of a row or a column, you must set its height (or width) to "-1". But,
if I remember well, it is not implemented in gb.gtk yet.”
Right there I got it wrong. As I wanted all of the GridView cells to adapt their Hight, I did put the command with the ‘general’ properties of MyGrid:
With MyGrid = New GridView(MyPanel) As "MyGrid"
.Rows.Count = 20
.Rows.Height = -1
And as this doesn’t produce an error, I assumed Benoit was right with “if I remember well, it is not implemented in gb.gtk yet”.
Only it had been, but I hadn’t cosidered setting Row Height at Cell level.
I have played a bit with the possibilities this property offers, such as applying it to a whole GridView. Just run the code below.
PS :
MyGrid.Grid = Not MyGrid.Grid
: Thanks once more Cogier. Never heard about that method before.
' Gambas class file
Public MyGrid As GridView

Public Sub Form_Open()

Dim R, C As Integer
 With Me
    .Center
    .Height = 500
    .Width = 600       'Try with different widths if you like
  End With
  
  With MyGrid = New GridView(Me) As "MyGrid"
    .width = Me.width - 20
    .left = 10
    .top = 10
    .rows.Count = 4
    .columns.Count = 3
    .height = .Rows.height * .Rows.Count + .Rows.height
    .Columns.Width = .width / 3
    .ScrollBar = False
    .NoTabFocus = True
    .border = True
    .Grid = True
    .Padding = 4
  End With
  
  MyGrid[0, 0].RichText = "Please click here"
   
  End

Public Sub MyGrid_Click()
Dim X, R, C, H As Integer
H = 0

With MyGrid[0, 0]
  .ColumnSpan = 3
  .Alignment = Align.Center
  .Font = Font["Arial,18,bold"]
  .RichText = "Welcome !"
End With

With MyGrid[1, 0]
    .Font = Font["Comic sans,10"]
    .RichText = "<b><i>This line uses HTML Bold & Italic</i></b><br>and switches them of in the next one</br>"
End With

With MyGrid[1, 1]
    .Font = Font["Comic sans,9"]
    .Alignment = Align.TopRight
    .RichText = "As you will notice, the row's "
End With

With MyGrid[1, 2]
    .Font = Font["Comic sans,9"]
    .Alignment = Align.TopLeft
    .RichText = "height gets set by the largest content in any of the cells in the row"
End With

With MyGrid[2, 0]
    .Alignment = Align.TopNormal
    .font = Font["Utopia,11,underline"]
    .RichText = "This Works with 'spanned' cells as well</br>"
End With

With MyGrid[2, 1]
    .ColumnSpan = 2
    .Alignment = Align.TopNormal
    .RichText = "Susie works in a shoeshine shop. <br>Where she shines she sits, and where she sits she shines</br>"
End With

With MyGrid[3, 2]
    .Font = Font["Comic sans,8"]
    .RichText = "Peter Piper picked a peck of pickled peppers<br>A peck of pickled peppers Peter Piper picked</br><br>If Peter Piper picked a peck of pickled peppers</br><br>Where’s the peck of pickled peppers Peter Piper picked?</br>"
End With

' This part has to be placed after the cell contents have been set.
For R = 0 To 3
  For C = 0 To 2
    MyGrid[R, C].WordWrap = True
    MyGrid.Rows[R].Height = -1
  Next
  H = H + MyGrid.Rows[R].Height
Next

MyGrid.height = H

End
Old african saying:
You eat an elephant one small bite at a time.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Automatic change of Row Height with WordWrap ?

Post by cogier »

I'm glad it helped. I have tried your code which works well.

Before publishing code I suggest you run from the Project menu Compile all. This would have told you about the unused variables. Also, there is no need to make H = 0 as it is that when you create it.

Image
Post Reply