summaryrefslogtreecommitdiffstats
path: root/views/examples/tabbed_pane_example.h
blob: f49d15c065c86d753e8bef173c4bbd94566427f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_EXAMPLES_TABBED_PANE_EXAMPLE_H_
#define VIEWS_EXAMPLES_TABBED_PANE_EXAMPLE_H_
#pragma once

#include "base/string_util.h"
#include "views/controls/button/text_button.h"
#include "views/examples/example_base.h"

namespace examples {

// A TabbedPane example tests adding/removing/selecting tabs.
class TabbedPaneExample : public ExampleBase,
                          public views::ButtonListener,
                          views::TabbedPane::Listener {
 public:
  explicit TabbedPaneExample(ExamplesMain* main) : ExampleBase(main) {}

  virtual ~TabbedPaneExample() {}

  virtual std::wstring GetExampleTitle() {
    return L"Tabbed Pane";
  }

  virtual void CreateExampleView(views::View* container) {
    tabbed_pane_ = new views::TabbedPane();
    add_ = new views::TextButton(this, L"Add");
    add_at_ = new views::TextButton(this, L"Add At 1");
    remove_at_ = new views::TextButton(this, L"Remove At 1");
    select_at_ = new views::TextButton(this, L"Select At 1");

    views::GridLayout* layout = new views::GridLayout(container);
    container->SetLayoutManager(layout);

    const int tabbed_pane_column = 0;
    views::ColumnSet* column_set = layout->AddColumnSet(tabbed_pane_column);
    column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
                          1.0f, views::GridLayout::USE_PREF, 0, 0);
    layout->StartRow(1 /* expand */, tabbed_pane_column);
    layout->AddView(tabbed_pane_);

    // Create a few tabs with a button first.
    AddButton(L"Tab 1");
    AddButton(L"Tab 2");

    // Add control buttons horizontally.
    const int button_column = 1;
    column_set = layout->AddColumnSet(button_column);
    for (int i = 0; i < 4; i++) {
      column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
                            1.0f, views::GridLayout::USE_PREF, 0, 0);
    }

    layout->StartRow(0 /* no expand */, button_column);
    layout->AddView(add_);
    layout->AddView(add_at_);
    layout->AddView(remove_at_);
    layout->AddView(select_at_);
  }

 private:
  // ButtonListener overrides.
  virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
    if (sender == add_) {
      AddButton(L"Added");
    } else if (sender == add_at_) {
      const std::wstring label = L"Added at 1";
      tabbed_pane_->AddTabAtIndex(1, label,
                                  new views::TextButton(NULL, label), true);
    } else if (sender == remove_at_) {
      if (tabbed_pane_->GetTabCount() > 1)
        delete tabbed_pane_->RemoveTabAtIndex(1);
    } else if (sender == select_at_) {
      if (tabbed_pane_->GetTabCount() > 1)
        tabbed_pane_->SelectTabAt(1);
    }
    PrintStatus();
  }

  // TabbedPane::Listener overrides.
  virtual void TabSelectedAt(int index) {
    // Just print the status when selection changes.
    PrintStatus();
  }

  // Print the status of the tab in the status area.
  void PrintStatus() {
    ExampleBase::PrintStatus(L"Tab Count:%d, Selected Tab:%d",
                             tabbed_pane_->GetTabCount(),
                             tabbed_pane_->GetSelectedTabIndex());
  }

  void AddButton(const std::wstring& label) {
    views::TextButton* button = new views::TextButton(NULL, label);
    tabbed_pane_->AddTab(label, button);
  }

  // The tabbed pane to be tested.
  views::TabbedPane* tabbed_pane_;

  // Control buttons to add, remove or select tabs.
  views::Button* add_, *add_at_, *remove_at_, *select_at_;

  DISALLOW_COPY_AND_ASSIGN(TabbedPaneExample);
};

}  // namespace examples

#endif  // VIEWS_EXAMPLES_TABBED_PANE_EXAMPLE_H_