Add ICU message format support
[chromium-blink-merge.git] / ui / views / examples / vector_example.cc
blobf563d50c726774256b8b9f0bbabc8aeeca3f8d53
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/examples/vector_example.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/gfx/paint_vector_icon.h"
12 #include "ui/gfx/vector_icons_public2.h"
13 #include "ui/views/controls/button/blue_button.h"
14 #include "ui/views/controls/button/button.h"
15 #include "ui/views/controls/image_view.h"
16 #include "ui/views/controls/textfield/textfield.h"
17 #include "ui/views/controls/textfield/textfield_controller.h"
18 #include "ui/views/layout/box_layout.h"
19 #include "ui/views/layout/fill_layout.h"
20 #include "ui/views/view.h"
22 namespace views {
23 namespace examples {
25 namespace {
27 class VectorIconGallery : public View,
28 public TextfieldController,
29 public ButtonListener {
30 public:
31 VectorIconGallery()
32 : image_view_(new ImageView()),
33 size_input_(new Textfield()),
34 color_input_(new Textfield()),
35 file_chooser_(new Textfield()),
36 file_go_button_(new BlueButton(this, base::ASCIIToUTF16("Render"))),
37 vector_id_(0),
38 size_(32),
39 color_(SK_ColorRED) {
40 AddChildView(size_input_);
41 AddChildView(color_input_);
42 AddChildView(image_view_);
44 file_chooser_->set_placeholder_text(
45 base::ASCIIToUTF16("Or enter a file to read"));
46 View* file_container = new View();
47 BoxLayout* file_box = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10);
48 file_container->SetLayoutManager(file_box);
49 file_container->AddChildView(file_chooser_);
50 file_container->AddChildView(file_go_button_);
51 file_box->SetFlexForView(file_chooser_, 1);
52 AddChildView(file_container);
54 size_input_->set_placeholder_text(base::ASCIIToUTF16("Size in dip"));
55 size_input_->set_controller(this);
56 color_input_->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)"));
57 color_input_->set_controller(this);
59 BoxLayout* box = new BoxLayout(BoxLayout::kVertical, 10, 10, 10);
60 SetLayoutManager(box);
61 box->SetFlexForView(image_view_, 1);
62 UpdateImage();
65 ~VectorIconGallery() override {}
67 // View implementation.
68 bool OnMousePressed(const ui::MouseEvent& event) override {
69 if (GetEventHandlerForPoint(event.location()) == image_view_) {
70 vector_id_ = ((vector_id_ + 1) %
71 static_cast<int>(gfx::VectorIconId::VECTOR_ICON_NONE));
72 UpdateImage();
73 return true;
75 return false;
78 // TextfieldController implementation.
79 void ContentsChanged(Textfield* sender,
80 const base::string16& new_contents) override {
81 if (sender == size_input_) {
82 if (base::StringToSizeT(new_contents, &size_))
83 UpdateImage();
84 else
85 size_input_->SetText(base::string16());
87 return;
90 DCHECK_EQ(color_input_, sender);
91 if (new_contents.size() != 8u)
92 return;
93 unsigned new_color =
94 strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16);
95 if (new_color <= 0xffffffff) {
96 color_ = new_color;
97 UpdateImage();
101 // ButtonListener
102 void ButtonPressed(Button* sender, const ui::Event& event) override {
103 DCHECK_EQ(file_go_button_, sender);
104 std::string contents;
105 #if defined(OS_POSIX)
106 base::FilePath path(base::UTF16ToUTF8(file_chooser_->text()));
107 #elif defined(OS_WIN)
108 base::FilePath path(file_chooser_->text());
109 #endif
110 base::ReadFileToString(path, &contents);
111 image_view_->SetImage(
112 gfx::CreateVectorIconFromSource(contents, size_, color_));
115 void UpdateImage() {
116 image_view_->SetImage(gfx::CreateVectorIcon(
117 static_cast<gfx::VectorIconId>(vector_id_), size_, color_));
120 private:
121 ImageView* image_view_;
122 Textfield* size_input_;
123 Textfield* color_input_;
124 Textfield* file_chooser_;
125 Button* file_go_button_;
127 int vector_id_;
128 size_t size_;
129 SkColor color_;
131 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery);
134 } // namespace
136 VectorExample::VectorExample() : ExampleBase("Vector Icon") {}
138 VectorExample::~VectorExample() {}
140 void VectorExample::CreateExampleView(View* container) {
141 container->SetLayoutManager(new FillLayout());
142 container->AddChildView(new VectorIconGallery());
145 } // namespace examples
146 } // namespace views