Add ICU message format support
[chromium-blink-merge.git] / ui / views / examples / textfield_example.cc
blob7dae434f353b6d4d55ba5c50d4a76f8ae0dc7911
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/textfield_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/events/event.h"
9 #include "ui/gfx/range/range.h"
10 #include "ui/gfx/render_text.h"
11 #include "ui/views/controls/button/label_button.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/controls/textfield/textfield.h"
14 #include "ui/views/layout/grid_layout.h"
15 #include "ui/views/view.h"
17 using base::ASCIIToUTF16;
18 using base::UTF16ToUTF8;
20 namespace views {
21 namespace examples {
23 TextfieldExample::TextfieldExample()
24 : ExampleBase("Textfield"),
25 name_(NULL),
26 password_(NULL),
27 read_only_(NULL),
28 show_password_(NULL),
29 clear_all_(NULL),
30 append_(NULL),
31 set_(NULL),
32 set_style_(NULL) {
35 TextfieldExample::~TextfieldExample() {
38 void TextfieldExample::CreateExampleView(View* container) {
39 name_ = new Textfield();
40 password_ = new Textfield();
41 password_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
42 password_->set_placeholder_text(ASCIIToUTF16("password"));
43 read_only_ = new Textfield();
44 read_only_->SetReadOnly(true);
45 read_only_->SetText(ASCIIToUTF16("read only"));
46 show_password_ = new LabelButton(this, ASCIIToUTF16("Show password"));
47 clear_all_ = new LabelButton(this, ASCIIToUTF16("Clear All"));
48 append_ = new LabelButton(this, ASCIIToUTF16("Append"));
49 set_ = new LabelButton(this, ASCIIToUTF16("Set"));
50 set_style_ = new LabelButton(this, ASCIIToUTF16("Set Styles"));
51 name_->set_controller(this);
52 password_->set_controller(this);
54 GridLayout* layout = new GridLayout(container);
55 container->SetLayoutManager(layout);
57 ColumnSet* column_set = layout->AddColumnSet(0);
58 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL,
59 0.2f, GridLayout::USE_PREF, 0, 0);
60 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL,
61 0.8f, GridLayout::USE_PREF, 0, 0);
62 layout->StartRow(0, 0);
63 layout->AddView(new Label(ASCIIToUTF16("Name:")));
64 layout->AddView(name_);
65 layout->StartRow(0, 0);
66 layout->AddView(new Label(ASCIIToUTF16("Password:")));
67 layout->AddView(password_);
68 layout->StartRow(0, 0);
69 layout->AddView(new Label(ASCIIToUTF16("Read Only:")));
70 layout->AddView(read_only_);
71 layout->StartRow(0, 0);
72 layout->AddView(show_password_);
73 layout->StartRow(0, 0);
74 layout->AddView(clear_all_);
75 layout->StartRow(0, 0);
76 layout->AddView(append_);
77 layout->StartRow(0, 0);
78 layout->AddView(set_);
79 layout->StartRow(0, 0);
80 layout->AddView(set_style_);
83 void TextfieldExample::ContentsChanged(Textfield* sender,
84 const base::string16& new_contents) {
85 if (sender == name_) {
86 PrintStatus("Name [%s]", UTF16ToUTF8(new_contents).c_str());
87 } else if (sender == password_) {
88 PrintStatus("Password [%s]", UTF16ToUTF8(new_contents).c_str());
89 } else if (sender == read_only_) {
90 PrintStatus("Read Only [%s]", UTF16ToUTF8(new_contents).c_str());
94 bool TextfieldExample::HandleKeyEvent(Textfield* sender,
95 const ui::KeyEvent& key_event) {
96 return false;
99 bool TextfieldExample::HandleMouseEvent(Textfield* sender,
100 const ui::MouseEvent& mouse_event) {
101 PrintStatus("HandleMouseEvent click count=%d", mouse_event.GetClickCount());
102 return false;
105 void TextfieldExample::ButtonPressed(Button* sender, const ui::Event& event) {
106 if (sender == show_password_) {
107 PrintStatus("Password [%s]", UTF16ToUTF8(password_->text()).c_str());
108 } else if (sender == clear_all_) {
109 base::string16 empty;
110 name_->SetText(empty);
111 password_->SetText(empty);
112 read_only_->SetText(empty);
113 } else if (sender == append_) {
114 name_->AppendText(ASCIIToUTF16("[append]"));
115 password_->AppendText(ASCIIToUTF16("[append]"));
116 read_only_->AppendText(ASCIIToUTF16("[append]"));
117 } else if (sender == set_) {
118 name_->SetText(ASCIIToUTF16("[set]"));
119 password_->SetText(ASCIIToUTF16("[set]"));
120 read_only_->SetText(ASCIIToUTF16("[set]"));
121 } else if (sender == set_style_) {
122 if (!name_->text().empty()) {
123 name_->SetColor(SK_ColorGREEN);
124 name_->SetStyle(gfx::BOLD, true);
126 if (name_->text().length() >= 5) {
127 size_t fifth = name_->text().length() / 5;
128 const gfx::Range big_range(1 * fifth, 4 * fifth);
129 name_->ApplyStyle(gfx::BOLD, false, big_range);
130 name_->ApplyStyle(gfx::UNDERLINE, true, big_range);
131 name_->ApplyColor(SK_ColorBLUE, big_range);
133 const gfx::Range small_range(2 * fifth, 3 * fifth);
134 name_->ApplyStyle(gfx::ITALIC, true, small_range);
135 name_->ApplyStyle(gfx::UNDERLINE, false, small_range);
136 name_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, small_range);
137 name_->ApplyColor(SK_ColorRED, small_range);
143 } // namespace examples
144 } // namespace views