]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FileHistory.py
5 #----------------------------------------------------------------------
8 Right-click on the panel above the line to get a menu. This menu will
9 be managed by a FileHistory object and so the files you select will
10 automatically be added to the end of the menu and will be selectable
11 the next time the menu is viewed. The filename selected, either via the
12 Open menu item, or from the history, will be displayed in the log
16 #----------------------------------------------------------------------
18 class TestPanel(wx
.Panel
):
19 def __init__(self
, parent
, log
):
21 wx
.Panel
.__init
__(self
, parent
, -1)
22 box
= wx
.BoxSizer(wx
.VERTICAL
)
24 # Make and layout the controls
25 fs
= self
.GetFont().GetPointSize()
26 bf
= wx
.Font(fs
+4, wx
.SWISS
, wx
.NORMAL
, wx
.BOLD
)
27 nf
= wx
.Font(fs
+2, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)
29 t
= wx
.StaticText(self
, -1, "FileHistory")
31 box
.Add(t
, 0, wx
.CENTER|wx
.ALL
, 5)
33 box
.Add(wx
.StaticLine(self
, -1), 0, wx
.EXPAND
)
36 t
= wx
.StaticText(self
, -1, text
)
38 box
.Add(t
, 0, wx
.CENTER|wx
.ALL
, 5)
41 self
.SetAutoLayout(True)
44 self
.menu
= m
= wx
.Menu()
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)
60 self
.filehistory
= wx
.FileHistory()
61 self
.filehistory
.UseMenu(self
.menu
)
63 # and finally the event handler bindings
64 self
.Bind(wx
.EVT_RIGHT_UP
, self
.OnRightClick
)
66 self
.Bind(wx
.EVT_MENU
, self
.OnFileOpenDialog
, id=wx
.ID_OPEN
)
69 wx
.EVT_MENU_RANGE
, self
.OnFileHistory
, id=wx
.ID_FILE1
, id2
=wx
.ID_FILE9
72 self
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.Cleanup
)
75 def Cleanup(self
, *args
):
76 # A little extra cleanup is required for the FileHistory control
81 def OnRightClick(self
, evt
):
82 self
.PopupMenu(self
.menu
, evt
.GetPosition())
85 def OnFileOpenDialog(self
, evt
):
86 dlg
= wx
.FileDialog(self
,
87 defaultDir
= os
.getcwd(),
88 wildcard
= "All Files|*",
89 style
= wx
.OPEN | wx
.CHANGE_DIR
)
91 if dlg
.ShowModal() == wx
.ID_OK
:
93 self
.log
.write("You selected %s\n" % path
)
95 # add it to the history
96 self
.filehistory
.AddFileToHistory(path
)
101 def OnFileHistory(self
, evt
):
102 # get the file based on the menu ID
103 fileNum
= evt
.GetId() - wx
.ID_FILE1
104 path
= self
.filehistory
.GetHistoryFile(fileNum
)
105 self
.log
.write("You selected %s\n" % path
)
107 # add it back to the history so it will be moved up the list
108 self
.filehistory
.AddFileToHistory(path
)
112 #----------------------------------------------------------------------
114 def runTest(frame
, nb
, log
):
115 win
= TestPanel(nb
, log
)
118 #----------------------------------------------------------------------
122 overview
= """<html><body>
125 wxFileHistory encapsulates functionality to record the last few files
126 visited, and to allow the user to quickly load these files using the
127 list appended to a menu, such as the File menu.
129 <p>Note that this inclusion is not automatic; as illustrated in this example,
130 you must add files (and remove them) as deemed necessary within the framework
133 <p>Note also the additional cleanup required for this class, namely trapping the
134 enclosing window's Destroy event and deleting the file history control and its
139 if __name__
== '__main__':
142 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])