Home > Articles > Programming > User Interface (UI)

Like this article? We recommend

Getting Into the Details: The Window

Now let’s move on to some details. The OnInit function of the MyApp class creates an instance of MyFrame, passing the title of the window to the constructor. Simply creating the instance of the class doesn’t actually cause a window to open, however. To open the window, you have to call the instance’s Show function.

If you’re familiar with various windowing-system APIs (such as Win32), you’re likely also familiar with an event loop of sorts that you must write—in the case of Win32, this is the while (GetMessage()) loop. With wxWidgets, thankfully, all of this message-looping junk is abstracted away from you so you don’t have to mess with it. The designers of wxWidgets took an excellent approach whereby the library has the code for the main entry point into the program. To get your own code in place, you derive classes and override functions—always a good way to go with a sound object-oriented library.

The MyFrame class represents your main window, and is derived from wxFrame. The wxFrame class is part of wxWidgets; the MyFrame class is your own class that you create for your application. (You’re free to use whatever name you like.) Inside the MyFrame constructor, you create the controls that will be inside your window. In Listing 1, I did quite a bit of work. Here’s the procedure I used:

  1. Create an icon for the window. (That line came right from one of the samples.)
  2. Create a menu bar. You create menu bars programmatically, even though various operating systems let you specify menus in resource files and attach these resources to your executables. To create the menu bar, I created two drop-down menus: a File menu and a Help menu, each of class wxMenu.
  3. Add a single menu item to each drop-down menu.
  4. Start piecing the menus together by first creating a main menu bar of class wxMenuBar. I appended the two drop-down menu instances to the menu bar instance. Then I called SetMenuBar, which is a function inherited from wxFrame, passing the menu bar instance. Pretty easy.
  5. Create a status bar to put at the bottom of the window. Again, that’s easy: I just called CreateStatusBar, which is a function inherited from wxFrame. I passed in the number 2, which gives the status bar two portions in which text can appear.
  6. Place text in the status bar. This process is trivial: Just call SetStatusText, passing the text to appear and a number representing the portion of the status bar in which to put the text (the first is 0). Like CreateStatusBar, SetStatusText is a member of wxFrame, which means that the function is available throughout your frame functions, making status bar text updates extremely quick.
  7. After creating the menu bar and status bar, create a book control. This is the wxWidgets name for a set of tabbed pages; the class is wxBookCtrl. As shown in Figures 1–3, my sample program has three tabs at the top. To create the book control, you first create a new instance of the wxBookCtrl class. Unlike the main window class, with controls you don’t derive a new class; instead, you just create an instance of the class, passing the owner window instance and a control ID.
  8. After creating the book control, create the first of the three tabbed pages. Each is an instance of wxPanel, which is a general-purpose control for holding other controls. When I created the wxPanel instance, I passed the book control into the constructor, because this panel will be part of the book control. However, after I added some controls to the panel (which I’ll explain next), I still had to add the panel to the book manually by calling the panel’s AddPage function and passing the panel, a name for the tab, and optionally a true value if I wanted this tab to be active when the program starts.
  9. Add the controls. This is the fun part! To add a couple of buttons to the panel, for example, you just create instances of the wxButton class, passing the panel to the constructor along with an ID for the button, text to appear on the button, and position and size information. Here’s a sample call from Listing 1:
    new wxButton( panel, BUTTON1,
     _T("Button &1"), wxPoint(50,30), wxSize(100,30) );

    Notice that the first parameter is the panel I just created. If I weren’t using a book control and instead intended to put these buttons right on the window itself, I would simply pass the instance of MyFrame for the first parameter instead; and since this code is in the MyFrame member code, the first parameter would simply be this, like so:

    new wxButton( this, BUTTON1,
    _T("Button &1"), wxPoint(50,30), wxSize(100,30) );

    Incidentally, some C++ programmers might find it odd to call new but not save the results back into a variable, in this manner:

    somevar = new someclass()

    Normally, if you don’t save the result away, you would end up with a memory leak. But not here. wxWidgets is saving away the instance through the control’s constructor. You can save the instance if you need to use the control later in your own code. In the case of this button, I don’t need to refer to the button elsewhere, but later I create a list box control and I need to access its members; thus, I save away the list box. I’ll show you that process next.

  10. Finish off by adding the panel through AddPage.

Okay, that’s the first panel. The next panel is a list box, which is created in a similar fashion:

  1. Create an array of strings that will be used to fill the list box. (The items in the array are really instances of wxString.) Then create another panel as before.
  2. Create the list box. Creating the list box is much like creating a button, except that in this case I also passed in the list of strings, as well as an optional flag, wxLB_SORT, which means that I wanted the list box to remain sorted. If you choose to leave out this sorting flag, the items will display in the same order as they are in the array. Also, this flag parameter is optional, so if you don’t want any additional flags, don’t pass anything at all.
  3. After passing the second panel to the constructor for the list box, you don’t need to do anything more to create the list box. However, I planned to access the list box later in my code, so I saved the results of the new operator to a variable called listbox1, which I made a member of my MyFrame class.
  4. Add the new panel to the book control as a page by calling AddPage.

For the third and final panel, I decided to demonstrate a really nice feature of wxWidgets. Typically, when you create a program in a windowing environment, the users will have the ability to resize the window. When windows resize, you’ll often want to resize a control inside the window. Look again at the third tab in Figure 4. Notice that the text control has a border that takes up almost the whole window. In fact, if you resize the window, the text control will grow with the window, always taking up most of the window.

Figure 4

Figure 4 The text control grows with the window when you resize.

The wxWidgets library has classes that provide the capabilities to resize any controls you want resized when a containing window or panel is resized. The book control I’m using here has this feature built in by default; if I resize the window, the book control resizes with the window. The text control that I’m going to put on the third panel, however, doesn’t have that capability, so we’ll need to add it.

To allow for automatic resizing, wxWidgets includes several layout classes that automate the resizing of controls. In this case, we’ll use the wxBoxSizer class, which lets you arrange controls on a panel in a stacked or horizontal fashion; certain controls of your choosing maintain their size while the remaining controls grow or shrink with the window.

Let’s get started:

  1. Create a panel and pass the wxBoxSizer instance to the panel by calling the SetSizer function:
    panel = new wxPanel(book);
    wxBoxSizer *mysizer = new wxBoxSizer( wxVERTICAL );
    panel->SetSizer(mysizer);

    I arbitrarily chose a vertical pattern, although horizontal would work just as well, since we have only one control.

  2. Create the text control:
    textLog = new wxTextCtrl(panel, TEXTBOX1, _T("Log\n"),
    wxPoint(0, 250), wxSize(100, 50), wxTE_MULTILINE);
  3. Most of these parameters are just like those with the button and list box. The third parameter is the initial text to put inside the control. The final parameter, wxTE_MULTILINE, means that we want the text control to be able to hold multiple lines of text, not just a single line.
  4. Add the text control into the sizer:
    mysizer->Add(textLog, 1, wxEXPAND | wxALL, 5);

    Let’s examine those parameters a little more closely:

    • The first parameter is the text control.
    • The second parameter is a proportion value. When you have multiple controls stacked in a single sizer, the number for the second parameter is the ratio of the sum, representing how much space the control should occupy. For example, if you have two controls stacked, and one control takes up 3/4 of the space and the other takes up 1/4, you would specify 3 for the first control and 1 for the second control in each respective call to mysizer->Add(). Since the sum of 3 and 1 is 4, the first control would get 3/4 of the space, and the second would get 1/4 of the space. (If you want any control to be fixed in size, pass 0. Such a control would get a fixed size, and the other controls would be divided up with their respective proportions within the remaining space.)
    • The third parameter is a set of flags. The first flag I’m passing is wxEXPAND, which means that the control will be expanded throughout its space. (I could have used wxSHAPED, which would expand the control as much as possible while maintaining a constant ratio of its length to its width.) The second flag I’m passing is wxALL, which refers to the next parameter; this next and final parameter is the size of the border to place around the controls. I’m placing 5 pixels around the control so its border doesn’t go right up against the edge of the containing panel. The wxALL flag means that this value of 5 pixels applies to all four sides. (Other options, which can be combined with OR (|) operators, are wxTOP, wxBOTTOM, wxLEFT, and wxRIGHT.)
  5. After adding the text control to the panel, add the panel to the book control as before, by calling AddPage.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020