The wxWidgets programming tutorial (8. Wigets part 2)

Author: Jan Bodnar. Link to original: http://zetcode.com/tutorials/wxwidgetstutorial/widgetsII/ (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 (8. Виджеты часть 2).. Translation complete.
Submitted for translation by ber113 19.03.2010 Published 2 years, 2 months ago.

Text

Widgets II

In this chapter, we will continue introducing various other widgets.

wxListBox

A wxListBox widget is used for displaying and working with a list of items. As it's name indicates, it is a rectangle that has a list of strings inside. We could use it for displaying a list of mp3 files, book names, module names of a larger project or names of our friends. A wxListBox can be created in two different states. In a single selection state or a multiple selection state. The single selection state is the default state. There are two significant events in wxListBox. The first one is the wxEVT_COMMAND_LISTBOX_SELECTED event. This event is generated when we select a string in a wxListBox. The second one is the wxEVT_COMMAND_LISTBOX_DOUBLE_CLICKED event. It is generated when we double click an item in a wxListBox. The number of elements inside a wxListBox is limited on GTK platform. According to the documentation, it is currently around 2000 elements. The elements are numbered from zero. Scrollbars are displayed automatically if needed.

listbox = new wxListBox(panel, ID_LISTBOX,

wxPoint(-1, -1), wxSize(-1, -1));

This is the constructor of the listbox widget.

In our example, we have a list box and four buttons. The buttons are used to add, rename, delete and clear all items in the listbox.

wxString str = wxGetTextFromUser(wxT("Add new item"));

if (str.Len() > 0)

m_lb->Append(str);

To add a new string to the listbox, we display a wxGetTextFromUser dialog. We call the Append() method to append string to the listbox.

m_lb->Clear();

To clear all items is the easiest action to do. We just call the Clear() method.

int sel = m_lb->GetSelection();

if (sel != -1) {

m_lb->Delete(sel);

}

To delete an item, we figure out the selected item. Then we call the Delete() method.

Renaming an item requires several steps.

wxString text;

wxString renamed;

We define two local variables.

int sel = listbox->GetSelection();

if (sel != -1) {

text = listbox->GetString(sel);

renamed = wxGetTextFromUser(wxT("Rename item"),

wxT("Rename dialog"), text);

}

We get the selected string and save it to the renamed variable.

if (!renamed.IsEmpty()) {

m_lb->Delete(sel);

m_lb->Insert(renamed, sel);

}

We check whether the renamed variable is empty. This is to avoid inserting empty strings. Then we delete the old item and insert a new one.

wxNotebook

wxNotebook widget joins multiple windows with corresponding tabs. You can position the Notebook widget using the following style flags:

* wxNB_LEFT

* wxNB_RIGHT

* wxNB_TOP

* wxNB_BOTTOM

The default position is wxNB_TOP.

In this example, we have created a notebook widget with three grids. The notebook widget is positioned at the bottom.

wxNotebook *nb = new wxNotebook(this, -1, wxPoint(-1, -1),

wxSize(-1, -1), wxNB_BOTTOM);

Here we create the notebook widget.

nb->AddPage(grid1, wxT("Sheet1"));

nb->AddPage(grid2, wxT("Sheet2"));

nb->AddPage(grid3, wxT("Sheet3"));

We add three grid objects into the notebook widget.

wxScrolledWindow

This is one of the container widgets. It can be useful, when we have a larger area than a window can display. In our example, we demonstrate such a case. We place a large image into our window. When the window is smaller than our image, Scrollbars are displayed automatically.

In our example, we display a picture of a Spis castle.

wxImage::AddHandler(new wxJPEGHandler);

To handle jpg images, we must initiate the wxJPEGHandler.

wxScrolledWindow *sw = new wxScrolledWindow(this);

wxBitmap bmp(wxT("castle.jpg"), wxBITMAP_TYPE_JPEG);

wxStaticBitmap *sb = new wxStaticBitmap(sw, -1, bmp);

We create a scroll window and put a static bitmap into it.

sw->SetScrollbars(10, 10, width/10, height/10);

We set the scrollbars.

sw->Scroll(50,10);

We scroll the window a bit.