Make castv2 performance test work.
[chromium-blink-merge.git] / ui / views / examples / label_example.cc
blob689bcfb6bd2e9cc0789f9c25aa7d4cfa5aef1371
1 // Copyright (c) 2012 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/label_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/gfx/geometry/vector2d.h"
9 #include "ui/views/background.h"
10 #include "ui/views/border.h"
11 #include "ui/views/controls/button/checkbox.h"
12 #include "ui/views/controls/combobox/combobox.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/examples/example_combobox_model.h"
16 #include "ui/views/layout/box_layout.h"
17 #include "ui/views/layout/grid_layout.h"
18 #include "ui/views/view.h"
20 using base::ASCIIToUTF16;
21 using base::WideToUTF16;
23 namespace views {
24 namespace examples {
26 namespace {
28 const char* kElideBehaviors[] = { "No Elide", "Truncate", "Elide Head",
29 "Elide Middle", "Elide Tail", "Elide Email", "Fade Tail" };
30 const char* kAlignments[] = { "Left", "Center", "Right", "Head" };
32 // A Label with a clamped preferred width to demonstrate eliding or wrapping.
33 class PreferredSizeLabel : public Label {
34 public:
35 PreferredSizeLabel() : Label() {
36 SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY));
38 ~PreferredSizeLabel() override {}
40 // Label:
41 gfx::Size GetPreferredSize() const override {
42 return gfx::Size(50, Label::GetPreferredSize().height());
45 private:
46 DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel);
49 } // namespace
51 LabelExample::LabelExample()
52 : ExampleBase("Label"),
53 textfield_(NULL),
54 alignment_(NULL),
55 elide_behavior_(NULL),
56 multiline_(NULL),
57 shadows_(NULL),
58 custom_label_(NULL) {
61 LabelExample::~LabelExample() {
62 // Remove the views first as some reference combobox models.
63 container()->RemoveAllChildViews(true);
66 void LabelExample::CreateExampleView(View* container) {
67 // A very simple label example, followed by additional helpful examples.
68 container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 0, 0, 10));
69 Label* label = new Label(ASCIIToUTF16("Hello world!"));
70 container->AddChildView(label);
72 const wchar_t hello_world_hebrew[] =
73 L"\x5e9\x5dc\x5d5\x5dd \x5d4\x5e2\x5d5\x5dc\x5dd!";
74 label = new Label(WideToUTF16(hello_world_hebrew));
75 label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
76 container->AddChildView(label);
78 label = new Label(WideToUTF16(L"A UTF16 surrogate pair: \x5d0\x5b0"));
79 label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
80 container->AddChildView(label);
82 label = new Label(ASCIIToUTF16("A left-aligned blue label."));
83 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
84 label->SetEnabledColor(SK_ColorBLUE);
85 container->AddChildView(label);
87 label = new Label(WideToUTF16(L"Password!"));
88 label->SetObscured(true);
89 container->AddChildView(label);
91 label = new Label(ASCIIToUTF16("A Courier-18 label with shadows."));
92 label->SetFontList(gfx::FontList("Courier, 18px"));
93 gfx::ShadowValues shadows(1,
94 gfx::ShadowValue(gfx::Vector2d(), 1, SK_ColorRED));
95 gfx::ShadowValue shadow(gfx::Vector2d(2, 2), 0, SK_ColorGRAY);
96 shadows.push_back(shadow);
97 label->SetShadows(shadows);
98 container->AddChildView(label);
100 label = new PreferredSizeLabel();
101 label->SetText(ASCIIToUTF16("A long label will elide toward its logical end "
102 "if the text's width exceeds the label's available width."));
103 container->AddChildView(label);
105 label = new PreferredSizeLabel();
106 label->SetText(ASCIIToUTF16("A multi-line label will wrap onto subsequent "
107 "lines if the text's width exceeds the label's available width, which is "
108 "helpful for extemely long text used to demonstrate line wrapping."));
109 label->SetMultiLine(true);
110 container->AddChildView(label);
112 label = new Label(ASCIIToUTF16("Label with thick border"));
113 label->SetBorder(Border::CreateSolidBorder(20, SK_ColorRED));
114 container->AddChildView(label);
116 AddCustomLabel(container);
119 void LabelExample::ButtonPressed(Button* button, const ui::Event& event) {
120 if (button == multiline_) {
121 custom_label_->SetMultiLine(multiline_->checked());
122 } else if (button == shadows_) {
123 gfx::ShadowValues shadows;
124 if (shadows_->checked()) {
125 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(), 1, SK_ColorRED));
126 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(2, 2), 0, SK_ColorGRAY));
128 custom_label_->SetShadows(shadows);
130 custom_label_->parent()->parent()->Layout();
131 custom_label_->SchedulePaint();
134 void LabelExample::OnPerformAction(Combobox* combobox) {
135 if (combobox == alignment_) {
136 custom_label_->SetHorizontalAlignment(
137 static_cast<gfx::HorizontalAlignment>(combobox->selected_index()));
138 } else if (combobox == elide_behavior_) {
139 custom_label_->SetElideBehavior(
140 static_cast<gfx::ElideBehavior>(combobox->selected_index()));
144 void LabelExample::ContentsChanged(Textfield* sender,
145 const base::string16& new_contents) {
146 custom_label_->SetText(new_contents);
147 custom_label_->parent()->parent()->Layout();
150 void LabelExample::AddCustomLabel(View* container) {
151 View* control_container = new View();
152 control_container->SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY));
153 control_container->set_background(
154 Background::CreateSolidBackground(SK_ColorLTGRAY));
155 GridLayout* layout = GridLayout::CreatePanel(control_container);
156 control_container->SetLayoutManager(layout);
158 ColumnSet* column_set = layout->AddColumnSet(0);
159 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL,
160 0.0f, GridLayout::USE_PREF, 0, 0);
161 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
162 1.0f, GridLayout::USE_PREF, 0, 0);
164 layout->StartRow(0, 0);
165 layout->AddView(new Label(ASCIIToUTF16("Content: ")));
166 textfield_ = new Textfield();
167 textfield_->SetText(ASCIIToUTF16("Use the provided controls to configure the "
168 "content and presentation of this custom label."));
169 textfield_->SetSelectionRange(gfx::Range());
170 textfield_->set_controller(this);
171 layout->AddView(textfield_);
173 alignment_ = AddCombobox(layout, "Alignment: ", kAlignments,
174 arraysize(kAlignments));
175 elide_behavior_ = AddCombobox(layout, "Elide Behavior: ", kElideBehaviors,
176 arraysize(kElideBehaviors));
178 column_set = layout->AddColumnSet(1);
179 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING,
180 0, GridLayout::USE_PREF, 0, 0);
181 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING,
182 0, GridLayout::USE_PREF, 0, 0);
183 layout->StartRow(0, 1);
184 multiline_ = new Checkbox(base::ASCIIToUTF16("Multiline"));
185 multiline_->set_listener(this);
186 layout->AddView(multiline_);
187 shadows_ = new Checkbox(base::ASCIIToUTF16("Shadows"));
188 shadows_->set_listener(this);
189 layout->AddView(shadows_);
190 layout->AddPaddingRow(0, 8);
192 column_set = layout->AddColumnSet(2);
193 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
194 1, GridLayout::USE_PREF, 0, 0);
195 layout->StartRow(0, 2);
196 custom_label_ = new PreferredSizeLabel();
197 custom_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
198 custom_label_->SetElideBehavior(gfx::NO_ELIDE);
199 custom_label_->SetText(textfield_->text());
200 layout->AddView(custom_label_);
202 container->AddChildView(control_container);
205 Combobox* LabelExample::AddCombobox(GridLayout* layout,
206 const char* name,
207 const char** strings,
208 int count) {
209 layout->StartRow(0, 0);
210 layout->AddView(new Label(base::ASCIIToUTF16(name)));
211 ExampleComboboxModel* model = new ExampleComboboxModel(strings, count);
212 example_combobox_models_.push_back(model);
213 Combobox* combobox = new Combobox(model);
214 combobox->SetSelectedIndex(0);
215 combobox->set_listener(this);
216 layout->AddView(combobox);
217 return combobox;
220 } // namespace examples
221 } // namespace views