~ubuntu-branches/ubuntu/trusty/drpython/trusty

« back to all changes in this revision

Viewing changes to drScriptMenu.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-07-03 22:11:49 UTC
  • mfrom: (0.1.3 upstream) (8 intrepid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20080703221149-puu681p4w33g3s3m
* New upstream release.
* Bump epoch due to new version numbering system.
* debian/control:
  - Adjust dependencies for wxwidgets2.8.
* debian/patches/15_output_redirection.dpatch:
  - Set standard output and error to valid UNIX locations.
* Adjust patches for new upstream release:
  - debian/patches/03_pythonfix.dpatch
* Drop patches, implemented upstream:
  - debian/patches/10_ctrl_q_to_exit_as_default_shortcut.dpatch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#       Programmer:     Daniel Pozmanter
2
 
#       E-mail:         drpython@bluebottle.com
3
 
#       Note:           You must reply to the verification e-mail to get through.
4
 
#
5
 
#       Copyright 2003-2005 Daniel Pozmanter
6
 
#
7
 
#       Distributed under the terms of the GPL (GNU Public License)
 
1
#   Programmer: Daniel Pozmanter
 
2
#   E-mail:     drpython@bluebottle.com
 
3
#   Note:       You must reply to the verification e-mail to get through.
 
4
#
 
5
#   Copyright 2003-2007 Daniel Pozmanter
 
6
#
 
7
#   Distributed under the terms of the GPL (GNU Public License)
8
8
#
9
9
#    DrPython is free software; you can redistribute it and/or modify
10
10
#    it under the terms of the GNU General Public License as published by
32
32
from drMenu import drMenu
33
33
 
34
34
def updatescripts(scriptfile, paths, titles):
35
 
        l = len(paths)
36
 
        x = 0
37
 
        f = open(scriptfile, 'w')
38
 
        while (x < l):
39
 
                f.write("<path>" + paths[x] + "</path><title>" + titles[x] + "</title>\n")
40
 
                x = x + 1
41
 
        f.close()
 
35
    l = len(paths)
 
36
    x = 0
 
37
    f = open(scriptfile, 'w')
 
38
    while x < l:
 
39
        f.write("<path>" + paths[x] + "</path><title>" + titles[x] + "</title>\n")
 
40
        x = x + 1
 
41
    f.close()
42
42
 
43
43
class drNewShellDialog(wx.Dialog):
44
44
 
45
 
        def __init__(self, parent, title):
46
 
                wx.Dialog.__init__(self, parent, -1, title, wx.Point(50, 50), wx.Size(375, 250), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)
47
 
 
48
 
                self.ID_SAVE = 1005
49
 
 
50
 
                self.theSizer = wx.FlexGridSizer(6, 3, 5, 10)
51
 
 
52
 
                self.btnSave = wx.Button(self, self.ID_SAVE, "&Save")
53
 
 
54
 
                self.parent = parent
55
 
 
56
 
                self.txtTitle = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
57
 
                self.txtCommand = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
58
 
                self.txtArguments = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
59
 
                self.txtDirectory = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
60
 
                self.chkRunInPrompt = wx.CheckBox(self, -1, "")
61
 
                self.chkRunInPrompt.SetValue(True)
62
 
 
63
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
64
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
65
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
66
 
 
67
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
68
 
                self.theSizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
69
 
                self.theSizer.Add(self.txtTitle, 0, wx.SHAPED)
70
 
 
71
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
72
 
                self.theSizer.Add(wx.StaticText(self, -1, "Command:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
73
 
                self.theSizer.Add(self.txtCommand, 0, wx.SHAPED)
74
 
 
75
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
76
 
                self.theSizer.Add(wx.StaticText(self, -1, "Arguments:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
77
 
                self.theSizer.Add(self.txtArguments, 0, wx.SHAPED)
78
 
 
79
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
80
 
                self.theSizer.Add(wx.StaticText(self, -1, "Directory:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
81
 
                self.theSizer.Add(self.txtDirectory, 0, wx.SHAPED)
82
 
 
83
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
84
 
                self.theSizer.Add(wx.StaticText(self, -1, "Run In Prompt:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
85
 
                self.theSizer.Add(self.chkRunInPrompt, 0, wx.SHAPED)
86
 
 
87
 
                self.btnClose = wx.Button(self, 101, "&Close")
88
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
89
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
90
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
91
 
                self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
92
 
                self.theSizer.Add(self.btnClose, 0, wx.SHAPED | wx.ALIGN_CENTER)
93
 
                self.theSizer.Add(self.btnSave, 0, wx.SHAPED | wx.ALIGN_RIGHT)
94
 
                self.txtTitle.SetFocus()
95
 
                self.btnSave.SetDefault()
96
 
 
97
 
                self.SetAutoLayout(True)
98
 
                self.SetSizer(self.theSizer)
99
 
 
100
 
                self.exitstatus = 0
101
 
 
102
 
                self.Bind(wx.EVT_BUTTON,  self.OnbtnSave, id=self.ID_SAVE)
103
 
                self.Bind(wx.EVT_BUTTON,  self.OnbtnClose, id=101)
104
 
 
105
 
        def GetExitStatus(self):
106
 
                return self.exitstatus
107
 
 
108
 
        def GetShellTuple(self):
109
 
                return self.txtTitle.GetValue(), self.txtCommand.GetValue(), self.txtArguments.GetValue(), self.txtDirectory.GetValue(), int(self.chkRunInPrompt.GetValue())
110
 
 
111
 
        def OnbtnClose(self, event):
112
 
                self.exitstatus = 0
113
 
                self.Close(1)
114
 
 
115
 
        def OnbtnSave(self, event):
116
 
                self.exitstatus = 1
117
 
                self.Close(1)
118
 
 
119
 
        def SetShellTuple(self, title, command, args, dir, inprompt):
120
 
                self.txtTitle.SetValue(title)
121
 
                self.txtCommand.SetValue(command)
122
 
                self.txtArguments.SetValue(args)
123
 
                self.txtDirectory.SetValue(dir)
124
 
                self.chkRunInPrompt.SetValue(inprompt)
 
45
    def __init__(self, parent, title):
 
46
        wx.Dialog.__init__(self, parent, -1, title, wx.Point(50, 50), wx.Size(375, 250), wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.THICK_FRAME | wx.RESIZE_BORDER)
 
47
 
 
48
        self.ID_SAVE = 1005
 
49
 
 
50
        self.theSizer = wx.FlexGridSizer(6, 3, 5, 10)
 
51
 
 
52
        self.btnSave = wx.Button(self, self.ID_SAVE, "&Save")
 
53
 
 
54
        self.parent = parent
 
55
 
 
56
        self.txtTitle = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
 
57
        self.txtCommand = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
 
58
        self.txtArguments = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
 
59
        self.txtDirectory = wx.TextCtrl(self, -1, "",  wx.Point(15, 325), wx.Size(250, -1))
 
60
        self.chkRunInPrompt = wx.CheckBox(self, -1, "")
 
61
        self.chkRunInPrompt.SetValue(True)
 
62
 
 
63
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
64
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
65
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
66
 
 
67
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
68
        self.theSizer.Add(wx.StaticText(self, -1, "Title:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
69
        self.theSizer.Add(self.txtTitle, 0, wx.SHAPED)
 
70
 
 
71
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
72
        self.theSizer.Add(wx.StaticText(self, -1, "Command:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
73
        self.theSizer.Add(self.txtCommand, 0, wx.SHAPED)
 
74
 
 
75
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
76
        self.theSizer.Add(wx.StaticText(self, -1, "Arguments:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
77
        self.theSizer.Add(self.txtArguments, 0, wx.SHAPED)
 
78
 
 
79
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
80
        self.theSizer.Add(wx.StaticText(self, -1, "Directory:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
81
        self.theSizer.Add(self.txtDirectory, 0, wx.SHAPED)
 
82
 
 
83
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
84
        self.theSizer.Add(wx.StaticText(self, -1, "Run In Prompt:"), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
85
        self.theSizer.Add(self.chkRunInPrompt, 0, wx.SHAPED)
 
86
 
 
87
        self.btnClose = wx.Button(self, 101, "&Close")
 
88
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
89
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
90
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
91
        self.theSizer.Add(wx.StaticText(self, -1, " "), 0, wx.ALIGN_CENTER | wx.SHAPED)
 
92
        self.theSizer.Add(self.btnClose, 0, wx.SHAPED | wx.ALIGN_CENTER)
 
93
        self.theSizer.Add(self.btnSave, 0, wx.SHAPED | wx.ALIGN_RIGHT)
 
94
        self.txtTitle.SetFocus()
 
95
        self.btnSave.SetDefault()
 
96
 
 
97
        self.SetAutoLayout(True)
 
98
        self.SetSizer(self.theSizer)
 
99
 
 
100
        self.exitstatus = 0
 
101
 
 
102
        self.Bind(wx.EVT_BUTTON,  self.OnbtnSave, id=self.ID_SAVE)
 
103
        self.Bind(wx.EVT_BUTTON,  self.OnbtnClose, id=101)
 
104
 
 
105
    def GetExitStatus(self):
 
106
        return self.exitstatus
 
107
 
 
108
    def GetShellTuple(self):
 
109
        return self.txtTitle.GetValue(), self.txtCommand.GetValue(), self.txtArguments.GetValue(), self.txtDirectory.GetValue(), int(self.chkRunInPrompt.GetValue())
 
110
 
 
111
    def OnbtnClose(self, event):
 
112
        self.exitstatus = 0
 
113
        self.Close(1)
 
114
 
 
115
    def OnbtnSave(self, event):
 
116
        self.exitstatus = 1
 
117
        self.Close(1)
 
118
 
 
119
    def SetShellTuple(self, title, command, args, dir, inprompt):
 
120
        self.txtTitle.SetValue(title)
 
121
        self.txtCommand.SetValue(command)
 
122
        self.txtArguments.SetValue(args)
 
123
        self.txtDirectory.SetValue(dir)
 
124
        self.chkRunInPrompt.SetValue(inprompt)
125
125
 
126
126
class drScriptMenu(drMenu):
127
 
        def __init__(self, parent):
128
 
                drMenu.__init__(self, parent)
129
 
 
130
 
                self.dynamicscriptext = ""
131
 
 
132
 
                self.ID_ADD_SCRIPT = 3100
133
 
                self.ID_NEW_SCRIPT = 3101
134
 
                self.ID_EXISTING_SCRIPT = 3102
135
 
                self.ID_SHELL_COMMAND = 3103
136
 
 
137
 
                self.ID_EDIT_SCRIPT = 3003
138
 
                self.ID_REMOVE_SCRIPT = 3005
139
 
                self.ID_DYNAMIC_SCRIPT = 3006
140
 
 
141
 
                self.ID_EXAMPLE_SCRIPTS = 3010
142
 
 
143
 
                self.ID_SCRIPT_BASE = parent.ID_SCRIPT_BASE
144
 
 
145
 
                self.ID_SCRIPT_MENU = 3500
146
 
 
147
 
                self.parent = parent
148
 
                self.userpreferencesdirectory = parent.userpreferencesdirectory
149
 
                self.programdirectory = parent.programdirectory
150
 
 
151
 
                self.scriptcount = 0
152
 
                self.scripts = []
153
 
                self.titles = []
154
 
 
155
 
                self.setupMenu()
156
 
 
157
 
                self.ExampleScriptCount = 0
158
 
 
159
 
                self.loadscripts()
160
 
 
161
 
        def getdrscriptmenulabel(self, label):
162
 
                shortcuttext = ''
163
 
 
164
 
                if len(self.parent.DrScriptShortcuts) == 0:
165
 
                        return label
166
 
 
167
 
                if label in self.titles:
168
 
                        i = self.titles.index(label)
169
 
                        shortcuttext = drShortcuts.GetShortcutLabel(self.parent.DrScriptShortcuts[i])
170
 
 
171
 
                if len(shortcuttext) > 1:
172
 
                        return label + '\t' + shortcuttext
173
 
 
174
 
                return label
175
 
 
176
 
        def loadscripts(self, SyncShortcuts = 1):
177
 
                self.scriptcount = 0
178
 
                if self.parent.prefs.drscriptloadexamples:
179
 
                        self.examplemenu = wx.Menu()
180
 
                        self.loadscriptsfromfile(self.programdirectory + "/examples/DrScript/drscript.dat", self.examplemenu, 1)
181
 
                        self.AppendMenu(self.ID_EXAMPLE_SCRIPTS, "Examples", self.examplemenu)
182
 
                        self.AppendSeparator()
183
 
 
184
 
                self.ExampleScriptCount = self.scriptcount
185
 
 
186
 
                self.loadscriptsfromfile(self.userpreferencesdirectory + "/drscript.dat", self)
187
 
 
188
 
                #Either there is no shortcuts file, or it is out of sync.
189
 
                if SyncShortcuts:
190
 
                        l = len(self.titles)
191
 
                        if not (l == len(self.parent.DrScriptShortcuts)):
192
 
                                self.parent.DrScriptShortcuts = []
193
 
                                self.parent.DrScriptShortcutNames = []
194
 
 
195
 
                                x = 0
196
 
                                while (x < l):
197
 
                                        self.parent.DrScriptShortcuts.append(' #')
198
 
                                        self.parent.DrScriptShortcutNames.append(self.titles[x])
199
 
                                        x = x + 1
200
 
 
201
 
        def loadscriptsfromfile(self, scriptfile, target, appendprogpath = 0):
202
 
                if (os.path.exists(scriptfile)):
203
 
                        folders = [target]
204
 
                        folderindex = 0
205
 
                        menuTitles = []
206
 
                        menuTitleindex = -1
207
 
                        lastCount = 0
208
 
                        try:
209
 
                                #Read from the file
210
 
                                f = open(scriptfile, 'rb')
211
 
                                #Initialize
212
 
                                line = f.readline()
213
 
                                while (len(line) > 0):
214
 
                                        c = line.count('\t')
215
 
                                        line = line[c:].rstrip()
216
 
 
217
 
                                        while (lastCount > c):
218
 
                                                folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
219
 
                                                folderindex = folderindex - 1
220
 
                                                menuTitleindex = menuTitleindex - 1
221
 
                                                lastCount = lastCount - 1
222
 
 
223
 
                                        if (line[0] == '>'):
224
 
                                                folders.append(wx.Menu())
225
 
                                                menuTitles.append(line[1:])
226
 
                                                folderindex = folderindex + 1
227
 
                                                menuTitleindex = menuTitleindex + 1
228
 
                                                c = c + 1
229
 
                                        else:
230
 
                                                line_path = ExtractPreferenceFromText(line, "path")
231
 
                                                if appendprogpath:
232
 
                                                        line_path = self.programdirectory + "/" + line_path.replace('\\', '/')
233
 
                                                line_title = ExtractPreferenceFromText(line, "title")
234
 
                                                if (os.path.exists(line_path)):
235
 
                                                        self.titles.append(line_title)
236
 
                                                        folders[folderindex].Append((self.ID_SCRIPT_BASE + self.scriptcount), self.getdrscriptmenulabel(line_title))
237
 
                                                        self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
238
 
                                                        self.scripts.append(line_path)
239
 
                                                        self.scriptcount = self.scriptcount + 1
240
 
                                        lastCount = c
241
 
                                        line = f.readline()
242
 
                                f.close()
243
 
                                #Add any menus not yet added:
244
 
                                c = 0
245
 
                                while (lastCount > c):
246
 
                                        folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
247
 
                                        folderindex = folderindex - 1
248
 
                                        menuTitleindex = menuTitleindex - 1
249
 
                                        lastCount = lastCount - 1
250
 
                        except:
251
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Your drscript file is a tad messed up.\n"), "Error")
252
 
 
253
 
        def OnAddExistingScript(self, event):
254
 
                scriptfile = self.userpreferencesdirectory + "/drscript.dat"
255
 
                dlg = drFileDialog.FileDialog(self.parent, "Select Script File", self.parent.prefs.wildcard)
256
 
                if (len(self.parent.prefs.drscriptdefaultdirectory) > 0):
257
 
                        try:
258
 
                                dlg.SetDirectory(self.parent.prefs.drscriptdefaultdirectory)
259
 
                        except:
260
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.prefs.drscriptdefaultdirectory), "DrPython Error")
261
 
                if (dlg.ShowModal() == wx.ID_OK):
262
 
                        filen = dlg.GetPath().replace("\\", "/")
263
 
                        d = wx.TextEntryDialog(self.parent, 'Enter Script Title:', 'Add DrScript: Title', '')
264
 
                        title = ""
265
 
                        if (d.ShowModal() == wx.ID_OK):
266
 
                                title = d.GetValue()
267
 
                        d.Destroy()
268
 
                        if (len(title) < 1):
269
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
270
 
                                return
271
 
                        self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
272
 
                        self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
273
 
                        self.scripts.append(filen)
274
 
                        self.titles.append(title)
275
 
 
276
 
                        x = len(self.parent.DrScriptShortcuts)
277
 
                        self.parent.DrScriptShortcuts.append('')
278
 
                        self.parent.DrScriptShortcutNames.append(title)
279
 
 
280
 
                        self.scriptcount = self.scriptcount + 1
281
 
                        try:
282
 
                                f = open(scriptfile, 'a')
283
 
                                f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
284
 
                                f.close()
285
 
 
286
 
                                #Update the shortcuts Too.
287
 
                                shortcutsfile = self.userpreferencesdirectory + "/drscript.shortcuts.dat"
288
 
                                drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
289
 
                        except:
290
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
291
 
                dlg.Destroy()
292
 
 
293
 
        def OnAddNewScript(self, event):
294
 
                scriptfile = self.userpreferencesdirectory + "/drscript.dat"
295
 
                dlg = drFileDialog.FileDialog(self.parent, "Save New Script File As", self.parent.prefs.wildcard, IsASaveDialog=True)
296
 
                if (len(self.parent.prefs.drscriptdefaultdirectory) > 0):
297
 
                        try:
298
 
                                dlg.SetDirectory(self.parent.prefs.drscriptdefaultdirectory)
299
 
                        except:
300
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.prefs.drscriptdefaultdirectory), "DrPython Error")
301
 
                if (dlg.ShowModal() == wx.ID_OK):
302
 
                        filen = dlg.GetPath().replace("\\", "/")
303
 
                        d = wx.TextEntryDialog(self.parent, "Enter Script Title:", "Add DrScript: Title", "")
304
 
                        title = ""
305
 
                        if (d.ShowModal() == wx.ID_OK):
306
 
                                title = d.GetValue()
307
 
                        d.Destroy()
308
 
                        if (len(title) < 1):
309
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
310
 
                                return
311
 
 
312
 
                        cfile = file(filen, 'wb')
313
 
                        cfile.write("#drscript\n\n")
314
 
                        cfile.close()
315
 
 
316
 
                        self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
317
 
                        self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
318
 
                        self.scripts.append(filen)
319
 
                        self.titles.append(title)
320
 
 
321
 
                        x = len(self.parent.DrScriptShortcuts)
322
 
                        self.parent.DrScriptShortcuts.append('')
323
 
                        self.parent.DrScriptShortcutNames.append(title)
324
 
 
325
 
                        self.scriptcount = self.scriptcount + 1
326
 
                        try:
327
 
                                f = open(scriptfile, 'a')
328
 
                                f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
329
 
                                f.close()
330
 
 
331
 
                                shortcutsfile = self.userpreferencesdirectory + "/drscript.shortcuts.dat"
332
 
                                drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
333
 
                        except:
334
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
335
 
 
336
 
                        old = self.parent.txtDocument.filename
337
 
                        filename = filen
338
 
                        if (len(old) > 0) or (self.parent.txtDocument.GetModify()):
339
 
                                self.parent.OpenFile(filename, True)
340
 
                        else:
341
 
                                self.parent.OpenFile(filename, False)
342
 
                dlg.Destroy()
343
 
 
344
 
        def OnAddShellCommand(self, event):
345
 
                scriptfile = self.userpreferencesdirectory + "/drscript.dat"
346
 
                dlg = drFileDialog.FileDialog(self.parent, "Save New Shell Command As", self.parent.prefs.wildcard, IsASaveDialog=True)
347
 
                if (len(self.parent.prefs.drscriptdefaultdirectory) > 0):
348
 
                        try:
349
 
                                dlg.SetDirectory(self.parent.prefs.drscriptdefaultdirectory)
350
 
                        except:
351
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.prefs.drscriptdefaultdirectory), "DrPython Error")
352
 
                if (dlg.ShowModal() == wx.ID_OK):
353
 
                        filen = dlg.GetPath().replace("\\", "/")
354
 
                        d = drNewShellDialog(self.parent, "New Shell Command")
355
 
                        if self.parent.prefs.platform_is_windows:
356
 
                                d.SetSize(wx.Size(425, 350))
357
 
                        d.ShowModal()
358
 
                        if d.GetExitStatus():
359
 
                                title, cmd, args, dir, inprompt = d.GetShellTuple()
360
 
                                d.Destroy()
361
 
                        else:
362
 
                                d.Destroy()
363
 
                                return
364
 
                        if (len(title) < 1):
365
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Bad shell title.\nDrPython will politely ignore your request to add this shell command."), "Error")
366
 
                                return
367
 
 
368
 
                        if args.find("<Current File>") > -1:
369
 
                                args = args.replace("<Current File>", self.parent.txtDocument.filename)
370
 
                        if args.find("<Current Directory>") > -1:
371
 
                                args = args.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
372
 
                        if dir.find("<Current Directory>") > -1:
373
 
                                dir = dir.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
374
 
 
375
 
                        cfile = file(filen, 'wb')
376
 
                        scripttext = "#drscript shell command\n\n"
377
 
                        if len(dir) > 0:
378
 
                                scripttext = scripttext + 'import os\n\nos.chdir("' + dir + '")\n\n'
379
 
 
380
 
                        if inprompt:
381
 
                                scripttext = scripttext + 'DrFrame.Execute("' + cmd + ' ' + args + '", "Running Shell Command")\n\n'
382
 
                        else:
383
 
                                scripttext = scripttext + 'wx.Shell("' + cmd + ' ' + args + '")\n\n'
384
 
                        cfile.write(scripttext)
385
 
                        cfile.close()
386
 
 
387
 
                        self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
388
 
                        self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
389
 
                        self.scripts.append(filen)
390
 
                        self.titles.append(title)
391
 
 
392
 
                        x = len(self.parent.DrScriptShortcuts)
393
 
                        self.parent.DrScriptShortcuts.append('')
394
 
                        self.parent.DrScriptShortcutNames.append(title)
395
 
 
396
 
                        self.scriptcount = self.scriptcount + 1
397
 
                        try:
398
 
                                f = open(scriptfile, 'a')
399
 
                                f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
400
 
                                f.close()
401
 
 
402
 
                                shortcutsfile = self.userpreferencesdirectory + "/drscript.shortcuts.dat"
403
 
                                drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
404
 
                        except:
405
 
                                drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
406
 
                dlg.Destroy()
407
 
 
408
 
        def OnDynamicScript(self, event):
409
 
                from drDynamicDrScriptDialog import drDynamicDrScriptDialog
410
 
                d = drDynamicDrScriptDialog(self.parent, self.dynamicscriptext)
411
 
                d.ShowModal()
412
 
                self.dynamicscriptext = d.GetText()
413
 
                d.Destroy()
414
 
 
415
 
        def OnEditScript(self, event):
416
 
                if (len(self.scripts) > 0):
417
 
                        def tuplify(x, y):
418
 
                                return (x, y)
419
 
 
420
 
                        def detuplify(x):
421
 
                                return x[0]
422
 
 
423
 
                        array = map(tuplify, self.titles, self.scripts)
424
 
                        array.sort()
425
 
 
426
 
                        titles = map(detuplify, array)
427
 
                        d = wx.SingleChoiceDialog(self.parent, "Select the Script to Edit:", "Edit Script", titles, wx.OK|wx.CANCEL)
428
 
                        d.SetSize(wx.Size(250, 250))
429
 
                        answer = d.ShowModal()
430
 
                        d.Destroy()
431
 
                        if (answer == wx.ID_OK):
432
 
                                i = d.GetSelection()
433
 
                                old = self.parent.txtDocument.filename
434
 
                                filename = array[i][1].replace("\\", "/")
435
 
                                alreadyopen = map(lambda x: x.filename.replace("\\", "/"), self.parent.txtDocumentArray)
436
 
                                if filename in alreadyopen:
437
 
                                        self.parent.setDocumentTo(alreadyopen.index(filename))
438
 
                                        return
439
 
                                if (len(old) > 0) or (self.parent.txtDocument.GetModify()):
440
 
                                        self.parent.OpenFile(filename, True, False)
441
 
                                else:
442
 
                                        self.parent.OpenFile(filename, False, False)
443
 
                else:
444
 
                        drScrolledMessageDialog.ShowMessage(self.parent, ("There aren't any scripts to edit.\nTry \"New Script\" or \"Add Script\" Instead."), "Error")
445
 
 
446
 
        def OnScript(self, event):
447
 
                scriptindex = event.GetId() - self.ID_SCRIPT_BASE
448
 
                f = open(self.scripts[scriptindex], 'r')
449
 
                scripttext = f.read()
450
 
                f.close()
451
 
 
452
 
                if (scripttext.find("DrFilename") > -1):
453
 
                        scripttext = scripttext.replace("DrFilename", "self.parent.txtDocument.filename")
454
 
                if (scripttext.find("DrScript") > -1):
455
 
                        scripttext = scripttext.replace("DrScript", "self.parent.DrScript")
456
 
                if (scripttext.find("DrDocument") > -1):
457
 
                        scripttext = scripttext.replace("DrDocument", "self.parent.txtDocument")
458
 
                if (scripttext.find("DrPrompt") > -1):
459
 
                        scripttext = scripttext.replace("DrPrompt", "self.parent.txtPrompt")
460
 
                if (scripttext.find("DrFrame") > -1):
461
 
                        scripttext = scripttext.replace("DrFrame", "self.parent")
462
 
 
463
 
                try:
464
 
                        code = compile((scripttext + '\n'), self.scripts[scriptindex], 'exec')
465
 
                except:
466
 
                        drScrolledMessageDialog.ShowMessage(self.parent, ("Error compiling script."), "Error", wx.DefaultPosition, wx.Size(550,300))
467
 
                        return
468
 
 
469
 
                try:
470
 
                        exec(code)
471
 
                except:
472
 
                        drScrolledMessageDialog.ShowMessage(self.parent, ("Error running script."), "Error", wx.DefaultPosition, wx.Size(550,300))
473
 
                        return
474
 
 
475
 
        def setupMenu(self):
476
 
                addscriptmenu = wx.Menu()
477
 
                addscriptmenu.Append(self.ID_NEW_SCRIPT, "&New Script...")
478
 
                addscriptmenu.Append(self.ID_EXISTING_SCRIPT, "&Existing Script...")
479
 
                addscriptmenu.Append(self.ID_SHELL_COMMAND, "&Shell Command...")
480
 
 
481
 
                self.AppendMenu(self.ID_ADD_SCRIPT, "&Add Script", addscriptmenu)
482
 
                self.Append(self.ID_EDIT_SCRIPT, "&Edit Script...")
483
 
                self.Append(self.ID_DYNAMIC_SCRIPT, "&Dynamic DrScript...")
484
 
                self.AppendSeparator()
485
 
 
486
 
                self.parent.Bind(wx.EVT_MENU, self.OnAddExistingScript, id=self.ID_EXISTING_SCRIPT)
487
 
                self.parent.Bind(wx.EVT_MENU, self.OnAddNewScript, id=self.ID_NEW_SCRIPT)
488
 
                self.parent.Bind(wx.EVT_MENU, self.OnAddShellCommand, id=self.ID_SHELL_COMMAND)
489
 
                self.parent.Bind(wx.EVT_MENU, self.OnEditScript, id=self.ID_EDIT_SCRIPT)
490
 
                self.parent.Bind(wx.EVT_MENU, self.OnDynamicScript, id=self.ID_DYNAMIC_SCRIPT)
491
 
 
492
 
        def reloadscripts(self, oldlength = -1, SyncShortcuts = 1):
493
 
                if oldlength < 0:
494
 
                        oldlength = len(self.scripts)
495
 
                mnuitems = self.GetMenuItems()
496
 
                num = len(mnuitems)
497
 
                x = 0
498
 
                while (x < num):
499
 
                        self.Remove(mnuitems[x].GetId())
500
 
                        #mnuitems[x].Destroy()
501
 
                        x = x + 1
502
 
                self.scripts = []
503
 
                self.titles = []
504
 
                self.setupMenu()
505
 
                self.loadscripts(SyncShortcuts)
 
127
    def __init__(self, parent):
 
128
        drMenu.__init__(self, parent)
 
129
 
 
130
        self.dynamicscriptext = ""
 
131
 
 
132
        self.ID_ADD_SCRIPT = 3100
 
133
        self.ID_NEW_SCRIPT = 3101
 
134
        self.ID_EXISTING_SCRIPT = 3102
 
135
        self.ID_SHELL_COMMAND = 3103
 
136
 
 
137
        self.ID_EDIT_SCRIPT = 3003
 
138
        self.ID_REMOVE_SCRIPT = 3005
 
139
        self.ID_DYNAMIC_SCRIPT = 3006
 
140
 
 
141
        self.ID_EXAMPLE_SCRIPTS = 3010
 
142
 
 
143
        self.ID_SCRIPT_BASE = parent.ID_SCRIPT_BASE
 
144
 
 
145
        self.ID_SCRIPT_MENU = 3500
 
146
 
 
147
        self.parent = parent
 
148
        self.preferencesdirectory = parent.preferencesdirectory
 
149
        self.programdirectory = parent.programdirectory
 
150
        self.shortcutsdirectory = parent.shortcutsdirectory
 
151
 
 
152
        self.scriptcount = 0
 
153
        self.scripts = []
 
154
        self.titles = []
 
155
 
 
156
        self.setupMenu()
 
157
 
 
158
        self.ExampleScriptCount = 0
 
159
 
 
160
        self.loadscripts()
 
161
 
 
162
    def getdrscriptmenulabel(self, label):
 
163
        shortcuttext = ''
 
164
 
 
165
        if not self.parent.DrScriptShortcuts:
 
166
            return label
 
167
 
 
168
        if label in self.titles:
 
169
            i = self.titles.index(label)
 
170
            self.parent.DrScriptShortcuts[i]
 
171
            shortcuttext = drShortcuts.GetShortcutLabel(self.parent.DrScriptShortcuts[i])
 
172
 
 
173
        if len(shortcuttext) > 1:
 
174
            if shortcuttext == "Tab":
 
175
                shortcuttext += " "
 
176
            return label + '\t' + shortcuttext
 
177
 
 
178
        return label
 
179
 
 
180
    def reloadscripts(self, oldlength = -1, SyncShortcuts = 1):
 
181
        if oldlength < 0:
 
182
            oldlength = len(self.scripts)
 
183
        mnuitems = self.GetMenuItems()
 
184
        num = len(mnuitems)
 
185
        x = 0
 
186
        while x < num:
 
187
            self.Remove(mnuitems[x].GetId())
 
188
            #mnuitems[x].Destroy()
 
189
            x = x + 1
 
190
        self.scripts = []
 
191
        self.titles = []
 
192
        self.setupMenu()
 
193
        self.loadscripts(SyncShortcuts)
 
194
        self.parent.DrScriptShortcutNames = []
 
195
        if self.titles:
 
196
            for l in self.titles:
 
197
                self.parent.DrScriptShortcutNames.append(l)
 
198
 
 
199
    def loadscripts(self, SyncShortcuts = 1):
 
200
        self.scriptcount = 0
 
201
        if self.parent.prefs.drscriptloadexamples:
 
202
            self.examplemenu = wx.Menu()
 
203
            self.loadscriptsfromfile(self.programdirectory + "/examples/DrScript/drscript.dat", self.examplemenu, 1)
 
204
            self.AppendMenu(self.ID_EXAMPLE_SCRIPTS, "Examples", self.examplemenu)
 
205
            self.AppendSeparator()
 
206
 
 
207
        self.ExampleScriptCount = self.scriptcount
 
208
 
 
209
        self.loadscriptsfromfile(self.preferencesdirectory + "/drscript.dat", self)
 
210
 
 
211
        #Either there is no shortcuts file, or it is out of sync.
 
212
        if SyncShortcuts:
 
213
            l = len(self.titles)
 
214
            if l != len(self.parent.DrScriptShortcuts):
 
215
                self.parent.DrScriptShortcuts = []
 
216
                self.parent.DrScriptShortcutNames = []
 
217
 
 
218
                x = 0
 
219
                while x < l:
 
220
                    self.parent.DrScriptShortcuts.append(' #')
 
221
                    self.parent.DrScriptShortcutNames.append(self.titles[x])
 
222
                    x = x + 1
 
223
 
 
224
    def loadscriptsfromfile(self, scriptfile, target, appendprogpath = 0):
 
225
        if os.path.exists(scriptfile):
 
226
            folders = [target]
 
227
            folderindex = 0
 
228
            menuTitles = []
 
229
            menuTitleindex = -1
 
230
            lastCount = 0
 
231
            try:
 
232
                #Read from the file
 
233
                f = open(scriptfile, 'rb')
 
234
                #Initialize
 
235
                line = f.readline()
 
236
                while len(line) > 0:
 
237
                    c = line.count('\t')
 
238
                    line = line[c:].rstrip()
 
239
 
 
240
                    while lastCount > c:
 
241
                        folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
 
242
                        folderindex = folderindex - 1
 
243
                        menuTitleindex = menuTitleindex - 1
 
244
                        lastCount = lastCount - 1
 
245
 
 
246
                    if line[0] == '>':
 
247
                        folders.append(wx.Menu())
 
248
                        menuTitles.append(line[1:])
 
249
                        folderindex = folderindex + 1
 
250
                        menuTitleindex = menuTitleindex + 1
 
251
                        c = c + 1
 
252
                    else:
 
253
                        line_path = ExtractPreferenceFromText(line, "path")
 
254
                        if appendprogpath:
 
255
                            line_path = self.programdirectory + "/" + line_path.replace('\\', '/')
 
256
                        else:
 
257
                            if not os.path.isabs(line_path):
 
258
                                line_path = os.path.join(self.parent.drscriptsdirectory, line_path)
 
259
                        line_title = ExtractPreferenceFromText(line, "title")
 
260
                        if os.path.exists(line_path):
 
261
                            self.titles.append(line_title)
 
262
                            folders[folderindex].Append((self.ID_SCRIPT_BASE + self.scriptcount), self.getdrscriptmenulabel(line_title))
 
263
                            self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
 
264
                            self.scripts.append(line_path)
 
265
                            self.scriptcount = self.scriptcount + 1
 
266
                    lastCount = c
 
267
                    line = f.readline()
 
268
                f.close()
 
269
                #Add any menus not yet added:
 
270
                c = 0
 
271
                while lastCount > c:
 
272
                    folders[(folderindex - 1)].AppendMenu(self.ID_SCRIPT_MENU, menuTitles.pop(), folders.pop())
 
273
                    folderindex = folderindex - 1
 
274
                    menuTitleindex = menuTitleindex - 1
 
275
                    lastCount = lastCount - 1
 
276
            except:
 
277
                drScrolledMessageDialog.ShowMessage(self.parent, ("Your drscript file is a tad messed up.\n"), "Error")
 
278
 
 
279
    def OnAddExistingScript(self, event):
 
280
        scriptfile = self.preferencesdirectory + "/drscript.dat"
 
281
        dlg = drFileDialog.FileDialog(self.parent, "Select Script File", self.parent.prefs.wildcard)
 
282
        if self.parent.drscriptsdirectory:
 
283
            try:
 
284
                dlg.SetDirectory(self.parent.drscriptsdirectory)
 
285
            except:
 
286
                drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " +
 
287
                    self.parent.drscriptsdirectory), "DrPython Error")
 
288
        if dlg.ShowModal() == wx.ID_OK:
 
289
            filen = dlg.GetPath().replace("\\", "/")
 
290
            p = os.path.basename(filen) #patch by AB (suggests filename as title), 06.03.2007, thanks
 
291
            s = os.path.splitext(p)
 
292
            d = wx.TextEntryDialog(self.parent, 'Enter Script Title:', 'Add DrScript: Title', s[0]) #end patch by AB
 
293
            title = ""
 
294
            if d.ShowModal() == wx.ID_OK:
 
295
                title = d.GetValue()
 
296
            d.Destroy()
 
297
            if not title:
 
298
                drScrolledMessageDialog.ShowMessage(self.parent,
 
299
                    ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
 
300
                return
 
301
            self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
 
302
            self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
 
303
 
 
304
            #if directory is relativ, store only filename without path
 
305
            pth = self.parent.drscriptsdirectory.lower()
 
306
            if filen.lower().startswith (pth):
 
307
                filen = filen[len(pth):]
 
308
                if filen[0] == '/':
 
309
                    filen = filen[1:]
 
310
 
 
311
            self.scripts.append(filen)
 
312
            self.titles.append(title)
 
313
 
 
314
            self.parent.DrScriptShortcuts.append('')
 
315
            self.parent.DrScriptShortcutNames.append(title)
 
316
 
 
317
            self.scriptcount += 1
 
318
            try:
 
319
                f = open(scriptfile, 'a')
 
320
                f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
 
321
                f.close()
 
322
 
 
323
                #Update the shortcuts Too.
 
324
                shortcutsfile = self.shortcutsdirectory + "/drscript.shortcuts.dat"
 
325
                drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts,
 
326
                    self.parent.DrScriptShortcutNames, "", False)
 
327
            except:
 
328
                drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
 
329
        dlg.Destroy()
 
330
 
 
331
    def OnAddNewScript(self, event):
 
332
        scriptfile = self.preferencesdirectory + "/drscript.dat"
 
333
        dlg = drFileDialog.FileDialog(self.parent, "Save New Script File As", self.parent.prefs.wildcard, IsASaveDialog=True)
 
334
        if self.parent.drscriptsdirectory:
 
335
            try:
 
336
                dlg.SetDirectory(self.parent.drscriptsdirectory)
 
337
            except:
 
338
                drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " + self.parent.drscriptsdirectory), "DrPython Error")
 
339
        if dlg.ShowModal() == wx.ID_OK:
 
340
            filename = dlg.GetPath().replace("\\", "/")
 
341
            if not filename.endswith('.py'):
 
342
                filename += '.py'
 
343
            s = os.path.splitext(filename)[0]
 
344
            s = os.path.basename(s)
 
345
            d = wx.TextEntryDialog(self.parent, "Enter Script Title:", "Add DrScript: Title", s)
 
346
            title = ""
 
347
            if d.ShowModal() == wx.ID_OK:
 
348
                title = d.GetValue()
 
349
            d.Destroy()
 
350
            if not title:
 
351
                drScrolledMessageDialog.ShowMessage(self.parent, ("Bad script title.\nDrPython will politely ignore your request to add this script."), "Error")
 
352
                return
 
353
 
 
354
            cfile = file(filename, 'wb')
 
355
            cfile.write("#drscript\n\n")
 
356
            cfile.close()
 
357
 
 
358
            self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
 
359
            self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
 
360
 
 
361
            #if directory is relativ, store only filename without path
 
362
            pth = self.parent.drscriptsdirectory.lower()
 
363
            fname = filename
 
364
            if filename.lower().startswith (pth):
 
365
                fname = filename[len(pth):]
 
366
                if fname[0] == '/':
 
367
                    fname = fname[1:]
 
368
            
 
369
            self.scripts.append(fname)
 
370
            self.titles.append(fname)
 
371
 
 
372
            self.parent.DrScriptShortcuts.append('')
 
373
            self.parent.DrScriptShortcutNames.append(title)
 
374
 
 
375
            self.scriptcount += 1
 
376
            try:
 
377
                f = open(scriptfile, 'a')
 
378
                f.write("<path>" + fname + "</path><title>" + title + "</title>\n")
 
379
                f.close()
 
380
 
 
381
                shortcutsfile = self.shortcutsdirectory + "/drscript.shortcuts.dat"
 
382
                drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
 
383
            except:
 
384
                drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
 
385
 
 
386
            old = self.parent.txtDocument.filename
 
387
            if (len(old) > 0) or (self.parent.txtDocument.GetModify()):
 
388
                self.parent.OpenFile(filename, True)
 
389
            else:
 
390
                self.parent.OpenFile(filename, False)
 
391
        dlg.Destroy()
 
392
 
 
393
    def OnAddShellCommand(self, event):
 
394
        scriptfile = self.preferencesdirectory + "/drscript.dat"
 
395
        dlg = drFileDialog.FileDialog(self.parent, "Save New Shell Command As", self.parent.prefs.wildcard, IsASaveDialog=True)
 
396
        if self.parent.drscriptsdirectory:
 
397
            try:
 
398
                dlg.SetDirectory(self.parent.drscriptsdirectory)
 
399
            except:
 
400
                drScrolledMessageDialog.ShowMessage(self.parent, ("Error Setting Default Directory To: " +
 
401
                    self.parent.drscriptsdirectory), "DrPython Error")
 
402
        if dlg.ShowModal() == wx.ID_OK:
 
403
            filen = dlg.GetPath().replace("\\", "/")
 
404
            d = drNewShellDialog(self.parent, "New Shell Command")
 
405
            if self.parent.PLATFORM_IS_WIN:
 
406
                d.SetSize(wx.Size(425, 350))
 
407
            d.ShowModal()
 
408
            if d.GetExitStatus():
 
409
                title, cmd, args, dir, inprompt = d.GetShellTuple()
 
410
                d.Destroy()
 
411
            else:
 
412
                d.Destroy()
 
413
                return
 
414
            if not title:
 
415
                drScrolledMessageDialog.ShowMessage(self.parent, ("Bad shell title.\nDrPython will politely ignore your request to add this shell command."), "Error")
 
416
                return
 
417
 
 
418
            if args.find("<Current File>") > -1:
 
419
                args = args.replace("<Current File>", self.parent.txtDocument.filename)
 
420
            if args.find("<Current Directory>") > -1:
 
421
                args = args.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
 
422
            if dir.find("<Current Directory>") > -1:
 
423
                dir = dir.replace("<Current Directory>", os.path.split(self.parent.txtDocument.filename)[0])
 
424
 
 
425
            cfile = file(filen, 'wb')
 
426
            scripttext = "#drscript shell command\n\n"
 
427
            if dir:
 
428
                scripttext = scripttext + 'import os\n\nos.chdir("' + dir + '")\n\n'
 
429
 
 
430
            if inprompt:
 
431
                scripttext = scripttext + 'DrFrame.Execute("' + cmd + ' ' + args + '", "Running Shell Command")\n\n'
 
432
            else:
 
433
                scripttext = scripttext + 'wx.Shell("' + cmd + ' ' + args + '")\n\n'
 
434
            cfile.write(scripttext)
 
435
            cfile.close()
 
436
 
 
437
            self.Append((self.ID_SCRIPT_BASE + self.scriptcount), title, title)
 
438
            self.parent.Bind(wx.EVT_MENU, self.OnScript, id=(self.ID_SCRIPT_BASE + self.scriptcount))
 
439
            self.scripts.append(filen)
 
440
            self.titles.append(title)
 
441
 
 
442
            self.parent.DrScriptShortcuts.append('')
 
443
            self.parent.DrScriptShortcutNames.append(title)
 
444
 
 
445
            self.scriptcount = self.scriptcount + 1
 
446
            try:
 
447
                f = open(scriptfile, 'a')
 
448
                f.write("<path>" + filen + "</path><title>" + title + "</title>\n")
 
449
                f.close()
 
450
 
 
451
                shortcutsfile = self.shortcutsdirectory + "/drscript.shortcuts.dat"
 
452
                drShortcutsFile.WriteShortcuts(shortcutsfile, self.parent.DrScriptShortcuts, self.parent.DrScriptShortcutNames, "", False)
 
453
            except:
 
454
                drScrolledMessageDialog.ShowMessage(self.parent, ("Problem saving script.\n"), "Error")
 
455
        dlg.Destroy()
 
456
 
 
457
    def OnDynamicScript(self, event):
 
458
        from drDynamicDrScriptDialog import drDynamicDrScriptDialog
 
459
        d = drDynamicDrScriptDialog(self.parent, self.dynamicscriptext)
 
460
        d.ShowModal()
 
461
        self.dynamicscriptext = d.GetText()
 
462
        d.Destroy()
 
463
 
 
464
    def OnEditScript(self, event):
 
465
        if self.scripts:
 
466
            def tuplify(x, y):
 
467
                return (x, y)
 
468
 
 
469
            def detuplify(x):
 
470
                return x[0]
 
471
 
 
472
            array = map(tuplify, self.titles, self.scripts)
 
473
            array.sort()
 
474
 
 
475
            titles = map(detuplify, array)
 
476
            d = wx.SingleChoiceDialog(self.parent, "Select the Script to Edit:", "Edit Script", titles, wx.CHOICEDLG_STYLE)
 
477
            #d.SetSize(wx.Size(250, 250))
 
478
            answer = d.ShowModal()
 
479
            d.Destroy()
 
480
            if answer == wx.ID_OK:
 
481
                i = d.GetSelection()
 
482
                filename = array[i][1].replace("\\", "/")
 
483
                self.parent.OpenOrSwitchToFile(filename, editRecentFiles=False)
 
484
        else:
 
485
            drScrolledMessageDialog.ShowMessage(self.parent, ("There aren't any scripts to edit.\nTry \"New Script\" or \"Add Script\" Instead."), "Error")
 
486
 
 
487
    def OnScript(self, event):
 
488
        scriptindex = event.GetId() - self.ID_SCRIPT_BASE
 
489
 
 
490
        scriptfname = self.scripts[scriptindex]
 
491
        #relativ path? => complete script filename
 
492
        if not os.path.isabs(scriptfname):
 
493
            scriptfname = os.path.join(self.parent.drscriptsdirectory, scriptfname)
 
494
 
 
495
        f = open(scriptfname, 'r')
 
496
        scripttext = f.read()
 
497
        f.close()
 
498
 
 
499
        if scripttext.find("DrFilename") > -1:
 
500
            scripttext = scripttext.replace("DrFilename", "self.parent.txtDocument.filename")
 
501
        if scripttext.find("DrScript") > -1:
 
502
            scripttext = scripttext.replace("DrScript", "self.parent.DrScript")
 
503
        if scripttext.find("DrDocument") > -1:
 
504
            scripttext = scripttext.replace("DrDocument", "self.parent.txtDocument")
 
505
        if scripttext.find("DrPrompt") > -1:
 
506
            scripttext = scripttext.replace("DrPrompt", "self.parent.txtPrompt")
 
507
        if scripttext.find("DrFrame") > -1:
 
508
            scripttext = scripttext.replace("DrFrame", "self.parent")
 
509
 
 
510
        try:
 
511
            code = compile((scripttext + '\n'), scriptfname, 'exec')
 
512
        except:
 
513
            drScrolledMessageDialog.ShowMessage(self.parent, ("Error compiling script."),
 
514
                "Error", wx.DefaultPosition, wx.Size(550,300))
 
515
            return
 
516
 
 
517
        try:
 
518
            exec(code)
 
519
        except:
 
520
            drScrolledMessageDialog.ShowMessage(self.parent, ("Error running script."),
 
521
                "Error", wx.DefaultPosition, wx.Size(550,300))
 
522
            return
 
523
 
 
524
    def setupMenu(self):
 
525
        addscriptmenu = wx.Menu()
 
526
        addscriptmenu.Append(self.ID_NEW_SCRIPT, "&New Script...")
 
527
        addscriptmenu.Append(self.ID_EXISTING_SCRIPT, "&Existing Script...")
 
528
        addscriptmenu.Append(self.ID_SHELL_COMMAND, "&Shell Command...")
 
529
 
 
530
        self.AppendMenu(self.ID_ADD_SCRIPT, "&Add Script", addscriptmenu)
 
531
        self.Append(self.ID_EDIT_SCRIPT, "&Edit Script...")
 
532
        self.Append(self.ID_DYNAMIC_SCRIPT, "&Dynamic DrScript...")
 
533
        self.AppendSeparator()
 
534
 
 
535
        self.parent.Bind(wx.EVT_MENU, self.OnAddExistingScript, id=self.ID_EXISTING_SCRIPT)
 
536
        self.parent.Bind(wx.EVT_MENU, self.OnAddNewScript, id=self.ID_NEW_SCRIPT)
 
537
        self.parent.Bind(wx.EVT_MENU, self.OnAddShellCommand, id=self.ID_SHELL_COMMAND)
 
538
        self.parent.Bind(wx.EVT_MENU, self.OnEditScript, id=self.ID_EDIT_SCRIPT)
 
539
        self.parent.Bind(wx.EVT_MENU, self.OnDynamicScript, id=self.ID_DYNAMIC_SCRIPT)
 
540