]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FileHistory.py
'[1219035] cleanup: miscellaneous' and minor source cleaning.
[wxWidgets.git] / wxPython / demo / FileHistory.py
1
2 import os
3 import wx
4
5 #----------------------------------------------------------------------
6
7 text = """\
8 Right-click on the panel above the line to get a menu. This menu will
9 be managed by a FileHistory object and so the files you select will
10 automatically be added to the end of the menu and will be selectable
11 the next time the menu is viewed. The filename selected, either via the
12 Open menu item, or from the history, will be displayed in the log
13 window below.
14 """
15
16 #----------------------------------------------------------------------
17
18 class TestPanel(wx.Panel):
19 def __init__(self, parent, log):
20 self.log = log
21 wx.Panel.__init__(self, parent, -1)
22 box = wx.BoxSizer(wx.VERTICAL)
23
24 # Make and layout the controls
25 fs = self.GetFont().GetPointSize()
26 bf = wx.Font(fs+4, wx.SWISS, wx.NORMAL, wx.BOLD)
27 nf = wx.Font(fs+2, wx.SWISS, wx.NORMAL, wx.NORMAL)
28
29 t = wx.StaticText(self, -1, "FileHistory")
30 t.SetFont(bf)
31 box.Add(t, 0, wx.CENTER|wx.ALL, 5)
32
33 box.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
34 box.Add((10,20))
35
36 t = wx.StaticText(self, -1, text)
37 t.SetFont(nf)
38 box.Add(t, 0, wx.CENTER|wx.ALL, 5)
39
40 self.SetSizer(box)
41 self.SetAutoLayout(True)
42
43 # Make a menu
44 self.menu = m = wx.Menu()
45
46 # Little know wx Fact #42: there are a number of pre-set IDs
47 # in the wx package, to be used for common controls such as those
48 # illustrated below. Neat, huh?
49 m.Append(wx.ID_NEW, "&New")
50 m.Append(wx.ID_OPEN, "&Open...")
51 m.Append(wx.ID_CLOSE, "&Close")
52 m.Append(wx.ID_SAVE, "&Save")
53 m.Append(wx.ID_SAVEAS, "Save &as...")
54 m.Enable(wx.ID_NEW, False)
55 m.Enable(wx.ID_CLOSE, False)
56 m.Enable(wx.ID_SAVE, False)
57 m.Enable(wx.ID_SAVEAS, False)
58
59 # and a file history
60 self.filehistory = wx.FileHistory()
61 self.filehistory.UseMenu(self.menu)
62
63 # and finally the event handler bindings
64 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
65
66 self.Bind(wx.EVT_MENU, self.OnFileOpenDialog, id=wx.ID_OPEN)
67
68 self.Bind(
69 wx.EVT_MENU_RANGE, self.OnFileHistory, id=wx.ID_FILE1, id2=wx.ID_FILE9
70 )
71
72 self.Bind(wx.EVT_WINDOW_DESTROY, self.Cleanup)
73
74
75 def Cleanup(self, *args):
76 # A little extra cleanup is required for the FileHistory control
77 del self.filehistory
78 self.menu.Destroy()
79
80
81 def OnRightClick(self, evt):
82 self.PopupMenu(self.menu, evt.GetPosition())
83
84
85 def OnFileOpenDialog(self, evt):
86 dlg = wx.FileDialog(self,
87 defaultDir = os.getcwd(),
88 wildcard = "All Files|*",
89 style = wx.OPEN | wx.CHANGE_DIR)
90
91 if dlg.ShowModal() == wx.ID_OK:
92 path = dlg.GetPath()
93 self.log.write("You selected %s\n" % path)
94
95 # add it to the history
96 self.filehistory.AddFileToHistory(path)
97
98 dlg.Destroy()
99
100
101 def OnFileHistory(self, evt):
102 # get the file based on the menu ID
103 fileNum = evt.GetId() - wx.ID_FILE1
104 path = self.filehistory.GetHistoryFile(fileNum)
105 self.log.write("You selected %s\n" % path)
106
107 # add it back to the history so it will be moved up the list
108 self.filehistory.AddFileToHistory(path)
109
110
111
112 #----------------------------------------------------------------------
113
114 def runTest(frame, nb, log):
115 win = TestPanel(nb, log)
116 return win
117
118 #----------------------------------------------------------------------
119
120
121
122 overview = """<html><body>
123 <h3>FileHistory</h3>
124
125 wxFileHistory encapsulates functionality to record the last few files
126 visited, and to allow the user to quickly load these files using the
127 list appended to a menu, such as the File menu.
128
129 <p>Note that this inclusion is not automatic; as illustrated in this example,
130 you must add files (and remove them) as deemed necessary within the framework
131 of your program.
132
133 <p>Note also the additional cleanup required for this class, namely trapping the
134 enclosing window's Destroy event and deleting the file history control and its
135 associated menu.
136 </body></html>
137 """
138
139 if __name__ == '__main__':
140 import sys,os
141 import run
142 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
143