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/views/background.h"
9 #include "ui/views/border.h"
10 #include "ui/views/controls/button/checkbox.h"
11 #include "ui/views/controls/combobox/combobox.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/controls/textfield/textfield.h"
14 #include "ui/views/examples/example_combobox_model.h"
15 #include "ui/views/layout/box_layout.h"
16 #include "ui/views/layout/grid_layout.h"
17 #include "ui/views/view.h"
19 using base::ASCIIToUTF16
;
20 using base::WideToUTF16
;
27 const char* kElideBehaviors
[] = { "No Elide", "Truncate", "Elide Head",
28 "Elide Middle", "Elide Tail", "Elide Email", "Fade Tail" };
29 const char* kAlignments
[] = { "Left", "Center", "Right", "Head" };
31 // A Label with a clamped preferred width to demonstrate eliding or wrapping.
32 class PreferredSizeLabel
: public Label
{
34 PreferredSizeLabel() : Label() {
35 SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY
));
37 virtual ~PreferredSizeLabel() {}
40 virtual gfx::Size
GetPreferredSize() const OVERRIDE
{
41 return gfx::Size(50, Label::GetPreferredSize().height());
45 DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel
);
50 LabelExample::LabelExample()
51 : ExampleBase("Label"),
54 elide_behavior_(NULL
),
60 LabelExample::~LabelExample() {
61 // Remove the views first as some reference combobox models.
62 container()->RemoveAllChildViews(true);
65 void LabelExample::CreateExampleView(View
* container
) {
66 // A very simple label example, followed by additional helpful examples.
67 container
->SetLayoutManager(new BoxLayout(BoxLayout::kVertical
, 0, 0, 10));
68 Label
* label
= new Label(ASCIIToUTF16("Hello world!"));
69 container
->AddChildView(label
);
71 const wchar_t hello_world_hebrew
[] =
72 L
"\x5e9\x5dc\x5d5\x5dd \x5d4\x5e2\x5d5\x5dc\x5dd!";
73 label
= new Label(WideToUTF16(hello_world_hebrew
));
74 label
->SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
75 container
->AddChildView(label
);
77 label
= new Label(WideToUTF16(L
"A UTF16 surrogate pair: \x5d0\x5b0"));
78 label
->SetHorizontalAlignment(gfx::ALIGN_RIGHT
);
79 container
->AddChildView(label
);
81 label
= new Label(ASCIIToUTF16("A left-aligned blue label."));
82 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
83 label
->SetEnabledColor(SK_ColorBLUE
);
84 container
->AddChildView(label
);
86 label
= new Label(WideToUTF16(L
"Password!"));
87 label
->SetObscured(true);
88 container
->AddChildView(label
);
90 label
= new Label(ASCIIToUTF16("A Courier-18 label with shadows."));
91 label
->SetFontList(gfx::FontList("Courier, 18px"));
92 gfx::ShadowValues
shadows(1, gfx::ShadowValue(gfx::Point(), 1, SK_ColorRED
));
93 gfx::ShadowValue
shadow(gfx::Point(2, 2), 0, SK_ColorGRAY
);
94 shadows
.push_back(shadow
);
95 label
->SetShadows(shadows
);
96 container
->AddChildView(label
);
98 label
= new PreferredSizeLabel();
99 label
->SetText(ASCIIToUTF16("A long label will elide toward its logical end "
100 "if the text's width exceeds the label's available width."));
101 container
->AddChildView(label
);
103 label
= new PreferredSizeLabel();
104 label
->SetText(ASCIIToUTF16("A multi-line label will wrap onto subsequent "
105 "lines if the text's width exceeds the label's available width, which is "
106 "helpful for extemely long text used to demonstrate line wrapping."));
107 label
->SetMultiLine(true);
108 container
->AddChildView(label
);
110 AddCustomLabel(container
);
113 void LabelExample::ButtonPressed(Button
* button
, const ui::Event
& event
) {
114 if (button
== multiline_
) {
115 custom_label_
->SetMultiLine(multiline_
->checked());
116 } else if (button
== shadows_
) {
117 gfx::ShadowValues shadows
;
118 if (shadows_
->checked()) {
119 shadows
.push_back(gfx::ShadowValue(gfx::Point(), 1, SK_ColorRED
));
120 shadows
.push_back(gfx::ShadowValue(gfx::Point(2, 2), 0, SK_ColorGRAY
));
122 custom_label_
->SetShadows(shadows
);
124 custom_label_
->parent()->parent()->Layout();
125 custom_label_
->SchedulePaint();
128 void LabelExample::OnPerformAction(Combobox
* combobox
) {
129 if (combobox
== alignment_
) {
130 custom_label_
->SetHorizontalAlignment(
131 static_cast<gfx::HorizontalAlignment
>(combobox
->selected_index()));
132 } else if (combobox
== elide_behavior_
) {
133 custom_label_
->SetElideBehavior(
134 static_cast<gfx::ElideBehavior
>(combobox
->selected_index()));
138 void LabelExample::ContentsChanged(Textfield
* sender
,
139 const base::string16
& new_contents
) {
140 custom_label_
->SetText(new_contents
);
141 custom_label_
->parent()->parent()->Layout();
144 void LabelExample::AddCustomLabel(View
* container
) {
145 View
* control_container
= new View();
146 control_container
->SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY
));
147 control_container
->set_background(
148 Background::CreateSolidBackground(SK_ColorLTGRAY
));
149 GridLayout
* layout
= GridLayout::CreatePanel(control_container
);
150 control_container
->SetLayoutManager(layout
);
152 ColumnSet
* column_set
= layout
->AddColumnSet(0);
153 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
,
154 0.0f
, GridLayout::USE_PREF
, 0, 0);
155 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
156 1.0f
, GridLayout::USE_PREF
, 0, 0);
158 layout
->StartRow(0, 0);
159 layout
->AddView(new Label(ASCIIToUTF16("Content: ")));
160 textfield_
= new Textfield();
161 textfield_
->SetText(ASCIIToUTF16("Use the provided controls to configure the "
162 "content and presentation of this custom label."));
163 textfield_
->SetSelectionRange(gfx::Range());
164 textfield_
->set_controller(this);
165 layout
->AddView(textfield_
);
167 alignment_
= AddCombobox(layout
, "Alignment: ", kAlignments
,
168 arraysize(kAlignments
));
169 elide_behavior_
= AddCombobox(layout
, "Elide Behavior: ", kElideBehaviors
,
170 arraysize(kElideBehaviors
));
172 column_set
= layout
->AddColumnSet(1);
173 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
,
174 0, GridLayout::USE_PREF
, 0, 0);
175 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
,
176 0, GridLayout::USE_PREF
, 0, 0);
177 layout
->StartRow(0, 1);
178 multiline_
= new Checkbox(base::ASCIIToUTF16("Multiline"));
179 multiline_
->set_listener(this);
180 layout
->AddView(multiline_
);
181 shadows_
= new Checkbox(base::ASCIIToUTF16("Shadows"));
182 shadows_
->set_listener(this);
183 layout
->AddView(shadows_
);
184 layout
->AddPaddingRow(0, 8);
186 column_set
= layout
->AddColumnSet(2);
187 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
,
188 1, GridLayout::USE_PREF
, 0, 0);
189 layout
->StartRow(0, 2);
190 custom_label_
= new PreferredSizeLabel();
191 custom_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
192 custom_label_
->SetElideBehavior(gfx::NO_ELIDE
);
193 custom_label_
->SetText(textfield_
->text());
194 layout
->AddView(custom_label_
);
196 container
->AddChildView(control_container
);
199 Combobox
* LabelExample::AddCombobox(GridLayout
* layout
,
201 const char** strings
,
203 layout
->StartRow(0, 0);
204 layout
->AddView(new Label(base::ASCIIToUTF16(name
)));
205 ExampleComboboxModel
* model
= new ExampleComboboxModel(strings
, count
);
206 example_combobox_models_
.push_back(model
);
207 Combobox
* combobox
= new Combobox(model
);
208 combobox
->SetSelectedIndex(0);
209 combobox
->set_listener(this);
210 layout
->AddView(combobox
);
214 } // namespace examples