= wx.FileHistory = The FileHistory is a menu item that displays a list of the most recently accessed files (from your application). This page will describe how to use it. {{attachment:recent.png}} == Usage == {{{ #!python #!/usr/bin/env python import wx class Example(wx.Frame): def __init__(self): wx.Frame.__init__(self, parent=None, title="wx.FileHistory example") self.filehistory = wx.FileHistory(8) self.config = wx.Config("Your-Programs-Name", style=wx.CONFIG_USE_LOCAL_FILE) self.filehistory.Load(self.config) self.menu = wx.MenuBar() _file = wx.Menu() recent = wx.Menu() self.filehistory.UseMenu(recent) self.filehistory.AddFilesToMenu() _file.Append(wx.ID_OPEN, "&Open") _file.AppendMenu(wx.ID_ANY, "&Recent Files", recent) self.menu.Append(_file, "&File") self.SetMenuBar(self.menu) self.Bind(wx.EVT_MENU, self.do_open, id=wx.ID_OPEN) self.Bind(wx.EVT_MENU_RANGE, self.on_file_history, id=wx.ID_FILE1, id2=wx.ID_FILE9) def do_open(self, event): dlg = wx.FileDialog(self, "Open file...", style=wx.OPEN) if dlg.ShowModal() == wx.ID_OK: self.filehistory.AddFileToHistory(dlg.GetPath()) self.filehistory.Save(self.config) self.config.Flush() else: dlg.Destroy() def on_file_history(self, event): fileNum = event.GetId() - wx.ID_FILE1 path = self.filehistory.GetHistoryFile(fileNum) self.filehistory.AddFileToHistory(path) # move up the list # do whatever you want with the file path... app = wx.App(False) frame = Example() frame.Show() app.MainLoop() }}} The integer parameter to the `FileHistory` is the maximum number of files to remember. We set up a `wx.Config` attribute that the `FileHistory` can read and write to. The call to Load() will ensure the file history is filled up with the last saved items when the application starts. We then create a sub-menu for the `FileHistory` to be displayed in, and tell the filehistory attribute to use that menu. The call to `AddFilesToMenu` actually fills the menu, without this it would be empty. When a user chooses a file from the open dialog, its path is added to the history list, and the config file is saved. Note the call to `config.Flush()` - the sample wouldn't work on Linux, otherwise. To handle a click on the menu items, we bind to EVT_MENU_RANGE. I'm not sure why exactly we Bind to `wx.ID_FILE1` and `wx.ID_FILE9`, but it works. :p Well, that's about it! Cheers, StevenSproat -- 18 Jan 2010 == Comments ==