The wxWidgets programming tutorial (6. Dialogs)

Author: Jan Bodnar. Link to original: http://zetcode.com/tutorials/wxwidgetstutorial/dialogs/ (English).
A more in-depth tutorial covering menus, toolbars, layout, events, standard and custom dialogs, common and custom controls, drag & drop, and drawing with device contexts, written by Jan Bodnar. This is a wxWidgets tutorial for the C++ programming language. wxWidgets is a cross platform toolkit or framework for creating C++ GUI applications. After reading this tutorial, you will be able to program non trivial wxWidgets applications.

Translations of this material:

into Russian: Руководство по программированию с wxWidgets (6. Диалоги).. Translation complete.
Submitted for translation by ber113 18.03.2010 Published 2 years, 2 months ago.

Text

Dialogs

Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.

There are essentially two types of dialogs. Predefined dialogs and custom dialogs.

Predefined dialogs

Predefined dialogs are dialogs available in the wxWidgets toolkit. These are dialogs for common programming tasks like showing text, receiving input , loading and saving files etc. They save programmer's time and enhance using some standard behaviour.

Message dialogs

Message dialogs are used to show messages to the user. They are customizable. We can change icons and buttons that will be shown in a dialog.

In our example, we have created four buttons and put them in a grid sizer. These buttons will show four different dialog windows. We create them by specifying different style flags.

wxMessageDialog *dial = new wxMessageDialog(NULL,

wxT("Error loading file"), wxT("Error"), wxOK | wxICON_ERROR);

dial->ShowModal();

The creation of the message dialog is simple. We set the dialog to be a toplevel window by providing NULL as a parent. The two strings provide the message text and the dialog title. We show an ok button and an error icon by specifying the wxOK and wxICON_ERROR flags. To show the dialog on screen, we call the ShowModal() method.

wxFileDialog

This is a common dialog for opening and saving files.

In our example, we display a open file menu item and a simple multiline text control. If we click on the open file menu item a wxFileDialog is displayed. We can load some simple text files into the text control.

tc = new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1),

wxSize(-1, -1), wxTE_MULTILINE);

We load text files into this text control.

wxFileDialog * openFileDialog = new wxFileDialog(this);

Here we create a wxFileDialog. We use the default parameters. (The open file dialog is the default dialog.)

if (openFileDialog->ShowModal() == wxID_OK){

wxString fileName = openFileDialog->GetPath();

tc->LoadFile(fileName);

}

Here we show the dialog. We get the selected file name and load the file into the text control.

wxFontDialog

This is a common dialog for choosing a font.

In this example, we will change the font of a static text example.

st = new wxStaticText(panel, wxID_ANY, wxT("The Agoge"),

wxPoint(20, 20));

Here we display a static text on the panel. We will change it's font using the wxFontDialog.

wxFontDialog *fontDialog = new wxFontDialog(this);

if (fontDialog->ShowModal() == wxID_OK) {

st->SetFont(fontDialog->GetFontData().GetChosenFont());

}

In these code lines, we show the font dialog. Then we get the choosen font. And finally, we change the font of the static text, we created earlier.

A custom dialog

In the next example we create a custom dialog. An image editing application can change a color depth of a picture. To provide this functionality, we could create a suitable custom dialog.

This example is a dialog based application. We illustrate, how to create a custom dialog.

class CustomDialog : public wxDialog

A custom dialog is based on the wxDialog class.

wxStaticBox *st = new wxStaticBox(panel, -1, wxT("Colors"),

wxPoint(5, 5), wxSize(240, 150));

wxRadioButton *rb = new wxRadioButton(panel, -1,

wxT("256 Colors"), wxPoint(15, 30), wxDefaultSize, wxRB_GROUP);

Note that wxStaticBox widget must be created before the widgets that it contains, and that those widgets should be siblings, not children, of the static box.

ShowModal();

Destroy();

To show the dialog on the screen, we call the ShowModal() method. To clear the dialog from the memory, we call the Destroy() method.