1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
# Programmer: Daniel Pozmanter
# E-mail: drpython@bluebottle.com
# Note: You must reply to the verification e-mail to get through.
#
# Copyright 2003-2005 Daniel Pozmanter
#
# Distributed under the terms of the GPL (GNU Public License)
#
# DrPython is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#Pop Up Menu Dialog
import os.path, re
import wx
import drScrolledMessageDialog
import drPopUp
import drShortcuts, drShortcutsFile
def GetPopUpMenuLabels(filename, frame):
try:
f = file(filename, 'r')
text = f.read()
f.close()
except:
drScrolledMessageDialog.ShowMessage(frame, 'File error with: "' + filename + '".', "ERROR")
return []
rePopUpMenu = re.compile(r'^\s*?DrFrame\.AddPluginPopUpMenuFunction\(.*\)', re.MULTILINE)
allPopUps = rePopUpMenu.findall(text)
PopUpArray = []
for s in allPopUps:
#From the Left most '('
start = s.find('(')
#To the Right most ')'
end = s.rfind(')')
if (start > -1) and (end > -1):
s = s[start+1:end]
i = s.find(',')
e = i + 1 + s[i+1:].find(',')
arglabel = s[i+1:e].strip().strip('"')
PopUpArray.append("<Plugin>:"+arglabel)
return PopUpArray
class drPopUpMenuDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, -1, ("Customize Pop Up Menu"), wx.DefaultPosition, wx.Size(-1, -1), wx.DEFAULT_DIALOG_STYLE | wx.THICK_FRAME)
wx.Yield()
self.ID_PROGRAM = 1001
self.ID_POPUP = 1002
self.ID_LIST = 1300
self.ID_ADD = 1003
self.ID_REMOVE = 1004
self.ID_UPDATE = 1005
self.ID_SAVE = 1006
self.ID_UP = 1111
self.ID_DOWN = 2222
self.parent = parent
self.theSizer = wx.FlexGridSizer(5, 4, 5, 10)
self.menubuttonSizer = wx.BoxSizer(wx.VERTICAL)
self.listSizer = wx.BoxSizer(wx.HORIZONTAL)
self.userpreferencesdirectory = parent.userpreferencesdirectory
popupmenulist = []
map(popupmenulist.append, parent.popupmenulist)
if len(popupmenulist) < 1:
popupmenulist = ["<ROOT>", "Undo", "Redo", "<Separator>", "Cut", "Copy", "Paste", "Delete", "<Separator>", "Select All"]
else:
popupmenulist.insert(0, "<ROOT>")
programmenulist = drShortcutsFile.GetShortcutList()
programmenulist.sort()
programmenulist.insert(0, "<Insert Separator>")
self.ListArray = []
self.ListArray.append(programmenulist)
#STC
stclist = []
map(stclist.append, drShortcutsFile.GetSTCShortcutList())
stclist.insert(0, "<Insert Separator>")
self.ListArray.append(stclist)
#DrScript
drscriptlist = []
map(drscriptlist.append, parent.drscriptmenu.titles)
x = 0
l = len(drscriptlist)
while x < l:
drscriptlist[x] = "<DrScript>:" + drscriptlist[x]
x = x + 1
drscriptlist.insert(0, "<Insert Separator>")
self.ListArray.append(drscriptlist)
#Plugins
plist = os.listdir(parent.userpreferencesdirectory + "/plugins")
self.PluginList = []
plugins = []
for p in plist:
i = p.find(".py")
l = len(p)
if i > -1 and (i + 3 == l):
self.PluginList.append("<Plugin>:" + p[:i])
plugins.append(p[:i])
poplist = []
for plugin in plugins:
pluginfile = self.parent.userpreferencesdirectory + "/plugins/" + plugin + ".py"
pluginlist = GetPopUpMenuLabels(pluginfile, self)
plist = self.parent.GetPluginLabels(pluginfile)
for p in plist:
if not (p in pluginlist):
pluginlist.append(p)
if len(pluginlist) > 0:
pluginlist.insert(0, "<Insert Separator>")
self.ListArray.append(pluginlist)
else:
poplist.append("<Plugin>:" + plugin)
for popl in poplist:
i = self.PluginList.index(popl)
self.PluginList.pop(i)
list = ["Standard", "Text Control", "DrScript"]
list.extend(self.PluginList)
self.cboList = wx.ComboBox(self, self.ID_LIST, "Standard", wx.DefaultPosition, (200, -1), list, wx.CB_DROPDOWN|wx.CB_READONLY)
self.programmenu = wx.ListBox(self, self.ID_PROGRAM, wx.DefaultPosition, wx.Size(250, 300), programmenulist)
self.popupmenu = wx.ListBox(self, self.ID_POPUP, wx.DefaultPosition, wx.Size(250, 300), popupmenulist)
self.btnUp = wx.Button(self, self.ID_UP, " Up ")
self.btnAdd = wx.Button(self, self.ID_ADD, " ---> ")
self.btnRemove = wx.Button(self, self.ID_REMOVE, " Remove ")
self.btnDown = wx.Button(self, self.ID_DOWN, " Down ")
self.menubuttonSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.menubuttonSizer.Add(self.btnAdd, 0, wx.SHAPED)
self.menubuttonSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.menubuttonSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.menubuttonSizer.Add(self.btnUp, 0, wx.SHAPED)
self.menubuttonSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.menubuttonSizer.Add(self.btnDown, 0, wx.SHAPED)
self.menubuttonSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.menubuttonSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.menubuttonSizer.Add(self.btnRemove, 0, wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.listSizer.Add(wx.StaticText(self, -1, "List: "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.listSizer.Add(self.cboList, 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(self.listSizer, 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, "Current List:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, "Pop Up Menu:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(self.programmenu, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(self.menubuttonSizer, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(self.popupmenu, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.btnUpdate = wx.Button(self, self.ID_UPDATE, "&Update")
self.btnSave = wx.Button(self, self.ID_SAVE, "&Save")
self.btnClose = wx.Button(self, 101, "&Close")
self.theSizer.Add(wx.StaticText(self, -1, ""), 0, wx.ALIGN_CENTER | wx.SHAPED)
self.theSizer.Add(self.btnClose, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(self.btnUpdate, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.theSizer.Add(self.btnSave, 0, wx.SHAPED | wx.ALIGN_CENTER)
self.btnClose.SetDefault()
self.SetAutoLayout(True)
self.SetSizerAndFit(self.theSizer)
self.Bind(wx.EVT_BUTTON, self.OnbtnUp, id=self.ID_UP)
self.Bind(wx.EVT_BUTTON, self.OnbtnAdd, id=self.ID_ADD)
self.Bind(wx.EVT_BUTTON, self.OnbtnRemove, id=self.ID_REMOVE)
self.Bind(wx.EVT_BUTTON, self.OnbtnDown, id=self.ID_DOWN)
self.Bind(wx.EVT_BUTTON, self.OnbtnUpdate, id=self.ID_UPDATE)
self.Bind(wx.EVT_BUTTON, self.OnbtnSave, id=self.ID_SAVE)
self.Bind(wx.EVT_BUTTON, self.OnbtnClose, id=101)
self.Bind(wx.EVT_COMBOBOX, self.OnList, id=self.ID_LIST)
self.parent.LoadDialogSizeAndPosition(self, 'popupmenudialog.sizeandposition.dat')
def OnCloseW(self, event):
self.parent.SaveDialogSizeAndPosition(self, 'popupmenudialog.sizeandposition.dat')
if event is not None:
event.Skip()
def OnbtnAdd(self, event):
tselection = self.programmenu.GetStringSelection()
tsel = self.programmenu.GetSelection()
if tsel == -1:
drScrolledMessageDialog.ShowMessage(self, "Nothing Selected to Add", "Mistake")
return
sel = self.popupmenu.GetSelection()
if sel == -1:
sel = 0
separator = (tselection == "<Insert Separator>")
if separator:
tselection = "<Separator>"
self.popupmenu.InsertItems([tselection], sel+1)
self.popupmenu.SetSelection(sel+1)
def OnbtnClose(self, event):
self.Close(1)
def OnbtnDown(self, event):
sel = self.popupmenu.GetSelection()
if sel < self.popupmenu.GetCount()-1 and sel > 0:
txt = self.popupmenu.GetString(sel)
self.popupmenu.Delete(sel)
self.popupmenu.InsertItems([txt], sel+1)
self.popupmenu.SetSelection(sel+1)
def OnbtnRemove(self, event):
sel = self.popupmenu.GetSelection()
selection = self.popupmenu.GetStringSelection()
if not sel:
drScrolledMessageDialog.ShowMessage(self, "You cannot remove the root item.", "Mistake")
return
if sel == -1:
drScrolledMessageDialog.ShowMessage(self, "Nothing Selected to Remove", "Mistake")
return
self.popupmenu.Delete(sel)
self.popupmenu.SetSelection(sel-1)
def OnbtnUp(self, event):
sel = self.popupmenu.GetSelection()
if sel > 1:
txt = self.popupmenu.GetString(sel)
self.popupmenu.Delete(sel)
self.popupmenu.InsertItems([txt], sel-1)
self.popupmenu.SetSelection(sel-1)
def OnbtnUpdate(self, event):
y = 0
c = self.popupmenu.GetCount()
popupmenulist = []
while y < c:
pop = self.popupmenu.GetString(y)
if not pop == "<ROOT>":
popupmenulist.append(pop)
y = y + 1
self.parent.popupmenulist = popupmenulist
if self.parent.prefs.enablefeedback:
drScrolledMessageDialog.ShowMessage(self, ("Succesfully updated the current instance of DrPython.\nClick Save to make it permanent."), "Updated Pop Up Menu")
def OnbtnSave(self, event):
y = 0
c = self.popupmenu.GetCount()
popupmenustring = ""
popupmenulist = []
while y < c:
pop = self.popupmenu.GetString(y)
if not pop == "<ROOT>":
popupmenustring = popupmenustring + pop + "\n"
popupmenulist.append(pop)
y = y + 1
self.parent.popupmenulist = popupmenulist
if (not os.path.exists(self.userpreferencesdirectory)):
drScrolledMessageDialog.ShowMessage(self, ("Dude, you've got some problems...\nYour userpreferencesdirectory (" + self.userpreferencesdirectory + ") does not exist!\nLet's not bother speculating about how or why.\nRead the help file for this truly screwed up situation.\nDrPython will now politely ignore your request to save.\nTry again when you have fixed this problem."), "Huge Error")
return
popupfile = self.userpreferencesdirectory + "/popupmenu.dat"
try:
f = file(popupfile, 'w')
f.write(popupmenustring)
f.close()
except IOError:
drScrolledMessageDialog.ShowMessage(self, ("There were some problems writing to:\n" + popupfile + "\nEither the file is having metaphysical issues, or you do not have permission to write.\nFor metaphysical issues, consult the documentation.\nFor permission issues, change the permissions on the directory to allow yourself write access.\nDrPython will now politely ignore your request to save.\nTry again when you have fixed the problem."), "Write Error")
return
if self.parent.prefs.enablefeedback:
drScrolledMessageDialog.ShowMessage(self, ("Succesfully wrote to:\n" + popupfile + "\nand updated the current instance of DrPython."), "Saved Pop Up Menu")
def OnList(self, event):
sel = self.cboList.GetSelection()
self.programmenu.Set(self.ListArray[sel])
|