<s> import wx <EOL> import wx . lib . newevent <EOL> from atom . api import Typed <EOL> from enaml . widgets . menu import ProxyMenu <EOL> from . wx_action import WxAction , EVT_ACTION_CHANGED <EOL> from . wx_action_group import WxActionGroup <EOL> from . wx_toolkit_object import WxToolkitObject <EOL> wxMenuChangedEvent , EVT_MENU_CHANGED = wx . lib . newevent . NewEvent ( ) <EOL> class wxMenu ( wx . Menu ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( wxMenu , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _parent = parent <EOL> self . _title = u'<STR_LIT>' <EOL> self . _all_items = [ ] <EOL> self . _menus_map = { } <EOL> self . _actions_map = { } <EOL> self . _enabled = True <EOL> self . _bar_enabled = True <EOL> self . _visible = True <EOL> self . _batch = False <EOL> self . _is_context_menu = False <EOL> self . _id = wx . NewId ( ) <EOL> def _EmitChanged ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _batch : <EOL> event = wxMenuChangedEvent ( ) <EOL> event . SetEventObject ( self ) <EOL> wx . PostEvent ( self , event ) <EOL> def _SetBarEnabled ( self , enabled ) : <EOL> """<STR_LIT>""" <EOL> if self . _bar_enabled != enabled : <EOL> old = self . IsEnabled ( ) <EOL> self . _bar_enabled = enabled <EOL> new = self . IsEnabled ( ) <EOL> if old != new : <EOL> self . _EmitChanged ( ) <EOL> def _InsertMenuItem ( self , index , menu ) : <EOL> """<STR_LIT>""" <EOL> text = menu . GetTitle ( ) <EOL> menu_id = menu . GetId ( ) <EOL> text = text or '<STR_LIT>' % menu_id <EOL> res = wx . MenuItem ( self , menu_id , text , '<STR_LIT>' , subMenu = menu ) <EOL> res . Enable ( menu . IsEnabled ( ) ) <EOL> self . InsertItem ( index , res ) <EOL> return res <EOL> def _InsertActionItem ( self , index , action ) : <EOL> """<STR_LIT>""" <EOL> text = action . GetText ( ) <EOL> help = action . GetStatusTip ( ) <EOL> if action . IsSeparator ( ) : <EOL> res = wx . MenuItem ( self , wx . ID_SEPARATOR , text , help ) <EOL> self . InsertItem ( index , res ) <EOL> else : <EOL> action_id = action . GetId ( ) <EOL> text = text or '<STR_LIT>' % action_id <EOL> if action . IsCheckable ( ) : <EOL> kind = wx . ITEM_CHECK <EOL> res = wx . MenuItem ( self , action_id , text , help , kind ) <EOL> self . InsertItem ( index , res ) <EOL> res . Check ( action . IsChecked ( ) ) <EOL> else : <EOL> kind = wx . ITEM_NORMAL <EOL> res = wx . MenuItem ( self , action_id , text , help , kind ) <EOL> self . InsertItem ( index , res ) <EOL> res . Enable ( action . IsEnabled ( ) ) <EOL> return res <EOL> def OnActionChanged ( self , event ) : <EOL> """<STR_LIT>""" <EOL> event . Skip ( ) <EOL> action = event . GetEventObject ( ) <EOL> item = self . _actions_map . get ( action ) <EOL> visible = action . IsVisible ( ) <EOL> if visible != bool ( item ) : <EOL> if visible : <EOL> index = self . _all_items . index ( action ) <EOL> n_visible = len ( self . _actions_map ) + len ( self . _menus_map ) <EOL> index = min ( index , n_visible ) <EOL> new_item = self . _InsertActionItem ( index , action ) <EOL> self . _actions_map [ action ] = new_item <EOL> else : <EOL> self . DestroyItem ( item ) <EOL> del self . _actions_map [ action ] <EOL> return <EOL> if not item : <EOL> return <EOL> item_sep = item . IsSeparator ( ) <EOL> action_sep = action . IsSeparator ( ) <EOL> if item_sep or action_sep : <EOL> if item_sep != action_sep : <EOL> self . DestroyItem ( item ) <EOL> index = self . _all_items . index ( action ) <EOL> n_visible = len ( self . _actions_map ) + len ( self . _menus_map ) <EOL> index = min ( index , n_visible ) <EOL> new_item = self . _InsertActionItem ( index , action ) <EOL> self . _actions_map [ action ] = new_item <EOL> return <EOL> item . SetItemLabel ( action . GetText ( ) ) <EOL> item . SetHelp ( action . GetStatusTip ( ) ) <EOL> if action . IsCheckable ( ) : <EOL> item . SetKind ( wx . ITEM_CHECK ) <EOL> item . Check ( action . IsChecked ( ) ) <EOL> else : <EOL> if item . IsCheckable ( ) : <EOL> item . Check ( False ) <EOL> item . SetKind ( wx . ITEM_NORMAL ) <EOL> item . Enable ( action . IsEnabled ( ) ) <EOL> def OnMenuChanged ( self , event ) : <EOL> """<STR_LIT>""" <EOL> event . Skip ( ) <EOL> menu = event . GetEventObject ( ) <EOL> item = self . _menus_map . get ( menu ) <EOL> visible = menu . IsVisible ( ) <EOL> if visible != bool ( item ) : <EOL> if visible : <EOL> index = self . _all_items . index ( menu ) <EOL> n_visible = len ( self . _actions_map ) + len ( self . _menus_map ) <EOL> index = min ( index , n_visible ) <EOL> new_item = self . _InsertMenuItem ( index , menu ) <EOL> self . _menus_map [ menu ] = new_item <EOL> else : <EOL> item . SetSubMenu ( None ) <EOL> self . DestroyItem ( item ) <EOL> del self . _menus_map [ menu ] <EOL> return <EOL> if not item : <EOL> return <EOL> item . SetItemLabel ( menu . GetTitle ( ) ) <EOL> item . Enable ( menu . IsEnabled ( ) ) <EOL> def OnShowContextMenu ( self , event ) : <EOL> """<STR_LIT>""" <EOL> parent = self . _parent <EOL> if parent and isinstance ( parent , wx . Window ) : <EOL> parent . PopupMenu ( self ) <EOL> def BeginBatch ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _batch = True <EOL> def EndBatch ( self , emit = True ) : <EOL> """<STR_LIT>""" <EOL> self . _batch = False <EOL> if emit : <EOL> self . _EmitChanged ( ) <EOL> def GetId ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _id <EOL> def GetTitle ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _title <EOL> def SetTitle ( self , title ) : <EOL> """<STR_LIT>""" <EOL> if self . _title != title : <EOL> self . _title = title <EOL> self . _EmitChanged ( ) <EOL> def IsEnabled ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _bar_enabled : <EOL> return self . _enabled <EOL> return False <EOL> def SetEnabled ( self , enabled ) : <EOL> """<STR_LIT>""" <EOL> if self . _enabled != enabled : <EOL> self . _enabled = enabled <EOL> if self . _bar_enabled : <EOL> self . _EmitChanged ( ) <EOL> def IsVisible ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _visible <EOL> def SetVisible ( self , visible ) : <EOL> """<STR_LIT>""" <EOL> if self . _visible != visible : <EOL> self . _visible = visible <EOL> self . _EmitChanged ( ) <EOL> def IsContextMenu ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _is_context_menu <EOL> def SetContextMenu ( self , context ) : <EOL> """<STR_LIT>""" <EOL> old_context = self . _is_context_menu <EOL> self . _is_context_menu = context <EOL> if old_context != context : <EOL> parent = self . _parent <EOL> if parent : <EOL> handler = self . OnShowContextMenu <EOL> if context : <EOL> parent . Bind ( wx . EVT_CONTEXT_MENU , handler ) <EOL> else : <EOL> parent . Unbind ( wx . EVT_CONTEXT_MENU , handler = handler ) <EOL> def AddMenu ( self , menu ) : <EOL> """<STR_LIT>""" <EOL> self . InsertMenu ( None , menu ) <EOL> def InsertMenu ( self , before , menu ) : <EOL> """<STR_LIT>""" <EOL> all_items = self . _all_items <EOL> if menu not in all_items : <EOL> if before in all_items : <EOL> index = all_items . index ( before ) <EOL> else : <EOL> index = len ( all_items ) <EOL> all_items . insert ( index , menu ) <EOL> if menu . IsVisible ( ) : <EOL> max_index = len ( self . _actions_map ) + len ( self . _menus_map ) <EOL> index = min ( index , max_index ) <EOL> menu_item = self . _InsertMenuItem ( index , menu ) <EOL> self . _menus_map [ menu ] = menu_item <EOL> menu . Bind ( EVT_MENU_CHANGED , self . OnMenuChanged ) <EOL> else : <EOL> self . RemoveMenu ( menu ) <EOL> self . InsertMenu ( before , menu ) <EOL> def RemoveMenu ( self , menu ) : <EOL> """<STR_LIT>""" <EOL> all_items = self . _all_items <EOL> if menu in all_items : <EOL> all_items . remove ( menu ) <EOL> menu . Unbind ( EVT_MENU_CHANGED , handler = self . OnMenuChanged ) <EOL> menu_item = self . _menus_map . pop ( menu , None ) <EOL> if menu_item is not None : <EOL> self . RemoveItem ( menu_item ) <EOL> menu_item . SetSubMenu ( None ) <EOL> def AddAction ( self , action ) : <EOL> """<STR_LIT>""" <EOL> self . InsertAction ( None , action ) <EOL> def AddActions ( self , actions ) : <EOL> """<STR_LIT>""" <EOL> insert = self . InsertAction <EOL> for action in actions : <EOL> insert ( None , action ) <EOL> def InsertAction ( self , before , action ) : <EOL> """<STR_LIT>""" <EOL> all_items = self . _all_items <EOL> if action not in all_items : <EOL> if before in all_items : <EOL> index = all_items . index ( before ) <EOL> else : <EOL> index = len ( all_items ) <EOL> all_items . insert ( index , action ) <EOL> if action . IsVisible ( ) : <EOL> max_index = len ( self . _actions_map ) + len ( self . _menus_map ) <EOL> index = min ( index , max_index ) <EOL> menu_item = self . _InsertActionItem ( index , action ) <EOL> self . _actions_map [ action ] = menu_item <EOL> action . Bind ( EVT_ACTION_CHANGED , self . OnActionChanged ) <EOL> else : <EOL> self . RemoveAction ( action ) <EOL> self . InsertAction ( before , action ) <EOL> def InsertActions ( self , before , actions ) : <EOL> """<STR_LIT>""" <EOL> insert = self . InsertAction <EOL> for action in actions : <EOL> insert ( before , action ) <EOL> def RemoveAction ( self , action ) : <EOL> """<STR_LIT>""" <EOL> all_items = self . _all_items <EOL> if action in all_items : <EOL> all_items . remove ( action ) <EOL> action . Unbind ( EVT_ACTION_CHANGED , handler = self . OnActionChanged ) <EOL> menu_item = self . _actions_map . pop ( action , None ) <EOL> if menu_item is not None : <EOL> self . RemoveItem ( menu_item ) <EOL> def RemoveActions ( self , actions ) : <EOL> """<STR_LIT>""" <EOL> remove = self . RemoveAction <EOL> for action in actions : <EOL> remove ( action ) <EOL> class WxMenu ( WxToolkitObject , ProxyMenu ) : <EOL> """<STR_LIT>""" <EOL> widget = Typed ( wxMenu ) <EOL> def create_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> self . widget = wxMenu ( self . parent_widget ( ) ) <EOL> self . widget . BeginBatch ( ) <EOL> def init_widget ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( WxMenu , self ) . init_widget ( ) <EOL> d = self . declaration <EOL> self . set_title ( d . title ) <EOL> self . set_enabled ( d . enabled ) <EOL> self . set_visible ( d . visible ) <EOL> self . set_context_menu ( d . context_menu ) <EOL> self . widget . EndBatch ( emit = False ) <EOL> def init_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( WxMenu , self ) . init_layout ( ) <EOL> widget = self . widget <EOL> for child in self . children ( ) : <EOL> if isinstance ( child , WxMenu ) : <EOL> widget . AddMenu ( child . widget ) <EOL> elif isinstance ( child , WxAction ) : <EOL> widget . AddAction ( child . widget ) <EOL> elif isinstance ( child , WxActionGroup ) : <EOL> widget . AddActions ( child . actions ( ) ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> del self . widget <EOL> del self . declaration <EOL> def find_next_action ( self , child ) : <EOL> """<STR_LIT>""" <EOL> found = False <EOL> for dchild in self . children ( ) : <EOL> if found : <EOL> if isinstance ( dchild , ( WxMenu , WxAction ) ) : <EOL> return dchild . widget <EOL> if isinstance ( dchild , WxActionGroup ) : <EOL> acts = dchild . actions ( ) <EOL> if len ( acts ) > <NUM_LIT:0> : <EOL> return acts [ <NUM_LIT:0> ] <EOL> else : <EOL> found = dchild is child <EOL> def child_added ( self , child ) : <EOL> """<STR_LIT>""" <EOL> super ( WxMenu , self ) . child_added ( child ) <EOL> if isinstance ( child , WxMenu ) : <EOL> before = self . find_next_action ( child ) <EOL> self . widget . InsertMenu ( before , child . widget ) <EOL> elif isinstance ( child , WxAction ) : <EOL> before = self . find_next_action ( child ) <EOL> self . widget . InsertAction ( before , child . widget ) <EOL> elif isinstance ( child , WxActionGroup ) : <EOL> before = self . find_next_action ( child ) <EOL> self . widget . InsertActions ( before , child . actions ( ) ) <EOL> def child_removed ( self , child ) : <EOL> """<STR_LIT>""" <EOL> super ( WxMenu , self ) . child_removed ( child ) <EOL> if isinstance ( child , WxMenu ) : <EOL> self . widget . RemoveMenu ( child . widget ) <EOL> elif isinstance ( child , WxAction ) : <EOL> self . widget . RemoveAction ( child . widget ) <EOL> elif isinstance ( child , WxActionGroup ) : <EOL> self . widget . RemoveActions ( child . actions ( ) ) <EOL> def set_title ( self , title ) : <EOL> """<STR_LIT>""" <EOL> self . widget . SetTitle ( title ) <EOL> def set_visible ( self , visible ) : <EOL> """<STR_LIT>""" <EOL> self . widget . SetVisible ( visible ) <EOL> def set_enabled ( self , enabled ) : <EOL> """<STR_LIT>""" <EOL> self . widget . SetEnabled ( enabled ) <EOL> def set_context_menu ( self , context ) : <EOL> """<STR_LIT>""" <EOL> self . widget . SetContextMenu ( context ) <EOL> def popup ( self ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
