What I mean by this is there a way to separate your MenuBar code from your wxFrame custom class. I would like to have a custom MenuBar class that holds all my wxMenuBar code and just add it to my custom wxFrame class. I just don't like having everything in MyFrame class.
I understand there is probably a dozen different ways to do this, How do you guys prefer to do this?
When I tried doing this I created a memory leak.
CODE:
Main.cpp
Code: Select all
#include <wx/wx.h>
#include "MainFrame.h"
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MainFrame *frame = new MainFrame(NULL);
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
Code: Select all
#pragma once
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/menu.h>
#include <wx/toolbar.h>
#include <wx/string.h>
#include <wx/stattext.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include <wx/frame.h>
#include "MainMenu.h"
class MainFrame : public wxFrame
{
public:
MainFrame(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Test Program"),
const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 497),
long style = wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL);
~MainFrame();
void MenuExit(wxCommandEvent& event);
protected:
MainMenu* mainmenu;
wxStaticText* Text_Title;
private:
};
Code: Select all
#include "MainFrame.h"
MainFrame::MainFrame(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style)
: wxFrame(parent, id, title, pos, size, style)
{
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
//Layout
wxBoxSizer* MainSizer;
MainSizer = new wxBoxSizer(wxVERTICAL);
//Menu
mainmenu = new MainMenu(0);
this->SetMenuBar(mainmenu->GetMenuBar());
//Frame objects
Text_Title = new wxStaticText(this, wxID_ANY, wxT("This is a long title name for this porogram"),
wxDefaultPosition, wxDefaultSize, 0);
Text_Title->Wrap(-1);
MainSizer->Add(Text_Title, 0, wxALIGN_CENTER_HORIZONTAL, 5);
//Set objects to layout
this->SetSizer(MainSizer);
this->Layout();
this->Centre(wxBOTH);
//Events
Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::MenuExit));
Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(MainFrame::MenuExit));
}
MainFrame::~MainFrame()
{
}
void MainFrame::MenuExit(wxCommandEvent& event)
{
Close(true);
}
Code: Select all
#pragma once
#include <wx/menu.h>
class MainMenu : public wxMenuBar
{
public:
MainMenu(long style);
~MainMenu();
wxMenuBar* GetMenuBar();
protected:
wxMenuBar* menu;
wxMenu* m_file;
wxMenu* m_about;
private:
};
inline MainMenu::MainMenu(long style)
{
menu = new wxMenuBar(style);
m_file = new wxMenu();
wxMenuItem* mi_exit;
mi_exit = new wxMenuItem(m_file, wxID_EXIT, wxString("Exit"), wxEmptyString, wxITEM_NORMAL);
m_file->Append(mi_exit);
menu->Append(m_file, wxT("File"));
}
inline MainMenu::~MainMenu()
{
delete m_file;
delete m_about;
delete menu;
delete this;
}
inline wxMenuBar* MainMenu::GetMenuBar()
{
return menu;
}
Thanks
Matthew