Page 1 of 2

Automatic change of Row Height with WordWrap ?

Posted: Sunday 9th May 2021 9:15am
by Doctor Watson
Hi.
In my search for a suitable way to display HTML rich text I discovered that a nice solution for my project would be to ‘abuse’ a GridView control for that purpose. The formatting possibilities of the grid itself and it’s cells, combined with the Rich Text displayed in those cells are manifold.
I have created a small project to demonstrate this.
It uses a GridView with only one column to display differently formatted text in each cell.
GridView_Project.zip
(15.62 KiB) Downloaded 236 times
Works fine for me, but there is one problem that – if solved – would make it really ideal: how to fit text in each cell. To do this, I have to set each cell’s height so that the text will be visible as it should. That can of course be done, but the intention is to use this to display help text or instructions in different languages. As we know, some languages need more words to explain things. And that will or can ruin the layout.
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.”
However, that post is 10 years old ….
It didn’t work when I tried. Has a solution been found since then?

Re: Automatic change of Row Height with WordWrap ?

Posted: Sunday 9th May 2021 9:21am
by Doctor Watson
Sorry, forgot to mention: when you run the project, clicking on the grid will toggle grid lines on and off.

Re: Automatic change of Row Height with WordWrap ?

Posted: Sunday 9th May 2021 11:50am
by BruceSteers
You can do it manually using RichTextSize command

Dim hRect As Rect
hRect = Me.Font.RichTextSize(sText, iWidth)

iTextWidth = hRect.W ' we don't need this as we know the width

iTextHeight = hRect.H ' this will be the height you need


Re: Automatic change of Row Height with WordWrap ?

Posted: Sunday 9th May 2021 11:56am
by cogier
I think you would be better off with a TextLabel. I have looked at your code and modified it, hopefully this will help. See what happens when the form is resized.

ImageImage
TextLabel1 As TextLabel

Public Sub Form_Open()

  With Me
    .H = 500
    .W = 600
    .Arrangement = Arrange.Vertical
    .Padding = 15
  End With

  With TextLabel1 = New TextLabel(Me) As "TextLabel1"
    .Expand = True
    .text = GetText()
    .Background = Color.LightBackground
    .padding = 5
  End With

End

Public Sub GetText() As String

  Dim sText As String

  sText &= "<Div align = center><h1><b><u>Welcome</h1></b></u><Div align = left>"
  sText &= "<b><i>This line uses HTML Bold & Italic</i></b><br>and switches them of in the next line<p>"
  sText &= "<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><p>"
  sText &= "<b><font color =red>I chose the title because it seems to touch on so much of what’s exciting and what’s threatening, too, about blogging and all the other changes that we call, collectively, the digital revolution. “Say everything”: the phrase suggests the thrill of the universal project the Web sometimes seems to be, in which everyone gets to contribute to a vast collective conversation and pool of knowledge. “Say everything” also raises all kinds of questions about this new world. If we say everything, how will we have time to listen? And, “Aren’t some things better left unsaid?” So these are some of the things I’m going to look at today.</font></b><br><p>"
  sText &= "<p><h3><Div align = center>Don't use<b> GridView </b> just use a <b>TextLabel</b></h3><p>."
  sText &= "<h2><s>Gridview</s>  H<sub>2</sub>SO<sub>4</sub> - 9<sup>th </sup>May</H2><p>"
  sText &= "<h2><Div align = right>Right align!<Div align = left></h2>"
  sText &= "<H4><font color =#FF00FF>This text is in pink</font><br />"
  sText &= "<font color =blue>This text is blue</font></h4>"

  Return sText

End

Re: Automatic change of Row Height with WordWrap ?

Posted: Monday 10th May 2021 6:27am
by Doctor Watson
Cogier : Thanks for the HTML demonstration. It seems that a lot more is possible than mentioned in the Gambas manual (http://gambaswiki.org/wiki/doc/richtext) .
I will however need the best of both worlds and use a GridView when I want to display graphics and text in one or more columns.

Bruce : Sorry, seems that doesn’t work. I suppose I’ve added the necessary declarations and copied your code.
' Gambas class file

Public sText As String
Public iWidth As Integer
Public iHeight As Integer
Public iTextWidth As Integer
Public iTextHeight As Integer

Public Sub Form_Open()

sText = "Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?"

Dim hRect As Rect
hRect = Me.Font.RichTextSize(sText, iWidth)
iTextWidth = hRect.W   ' we don't need this as we know the width
iTextHeight = hRect.H  ' this will be the height you need
End
I got an error : Unknown symbol ‘RichTextSize’ in class ‘Font’ in Form ….
I tried to figure out what was wrong – typing error perhaps – so I started to enter that line ‘manually’ ;)
And was presented with this when I arrived at ...
NoRichTextSize.png
NoRichTextSize.png (46.36 KiB) Viewed 5441 times
There is no RichTextSize.
One more question – provided RichTextSize can be made to work : I suppose I can use
hRect = MyGrid[0,0].Font.RichTextSize(sText, iWidth)
If so, I can indeed determine the hight to set Row 0 to.

Re: Automatic change of Row Height with WordWrap ?

Posted: Monday 10th May 2021 8:19am
by BruceSteers
Odd , i copy and pasted the text straight from the Label.class in gb.gui.base...

I just checked and have Font.RichtextSize on Form, Me, and Gridview using gambas 3.16


Also i noticed in your code example That iWidth was not given a value

RichTextHeight should do the same then.
(no need to use Rect just use an integer)

Maybe this one liner...

MyGrid.Rows[0].Height = MyGrid[0,0].Font.RichTextHeight(MyGrid.Rows[0, 0].Text, MyGrid.Rows[0, 0].Width)


Re: Automatic change of Row Height with WordWrap ?

Posted: Monday 10th May 2021 9:12am
by Doctor Watson
I just found something, but it’s way beyond my pay grade.
I checked the project properties and noticed that in ‘Components’ ‘gb.qt4’ is disabled.
I tried to enable it, but got a message that gb.gui and gb.qt4 are incompatible.
So I disabled gb.gui and now RichTextSize no longer gives an error.
But many controls have now diappeared.
Does this mean anything to you?

Re: Automatic change of Row Height with WordWrap ?

Posted: Monday 10th May 2021 10:40am
by BruceSteers
Doctor Watson wrote: Monday 10th May 2021 9:12am I just found something, but it’s way beyond my pay grade.
I checked the project properties and noticed that in ‘Components’ ‘gb.qt4’ is disabled.
I tried to enable it, but got a message that gb.gui and gb.qt4 are incompatible.
So I disabled gb.gui and now RichTextSize no longer gives an error.
But many controls have now diappeared.
Does this mean anything to you?
gb.gui is a switcher component. it checks your system and loads either gb.gtk2 gb.gtk3 gb.qt4 or gb.qt5 depending on your system.
so you can't have gb.qt4 and gb.gui together

independently they can lack some features.
I think gb.gui loads gb.gui.base and translates some gtk/qt things for overall compatibility.
but loading one or the other explicitly will limit you to only the components in the gui class.

i am only using gb.gui and i have RichTextSize()
Just use RichTextHeight()
it's less complicated and you are able to see that command.

Re: Automatic change of Row Height with WordWrap ?

Posted: Monday 10th May 2021 2:29pm
by PJBlack
for me RichTextSize works perfect with qt4 qt5 gtk2 gtk3 ...

if you want to use Font.RichTextSize("BlahBlubb") you need an object too ->
SomeObjectWithFontProperty.Font.RichTextSize("BlahBlubb")
or you can do like
Dim Test as New Font
Test.Font.RichTextSize("BlahBlubb")

Re: Automatic change of Row Height with WordWrap ?

Posted: Thursday 13th May 2021 3:08pm
by Doctor Watson
Hi everybody
I’ve been rather busy to get this project working. As Cogier pointed out, it could perhaps be written entirely in HTML, but there are options – for me at least – can be achieved easier with GridView, combined with it’s option to edit cells with HTML.
I suppose things would be easier if I could use RichTextSize but – PJBlack – I tried it once more and no result on this machine. Or is it me ? :?
I have now a new version of a grid that really works after much trial & error. :P
First, RichRextHeight only sets the cell’s height to fit text ‘written’ with a choosen Font. When a long text gets wrapped, the cell’s height just stays the same. But you can use the text width and the cell’s width. Now the tricky part was how to count the number of text lines in the cell.
Then you just multiply cell height with the number of lines.
Have a look and play with a different font and size in line 64 and the text in lines 86 to 94.
I only tested what happens with HTML markup I want to use. Do not use <p> or <h1> as that will ruin things.
It would be nice if everything worked already perfectly. Not so. With some font sizes or certain text lenghts, the lines routine counts one or two lines where it shouldn’t. When gridlines are turned off, this is hardly noticed but I would like to get this completely right.
Back to the drawing table ...
GridView_Wordwrap.zip
(36.15 KiB) Downloaded 217 times