| 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 | # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
| 20 | # |
| 21 | # o wxRightTextCtrl -> RightTextCtrl |
| 22 | # |
| 23 | |
| 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 |
| 40 | Ubera Servicios Informaticos. |
| 41 | |
| 42 | |
| 43 | P.S. This only works well on wxMSW. |
| 44 | """ |
| 45 | |
| 46 | import warnings |
| 47 | import wx |
| 48 | |
| 49 | #---------------------------------------------------------------------- |
| 50 | |
| 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 | |
| 67 | class RightTextCtrl(wx.TextCtrl): |
| 68 | def __init__(self, parent, id, *args, **kwargs): |
| 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) |
| 72 | |
| 73 | def OnPaint(self, event): |
| 74 | dc = wx.PaintDC(self) |
| 75 | dc.SetFont(self.GetFont()) |
| 76 | dc.Clear() |
| 77 | text = self.GetValue() |
| 78 | textwidth, textheight = dc.GetTextExtent(text) |
| 79 | dcwidth, dcheight = self.GetClientSize() |
| 80 | |
| 81 | y = (dcheight - textheight) / 2 |
| 82 | x = dcwidth - textwidth - 2 |
| 83 | |
| 84 | if self.IsEnabled(): |
| 85 | fclr = self.GetForegroundColour() |
| 86 | else: |
| 87 | fclr = wx.SystemSettings_GetColour(wx.SYS_COLOUR_GRAYTEXT) |
| 88 | |
| 89 | dc.SetTextForeground(fclr) |
| 90 | |
| 91 | dc.SetClippingRegion((0, 0), (dcwidth, dcheight)) |
| 92 | dc.DrawText(text, x, y) |
| 93 | |
| 94 | if x < 0: |
| 95 | toofat = '...' |
| 96 | markwidth = dc.GetTextExtent(toofat)[0] |
| 97 | dc.SetPen(wx.Pen(dc.GetBackground().GetColour(), 1, wx.SOLID )) |
| 98 | dc.DrawRectangle(0,0, markwidth, dcheight) |
| 99 | dc.SetPen(wx.Pen(wx.RED, 1, wx.SOLID )) |
| 100 | dc.SetBrush(wx.TRANSPARENT_BRUSH) |
| 101 | dc.DrawRectangle(1, 1, dcwidth-2, dcheight-2) |
| 102 | dc.DrawText(toofat, 1, y) |
| 103 | |
| 104 | |
| 105 | def OnKillFocus(self, event): |
| 106 | if not self.GetParent(): return |
| 107 | self.Refresh() |
| 108 | event.Skip() |
| 109 | |