<s> import os <EOL> from tkinter import * <EOL> import imp <EOL> from . import ZoomHeight <EOL> from . configHandler import idleConf <EOL> ICONDIR = "<STR_LIT>" <EOL> try : <EOL> _icondir = os . path . join ( os . path . dirname ( __file__ ) , ICONDIR ) <EOL> except NameError : <EOL> _icondir = ICONDIR <EOL> if os . path . isdir ( _icondir ) : <EOL> ICONDIR = _icondir <EOL> elif not os . path . isdir ( ICONDIR ) : <EOL> raise RuntimeError ( "<STR_LIT>" % ( ICONDIR , ) ) <EOL> def listicons ( icondir = ICONDIR ) : <EOL> """<STR_LIT>""" <EOL> root = Tk ( ) <EOL> import glob <EOL> list = glob . glob ( os . path . join ( icondir , "<STR_LIT>" ) ) <EOL> list . sort ( ) <EOL> images = [ ] <EOL> row = column = <NUM_LIT:0> <EOL> for file in list : <EOL> name = os . path . splitext ( os . path . basename ( file ) ) [ <NUM_LIT:0> ] <EOL> image = PhotoImage ( file = file , master = root ) <EOL> images . append ( image ) <EOL> label = Label ( root , image = image , bd = <NUM_LIT:1> , relief = "<STR_LIT>" ) <EOL> label . grid ( row = row , column = column ) <EOL> label = Label ( root , text = name ) <EOL> label . grid ( row = row + <NUM_LIT:1> , column = column ) <EOL> column = column + <NUM_LIT:1> <EOL> if column >= <NUM_LIT:10> : <EOL> row = row + <NUM_LIT:2> <EOL> column = <NUM_LIT:0> <EOL> root . images = images <EOL> class TreeNode : <EOL> def __init__ ( self , canvas , parent , item ) : <EOL> self . canvas = canvas <EOL> self . parent = parent <EOL> self . item = item <EOL> self . state = '<STR_LIT>' <EOL> self . selected = False <EOL> self . children = [ ] <EOL> self . x = self . y = None <EOL> self . iconimages = { } <EOL> def destroy ( self ) : <EOL> for c in self . children [ : ] : <EOL> self . children . remove ( c ) <EOL> c . destroy ( ) <EOL> self . parent = None <EOL> def geticonimage ( self , name ) : <EOL> try : <EOL> return self . iconimages [ name ] <EOL> except KeyError : <EOL> pass <EOL> file , ext = os . path . splitext ( name ) <EOL> ext = ext or "<STR_LIT>" <EOL> fullname = os . path . join ( ICONDIR , file + ext ) <EOL> image = PhotoImage ( master = self . canvas , file = fullname ) <EOL> self . iconimages [ name ] = image <EOL> return image <EOL> def select ( self , event = None ) : <EOL> if self . selected : <EOL> return <EOL> self . deselectall ( ) <EOL> self . selected = True <EOL> self . canvas . delete ( self . image_id ) <EOL> self . drawicon ( ) <EOL> self . drawtext ( ) <EOL> def deselect ( self , event = None ) : <EOL> if not self . selected : <EOL> return <EOL> self . selected = False <EOL> self . canvas . delete ( self . image_id ) <EOL> self . drawicon ( ) <EOL> self . drawtext ( ) <EOL> def deselectall ( self ) : <EOL> if self . parent : <EOL> self . parent . deselectall ( ) <EOL> else : <EOL> self . deselecttree ( ) <EOL> def deselecttree ( self ) : <EOL> if self . selected : <EOL> self . deselect ( ) <EOL> for child in self . children : <EOL> child . deselecttree ( ) <EOL> def flip ( self , event = None ) : <EOL> if self . state == '<STR_LIT>' : <EOL> self . collapse ( ) <EOL> else : <EOL> self . expand ( ) <EOL> self . item . OnDoubleClick ( ) <EOL> return "<STR_LIT>" <EOL> def expand ( self , event = None ) : <EOL> if not self . item . _IsExpandable ( ) : <EOL> return <EOL> if self . state != '<STR_LIT>' : <EOL> self . state = '<STR_LIT>' <EOL> self . update ( ) <EOL> self . view ( ) <EOL> def collapse ( self , event = None ) : <EOL> if self . state != '<STR_LIT>' : <EOL> self . state = '<STR_LIT>' <EOL> self . update ( ) <EOL> def view ( self ) : <EOL> top = self . y - <NUM_LIT:2> <EOL> bottom = self . lastvisiblechild ( ) . y + <NUM_LIT> <EOL> height = bottom - top <EOL> visible_top = self . canvas . canvasy ( <NUM_LIT:0> ) <EOL> visible_height = self . canvas . winfo_height ( ) <EOL> visible_bottom = self . canvas . canvasy ( visible_height ) <EOL> if visible_top <= top and bottom <= visible_bottom : <EOL> return <EOL> x0 , y0 , x1 , y1 = self . canvas . _getints ( self . canvas [ '<STR_LIT>' ] ) <EOL> if top >= visible_top and height <= visible_height : <EOL> fraction = top + height - visible_height <EOL> else : <EOL> fraction = top <EOL> fraction = float ( fraction ) / y1 <EOL> self . canvas . yview_moveto ( fraction ) <EOL> def lastvisiblechild ( self ) : <EOL> if self . children and self . state == '<STR_LIT>' : <EOL> return self . children [ - <NUM_LIT:1> ] . lastvisiblechild ( ) <EOL> else : <EOL> return self <EOL> def update ( self ) : <EOL> if self . parent : <EOL> self . parent . update ( ) <EOL> else : <EOL> oldcursor = self . canvas [ '<STR_LIT>' ] <EOL> self . canvas [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> self . canvas . update ( ) <EOL> self . canvas . delete ( ALL ) <EOL> self . draw ( <NUM_LIT:7> , <NUM_LIT:2> ) <EOL> x0 , y0 , x1 , y1 = self . canvas . bbox ( ALL ) <EOL> self . canvas . configure ( scrollregion = ( <NUM_LIT:0> , <NUM_LIT:0> , x1 , y1 ) ) <EOL> self . canvas [ '<STR_LIT>' ] = oldcursor <EOL> def draw ( self , x , y ) : <EOL> self . x , self . y = x , y <EOL> self . drawicon ( ) <EOL> self . drawtext ( ) <EOL> if self . state != '<STR_LIT>' : <EOL> return y + <NUM_LIT> <EOL> if not self . children : <EOL> sublist = self . item . _GetSubList ( ) <EOL> if not sublist : <EOL> return y + <NUM_LIT> <EOL> for item in sublist : <EOL> child = self . __class__ ( self . canvas , self , item ) <EOL> self . children . append ( child ) <EOL> cx = x + <NUM_LIT:20> <EOL> cy = y + <NUM_LIT> <EOL> cylast = <NUM_LIT:0> <EOL> for child in self . children : <EOL> cylast = cy <EOL> self . canvas . create_line ( x + <NUM_LIT:9> , cy + <NUM_LIT:7> , cx , cy + <NUM_LIT:7> , fill = "<STR_LIT>" ) <EOL> cy = child . draw ( cx , cy ) <EOL> if child . item . _IsExpandable ( ) : <EOL> if child . state == '<STR_LIT>' : <EOL> iconname = "<STR_LIT>" <EOL> callback = child . collapse <EOL> else : <EOL> iconname = "<STR_LIT>" <EOL> callback = child . expand <EOL> image = self . geticonimage ( iconname ) <EOL> id = self . canvas . create_image ( x + <NUM_LIT:9> , cylast + <NUM_LIT:7> , image = image ) <EOL> self . canvas . tag_bind ( id , "<STR_LIT>" , callback ) <EOL> self . canvas . tag_bind ( id , "<STR_LIT>" , lambda x : None ) <EOL> id = self . canvas . create_line ( x + <NUM_LIT:9> , y + <NUM_LIT:10> , x + <NUM_LIT:9> , cylast + <NUM_LIT:7> , <EOL> fill = "<STR_LIT>" ) <EOL> self . canvas . tag_lower ( id ) <EOL> return cy <EOL> def drawicon ( self ) : <EOL> if self . selected : <EOL> imagename = ( self . item . GetSelectedIconName ( ) or <EOL> self . item . GetIconName ( ) or <EOL> "<STR_LIT>" ) <EOL> else : <EOL> imagename = self . item . GetIconName ( ) or "<STR_LIT>" <EOL> image = self . geticonimage ( imagename ) <EOL> id = self . canvas . create_image ( self . x , self . y , anchor = "<STR_LIT>" , image = image ) <EOL> self . image_id = id <EOL> self . canvas . tag_bind ( id , "<STR_LIT>" , self . select ) <EOL> self . canvas . tag_bind ( id , "<STR_LIT>" , self . flip ) <EOL> def drawtext ( self ) : <EOL> textx = self . x + <NUM_LIT:20> - <NUM_LIT:1> <EOL> texty = self . y - <NUM_LIT:1> <EOL> labeltext = self . item . GetLabelText ( ) <EOL> if labeltext : <EOL> id = self . canvas . create_text ( textx , texty , anchor = "<STR_LIT>" , <EOL> text = labeltext ) <EOL> self . canvas . tag_bind ( id , "<STR_LIT>" , self . select ) <EOL> self . canvas . tag_bind ( id , "<STR_LIT>" , self . flip ) <EOL> x0 , y0 , x1 , y1 = self . canvas . bbox ( id ) <EOL> textx = max ( x1 , <NUM_LIT:200> ) + <NUM_LIT:10> <EOL> text = self . item . GetText ( ) or "<STR_LIT>" <EOL> try : <EOL> self . entry <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . edit_finish ( ) <EOL> try : <EOL> label = self . label <EOL> except AttributeError : <EOL> self . label = Label ( self . canvas , text = text , bd = <NUM_LIT:0> , padx = <NUM_LIT:2> , pady = <NUM_LIT:2> ) <EOL> theme = idleConf . GetOption ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> if self . selected : <EOL> self . label . configure ( idleConf . GetHighlight ( theme , '<STR_LIT>' ) ) <EOL> else : <EOL> self . label . configure ( idleConf . GetHighlight ( theme , '<STR_LIT>' ) ) <EOL> id = self . canvas . create_window ( textx , texty , <EOL> anchor = "<STR_LIT>" , window = self . label ) <EOL> self . label . bind ( "<STR_LIT>" , self . select_or_edit ) <EOL> self . label . bind ( "<STR_LIT>" , self . flip ) <EOL> self . text_id = id <EOL> def select_or_edit ( self , event = None ) : <EOL> if self . selected and self . item . IsEditable ( ) : <EOL> self . edit ( event ) <EOL> else : <EOL> self . select ( event ) <EOL> def edit ( self , event = None ) : <EOL> self . entry = Entry ( self . label , bd = <NUM_LIT:0> , highlightthickness = <NUM_LIT:1> , width = <NUM_LIT:0> ) <EOL> self . entry . insert ( <NUM_LIT:0> , self . label [ '<STR_LIT:text>' ] ) <EOL> self . entry . selection_range ( <NUM_LIT:0> , END ) <EOL> self . entry . pack ( ipadx = <NUM_LIT:5> ) <EOL> self . entry . focus_set ( ) <EOL> self . entry . bind ( "<STR_LIT>" , self . edit_finish ) <EOL> self . entry . bind ( "<STR_LIT>" , self . edit_cancel ) <EOL> def edit_finish ( self , event = None ) : <EOL> try : <EOL> entry = self . entry <EOL> del self . entry <EOL> except AttributeError : <EOL> return <EOL> text = entry . get ( ) <EOL> entry . destroy ( ) <EOL> if text and text != self . item . GetText ( ) : <EOL> self . item . SetText ( text ) <EOL> text = self . item . GetText ( ) <EOL> self . label [ '<STR_LIT:text>' ] = text <EOL> self . drawtext ( ) <EOL> self . canvas . focus_set ( ) <EOL> def edit_cancel ( self , event = None ) : <EOL> try : <EOL> entry = self . entry <EOL> del self . entry <EOL> except AttributeError : <EOL> return <EOL> entry . destroy ( ) <EOL> self . drawtext ( ) <EOL> self . canvas . focus_set ( ) <EOL> class TreeItem : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> def GetText ( self ) : <EOL> """<STR_LIT>""" <EOL> def GetLabelText ( self ) : <EOL> """<STR_LIT>""" <EOL> expandable = None <EOL> def _IsExpandable ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . expandable is None : <EOL> self . expandable = self . IsExpandable ( ) <EOL> return self . expandable <EOL> def IsExpandable ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:1> <EOL> def _GetSubList ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . IsExpandable ( ) : <EOL> return [ ] <EOL> sublist = self . GetSubList ( ) <EOL> if not sublist : <EOL> self . expandable = <NUM_LIT:0> <EOL> return sublist <EOL> def IsEditable ( self ) : <EOL> """<STR_LIT>""" <EOL> def SetText ( self , text ) : <EOL> """<STR_LIT>""" <EOL> def GetIconName ( self ) : <EOL> """<STR_LIT>""" <EOL> def GetSelectedIconName ( self ) : <EOL> """<STR_LIT>""" <EOL> def GetSubList ( self ) : <EOL> """<STR_LIT>""" <EOL> def OnDoubleClick ( self ) : <EOL> """<STR_LIT>""" <EOL> class FileTreeItem ( TreeItem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> def GetText ( self ) : <EOL> return os . path . basename ( self . path ) or self . path <EOL> def IsEditable ( self ) : <EOL> return os . path . basename ( self . path ) != "<STR_LIT>" <EOL> def SetText ( self , text ) : <EOL> newpath = os . path . dirname ( self . path ) <EOL> newpath = os . path . join ( newpath , text ) <EOL> if os . path . dirname ( newpath ) != os . path . dirname ( self . path ) : <EOL> return <EOL> try : <EOL> os . rename ( self . path , newpath ) <EOL> self . path = newpath <EOL> except os . error : <EOL> pass <EOL> def GetIconName ( self ) : <EOL> if not self . IsExpandable ( ) : <EOL> return "<STR_LIT>" <EOL> def IsExpandable ( self ) : <EOL> return os . path . isdir ( self . path ) <EOL> def GetSubList ( self ) : <EOL> try : <EOL> names = os . listdir ( self . path ) <EOL> except os . error : <EOL> return [ ] <EOL> names . sort ( key = os . path . normcase ) <EOL> sublist = [ ] <EOL> for name in names : <EOL> item = FileTreeItem ( os . path . join ( self . path , name ) ) <EOL> sublist . append ( item ) <EOL> return sublist <EOL> class ScrolledCanvas : <EOL> def __init__ ( self , master , ** opts ) : <EOL> if '<STR_LIT>' not in opts : <EOL> opts [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . master = master <EOL> self . frame = Frame ( master ) <EOL> self . frame . rowconfigure ( <NUM_LIT:0> , weight = <NUM_LIT:1> ) <EOL> self . frame . columnconfigure ( <NUM_LIT:0> , weight = <NUM_LIT:1> ) <EOL> self . canvas = Canvas ( self . frame , ** opts ) <EOL> self . canvas . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:0> , sticky = "<STR_LIT>" ) <EOL> self . vbar = Scrollbar ( self . frame , name = "<STR_LIT>" ) <EOL> self . vbar . grid ( row = <NUM_LIT:0> , column = <NUM_LIT:1> , sticky = "<STR_LIT>" ) <EOL> self . hbar = Scrollbar ( self . frame , name = "<STR_LIT>" , orient = "<STR_LIT>" ) <EOL> self . hbar . grid ( row = <NUM_LIT:1> , column = <NUM_LIT:0> , sticky = "<STR_LIT>" ) <EOL> self . canvas [ '<STR_LIT>' ] = self . vbar . set <EOL> self . vbar [ '<STR_LIT>' ] = self . canvas . yview <EOL> self . canvas [ '<STR_LIT>' ] = self . hbar . set <EOL> self . hbar [ '<STR_LIT>' ] = self . canvas . xview <EOL> self . canvas . bind ( "<STR_LIT>" , self . page_up ) <EOL> self . canvas . bind ( "<STR_LIT>" , self . page_down ) <EOL> self . canvas . bind ( "<STR_LIT>" , self . unit_up ) <EOL> self . canvas . bind ( "<STR_LIT>" , self . unit_down ) <EOL> self . canvas . bind ( "<STR_LIT>" , self . zoom_height ) <EOL> self . canvas . focus_set ( ) <EOL> def page_up ( self , event ) : <EOL> self . canvas . yview_scroll ( - <NUM_LIT:1> , "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> def page_down ( self , event ) : <EOL> self . canvas . yview_scroll ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> def unit_up ( self , event ) : <EOL> self . canvas . yview_scroll ( - <NUM_LIT:1> , "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> def unit_down ( self , event ) : <EOL> self . canvas . yview_scroll ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> def zoom_height ( self , event ) : <EOL> ZoomHeight . zoom_height ( self . master ) <EOL> return "<STR_LIT>" <EOL> def test ( ) : <EOL> from . import PyShell <EOL> root = Toplevel ( PyShell . root ) <EOL> root . configure ( bd = <NUM_LIT:0> , bg = "<STR_LIT>" ) <EOL> root . focus_set ( ) <EOL> sc = ScrolledCanvas ( root , bg = "<STR_LIT>" , highlightthickness = <NUM_LIT:0> , takefocus = <NUM_LIT:1> ) <EOL> sc . frame . pack ( expand = <NUM_LIT:1> , fill = "<STR_LIT>" ) <EOL> item = FileTreeItem ( "<STR_LIT>" ) <EOL> node = TreeNode ( sc . canvas , None , item ) <EOL> node . expand ( ) <EOL> def test2 ( ) : <EOL> root = Tk ( ) <EOL> root . configure ( bd = <NUM_LIT:0> ) <EOL> canvas = Canvas ( root , bg = "<STR_LIT>" , highlightthickness = <NUM_LIT:0> ) <EOL> canvas . pack ( expand = <NUM_LIT:1> , fill = "<STR_LIT>" ) <EOL> item = FileTreeItem ( os . curdir ) <EOL> node = TreeNode ( canvas , None , item ) <EOL> node . update ( ) <EOL> canvas . focus_set ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
