jpayne@69
|
1 # XXX TO DO:
|
jpayne@69
|
2 # - popup menu
|
jpayne@69
|
3 # - support partial or total redisplay
|
jpayne@69
|
4 # - key bindings (instead of quick-n-dirty bindings on Canvas):
|
jpayne@69
|
5 # - up/down arrow keys to move focus around
|
jpayne@69
|
6 # - ditto for page up/down, home/end
|
jpayne@69
|
7 # - left/right arrows to expand/collapse & move out/in
|
jpayne@69
|
8 # - more doc strings
|
jpayne@69
|
9 # - add icons for "file", "module", "class", "method"; better "python" icon
|
jpayne@69
|
10 # - callback for selection???
|
jpayne@69
|
11 # - multiple-item selection
|
jpayne@69
|
12 # - tooltips
|
jpayne@69
|
13 # - redo geometry without magic numbers
|
jpayne@69
|
14 # - keep track of object ids to allow more careful cleaning
|
jpayne@69
|
15 # - optimize tree redraw after expand of subnode
|
jpayne@69
|
16
|
jpayne@69
|
17 import os
|
jpayne@69
|
18
|
jpayne@69
|
19 from tkinter import *
|
jpayne@69
|
20 from tkinter.ttk import Frame, Scrollbar
|
jpayne@69
|
21
|
jpayne@69
|
22 from idlelib.config import idleConf
|
jpayne@69
|
23 from idlelib import zoomheight
|
jpayne@69
|
24
|
jpayne@69
|
25 ICONDIR = "Icons"
|
jpayne@69
|
26
|
jpayne@69
|
27 # Look for Icons subdirectory in the same directory as this module
|
jpayne@69
|
28 try:
|
jpayne@69
|
29 _icondir = os.path.join(os.path.dirname(__file__), ICONDIR)
|
jpayne@69
|
30 except NameError:
|
jpayne@69
|
31 _icondir = ICONDIR
|
jpayne@69
|
32 if os.path.isdir(_icondir):
|
jpayne@69
|
33 ICONDIR = _icondir
|
jpayne@69
|
34 elif not os.path.isdir(ICONDIR):
|
jpayne@69
|
35 raise RuntimeError("can't find icon directory (%r)" % (ICONDIR,))
|
jpayne@69
|
36
|
jpayne@69
|
37 def listicons(icondir=ICONDIR):
|
jpayne@69
|
38 """Utility to display the available icons."""
|
jpayne@69
|
39 root = Tk()
|
jpayne@69
|
40 import glob
|
jpayne@69
|
41 list = glob.glob(os.path.join(icondir, "*.gif"))
|
jpayne@69
|
42 list.sort()
|
jpayne@69
|
43 images = []
|
jpayne@69
|
44 row = column = 0
|
jpayne@69
|
45 for file in list:
|
jpayne@69
|
46 name = os.path.splitext(os.path.basename(file))[0]
|
jpayne@69
|
47 image = PhotoImage(file=file, master=root)
|
jpayne@69
|
48 images.append(image)
|
jpayne@69
|
49 label = Label(root, image=image, bd=1, relief="raised")
|
jpayne@69
|
50 label.grid(row=row, column=column)
|
jpayne@69
|
51 label = Label(root, text=name)
|
jpayne@69
|
52 label.grid(row=row+1, column=column)
|
jpayne@69
|
53 column = column + 1
|
jpayne@69
|
54 if column >= 10:
|
jpayne@69
|
55 row = row+2
|
jpayne@69
|
56 column = 0
|
jpayne@69
|
57 root.images = images
|
jpayne@69
|
58
|
jpayne@69
|
59 def wheel_event(event, widget=None):
|
jpayne@69
|
60 """Handle scrollwheel event.
|
jpayne@69
|
61
|
jpayne@69
|
62 For wheel up, event.delta = 120*n on Windows, -1*n on darwin,
|
jpayne@69
|
63 where n can be > 1 if one scrolls fast. Flicking the wheel
|
jpayne@69
|
64 generates up to maybe 20 events with n up to 10 or more 1.
|
jpayne@69
|
65 Macs use wheel down (delta = 1*n) to scroll up, so positive
|
jpayne@69
|
66 delta means to scroll up on both systems.
|
jpayne@69
|
67
|
jpayne@69
|
68 X-11 sends Control-Button-4,5 events instead.
|
jpayne@69
|
69
|
jpayne@69
|
70 The widget parameter is needed so browser label bindings can pass
|
jpayne@69
|
71 the underlying canvas.
|
jpayne@69
|
72
|
jpayne@69
|
73 This function depends on widget.yview to not be overridden by
|
jpayne@69
|
74 a subclass.
|
jpayne@69
|
75 """
|
jpayne@69
|
76 up = {EventType.MouseWheel: event.delta > 0,
|
jpayne@69
|
77 EventType.ButtonPress: event.num == 4}
|
jpayne@69
|
78 lines = -5 if up[event.type] else 5
|
jpayne@69
|
79 widget = event.widget if widget is None else widget
|
jpayne@69
|
80 widget.yview(SCROLL, lines, 'units')
|
jpayne@69
|
81 return 'break'
|
jpayne@69
|
82
|
jpayne@69
|
83
|
jpayne@69
|
84 class TreeNode:
|
jpayne@69
|
85
|
jpayne@69
|
86 def __init__(self, canvas, parent, item):
|
jpayne@69
|
87 self.canvas = canvas
|
jpayne@69
|
88 self.parent = parent
|
jpayne@69
|
89 self.item = item
|
jpayne@69
|
90 self.state = 'collapsed'
|
jpayne@69
|
91 self.selected = False
|
jpayne@69
|
92 self.children = []
|
jpayne@69
|
93 self.x = self.y = None
|
jpayne@69
|
94 self.iconimages = {} # cache of PhotoImage instances for icons
|
jpayne@69
|
95
|
jpayne@69
|
96 def destroy(self):
|
jpayne@69
|
97 for c in self.children[:]:
|
jpayne@69
|
98 self.children.remove(c)
|
jpayne@69
|
99 c.destroy()
|
jpayne@69
|
100 self.parent = None
|
jpayne@69
|
101
|
jpayne@69
|
102 def geticonimage(self, name):
|
jpayne@69
|
103 try:
|
jpayne@69
|
104 return self.iconimages[name]
|
jpayne@69
|
105 except KeyError:
|
jpayne@69
|
106 pass
|
jpayne@69
|
107 file, ext = os.path.splitext(name)
|
jpayne@69
|
108 ext = ext or ".gif"
|
jpayne@69
|
109 fullname = os.path.join(ICONDIR, file + ext)
|
jpayne@69
|
110 image = PhotoImage(master=self.canvas, file=fullname)
|
jpayne@69
|
111 self.iconimages[name] = image
|
jpayne@69
|
112 return image
|
jpayne@69
|
113
|
jpayne@69
|
114 def select(self, event=None):
|
jpayne@69
|
115 if self.selected:
|
jpayne@69
|
116 return
|
jpayne@69
|
117 self.deselectall()
|
jpayne@69
|
118 self.selected = True
|
jpayne@69
|
119 self.canvas.delete(self.image_id)
|
jpayne@69
|
120 self.drawicon()
|
jpayne@69
|
121 self.drawtext()
|
jpayne@69
|
122
|
jpayne@69
|
123 def deselect(self, event=None):
|
jpayne@69
|
124 if not self.selected:
|
jpayne@69
|
125 return
|
jpayne@69
|
126 self.selected = False
|
jpayne@69
|
127 self.canvas.delete(self.image_id)
|
jpayne@69
|
128 self.drawicon()
|
jpayne@69
|
129 self.drawtext()
|
jpayne@69
|
130
|
jpayne@69
|
131 def deselectall(self):
|
jpayne@69
|
132 if self.parent:
|
jpayne@69
|
133 self.parent.deselectall()
|
jpayne@69
|
134 else:
|
jpayne@69
|
135 self.deselecttree()
|
jpayne@69
|
136
|
jpayne@69
|
137 def deselecttree(self):
|
jpayne@69
|
138 if self.selected:
|
jpayne@69
|
139 self.deselect()
|
jpayne@69
|
140 for child in self.children:
|
jpayne@69
|
141 child.deselecttree()
|
jpayne@69
|
142
|
jpayne@69
|
143 def flip(self, event=None):
|
jpayne@69
|
144 if self.state == 'expanded':
|
jpayne@69
|
145 self.collapse()
|
jpayne@69
|
146 else:
|
jpayne@69
|
147 self.expand()
|
jpayne@69
|
148 self.item.OnDoubleClick()
|
jpayne@69
|
149 return "break"
|
jpayne@69
|
150
|
jpayne@69
|
151 def expand(self, event=None):
|
jpayne@69
|
152 if not self.item._IsExpandable():
|
jpayne@69
|
153 return
|
jpayne@69
|
154 if self.state != 'expanded':
|
jpayne@69
|
155 self.state = 'expanded'
|
jpayne@69
|
156 self.update()
|
jpayne@69
|
157 self.view()
|
jpayne@69
|
158
|
jpayne@69
|
159 def collapse(self, event=None):
|
jpayne@69
|
160 if self.state != 'collapsed':
|
jpayne@69
|
161 self.state = 'collapsed'
|
jpayne@69
|
162 self.update()
|
jpayne@69
|
163
|
jpayne@69
|
164 def view(self):
|
jpayne@69
|
165 top = self.y - 2
|
jpayne@69
|
166 bottom = self.lastvisiblechild().y + 17
|
jpayne@69
|
167 height = bottom - top
|
jpayne@69
|
168 visible_top = self.canvas.canvasy(0)
|
jpayne@69
|
169 visible_height = self.canvas.winfo_height()
|
jpayne@69
|
170 visible_bottom = self.canvas.canvasy(visible_height)
|
jpayne@69
|
171 if visible_top <= top and bottom <= visible_bottom:
|
jpayne@69
|
172 return
|
jpayne@69
|
173 x0, y0, x1, y1 = self.canvas._getints(self.canvas['scrollregion'])
|
jpayne@69
|
174 if top >= visible_top and height <= visible_height:
|
jpayne@69
|
175 fraction = top + height - visible_height
|
jpayne@69
|
176 else:
|
jpayne@69
|
177 fraction = top
|
jpayne@69
|
178 fraction = float(fraction) / y1
|
jpayne@69
|
179 self.canvas.yview_moveto(fraction)
|
jpayne@69
|
180
|
jpayne@69
|
181 def lastvisiblechild(self):
|
jpayne@69
|
182 if self.children and self.state == 'expanded':
|
jpayne@69
|
183 return self.children[-1].lastvisiblechild()
|
jpayne@69
|
184 else:
|
jpayne@69
|
185 return self
|
jpayne@69
|
186
|
jpayne@69
|
187 def update(self):
|
jpayne@69
|
188 if self.parent:
|
jpayne@69
|
189 self.parent.update()
|
jpayne@69
|
190 else:
|
jpayne@69
|
191 oldcursor = self.canvas['cursor']
|
jpayne@69
|
192 self.canvas['cursor'] = "watch"
|
jpayne@69
|
193 self.canvas.update()
|
jpayne@69
|
194 self.canvas.delete(ALL) # XXX could be more subtle
|
jpayne@69
|
195 self.draw(7, 2)
|
jpayne@69
|
196 x0, y0, x1, y1 = self.canvas.bbox(ALL)
|
jpayne@69
|
197 self.canvas.configure(scrollregion=(0, 0, x1, y1))
|
jpayne@69
|
198 self.canvas['cursor'] = oldcursor
|
jpayne@69
|
199
|
jpayne@69
|
200 def draw(self, x, y):
|
jpayne@69
|
201 # XXX This hard-codes too many geometry constants!
|
jpayne@69
|
202 dy = 20
|
jpayne@69
|
203 self.x, self.y = x, y
|
jpayne@69
|
204 self.drawicon()
|
jpayne@69
|
205 self.drawtext()
|
jpayne@69
|
206 if self.state != 'expanded':
|
jpayne@69
|
207 return y + dy
|
jpayne@69
|
208 # draw children
|
jpayne@69
|
209 if not self.children:
|
jpayne@69
|
210 sublist = self.item._GetSubList()
|
jpayne@69
|
211 if not sublist:
|
jpayne@69
|
212 # _IsExpandable() was mistaken; that's allowed
|
jpayne@69
|
213 return y+17
|
jpayne@69
|
214 for item in sublist:
|
jpayne@69
|
215 child = self.__class__(self.canvas, self, item)
|
jpayne@69
|
216 self.children.append(child)
|
jpayne@69
|
217 cx = x+20
|
jpayne@69
|
218 cy = y + dy
|
jpayne@69
|
219 cylast = 0
|
jpayne@69
|
220 for child in self.children:
|
jpayne@69
|
221 cylast = cy
|
jpayne@69
|
222 self.canvas.create_line(x+9, cy+7, cx, cy+7, fill="gray50")
|
jpayne@69
|
223 cy = child.draw(cx, cy)
|
jpayne@69
|
224 if child.item._IsExpandable():
|
jpayne@69
|
225 if child.state == 'expanded':
|
jpayne@69
|
226 iconname = "minusnode"
|
jpayne@69
|
227 callback = child.collapse
|
jpayne@69
|
228 else:
|
jpayne@69
|
229 iconname = "plusnode"
|
jpayne@69
|
230 callback = child.expand
|
jpayne@69
|
231 image = self.geticonimage(iconname)
|
jpayne@69
|
232 id = self.canvas.create_image(x+9, cylast+7, image=image)
|
jpayne@69
|
233 # XXX This leaks bindings until canvas is deleted:
|
jpayne@69
|
234 self.canvas.tag_bind(id, "<1>", callback)
|
jpayne@69
|
235 self.canvas.tag_bind(id, "<Double-1>", lambda x: None)
|
jpayne@69
|
236 id = self.canvas.create_line(x+9, y+10, x+9, cylast+7,
|
jpayne@69
|
237 ##stipple="gray50", # XXX Seems broken in Tk 8.0.x
|
jpayne@69
|
238 fill="gray50")
|
jpayne@69
|
239 self.canvas.tag_lower(id) # XXX .lower(id) before Python 1.5.2
|
jpayne@69
|
240 return cy
|
jpayne@69
|
241
|
jpayne@69
|
242 def drawicon(self):
|
jpayne@69
|
243 if self.selected:
|
jpayne@69
|
244 imagename = (self.item.GetSelectedIconName() or
|
jpayne@69
|
245 self.item.GetIconName() or
|
jpayne@69
|
246 "openfolder")
|
jpayne@69
|
247 else:
|
jpayne@69
|
248 imagename = self.item.GetIconName() or "folder"
|
jpayne@69
|
249 image = self.geticonimage(imagename)
|
jpayne@69
|
250 id = self.canvas.create_image(self.x, self.y, anchor="nw", image=image)
|
jpayne@69
|
251 self.image_id = id
|
jpayne@69
|
252 self.canvas.tag_bind(id, "<1>", self.select)
|
jpayne@69
|
253 self.canvas.tag_bind(id, "<Double-1>", self.flip)
|
jpayne@69
|
254
|
jpayne@69
|
255 def drawtext(self):
|
jpayne@69
|
256 textx = self.x+20-1
|
jpayne@69
|
257 texty = self.y-4
|
jpayne@69
|
258 labeltext = self.item.GetLabelText()
|
jpayne@69
|
259 if labeltext:
|
jpayne@69
|
260 id = self.canvas.create_text(textx, texty, anchor="nw",
|
jpayne@69
|
261 text=labeltext)
|
jpayne@69
|
262 self.canvas.tag_bind(id, "<1>", self.select)
|
jpayne@69
|
263 self.canvas.tag_bind(id, "<Double-1>", self.flip)
|
jpayne@69
|
264 x0, y0, x1, y1 = self.canvas.bbox(id)
|
jpayne@69
|
265 textx = max(x1, 200) + 10
|
jpayne@69
|
266 text = self.item.GetText() or "<no text>"
|
jpayne@69
|
267 try:
|
jpayne@69
|
268 self.entry
|
jpayne@69
|
269 except AttributeError:
|
jpayne@69
|
270 pass
|
jpayne@69
|
271 else:
|
jpayne@69
|
272 self.edit_finish()
|
jpayne@69
|
273 try:
|
jpayne@69
|
274 self.label
|
jpayne@69
|
275 except AttributeError:
|
jpayne@69
|
276 # padding carefully selected (on Windows) to match Entry widget:
|
jpayne@69
|
277 self.label = Label(self.canvas, text=text, bd=0, padx=2, pady=2)
|
jpayne@69
|
278 theme = idleConf.CurrentTheme()
|
jpayne@69
|
279 if self.selected:
|
jpayne@69
|
280 self.label.configure(idleConf.GetHighlight(theme, 'hilite'))
|
jpayne@69
|
281 else:
|
jpayne@69
|
282 self.label.configure(idleConf.GetHighlight(theme, 'normal'))
|
jpayne@69
|
283 id = self.canvas.create_window(textx, texty,
|
jpayne@69
|
284 anchor="nw", window=self.label)
|
jpayne@69
|
285 self.label.bind("<1>", self.select_or_edit)
|
jpayne@69
|
286 self.label.bind("<Double-1>", self.flip)
|
jpayne@69
|
287 self.label.bind("<MouseWheel>", lambda e: wheel_event(e, self.canvas))
|
jpayne@69
|
288 self.label.bind("<Button-4>", lambda e: wheel_event(e, self.canvas))
|
jpayne@69
|
289 self.label.bind("<Button-5>", lambda e: wheel_event(e, self.canvas))
|
jpayne@69
|
290 self.text_id = id
|
jpayne@69
|
291
|
jpayne@69
|
292 def select_or_edit(self, event=None):
|
jpayne@69
|
293 if self.selected and self.item.IsEditable():
|
jpayne@69
|
294 self.edit(event)
|
jpayne@69
|
295 else:
|
jpayne@69
|
296 self.select(event)
|
jpayne@69
|
297
|
jpayne@69
|
298 def edit(self, event=None):
|
jpayne@69
|
299 self.entry = Entry(self.label, bd=0, highlightthickness=1, width=0)
|
jpayne@69
|
300 self.entry.insert(0, self.label['text'])
|
jpayne@69
|
301 self.entry.selection_range(0, END)
|
jpayne@69
|
302 self.entry.pack(ipadx=5)
|
jpayne@69
|
303 self.entry.focus_set()
|
jpayne@69
|
304 self.entry.bind("<Return>", self.edit_finish)
|
jpayne@69
|
305 self.entry.bind("<Escape>", self.edit_cancel)
|
jpayne@69
|
306
|
jpayne@69
|
307 def edit_finish(self, event=None):
|
jpayne@69
|
308 try:
|
jpayne@69
|
309 entry = self.entry
|
jpayne@69
|
310 del self.entry
|
jpayne@69
|
311 except AttributeError:
|
jpayne@69
|
312 return
|
jpayne@69
|
313 text = entry.get()
|
jpayne@69
|
314 entry.destroy()
|
jpayne@69
|
315 if text and text != self.item.GetText():
|
jpayne@69
|
316 self.item.SetText(text)
|
jpayne@69
|
317 text = self.item.GetText()
|
jpayne@69
|
318 self.label['text'] = text
|
jpayne@69
|
319 self.drawtext()
|
jpayne@69
|
320 self.canvas.focus_set()
|
jpayne@69
|
321
|
jpayne@69
|
322 def edit_cancel(self, event=None):
|
jpayne@69
|
323 try:
|
jpayne@69
|
324 entry = self.entry
|
jpayne@69
|
325 del self.entry
|
jpayne@69
|
326 except AttributeError:
|
jpayne@69
|
327 return
|
jpayne@69
|
328 entry.destroy()
|
jpayne@69
|
329 self.drawtext()
|
jpayne@69
|
330 self.canvas.focus_set()
|
jpayne@69
|
331
|
jpayne@69
|
332
|
jpayne@69
|
333 class TreeItem:
|
jpayne@69
|
334
|
jpayne@69
|
335 """Abstract class representing tree items.
|
jpayne@69
|
336
|
jpayne@69
|
337 Methods should typically be overridden, otherwise a default action
|
jpayne@69
|
338 is used.
|
jpayne@69
|
339
|
jpayne@69
|
340 """
|
jpayne@69
|
341
|
jpayne@69
|
342 def __init__(self):
|
jpayne@69
|
343 """Constructor. Do whatever you need to do."""
|
jpayne@69
|
344
|
jpayne@69
|
345 def GetText(self):
|
jpayne@69
|
346 """Return text string to display."""
|
jpayne@69
|
347
|
jpayne@69
|
348 def GetLabelText(self):
|
jpayne@69
|
349 """Return label text string to display in front of text (if any)."""
|
jpayne@69
|
350
|
jpayne@69
|
351 expandable = None
|
jpayne@69
|
352
|
jpayne@69
|
353 def _IsExpandable(self):
|
jpayne@69
|
354 """Do not override! Called by TreeNode."""
|
jpayne@69
|
355 if self.expandable is None:
|
jpayne@69
|
356 self.expandable = self.IsExpandable()
|
jpayne@69
|
357 return self.expandable
|
jpayne@69
|
358
|
jpayne@69
|
359 def IsExpandable(self):
|
jpayne@69
|
360 """Return whether there are subitems."""
|
jpayne@69
|
361 return 1
|
jpayne@69
|
362
|
jpayne@69
|
363 def _GetSubList(self):
|
jpayne@69
|
364 """Do not override! Called by TreeNode."""
|
jpayne@69
|
365 if not self.IsExpandable():
|
jpayne@69
|
366 return []
|
jpayne@69
|
367 sublist = self.GetSubList()
|
jpayne@69
|
368 if not sublist:
|
jpayne@69
|
369 self.expandable = 0
|
jpayne@69
|
370 return sublist
|
jpayne@69
|
371
|
jpayne@69
|
372 def IsEditable(self):
|
jpayne@69
|
373 """Return whether the item's text may be edited."""
|
jpayne@69
|
374
|
jpayne@69
|
375 def SetText(self, text):
|
jpayne@69
|
376 """Change the item's text (if it is editable)."""
|
jpayne@69
|
377
|
jpayne@69
|
378 def GetIconName(self):
|
jpayne@69
|
379 """Return name of icon to be displayed normally."""
|
jpayne@69
|
380
|
jpayne@69
|
381 def GetSelectedIconName(self):
|
jpayne@69
|
382 """Return name of icon to be displayed when selected."""
|
jpayne@69
|
383
|
jpayne@69
|
384 def GetSubList(self):
|
jpayne@69
|
385 """Return list of items forming sublist."""
|
jpayne@69
|
386
|
jpayne@69
|
387 def OnDoubleClick(self):
|
jpayne@69
|
388 """Called on a double-click on the item."""
|
jpayne@69
|
389
|
jpayne@69
|
390
|
jpayne@69
|
391 # Example application
|
jpayne@69
|
392
|
jpayne@69
|
393 class FileTreeItem(TreeItem):
|
jpayne@69
|
394
|
jpayne@69
|
395 """Example TreeItem subclass -- browse the file system."""
|
jpayne@69
|
396
|
jpayne@69
|
397 def __init__(self, path):
|
jpayne@69
|
398 self.path = path
|
jpayne@69
|
399
|
jpayne@69
|
400 def GetText(self):
|
jpayne@69
|
401 return os.path.basename(self.path) or self.path
|
jpayne@69
|
402
|
jpayne@69
|
403 def IsEditable(self):
|
jpayne@69
|
404 return os.path.basename(self.path) != ""
|
jpayne@69
|
405
|
jpayne@69
|
406 def SetText(self, text):
|
jpayne@69
|
407 newpath = os.path.dirname(self.path)
|
jpayne@69
|
408 newpath = os.path.join(newpath, text)
|
jpayne@69
|
409 if os.path.dirname(newpath) != os.path.dirname(self.path):
|
jpayne@69
|
410 return
|
jpayne@69
|
411 try:
|
jpayne@69
|
412 os.rename(self.path, newpath)
|
jpayne@69
|
413 self.path = newpath
|
jpayne@69
|
414 except OSError:
|
jpayne@69
|
415 pass
|
jpayne@69
|
416
|
jpayne@69
|
417 def GetIconName(self):
|
jpayne@69
|
418 if not self.IsExpandable():
|
jpayne@69
|
419 return "python" # XXX wish there was a "file" icon
|
jpayne@69
|
420
|
jpayne@69
|
421 def IsExpandable(self):
|
jpayne@69
|
422 return os.path.isdir(self.path)
|
jpayne@69
|
423
|
jpayne@69
|
424 def GetSubList(self):
|
jpayne@69
|
425 try:
|
jpayne@69
|
426 names = os.listdir(self.path)
|
jpayne@69
|
427 except OSError:
|
jpayne@69
|
428 return []
|
jpayne@69
|
429 names.sort(key = os.path.normcase)
|
jpayne@69
|
430 sublist = []
|
jpayne@69
|
431 for name in names:
|
jpayne@69
|
432 item = FileTreeItem(os.path.join(self.path, name))
|
jpayne@69
|
433 sublist.append(item)
|
jpayne@69
|
434 return sublist
|
jpayne@69
|
435
|
jpayne@69
|
436
|
jpayne@69
|
437 # A canvas widget with scroll bars and some useful bindings
|
jpayne@69
|
438
|
jpayne@69
|
439 class ScrolledCanvas:
|
jpayne@69
|
440
|
jpayne@69
|
441 def __init__(self, master, **opts):
|
jpayne@69
|
442 if 'yscrollincrement' not in opts:
|
jpayne@69
|
443 opts['yscrollincrement'] = 17
|
jpayne@69
|
444 self.master = master
|
jpayne@69
|
445 self.frame = Frame(master)
|
jpayne@69
|
446 self.frame.rowconfigure(0, weight=1)
|
jpayne@69
|
447 self.frame.columnconfigure(0, weight=1)
|
jpayne@69
|
448 self.canvas = Canvas(self.frame, **opts)
|
jpayne@69
|
449 self.canvas.grid(row=0, column=0, sticky="nsew")
|
jpayne@69
|
450 self.vbar = Scrollbar(self.frame, name="vbar")
|
jpayne@69
|
451 self.vbar.grid(row=0, column=1, sticky="nse")
|
jpayne@69
|
452 self.hbar = Scrollbar(self.frame, name="hbar", orient="horizontal")
|
jpayne@69
|
453 self.hbar.grid(row=1, column=0, sticky="ews")
|
jpayne@69
|
454 self.canvas['yscrollcommand'] = self.vbar.set
|
jpayne@69
|
455 self.vbar['command'] = self.canvas.yview
|
jpayne@69
|
456 self.canvas['xscrollcommand'] = self.hbar.set
|
jpayne@69
|
457 self.hbar['command'] = self.canvas.xview
|
jpayne@69
|
458 self.canvas.bind("<Key-Prior>", self.page_up)
|
jpayne@69
|
459 self.canvas.bind("<Key-Next>", self.page_down)
|
jpayne@69
|
460 self.canvas.bind("<Key-Up>", self.unit_up)
|
jpayne@69
|
461 self.canvas.bind("<Key-Down>", self.unit_down)
|
jpayne@69
|
462 self.canvas.bind("<MouseWheel>", wheel_event)
|
jpayne@69
|
463 self.canvas.bind("<Button-4>", wheel_event)
|
jpayne@69
|
464 self.canvas.bind("<Button-5>", wheel_event)
|
jpayne@69
|
465 #if isinstance(master, Toplevel) or isinstance(master, Tk):
|
jpayne@69
|
466 self.canvas.bind("<Alt-Key-2>", self.zoom_height)
|
jpayne@69
|
467 self.canvas.focus_set()
|
jpayne@69
|
468 def page_up(self, event):
|
jpayne@69
|
469 self.canvas.yview_scroll(-1, "page")
|
jpayne@69
|
470 return "break"
|
jpayne@69
|
471 def page_down(self, event):
|
jpayne@69
|
472 self.canvas.yview_scroll(1, "page")
|
jpayne@69
|
473 return "break"
|
jpayne@69
|
474 def unit_up(self, event):
|
jpayne@69
|
475 self.canvas.yview_scroll(-1, "unit")
|
jpayne@69
|
476 return "break"
|
jpayne@69
|
477 def unit_down(self, event):
|
jpayne@69
|
478 self.canvas.yview_scroll(1, "unit")
|
jpayne@69
|
479 return "break"
|
jpayne@69
|
480 def zoom_height(self, event):
|
jpayne@69
|
481 zoomheight.zoom_height(self.master)
|
jpayne@69
|
482 return "break"
|
jpayne@69
|
483
|
jpayne@69
|
484
|
jpayne@69
|
485 def _tree_widget(parent): # htest #
|
jpayne@69
|
486 top = Toplevel(parent)
|
jpayne@69
|
487 x, y = map(int, parent.geometry().split('+')[1:])
|
jpayne@69
|
488 top.geometry("+%d+%d" % (x+50, y+175))
|
jpayne@69
|
489 sc = ScrolledCanvas(top, bg="white", highlightthickness=0, takefocus=1)
|
jpayne@69
|
490 sc.frame.pack(expand=1, fill="both", side=LEFT)
|
jpayne@69
|
491 item = FileTreeItem(ICONDIR)
|
jpayne@69
|
492 node = TreeNode(sc.canvas, None, item)
|
jpayne@69
|
493 node.expand()
|
jpayne@69
|
494
|
jpayne@69
|
495 if __name__ == '__main__':
|
jpayne@69
|
496 from unittest import main
|
jpayne@69
|
497 main('idlelib.idle_test.test_tree', verbosity=2, exit=False)
|
jpayne@69
|
498
|
jpayne@69
|
499 from idlelib.idle_test.htest import run
|
jpayne@69
|
500 run(_tree_widget)
|