
"""
PenAndBrushStyles.py

Andrea Gavana
2011-08-09

https://groups.google.com/forum/#!topic/wxpython-users/ECgRONRxsmg
"""

import os

import wx

from wx.lib.embeddedimage import PyEmbeddedImage

#------------------------------------------------------------------------------

class PenAndBrushFrame( wx.Frame ) :

    def __init__( self ) :

        wx.Frame.__init__( self, None, title="wx.Pen and wx.Brush Styles", size=(700, 600) )
        
        # Convert a wx.Bitmap to a wx.Icon and then display it as a Frame titlebar graphic.
        iconWxImage  = mondrian.GetBitmap().ConvertToImage().Rescale( 16, 16 )
        # There's no direct way to convert a wx.Bitmap or wx.Image to an wx.Icon.
        iconTmpFilename = './Mondrian.ico'
        iconWxImage.SaveFile( iconTmpFilename, wx.BITMAP_TYPE_ICO )
        icon = wx.Icon( iconTmpFilename, wx.BITMAP_TYPE_ICO )
        os.remove( iconTmpFilename )
        self.SetIcon( icon )
        
        #-----
        
        # Set a Panel into the Frame to standardize the look across all platform types.
        # Every visible control gets put on frmPanel.
        frmPanel = wx.Panel( self )
        frmPanel.SetBackgroundColour( (240, 240, 240) )     # eggshell white or "off-white"

        font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
        font.SetWeight( wx.BOLD )

        frame_vSzr = wx.BoxSizer( wx.VERTICAL )
        
        penLabel = 'Pen Styles (os.name = %s) :' % (os.name)
        penLabel = wx.StaticText( frmPanel, -1, penLabel )
        penLabel.SetFont( font )

        frame_vSzr.Add( penLabel, 0, wx.EXPAND|wx.ALL, 10 )
        
        grdSzr_top = wx.GridSizer( 4, 4, 3, 3 )  # rows, cols, vgap, hgap

        for pen_name in pen_styles :
            small = PenPanel( frmPanel, pen_name )
            grdSzr_top.Add( small, 0, wx.EXPAND )
        #end for
        
        frame_vSzr.Add( grdSzr_top, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, 10 )

        brushLabel = wx.StaticText( frmPanel, -1, "Brush Styles :" )
        brushLabel.SetFont( font )

        frame_vSzr.Add( brushLabel, 0, wx.EXPAND|wx.ALL, 10 )
        
        grdSzr_bot = wx.GridSizer( 3, 3, 3, 3 )  # rows, cols, vgap, hgap

        for aBrushStyle in brush_styles :
            aBrushPanel = BrushPanel( frmPanel, aBrushStyle )
            grdSzr_bot.Add( aBrushPanel, 0, wx.EXPAND )
        #end for
        
        frame_vSzr.Add( grdSzr_bot, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, 10 )
        
        frmPanel.SetSizer( frame_vSzr )
        self.Layout()

    #end __init__
    
#end PenAndBrushFrame class

#------------------------------------------------------------------------------

pen_styles = [ "wx.SOLID", "wx.DOT", "wx.LONG_DASH",
               "wx.SHORT_DASH", "wx.DOT_DASH", "wx.BDIAGONAL_HATCH",
               "wx.CROSSDIAG_HATCH", "wx.FDIAGONAL_HATCH", "wx.CROSS_HATCH",
               "wx.VERTICAL_HATCH", "wx.STIPPLE", "wx.USER_DASH" ]

brush_styles = [ "wx.SOLID", "wx.STIPPLE", "wx.BDIAGONAL_HATCH",
                 "wx.CROSSDIAG_HATCH", "wx.FDIAGONAL_HATCH", "wx.CROSS_HATCH",
                 "wx.HORIZONTAL_HATCH", "wx.VERTICAL_HATCH", "wx.TRANSPARENT" ]


mondrian = PyEmbeddedImage( 
    "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAHFJ"
    "REFUWIXt1jsKgDAQRdF7xY25cpcWC60kioI6Fm/ahHBCMh+BRmGMnAgEWnvPpzK8dvrFCCCA"
    "coD8og4c5Lr6WB3Q3l1TBwLYPuF3YS1gn1HphgEEEABcKERrGy0E3B0HFJg7C1N/f/kTBBBA"
    "+Vi+AMkgFEvBPD17AAAAAElFTkSuQmCC" )

#------------------------------------------------------------------------------

class BasePanel( wx.Panel ) :

    def __init__( self, parent ) :

        wx.Panel.__init__( self, parent, style=wx.SUNKEN_BORDER|wx.WANTS_CHARS )

        self.SetBackgroundStyle( wx.BG_STYLE_CUSTOM )

        self.Bind( wx.EVT_SIZE, self.OnSize )        
        self.Bind( wx.EVT_PAINT, self.OnPaint )


    def OnSize( self, event ) :

        event.Skip()
        self.Refresh()

#end class

#------------------------------------------------------------------------------

class PenPanel( BasePanel ) :

    def __init__( self, parent, pen_name ) :

        BasePanel.__init__( self, parent )
        self.pen_name = pen_name
        
    #end __init__
    
    #----------------------------------
    
    def OnPaint( self, event ) :

        width, height = self.GetClientSize()

        dc = wx.AutoBufferedPaintDC( self )
        dc.SetBackground( wx.WHITE_BRUSH )
        dc.Clear()

        font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
        dc.SetFont( font )

        name = self.pen_name        

        if "STIPPLE" in name :
            patternImg = mondrian.GetBitmap().ConvertToImage()
            patternBmap = patternImg.Rescale( 10, 10 ).ConvertToBitmap()
            pen = wx.Pen( wx.BLUE, patternBmap.GetWidth(), eval( self.pen_name ) )
            pen.SetStipple( patternBmap )
        else :
            pen = wx.Pen( wx.BLUE, 2, eval( name ) )

        if "USER" in name :
            pen.SetDashes( [2, 5] )
            name += " ( [2, 5] )"
        
        dc.SetTextForeground( wx.BLACK )
        dc.DrawText( name, 5, 5 )

        dc.SetPen( pen )
        dc.DrawLine( 5, 25, width-5, 25 )               # horizontal
        dc.DrawLine( 5, 30, width-5, self.Size[1]-10 )  # diagonal U-L to L-R

#end PenPanel class

#------------------------------------------------------------------------------

class BrushPanel( BasePanel ) :

    def __init__( self, parent, aBrushStyle ) :

        BasePanel.__init__( self, parent )
        
        self.aBrushStyle = aBrushStyle
    
    #----------------------------------
    
    def OnPaint( self, event ) :
        
        width, height = self.GetClientSize()
        
        dc = wx.AutoBufferedPaintDC( self )
        dc.SetBackground( wx.WHITE_BRUSH )
        dc.Clear()
        
        font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT )
        dc.SetFont( font )
        
        dc.SetPen( wx.TRANSPARENT_PEN )        
        name = self.aBrushStyle
        
        if "STIPPLE" in name :
            patternImg = mondrian.GetBitmap().ConvertToImage()
            patternImg.Rescale( 10, 10 )
            patternImg.SetMask( False )
            patternBmap = patternImg.ConvertToBitmap()
            brush = wx.BrushFromBitmap( patternBmap )
        else :
            brush = wx.Brush( wx.BLUE, eval( name ) )
        
        dc.SetTextForeground( wx.BLACK )
        dc.DrawText( name, 5, 5 )
        
        if "wx.TRANSPARENT" in name :
            dc.SetPen( wx.Pen( wx.BLUE, 2, wx.SOLID ) )
        dc.SetBrush( brush )
        dc.DrawRectangle( 5, 30, width-10, height-35 )
        
    #end OnPaint def
    
#end BrushPanel class

#==============================================================================

if __name__ == '__main__' :

    app = wx.App( redirect=False )
    
    appFrame = PenAndBrushFrame()
    appFrame.CenterOnScreen()
    appFrame.Show()
    
    app.MainLoop()

#end if
