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