]>
git.saurik.com Git - wxWidgets.git/blob - 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.)
7 # Author: Josu Oyanguren
9 # Created: 19-October-2001
11 # Copyright: (c) 2001 by Total Control Software
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
16 Some time ago, I asked about how to right-align
17 wxTextCtrls. Answer was that it is not supported. I forgot it.
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).
22 So the game begun. Hacking, hacking, ...
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.
28 Hope this can help someone, as much as this list helps me.
31 Ubera Servicios Informáticos.
34 P.S. This only works well on wxMSW.
37 from wxPython
.wx
import *
39 #----------------------------------------------------------------------
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
)
47 def OnPaint(self
, event
):
49 dc
.SetFont(self
.GetFont())
51 text
= self
.GetValue()
52 textwidth
, textheight
= dc
.GetTextExtent(text
)
53 dcwidth
, dcheight
= self
.GetClientSizeTuple()
55 y
= (dcheight
- textheight
) / 2
56 x
= dcwidth
- textwidth
- 2
59 fclr
= self
.GetForegroundColour()
61 fclr
= wxSystemSettings_GetColour(wxSYS_COLOUR_GRAYTEXT
)
62 dc
.SetTextForeground(fclr
)
64 dc
.SetClippingRegion(0, 0, dcwidth
, dcheight
)
65 dc
.DrawText(text
, x
, y
)
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
)
78 def OnKillFocus(self
, event
):
79 if not self
.GetParent(): return