To read/write into the menu option, I passed the pointer of the wxFrame (MainFrame) during wxFrame initialization to an internally maintained class meant to hold preferences and settings. I then used GetMenuBar() to get the menu bar and get the values of options exposed in the menu bar itself when needed. However, it results to a segmentation fault. Using wxGetTopLevelWindow() to the control component calling one of my preference writing functions SudokuControl::TogglePencilMode proves that I'm stroring an invalid wxFrame pointer address (and that subsequent calls to wxGetTopLevelWindow() when the function is called again proves that the correct address doesnt change).
So, at what point should I get a pointer to the main wxFrame?
(Sorry this is so long.. I posted a question last time and was requested code. This is more or less part of the actual code im currently writing)
main.cc
Code: Select all
MainFrame::MainFrame() : wxFrame(NULL, wxID_ANY, "Hello World"){
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuInput = new wxMenu;
menuInput->AppendCheckItem(ID_PencilMode, "&Pen Mode\tP");
menuInput->Check(ID_PencilMode, true);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuInput, "&Input");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Test app for wxWidgets :)");
Bind(wxEVT_MENU, &MainFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &MainFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MainFrame::OnExit, this, wxID_EXIT);
wxPanel *panel = new wxPanel(this, wxID_ANY);
wxSizer *grid = new wxGridSizer(3);
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
grids[x][y] = new SudokuGrid(panel, wxPoint(x,y), wxSize(3, 3));
grid->Add(grids[x][y], wxSizerFlags(0).Center().Border());
}
}
panel->SetSizer(grid);
panel->SetBackgroundColour(wxColour(0, 0, 0));
wxSizer *frameSizer = new wxGridSizer(1);
frameSizer->Add(panel, wxSizerFlags(0).Center());
SetSizerAndFit(frameSizer);
sudokuControl.sss(wxGetTopLevelParent(this));
}
void MainFrame::OnExit(wxCommandEvent& WXUNUSED(event)){
Close(true);
}
void MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event)){
wxMessageBox("This is a work in progress sudoku app", "About this program", wxOK|wxICON_INFORMATION);
}
void MainFrame::OnHello(wxCommandEvent& WXUNUSED(event)){
wxLogMessage(wxT("Hiiiii!"));
}
void MainFrame::hi(){
wxLogMessage(wxT("oh hey there"));
}
Code: Select all
class MainApp : public wxApp{
public:
virtual bool OnInit();
};
class MainFrame : public wxFrame{
public:
MainFrame();
void hi();
private:
SudokuGrid *grids[3][3];
void OnHello(wxCommandEvent& WXUNUSED(event));
void OnExit(wxCommandEvent& WXUNUSED(event));
void OnAbout(wxCommandEvent& WXUNUSED(event));
};
Code: Select all
void SudokuControl::TogglePencilMode(wxWindow *window){
//wxMessageBox("toggling");
((MainFrame*) menuPencilMode)->hi();
wxFrame *fr = (wxFrame*)wxGetTopLevelParent(window);
if( fr != menuPencilMode){
wxMessageBox("they are unequal");
menuPencilMode = (MainFrame*) fr;
//((MainFrame*) menuPencilMode)->hi();
}
wxMenuBar* menu = fr->GetMenuBar();
//wxMessageBox("toggle middle");
menu->Check(ID_PencilMode, !(menu->IsChecked(ID_PencilMode)));
//wxMessageBox("toggle end");
}
bool SudokuControl::PencilMode(){
//wxMessageBox("tilted try");
return menuPencilMode->GetMenuBar()->IsChecked(ID_PencilMode);
//wxMessageBox("tilted done");
}
void SudokuControl::sss(wxWindow *topLevel){
menuPencilMode = (wxFrame*) topLevel;
((MainFrame*) menuPencilMode)->hi();
}