The Event Handlers

team bbl


As you may have noticed, the event handler functions in MyFrame are not virtual and should not be virtual. How, then, are they called? The answer lies in the event table, as follows.

 // Event table for MyFrame BEGIN_EVENT_TABLE(MyFrame, wxFrame)     EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)     EVT_MENU(wxID_EXIT,  MyFrame::OnQuit) END_EVENT_TABLE() 

An event table, placed in a class's implementation file, tells wxWidgets how events coming from the user or from other sources are routed to member functions.

With the event table shown previously, mouse clicks on menu items with the identifiers wxID_EXIT and wxID_ABOUT are routed to the functions MyFrame::OnQuit and MyFrame::OnAbout, respectively. EVT_MENU is just one of many event table macros you can use to tell wxWidgets what kind of event should be routed to what function. The identifiers used here are predefined by wxWidgets, but you will often define your own identifiers, using enums, consts, or preprocessor defines.

This kind of event table is a static way of routing events, and cannot be changed at runtime. In the next chapter, we'll describe how to set up dynamic event handlers.

While we're dealing with event tables, let's see the two functions we're using as event handlers.

 void MyFrame::OnAbout(wxCommandEvent& event) {     wxString msg;     msg.Printf(wxT("Hello and welcome to %s"),                wxVERSION_STRING);     wxMessageBox(msg, wxT("About Minimal"),                  wxOK | wxICON_INFORMATION, this); } void MyFrame::OnQuit(wxCommandEvent& event) {     // Destroy the frame     Close(); } 

MyFrame::OnAbout shows a message box when the user clicks on the About menu item. wxMessageBox takes a message, a caption, a combination of styles, and a parent window.

MyFrame::OnQuit is called when the user clicks on the Quit menu item, thanks to the event table. It calls Close to destroy the frame, triggering the shutdown of the application, because there are no other frames. In fact, Close doesn't directly destroy the frameit generates a wxEVT_CLOSE_WINDOW event, and the default handler for this event destroys the frame using wxWindow::Destroy.

There's another way the frame can be closed and the application shut downthe user can click on the close button on the frame, or select Close from the system (or window manager) menu. How does OnQuit get called in this case? Well, it doesn'tinstead, wxWidgets sends a wxEVT_CLOSE_WINDOW event to the frame via Close (as used in OnQuit). wxWidgets handles this event by default and destroys the window. Your application can override this behavior and provide its own event handlerfor example, if you want to ask the user for confirmation before closing. For more details, please see Chapter 4, "Window Basics."

This sample doesn't need it, but most applications should provide an OnExit function in its application class to clean up data structures before quitting. Note that this function is only called if OnInit returns true.

    team bbl



    Cross-Platform GUI Programming with wxWidgets
    Cross-Platform GUI Programming with wxWidgets
    ISBN: 0131473816
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net