3 from wxPython
.wx
import *
6 # Process the command line. Not much to do;
7 # just get the name of the project file if it's given. Simple.
10 projfile
= sys
.argv
[1]
13 def MsgBox (window
, string
):
14 dlg
=wxMessageDialog(window
, string
, 'wxProject', wxOK
)
18 class main_window(wxFrame
):
19 def __init__(self
, parent
, id, title
):
20 wxFrame
.__init
__(self
, parent
, -1, title
, size
= (500, 500),
21 style
=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE
)
24 # ------------------------------------------------------------------------------------
25 # Set up menu bar for the program.
26 # ------------------------------------------------------------------------------------
27 self
.mainmenu
= wxMenuBar() # Create menu bar.
30 menu
=wxMenu() # Make a menu (will be the Project menu)
32 exitID
=wxNewId() # Make a new ID for a menu entry.
33 menu
.Append(exitID
, '&Open', 'Open project') # Name the ID by adding it to the menu.
34 EVT_MENU(self
, exitID
, self
.OnProjectOpen
) # Create and assign a menu event.
36 menu
.Append(exitID
, '&New', 'New project')
37 EVT_MENU(self
, exitID
, self
.OnProjectNew
)
39 menu
.Append(exitID
, 'E&xit', 'Exit program')
40 EVT_MENU(self
, exitID
, self
.OnProjectExit
)
42 self
.mainmenu
.Append (menu
, '&Project') # Add the project menu to the menu bar.
45 menu
=wxMenu() # Make a menu (will be the File menu)
48 menu
.Append(exitID
, '&Add', 'Add file to project')
49 EVT_MENU(self
, exitID
, self
.OnFileAdd
)
51 menu
.Append(exitID
, '&Remove', 'Remove file from project')
52 EVT_MENU(self
, exitID
, self
.OnFileRemove
)
54 menu
.Append(exitID
, '&Open', 'Open file for editing')
55 EVT_MENU(self
, exitID
, self
.OnFileOpen
)
57 menu
.Append(exitID
, '&Save', 'Save file')
58 EVT_MENU(self
, exitID
, self
.OnFileSave
)
60 self
.mainmenu
.Append (menu
, '&File') # Add the file menu to the menu bar.
62 self
.SetMenuBar (self
.mainmenu
) # Attach the menu bar to the window.
65 # ------------------------------------------------------------------------------------
66 # Create the splitter window.
67 # ------------------------------------------------------------------------------------
68 splitter
= wxSplitterWindow (self
, -1, style
=wxNO_3D|wxSP_3D
)
69 splitter
.SetMinimumPaneSize (1)
71 # ------------------------------------------------------------------------------------
72 # Create the tree on the left.
73 # ------------------------------------------------------------------------------------
75 self
.tree
= wxTreeCtrl (splitter
, tID
, style
=wxTR_HAS_BUTTONS |
77 wxTR_HAS_VARIABLE_ROW_HEIGHT
)
78 EVT_TREE_BEGIN_LABEL_EDIT(self
.tree
, tID
, self
.OnTreeLabelEdit
)
79 EVT_TREE_END_LABEL_EDIT(self
.tree
, tID
, self
.OnTreeLabelEditEnd
)
80 EVT_TREE_ITEM_ACTIVATED(self
.tree
, tID
, self
.OnTreeItemActivated
)
82 # ------------------------------------------------------------------------------------
83 # Create the editor on the right.
84 # ------------------------------------------------------------------------------------
85 self
.editor
= wxTextCtrl(splitter
, -1, style
=wxTE_MULTILINE
)
86 self
.editor
.Enable (0)
88 # ------------------------------------------------------------------------------------
89 # Install the tree and the editor.
90 # ------------------------------------------------------------------------------------
91 splitter
.SplitVertically (self
.tree
, self
.editor
)
92 splitter
.SetSashPosition (180, True)
96 # Some global state variables.
97 self
.projectdirty
= False
99 # ----------------------------------------------------------------------------------------
100 # Some nice little handlers.
101 # ----------------------------------------------------------------------------------------
104 def project_open(self
, project_file
):
106 input = open (project_file
, 'r')
108 self
.tree
.DeleteAllItems()
110 self
.project_file
= project_file
111 name
= replace (input.readline(), "\n", "")
113 self
.root
= self
.tree
.AddRoot(name
)
114 self
.activeitem
= self
.root
115 for line
in input.readlines():
116 self
.tree
.AppendItem (self
.root
, replace(line
, "\n", ""))
118 self
.tree
.Expand (self
.root
)
121 self
.editor
.Enable (False)
123 self
.projectdirty
= False
127 def project_save(self
):
129 output
= open (self
.project_file
, 'w+')
130 output
.write (self
.tree
.GetItemText (self
.root
) + "\n")
132 count
= self
.tree
.GetChildrenCount (self
.root
)
135 for i
in range(count
):
137 (child
,iter) = self
.tree
.GetFirstChild(self
.root
,iter)
139 (child
,iter) = self
.tree
.GetNextChild(self
.root
,iter)
140 output
.write (self
.tree
.GetItemText(child
) + "\n")
142 self
.projectdirty
= False
144 dlg_m
= wxMessageDialog (self
, 'There was an error saving the project file.',
149 # ----------------------------------------------------------------------------------------
150 # Event handlers from here on out.
151 # ----------------------------------------------------------------------------------------
153 def OnProjectOpen(self
, event
):
155 if self
.projectdirty
:
156 dlg
=wxMessageDialog(self
, 'The project has been changed. Save?', 'wxProject',
158 result
= dlg
.ShowModal()
159 if result
== wxID_YES
:
161 if result
== wxID_CANCEL
:
165 dlg
= wxFileDialog(self
, "Choose a project to open", ".", "", "*.wxp", wxOPEN
)
166 if dlg
.ShowModal() == wxID_OK
:
167 self
.project_open(dlg
.GetPath())
170 def OnProjectNew(self
, event
):
172 if self
.projectdirty
:
173 dlg
=wxMessageDialog(self
, 'The project has been changed. Save?', 'wxProject',
175 result
= dlg
.ShowModal()
176 if result
== wxID_YES
:
178 if result
== wxID_CANCEL
:
183 dlg
= wxTextEntryDialog (self
, "Name for new project:", "New Project",
184 "New project", wxOK | wxCANCEL
)
185 if dlg
.ShowModal() == wxID_OK
:
186 newproj
= dlg
.GetValue()
188 dlg
= wxFileDialog (self
, "Place to store new project", ".", "", "*.wxp",
190 if dlg
.ShowModal() == wxID_OK
:
192 proj
= open (dlg
.GetPath(), 'w')
193 proj
.write (newproj
+ "\n")
195 self
.project_open (dlg
.GetPath())
197 dlg_m
= wxMessageDialog (self
,
198 'There was an error saving the new project file.',
204 def OnProjectExit(self
, event
):
206 if self
.projectdirty
:
207 dlg
=wxMessageDialog(self
, 'The project has been changed. Save?', 'wxProject',
209 result
= dlg
.ShowModal()
210 if result
== wxID_YES
:
212 if result
== wxID_CANCEL
:
218 def OnFileAdd(self
, event
):
219 dlg
= wxFileDialog (self
, "Choose a file to add", ".", "", "*.*", wxOPEN
)
220 if dlg
.ShowModal() == wxID_OK
:
221 path
= os
.path
.split(dlg
.GetPath())
222 self
.tree
.AppendItem (self
.root
, path
[1])
223 self
.tree
.Expand (self
.root
)
226 def OnFileRemove(self
, event
):
227 item
= self
.tree
.GetSelection()
228 if item
!= self
.root
:
229 self
.tree
.Delete (item
)
232 def OnFileOpen(self
, event
):
233 item
= self
.tree
.GetSelection()
235 def OnFileSave(self
, event
):
236 if self
.activeitem
!= self
.root
:
237 self
.editor
.SaveFile (self
.tree
.GetItemText (self
.activeitem
))
240 def OnTreeLabelEdit(self
, event
):
242 if item
!= self
.root
:
245 def OnTreeLabelEditEnd(self
, event
):
246 self
.projectdirty
= True
248 def OnTreeItemActivated(self
, event
):
250 if self
.activeitem
!= self
.root
:
251 if self
.editor
.IsModified():
252 dlg
=wxMessageDialog(self
, 'The edited file has changed. Save it?',
253 'wxProject', wxYES_NO | wxCANCEL
)
254 result
= dlg
.ShowModal()
255 if result
== wxID_YES
:
256 self
.editor
.SaveFile (self
.tree
.GetItemText (self
.activeitem
))
257 if result
== wxID_CANCEL
:
261 self
.tree
.SetItemBold (self
.activeitem
, 0)
265 self
.activeitem
= item
266 if item
!= self
.root
:
267 self
.tree
.SetItemBold (item
, 1)
268 self
.editor
.Enable (1)
269 self
.editor
.LoadFile (self
.tree
.GetItemText(item
))
270 self
.editor
.SetInsertionPoint (0)
271 self
.editor
.SetFocus()
274 self
.editor
.Enable (0)
278 frame
= main_window(None, -1, "wxProject - " + projfile
)
279 self
.SetTopWindow(frame
)
280 if (projfile
!= 'Unnamed'):
281 frame
.project_open (projfile
)