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