#!/usr/bin/python

"""
Demo of a window having a hatched background pattern.
"""

import sys
import os

import wx

#---------------------------------------------------------------------------
############################################################################
#---------------------------------------------------------------------------

class ParentContainerControl( wx.Frame ) :
   
    def __init__( self, parent ) :
        
        titleTxt = 'HatchedWindow'
        wx.Frame.__init__( self, parent, -1, title=titleTxt )
        self.SetClientSize( (400, 350) )
        self.Bind( wx.EVT_SIZE, self.OnSize )
        
        self.frmPanel = wx.Panel( self, -1)
        self.frmPanel.SetBackgroundColour( (240, 200, 175) )    # pink
        self.frmPanel.Bind( wx.EVT_LEFT_DCLICK, self.OnClose )
        
        #-----
        
        self.CreateStatusBar()
        statusTxt = 'HatchedWindow managed with position and size'
        self.SetStatusText( statusTxt )
        
        #-----
        
        # Every wx.EVT_SIZE event will position and size self.hatchedWin.
        # Leaving __init__ will cause the first ex.EVT_SIZE event,
        #   so, there's no need to do anything here in __init__
        #   in additiona to just creating the hatched window object.
        self.hatchedWinBorder = 15      # arbitrary value
        self.hatchedWin = HatchedWindow( self, self.hatchedWinBorder )
        
    #end __init__
    
    #----------------------------------
    
    def OnSize( self, event ) :
        """
        Set the position and size of the hatched window.
        This is specifically in lieu of using a sizer to manage the hatched window.
        A wx.EVT_SIZE event in this parent object generates a wx.EVT_PAINT event 
        in all child objects created in this class (meaning, instantiated from the 
        class definition).
        """
        
        # Manage this classes own panel.
        self.frmPanel.SetSize( self.GetClientSize() )
        
        #-----
        
        # Set the position and size of the hatched window.
        self.CenterControlInClient( self.hatchedWin, self.hatchedWinBorder)
        
        self.Refresh()
        
    #end def
    
    #----------------------------------
    
    def CenterControlInClient( self, control, border ) :
        
        control.SetPosition( (border, border) )
        
        selfSizeX, selfSizeY = self.GetClientSize()
        controlSizeX = selfSizeX - 2*border
        controlSizeY = selfSizeY - 2*border
        control.SetSize( (controlSizeX, controlSizeY) )
        
    #end def
    
    #----------------------------------
    
    def OnClose( self, event ) :
        
        self.Destroy()      # Exit app.MainLoop()
        
    #end def

#end ParentContainerControl class

#---------------------------------------------------------------------------
############################################################################
#---------------------------------------------------------------------------

class HatchedWindow( wx.Window ) :
    
    def __init__( self, parent, border=30 ) :
        
        self.border = border
        
        wx.Window.__init__( self, parent, -1 )
        self.SetBackgroundColour( 'WHITE' )             # use any color
        self.Bind( wx.EVT_PAINT, self.OnPaint )
        
    #end __init__
    
    #----------------------------------
    
    def OnPaint( self, event=None ) :
        
        dc = wx.PaintDC( self )
        selfSizeX, selfSizeY = self.GetSize()
        
        # Define the background pattern. The HatchedWindow background color is retained.
        hatchColor = (0, 75, 0)                 # dark green - use any color
        dc.SetPen( wx.Pen( hatchColor, 1 ) )
        bgPattern = wx.BDIAGONAL_HATCH          # See the various wx.Brush() styles.
        dc.SetBrush( wx.Brush( hatchColor, bgPattern ) )
        
        dc.DrawRectangle( 0, 0, selfSizeX, selfSizeY )
        
        """ 
        # Overdraw the original rectangle outline with another color.
        dc.SetPen( wx.Pen( 'wHiTe', 5 ) )                   # use any color
        dc.SetBrush( wx.Brush( 'BluE', wx.TRANSPARENT ) )   # Leave the interior intact
        dc.DrawRectangle( 0, 0, selfSizeX, selfSizeY )
        """ 
        
        event.Skip()
        
    #end OnPaint def
        
#end PanelHatchedBG class

#==============================================================================

if __name__ == '__main__'  :
    
    app = wx.App( redirect=False )
    
    appFrame = ParentContainerControl( None )
    appFrame.Show()
    
    app.MainLoop()
        
#end if
