Contents Up Previous Next

Tab classes overview

Classes: wxTabView, wxPanelTabView, wxTabbedPanel, wxTabbedDialogBox, wxTabControl

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

To use them, include the wxtab.h header file and link with the wxtab.lib (or libwxtab_motif.a) library.

You must create both a view to handle the tabs, and a window to display the tabs and related information. The wxTabbedDialogBox 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 wxTabbedDialogBox 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:

Example


Example

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

void MyFrame::TestTabbedDialog(void)
{
  int dialogWidth = 365;
  int dialogHeight = 400;
  
  wxTabbedDialogBox *dialog =
    new wxTabbedDialogBox(this, "Tabbed Dialog Box", TRUE, -1, -1, 365, 400);
  
  wxButton *okButton = new wxButton(dialog, (wxFunction)GenericOk, "Close",
    230, 100, 80, 25);
  wxButton *cancelButton = new wxButton(dialog, NULL, "Help", 230, 130, 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(dialog, 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 = 300;
  
  view->SetViewRect(rect);

  // 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")))
    return;
    
  if (!view->AddTab(TEST_TAB_DOG,        wxString("Dog")))
    return;
  if (!view->AddTab(TEST_TAB_GUINEAPIG,  wxString("Guinea Pig")))
    return;
  if (!view->AddTab(TEST_TAB_GOAT,       wxString("Goat")))
    return;
  if (!view->AddTab(TEST_TAB_ANTEATER,   wxString("Ant-eater")))
    return;
  if (!view->AddTab(TEST_TAB_SHEEP,      wxString("Sheep")))
    return;
  if (!view->AddTab(TEST_TAB_COW,        wxString("Cow")))
    return;
  if (!view->AddTab(TEST_TAB_HORSE,      wxString("Horse")))
    return;
  if (!view->AddTab(TEST_TAB_PIG,        wxString("Pig")))
    return;
  if (!view->AddTab(TEST_TAB_OSTRICH,    wxString("Ostrich")))
    return;
  if (!view->AddTab(TEST_TAB_AARDVARK,   wxString("Aardvark")))
    return;
  if (!view->AddTab(TEST_TAB_HUMMINGBIRD,wxString("Hummingbird")))
    return;
    
  // Add some panels
  wxPanel *panel1 = new wxPanel(dialog, rect.x + 20, rect.y + 10, 200, 250);
  (void)new wxButton(panel1, NULL, "Press me");
  panel1->NewLine();
  (void)new wxText(panel1, NULL, "Input:", "1234", -1, -1, 120);
  
  view->AddTabWindow(TEST_TAB_CAT, panel1);

  wxPanel *panel2 = new wxPanel(dialog, rect.x + 20, rect.y + 10, 200, 250);
  panel2->SetLabelPosition(wxVERTICAL);
  
  char *animals[] = { "Fox", "Hare", "Rabbit", "Sabre-toothed tiger", "T Rex" };
  (void)new wxListBox(panel2, NULL, "List of animals", wxSINGLE,
    5, 5, 170, 80, 5, animals);

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

  view->SetTabSelection(TEST_TAB_CAT);
  
  dialog->Show(TRUE);  
}