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