<s> import pyglet . text <EOL> from pyglet_gui . constants import VALIGN_BOTTOM , HALIGN_LEFT , HALIGN_CENTER , ANCHOR_CENTER , GetRelativePoint <EOL> from pyglet_gui . core import Rectangle , Viewer <EOL> from pyglet_gui . controllers import Controller <EOL> from pyglet_gui . containers import HorizontalContainer , VerticalContainer , Wrapper <EOL> from pyglet_gui . buttons import Button , FocusButton <EOL> from pyglet_gui . manager import Manager <EOL> class Graphic ( Viewer ) : <EOL> def __init__ ( self , path , is_expandable = False ) : <EOL> Viewer . __init__ ( self ) <EOL> self . _path = path <EOL> self . _expandable = is_expandable <EOL> self . _graphic = None <EOL> self . _min_width = self . _min_height = <NUM_LIT:0> <EOL> def get_path ( self ) : <EOL> return self . _path <EOL> def load_graphics ( self ) : <EOL> theme = self . theme [ self . get_path ( ) ] <EOL> self . _graphic = theme [ '<STR_LIT:image>' ] . generate ( theme [ self . _path ] [ '<STR_LIT>' ] , ** self . get_batch ( '<STR_LIT>' ) ) <EOL> self . _min_width = self . _graphic . width <EOL> self . _min_height = self . _graphic . height <EOL> def unload_graphics ( self ) : <EOL> self . _graphic . unload ( ) <EOL> def expand ( self , width , height ) : <EOL> assert self . _expandable <EOL> self . width , self . height = width , height <EOL> self . _graphic . update ( self . x , self . y , self . width , self . height ) <EOL> def is_expandable ( self ) : <EOL> return self . _expandable <EOL> def layout ( self ) : <EOL> self . _graphic . update ( self . x , self . y , self . width , self . height ) <EOL> def compute_size ( self ) : <EOL> return self . _min_width , self . _min_height <EOL> class Label ( Viewer ) : <EOL> def __init__ ( self , text = "<STR_LIT>" , bold = False , italic = False , <EOL> font_name = None , font_size = None , color = None , path = None ) : <EOL> Viewer . __init__ ( self ) <EOL> self . text = text <EOL> self . bold = bold <EOL> self . italic = italic <EOL> self . font_name = font_name <EOL> self . font_size = font_size <EOL> self . color = color <EOL> self . path = path <EOL> self . label = None <EOL> def get_path ( self ) : <EOL> return self . path <EOL> def load_graphics ( self ) : <EOL> theme = self . theme [ self . get_path ( ) ] <EOL> self . label = pyglet . text . Label ( self . text , <EOL> bold = self . bold , <EOL> italic = self . italic , <EOL> color = self . color or theme [ '<STR_LIT>' ] , <EOL> font_name = self . font_name or theme [ '<STR_LIT>' ] , <EOL> font_size = self . font_size or theme [ '<STR_LIT>' ] , <EOL> ** self . get_batch ( '<STR_LIT>' ) ) <EOL> def unload_graphics ( self ) : <EOL> self . label . delete ( ) <EOL> def layout ( self ) : <EOL> font = self . label . document . get_font ( ) <EOL> self . label . x = self . x <EOL> self . label . y = self . y - font . descent <EOL> def set_text ( self , text ) : <EOL> self . text = text <EOL> self . reload ( ) <EOL> self . reset_size ( ) <EOL> def compute_size ( self ) : <EOL> font = self . label . document . get_font ( ) <EOL> return self . label . content_width , font . ascent - font . descent <EOL> class Frame ( Wrapper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , content , path = None , image_name = '<STR_LIT:image>' , <EOL> is_expandable = False , anchor = ANCHOR_CENTER ) : <EOL> Wrapper . __init__ ( self , content , is_expandable = is_expandable , anchor = anchor ) <EOL> self . _frame = None <EOL> if path is None : <EOL> self . _path = [ '<STR_LIT>' ] <EOL> else : <EOL> self . _path = path <EOL> self . _image_name = image_name <EOL> def get_path ( self ) : <EOL> return self . _path <EOL> def load_graphics ( self ) : <EOL> Wrapper . load_graphics ( self ) <EOL> theme = self . theme [ self . get_path ( ) ] <EOL> if self . _frame is None : <EOL> template = theme [ self . _image_name ] <EOL> self . _frame = template . generate ( theme [ '<STR_LIT>' ] , ** self . get_batch ( '<STR_LIT>' ) ) <EOL> def unload_graphics ( self ) : <EOL> if self . _frame is not None : <EOL> self . _frame . unload ( ) <EOL> self . _frame = None <EOL> Wrapper . unload_graphics ( self ) <EOL> def expand ( self , width , height ) : <EOL> if self . content . is_expandable ( ) : <EOL> content_width , content_height = self . _frame . get_content_size ( width , height ) <EOL> self . content . expand ( content_width , content_height ) <EOL> self . width , self . height = width , height <EOL> def layout ( self ) : <EOL> self . _frame . update ( self . x , self . y , self . width , self . height ) <EOL> x , y , width , height = self . _frame . get_content_region ( ) <EOL> interior = Rectangle ( x , y , width , height ) <EOL> x , y = GetRelativePoint ( interior , self . anchor , self . content , self . anchor , self . content_offset ) <EOL> self . content . set_position ( x , y ) <EOL> def compute_size ( self ) : <EOL> self . content . compute_size ( ) <EOL> return self . _frame . get_needed_size ( self . content . width , self . content . height ) <EOL> class TitleFrame ( VerticalContainer ) : <EOL> def __init__ ( self , title , content ) : <EOL> VerticalContainer . __init__ ( self , content = [ <EOL> HorizontalContainer ( [ Graphic ( path = [ "<STR_LIT>" , "<STR_LIT:left>" ] , is_expandable = True ) , <EOL> Frame ( Label ( title , path = [ "<STR_LIT>" ] ) , <EOL> path = [ "<STR_LIT>" , "<STR_LIT>" ] ) , <EOL> Graphic ( path = [ "<STR_LIT>" , "<STR_LIT:right>" ] , is_expandable = True ) , <EOL> ] , align = VALIGN_BOTTOM , padding = <NUM_LIT:0> ) , <EOL> Frame ( content , path = [ "<STR_LIT>" , "<STR_LIT>" ] , is_expandable = True ) , <EOL> ] , padding = <NUM_LIT:0> ) <EOL> class SectionHeader ( HorizontalContainer ) : <EOL> def __init__ ( self , title , align = HALIGN_CENTER ) : <EOL> if align == HALIGN_LEFT : <EOL> left_expand = False <EOL> right_expand = True <EOL> elif align == HALIGN_CENTER : <EOL> left_expand = True <EOL> right_expand = True <EOL> else : <EOL> left_expand = True <EOL> right_expand = False <EOL> HorizontalContainer . __init__ ( self , content = [ <EOL> Graphic ( path = [ "<STR_LIT>" , "<STR_LIT:left>" ] , is_expandable = left_expand ) , <EOL> Frame ( Label ( title , path = [ "<STR_LIT>" ] ) , path = [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> Graphic ( path = [ "<STR_LIT>" , "<STR_LIT:right>" ] , is_expandable = right_expand ) , <EOL> ] , align = VALIGN_BOTTOM , padding = <NUM_LIT:0> ) <EOL> class FoldingSection ( VerticalContainer , Controller ) : <EOL> def __init__ ( self , title , content = None , is_open = True , align = HALIGN_CENTER ) : <EOL> Controller . __init__ ( self ) <EOL> if align == HALIGN_LEFT : <EOL> left_expand = False <EOL> right_expand = True <EOL> elif align == HALIGN_CENTER : <EOL> left_expand = True <EOL> right_expand = True <EOL> else : <EOL> left_expand = True <EOL> right_expand = False <EOL> self . is_open = is_open <EOL> self . folding_content = content <EOL> self . book = Graphic ( self . _get_image_path ( ) ) <EOL> self . header = HorizontalContainer ( [ Graphic ( path = [ "<STR_LIT>" , "<STR_LIT:left>" ] , is_expandable = left_expand ) , <EOL> Frame ( HorizontalContainer ( [ <EOL> self . book , <EOL> Label ( title , path = [ "<STR_LIT>" ] ) , <EOL> ] ) , path = [ "<STR_LIT>" , "<STR_LIT>" ] ) , <EOL> Graphic ( path = [ "<STR_LIT>" , "<STR_LIT:right>" ] , is_expandable = right_expand ) , <EOL> ] , align = VALIGN_BOTTOM , padding = <NUM_LIT:0> ) <EOL> layout = [ self . header ] <EOL> if self . is_open : <EOL> layout . append ( self . folding_content ) <EOL> VerticalContainer . __init__ ( self , content = layout , align = align ) <EOL> def set_manager ( self , manager ) : <EOL> Controller . set_manager ( self , manager ) <EOL> self . header . set_manager ( manager ) <EOL> self . header . parent = self <EOL> for item in self . _content : <EOL> if item == self . header : <EOL> continue <EOL> item . set_manager ( self . _manager ) <EOL> item . parent = self <EOL> def _get_image_path ( self ) : <EOL> if self . is_open : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def hit_test ( self , x , y ) : <EOL> return self . header . is_inside ( x , y ) <EOL> def on_mouse_press ( self , x , y , button , modifiers ) : <EOL> self . is_open = not self . is_open <EOL> self . book . _path = self . _get_image_path ( ) <EOL> self . book . reload ( ) <EOL> if self . is_open : <EOL> self . add ( self . folding_content ) <EOL> else : <EOL> self . remove ( self . folding_content ) <EOL> def delete ( self ) : <EOL> if not self . is_open : <EOL> self . folding_content . delete ( ) <EOL> self . folding_content = None <EOL> VerticalContainer . delete ( self ) <EOL> class PopupMessage ( Manager ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text = "<STR_LIT>" , window = None , batch = None , group = None , <EOL> theme = None , on_escape = None , have_focus = False ) : <EOL> def on_ok ( _ ) : <EOL> if on_escape is not None : <EOL> on_escape ( self ) <EOL> self . delete ( ) <EOL> button = FocusButton ( "<STR_LIT>" , on_press = on_ok ) <EOL> Manager . __init__ ( self , content = Frame ( VerticalContainer ( <EOL> [ Label ( text ) , button ] ) ) , <EOL> window = window , batch = batch , group = group , <EOL> theme = theme , is_movable = True ) <EOL> Manager . set_next_focus ( self , <NUM_LIT:1> ) <EOL> class PopupConfirm ( Manager ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text = "<STR_LIT>" , ok = "<STR_LIT>" , cancel = "<STR_LIT>" , <EOL> window = None , batch = None , group = None , theme = None , <EOL> on_ok = None , on_cancel = None ) : <EOL> def on_ok_click ( _ ) : <EOL> if on_ok is not None : <EOL> on_ok ( self ) <EOL> self . delete ( ) <EOL> def on_cancel_click ( _ ) : <EOL> if on_cancel is not None : <EOL> on_cancel ( self ) <EOL> self . delete ( ) <EOL> Manager . __init__ ( self , content = Frame ( <EOL> VerticalContainer ( [ <EOL> Label ( text ) , <EOL> HorizontalContainer ( [ Button ( ok , on_press = on_ok_click ) , <EOL> None , <EOL> Button ( cancel , on_press = on_cancel_click ) ] <EOL> ) ] ) <EOL> ) , window = window , batch = batch , group = group , theme = theme , is_movable = True ) </s>
