The Frame Constructor

team bbl


Finally, we have the frame constructor, which implements the frame icon, a menu bar, and a status bar.

 #include "mondrian.xpm" MyFrame::MyFrame(const wxString& title)        : wxFrame(NULL, wxID_ANY, title) {     // Set the frame icon     SetIcon(wxIcon(mondrian_xpm));     // Create a menu bar     wxMenu *fileMenu = new wxMenu;     // The "About" item should be in the help menu     wxMenu *helpMenu = new wxMenu;     helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),                      wxT("Show about dialog"));     fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),                      wxT("Quit this program"));     // Now append the freshly created menu to the menu bar...     wxMenuBar *menuBar = new wxMenuBar();     menuBar->Append(fileMenu, wxT("&File"));     menuBar->Append(helpMenu, wxT("&Help"));     // ... and attach this menu bar to the frame     SetMenuBar(menuBar);     // Create a status bar just for fun     CreateStatusBar(2);     SetStatusText(wxT("Welcome to wxWidgets!")); } 

This constructor calls the base constructor with the parent window (none, hence NULL), window identifier, and title. The identifier argument is wxID_ANY, which tells wxWidgets to generate an identifier itself. The base constructor creates the actual window associated with the C++ instanceanother way to achieve this is to call the default constructor of the base class, and then explicitly call wxFrame::Create from within the MyFrame constructor.

Small bitmaps and icons can be implemented using the XPM format on all platforms. XPM files have valid C++ syntax and so can be included as shown previously; the SetIcon line creates an icon on the stack using the C++ variable mondrian_xpm defined in mondrian.xpm, and associates it with the frame.

The menu bar is created next. Menu items are added using the identifier (such as the standard identifier wxID_ABOUT), the label to be displayed, and a help string to be shown on the status bar. Within each label, a mnemonic letter is marked by a preceding ampersand, and an accelerator is preceded by the tab character (\t). A mnemonic is the letter a user presses to highlight a particular item when the menu is displayed. An accelerator is a key combination (such as Alt+X) that can be used to perform that action without showing the menu at all.

The last thing that the constructor does is to create a status bar with two fields at the bottom of the frame and set the first field to the string "Welcome to wxWidgets!"

    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