<s> import sys <EOL> import xml . sax . saxutils <EOL> from pyglet . gl import * <EOL> from pyglet . window import mouse , key <EOL> from wydget import event , layouts , loadxml <EOL> from wydget . widgets . frame import Frame <EOL> from wydget . widgets . label import Label <EOL> class MenuItem ( Label ) : <EOL> name = '<STR_LIT>' <EOL> @ event . default ( '<STR_LIT>' ) <EOL> def on_element_enter ( item , x , y ) : <EOL> item . _save_bgcolor = item . bgcolor <EOL> item . bgcolor = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> return event . EVENT_HANDLED <EOL> @ event . default ( '<STR_LIT>' ) <EOL> def on_element_leave ( item , x , y ) : <EOL> item . bgcolor = item . _save_bgcolor <EOL> return event . EVENT_HANDLED <EOL> @ event . default ( '<STR_LIT>' ) <EOL> def on_click ( widget , * args ) : <EOL> menu = widget . parent <EOL> menu . hide ( ) <EOL> return event . EVENT_HANDLED <EOL> class PopupMenu ( Frame ) : <EOL> '''<STR_LIT>''' <EOL> name = '<STR_LIT>' <EOL> is_focusable = True <EOL> def __init__ ( self , parent , items , ** kw ) : <EOL> super ( PopupMenu , self ) . __init__ ( parent , border = "<STR_LIT>" , <EOL> is_visible = False , ** kw ) <EOL> for n , ( label , id ) in enumerate ( items ) : <EOL> MenuItem ( self , text = label , id = id , width = '<STR_LIT>' , <EOL> bgcolor = ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) [ n % <NUM_LIT:2> ] ) <EOL> self . layout = layouts . Vertical ( self ) <EOL> def expose ( self , mouse ) : <EOL> w = self . getGUI ( ) . window <EOL> w , h = w . width , w . height <EOL> self . center = map ( int , mouse ) <EOL> if self . x < <NUM_LIT:0> : self . x = <NUM_LIT:0> <EOL> if self . y < <NUM_LIT:0> : self . y = <NUM_LIT:0> <EOL> if self . x + self . width > w : self . x = w - self . width <EOL> if self . y + self . height > h : self . y = h - self . height <EOL> self . setVisible ( True ) <EOL> self . gainFocus ( ) <EOL> def hide ( self ) : <EOL> self . setVisible ( False ) <EOL> @ classmethod <EOL> def fromXML ( cls , element , parent ) : <EOL> '''<STR_LIT>''' <EOL> kw = loadxml . parseAttributes ( element ) <EOL> items = [ ] <EOL> for child in element . getchildren ( ) : <EOL> text = xml . sax . saxutils . unescape ( child . text ) <EOL> items . append ( ( text , child . attrib [ '<STR_LIT:id>' ] ) ) <EOL> return cls ( parent , items , ** kw ) <EOL> @ staticmethod <EOL> def isActivatingClick ( button , modifiers ) : <EOL> '''<STR_LIT>''' <EOL> if sys . platform == '<STR_LIT>' : <EOL> if button & mouse . LEFT and modifiers & key . MOD_CTRL : <EOL> return True <EOL> return button & mouse . RIGHT <EOL> @ event . default ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_menu_gain_focus ( menu , method ) : <EOL> return event . EVENT_HANDLED <EOL> @ event . default ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def on_menu_lose_focus ( menu , method ) : <EOL> menu . hide ( ) <EOL> return event . EVENT_HANDLED </s>
