About Dialog

Introduction

Applications usually display information such as their developers, version, copyright holders and license in a dialog. wxWidgets provides dialogs that facilitate the implementation of an "About" dialog.

The wxAboutBox (wxWidgets: Dialogs (wxAboutBox)) function will display a dialog containing the application info passed in as argument. The dialog class used will depend on the information provided and the platform the application is running on. If the native dialog is able to display all the information provided it will be used, if not then the wxGenericAboutBox (wxWidgets: Dialogs (wxGenericAboutBox)) function will be used.

Native About Dialog Example

We'll start by showing an example of an About dialog that uses the native dialog on Windows platforms.

The example is a simple modification of the MinimalApp2 example we presented in the minimal application tutorial. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/Dialogs/WxAboutBox1.

The code is shown below. The following changes were made:

  1. A menu was added to launch the About dialog.
  2. The wxAboutBox is used to create the About dialog.
 File: Dialogs/WxAboutBox1/src/WxAboutBox1Frame.h
#ifndef _TUTORIALS_WXWIDGETS_WXABOUTBOX1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXABOUTBOX1FRAME_H_

#include <wx/frame.h>

class WxAboutBox1Frame : public wxFrame
{
public:
    WxAboutBox1Frame(const wxString& title);

private:
    void OnAbout(wxCommandEvent& evt);

    wxDECLARE_EVENT_TABLE();
};

#endif
 File: Dialogs/WxAboutBox1/src/WxAboutBox1Frame.cpp
#include "WxAboutBox1Frame.h"
#include <wx/menu.h>
#include <wx/aboutdlg.h>

WxAboutBox1Frame::WxAboutBox1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    // The About dialog is usually accessible from
    // the "Help" menu so we create one.
    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar(menuBar);
}

// Event handler for the About menu item. It will
// display the About dialog.
void WxAboutBox1Frame::OnAbout(wxCommandEvent& evt)
{
    // Create the About dialog.
    // On Windows, since there is no website, icon or license,
    // the simple version of the dialog, based on a MessageBox,
    // will be used.
    wxAboutDialogInfo info;

    info.SetName(L"WxAboutBox1");
    info.SetVersion(L"1.0.0");
    info.SetDescription(L"A sample application to demonstrate the wxWidgets wxAboutBox function.");
    info.SetCopyright(L"Copyright (c) 2015 Xavier Leclercq");

    wxArrayString developers;
    developers.Add(L"Xavier Leclercq");
    info.SetDevelopers(developers);

    wxAboutBox(info);
}

// Add the event handler for the About menu item
// to the event table.
wxBEGIN_EVENT_TABLE(WxAboutBox1Frame, wxFrame)
    EVT_MENU(wxID_ABOUT, WxAboutBox1Frame::OnAbout)
wxEND_EVENT_TABLE()

The resulting dialog is shown in Figure 1.

Figure 1: The WxAboutBox1 Application

The other source files don't have any significants changes compared to the MinimalApp2 but are shown here for completeness.

 File: Dialogs/WxAboutBox1/src/WxAboutBox1App.h
#ifndef _TUTORIALS_WXWIDGETS_WXABOUTBOX1APP_H_
#define _TUTORIALS_WXWIDGETS_WXABOUTBOX1APP_H_

#include <wx/app.h>

class WxAboutBox1App : public wxApp
{
public:
    virtual bool OnInit();
};

#endif
 File: Dialogs/WxAboutBox1/src/WxAboutBox1App.cpp
#include "WxAboutBox1App.h"
#include "WxAboutBox1Frame.h"

wxIMPLEMENT_APP(WxAboutBox1App);

bool WxAboutBox1App::OnInit()
{
    WxAboutBox1Frame* frame = new WxAboutBox1Frame("WxAboutBox1");
    frame->Show(true);
    return true;
}

wxGenericAboutBox Example

Our second example will show the wxGenericAboutBox dialog. We are still calling the wxAboutBox function but this time it will use the generic dialog as the information doesn't fit in the native dialog.

We modify the code of the previous example as shown below. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/Dialogs/WxAboutBox2.

 File: Dialogs/WxAboutBox2/src/WxAboutBox2Frame.cpp
#include "WxAboutBox2Frame.h"
#include <wx/menu.h>
#include <wx/aboutdlg.h>

WxAboutBox2Frame::WxAboutBox2Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    // The About dialog is usually accessible from
    // the "Help" menu so we create one.
    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    menuBar->Append(menuHelp, "&Help");
    SetMenuBar(menuBar);
}

// Event handler for the About menu item. It will
// display the About dialog.
void WxAboutBox2Frame::OnAbout(wxCommandEvent& evt)
{
    // Create the About dialog.
    // On Windows, the wxGenericAboutBox will be used
    // because it is used when an icon, a website or
    // a license is specified.
    wxAboutDialogInfo info;

    info.SetName(L"WxAboutBox2");
    info.SetVersion(L"1.0.0");
    info.SetDescription(L"A sample application to demonstrate the wxWidgets wxAboutBox function.");
    info.SetCopyright(L"Copyright (c) 2015 Xavier Leclercq");
    info.SetWebSite(L"http://www.needfulsoftware.com/Documentation/WxWidgetsAboutDialog");

    wxArrayString developers;
    developers.Add(L"Xavier Leclercq");
    info.SetDevelopers(developers);

    wxArrayString docWriters;
    docWriters.Add(L"Xavier Leclercq");
    info.SetDocWriters(docWriters);

    info.SetLicence(
        L"Permission is hereby granted, free of charge, to any person obtaining a "
        L"copy of this software and associated documentation files (the \"Software\"), "
        L"to deal in the Software without restriction, including without limitation "
        L"the rights to use, copy, modify, merge, publish, distribute, sublicense, "
        L"and / or sell copies of the Software, and to permit persons to whom the "
        L"Software is furnished to do so, subject to the following conditions :\n"
        L"\n"
        L"The above copyright notice and this permission notice shall be included in "
        L"all copies or substantial portions of the Software.\n"
        L"\n"
        L"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR "
        L"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, "
        L"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL "
        L"THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER "
        L"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING "
        L"FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS "
        L"IN THE SOFTWARE.");

    wxAboutBox(info);
}

// Add the event handler for the About menu item
// to the event table.
wxBEGIN_EVENT_TABLE(WxAboutBox2Frame, wxFrame)
    EVT_MENU(wxID_ABOUT, WxAboutBox2Frame::OnAbout)
wxEND_EVENT_TABLE()

The resulting dialog is shown in Figure 2.

Figure 2: The WxAboutBox2 Application

The rest of the source files are unchanged but shown here for completeness.

 File: Dialogs/WxAboutBox2/src/WxAboutBox2Frame.h
#ifndef _TUTORIALS_WXWIDGETS_WXABOUTBOX2FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXABOUTBOX2FRAME_H_

#include <wx/frame.h>

class WxAboutBox2Frame : public wxFrame
{
public:
    WxAboutBox2Frame(const wxString& title);

private:
    void OnAbout(wxCommandEvent& evt);

    wxDECLARE_EVENT_TABLE();
};

#endif
 File: Dialogs/WxAboutBox2/src/WxAboutBox2App.h
#define _TUTORIALS_WXWIDGETS_WXABOUTBOX2APP_H_
#include <wx/app.h>

class WxAboutBox2App : public wxApp
{
public:
    virtual bool OnInit();
};

#endif
 File: Dialogs/WxAboutBox2/src/WxAboutBox2App.cpp
#include "WxAboutBox2App.h"
#include "WxAboutBox2Frame.h"

wxIMPLEMENT_APP(WxAboutBox2App);

bool WxAboutBox2App::OnInit()
{
    WxAboutBox2Frame* frame = new WxAboutBox2Frame("WxAboutBox2");
    frame->Show(true);
    return true;
}