<s> """<STR_LIT>""" <EOL> import wx <EOL> from pyface . widget import Widget <EOL> from traits . api import Bool , Dict , Int , List , Tuple <EOL> HTML = """<STR_LIT>""" <EOL> PART = """<STR_LIT>""" <EOL> class ToolPalette ( Widget ) : <EOL> tools = List <EOL> id_tool_map = Dict <EOL> tool_id_to_button_map = Dict <EOL> button_size = Tuple ( ( <NUM_LIT> , <NUM_LIT> ) , Int , Int ) <EOL> is_realized = Bool ( False ) <EOL> tool_listeners = Dict <EOL> button_tool_map = Dict <EOL> def __init__ ( self , parent , ** traits ) : <EOL> """<STR_LIT>""" <EOL> super ( ToolPalette , self ) . __init__ ( ** traits ) <EOL> self . control = self . _create_control ( parent ) <EOL> return <EOL> def add_tool ( self , label , bmp , kind , tooltip , longtip ) : <EOL> """<STR_LIT>""" <EOL> wxid = wx . NewId ( ) <EOL> params = ( wxid , label , bmp , kind , tooltip , longtip ) <EOL> self . tools . append ( params ) <EOL> self . id_tool_map [ wxid ] = params <EOL> if self . is_realized : <EOL> self . _reflow ( ) <EOL> return wxid <EOL> def toggle_tool ( self , id , checked ) : <EOL> """<STR_LIT>""" <EOL> button = self . tool_id_to_button_map . get ( id , None ) <EOL> if button is not None and hasattr ( button , '<STR_LIT>' ) : <EOL> button . SetToggle ( checked ) <EOL> return <EOL> def enable_tool ( self , id , enabled ) : <EOL> """<STR_LIT>""" <EOL> button = self . tool_id_to_button_map . get ( id , None ) <EOL> if button is not None : <EOL> button . SetEnabled ( enabled ) <EOL> return <EOL> def on_tool_event ( self , id , callback ) : <EOL> """<STR_LIT>""" <EOL> callbacks = self . tool_listeners . setdefault ( id , [ ] ) <EOL> callbacks . append ( callback ) <EOL> return <EOL> def realize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . is_realized = True <EOL> self . _reflow ( ) <EOL> return <EOL> def get_tool_state ( self , id ) : <EOL> """<STR_LIT>""" <EOL> button = self . tool_id_to_button_map . get ( id , None ) <EOL> if hasattr ( button , '<STR_LIT>' ) : <EOL> if button . GetToggle ( ) : <EOL> state = <NUM_LIT:1> <EOL> else : <EOL> state = <NUM_LIT:0> <EOL> else : <EOL> state = <NUM_LIT:0> <EOL> return state <EOL> def _create_control ( self , parent ) : <EOL> html_window = wx . html . HtmlWindow ( parent , - <NUM_LIT:1> , style = wx . CLIP_CHILDREN ) <EOL> return html_window <EOL> def _reflow ( self ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> for param in self . tools : <EOL> parts . append ( PART % ( str ( param [ <NUM_LIT:0> ] ) , self . button_size ) ) <EOL> html = HTML % '<STR_LIT>' . join ( parts ) <EOL> self . control . SetPage ( html ) <EOL> for param in self . tools : <EOL> self . _initialize_tool ( param ) <EOL> return <EOL> def _initialize_tool ( self , param ) : <EOL> """<STR_LIT>""" <EOL> wxid , label , bmp , kind , tooltip , longtip = param <EOL> panel = self . control . FindWindowById ( wxid ) <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> panel . SetSizer ( sizer ) <EOL> panel . SetAutoLayout ( True ) <EOL> panel . SetWindowStyleFlag ( wx . CLIP_CHILDREN ) <EOL> from wx . lib . buttons import GenBitmapToggleButton , GenBitmapButton <EOL> if kind == '<STR_LIT>' : <EOL> button = GenBitmapToggleButton ( panel , - <NUM_LIT:1> , None , size = self . button_size ) <EOL> else : <EOL> button = GenBitmapButton ( panel , - <NUM_LIT:1> , None , size = self . button_size ) <EOL> self . button_tool_map [ button . GetId ( ) ] = wxid <EOL> self . tool_id_to_button_map [ wxid ] = button <EOL> wx . EVT_BUTTON ( panel , button . GetId ( ) , self . _on_button ) <EOL> button . SetBitmapLabel ( bmp ) <EOL> button . SetToolTipString ( label ) <EOL> sizer . Add ( button , <NUM_LIT:0> , wx . EXPAND ) <EOL> return <EOL> def _on_button ( self , event ) : <EOL> button_id = event . GetId ( ) <EOL> tool_id = self . button_tool_map . get ( button_id , None ) <EOL> if tool_id is not None : <EOL> for listener in self . tool_listeners . get ( tool_id , [ ] ) : <EOL> listener ( event ) <EOL> return </s>
