<commit_before>#------------------------------------------------------------------------------
#
#  Copyright (c) 2005, Enthought, Inc.
#  All rights reserved.
# 
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#  Thanks for using Enthought open source!
# 
#  Author: <NAME>
#
#  Description: Image and text-based pyface button/toolbar/radio button control.
#
#------------------------------------------------------------------------------

""" An image and text-based control that can be used as a normal, radio or 
    toolbar button.
"""

#-------------------------------------------------------------------------------
#  Imports:  
#-------------------------------------------------------------------------------

import wx

from enthought.util.numerix \
    import array, fromstring, reshape, UnsignedInt8, ravel

from enthought.traits.api \
    import Str, Range, Enum, Instance, Event, false

from widget \
    import Widget
    
from image_resource \
    import ImageResource
    
#-------------------------------------------------------------------------------
#  Constants:  
#-------------------------------------------------------------------------------
        
# Text color used when a button is disabled:
DisabledTextColor = wx.Colour( 128, 128, 128 )

#-------------------------------------------------------------------------------
#  'ImageButton' class:  
#-------------------------------------------------------------------------------

class ImageButton ( Widget ):
    """ An image and text-based control that can be used as a normal, radio or 
        toolbar button.
    """

    # Pens used to draw the 'selection' marker:
    _selectedPenDark = wx.Pen( 
        wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DSHADOW ), 1, wx.SOLID
    )

    _selectedPenLight = wx.Pen( 
        wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DHIGHLIGHT ), 1, wx.SOLID
    )

    #---------------------------------------------------------------------------
    #  Trait definitions:  
    #---------------------------------------------------------------------------
        
    # The image:
    image = Instance( ImageResource, allow_none = True )
    
    # The (optional) label:
    label = Str
    
    # Extra padding to add to both the left and right sides:
    width_padding = Range( 0, 31, 7 )
    
    # Extra padding to add to both the top and bottom sides:
    height_padding = Range( 0, 31, 5 )
    
    # Presentation style:
    style = Enum( 'button', 'radio', 'toolbar', 'checkbox' )
    
    # Orientation of the text relative to the image:
    orientation = Enum( 'vertical', 'horizontal' )
    
    # Is the control selected ('radio' or 'checkbox' style)?
    selected = false
    
    # Fired when a 'button' or 'toolbar' style control is clicked: 
    clicked = Event

    #---------------------------------------------------------------------------
    #  Initializes the object:  
    #---------------------------------------------------------------------------
     
    def __init__ ( self, parent, **traits ):
        """ Creates a new image control. 
        """
        super( ImageButton, self ).__init__( **traits )

        # Calculate the size of the button:
        idx = idy = tdx = tdy = 0
        if self._image is not None:
            idx = self._image.GetWidth()
            idy = self._image.GetHeight()
            
        if self.label != '':
            dc = wx.ScreenDC()
            dc.SetFont( wx.NORMAL_FONT )
            tdx, tdy = dc.GetTextExtent( self.label )
        
        wp2 = self.width_padding  + 2 
        hp2 = self.height_padding + 2 
        if self.orientation == 'horizontal':
            self._ix = wp2
            spacing  = (idx > 0) * (tdx > 0) * 4
            self._tx = self._ix + idx + spacing
            dx       = idx + tdx + spacing
            dy       = max( idy, tdy )
            self._iy = hp2 + ((dy - idy) / 2)
            self._ty = hp2 + ((dy - tdy) / 2) 
        else:
            self._iy = hp2
            spacing  = (idy > 0) * (tdy > 0) * 2
            self._ty = self._iy + idy + spacing
            dx       = max( idx, tdx )<commit_msg>Fixed an attribute error in ImageButton's __init__().<commit_after>#------------------------------------------------------------------------------
#
#  Copyright (c) 2005, Enthought, Inc.
#  All rights reserved.
# 
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#  Thanks for using Enthought open source!
# 
#  Author: <NAME>
#
#  Description: Image and text-based pyface button/toolbar/radio button control.
#
#------------------------------------------------------------------------------

""" An image and text-based control that can be used as a normal, radio or 
    toolbar button.
"""

#-------------------------------------------------------------------------------
#  Imports:  
#-------------------------------------------------------------------------------

import wx

from enthought.util.numerix \
    import array, fromstring, reshape, UnsignedInt8, ravel

from enthought.traits.api \
    import Str, Range, Enum, Instance, Event, false

from widget \
    import Widget
    
from image_resource \
    import ImageResource
    
#-------------------------------------------------------------------------------
#  Constants:  
#-------------------------------------------------------------------------------
        
# Text color used when a button is disabled:
DisabledTextColor = wx.Colour( 128, 128, 128 )

#-------------------------------------------------------------------------------
#  'ImageButton' class:  
#-------------------------------------------------------------------------------

class ImageButton ( Widget ):
    """ An image and text-based control that can be used as a normal, radio or 
        toolbar button.
    """

    # Pens used to draw the 'selection' marker:
    _selectedPenDark = wx.Pen( 
        wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DSHADOW ), 1, wx.SOLID
    )

    _selectedPenLight = wx.Pen( 
        wx.SystemSettings_GetColour( wx.SYS_COLOUR_3DHIGHLIGHT ), 1, wx.SOLID
    )

    #---------------------------------------------------------------------------
    #  Trait definitions:  
    #---------------------------------------------------------------------------
        
    # The image:
    image = Instance( ImageResource, allow_none = True )
    
    # The (optional) label:
    label = Str
    
    # Extra padding to add to both the left and right sides:
    width_padding = Range( 0, 31, 7 )
    
    # Extra padding to add to both the top and bottom sides:
    height_padding = Range( 0, 31, 5 )
    
    # Presentation style:
    style = Enum( 'button', 'radio', 'toolbar', 'checkbox' )
    
    # Orientation of the text relative to the image:
    orientation = Enum( 'vertical', 'horizontal' )
    
    # Is the control selected ('radio' or 'checkbox' style)?
    selected = false
    
    # Fired when a 'button' or 'toolbar' style control is clicked: 
    clicked = Event

    #---------------------------------------------------------------------------
    #  Initializes the object:  
    #---------------------------------------------------------------------------
     
    def __init__ ( self, parent, **traits ):
        """ Creates a new image control. 
        """
        self._image = None

        super( ImageButton, self ).__init__( **traits )

        # Calculate the size of the button:
        idx = idy = tdx = tdy = 0
        if self._image is not None:
            idx = self._image.GetWidth()
            idy = self._image.GetHeight()
            
        if self.label != '':
            dc = wx.ScreenDC()
            dc.SetFont( wx.NORMAL_FONT )
            tdx, tdy = dc.GetTextExtent( self.label )
        
        wp2 = self.width_padding  + 2 
        hp2 = self.height_padding + 2 
        if self.orientation == 'horizontal':
            self._ix = wp2
            spacing  = (idx > 0) * (tdx > 0) * 4
            self._tx = self._ix + idx + spacing
            dx       = idx + tdx + spacing
            dy       = max( idy, tdy )
            self._iy = hp2 + ((dy - idy) / 2)
            self._ty = hp2 + ((dy - tdy) / 2) 
        else:
            self._iy = hp2
            spacing  = (idy > 0) * (tdy > 0) * 2
            self._ty = self._iy + idy + spacing
            dx       = max( idx, tdx )
{'fraction_non_alphanumeric': 0.28817009726306264, 'fraction_numerical': 0.00972630626555078, 'mean_word_length': 1.1160564728403923, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5427630', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2160, 'n_words': 712}