Xojo Conferences
XDCMay2019MiamiUSA

Listbox Column Width in usable stringlen? (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: [OT] MySQL users can sleep soundly...
Next thread: bug?


Re: File Suggestions?   -   Rubber Chicken Software Co.
  Listbox Column Width in usable stringlen?   -   Tim Jones
   Re: Listbox Column Width in usable stringlen?   -   Tom Benson
   Re: Listbox Column Width in usable stringlen?   -   Carlos M
   Re: Listbox Column Width in usable stringlen?   -   Carlos M
   Re: Listbox Column Width in usable stringlen?   -   Charles Yeomans
   Re: Listbox Column Width in usable stringlen?   -   Tim Jones
   Re: Listbox Column Width in usable stringlen?   -   Tim Jones
   Re: Listbox Column Width in usable stringlen?   -   Tim Jones
   Re: Listbox Column Width in usable stringlen?   -   Tim Jones
   Re: Listbox Column Width in usable stringlen?   -   Carlos M

Listbox Column Width in usable stringlen?
Date: 26.04.09 21:12 (Sun, 26 Apr 2009 13:12:56 -0700)
From: Tim Jones
I'm working with a listbox that will contain a list of file paths in
a column. For short paths, the display is fine, but for deeper
paths, the text is cut off by the column width. What I would like to
do is to use a truncate function to cut the full path in half,
stripping out enough characters to allow the string to fit within the
constraints of the cell's width. For example:

/Users/tjones/Documents/Daily Reviews/Color Corrected/Reel 1/Frame
34223.mxp

Would become something like this:

/Users/tjones/Documents/Dai....Frame 34223.mxp

With the full string being placed into the CellTag.

The function is pretty straight forward, but I can't figure out how
to determine the width of the cell being used in reference to the
font size.

Does anyone have a pixel to letter count (using fixed width, Monaco
or Courier New font) conversion or have a tip on how to convert the
Column(x).WidthActual to characters they could share?

Thanks,
Tim

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 26.04.09 22:16 (Mon, 27 Apr 2009 07:16:54 +1000)
From: Tom Benson
RB has this built in, you don't need to do do declares yourself.
All you need to do is override the text pain event of the Listbox by
putting the following in the "CellTextPaint" event:

> dim yOffset as integer = max(22-(g.textascent/
> 2),me.DefaultRowHeight)
> g.drawstring me.cell(row,column),
> 0,yOffset,me.column(column).widthActual,true
> Return true

This will draw your cell's text into a nicely truncated string,
however calls to me.cell(x,y) will still return the full string...

I use this all the time.

You may want to elaborate on the code supplied by setting g.textFont,
g.textSize etc to match Listbo.Font and CellBold, CellItalic etc
properties so that you don't loose styling...

- Tom Benson
---------------------------------------------------------------
Custom Controls & Code Libraries
for REALbasic version 5.5 and up

http://www.qedit.com.au



On 27/04/2009, at 6:12 AM, Tim Jones wrote:

> I'm working with a listbox that will contain a list of file paths in
> a column. For short paths, the display is fine, but for deeper
> paths, the text is cut off by the column width. What I would like
> to do is to use a truncate function to cut the full path in half,
> stripping out enough characters to allow the string to fit within
> the constraints of the cell's width. For example:
>
> /Users/tjones/Documents/Daily Reviews/Color Corrected/Reel 1/Frame
> 34223.mxp
>
> Would become something like this:
>
> /Users/tjones/Documents/Dai....Frame 34223.mxp
>
> With the full string being placed into the CellTag.
>
> The function is pretty straight forward, but I can't figure out how
> to determine the width of the cell being used in reference to the
> font size.
>
> Does anyone have a pixel to letter count (using fixed width, Monaco
> or Courier New font) conversion or have a tip on how to convert the
> Column(x).WidthActual to characters they could share?
>
> Thanks,
> Tim
>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 00:35 (Mon, 27 Apr 2009 00:35:55 +0100)
From: Carlos M
Here are two functions that I use to truncate text at end or center
with an ellipsis

Function TruncateString(s As String, width As Integer, textFont As
String, textSize As Integer, center As Boolean = False) As String
// Truncates a string to the passed width (IN PIXELS), text font and size
// adds an ellipsis (...) at end or center

If s = "" Then Return s

// Get string width in pixels
Dim sWidth As Integer = StringWidth( s, textFont, textSize )

// If string width is less or equal than the passed width, then
return the string
If sWidth <= width Then Return s

Dim chars() As String = Split( s, "" ) // split s into chars
Dim iMax As Integer = Ubound( chars )
Dim newString As String

// Get width of ellipsis "..."
Dim eWidth As Integer = StringWidth( "...", textFont, textSize )

// Get halfWidth
Dim halfWidth As Integer = width / 2

For i As Integer = 0 To iMax
newString = newString + chars( i )
Dim newWidth As Integer = StringWidth( newString, textFont, textSize )
If not center Then
If newWidth + eWidth >= width Then
Return newString + "..."
End If
Else
If newWidth + eWidth >= halfWidth Then
newString = newString + "..."
Exit // exit loop
End If
End If
Next

If center Then
// Get the end of the string and append it to new string
Dim endString As String
Dim leftWidth As Integer = StringWidth( newString, textFont, textSize )
For i As Integer = iMax DownTo 0
endString = chars( i ) + endString
Dim endWidth As Integer = StringWidth( endString, textFont, textSize )
If endWidth + leftWidth >= width Then
Return newString + endString
End If
Next
End If

Return s

End Function

Function StringWidth(s As String, textFont As String, textSize As
Integer) As Integer
// Returns the string width IN PIXELS for the passed text font and size
Dim p As Picture = New Picture(1, 1, 32)
p.Graphics.TextFont = textFont
p.Graphics.TextSize = textSize
Return p.Graphics.StringWidth( s )
End Function

So, in the Listbox CellTextPaint event handler you can use something like:
// Get text from the Cell TAG in col 0
Dim s As String = Me.CellTag(row, 0)
// Truncate text at center
Me.Cell(row,column) = TruncateStringNew(s, g.Width - 8, Me.TextFont,
Me.TextSize, True)
Note: On Windows I need to pass the cell's width with less 8 pixels in
order to work correctly - you should test to see if this value is ok
or not for you.

You can adapt this in order to draw the text directly in the cell
without the need to store it the in CellTag.

Oh... and this code can be improved, but for my needs it works fine -
I only use it for text in a single line EditField/StaticText.

Carlos

On Sun, Apr 26, 2009 at 9:12 PM, Tim Jones wrote:
> I'm working with a listbox that will contain a list of file paths in a
> column.  For short paths, the display is fine, but for deeper paths, the
> text is cut off by the column width.  What I would like to do is to use a
> truncate function to cut the full path in half, stripping out enough
> characters to allow the string to fit within the constraints of the cell's
> width.  For example:
>
> /Users/tjones/Documents/Daily Reviews/Color Corrected/Reel 1/Frame 34223.mxp
>
> Would become something like this:
>
> /Users/tjones/Documents/Dai....Frame 34223.mxp
>
> With the full string being placed into the CellTag.
>
> The function is pretty straight forward, but I can't figure out how to
> determine the width of the cell being used in reference to the font size.
>
> Does anyone have a pixel to letter count (using fixed width, Monaco or
> Courier New font) conversion or have a tip on how to convert the
> Column(x).WidthActual to characters they could share?
>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 00:47 (Mon, 27 Apr 2009 00:47:03 +0100)
From: Carlos M
A small correction:

On Mon, Apr 27, 2009 at 12:35 AM, Carlos M wrote:
> Here are two functions that I use to truncate text at end or center
> with an ellipsis
<snip>
> So, in the Listbox CellTextPaint event handler you can use something like:
>  // Get text from the Cell TAG in col 0
>  Dim s As String = Me.CellTag(row, 0)
>  // Truncate text at center
>  Me.Cell(row,column) = TruncateStringNew(s, g.Width - 8, Me.TextFont,
> Me.TextSize, True)

Instead of:
"Me.Cell(row,column) = TruncateStringNew(..."
it should be:
"Me.Cell(row,column) = TruncateString(..."

Carlos

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 01:51 (Sun, 26 Apr 2009 20:51:06 -0400)
From: Charles Yeomans

On Apr 26, 2009, at 4:12 PM, Tim Jones wrote:

> I'm working with a listbox that will contain a list of file paths in
> a column. For short paths, the display is fine, but for deeper
> paths, the text is cut off by the column width. What I would like
> to do is to use a truncate function to cut the full path in half,
> stripping out enough characters to allow the string to fit within
> the constraints of the cell's width. For example:
>
> /Users/tjones/Documents/Daily Reviews/Color Corrected/Reel 1/Frame
> 34223.mxp
>
> Would become something like this:
>
> /Users/tjones/Documents/Dai....Frame 34223.mxp
>
> With the full string being placed into the CellTag.
>
> The function is pretty straight forward, but I can't figure out how
> to determine the width of the cell being used in reference to the
> font size.
>
> Does anyone have a pixel to letter count (using fixed width, Monaco
> or Courier New font) conversion or have a tip on how to convert the
> Column(x).WidthActual to characters they could share?

If you don't want to use the truncation capability in
Graphics.StringWidth, you might have a look at http://support.realsoftware.com/listarchives/realbasic-nug/2004-11/msg02224.html
, where I offered a truncation function. Probably the code could be
modified to do center truncation.

Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 16:35 (Mon, 27 Apr 2009 08:35:00 -0700)
From: Tim Jones
On Apr 26, 2009, at 2:16 PM, Tom Benson wrote:

> RB has this built in, you don't need to do do declares yourself.
> All you need to do is override the text pain event of the Listbox
> by putting the following in the "CellTextPaint" event:
>
>> dim yOffset as integer = max(22-(g.textascent/
>> 2),me.DefaultRowHeight)
>> g.drawstring me.cell(row,column),0,yOffset,me.column
>> (column).widthActual,true
>> Return true
>
> This will draw your cell's text into a nicely truncated string,
> however calls to me.cell(x,y) will still return the full string...

The issue with this code is that the ellipsis appears at the end of
the drawn string. I want the ellipsis in the middle so the filename
portion is visible.

I already have a very good truncate method, I just need to get the
with of the cell in a character count version rather than a pixel
width. That's what I'm looking for here.

Thanks for the input,

Tim

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 16:47 (Mon, 27 Apr 2009 08:47:00 -0700)
From: Tim Jones
Carlos,

I won't quote everything again, but I owe you a Bottle of Cava or a
pound of Jamaican Blue coffee! That is dead on what I needed. The
magic is in converting the string to pixels and working from that
direction rather than trying to convert the cell width to characters.

Thanks,
Tim

On Apr 26, 2009, at 4:35 PM, Carlos M wrote:

> Here are two functions that I use to truncate text at end or center
> with an ellipsis
>
> Function TruncateString(s As String, width As Integer, textFont As
> String, textSize As Integer, center As Boolean = False) As String
> // Truncates a string to the passed width (IN PIXELS), text font
> and size
> // adds an ellipsis (...) at end or center
>
> If s = "" Then Return s
>
> // Get string width in pixels
> Dim sWidth As Integer = StringWidth( s, textFont, textSize )
>
> // If string width is less or equal than the passed width, then
> return the string
> If sWidth <= width Then Return s
>
> Dim chars() As String = Split( s, "" ) // split s into chars
> Dim iMax As Integer = Ubound( chars )
> Dim newString As String
>
> // Get width of ellipsis "..."
> Dim eWidth As Integer = StringWidth( "...", textFont, textSize )
>
> // Get halfWidth
> Dim halfWidth As Integer = width / 2
>
> For i As Integer = 0 To iMax
> newString = newString + chars( i )
> Dim newWidth As Integer = StringWidth( newString, textFont,
> textSize )
> If not center Then
> If newWidth + eWidth >= width Then
> Return newString + "..."
> End If
> Else
> If newWidth + eWidth >= halfWidth Then
> newString = newString + "..."
> Exit // exit loop
> End If
> End If
> Next
>
> If center Then
> // Get the end of the string and append it to new string
> Dim endString As String
> Dim leftWidth As Integer = StringWidth( newString, textFont,
> textSize )
> For i As Integer = iMax DownTo 0
> endString = chars( i ) + endString
> Dim endWidth As Integer = StringWidth( endString, textFont,
> textSize )
> If endWidth + leftWidth >= width Then
> Return newString + endString
> End If
> Next
> End If
>
> Return s
>
> End Function
>
> Function StringWidth(s As String, textFont As String, textSize As
> Integer) As Integer
> // Returns the string width IN PIXELS for the passed text font
> and size
> Dim p As Picture = New Picture(1, 1, 32)
> p.Graphics.TextFont = textFont
> p.Graphics.TextSize = textSize
> Return p.Graphics.StringWidth( s )
> End Function
>
> So, in the Listbox CellTextPaint event handler you can use
> something like:
> // Get text from the Cell TAG in col 0
> Dim s As String = Me.CellTag(row, 0)
> // Truncate text at center
> Me.Cell(row,column) = TruncateStringNew(s, g.Width - 8, Me.TextFont,
> Me.TextSize, True)
> Note: On Windows I need to pass the cell's width with less 8 pixels in
> order to work correctly - you should test to see if this value is ok
> or not for you.
>
> You can adapt this in order to draw the text directly in the cell
> without the need to store it the in CellTag.
>
> Oh... and this code can be improved, but for my needs it works fine -
> I only use it for text in a single line EditField/StaticText.
>
> Carlos
>
> On Sun, Apr 26, 2009 at 9:12 PM, Tim Jones wrote:
>> I'm working with a listbox that will contain a list of file paths
>> in a
>> column. For short paths, the display is fine, but for deeper
>> paths, the
>> text is cut off by the column width. What I would like to do is
>> to use a
>> truncate function to cut the full path in half, stripping out enough
>> characters to allow the string to fit within the constraints of
>> the cell's
>> width. For example:
>>
>> /Users/tjones/Documents/Daily Reviews/Color Corrected/Reel 1/Frame
>> 34223.mxp
>>
>> Would become something like this:
>>
>> /Users/tjones/Documents/Dai....Frame 34223.mxp
>>
>> With the full string being placed into the CellTag.
>>
>> The function is pretty straight forward, but I can't figure out
>> how to
>> determine the width of the cell being used in reference to the
>> font size.
>>
>> Does anyone have a pixel to letter count (using fixed width,
>> Monaco or
>> Courier New font) conversion or have a tip on how to convert the
>> Column(x).WidthActual to characters they could share?
>>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 16:49 (Mon, 27 Apr 2009 08:49:26 -0700)
From: Tim Jones
On Apr 26, 2009, at 5:51 PM, Charles Yeomans wrote:

> On Apr 26, 2009, at 4:12 PM, Tim Jones wrote:
>
>> If you don't want to use the truncation capability in
>> Graphics.StringWidth, you might have a look at http://
>> support.realsoftware.com/listarchives/realbasic-nug/2004-11/
>> msg02224.html , where I offered a truncation function. Probably
>> the code could be modified to do center truncation.

Thanks Charles, but I've already got a proper TruncateString that
properly splits in the middle. I just need a way to convert the cell
width pixels to characters. Carlos provided the perfect solution by
instead converting the string width to pixels instead..

Tim

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 27.04.09 16:52 (Mon, 27 Apr 2009 08:52:40 -0700)
From: Tim Jones
On Apr 27, 2009, at 8:47 AM, Tim Jones wrote:

> Carlos,
>
> I won't quote everything again, but I owe you a Bottle of Cava or a
> pound of Jamaican Blue coffee! That is dead on what I needed. The
> magic is in converting the string to pixels and working from that
> direction rather than trying to convert the cell width to characters.

And of course, I did quote everything again :-(...

Life before coffee.

Tim

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Listbox Column Width in usable stringlen?
Date: 28.04.09 04:19 (Tue, 28 Apr 2009 04:19:30 +0100)
From: Carlos M
On Mon, Apr 27, 2009 at 4:47 PM, Tim Jones wrote:
> I won't quote everything again, but I owe you a Bottle of Cava or a pound of
> Jamaican Blue coffee!

If you don't mind, I prefer a Vintage Port ;-)

>That is dead on what I needed.

If you're using it just for the Listbox, check again Charles' function
that has the advantage of being a Graphics extension that's easier to
use (and maybe faster) with the Listbox - you just have to adapt it to
truncate text at center.

Carlos

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>