11
#Why should I use an object when a closure works just as well?
12
#God this is such a hack, but it makes me smile.
13
def WorkspaceMenu(parentmenu, parentwindow, workspaces, workspace_order):
15
def OnCloseAll(event):
17
sel = self.control.GetSelection()
18
cnt = self.control.GetPageCount()
21
win = self.control.GetPage(i).GetWindow1()
23
#Yeah, I know that using a function that is placed in the
24
#module's namespace AFTER import is bad form, but I do it
27
self.control.SetSelection(i)
33
for i in xrange(cnt-1, -1, -1):
34
self.OnClose(None, i, self.control.GetPage(i).GetWindow1())
38
#check for dirty and unnamed files...
40
sel = self.control.GetSelection()
41
cnt = self.control.GetPageCount()
45
win = self.control.GetPage(i).GetWindow1()
46
data = os.path.join(win.dirname, win.filename).encode('ascii')
47
d += win.dirty and data == ' '
52
self.dialog("Cannot save workspace with\nmodified untitled documents.", "Workspace Save Aborted!")
55
#get the name of the workspace
58
dlg = wx.TextEntryDialog(parentwindow, "What would you like this workspace to be called?", "Workspace name")
60
dlg.SetValue(openmenu.last)
61
rslt = dlg.ShowModal()
62
workspacename = dlg.GetValue()
65
self.SetStatusText("Workspace save cancelled")
68
wn = workspacename.strip()
70
#check for usable name
72
self.SetStatusText("Workspace save cancelled")
75
#check for unused name
76
if wn.lower() in [i.lower() for i in workspace_order]:
77
if self.dialog("Are you sure you want to replace\nthe pre-existing workspace:\n%s"%wn.lower(), "Duplicate Workspace", wx.YES_NO) != wx.ID_YES:
82
#remove potential duplicates
83
workspace_order[:] = [i for i in workspace_order if i.lower() != wn.lower() or openmenu.ItemRemove(i) or deletemenu.ItemRemove(i)]
86
for i in workspace_order:
89
#handle workspace ordering
90
workspace_order.insert(0, wn)
93
deletemenu.ItemAdd(wn)
96
## print "opening workspace"
98
#handle ordering of workspaces
99
if label in workspace_order:
100
workspace_order.remove(label)
102
workspace_order.insert(0, label)
105
ws = workspaces.get(label, [])
107
#open all documents in the workspace
108
parentwindow.OnDrop(ws)
111
## print "deleting workspace..."
112
#delete the workspace and fix workspace order
114
if label in workspace_order:
115
workspace_order.remove(label)
116
workspaces.pop(label, None)
118
#code that actually modifies the menu
120
closeall = wx.NewId()
121
parentmenu.Append(closeall, "Close All Documents",
122
"Closes all open documents, asking to save changes on modified files")
123
wx.EVT_MENU(parentwindow, closeall, OnCloseAll)
124
parentmenu.AppendSeparator()
126
parentmenu.Append(nwksid, "Save Workspace",
127
"Saves the current workspace, aborts if modified and unnamed files are open")
128
wx.EVT_MENU(parentwindow, nwksid, OnSave)
130
openmenu = filehistory.FileHistory(parentwindow, callback=[OnOpen], seq=workspace_order)
131
deletemenu = filehistory.FileHistory(parentwindow, remove=1,
132
callback=[openmenu.ItemRemove, OnDelete],
134
delmsg=("Are you sure you want to delete the workspace:\n%s",
135
"Delete Workspace?"))
136
openmenu.callback.append(deletemenu.ItemAdd)
137
parentmenu.AppendMenu(wx.NewId(), "Open Workspace", openmenu)
138
parentmenu.AppendMenu(wx.NewId(), "Delete Workspace", deletemenu)
139
parentmenu.AppendSeparator()