From f9c1b462c7ea8ff8237c475788c180b956d875a5 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 16 Oct 2006 19:49:43 +0000 Subject: [PATCH] Add a utility function that can calculate word-wrap line breaks, and use it in the AboutBox demo. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42055 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/demo/AboutBox.py | 19 ++++---- wxPython/wx/lib/wordwrap.py | 91 +++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 wxPython/wx/lib/wordwrap.py diff --git a/wxPython/demo/AboutBox.py b/wxPython/demo/AboutBox.py index 783c33b0dc..ee75822274 100644 --- a/wxPython/demo/AboutBox.py +++ b/wxPython/demo/AboutBox.py @@ -1,5 +1,6 @@ import wx +from wx.lib.wordwrap import wordwrap #---------------------------------------------------------------------- @@ -18,14 +19,16 @@ class TestPanel(wx.Panel): info.Name = "Hello World" info.Version = "1.2.3" info.Copyright = "(C) 2006 Programmers and Coders Everywhere" - info.Description = \ - "A \"hello world\" program is a software program that prints out "\ - "\"Hello world!\" on a display device. It is used in many introductory "\ - "tutorials for teaching a programming language. Such a program is "\ - "typically one of the simplest programs possible in a computer language. "\ - "A \"hello world\" program can be a useful sanity test to make sure that "\ - "a language's compiler, development environment, and run-time environment "\ - "are correctly installed." + info.Description = wordwrap( + "A \"hello world\" program is a software program that prints out " + "\"Hello world!\" on a display device. It is used in many introductory " + "tutorials for teaching a programming language." + + "\n\nSuch a program is typically one of the simplest programs possible " + "in a computer language. A \"hello world\" program can be a useful " + "sanity test to make sure that a language's compiler, development " + "environment, and run-time environment are correctly installed.", + 350, wx.ClientDC(self)) info.WebSite = ("http://en.wikipedia.org/wiki/Hello_world", "Hello World home page") info.Developers = [ "Joe Programmer", "Jane Coder", diff --git a/wxPython/wx/lib/wordwrap.py b/wxPython/wx/lib/wordwrap.py new file mode 100644 index 0000000000..f5e300f5b2 --- /dev/null +++ b/wxPython/wx/lib/wordwrap.py @@ -0,0 +1,91 @@ +#---------------------------------------------------------------------- +# Name: wx.lib.wrap +# Purpose: Contains a function to aid in word-wrapping some text +# +# Author: Robin Dunn +# +# Created: 15-Oct-2006 +# RCS-ID: $Id$ +# Copyright: (c) 2006 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + + + +def wordwrap(text, width, dc, breakLongWords=True): + """ + Returns a copy of text with newline characters inserted where long + lines should be broken such that they will fit within the given + width, on the given `wx.DC` using its current font settings. By + default words that are wider than width will be broken at the + nearest character boundary, but this can be disabled by passing + ``False`` for the ``breakLongWords`` parameter. + """ + + wrapped_lines = [] + text = text.split('\n') + for line in text: + pte = dc.GetPartialTextExtents(line) + idx = 0 + start = 0 + startIdx = 0 + spcIdx = -1 + while idx < len(pte): + # remember the last seen space + if line[idx] == ' ': + spcIdx = idx + + # have we reached the max width? + if pte[idx] - start > width and (spcIdx != -1 or breakLongWords): + if spcIdx != -1: + idx = spcIdx + 1 + wrapped_lines.append( line[startIdx : idx] ) + start = pte[idx] + startIdx = idx + spcIdx = -1 + + idx += 1 + + wrapped_lines.append( line[startIdx : idx] ) + + return '\n'.join(wrapped_lines) + + + + + +if __name__ == '__main__': + import wx + class TestPanel(wx.Panel): + def __init__(self, parent): + wx.Panel.__init__(self, parent) + + self.tc = wx.TextCtrl(self, -1, "", (20,20), (150,150), wx.TE_MULTILINE) + self.Bind(wx.EVT_TEXT, self.OnDoUpdate, self.tc) + + def OnDoUpdate(self, evt): + WIDTH = 200 + bmp = wx.EmptyBitmap(WIDTH, WIDTH) + mdc = wx.MemoryDC(bmp) + mdc.SetBackground(wx.Brush("white")) + mdc.Clear() + mdc.SetPen(wx.Pen("black")) + mdc.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL)) + mdc.DrawRectangle(0,0, WIDTH, WIDTH) + + text = wordwrap(self.tc.GetValue(), WIDTH-2, mdc, False) + #print repr(text) + mdc.DrawLabel(text, (1,1, WIDTH-2, WIDTH-2)) + + del mdc + dc = wx.ClientDC(self) + dc.DrawBitmap(bmp, 200, 20) + + + app = wx.App(False) + frm = wx.Frame(None, title="Test wordWrap") + pnl = TestPanel(frm) + frm.Show() + app.MainLoop() + +