atom feed5 messages in com.googlegroups.wxpython-usersRe: [wxPython-users] How to handle wo...
FromSent OnAttachments
Erik WestraDec 22, 2004 10:59 am 
Nigel RoweDec 22, 2004 2:14 pm 
Erik WestraDec 22, 2004 4:15 pm 
Robin DunnDec 23, 2004 4:26 pm 
Roger BinnsDec 24, 2004 8:07 am 
Subject:Re: [wxPython-users] How to handle word-wrapping static text?
From:Robin Dunn ("
Date:Dec 23, 2004 4:26:18 pm

Erik Westra wrote:

How have others handled this problem? Is there a generic solution to making sure this works correctly across all platforms? I've had a go at creating my own "WrappingStaticTextCtrl" class based on wx.lib.stattext.GenStaticText, but it's a bit of an ugly hack and doesn't really work all that well. There must be a better way. Any suggestions?

I think that the approach to take will be to break the message string into words (leaving the separating spaces in the words) and then measure the width of each word using dc.GetTextExtent. Then choose some fixed width for the control, or require that a width be passed to the __init__, (but it can resize itself smaller if there is only one or more short lines in the label) and then start adding word widths until you reach that limit. Then reassemble the string with newlines before the words that would have caused the word to cross the limit. Don't forget to account for newlines already in the source string. You could probably derive this class from wx.StaticText so you could take advantage of the builtin DoGetBestSize, etc., just manipulate the label before passing it to the native control.

Make sense?

P.S. This would be nice to have in the lib if you are able to contribute it.