]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/rightalign.py
Reworked how stock objects are initialized. They now have an
[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 # 12/11/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 #
16 # o 2.5 compatability update.
17 # o Added deprecation warning.
18 #
19
20 """
21 Some time ago, I asked about how to right-align
22 wxTextCtrls. Answer was that it is not supported. I forgot it.
23
24 Just a week ago, one of my clients asked me to have numbers right
25 aligned. (Indeed it was that numbers MUST be right aligned).
26
27 So the game begun. Hacking, hacking, ...
28
29 At last, i succeed. Here is some code that someone may find
30 useful. ubRightTextCtrl is right-aligned when you are not editing, but
31 left-aligned if it has focus.
32
33 Hope this can help someone, as much as this list helps me.
34
35 Josu Oyanguren
36 Ubera Servicios Informáticos.
37
38
39 P.S. This only works well on wxMSW.
40 """
41
42 import warnings
43 import wx
44
45 #----------------------------------------------------------------------
46
47 warningmsg = r"""\
48
49 ##############################################################\
50 # THIS MODULE IS DEPRECATED |
51 # |
52 # This control still functions, but it is deprecated because |
53 # wx.TextCtrl now supports the wx.TE_RIGHT style flag |
54 ##############################################################/
55
56
57 """
58
59 warnings.warn(warningmsg, DeprecationWarning, stacklevel=2)
60
61 #----------------------------------------------------------------------
62
63 class wxRightTextCtrl(wx.TextCtrl):
64 def __init__(self, parent, id, *args, **kwargs):
65 wx.TextCtrl.__init__(self, parent, id, *args, **kwargs)
66 self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
67 self.Bind(wx.EVT_PAINT, self.OnPaint)
68
69 def OnPaint(self, event):
70 dc = wx.PaintDC(self)
71 dc.SetFont(self.GetFont())
72 dc.Clear()
73 text = self.GetValue()
74 textwidth, textheight = dc.GetTextExtent(text)
75 dcwidth, dcheight = self.GetClientSize()
76
77 y = (dcheight - textheight) / 2
78 x = dcwidth - textwidth - 2
79
80 if self.IsEnabled():
81 fclr = self.GetForegroundColour()
82 else:
83 fclr = wx.SystemSettings_GetColour(wx.SYS_COLOUR_GRAYTEXT)
84
85 dc.SetTextForeground(fclr)
86
87 dc.SetClippingRegion(0, 0, dcwidth, dcheight)
88 dc.DrawText(text, (x, y))
89
90 if x < 0:
91 toofat = '...'
92 markwidth = dc.GetTextExtent(toofat)[0]
93 dc.SetPen(wx.Pen(dc.GetBackground().GetColour(), 1, wx.SOLID ))
94 dc.DrawRectangle((0,0), (markwidth, dcheight))
95 dc.SetPen(wx.Pen(wx.RED, 1, wx.SOLID ))
96 dc.SetBrush(wx.TRANSPARENT_BRUSH)
97 dc.DrawRectangle((1, 1), (dcwidth-2, dcheight-2))
98 dc.DrawText(toofat, (1, y))
99
100
101 def OnKillFocus(self, event):
102 if not self.GetParent(): return
103 self.Refresh()
104 event.Skip()
105