Use `wx.FileHistory`
Brought to you by:
drpython
`wx` comes with a class to handle an MRU list that can be attached to a menu for things like file histories. I sublassed it to get a more Python sequence like behaviour (len(), read only index access, iterable) and moved the code to save and load the list into that class. This eliminated a little bit code repetition. Downside: Changing the number of recent files in the preferences needs a restart to become effective.
thank you for all your improvements and patches. I will take a look into them in the next days. Important things are, that they do not change the interface, because many plugins rely on this interfaces.
I have a script, which access also to the recent files. I will take a look, if I can adapt this. I like it very much, because it uses the drSingleChoice (with the ability to shrink the choice, while you are typing)
#drscript
import wx
from drSingleChoiceDialog import drSingleChoiceDialog
DrFrame.myrecentfiles = DrFrame.recentfiles[:]
d = drSingleChoiceDialog(DrFrame, "Select from recent Files", DrFrame.myrecentfiles)
d.SetSize((500, 200))
answer = d.ShowModal()
d.Destroy()
if answer == wx.ID_OK:
->DrFrame.OpenOrSwitchToFile(d.GetStringSelection())
(-> is a tab character)
You make the assumption that `DrFrame.recentfiles` is a list by using ``[:]`` to copy it. If you just assume that it is a sequence or iterable by changing that line to: ``DrFrame.myrecentfiles = list(DrFrame.recentfiles)`` it should work with my patch just fine.
BTW: Is it really a good idea to attach new attributes to `DrFrame`? That object already has too many attributes and every new bears the risk of a name collision. Especially in this case you don't even seem to use the fact that the copy of the recent file names is persistent.
Yes, the DrFrame Variable is unnecessary. I also saw this, but here, one cannot change the posting afterwards again, a "local" variable is preferable of course. It isn't needed aftwards anyway. The drSingleChoice Dialog sorts the list inline, so I copied it before. Maybe a change from sort to sorted would be better, so the list is not changed, and the copy list is unnecesary then.
I've checked in the copy list in drSingleChoiceDialog.py this, so now:
from drSingleChoiceDialog import drSingleChoiceDialog
d = drSingleChoiceDialog(DrFrame, "Select from recent Files", DrFrame.recentfiles)
(default it sorts the list) works
this patch was quite difficult to apply. Now my drscript is not working anymore (as I awaited; so I must look, how reanimate it again)
That was quite easy:
creating the list anew:
cnt = DrFrame.recentfiles.GetCount()
recentfiles = list()
for i in range(cnt):
recentfiles.append(DrFrame.recentfiles.GetHistoryFile(i))
added patches, thanks
That code is way too complex. That object behaves like a (read only) sequence, so it is iterable:
recentfiles = list(DrFrame.recentfiles)
That's all what it takes to copy the items into a new list.
Even if you insist on writing a loop yourself your version is more wx-like than Python-like because that object works with `len()` and index access. No need for `GetCount()` and `GetHistoryFile()`.
I didn't know/see it first, that the DrDrame.recentfiles is a sequence itself.