~ubuntu-branches/ubuntu/quantal/drpython/quantal

« back to all changes in this revision

Viewing changes to .pc/program_paths/drScriptMenu.py

  • Committer: Bazaar Package Importer
  • Author(s): William Vera
  • Date: 2010-04-01 14:21:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100401142109-lecjio9de0kt3lfk
Tags: 1:3.11.1-2
* New Maintainer (Closes: #575845). 
* Bump Standards-Version to 3.8.4.
* Switch to dpkg-source 3.0 (quilt) format

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-2007 Daniel Pozmanter
 
6
#
 
7
#   Distributed under the terms of the GPL (GNU Public License)
 
8
#
 
9
#    DrPython is free software; you can redistribute it and/or modify
 
10
#    it under the terms of the GNU General Public License as published by
 
11
#    the Free Software Foundation; either version 2 of the License, or
 
12
#    (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU General Public License
 
20
#    along with this program; if not, write to the Free Software
 
21
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
 
 
23
#drScript Menu
 
24
 
 
25
import os.path
 
26
import wx
 
27
import drFileDialog
 
28
import drScrolledMessageDialog
 
29
from drPrefsFile import ExtractPreferenceFromText
 
30
import drShortcutsFile
 
31
import drShortcuts
 
32
from drMenu import drMenu
 
33
 
 
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()
 
42
 
 
43
class drNewShellDialog(wx.Dialog):
 
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)
 
125
 
 
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.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