summaryrefslogtreecommitdiff
path: root/chromium/ui/views/examples/combobox_example.cc
blob: 2ee32316b375a1165e33e7ef82c905eebd8eb0e8 (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
// 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 <utility>

#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_view.h"
#include "ui/views/layout/fill_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(const ComboboxModelExample&) = delete;
  ComboboxModelExample& operator=(const ComboboxModelExample&) = delete;
  ~ComboboxModelExample() override = default;

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

}  // namespace

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

ComboboxExample::~ComboboxExample() = default;

void ComboboxExample::CreateExampleView(View* container) {
  container->SetLayoutManager(std::make_unique<FillLayout>());

  auto view =
      Builder<BoxLayoutView>()
          .SetOrientation(BoxLayout::Orientation::kVertical)
          .SetInsideBorderInsets(gfx::Insets(10, 0))
          .SetBetweenChildSpacing(5)
          .AddChildren(
              {Builder<Combobox>()
                   .CopyAddressTo(&combobox_)
                   .SetOwnedModel(std::make_unique<ComboboxModelExample>())
                   .SetSelectedIndex(3)
                   .SetCallback(base::BindRepeating(
                       &ComboboxExample::ValueChanged, base::Unretained(this))),
               Builder<Combobox>()
                   .SetOwnedModel(std::make_unique<ComboboxModelExample>())
                   .SetEnabled(false)
                   .SetSelectedIndex(4)
                   .SetCallback(
                       base::BindRepeating(&ComboboxExample::ValueChanged,
                                           base::Unretained(this)))})
          .Build();

  container->AddChildView(std::move(view));
}

void ComboboxExample::ValueChanged() {
  PrintStatus("Selected: %s",
              base::UTF16ToUTF8(combobox_->GetModel()->GetItemAt(
                                    combobox_->GetSelectedIndex()))
                  .c_str());
}

}  // namespace examples
}  // namespace views