Search code examples
c++user-interfacewxwidgets

Must I declare own wxFrame class?


I'm creating simple c++ application with user interface. I have written own classes with needed mechanics, and now I must display some of its components using wxWidgets. MyApp contains objects of that classes, but they are also needed in MyFrame when handling events. It would be much more comfortable without passing pointers to that objects to MyFrame and implement everything in MyApp::OnInit function. Is that possible?

// should contain informations about game state, players, gameboard, etc
class MyApp : public wxApp {
    GameBoard gb;
    Player player;
    bool lightTheme;
public:
    virtual bool OnInit();
};
// should contain only window layout, button events, etc
class MyFrame : public wxFrame {
    GameBoard *gb;
    Player *player;
    bool *lightTheme;

    std::vector<wxStaticBitmap*> cardImages;
// some events here
    void displayImages(wxGridSizer*);
public:
    MyFrame(GameBoard*, Player*, bool*);
};

Solution

  • To answer your title question, you don't have to declare MyFrame and can just create and directly use wxFrame and use Bind() to connect the different event handlers. In all but the simplest situations, it's typically convenient to have a MyFrame class centralizing the data and event handlers for the window, but it is not required by any means.

    If you decide to keep everything in MyApp instead, you may find wxGetApp() useful for accessing it from various places in your code.