~ubuntu-branches/ubuntu/maverick/pype/maverick-proposed

« back to all changes in this revision

Viewing changes to plugins/workspace.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2005-09-13 20:51:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050913205129-ph18px3tkx7ay4pv
Tags: upstream-2.1.1
ImportĀ upstreamĀ versionĀ 2.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#system imports
 
3
import os
 
4
 
 
5
#site-packages imports
 
6
import wx
 
7
 
 
8
#local imports
 
9
import filehistory
 
10
 
 
11
#Why should I use an object when a closure works just as well?
 
12
#God this is such a hack, but it makes me smile.
 
13
def WorkspaceMenu(parentmenu, parentwindow, workspaces, workspace_order):
 
14
    
 
15
    def OnCloseAll(event):
 
16
        self = parentwindow
 
17
        sel = self.control.GetSelection()
 
18
        cnt = self.control.GetPageCount()
 
19
        try:
 
20
            for i in xrange(cnt):
 
21
                win = self.control.GetPage(i).GetWindow1()
 
22
                
 
23
                #Yeah, I know that using a function that is placed in the
 
24
                #module's namespace AFTER import is bad form, but I do it
 
25
                #anyways.
 
26
                if isdirty(win):
 
27
                    self.control.SetSelection(i)
 
28
                    self.sharedsave(win)
 
29
        except cancelled:
 
30
            event.Skip()
 
31
            return
 
32
        self.starting = 1
 
33
        for i in xrange(cnt-1, -1, -1):
 
34
            self.OnClose(None, i, self.control.GetPage(i).GetWindow1())
 
35
        self.starting = 0
 
36
    
 
37
    def OnSave(event):
 
38
        #check for dirty and unnamed files...
 
39
        self = parentwindow
 
40
        sel = self.control.GetSelection()
 
41
        cnt = self.control.GetPageCount()
 
42
        d = 0
 
43
        l = []
 
44
        for i in xrange(cnt):
 
45
            win = self.control.GetPage(i).GetWindow1()
 
46
            data = os.path.join(win.dirname, win.filename).encode('ascii')
 
47
            d += win.dirty and data == ' '
 
48
            if data != ' ':
 
49
                l.append(data)
 
50
        
 
51
        if d:
 
52
            self.dialog("Cannot save workspace with\nmodified untitled documents.", "Workspace Save Aborted!")
 
53
            return
 
54
        
 
55
        #get the name of the workspace
 
56
        while 1:
 
57
        
 
58
            dlg = wx.TextEntryDialog(parentwindow, "What would you like this workspace to be called?", "Workspace name")
 
59
            if openmenu.last:
 
60
                dlg.SetValue(openmenu.last)
 
61
            rslt = dlg.ShowModal()
 
62
            workspacename = dlg.GetValue()
 
63
            dlg.Destroy()
 
64
            if rslt != wx.ID_OK:
 
65
                self.SetStatusText("Workspace save cancelled")
 
66
                return
 
67
            
 
68
            wn = workspacename.strip()
 
69
            
 
70
            #check for usable name
 
71
            if not wn:
 
72
                self.SetStatusText("Workspace save cancelled")
 
73
                return
 
74
            
 
75
            #check for unused name
 
76
            if wn.lower() in [i.lower() for i in workspace_order]:
 
77
                if self.dialog("Are you sure you want to replace\nthe pre-existing workspace:\n%s"%wn.lower(), "Duplicate Workspace", wx.YES_NO) != wx.ID_YES:
 
78
                    continue
 
79
            
 
80
            break
 
81
        
 
82
        #remove potential duplicates
 
83
        workspace_order[:] = [i for i in workspace_order if i.lower() != wn.lower() or openmenu.ItemRemove(i) or deletemenu.ItemRemove(i)]
 
84
        x = dict(workspaces)
 
85
        workspaces.clear()
 
86
        for i in workspace_order:
 
87
            workspaces[i] = x[i]
 
88
        
 
89
        #handle workspace ordering
 
90
        workspace_order.insert(0, wn)
 
91
        workspaces[wn] = l
 
92
        openmenu.ItemAdd(wn)
 
93
        deletemenu.ItemAdd(wn)
 
94
    
 
95
    def OnOpen(label):
 
96
        ## print "opening workspace"
 
97
        
 
98
        #handle ordering of workspaces
 
99
        if label in workspace_order:
 
100
            workspace_order.remove(label)
 
101
        
 
102
        workspace_order.insert(0, label)
 
103
        
 
104
        #fetch the workspace
 
105
        ws = workspaces.get(label, [])
 
106
        
 
107
        #open all documents in the workspace
 
108
        parentwindow.OnDrop(ws)
 
109
    
 
110
    def OnDelete(label):
 
111
        ## print "deleting workspace..."
 
112
        #delete the workspace and fix workspace order
 
113
        
 
114
        if label in workspace_order:
 
115
            workspace_order.remove(label)
 
116
        workspaces.pop(label, None)
 
117
    
 
118
    #code that actually modifies the menu
 
119
    if 1:
 
120
        closeall = wx.NewId()
 
121
        parentmenu.Append(closeall, "Close All Documents",
 
122
                        "Closes all open documents, asking to save changes on modified files")
 
123
        wx.EVT_MENU(parentwindow, closeall, OnCloseAll)
 
124
        parentmenu.AppendSeparator()
 
125
        nwksid = wx.NewId()
 
126
        parentmenu.Append(nwksid, "Save Workspace",
 
127
                        "Saves the current workspace, aborts if modified and unnamed files are open")
 
128
        wx.EVT_MENU(parentwindow, nwksid, OnSave)
 
129
        
 
130
        openmenu = filehistory.FileHistory(parentwindow, callback=[OnOpen], seq=workspace_order)
 
131
        deletemenu = filehistory.FileHistory(parentwindow, remove=1,
 
132
                                callback=[openmenu.ItemRemove, OnDelete],
 
133
                                seq=workspace_order,
 
134
                                delmsg=("Are you sure you want to delete the workspace:\n%s",
 
135
                                        "Delete Workspace?"))
 
136
        openmenu.callback.append(deletemenu.ItemAdd)
 
137
        parentmenu.AppendMenu(wx.NewId(), "Open Workspace", openmenu)
 
138
        parentmenu.AppendMenu(wx.NewId(), "Delete Workspace", deletemenu)
 
139
        parentmenu.AppendSeparator()
 
140