How prevent StaticText from wrapping?

Hi Grant,

···

On Nov 15, 2007 3:27 PM, Grant Edwards wrote:

On 2007-11-15, Robin Dunn <robin@alldunn.com> wrote:
> Grant Edwards wrote:
>> On 2007-11-12, Robin Dunn <robin@alldunn.com> wrote:
>>> Grant Edwards wrote:
>>>> I can't figure out how to tell a StaticText not to wrap.
>>>> According to the docs, calling Wrap(-1) should disable
>>>> wrapping, but it doesn't seem to work.
>>
>>> platform?
>>
>> Gento Linux (IA32)
>>
>> Python 2.4.4
>> wxPython 2.6.4.0
>> wxGTK 2.6.4.0
>> gtk+ 2.10.14
>>
>
> The GTK static text has automatic wrapping built in

Rats. That's pretty useless when the widget is only tall
enough for one line of text.

> so I think the only way to prevent it is to make sure that the
> widget has enough width so it doesn't need to wrap.

Can't do that.

> At one point in time (might have been 2.6.4, but I don't
> remember) there was an off-by-one bug where it would default
> to one pixel too small, and so it would often wrap when you
> didn't expect it. If that's what is happening in your case
> then it then it's pretty easy to work around.

No, I just want the text to be clipped if it's too wide.

Wrapping is pointless because the panel is only tall enough for
one line of text. What happens now is that the text is
wrapped, then clipped, so you can't ready anything. You see
the bottom third of the first line and the top third of the
second line. If you try you can sometimes guess what it's
saying, but it would be a lot more useful if it just didn't wrap.

Have you tried to play with wx.lib.stattext? It might not be exactly
ready for what you ask, but it should be trivial to modify it to clip
text (or to add a "..." if the text is too wide).

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

This may be naive on my part, but if you know the size of the StaticText
control, then you can probably do some guessing on the string length to
determine when it's about to hit the end of the control. Then you'd just
clip it using string slicing.

But Andrea is very smart, so you will likely be better off listening to
him.

Mike

···

-----Original Message-----
From: Andrea Gavana [mailto:andrea.gavana@gmail.com]
Sent: Thursday, November 15, 2007 9:47 AM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Re: How prevent StaticText from
wrapping?

Hi Grant,

On Nov 15, 2007 3:27 PM, Grant Edwards wrote:
> On 2007-11-15, Robin Dunn <robin@alldunn.com> wrote:
> > Grant Edwards wrote:
> >> On 2007-11-12, Robin Dunn <robin@alldunn.com> wrote:
> >>> Grant Edwards wrote:
> >>>> I can't figure out how to tell a StaticText not to wrap.
> >>>> According to the docs, calling Wrap(-1) should disable
wrapping,
> >>>> but it doesn't seem to work.
> >>
> >>> platform?
> >>
> >> Gento Linux (IA32)
> >>
> >> Python 2.4.4
> >> wxPython 2.6.4.0
> >> wxGTK 2.6.4.0
> >> gtk+ 2.10.14
> >>
> >
> > The GTK static text has automatic wrapping built in
>
> Rats. That's pretty useless when the widget is only tall
enough for
> one line of text.
>
> > so I think the only way to prevent it is to make sure that the
> > widget has enough width so it doesn't need to wrap.
>
> Can't do that.
>
> > At one point in time (might have been 2.6.4, but I don't
> > remember) there was an off-by-one bug where it would
default to one
> > pixel too small, and so it would often wrap when you
didn't expect
> > it. If that's what is happening in your case then it then it's
> > pretty easy to work around.
>
> No, I just want the text to be clipped if it's too wide.
>
> Wrapping is pointless because the panel is only tall enough for one
> line of text. What happens now is that the text is wrapped, then
> clipped, so you can't ready anything. You see the bottom
third of the
> first line and the top third of the second line. If you
try you can
> sometimes guess what it's saying, but it would be a lot
more useful if
> it just didn't wrap.

Have you tried to play with wx.lib.stattext? It might not be
exactly ready for what you ask, but it should be trivial to
modify it to clip text (or to add a "..." if the text is too wide).

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

hi Mike & All,

> From: Andrea Gavana [mailto:andrea.gavana@gmail.com]
> Sent: Thursday, November 15, 2007 9:47 AM
> To: wxPython-users@lists.wxwidgets.org
> Subject: Re: [wxPython-users] Re: How prevent StaticText from
> wrapping?
>
> Hi Grant,
>
> > > Grant Edwards wrote:
> > >>> Grant Edwards wrote:
> > >>>> I can't figure out how to tell a StaticText not to wrap.
> > >>>> According to the docs, calling Wrap(-1) should disable
> wrapping,
> > >>>> but it doesn't seem to work.
> > >>
> > >>> platform?
> > >>
> > >> Gento Linux (IA32)
> > >>
> > >> Python 2.4.4
> > >> wxPython 2.6.4.0
> > >> wxGTK 2.6.4.0
> > >> gtk+ 2.10.14
> > >>
> > >
> > > The GTK static text has automatic wrapping built in
> >
> > Rats. That's pretty useless when the widget is only tall
> enough for
> > one line of text.
> >
> > > so I think the only way to prevent it is to make sure that the
> > > widget has enough width so it doesn't need to wrap.
> >
> > Can't do that.
> >
> > > At one point in time (might have been 2.6.4, but I don't
> > > remember) there was an off-by-one bug where it would
> default to one
> > > pixel too small, and so it would often wrap when you
> didn't expect
> > > it. If that's what is happening in your case then it then it's
> > > pretty easy to work around.
> >
> > No, I just want the text to be clipped if it's too wide.
> >
> > Wrapping is pointless because the panel is only tall enough for one
> > line of text. What happens now is that the text is wrapped, then
> > clipped, so you can't ready anything. You see the bottom
> third of the
> > first line and the top third of the second line. If you
> try you can
> > sometimes guess what it's saying, but it would be a lot
> more useful if
> > it just didn't wrap.
>
> Have you tried to play with wx.lib.stattext? It might not be
> exactly ready for what you ask, but it should be trivial to
> modify it to clip text (or to add a "..." if the text is too wide).
>
> Andrea.
>
> "Imagination Is The Only Weapon In The War Against Reality."
> http://xoomer.alice.it/infinity77/
>

This may be naive on my part, but if you know the size of the StaticText
control, then you can probably do some guessing on the string length to
determine when it's about to hit the end of the control. Then you'd just
clip it using string slicing.

But Andrea is very smart, so you will likely be better off listening to
him.

I didn't think about this approach, which is indeed easier. You might
try something like this (taken from LabelBook, not very efficient but
it works):

dc = wx.ClientDC(yourStaticText)
maxWidth = yourStaticText.GetSize().x
text = yourStaticText.GetLabel()

newText = TruncateText(dc, text, maxWidth)

def TruncateText(dc, text, maxWidth):
    """
    Truncates a given string to fit given width size. if the text does not fit
    into the given width it is truncated to fit. the format of the fixed text
    is <truncate text ..>.
    """

    textLen = len(text)
    tempText = text
    rectSize = maxWidth

    fixedText = ""

    textW, textH = dc.GetTextExtent(text)

    if rectSize >= textW:
        return text

    # The text does not fit in the designated area,
    # so we need to truncate it a bit
    suffix = ".."
    w, h = dc.GetTextExtent(suffix)
    rectSize -= w

    for i in xrange(textLen, -1, -1):

        textW, textH = dc.GetTextExtent(tempText)
        if rectSize >= textW:
            fixedText = tempText
            fixedText += ".."
            return fixedText

        tempText = tempText[:-1]

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On Nov 15, 2007 10:50 PM, Mike Driscoll wrote:

> -----Original Message-----
> On Nov 15, 2007 3:27 PM, Grant Edwards wrote:
> > On 2007-11-15, Robin Dunn <robin@alldunn.com> wrote:
> > >> On 2007-11-12, Robin Dunn <robin@alldunn.com> wrote: