]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxFileHistory.py
1 # 11/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
9 #----------------------------------------------------------------------
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
20 #----------------------------------------------------------------------
22 class TestPanel(wx
.Panel
):
23 def __init__(self
, parent
, log
):
25 wx
.Panel
.__init
__(self
, parent
, -1)
26 box
= wx
.BoxSizer(wx
.VERTICAL
)
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
)
33 t
= wx
.StaticText(self
, -1, "FileHistory")
35 box
.Add(t
, 0, wx
.CENTER|wx
.ALL
, 5)
37 box
.Add(wx
.StaticLine(self
, -1), 0, wx
.EXPAND
)
40 t
= wx
.StaticText(self
, -1, text
)
42 box
.Add(t
, 0, wx
.CENTER|wx
.ALL
, 5)
45 self
.SetAutoLayout(True)
48 self
.menu
= m
= wx
.Menu()
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)
64 self
.filehistory
= wx
.FileHistory()
65 self
.filehistory
.UseMenu(self
.menu
)
67 # and finally the event handler bindings
68 self
.Bind(wx
.EVT_RIGHT_UP
, self
.OnRightClick
)
70 self
.Bind(wx
.EVT_MENU
, self
.OnFileOpenDialog
, id=wx
.ID_OPEN
)
73 wx
.EVT_MENU_RANGE
, self
.OnFileHistory
, id=wx
.ID_FILE1
, id2
=wx
.ID_FILE9
76 self
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.Cleanup
)
79 def Cleanup(self
, *args
):
80 # A little extra cleanup is required for the FileHistory control
85 def OnRightClick(self
, evt
):
86 self
.PopupMenu(self
.menu
, evt
.GetPosition())
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
)
95 if dlg
.ShowModal() == wx
.ID_OK
:
97 self
.log
.write("You selected %s\n" % path
)
99 # add it to the history
100 self
.filehistory
.AddFileToHistory(path
)
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
)
111 # add it back to the history so it will be moved up the list
112 self
.filehistory
.AddFileToHistory(path
)
116 #----------------------------------------------------------------------
118 def runTest(frame
, nb
, log
):
119 win
= TestPanel(nb
, log
)
122 #----------------------------------------------------------------------
126 overview
= """<html><body>
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.
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
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
143 if __name__
== '__main__':
146 run
.main(['', os
.path
.basename(sys
.argv
[0])])