]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/rightalign.py
Fixed the code to actually work.
[wxWidgets.git] / wxPython / wx / lib / rightalign.py
1 # -*- coding: iso-8859-1 -*-
2 #----------------------------------------------------------------------
3 # Name: wxPython.lib.rightalign
4 # Purpose: A class derived from wxTextCtrl that aligns the text
5 # on the right side of the control, (except when editing.)
6 #
7 # Author: Josu Oyanguren
8 #
9 # Created: 19-October-2001
10 # RCS-ID: $Id$
11 # Copyright: (c) 2001 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
14
15 """
16 Some time ago, I asked about how to right-align
17 wxTextCtrls. Answer was that it is not supported. I forgot it.
18
19 Just a week ago, one of my clients asked me to have numbers right
20 aligned. (Indeed it was that numbers MUST be right aligned).
21
22 So the game begun. Hacking, hacking, ...
23
24 At last, i succeed. Here is some code that someone may find
25 useful. ubRightTextCtrl is right-aligned when you are not editing, but
26 left-aligned if it has focus.
27
28 Hope this can help someone, as much as this list helps me.
29
30 Josu Oyanguren
31 Ubera Servicios Informáticos.
32
33
34 P.S. This only works well on wxMSW.
35 """
36
37 from wxPython.wx import *
38
39 #----------------------------------------------------------------------
40
41 class wxRightTextCtrl(wxTextCtrl):
42 def __init__(self, parent, id, *args, **kwargs):
43 wxTextCtrl.__init__(self, parent, id, *args, **kwargs)
44 EVT_KILL_FOCUS(self, self.OnKillFocus)
45 EVT_PAINT(self, self.OnPaint)
46
47 def OnPaint(self, event):
48 dc = wxPaintDC(self)
49 dc.SetFont(self.GetFont())
50 dc.Clear()
51 text = self.GetValue()
52 textwidth, textheight = dc.GetTextExtent(text)
53 dcwidth, dcheight = self.GetClientSizeTuple()
54
55 y = (dcheight - textheight) / 2
56 x = dcwidth - textwidth - 2
57
58 if self.IsEnabled():
59 fclr = self.GetForegroundColour()
60 else:
61 fclr = wxSystemSettings_GetColour(wxSYS_COLOUR_GRAYTEXT)
62 dc.SetTextForeground(fclr)
63
64 dc.SetClippingRegion(0, 0, dcwidth, dcheight)
65 dc.DrawText(text, x, y)
66
67 if x < 0:
68 toofat = '...'
69 markwidth = dc.GetTextExtent(toofat)[0]
70 dc.SetPen(wxPen(dc.GetBackground().GetColour(), 1, wxSOLID ))
71 dc.DrawRectangle(0,0, markwidth, dcheight)
72 dc.SetPen(wxPen(wxRED, 1, wxSOLID ))
73 dc.SetBrush(wxTRANSPARENT_BRUSH)
74 dc.DrawRectangle(1, 1, dcwidth-2, dcheight-2)
75 dc.DrawText(toofat, 1, y)
76
77
78 def OnKillFocus(self, event):
79 if not self.GetParent(): return
80 self.Refresh()
81 event.Skip()
82