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"
27 class VectorIconGallery
: public View
,
28 public TextfieldController
,
29 public ButtonListener
{
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"))),
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);
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
));
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_
))
85 size_input_
->SetText(base::string16());
90 DCHECK_EQ(color_input_
, sender
);
91 if (new_contents
.size() != 8u)
94 strtoul(base::UTF16ToASCII(new_contents
).c_str(), nullptr, 16);
95 if (new_color
<= 0xffffffff) {
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());
110 base::ReadFileToString(path
, &contents
);
111 image_view_
->SetImage(
112 gfx::CreateVectorIconFromSource(contents
, size_
, color_
));
116 image_view_
->SetImage(gfx::CreateVectorIcon(
117 static_cast<gfx::VectorIconId
>(vector_id_
), size_
, color_
));
121 ImageView
* image_view_
;
122 Textfield
* size_input_
;
123 Textfield
* color_input_
;
124 Textfield
* file_chooser_
;
125 Button
* file_go_button_
;
131 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery
);
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