0001 #!/usr/bin/env python 0002 0003 ### BITPIM 0004 ### 0005 ### Copyright (C) 2006 Joe Pham<djpham@bitpim.org> 0006 ### 0007 ### This program is free software; you can redistribute it and/or modify 0008 ### it under the terms of the BitPim license as detailed in the LICENSE file. 0009 ### 0010 ### $Id: tipwin.py 3676 2006-11-14 03:25:45Z djpham $ 0011 0012 """ 0013 A TipWindow class that can properly handle tab(\t) characters. 0014 Limitation: This class does not do text wrapping! 0015 """ 0016 0017 import wx 0018 0019 space_count=4 # expand a tab by the # of spaces 0020 TEXT_MARGIN_X=3 0021 TEXT_MARGIN_Y=3 0022 parentclass=wx.TipWindow 0023 class TipWindow(parentclass): 0024 def __init__(self, parent, text, maxwidth=100, rectbound=None): 0025 global TEXT_MARGIN_X 0026 super(TipWindow, self).__init__(parent, text, maxwidth, rectbound) 0027 _children_wins=self.GetChildren() 0028 if len(_children_wins)!=1: 0029 # can't find the view window, bail 0030 return 0031 self._view=_children_wins[0] 0032 self._text=text 0033 self._max_w=maxwidth 0034 self._line_h=0 0035 self._col_x=[TEXT_MARGIN_X] 0036 self.adjust() 0037 wx.EVT_PAINT(self._view, self.OnPaintView) 0038 0039 def adjust(self): 0040 # adjust the width of this window if there tabs 0041 global space_count 0042 if self._text.find('\t')==-1: 0043 # no tab, bail 0044 return 0045 _dc=wx.ClientDC(self._view) 0046 _dc.SetFont(self._view.GetFont()) 0047 for _line in self._text.split('\n'): 0048 _cnt=0 0049 for _col in _line.split('\t'): 0050 _cnt+=1 0051 if _cnt>=len(self._col_x): 0052 self._col_x.append(0) 0053 _w, _h=_dc.GetTextExtent(_col) 0054 self._col_x[_cnt]=max(_w, self._col_x[_cnt]) 0055 self._line_h=max(self._line_h, _h) 0056 _space_w=_dc.GetTextExtent(' '*space_count)[0] 0057 for _cnt in range(1, len(self._col_x)): 0058 self._col_x[_cnt]+=self._col_x[_cnt-1]+_space_w 0059 _w, _h=self.GetClientSizeTuple() 0060 _w=min(self._col_x[-1]-_space_w+self._col_x[0], self._max_w) 0061 self.SetClientSizeWH(_w, _h) 0062 self._view.SetDimensions(0, 0, _w, _h) 0063 0064 def OnPaintView(self, _): 0065 global TEXT_MARGIN_Y 0066 _dc=wx.PaintDC(self._view) 0067 # First, fill the background 0068 _background=self._view.GetBackgroundColour() 0069 _foreground=self._view.GetForegroundColour() 0070 _dc.SetBrush(wx.Brush(_background, wx.SOLID)) 0071 _dc.SetPen(wx.Pen(_foreground, 1, wx.SOLID)) 0072 _dc.DrawRectangle(0, 0, *self._view.GetClientSizeTuple()) 0073 # and then draw the text line by line 0074 _dc.SetTextBackground(_background) 0075 _dc.SetTextForeground(_foreground) 0076 _dc.SetFont(self._view.GetFont()) 0077 _y=TEXT_MARGIN_Y 0078 for _line in self._text.split('\n'): 0079 for _idx, _col in enumerate(_line.split('\t')): 0080 _dc.DrawText(_col, self._col_x[_idx], _y) 0081 _y+=self._line_h 0082
Generated by PyXR 0.9.4