<s> '''<STR_LIT>''' <EOL> import inspect <EOL> import time <EOL> from pyglet . event import ( EventDispatcher , EVENT_UNHANDLED , EVENT_HANDLED , <EOL> EventException ) <EOL> from pyglet . window import key <EOL> from layout . css import Rule , RuleSet , Selector , SimpleSelector <EOL> class GUIEventDispatcher ( EventDispatcher ) : <EOL> default_event_handlers = { } <EOL> def __init__ ( self ) : <EOL> EventDispatcher . __init__ ( self ) <EOL> assert isinstance ( self . _event_stack , tuple ) <EOL> self . _event_stack = [ self . default_event_handlers ] <EOL> self . entered_elements = [ ] <EOL> @ classmethod <EOL> def set_default_handler ( cls , name , selector , handler ) : <EOL> '''<STR_LIT>''' <EOL> if name not in cls . default_event_handlers : <EOL> cls . default_event_handlers [ name ] = RuleSet ( ) <EOL> ruleset = cls . default_event_handlers [ name ] <EOL> ruleset . add_rule ( Rule ( selector , handler ) ) <EOL> def select ( self , rule , event_name = None ) : <EOL> if not isinstance ( rule , str ) : <EOL> rule = '<STR_LIT:#>' + rule . id <EOL> def decorate ( func ) : <EOL> func . selectors = [ Selector . from_string ( r . strip ( ) ) <EOL> for r in rule . split ( '<STR_LIT:U+002C>' ) ] <EOL> if event_name is not None : <EOL> func . event_name = event_name <EOL> self . push_handlers ( func ) <EOL> return func <EOL> return decorate <EOL> def set_handlers ( self , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if type ( self . _event_stack ) is tuple : <EOL> self . _event_stack = [ { } ] <EOL> for object in args : <EOL> if inspect . isroutine ( object ) : <EOL> name = getattr ( object , '<STR_LIT>' , object . __name__ ) <EOL> if name not in self . event_types : <EOL> raise EventException ( '<STR_LIT>' % name ) <EOL> self . set_handler ( name , object ) <EOL> else : <EOL> for name , handler in inspect . getmembers ( object ) : <EOL> name = getattr ( handler , '<STR_LIT>' , name ) <EOL> if name in self . event_types : <EOL> self . set_handler ( name , handler ) <EOL> for name , handler in kwargs . items ( ) : <EOL> if name not in self . event_types : <EOL> raise EventException ( '<STR_LIT>' % name ) <EOL> self . set_handler ( name , handler ) <EOL> def set_handler ( self , name , handler ) : <EOL> '''<STR_LIT>''' <EOL> if name not in self . _event_stack [ <NUM_LIT:0> ] : <EOL> self . _event_stack [ <NUM_LIT:0> ] [ name ] = RuleSet ( ) <EOL> ruleset = self . _event_stack [ <NUM_LIT:0> ] [ name ] <EOL> for selector in handler . selectors : <EOL> ruleset . add_rule ( Rule ( selector , handler ) ) <EOL> def dispatch_event ( self , element , event_type , * args , ** kw ) : <EOL> '''<STR_LIT>''' <EOL> propogate = kw . get ( '<STR_LIT>' , True ) <EOL> for frame in self . _event_stack : <EOL> ruleset = frame . get ( event_type , None ) <EOL> if not ruleset : continue <EOL> rules = ruleset . get_matching_rules ( element ) <EOL> for rule in rules : <EOL> handler = rule . declaration_set <EOL> try : <EOL> ret = handler ( element , * args ) <EOL> except TypeError , message : <EOL> print '<STR_LIT>' % ( handler , <EOL> element , args ) <EOL> raise <EOL> if ret != EVENT_UNHANDLED : <EOL> return element , EVENT_HANDLED <EOL> if propogate and element . parent is not None : <EOL> return self . dispatch_event ( element . parent , event_type , * args , ** kw ) <EOL> return ( None , EVENT_UNHANDLED ) <EOL> mouse_press_element = None <EOL> drag_element = None <EOL> mouse_drag_element = None <EOL> drag_over_element = None <EOL> cumulative_drag = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> focused_element = None <EOL> def setModal ( self , element ) : <EOL> '''<STR_LIT>''' <EOL> if element is None : <EOL> for child in self . children : <EOL> child . is_enabled = True <EOL> child . is_modal = False <EOL> else : <EOL> found = False <EOL> for child in self . children : <EOL> if child is not element : <EOL> child . is_enabled = False <EOL> else : <EOL> found = True <EOL> child . is_modal = True <EOL> assert found , '<STR_LIT>' % ( element , ) <EOL> def setFocus ( self , element , source ) : <EOL> '''<STR_LIT>''' <EOL> if element is not None and self . focused_element is not element : <EOL> element = self . dispatch_event ( element , '<STR_LIT>' , source ) [ <NUM_LIT:0> ] <EOL> if element is None : <EOL> return <EOL> if ( self . focused_element is not None and <EOL> self . focused_element is not element ) : <EOL> self . dispatch_event ( self . focused_element , '<STR_LIT>' ) <EOL> self . focused_element = element <EOL> def focusNextElement ( self , direction = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> if not self . _focus_order : return <EOL> N = len ( self . _focus_order ) <EOL> if self . focused_element is None : <EOL> if direction == <NUM_LIT:1> : i = <NUM_LIT:0> <EOL> else : i = N - <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> i = self . _focus_order . index ( self . focused_element . id ) + direction <EOL> except ValueError : <EOL> i = <NUM_LIT:0> <EOL> if i < <NUM_LIT:0> : i = N - <NUM_LIT:1> <EOL> if i >= N : i = <NUM_LIT:0> <EOL> j = i <EOL> while True : <EOL> element = self . _by_id [ self . _focus_order [ i ] ] <EOL> if element . isEnabled ( ) and element . isVisible ( ) : <EOL> self . setFocus ( element , source = '<STR_LIT>' ) <EOL> return <EOL> i += direction <EOL> if i < <NUM_LIT:0> : i = N - <NUM_LIT:1> <EOL> if i >= N : i = <NUM_LIT:0> <EOL> if i == j : return <EOL> def on_resize ( self , w , h ) : <EOL> self . width = self . inner_width = w <EOL> self . height = self . inner_height = h <EOL> self . layout ( ) <EOL> return EVENT_UNHANDLED <EOL> def generateEnterLeave ( self , x , y , element ) : <EOL> enter = [ ] <EOL> over = [ ] <EOL> e = element <EOL> while e : <EOL> if not e . isEnabled ( ) : <EOL> e = e . parent <EOL> continue <EOL> if e in self . entered_elements : <EOL> over . append ( e ) <EOL> else : <EOL> enter . append ( e ) <EOL> e = e . parent <EOL> all = enter + over <EOL> for e in self . entered_elements : <EOL> if e not in all and self . has ( '<STR_LIT:#>' + e . id ) : <EOL> self . dispatch_event ( e , '<STR_LIT>' , x , y , <EOL> propogate = False ) <EOL> for e in enter : <EOL> if self . dispatch_event ( e , '<STR_LIT>' , x , y , <EOL> propogate = False ) [ <NUM_LIT:1> ] : <EOL> over . append ( e ) <EOL> self . entered_elements = over <EOL> def on_mouse_motion ( self , x , y , dx , dy ) : <EOL> '''<STR_LIT>''' <EOL> element = self . determineHit ( x , y ) <EOL> if self . debug_display is not None : <EOL> self . debug_display . text = '<STR_LIT>' % ( element , <EOL> element and element . classes ) <EOL> self . generateEnterLeave ( x , y , element ) <EOL> if element is not None and element . isEnabled ( ) : <EOL> return self . dispatch_event ( element , '<STR_LIT>' , x , y , <EOL> dx , dy ) [ <NUM_LIT:1> ] <EOL> return EVENT_UNHANDLED <EOL> def on_mouse_enter ( self , x , y ) : <EOL> '''<STR_LIT>''' <EOL> return self . on_mouse_motion ( x , y , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def on_mouse_leave ( self , x , y ) : <EOL> '''<STR_LIT>''' <EOL> for e in self . entered_elements : <EOL> self . dispatch_event ( e , '<STR_LIT>' , x , y , propogate = False ) <EOL> self . entered_elements = [ ] <EOL> return EVENT_HANDLED <EOL> def on_mouse_press ( self , x , y , button , modifiers ) : <EOL> '''<STR_LIT>''' <EOL> element = self . determineHit ( x , y ) <EOL> self . mouse_press_element = element <EOL> self . mouse_drag_element = None <EOL> self . drag_element = None <EOL> self . cumulative_drag = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if element is None : return EVENT_UNHANDLED <EOL> if not element . isEnabled ( ) : return EVENT_UNHANDLED <EOL> self . setFocus ( element , source = '<STR_LIT>' ) <EOL> return self . dispatch_event ( element , '<STR_LIT>' , x , y , button , <EOL> modifiers ) [ <NUM_LIT:1> ] <EOL> def on_mouse_drag ( self , x , y , dx , dy , buttons , modifiers ) : <EOL> '''<STR_LIT>''' <EOL> element = self . determineHit ( x , y ) <EOL> self . generateEnterLeave ( x , y , element ) <EOL> if self . drag_element is not None : <EOL> element , handled = self . dispatch_event ( self . drag_element , <EOL> '<STR_LIT>' , x , y , dx , dy , buttons , modifiers ) <EOL> self . drag_element = element <EOL> over = self . determineHit ( x , y , exclude = element ) <EOL> if ( over is not self . drag_over_element and <EOL> self . drag_over_element is not None ) : <EOL> self . dispatch_event ( self . drag_over_element , '<STR_LIT>' , <EOL> x , y , element ) <EOL> if over is not None and over . isEnabled ( ) : <EOL> self . dispatch_event ( over , '<STR_LIT>' , x , y , element ) <EOL> self . drag_over_element = over <EOL> return handled <EOL> elif self . mouse_drag_element is not None : <EOL> element , handled = self . dispatch_event ( self . mouse_drag_element , <EOL> '<STR_LIT>' , x , y , dx , dy , buttons , modifiers ) <EOL> self . mouse_drag_element = element <EOL> return handled <EOL> if self . mouse_press_element is None : <EOL> return EVENT_UNHANDLED <EOL> if self . mouse_press_element . isEnabled ( ) : <EOL> element , handled = self . dispatch_event ( self . mouse_press_element , <EOL> '<STR_LIT>' , x , y , dx , dy , buttons , modifiers ) <EOL> if handled == EVENT_HANDLED : <EOL> self . mouse_drag_element = element <EOL> return EVENT_HANDLED <EOL> cdx , cdy = self . cumulative_drag <EOL> cdx += abs ( dx ) ; cdy += abs ( dy ) <EOL> self . cumulative_drag = ( cdx , cdy ) <EOL> if cdx + cdy < <NUM_LIT:4> : <EOL> return EVENT_UNHANDLED <EOL> element = self . mouse_press_element <EOL> if not element . isEnabled ( ) : <EOL> while not element . isEnabled ( ) : <EOL> element = element . parent <EOL> if element is None : <EOL> return EVENT_UNHANDLED <EOL> element , handled = self . dispatch_event ( element , '<STR_LIT>' , <EOL> x , y , dx , dy , buttons , modifiers ) <EOL> if handled == EVENT_UNHANDLED : <EOL> return EVENT_UNHANDLED <EOL> self . drag_element = element <EOL> element = self . determineHit ( x , y , exclude = element ) <EOL> if element is not self . drag_over_element : <EOL> if self . drag_over_element is not None : <EOL> self . dispatch_event ( self . drag_over_element , <EOL> '<STR_LIT>' , x , y , self . drag_element ) <EOL> if element is not None and element . isEnabled ( ) : <EOL> self . dispatch_event ( element , '<STR_LIT>' , x , y , <EOL> self . drag_element ) <EOL> self . drag_over_element = element <EOL> return EVENT_HANDLED <EOL> _last_click = <NUM_LIT:0> <EOL> def on_mouse_release ( self , x , y , button , modifiers ) : <EOL> if self . mouse_press_element is not None : <EOL> if self . mouse_press_element . isEnabled ( ) : <EOL> self . dispatch_event ( self . mouse_press_element , <EOL> '<STR_LIT>' , x , y , button , modifiers ) <EOL> if self . drag_element is not None : <EOL> drop = self . determineHit ( x , y , exclude = self . drag_element ) <EOL> if drop is not None and drop . isEnabled ( ) : <EOL> ok = self . dispatch_event ( drop , '<STR_LIT>' , x , y , button , <EOL> modifiers , self . drag_element ) [ <NUM_LIT:1> ] == EVENT_HANDLED <EOL> else : <EOL> ok = False <EOL> handled = self . dispatch_event ( self . drag_element , <EOL> '<STR_LIT>' , x , y , button , modifiers , ok ) [ <NUM_LIT:1> ] <EOL> self . drag_element = self . mouse_press_element = self . drag_over_element = None <EOL> return handled <EOL> element = self . determineHit ( x , y ) <EOL> if element is None : return EVENT_UNHANDLED <EOL> if not element . isEnabled ( ) : <EOL> return EVENT_UNHANDLED <EOL> now = time . time ( ) <EOL> if now - self . _last_click < <NUM_LIT> : <EOL> self . _click_count += <NUM_LIT:1> <EOL> else : <EOL> self . _click_count = <NUM_LIT:1> <EOL> self . _last_click = now <EOL> if element is self . mouse_press_element : <EOL> return self . dispatch_event ( element , '<STR_LIT>' , x , y , button , <EOL> modifiers , self . _click_count ) [ <NUM_LIT:1> ] <EOL> return EVENT_UNHANDLED <EOL> def on_mouse_scroll ( self , x , y , dx , dy ) : <EOL> element = self . determineHit ( x , y ) <EOL> if element is None : return EVENT_UNHANDLED <EOL> return self . dispatch_event ( element , '<STR_LIT>' , x , y , <EOL> dx , dy ) [ <NUM_LIT:1> ] <EOL> def on_key_press ( self , symbol , modifiers ) : <EOL> handled = EVENT_UNHANDLED <EOL> if self . focused_element is not None : <EOL> handled = self . dispatch_event ( self . focused_element , <EOL> '<STR_LIT>' , symbol , modifiers ) [ <NUM_LIT:1> ] <EOL> if handled == EVENT_UNHANDLED and symbol == key . TAB : <EOL> if modifiers & key . MOD_SHIFT : <EOL> self . focusNextElement ( - <NUM_LIT:1> ) <EOL> else : <EOL> self . focusNextElement ( ) <EOL> return handled <EOL> def on_text ( self , text ) : <EOL> if self . focused_element is None : return <EOL> return self . dispatch_event ( self . focused_element , '<STR_LIT>' , text ) [ <NUM_LIT:1> ] <EOL> def on_text_motion ( self , motion ) : <EOL> if self . focused_element is None : return <EOL> return self . dispatch_event ( self . focused_element , '<STR_LIT>' , <EOL> motion ) [ <NUM_LIT:1> ] <EOL> def on_text_motion_select ( self , motion ) : <EOL> if self . focused_element is None : return <EOL> return self . dispatch_event ( self . focused_element , <EOL> '<STR_LIT>' , motion ) [ <NUM_LIT:1> ] <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> GUIEventDispatcher . register_event_type ( '<STR_LIT>' ) <EOL> def select ( rule , event_name = None ) : <EOL> if not isinstance ( rule , str ) : <EOL> rule = '<STR_LIT:#>' + rule . id <EOL> def decorate ( func ) : <EOL> func . selectors = [ Selector . from_string ( r . strip ( ) ) <EOL> for r in rule . split ( '<STR_LIT:U+002C>' ) ] <EOL> if event_name is not None : <EOL> func . event_name = event_name <EOL> return func <EOL> return decorate <EOL> def default ( rule , event_name = None ) : <EOL> def decorate ( func ) : <EOL> name = event_name or func . __name__ <EOL> if name not in GUIEventDispatcher . event_types : <EOL> raise EventException ( '<STR_LIT>' % name ) <EOL> for r in rule . split ( '<STR_LIT:U+002C>' ) : <EOL> selector = Selector . from_string ( r . strip ( ) ) <EOL> GUIEventDispatcher . set_default_handler ( name , selector , func ) <EOL> return func <EOL> return decorate <EOL> universal_selector = Selector ( SimpleSelector ( None , None , ( ) , ( ) , ( ) ) , ( ) ) </s>
