From 729f427637dc69b7bb2f25316d538f2288057544 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sun, 21 Oct 2001 03:43:58 +0000 Subject: [PATCH] Added wxRightTextCtrl from Josu Oyanguren git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12126 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/CHANGES.txt | 2 + wxPython/demo/Main.py | 2 + wxPython/demo/wxRightTextCtrl.py | 48 +++++++++++++++++++ wxPython/wxPython/lib/rightalign.py | 72 +++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 wxPython/demo/wxRightTextCtrl.py create mode 100644 wxPython/wxPython/lib/rightalign.py diff --git a/wxPython/CHANGES.txt b/wxPython/CHANGES.txt index cc795736ed..30dc7151d2 100644 --- a/wxPython/CHANGES.txt +++ b/wxPython/CHANGES.txt @@ -67,6 +67,8 @@ DrawPointList. Added a set of sophisticated Error Dialogs from Chris Fama. +Added wxRightTextCtrl from Josu Oyanguren to wxPython.lib for aligning +text in a wxTextCtrl to the right side. diff --git a/wxPython/demo/Main.py b/wxPython/demo/Main.py index 95079ef532..e0ca92f260 100644 --- a/wxPython/demo/Main.py +++ b/wxPython/demo/Main.py @@ -32,6 +32,7 @@ _treeList = [ 'wxFindReplaceDialog', 'DrawXXXList', 'ErrorDialogs', + 'wxRightTextCtrl', ##'wxPopupWindow', ]), @@ -74,6 +75,7 @@ _treeList = [ 'FileBrowseButton', 'GenericButtons', 'wxEditor', 'ColourSelect', 'ImageBrowser', 'infoframe', 'ColourDB', 'PyCrust', 'TablePrint', + 'wxRightTextCtrl', ]), ('Cool Contribs', ['pyTree', 'hangman', 'SlashDot', 'XMLtreeview']), diff --git a/wxPython/demo/wxRightTextCtrl.py b/wxPython/demo/wxRightTextCtrl.py new file mode 100644 index 0000000000..47428fc642 --- /dev/null +++ b/wxPython/demo/wxRightTextCtrl.py @@ -0,0 +1,48 @@ + +from wxPython.wx import * +from wxPython.lib.rightalign import wxRightTextCtrl +import wxPython.lib.rightalign + + +#---------------------------------------------------------------------- + +class TestPanel(wxPanel): + def __init__(self, parent): + wxPanel.__init__(self, parent, -1) + + fgs = wxFlexGridSizer(cols=2, vgap=5, hgap=5) + txt = wxStaticText(self, -1, + "These text controls will align their contents\n" + "to the right when they don't have focus.", style=wxALIGN_RIGHT ) + fgs.Add(txt) + fgs.Add(wxRightTextCtrl(self, -1, "", size=(75, -1))) + + fgs.Add(10,10) + fgs.Add(wxRightTextCtrl(self, -1, "123.45", size=(75, -1))) + + fgs.Add(10,10) + fgs.Add(wxRightTextCtrl(self, -1, "234.56", size=(75, -1))) + + fgs.Add(10,10) + fgs.Add(wxRightTextCtrl(self, -1, "345.67", size=(75, -1))) + + fgs.Add(10,10) + fgs.Add(wxRightTextCtrl(self, -1, "456.78", size=(75, -1))) + + sizer = wxBoxSizer(wxVERTICAL) + sizer.Add(fgs, 0, wxALL, 25) + + self.SetSizer(sizer) + self.SetAutoLayout(true) + + + +#---------------------------------------------------------------------- + +def runTest(frame, nb, log): + win = TestPanel(nb) + return win + +#---------------------------------------------------------------------- + +overview = wxPython.lib.rightalign.__doc__ diff --git a/wxPython/wxPython/lib/rightalign.py b/wxPython/wxPython/lib/rightalign.py new file mode 100644 index 0000000000..5c363ef4ac --- /dev/null +++ b/wxPython/wxPython/lib/rightalign.py @@ -0,0 +1,72 @@ +#---------------------------------------------------------------------- +# Name: wxPython.lib.rightalign +# Purpose: A class derived from wxTextCtrl that aligns the text +# on the right side of the control, (except when editing.) +# +# Author: Josu Oyanguren +# +# Created: 19-October-2001 +# RCS-ID: $Id$ +# Copyright: (c) 2001 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + +""" +Some time ago, I asked about how to right-align +wxTextCtrls. Answer was that it is not supported. I forgot it. + +Just a week ago, one of my clients asked me to have numbers right +aligned. (Indeed it was that numbers MUST be right aligned). + +So the game begun. Hacking, hacking, ... + +At last, i succeed. Here is some code that someone may find +useful. ubRightTextCtrl is right-aligned when you are not editing, but +left-aligned if it has focus. + +Hope this can help someone, as much as this list helps me. + +Josu Oyanguren +Ubera Servicios Informáticos. +""" + +from wxPython.wx import * + +#---------------------------------------------------------------------- + +class wxRightTextCtrl(wxTextCtrl): + def __init__(self, parent, id, *args, **kwargs): + wxTextCtrl.__init__(self, parent, id, *args, **kwargs) + EVT_KILL_FOCUS(self, self.OnKillFocus) + EVT_PAINT(self, self.OnPaint) + + def OnPaint(self, event): + dc = wxPaintDC(self) + dc.SetFont(self.GetFont()) + dc.Clear() + text = self.GetValue() + textwidth, textheight = dc.GetTextExtent(text) + dcwidth, dcheight = self.GetClientSizeTuple() + + y = (dcheight - textheight) / 2 + x = dcwidth - textwidth - 2 + + dc.SetClippingRegion(0, 0, dcwidth, dcheight) + dc.DrawText(text, x, y) + + if x < 0: + toofat = '...' + markwidth = dc.GetTextExtent(toofat)[0] + dc.SetPen(wxPen(dc.GetBackground().GetColour(), 1, wxSOLID )) + dc.DrawRectangle(0,0, markwidth, dcheight) + dc.SetPen(wxPen(wxRED, 1, wxSOLID )) + dc.SetBrush(wxTRANSPARENT_BRUSH) + dc.DrawRectangle(1, 1, dcwidth-2, dcheight-2) + dc.DrawText(toofat, 1, y) + + + def OnKillFocus(self, event): + if not self.GetParent(): return + self.Refresh() + event.Skip() + -- 2.45.2