Merge branch 'horizontals'
[swftools.git] / wx / lib / wordwrap.py
1 #----------------------------------------------------------------------
2 # Name:        wx.lib.wordwrap
3 # Purpose:     Contains a function to aid in word-wrapping some text
4 #
5 # Author:      Robin Dunn
6 #
7 # Created:     15-Oct-2006
8 # RCS-ID:      $Id: wordwrap.py 54718 2008-07-19 18:57:26Z RD $
9 # Copyright:   (c) 2006 by Total Control Software
10 # Licence:     wxWindows license
11 #----------------------------------------------------------------------
12
13 def wordwrap(text, width, dc, breakLongWords=True, margin=0):
14     """
15     Returns a copy of text with newline characters inserted where long
16     lines should be broken such that they will fit within the given
17     width, with the given margin left and right, on the given `wx.DC`
18     using its current font settings.  By default words that are wider
19     than the margin-adjusted width will be broken at the nearest
20     character boundary, but this can be disabled by passing ``False``
21     for the ``breakLongWords`` parameter.
22     """
23
24     wrapped_lines = []
25     text = text.split('\n')
26     for line in text:
27         pte = dc.GetPartialTextExtents(line)        
28         wid = ( width - (2*margin+1)*dc.GetTextExtent(' ')[0] 
29               - max([0] + [pte[i]-pte[i-1] for i in range(1,len(pte))]) )
30         idx = 0
31         start = 0
32         startIdx = 0
33         spcIdx = -1
34         while idx < len(pte):
35             # remember the last seen space
36             if line[idx] == ' ':
37                 spcIdx = idx
38
39             # have we reached the max width?
40             if pte[idx] - start > wid and (spcIdx != -1 or breakLongWords):
41                 if spcIdx != -1:
42                     idx = spcIdx + 1
43                 wrapped_lines.append(' '*margin + line[startIdx : idx] + ' '*margin)
44                 start = pte[idx]
45                 startIdx = idx
46                 spcIdx = -1
47
48             idx += 1
49
50         wrapped_lines.append(' '*margin + line[startIdx : idx] + ' '*margin)
51
52     return '\n'.join(wrapped_lines)
53
54
55
56 if __name__ == '__main__':
57     import wx
58     class TestPanel(wx.Panel):
59         def __init__(self, parent):
60             wx.Panel.__init__(self, parent)
61
62             self.tc = wx.TextCtrl(self, -1, "", (20,20), (150,150), wx.TE_MULTILINE)
63             self.Bind(wx.EVT_TEXT, self.OnDoUpdate, self.tc)
64             self.Bind(wx.EVT_SIZE, self.OnSize)
65             
66
67         def OnSize(self, evt):
68             wx.CallAfter(self.OnDoUpdate, None)
69             
70             
71         def OnDoUpdate(self, evt):
72             WIDTH = self.GetSize().width - 220
73             HEIGHT = 200
74             bmp = wx.EmptyBitmap(WIDTH, HEIGHT)
75             mdc = wx.MemoryDC(bmp)
76             mdc.SetBackground(wx.Brush("white"))
77             mdc.Clear()
78             mdc.SetPen(wx.Pen("black"))
79             mdc.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
80             mdc.DrawRectangle(0,0, WIDTH, HEIGHT)
81
82             text = wordwrap(self.tc.GetValue(), WIDTH-2, mdc, False)
83             #print repr(text)
84             mdc.DrawLabel(text, (1,1, WIDTH-2, HEIGHT-2))
85
86             del mdc
87             dc = wx.ClientDC(self)
88             dc.DrawBitmap(bmp, 200, 20)
89
90
91     app = wx.App(False)
92     frm = wx.Frame(None, title="Test wordWrap")
93     pnl = TestPanel(frm)
94     frm.Show()
95     app.MainLoop()
96
97