]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/ide/activegrid/tool/UICommon.py
Added the ActiveGrid IDE as a sample application
[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 _ = wx.GetTranslation
18
19 def CreateDirectoryControl( parent, fileLabel, dirLabel, fileExtension, startingName="", startingDirectory=""):
20
21 nameControl = wx.TextCtrl(parent, -1, startingName, size=(-1,-1))
22 nameLabelText = wx.StaticText(parent, -1, fileLabel)
23 dirLabelText = wx.StaticText(parent, -1, dirLabel)
24 dirControl = wx.TextCtrl(parent, -1, startingDirectory, size=(-1,-1))
25 dirControl.SetToolTipString(startingDirectory)
26 button = wx.Button(parent, -1, _("Browse..."), size=(60,-1))
27
28 def OnFindDirClick(event):
29 name = ""
30 nameCtrlValue = nameControl.GetValue()
31 if nameCtrlValue:
32 root, ext = os.path.splitext( nameCtrlValue )
33 if ext == '.' + fileExtension:
34 name = nameCtrlValue
35 else:
36 name = _("%s.%s") % (nameCtrlValue, fileExtension)
37 path = wx.FileSelector(_("Choose a filename and directory"),
38 "",
39 "%s" % name,
40 wildcard=_("*.%s") % fileExtension ,
41 flags=wx.SAVE,
42 parent=parent)
43
44 if path:
45 dir, filename = os.path.split(path)
46 dirControl.SetValue(dir)
47 dirControl.SetToolTipString(dir)
48 nameControl.SetValue(filename)
49
50 parent.Bind(wx.EVT_BUTTON, OnFindDirClick, button)
51
52 def Validate(allowOverwriteOnPrompt=False):
53 if nameControl.GetValue() == "":
54 wx.MessageBox(_("Please provide a filename."), _("Provide a Filename"))
55 return False
56 if nameControl.GetValue().find(' ') != -1:
57 wx.MessageBox(_("Please provide a filename that does not contains spaces."), _("Spaces in Filename"))
58 return False
59 filePath = os.path.join(dirControl.GetValue(), MakeNameEndInExtension(nameControl.GetValue(), "." + fileExtension))
60 if os.path.exists(filePath):
61 if allowOverwriteOnPrompt:
62 res = wx.MessageBox(_("That file already exists. Would you like to overwrite it."), "File Exists", style=wx.YES_NO|wx.NO_DEFAULT)
63 return (res == wx.YES)
64 else:
65 wx.MessageBox(_("That file already exists. Please choose a different name."), "File Exists")
66 return False
67 return True
68 HALF_SPACE = 5
69 flexGridSizer = wx.FlexGridSizer(cols = 3, vgap = HALF_SPACE, hgap = HALF_SPACE)
70 flexGridSizer.AddGrowableCol(1,1)
71 flexGridSizer.Add(nameLabelText, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.TOP|wx.RIGHT, HALF_SPACE)
72 flexGridSizer.Add(nameControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND)
73 flexGridSizer.Add(button, flag=wx.ALIGN_RIGHT|wx.LEFT, border=HALF_SPACE)
74
75 flexGridSizer.Add(dirLabelText, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.TOP|wx.RIGHT, border=HALF_SPACE)
76 flexGridSizer.Add(dirControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border=HALF_SPACE)
77 flexGridSizer.Add(wx.StaticText(parent, -1, ""), 0)
78 return nameControl, dirControl, flexGridSizer, Validate
79
80 def AddFilesToCurrentProject(paths, save=False):
81 projectService = wx.GetApp().GetService(ProjectEditor.ProjectService)
82 if projectService:
83 projectDocument = projectService.GetCurrentProject()
84 if projectDocument:
85 files = projectDocument.GetFiles()
86 for path in paths:
87 if path in files:
88 paths.remove(path)
89 if paths:
90 projectDocument.GetCommandProcessor().Submit(ProjectEditor.ProjectAddFilesCommand(projectDocument, paths))
91 if save:
92 projectDocument.OnSaveDocument(projectDocument.GetFilename())
93
94 def MakeNameEndInExtension(name, extension):
95 if not name:
96 return name
97 root, ext = os.path.splitext(name)
98 if ext == extension:
99 return name
100 else:
101 return name + extension
102
103 # Lame
104 def PluralName(name):
105 if not name:
106 return name
107 if name.endswith('us'):
108 return name[0:-2] + 'ii'
109 elif name.endswith('s'):
110 return name
111 elif name.endswith('y'):
112 return name[0:-1] + 'ies'
113 else:
114 return name + 's'
115