1

I intend to provide a text wrapping functionality option to my wxPython-based application to be switched ON and OFF from menu (like, say notepad++ allows it).

I have a wxTextCtrl and read in wxWidgets documentation that I have to manipulate the character/paragraph style with TextCtrl::SetStyle( wxTextAttr( my_atributes )) in order to provide text wrapping functionality.

Here is how I do it in my menu item handler:

def OnMenuWrapLines( self, evt):
  current_style = wx.TextAttr()
  self.TextCtrl.GetStyle( 0, current_style)

  print "DBG: OnMenuWrapLines(): prev style = 0x%X" % current_style.GetFlags()

  if evt.IsChecked():
     current_style.SetFlags( current_style.GetFlags() & ~wx.TE_DONTWRAP | wx.TE_BESTWRAP)
  else:
     current_style.SetFlags( current_style.GetFlags() & ~wx.TE_BESTWRAP | wx.TE_DONTWRAP)

  print "DBG: SetStyle(0x%x)=%d" % ( current_style.GetFlags(), self.TextCtrl.SetStyle( 0, self.TextCtrl.GetNumberOfLines(), current_style))

Which does not work as expected - the style itself is calculated ok, but it can't be set (every time this function gets entered - the style value is the same, the modified value gets not applied).

I heard somewhere that on wxMSW wrapping style can be only specified at the construction time (which is not an option for me). In this case any workaround is welcome.

Thanks in advance

Nikita Vorontsov
  • 194
  • 2
  • 15
  • 1
    I checked the wx docs: "Note that alignment styles (wxTE_LEFT, wxTE_CENTRE and wxTE_RIGHT) can be changed dynamically after control creation on wxMSW and wxGTK. wxTE_READONLY, wxTE_PASSWORD and wrapping styles can be dynamically changed under wxGTK but not wxMSW. The other styles can be only set during control creation." [see at the end of section styles](http://docs.wxwidgets.org/3.0/classwx_text_ctrl.html) – nepix32 Jan 08 '15 at 11:36
  • @nepix32: True - I have seen that too but was hoping that there is a workaround of some kind. Thanks anyways. – Nikita Vorontsov Jan 08 '15 at 13:24

1 Answers1

1

If you like how Notepad++ acts as text editor, you could have a look at wx.StyledTextCtrl, which uses the same Scintilla component at its core (see SetWrapMode for setting Word wrap). Scintilla line wrap documentation.

Tested it, wraps immediately.

from wx.stc import StyledTextCtrl, STC_WRAP_NONE, STC_WRAP_WORD

    my_stc.SetWrapMode(STC_WRAP_WORD)
    …
    my_stc.SetWrapMode(STC_WRAP_NONE)
nepix32
  • 3,012
  • 2
  • 14
  • 29