File: wxProject.py

package info (click to toggle)
wxwindows2.4 2.4.5.1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 59,920 kB
  • ctags: 97,489
  • sloc: cpp: 610,307; ansic: 111,957; python: 103,357; makefile: 3,676; sh: 3,391; lex: 192; yacc: 128; xml: 95; pascal: 74
file content (286 lines) | stat: -rw-r--r-- 11,056 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/python
import sys, os
from   wxPython.wx import *
from   string import *

# Process the command line.  Not much to do;
# just get the name of the project file if it's given. Simple.
projfile = 'Unnamed'
if len(sys.argv) > 1:
   projfile = sys.argv[1]


def MsgBox (window, string):
    dlg=wxMessageDialog(window, string, 'wxProject', wxOK)
    dlg.ShowModal()
    dlg.Destroy()

class main_window(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, -1, title, size = (500, 500),
                         style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)


        # ------------------------------------------------------------------------------------
        # Set up menu bar for the program.
        # ------------------------------------------------------------------------------------
        self.mainmenu = wxMenuBar()   # Create menu bar.
        mainwindow = self

        menu=wxMenu()                                 # Make a menu (will be the Project menu)

        exitID=wxNewId()                              # Make a new ID for a menu entry.
        menu.Append(exitID, '&Open', 'Open project')  # Name the ID by adding it to the menu.
        EVT_MENU(self, exitID, self.OnProjectOpen)    # Create and assign a menu event.
        exitID=wxNewId()
        menu.Append(exitID, '&New', 'New project')
        EVT_MENU(self, exitID, self.OnProjectNew)
        exitID=wxNewId()
        menu.Append(exitID, 'E&xit', 'Exit program')
        EVT_MENU(self, exitID, self.OnProjectExit)

        self.mainmenu.Append (menu, '&Project')       # Add the project menu to the menu bar.


        menu=wxMenu()                                 # Make a menu (will be the File menu)

        exitID=wxNewId()
        menu.Append(exitID, '&Add', 'Add file to project')
        EVT_MENU(self, exitID, self.OnFileAdd)
        exitID=wxNewId()
        menu.Append(exitID, '&Remove', 'Remove file from project')
        EVT_MENU(self, exitID, self.OnFileRemove)
        exitID=wxNewId()
        menu.Append(exitID, '&Open', 'Open file for editing')
        EVT_MENU(self, exitID, self.OnFileOpen)
        exitID=wxNewId()
        menu.Append(exitID, '&Save', 'Save file')
        EVT_MENU(self, exitID, self.OnFileSave)

        self.mainmenu.Append (menu, '&File')          # Add the file menu to the menu bar.

        self.SetMenuBar (self.mainmenu)               # Attach the menu bar to the window.


        # ------------------------------------------------------------------------------------
        # Create the splitter window.
        # ------------------------------------------------------------------------------------
        splitter = wxSplitterWindow (self, -1, style=wxNO_3D|wxSP_3D)
        splitter.SetMinimumPaneSize (1)

        # ------------------------------------------------------------------------------------
        # Create the tree on the left.
        # ------------------------------------------------------------------------------------
        tID = wxNewId()
        self.tree = wxTreeCtrl (splitter, tID, style=wxTR_HAS_BUTTONS |
                                   wxTR_EDIT_LABELS |
                                   wxTR_HAS_VARIABLE_ROW_HEIGHT)
        EVT_TREE_BEGIN_LABEL_EDIT(self.tree, tID, self.OnTreeLabelEdit)
        EVT_TREE_END_LABEL_EDIT(self.tree, tID, self.OnTreeLabelEditEnd)
        EVT_TREE_ITEM_ACTIVATED(self.tree, tID, self.OnTreeItemActivated)

        # ------------------------------------------------------------------------------------
        # Create the editor on the right.
        # ------------------------------------------------------------------------------------
        self.editor = wxTextCtrl(splitter, -1, style=wxTE_MULTILINE)
        self.editor.Enable (0)

        # ------------------------------------------------------------------------------------
        # Install the tree and the editor.
        # ------------------------------------------------------------------------------------
        splitter.SplitVertically (self.tree, self.editor)
        splitter.SetSashPosition (180, True)

        self.Show(True)

        # Some global state variables.
        self.projectdirty = False

    # ----------------------------------------------------------------------------------------
    # Some nice little handlers.
    # ----------------------------------------------------------------------------------------


    def project_open(self, project_file):
        try:
            input = open (project_file, 'r')

            self.tree.DeleteAllItems()

            self.project_file = project_file
            name = replace (input.readline(), "\n", "")
            self.SetTitle (name)
            self.root = self.tree.AddRoot(name)
            self.activeitem = self.root
            for line in input.readlines():
                self.tree.AppendItem (self.root, replace(line, "\n", ""))
            input.close
            self.tree.Expand (self.root)

            self.editor.Clear()
            self.editor.Enable (False)

            self.projectdirty = False
        except IOError:
            pass

    def project_save(self):
        try:
            output = open (self.project_file, 'w+')
            output.write (self.tree.GetItemText (self.root) + "\n")

            count = self.tree.GetChildrenCount (self.root)
            iter = 0
            child = ''
            for i in range(count):
               if i == 0:
                  (child,iter) = self.tree.GetFirstChild(self.root,iter)
               else:
                  (child,iter) = self.tree.GetNextChild(self.root,iter)
               output.write (self.tree.GetItemText(child) + "\n")
            output.close()
            self.projectdirty = False
        except IOError:
            dlg_m = wxMessageDialog (self, 'There was an error saving the project file.',
                                     'Error!', wxOK)
            dlg_m.ShowModal()
            dlg_m.Destroy()

    # ----------------------------------------------------------------------------------------
    # Event handlers from here on out.
    # ----------------------------------------------------------------------------------------

    def OnProjectOpen(self, event):
        open_it = True
        if self.projectdirty:
           dlg=wxMessageDialog(self, 'The project has been changed.  Save?', 'wxProject',
                               wxYES_NO | wxCANCEL)
           result = dlg.ShowModal()
           if result == wxID_YES:
              self.project_save()
           if result == wxID_CANCEL:
              open_it = False
           dlg.Destroy()
        if open_it:
           dlg = wxFileDialog(self, "Choose a project to open", ".", "", "*.wxp", wxOPEN)
           if dlg.ShowModal() == wxID_OK:
              self.project_open(dlg.GetPath())
           dlg.Destroy()

    def OnProjectNew(self, event):
        open_it = True
        if self.projectdirty:
           dlg=wxMessageDialog(self, 'The project has been changed.  Save?', 'wxProject',
                               wxYES_NO | wxCANCEL)
           result = dlg.ShowModal()
           if result == wxID_YES:
              self.project_save()
           if result == wxID_CANCEL:
              open_it = False
           dlg.Destroy()

        if open_it:
           dlg = wxTextEntryDialog (self, "Name for new project:", "New Project",
                                    "New project", wxOK | wxCANCEL)
           if dlg.ShowModal() == wxID_OK:
               newproj = dlg.GetValue()
               dlg.Destroy()
               dlg = wxFileDialog (self, "Place to store new project", ".", "", "*.wxp",
                                   wxSAVE)
               if dlg.ShowModal() == wxID_OK:
                  try:
                     proj = open (dlg.GetPath(), 'w')
                     proj.write (newproj + "\n")
                     proj.close()
                     self.project_open (dlg.GetPath())
                  except IOError:
                     dlg_m = wxMessageDialog (self,
                                              'There was an error saving the new project file.',
                                              'Error!', wxOK)
                     dlg_m.ShowModal()
                     dlg_m.Destroy()
        dlg.Destroy()

    def OnProjectExit(self, event):
        close = True
        if self.projectdirty:
           dlg=wxMessageDialog(self, 'The project has been changed.  Save?', 'wxProject',
                               wxYES_NO | wxCANCEL)
           result = dlg.ShowModal()
           if result == wxID_YES:
              self.project_save()
           if result == wxID_CANCEL:
              close = False
           dlg.Destroy()
        if close:
           self.Close()

    def OnFileAdd(self, event):
        dlg = wxFileDialog (self, "Choose a file to add", ".", "", "*.*", wxOPEN)
        if dlg.ShowModal() == wxID_OK:
           path = os.path.split(dlg.GetPath())
           self.tree.AppendItem (self.root, path[1])
           self.tree.Expand (self.root)
           self.project_save()

    def OnFileRemove(self, event):
        item = self.tree.GetSelection()
        if item != self.root:
           self.tree.Delete (item)
           self.project_save()

    def OnFileOpen(self, event):
        item = self.tree.GetSelection()

    def OnFileSave(self, event):
        if self.activeitem != self.root:
           self.editor.SaveFile (self.tree.GetItemText (self.activeitem))


    def OnTreeLabelEdit(self, event):
        item=event.GetItem()
        if item != self.root:
           event.Veto()

    def OnTreeLabelEditEnd(self, event):
        self.projectdirty = True

    def OnTreeItemActivated(self, event):
        go_ahead = True
        if self.activeitem != self.root:
           if self.editor.IsModified():
              dlg=wxMessageDialog(self, 'The edited file has changed.  Save it?',
                                  'wxProject', wxYES_NO | wxCANCEL)
              result = dlg.ShowModal()
              if result == wxID_YES:
                 self.editor.SaveFile (self.tree.GetItemText (self.activeitem))
              if result == wxID_CANCEL:
                 go_ahead = False
              dlg.Destroy()
           if go_ahead:
              self.tree.SetItemBold (self.activeitem, 0)

        if go_ahead:
           item=event.GetItem()
           self.activeitem = item
           if item != self.root:
              self.tree.SetItemBold (item, 1)
              self.editor.Enable (1)
              self.editor.LoadFile (self.tree.GetItemText(item))
              self.editor.SetInsertionPoint (0)
              self.editor.SetFocus()
           else:
              self.editor.Clear()
              self.editor.Enable (0)

class App(wxApp):
    def OnInit(self):
        frame = main_window(None, -1, "wxProject - " + projfile)
        self.SetTopWindow(frame)
        if (projfile != 'Unnamed'):
            frame.project_open (projfile)
        return True

app = App(0)
app.MainLoop()