]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/wordwrap.py
   1 #---------------------------------------------------------------------- 
   3 # Purpose:     Contains a function to aid in word-wrapping some text 
   9 # Copyright:   (c) 2006 by Total Control Software 
  10 # Licence:     wxWindows license 
  11 #---------------------------------------------------------------------- 
  15 def wordwrap(text
, width
, dc
, breakLongWords
=True): 
  17     Returns a copy of text with newline characters inserted where long 
  18     lines should be broken such that they will fit within the given 
  19     width, on the given `wx.DC` using its current font settings.  By 
  20     default words that are wider than width will be broken at the 
  21     nearest character boundary, but this can be disabled by passing 
  22     ``False`` for the ``breakLongWords`` parameter. 
  26     text 
= text
.split('\n') 
  28         pte 
= dc
.GetPartialTextExtents(line
) 
  34             # remember the last seen space 
  38             # have we reached the max width? 
  39             if pte
[idx
] - start 
> width 
and (spcIdx 
!= -1 or breakLongWords
): 
  42                 wrapped_lines
.append( line
[startIdx 
: idx
] ) 
  49         wrapped_lines
.append( line
[startIdx 
: idx
] ) 
  51     return '\n'.join(wrapped_lines
) 
  57 if __name__ 
== '__main__': 
  59     class TestPanel(wx
.Panel
): 
  60         def __init__(self
, parent
): 
  61             wx
.Panel
.__init
__(self
, parent
) 
  63             self
.tc 
= wx
.TextCtrl(self
, -1, "", (20,20), (150,150), wx
.TE_MULTILINE
) 
  64             self
.Bind(wx
.EVT_TEXT
, self
.OnDoUpdate
, self
.tc
) 
  66         def OnDoUpdate(self
, evt
): 
  68             bmp 
= wx
.EmptyBitmap(WIDTH
, WIDTH
) 
  69             mdc 
= wx
.MemoryDC(bmp
) 
  70             mdc
.SetBackground(wx
.Brush("white")) 
  72             mdc
.SetPen(wx
.Pen("black")) 
  73             mdc
.SetFont(wx
.Font(10, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)) 
  74             mdc
.DrawRectangle(0,0, WIDTH
, WIDTH
) 
  76             text 
= wordwrap(self
.tc
.GetValue(), WIDTH
-2, mdc
, False) 
  78             mdc
.DrawLabel(text
, (1,1, WIDTH
-2, WIDTH
-2)) 
  81             dc 
= wx
.ClientDC(self
) 
  82             dc
.DrawBitmap(bmp
, 200, 20) 
  86     frm 
= wx
.Frame(None, title
="Test wordWrap")