]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/tool/UICommon.py
Applied patch [ 1284335 ] doc update for wxString::operator[]
[wxWidgets.git] / wxPython / samples / ide / activegrid / tool / UICommon.py
1 #----------------------------------------------------------------------------
2 # Name: UICommon.py
3 # Purpose: Shared UI stuff
4 #
5 # Author: Matt Fryer
6 #
7 # Created: 3/10/05
8 # CVS-ID: $Id$
9 # Copyright: (c) 2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
12
13 import os
14 import os.path
15 import wx
16 import ProjectEditor
17 import activegrid.util as utillib
18 _ = wx.GetTranslation
19
20 def CreateDirectoryControl( parent, fileLabel, dirLabel, fileExtension, startingName="", startingDirectory=""):
21
22 nameControl = wx.TextCtrl(parent, -1, startingName, size=(-1,-1))
23 nameLabelText = wx.StaticText(parent, -1, fileLabel)
24 dirLabelText = wx.StaticText(parent, -1, dirLabel)
25 dirControl = wx.TextCtrl(parent, -1, startingDirectory, size=(-1,-1))
26 dirControl.SetToolTipString(startingDirectory)
27 button = wx.Button(parent, -1, _("Browse..."), size=(60,-1))
28
29 def OnFindDirClick(event):
30 name = ""
31 nameCtrlValue = nameControl.GetValue()
32 if nameCtrlValue:
33 root, ext = os.path.splitext( nameCtrlValue )
34 if ext == '.' + fileExtension:
35 name = nameCtrlValue
36 else:
37 name = _("%s.%s") % (nameCtrlValue, fileExtension)
38 path = wx.FileSelector(_("Choose a filename and directory"),
39 "",
40 "%s" % name,
41 wildcard=_("*.%s") % fileExtension ,
42 flags=wx.SAVE,
43 parent=parent)
44
45 if path:
46 dir, filename = os.path.split(path)
47 dirControl.SetValue(dir)
48 dirControl.SetToolTipString(dir)
49 nameControl.SetValue(filename)
50
51 parent.Bind(wx.EVT_BUTTON, OnFindDirClick, button)
52
53 def Validate(allowOverwriteOnPrompt=False):
54 if nameControl.GetValue() == "":
55 wx.MessageBox(_("Please provide a filename."), _("Provide a Filename"))
56 return False
57 if nameControl.GetValue().find(' ') != -1:
58 wx.MessageBox(_("Please provide a filename that does not contains spaces."), _("Spaces in Filename"))
59 return False
60 if not os.path.exists(dirControl.GetValue()):
61 wx.MessageBox(_("That directory does not exist. Please choose an existing directory."), _("Provide a Valid Directory"))
62 return False
63
64 filePath = os.path.join(dirControl.GetValue(), MakeNameEndInExtension(nameControl.GetValue(), "." + fileExtension))
65 if os.path.exists(filePath):
66 if allowOverwriteOnPrompt:
67 res = wx.MessageBox(_("That file already exists. Would you like to overwrite it."), "File Exists", style=wx.YES_NO|wx.NO_DEFAULT)
68 return (res == wx.YES)
69 else:
70 wx.MessageBox(_("That file already exists. Please choose a different name."), "File Exists")
71 return False
72 return True
73 HALF_SPACE = 5
74 flexGridSizer = wx.FlexGridSizer(cols = 3, vgap = HALF_SPACE, hgap = HALF_SPACE)
75 flexGridSizer.AddGrowableCol(1,1)
76 flexGridSizer.Add(nameLabelText, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.TOP|wx.RIGHT, HALF_SPACE)
77 flexGridSizer.Add(nameControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
78 flexGridSizer.Add(button, flag=wx.ALIGN_RIGHT|wx.LEFT, border=HALF_SPACE)
79
80 flexGridSizer.Add(dirLabelText, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.TOP|wx.RIGHT, border=HALF_SPACE)
81 flexGridSizer.Add(dirControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border=HALF_SPACE)
82 flexGridSizer.Add(wx.StaticText(parent, -1, ""), 0)
83 return nameControl, dirControl, flexGridSizer, Validate
84
85 def AddFilesToCurrentProject(paths, save=False):
86 projectService = wx.GetApp().GetService(ProjectEditor.ProjectService)
87 if projectService:
88 projectDocument = projectService.GetCurrentProject()
89 if projectDocument:
90 files = projectDocument.GetFiles()
91 for path in paths:
92 if path in files:
93 paths.remove(path)
94 if paths:
95 projectDocument.GetCommandProcessor().Submit(ProjectEditor.ProjectAddFilesCommand(projectDocument, paths))
96 projectDocument.GetFirstView().DoSelectFiles([paths[0]])
97 if save:
98 projectDocument.OnSaveDocument(projectDocument.GetFilename())
99
100 def MakeNameEndInExtension(name, extension):
101 if not name:
102 return name
103 root, ext = os.path.splitext(name)
104 if ext == extension:
105 return name
106 else:
107 return name + extension
108
109 # Lame
110 def PluralName(name):
111 if not name:
112 return name
113 if name.endswith('us'):
114 return name[0:-2] + 'ii'
115 elif name.endswith('s'):
116 return name
117 elif name.endswith('y'):
118 return name[0:-1] + 'ies'
119 else:
120 return name + 's'
121
122 def GetPythonExecPath():
123 pythonExecPath = wx.ConfigBase_Get().Read("ActiveGridPythonLocation")
124 if not pythonExecPath:
125 pythonExecPath = utillib.pythonExecPath
126 return pythonExecPath
127
128