StaticTextCtrl.py :  » IDE » Boa-Constructor » boa-constructor-0.6.1 » bcrtl » user » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » IDE » Boa Constructor 
Boa Constructor » boa constructor 0.6.1 » bcrtl » user » StaticTextCtrl.py
import wx

class CaptionedCtrlMixin:
    """ Mixin to attach a caption to a control
    
    The caption can be aligned to the top or left of the text control
    """
    def __init__(self, parent, caption):
        self.StaticText = wx.StaticText(parent, -1, caption)
        self._captionAlignment = wx.TOP
        self._captionOffset = wx.Point(0, 0)
        self.updateStaticTextPos()

        self.PushEventHandler(_PosEvtHandler(self))
        
    
    def updateStaticTextPos(self):
        pos = self.GetPosition()
        offset = self._captionOffset
        if self.StaticText:
            if self._captionAlignment == wx.TOP:
                self.StaticText.SetPosition(\
                      (pos.x + offset.x, 
                       offset.y + pos.y - self.StaticText.GetSize().y-5) )
            elif self._captionAlignment == wx.LEFT:
                self.StaticText.SetPosition(\
                      (offset.x + pos.x - self.StaticText.GetSize().x -5, 
                       offset.y + pos.y) )
        self.StaticText.Refresh(1)
    
    def Destroy(self):
        self.PopEventHandler(1)

        self.StaticText.Destroy()
        self.StaticText = None
    
#---Properties------------------------------------------------------------------
    def GetCaption(self):
        return self.StaticText.GetLabel()
    
    def SetCaption(self, caption):
        self.StaticText.SetLabel(caption)
        self.updateStaticTextPos()
    
    def GetCaptionAlignment(self):
        return self._captionAlignment
        
    def SetCaptionAlignment(self, align):
        if align not in (wx.LEFT, wx.TOP):
            raise 'Unsupported alignment'
        self._captionAlignment = align
        self.updateStaticTextPos()

    def GetCaptionOffset(self):
        return self._captionOffset

    def SetCaptionOffset(self, offset):
        self._captionOffset = offset
        self.updateStaticTextPos()

class StaticTextCtrl(wx.TextCtrl, CaptionedCtrlMixin):
    """ Text Control with attached Static Text caption
    
    The caption can be aligned to the top or left of the text control
    """
    def __init__(self, parent, id, value = '', caption = '', 
              pos = wx.DefaultPosition, size = wx.DefaultSize, style = 0, 
              validator = wx.DefaultValidator, name = 'text'):
        wx.TextCtrl.__init__(self, parent, id, value, pos, size, style, 
              validator, name)
        CaptionedCtrlMixin.__init__(self, parent, caption)

    def Destroy(self):
        CaptionedCtrlMixin.Destroy(self)
        wx.TextCtrl.Destroy(self)


##class wxStaticTextCtrl(wx.wxTextCtrl):
##    """ Text Control with attached Static Text caption
##    
##    The caption can be aligned to the top or left of the text control
##    """
##    def __init__(self, parent, id, value = '', caption = '', 
##              pos = wx.wxDefaultPosition, size = wx.wxDefaultSize, style = 0, 
##              validator = wx.wxDefaultValidator, name = 'text'):
##        wx.wxTextCtrl.__init__(self, parent, id, value, pos, size, style, 
##              validator, name)
##
##        self.StaticText = wx.wxStaticText(parent, -1, caption)
##        self._captionAlignment = wx.wxTOP
##        self._captionOffset = wx.wxPoint(0, 0)
##        self.updateStaticTextPos()
##
##        self.PushEventHandler(_PosEvtHandler(self))
##        
##    
##    def updateStaticTextPos(self):
##        pos = self.GetPosition()
##        offset = self._captionOffset
##        if self.StaticText:
##            if self._captionAlignment == wx.wxTOP:
##                self.StaticText.SetPosition(\
##                      (pos.x + offset.x, 
##                       offset.y + pos.y - self.StaticText.GetSize().y-5) )
##            elif self._captionAlignment == wx.wxLEFT:
##                self.StaticText.SetPosition(\
##                      (offset.x + pos.x - self.StaticText.GetSize().x -5, 
##                       offset.y + pos.y) )
##        self.StaticText.Refresh(1)
##    
##    def Destroy(self):
##        self.PopEventHandler(1)
##
##        self.StaticText.Destroy()
##        self.StaticText = None
##        wx.wxTextCtrl.Destroy(self)
##    
###---Properties------------------------------------------------------------------
##    def GetCaption(self):
##        return self.StaticText.GetLabel()
##    
##    def SetCaption(self, caption):
##        self.StaticText.SetLabel(caption)
##        self.updateStaticTextPos()
##    
##    def GetCaptionAlignment(self):
##        return self._captionAlignment
##        
##    def SetCaptionAlignment(self, align):
##        if align not in (wx.wxLEFT, wx.wxTOP):
##            raise 'Unsupported alignment'
##        self._captionAlignment = align
##        self.updateStaticTextPos()
##
##    def GetCaptionOffset(self):
##        return self._captionOffset
##
##    def SetCaptionOffset(self, offset):
##        self._captionOffset = offset
##        self.updateStaticTextPos()


class _PosEvtHandler(wx.EvtHandler):
    """ Custom position event handler that moves the caption with the text ctrl

    An wxEventHandler is used so that user can still use the OnMove event 
    """
    def __init__(self, ctrl):
        wx.EvtHandler.__init__(self)
        wx.EVT_MOVE(ctrl, self.OnMove)
        self.ctrl = ctrl
        
    def OnMove(self, event):
        event.Skip()
        self.ctrl.updateStaticTextPos()
              
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.