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