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