Module tipwin
[hide private]
[frames] | no frames]

Source Code for Module tipwin

 1  #!/usr/bin/env python 
 2   
 3  ### BITPIM 
 4  ### 
 5  ### Copyright (C) 2006 Joe Pham<djpham@bitpim.org> 
 6  ### 
 7  ### This program is free software; you can redistribute it and/or modify 
 8  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
 9  ### 
10  ### $Id: tipwin.py 3676 2006-11-14 03:25:45Z djpham $ 
11   
12  """ 
13  A TipWindow class that can properly handle tab(\t) characters. 
14  Limitation: This class does not do text wrapping! 
15  """ 
16   
17  import wx 
18   
19  space_count=4   # expand a tab by the # of spaces 
20  TEXT_MARGIN_X=3 
21  TEXT_MARGIN_Y=3 
22  parentclass=wx.TipWindow 
23 -class TipWindow(parentclass):
24 - def __init__(self, parent, text, maxwidth=100, rectbound=None):
25 global TEXT_MARGIN_X 26 super(TipWindow, self).__init__(parent, text, maxwidth, rectbound) 27 _children_wins=self.GetChildren() 28 if len(_children_wins)!=1: 29 # can't find the view window, bail 30 return 31 self._view=_children_wins[0] 32 self._text=text 33 self._max_w=maxwidth 34 self._line_h=0 35 self._col_x=[TEXT_MARGIN_X] 36 self.adjust() 37 wx.EVT_PAINT(self._view, self.OnPaintView)
38
39 - def adjust(self):
40 # adjust the width of this window if there tabs 41 global space_count 42 if self._text.find('\t')==-1: 43 # no tab, bail 44 return 45 _dc=wx.ClientDC(self._view) 46 _dc.SetFont(self._view.GetFont()) 47 for _line in self._text.split('\n'): 48 _cnt=0 49 for _col in _line.split('\t'): 50 _cnt+=1 51 if _cnt>=len(self._col_x): 52 self._col_x.append(0) 53 _w, _h=_dc.GetTextExtent(_col) 54 self._col_x[_cnt]=max(_w, self._col_x[_cnt]) 55 self._line_h=max(self._line_h, _h) 56 _space_w=_dc.GetTextExtent(' '*space_count)[0] 57 for _cnt in range(1, len(self._col_x)): 58 self._col_x[_cnt]+=self._col_x[_cnt-1]+_space_w 59 _w, _h=self.GetClientSizeTuple() 60 _w=min(self._col_x[-1]-_space_w+self._col_x[0], self._max_w) 61 self.SetClientSizeWH(_w, _h) 62 self._view.SetDimensions(0, 0, _w, _h)
63
64 - def OnPaintView(self, _):
65 global TEXT_MARGIN_Y 66 _dc=wx.PaintDC(self._view) 67 # First, fill the background 68 _background=self._view.GetBackgroundColour() 69 _foreground=self._view.GetForegroundColour() 70 _dc.SetBrush(wx.Brush(_background, wx.SOLID)) 71 _dc.SetPen(wx.Pen(_foreground, 1, wx.SOLID)) 72 _dc.DrawRectangle(0, 0, *self._view.GetClientSizeTuple()) 73 # and then draw the text line by line 74 _dc.SetTextBackground(_background) 75 _dc.SetTextForeground(_foreground) 76 _dc.SetFont(self._view.GetFont()) 77 _y=TEXT_MARGIN_Y 78 for _line in self._text.split('\n'): 79 for _idx, _col in enumerate(_line.split('\t')): 80 _dc.DrawText(_col, self._col_x[_idx], _y) 81 _y+=self._line_h
82