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 * |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 5 | import macfs |
| 6 | import sys |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 7 | |
| 8 | def main(): |
| 9 | global oldbar |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 10 | my_volumes = [] |
| 11 | while 1: |
| 12 | fss, ok = macfs.GetDirectory() |
| 13 | if not ok: |
| 14 | break |
| 15 | my_volumes.append(fss.as_pathname()) |
| 16 | if not my_volumes: |
| 17 | return |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 18 | oldbar = GetMenuBar() |
| 19 | ClearMenuBar() |
| 20 | makevolmenus(my_volumes) |
| 21 | DrawMenuBar() |
| 22 | |
| 23 | def reset(): |
| 24 | oldbar.SetMenuBar() |
| 25 | DrawMenuBar() |
| 26 | |
| 27 | id = 1 |
| 28 | def nextid(): |
| 29 | global id |
| 30 | nid = id |
| 31 | id = id+1 |
| 32 | return nid |
| 33 | |
| 34 | def makevolmenus(volumes): |
| 35 | for vol in volumes: |
| 36 | makevolmenu(vol) |
| 37 | |
| 38 | def makevolmenu(vol): |
| 39 | menu = NewMenu(nextid(), vol) |
| 40 | adddirectory(menu, vol) |
| 41 | menu.InsertMenu(0) |
| 42 | |
| 43 | def adddirectory(menu, dir, maxdepth = 1): |
| 44 | print "adddirectory:", `dir`, maxdepth |
| 45 | files = os.listdir(dir) |
| 46 | item = 0 |
| 47 | for file in files: |
| 48 | item = item+1 |
| 49 | menu.AppendMenu('x') # add a dummy string |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 50 | menu.SetMenuItemText(item, file) # set the actual text |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 51 | fullname = os.path.join(dir, file) |
| 52 | if os.path.isdir(fullname): |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 53 | menu.SetMenuItemText(item, ':' + file + ':') # append colons |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 54 | if maxdepth > 0: |
| 55 | id = nextid() |
| 56 | submenu = NewMenu(id, fullname) |
| 57 | adddirectory(submenu, fullname, maxdepth-1) |
| 58 | submenu.InsertMenu(-1) |
| 59 | # If the 'Cmd' is 0x1B, then the 'Mark' is the submenu id |
| 60 | menu.SetItemMark(item, id) |
| 61 | menu.SetItemCmd(item, 0x1B) |
| 62 | if not files: |
| 63 | menu.AppendMenu(':') # dummy item to make it selectable |
| 64 | return menu |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | main() |
Jack Jansen | b81cf9d | 1995-06-06 13:08:40 +0000 | [diff] [blame] | 68 | sys.exit(1) # To allow the user to interact... |