Skip to content

Instantly share code, notes, and snippets.

@mrmikejones
Created February 12, 2013 11:27
Show Gist options
  • Save mrmikejones/4761692 to your computer and use it in GitHub Desktop.
Save mrmikejones/4761692 to your computer and use it in GitHub Desktop.
How to set up an "Open Recent" menu in wxWidgets
#include "wx/wx.h"
#include "wx/filename.h"
#include "wx/filehistory.h"
#include "wx/config.h"
#define APP_NAME wxString("File History Example")
class MyApp: public wxApp
{
virtual bool OnInit();
};
enum
{
ID_Quit = 1,
ID_FileOpen,
ID_FileOpenRecent,
ID_About,
};
class wxFileHistory;
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title,
const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnOpen(wxCommandEvent& event);
void OnMRUFile(wxCommandEvent& event);
void OpenFile(wxString filename);
private:
wxFileHistory* mFileHistory;
wxConfig* mConfig;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_FileOpen, MyFrame::OnOpen)
EVT_MENU(ID_About, MyFrame::OnAbout)
EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, MyFrame::OnMRUFile)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame(APP_NAME, wxPoint(50,50), wxSize(450,340));
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
mConfig = new wxConfig(title);
SetMinSize(size);
wxMenu* menuHelp = new wxMenu;
menuHelp->Append( ID_About, "&About..." );
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_FileOpen, "&Open");
wxMenu* menuRecent = new wxMenu;
menuFile->AppendSubMenu(menuRecent, "Open Recent");
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, "E&xit" );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File" );
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
mFileHistory = new wxFileHistory(10);
mFileHistory->UseMenu(menuRecent);
mFileHistory->AddFilesToMenu(menuRecent);
mFileHistory->Load(*mConfig);
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
mFileHistory->Save(*mConfig);
delete mConfig;
delete mFileHistory;
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox("Copyright (c) 2012 Mike Jones\naudioprogrammermike@gmail.com", APP_NAME, wxOK | wxICON_INFORMATION, this);
}
void MyFrame::OnOpen(wxCommandEvent& event)
{
wxFileDialog* OpenDialog = new wxFileDialog(
this, _("Choose a file to open"), wxEmptyString, wxEmptyString,
_("All Files (*.*)|*.*"),
wxFD_OPEN, wxDefaultPosition);
if (OpenDialog->ShowModal() == wxID_OK) // if the user click "Open" instead of "Cancel"
{
wxString CurrentDocPath = OpenDialog->GetPath();
OpenFile(CurrentDocPath);
}
// Clean up after ourselves
OpenDialog->Destroy();
}
void MyFrame::OpenFile(wxString path)
{
wxFileName filename(path);
SetTitle(APP_NAME << wxString(" - ") << filename.GetFullName()); // Set the Title to reflect the file open
mFileHistory->AddFileToHistory(path);
}
void MyFrame::OnMRUFile(wxCommandEvent& event)
{
wxString f(mFileHistory->GetHistoryFile(event.GetId() - wxID_FILE1));
if (!f.empty())
OpenFile(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment