summaryrefslogtreecommitdiff
path: root/chromium/ui/views/examples/combobox_example.cc
blob: 178a465e430c6d196bfc41e4eb48906b74c92f2a (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
// Copyright (c) 2012 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.

#include "ui/views/examples/combobox_example.h"

#include <memory>

#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/views/controls/combobox/combobox.h"
#include "ui/views/examples/examples_window.h"
#include "ui/views/layout/box_layout.h"

namespace views {
namespace examples {

namespace {

// A combobox model implementation that generates a list of "Item <index>".
class ComboboxModelExample : public ui::ComboboxModel {
 public:
  ComboboxModelExample() = default;
  ~ComboboxModelExample() override = default;

 private:
  // ui::ComboboxModel:
  int GetItemCount() const override { return 10; }
  base::string16 GetItemAt(int index) override {
    return base::UTF8ToUTF16(base::StringPrintf("%c item", 'A' + index));
  }

  DISALLOW_COPY_AND_ASSIGN(ComboboxModelExample);
};

}  // namespace

ComboboxExample::ComboboxExample() : ExampleBase("Combo Box") {}

ComboboxExample::~ComboboxExample() = default;

void ComboboxExample::CreateExampleView(View* container) {
  combobox_ = new Combobox(std::make_unique<ComboboxModelExample>());
  combobox_->set_listener(this);
  combobox_->SetSelectedIndex(3);

  auto* disabled_combobox =
      new Combobox(std::make_unique<ComboboxModelExample>());
  disabled_combobox->set_listener(this);
  disabled_combobox->SetSelectedIndex(4);
  disabled_combobox->SetEnabled(false);

  container->SetLayoutManager(std::make_unique<BoxLayout>(
      BoxLayout::Orientation::kVertical, gfx::Insets(10, 0), 5));
  container->AddChildView(combobox_);
  container->AddChildView(disabled_combobox);
}

void ComboboxExample::OnPerformAction(Combobox* combobox) {
  DCHECK_EQ(combobox, combobox_);
  PrintStatus("Selected: %s",
              base::UTF16ToUTF8(
                  combobox->model()->GetItemAt(combobox->GetSelectedIndex()))
                  .c_str());
}

}  // namespace examples
}  // namespace views