]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/FileHistory.py
workaround sizing bug on Mac
[wxWidgets.git] / wxPython / demo / FileHistory.py
CommitLineData
7c41d11a 1
8fa876ca
RD
2import os
3import wx
7c41d11a
RD
4
5#----------------------------------------------------------------------
6
7text = """\
8fa876ca
RD
8Right-click on the panel above the line to get a menu. This menu will
9be managed by a FileHistory object and so the files you select will
10automatically be added to the end of the menu and will be selectable
11the next time the menu is viewed. The filename selected, either via the
12Open menu item, or from the history, will be displayed in the log
13window below.
7c41d11a
RD
14"""
15
16#----------------------------------------------------------------------
17
8fa876ca 18class TestPanel(wx.Panel):
7c41d11a
RD
19 def __init__(self, parent, log):
20 self.log = log
8fa876ca
RD
21 wx.Panel.__init__(self, parent, -1)
22 box = wx.BoxSizer(wx.VERTICAL)
7c41d11a
RD
23
24 # Make and layout the controls
d3bfec74 25 fs = self.GetFont().GetPointSize()
8fa876ca
RD
26 bf = wx.Font(fs+4, wx.SWISS, wx.NORMAL, wx.BOLD)
27 nf = wx.Font(fs+2, wx.SWISS, wx.NORMAL, wx.NORMAL)
7c41d11a 28
8fa876ca 29 t = wx.StaticText(self, -1, "FileHistory")
7c41d11a 30 t.SetFont(bf)
8fa876ca 31 box.Add(t, 0, wx.CENTER|wx.ALL, 5)
7c41d11a 32
8fa876ca 33 box.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
d14a1e28 34 box.Add((10,20))
7c41d11a 35
8fa876ca 36 t = wx.StaticText(self, -1, text)
7c41d11a 37 t.SetFont(nf)
8fa876ca 38 box.Add(t, 0, wx.CENTER|wx.ALL, 5)
7c41d11a
RD
39
40 self.SetSizer(box)
1e4a197e 41 self.SetAutoLayout(True)
7c41d11a
RD
42
43 # Make a menu
8fa876ca
RD
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)
7c41d11a
RD
58
59 # and a file history
8fa876ca 60 self.filehistory = wx.FileHistory()
7c41d11a
RD
61 self.filehistory.UseMenu(self.menu)
62
63 # and finally the event handler bindings
8fa876ca
RD
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)
7c41d11a
RD
73
74
f2a10cdc 75 def Cleanup(self, *args):
8fa876ca 76 # A little extra cleanup is required for the FileHistory control
7c41d11a
RD
77 del self.filehistory
78 self.menu.Destroy()
79
80
81 def OnRightClick(self, evt):
095315e2 82 self.PopupMenu(self.menu)
7c41d11a
RD
83
84
85 def OnFileOpenDialog(self, evt):
8fa876ca 86 dlg = wx.FileDialog(self,
7c41d11a 87 defaultDir = os.getcwd(),
93214200 88 wildcard = "All Files|*",
8fa876ca
RD
89 style = wx.OPEN | wx.CHANGE_DIR)
90
91 if dlg.ShowModal() == wx.ID_OK:
7c41d11a
RD
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
8fa876ca 103 fileNum = evt.GetId() - wx.ID_FILE1
7c41d11a
RD
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
114def runTest(frame, nb, log):
115 win = TestPanel(nb, log)
116 return win
117
118#----------------------------------------------------------------------
119
120
121
122overview = """<html><body>
8fa876ca 123<h3>FileHistory</h3>
7c41d11a
RD
124
125wxFileHistory encapsulates functionality to record the last few files
126visited, and to allow the user to quickly load these files using the
127list appended to a menu, such as the File menu.
128
8fa876ca
RD
129<p>Note that this inclusion is not automatic; as illustrated in this example,
130you must add files (and remove them) as deemed necessary within the framework
131of your program.
132
133<p>Note also the additional cleanup required for this class, namely trapping the
134enclosing window's Destroy event and deleting the file history control and its
135associated menu.
7c41d11a
RD
136</body></html>
137"""
93214200
RD
138
139if __name__ == '__main__':
8641d30c 140 import sys,os
93214200 141 import run
8eca4fef 142 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
93214200 143