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
;
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
{
35 PreferredSizeLabel() : Label() {
36 SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY
));
38 ~PreferredSizeLabel() override
{}
41 gfx::Size
GetPreferredSize() const override
{
42 return gfx::Size(50, Label::GetPreferredSize().height());
46 DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel
);
51 LabelExample::LabelExample()
52 : ExampleBase("Label"),
55 elide_behavior_(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 AddCustomLabel(container
);
115 void LabelExample::ButtonPressed(Button
* button
, const ui::Event
& event
) {
116 if (button
== multiline_
) {
117 custom_label_
->SetMultiLine(multiline_
->checked());
118 } else if (button
== shadows_
) {
119 gfx::ShadowValues shadows
;
120 if (shadows_
->checked()) {
121 shadows
.push_back(gfx::ShadowValue(gfx::Vector2d(), 1, SK_ColorRED
));
122 shadows
.push_back(gfx::ShadowValue(gfx::Vector2d(2, 2), 0, SK_ColorGRAY
));
124 custom_label_
->SetShadows(shadows
);
126 custom_label_
->parent()->parent()->Layout();
127 custom_label_
->SchedulePaint();
130 void LabelExample::OnPerformAction(Combobox
* combobox
) {
131 if (combobox
== alignment_
) {
132 custom_label_
->SetHorizontalAlignment(
133 static_cast<gfx::HorizontalAlignment
>(combobox
->selected_index()));
134 } else if (combobox
== elide_behavior_
) {
135 custom_label_
->SetElideBehavior(
136 static_cast<gfx::ElideBehavior
>(combobox
->selected_index()));
140 void LabelExample::ContentsChanged(Textfield
* sender
,
141 const base::string16
& new_contents
) {
142 custom_label_
->SetText(new_contents
);
143 custom_label_
->parent()->parent()->Layout();
146 void LabelExample::AddCustomLabel(View
* container
) {
147 View
* control_container
= new View();
148 control_container
->SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY
));
149 control_container
->set_background(
150 Background::CreateSolidBackground(SK_ColorLTGRAY
));
151 GridLayout
* layout
= GridLayout::CreatePanel(control_container
);
152 control_container
->SetLayoutManager(layout
);
154 ColumnSet
* column_set
= layout
->AddColumnSet(0);
155 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
,
156 0.0f
, GridLayout::USE_PREF
, 0, 0);
157 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
158 1.0f
, GridLayout::USE_PREF
, 0, 0);
160 layout
->StartRow(0, 0);
161 layout
->AddView(new Label(ASCIIToUTF16("Content: ")));
162 textfield_
= new Textfield();
163 textfield_
->SetText(ASCIIToUTF16("Use the provided controls to configure the "
164 "content and presentation of this custom label."));
165 textfield_
->SetSelectionRange(gfx::Range());
166 textfield_
->set_controller(this);
167 layout
->AddView(textfield_
);
169 alignment_
= AddCombobox(layout
, "Alignment: ", kAlignments
,
170 arraysize(kAlignments
));
171 elide_behavior_
= AddCombobox(layout
, "Elide Behavior: ", kElideBehaviors
,
172 arraysize(kElideBehaviors
));
174 column_set
= layout
->AddColumnSet(1);
175 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
,
176 0, GridLayout::USE_PREF
, 0, 0);
177 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
,
178 0, GridLayout::USE_PREF
, 0, 0);
179 layout
->StartRow(0, 1);
180 multiline_
= new Checkbox(base::ASCIIToUTF16("Multiline"));
181 multiline_
->set_listener(this);
182 layout
->AddView(multiline_
);
183 shadows_
= new Checkbox(base::ASCIIToUTF16("Shadows"));
184 shadows_
->set_listener(this);
185 layout
->AddView(shadows_
);
186 layout
->AddPaddingRow(0, 8);
188 column_set
= layout
->AddColumnSet(2);
189 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
190 1, GridLayout::USE_PREF
, 0, 0);
191 layout
->StartRow(0, 2);
192 custom_label_
= new PreferredSizeLabel();
193 custom_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
194 custom_label_
->SetElideBehavior(gfx::NO_ELIDE
);
195 custom_label_
->SetText(textfield_
->text());
196 layout
->AddView(custom_label_
);
198 container
->AddChildView(control_container
);
201 Combobox
* LabelExample::AddCombobox(GridLayout
* layout
,
203 const char** strings
,
205 layout
->StartRow(0, 0);
206 layout
->AddView(new Label(base::ASCIIToUTF16(name
)));
207 ExampleComboboxModel
* model
= new ExampleComboboxModel(strings
, count
);
208 example_combobox_models_
.push_back(model
);
209 Combobox
* combobox
= new Combobox(model
);
210 combobox
->SetSelectedIndex(0);
211 combobox
->set_listener(this);
212 layout
->AddView(combobox
);
216 } // namespace examples