<s> """<STR_LIT>""" <EOL> import wx <EOL> from helper import restore_window , save_window , TraitsUIScrolledPanel <EOL> from ui_base import BaseDialog <EOL> from ui_panel import panel , show_help <EOL> from constants import DefaultTitle , WindowColor , screen_dy , scrollbar_dx <EOL> from traitsui . menu import ApplyButton , RevertButton , OKButton , CancelButton , HelpButton <EOL> def ui_modal ( ui , parent ) : <EOL> """<STR_LIT>""" <EOL> ui_dialog ( ui , parent , True ) <EOL> def ui_nonmodal ( ui , parent ) : <EOL> """<STR_LIT>""" <EOL> ui_dialog ( ui , parent , False ) <EOL> def ui_dialog ( ui , parent , is_modal ) : <EOL> """<STR_LIT>""" <EOL> if ui . owner is None : <EOL> ui . owner = ModalDialog ( ) <EOL> ui . owner . init ( ui , parent , is_modal ) <EOL> ui . control = ui . owner . control <EOL> ui . control . _parent = parent <EOL> try : <EOL> ui . prepare_ui ( ) <EOL> except : <EOL> ui . control . Destroy ( ) <EOL> ui . control . ui = None <EOL> ui . control = None <EOL> ui . owner = None <EOL> ui . result = False <EOL> raise <EOL> ui . handler . position ( ui . info ) <EOL> restore_window ( ui ) <EOL> if is_modal : <EOL> ui . control . ShowModal ( ) <EOL> else : <EOL> ui . control . Show ( ) <EOL> class ModalDialog ( BaseDialog ) : <EOL> """<STR_LIT>""" <EOL> def init ( self , ui , parent , is_modal ) : <EOL> self . is_modal = is_modal <EOL> style = <NUM_LIT:0> <EOL> view = ui . view <EOL> if view . resizable : <EOL> style |= wx . RESIZE_BORDER <EOL> title = view . title <EOL> if title == '<STR_LIT>' : <EOL> title = DefaultTitle <EOL> revert = apply = False <EOL> window = ui . control <EOL> if window is not None : <EOL> window . SetSizer ( None ) <EOL> ui . reset ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> revert = self . revert . IsEnabled ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> apply = self . apply . IsEnabled ( ) <EOL> else : <EOL> self . ui = ui <EOL> if is_modal : <EOL> window = wx . Dialog ( parent , - <NUM_LIT:1> , title , <EOL> style = style | wx . DEFAULT_DIALOG_STYLE ) <EOL> else : <EOL> window = wx . Frame ( parent , - <NUM_LIT:1> , title , style = style | <EOL> ( wx . DEFAULT_FRAME_STYLE & ( ~ wx . RESIZE_BORDER ) ) ) <EOL> window . SetBackgroundColour ( WindowColor ) <EOL> self . control = window <EOL> self . set_icon ( view . icon ) <EOL> wx . EVT_CLOSE ( window , self . _on_close_page ) <EOL> wx . EVT_CHAR ( window , self . _on_key ) <EOL> context = ui . context <EOL> ui . _context = context <EOL> ui . context = self . _copy_context ( context ) <EOL> ui . _revert = self . _copy_context ( context ) <EOL> sw_sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> if ui . scrollable : <EOL> sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> sw = TraitsUIScrolledPanel ( window ) <EOL> trait_sheet = panel ( ui , sw ) <EOL> sizer . Add ( trait_sheet , <NUM_LIT:1> , wx . EXPAND | wx . ALL , <NUM_LIT:4> ) <EOL> tsdx , tsdy = trait_sheet . GetSizeTuple ( ) <EOL> tsdx += <NUM_LIT:8> <EOL> tsdy += <NUM_LIT:8> <EOL> sw . SetScrollRate ( <NUM_LIT:16> , <NUM_LIT:16> ) <EOL> max_dy = ( <NUM_LIT:2> * screen_dy ) / <NUM_LIT:3> <EOL> sw . SetSizer ( sizer ) <EOL> sw . SetSize ( wx . Size ( tsdx + ( ( tsdy > max_dy ) * scrollbar_dx ) , <EOL> min ( tsdy , max_dy ) ) ) <EOL> else : <EOL> sw = panel ( ui , window ) <EOL> sw_sizer . Add ( sw , <NUM_LIT:1> , wx . EXPAND ) <EOL> buttons = [ self . coerce_button ( button ) for button in view . buttons ] <EOL> nbuttons = len ( buttons ) <EOL> if ( nbuttons != <NUM_LIT:1> ) or ( not self . is_button ( buttons [ <NUM_LIT:0> ] , '<STR_LIT>' ) ) : <EOL> sw_sizer . Add ( wx . StaticLine ( window , - <NUM_LIT:1> ) , <NUM_LIT:0> , wx . EXPAND ) <EOL> b_sizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> if nbuttons == <NUM_LIT:0> : <EOL> if view . apply : <EOL> self . check_button ( buttons , ApplyButton ) <EOL> if view . revert : <EOL> self . check_button ( buttons , RevertButton ) <EOL> if view . ok : <EOL> self . check_button ( buttons , OKButton ) <EOL> if view . cancel : <EOL> self . check_button ( buttons , CancelButton ) <EOL> if view . help : <EOL> self . check_button ( buttons , HelpButton ) <EOL> for raw_button , button in zip ( view . buttons , buttons ) : <EOL> default = raw_button == view . default_button <EOL> if self . is_button ( button , '<STR_LIT>' ) : <EOL> self . apply = self . add_button ( button , b_sizer , <EOL> self . _on_apply , apply , default = default ) <EOL> ui . on_trait_change ( self . _on_applyable , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) <EOL> elif self . is_button ( button , '<STR_LIT>' ) : <EOL> self . revert = self . add_button ( button , b_sizer , <EOL> self . _on_revert , revert , default = default ) <EOL> elif self . is_button ( button , '<STR_LIT:OK>' ) : <EOL> self . ok = self . add_button ( button , b_sizer , self . _on_ok , <EOL> default = default ) <EOL> ui . on_trait_change ( self . _on_error , '<STR_LIT>' , <EOL> dispatch = '<STR_LIT>' ) <EOL> elif self . is_button ( button , '<STR_LIT>' ) : <EOL> self . add_button ( button , b_sizer , self . _on_cancel , <EOL> default = default ) <EOL> elif self . is_button ( button , '<STR_LIT>' ) : <EOL> self . add_button ( button , b_sizer , self . _on_help , <EOL> default = default ) <EOL> elif not self . is_button ( button , '<STR_LIT>' ) : <EOL> self . add_button ( button , b_sizer , default = default ) <EOL> sw_sizer . Add ( b_sizer , <NUM_LIT:0> , wx . ALIGN_RIGHT | wx . ALL , <NUM_LIT:5> ) <EOL> self . add_menubar ( ) <EOL> self . add_toolbar ( ) <EOL> self . add_statusbar ( ) <EOL> window . SetSizerAndFit ( sw_sizer ) <EOL> def close ( self , rc = wx . ID_OK ) : <EOL> """<STR_LIT>""" <EOL> ui = self . ui <EOL> ui . result = ( rc == wx . ID_OK ) <EOL> save_window ( ui ) <EOL> if self . is_modal : <EOL> self . control . EndModal ( rc ) <EOL> ui . finish ( ) <EOL> self . ui = self . apply = self . revert = self . help = self . control = None <EOL> def _copy_context ( self , context ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for name , value in context . items ( ) : <EOL> if value is not None : <EOL> result [ name ] = value . clone_traits ( ) <EOL> else : <EOL> result [ name ] = None <EOL> return result <EOL> def _apply_context ( self , from_context , to_context ) : <EOL> """<STR_LIT>""" <EOL> for name , value in from_context . items ( ) : <EOL> if value is not None : <EOL> to_context [ name ] . copy_traits ( value ) <EOL> else : <EOL> to_context [ name ] = None <EOL> if to_context is self . ui . _context : <EOL> on_apply = self . ui . view . on_apply <EOL> if on_apply is not None : <EOL> on_apply ( ) <EOL> def _on_close_page ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . ui . view . close_result == True : <EOL> self . _on_ok ( event ) <EOL> else : <EOL> self . _on_cancel ( event ) <EOL> def _on_ok ( self , event = None ) : <EOL> """<STR_LIT>""" <EOL> if self . ui . handler . close ( self . ui . info , True ) : <EOL> self . _apply_context ( self . ui . context , self . ui . _context ) <EOL> self . close ( wx . ID_OK ) <EOL> def _on_cancel ( self , event = None ) : <EOL> """<STR_LIT>""" <EOL> if self . ui . handler . close ( self . ui . info , False ) : <EOL> self . _apply_context ( self . ui . _revert , self . ui . _context ) <EOL> self . close ( wx . ID_CANCEL ) <EOL> def _on_help ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . ui . handler . show_help ( self . ui . info , event . GetEventObject ( ) ) <EOL> def _on_key ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if event . GetKeyCode ( ) == <NUM_LIT> : <EOL> self . _on_close_page ( event ) <EOL> def _on_apply ( self , event ) : <EOL> """<STR_LIT>""" <EOL> ui = self . ui <EOL> self . _apply_context ( ui . context , ui . _context ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . revert . Enable ( True ) <EOL> ui . handler . apply ( ui . info ) <EOL> ui . modified = False <EOL> def _on_revert ( self , event ) : <EOL> """<STR_LIT>""" <EOL> ui = self . ui <EOL> self . _apply_context ( ui . _revert , ui . context ) <EOL> self . _apply_context ( ui . _revert , ui . _context ) <EOL> self . revert . Enable ( False ) <EOL> ui . handler . revert ( ui . info ) <EOL> ui . modified = False <EOL> def _on_applyable ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . apply . Enable ( state ) <EOL> def _on_error ( self , errors ) : <EOL> """<STR_LIT>""" <EOL> self . ok . Enable ( errors == <NUM_LIT:0> ) </s>
