Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 1 | # Create hierarchical menus for some volumes. |
| 2 | |
| 3 | import os |
| 4 | from Menu import * |
| 5 | |
| 6 | # Since we can't (yet) list the mounted volumes, here's a list of some: |
| 7 | my_volumes = ['C:', 'D:', 'E:', 'F:'] |
| 8 | |
| 9 | def main(): |
| 10 | global oldbar |
| 11 | oldbar = GetMenuBar() |
| 12 | ClearMenuBar() |
| 13 | makevolmenus(my_volumes) |
| 14 | DrawMenuBar() |
| 15 | |
| 16 | def reset(): |
| 17 | oldbar.SetMenuBar() |
| 18 | DrawMenuBar() |
| 19 | |
| 20 | id = 1 |
| 21 | def nextid(): |
| 22 | global id |
| 23 | nid = id |
| 24 | id = id+1 |
| 25 | return nid |
| 26 | |
| 27 | def makevolmenus(volumes): |
| 28 | for vol in volumes: |
| 29 | makevolmenu(vol) |
| 30 | |
| 31 | def makevolmenu(vol): |
| 32 | menu = NewMenu(nextid(), vol) |
| 33 | adddirectory(menu, vol) |
| 34 | menu.InsertMenu(0) |
| 35 | |
| 36 | def adddirectory(menu, dir, maxdepth = 1): |
| 37 | print "adddirectory:", `dir`, maxdepth |
| 38 | files = os.listdir(dir) |
| 39 | item = 0 |
| 40 | for file in files: |
| 41 | item = item+1 |
| 42 | menu.AppendMenu('x') # add a dummy string |
| 43 | menu.SetItem(item, file) # set the actual text |
| 44 | fullname = os.path.join(dir, file) |
| 45 | if os.path.isdir(fullname): |
| 46 | menu.SetItem(item, ':' + file + ':') # append colons |
| 47 | if maxdepth > 0: |
| 48 | id = nextid() |
| 49 | submenu = NewMenu(id, fullname) |
| 50 | adddirectory(submenu, fullname, maxdepth-1) |
| 51 | submenu.InsertMenu(-1) |
| 52 | # If the 'Cmd' is 0x1B, then the 'Mark' is the submenu id |
| 53 | menu.SetItemMark(item, id) |
| 54 | menu.SetItemCmd(item, 0x1B) |
| 55 | if not files: |
| 56 | menu.AppendMenu(':') # dummy item to make it selectable |
| 57 | return menu |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | main() |