Contents Up Previous Next

wxTab classes overview

Classes: wxTabView, wxPanelTabView, wxTabbedPanel, wxTabbedDialog, wxTabControl

The tab classes provides a way to display rows of tabs (like file divider tabs), which can be used to switch between panels or other information. Tabs are most commonly used in dialog boxes where the number of options is too great to fit on one dialog.

Please note that the preferred class for programming tabbed windows is wxNotebook. The old tab classes are retained for backward compatibility and also to implement wxNotebook on platforms that don't have native tab controls.

The appearance and behaviour of a wxTabbedDialog

The following screenshot shows the appearance of the sample tabbed dialog application.

By clicking on the tabs, the user can display a different set of controls. In the example, the Close and Help buttons remain constant. These two buttons are children of the main dialog box, whereas the other controls are children of panels which are shown and hidden according to which tab is active.

A tabbed dialog may have several layers (rows) of tabs, each being offset vertically and horizontally from the previous. Tabs work in columns, in that when a tab is pressed, it swaps place with the tab on the first row of the same column, in order to give the effect of displaying that tab. All tabs must be of the same width. This is a constraint of the implementation, but it also means that the user will find it easier to find tabs since there are distinct tab columns. On some tabbed dialog implementations, tabs jump around seemingly randomly because tabs have different widths. In this implementation, a tab can always be found on the same column.

Tabs are always drawn along the top of the view area; the implementation does not allow for vertical tabs or any other configuration.

Using tabs

The tab classes provide facilities for switching between contexts by means of 'tabs', which look like file divider tabs.

You must create both a view to handle the tabs, and a window to display the tabs and related information. The wxTabbedDialog and wxTabbedPanel classes are provided for convenience, but you could equally well construct your own window class and derived tab view.

If you wish to display a tabbed dialog - the most common use - you should follow these steps.

  1. Create a new wxTabbedDialog class, and any buttons you wish always to be displayed (regardless of which tab is active).
  2. Create a new wxPanelTabView, passing the dialog as the first argument.
  3. Set the view rectangle with wxTabView::SetViewRect, to specify the area in which child panels will be shown. The tabs will sit on top of this view rectangle.
  4. Call wxTabView::CalculateTabWidth to calculate the width of the tabs based on the view area. This is optional if, for example, you have one row of tabs which does not extend the full width of the view area.
  5. Call wxTabView::AddTab for each of the tabs you wish to create, passing a unique identifier and a tab label.
  6. Construct a number of windows, one for each tab, and call wxPanelTabView::AddTabWindow for each of these, passing a tab identifier and the window.
  7. Set the tab selection.
  8. Show the dialog.

Under Motif, you may also need to size the dialog just before setting the tab selection, for unknown reasons.

Some constraints you need to be aware of:



The following fragment is taken from the file test.cpp.

void MyDialog::Init(void)
  int dialogWidth = 365;
  int dialogHeight = 390;
  wxButton *okButton = new wxButton(this, wxID_OK, "Close", wxPoint(100, 330), wxSize(80, 25));
  wxButton *cancelButton = new wxButton(this, wxID_CANCEL, "Cancel", wxPoint(185, 330), wxSize(80, 25));
  wxButton *HelpButton = new wxButton(this, wxID_HELP, "Help", wxPoint(270, 330), wxSize(80, 25));

  // Note, omit the wxTAB_STYLE_COLOUR_INTERIOR, so we will guarantee a match
  // with the panel background, and save a bit of time.
  wxPanelTabView *view = new wxPanelTabView(this, wxTAB_STYLE_DRAW_BOX);

  wxRectangle rect;
  rect.x = 5;
  rect.y = 70;
  // Could calculate the view width from the tab width and spacing,
  // as below, but let's assume we have a fixed view width.
//  rect.width = view->GetTabWidth()*4 + 3*view->GetHorizontalTabSpacing();
  rect.width = 326;
  rect.height = 250;

  // Calculate the tab width for 4 tabs, based on a view width of 326 and
  // the current horizontal spacing. Adjust the view width to exactly fit
  // the tabs.
  view->CalculateTabWidth(4, TRUE);

  if (!view->AddTab(TEST_TAB_CAT,        wxString("Cat")))

  if (!view->AddTab(TEST_TAB_DOG,        wxString("Dog")))
  if (!view->AddTab(TEST_TAB_GUINEAPIG,  wxString("Guinea Pig")))
  if (!view->AddTab(TEST_TAB_GOAT,       wxString("Goat")))
  if (!view->AddTab(TEST_TAB_ANTEATER,   wxString("Ant-eater")))
  if (!view->AddTab(TEST_TAB_SHEEP,      wxString("Sheep")))
  if (!view->AddTab(TEST_TAB_COW,        wxString("Cow")))
  if (!view->AddTab(TEST_TAB_HORSE,      wxString("Horse")))
  if (!view->AddTab(TEST_TAB_PIG,        wxString("Pig")))
  if (!view->AddTab(TEST_TAB_OSTRICH,    wxString("Ostrich")))
  if (!view->AddTab(TEST_TAB_AARDVARK,   wxString("Aardvark")))
  if (!view->AddTab(TEST_TAB_HUMMINGBIRD,wxString("Hummingbird")))
  // Add some panels
  wxPanel *panel1 = new wxPanel(this, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220), wxTAB_TRAVERSAL);
  (void)new wxButton(panel1, -1, "Press me", wxPoint(10, 10));
  (void)new wxTextCtrl(panel1, -1, "1234", wxPoint(10, 40), wxSize(120, 150));
  view->AddTabWindow(TEST_TAB_CAT, panel1);

  wxPanel *panel2 = new wxPanel(this, -1, wxPoint(rect.x + 20, rect.y + 10), wxSize(290, 220));

  wxString animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
  (void)new wxListBox(panel2, -1, wxPoint(5, 5), wxSize(170, 80), 5, animals);

  (void)new wxTextCtrl(panel2, -1, "Some notes about the animals in this house", wxPoint(5, 100), wxSize(170, 100)),
  view->AddTabWindow(TEST_TAB_DOG, panel2);
  // Don't know why this is necessary under Motif...
#ifdef wx_motif
  this->SetSize(dialogWidth, dialogHeight-20);