| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
| 11 #include "ui/gfx/color_palette.h" | 11 #include "ui/gfx/color_palette.h" |
| 12 #include "ui/gfx/range/range.h" | 12 #include "ui/gfx/range/range.h" |
| 13 #include "ui/gfx/render_text.h" | 13 #include "ui/gfx/render_text.h" |
| 14 #include "ui/views/controls/button/label_button.h" | 14 #include "ui/views/controls/button/label_button.h" |
| 15 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
| 16 #include "ui/views/controls/textfield/textfield.h" | 16 #include "ui/views/controls/textfield/textfield.h" |
| 17 #include "ui/views/layout/grid_layout.h" | 17 #include "ui/views/layout/grid_layout.h" |
| 18 #include "ui/views/view.h" | 18 #include "ui/views/view.h" |
| 19 | 19 |
| 20 using base::ASCIIToUTF16; | 20 using base::ASCIIToUTF16; |
| 21 using base::UTF16ToUTF8; | 21 using base::UTF16ToUTF8; |
| 22 | 22 |
| 23 namespace views { | 23 namespace views { |
| 24 namespace examples { | 24 namespace examples { |
| 25 | 25 |
| 26 TextfieldExample::TextfieldExample() | 26 TextfieldExample::TextfieldExample() |
| 27 : ExampleBase("Textfield"), | 27 : ExampleBase("Textfield"), |
| 28 name_(NULL), | 28 name_(nullptr), |
| 29 password_(NULL), | 29 password_(nullptr), |
| 30 read_only_(NULL), | 30 disabled_(nullptr), |
| 31 show_password_(NULL), | 31 read_only_(nullptr), |
| 32 clear_all_(NULL), | 32 show_password_(nullptr), |
| 33 append_(NULL), | 33 clear_all_(nullptr), |
| 34 set_(NULL), | 34 append_(nullptr), |
| 35 set_style_(NULL) { | 35 set_(nullptr), |
| 36 set_style_(nullptr) { |
| 36 } | 37 } |
| 37 | 38 |
| 38 TextfieldExample::~TextfieldExample() { | 39 TextfieldExample::~TextfieldExample() { |
| 39 } | 40 } |
| 40 | 41 |
| 41 void TextfieldExample::CreateExampleView(View* container) { | 42 void TextfieldExample::CreateExampleView(View* container) { |
| 42 name_ = new Textfield(); | 43 name_ = new Textfield(); |
| 43 password_ = new Textfield(); | 44 password_ = new Textfield(); |
| 44 password_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); | 45 password_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 45 password_->set_placeholder_text(ASCIIToUTF16("password")); | 46 password_->set_placeholder_text(ASCIIToUTF16("password")); |
| 47 disabled_ = new Textfield(); |
| 48 disabled_->SetEnabled(false); |
| 49 disabled_->SetText(ASCIIToUTF16("disabled")); |
| 46 read_only_ = new Textfield(); | 50 read_only_ = new Textfield(); |
| 47 read_only_->SetReadOnly(true); | 51 read_only_->SetReadOnly(true); |
| 48 read_only_->SetText(ASCIIToUTF16("read only")); | 52 read_only_->SetText(ASCIIToUTF16("read only")); |
| 49 show_password_ = new LabelButton(this, ASCIIToUTF16("Show password")); | 53 show_password_ = new LabelButton(this, ASCIIToUTF16("Show password")); |
| 50 set_background_ = | 54 set_background_ = |
| 51 new LabelButton(this, ASCIIToUTF16("Set non-default background")); | 55 new LabelButton(this, ASCIIToUTF16("Set non-default background")); |
| 52 clear_all_ = new LabelButton(this, ASCIIToUTF16("Clear All")); | 56 clear_all_ = new LabelButton(this, ASCIIToUTF16("Clear All")); |
| 53 append_ = new LabelButton(this, ASCIIToUTF16("Append")); | 57 append_ = new LabelButton(this, ASCIIToUTF16("Append")); |
| 54 set_ = new LabelButton(this, ASCIIToUTF16("Set")); | 58 set_ = new LabelButton(this, ASCIIToUTF16("Set")); |
| 55 set_style_ = new LabelButton(this, ASCIIToUTF16("Set Styles")); | 59 set_style_ = new LabelButton(this, ASCIIToUTF16("Set Styles")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 66 0.8f, GridLayout::USE_PREF, 0, 0); | 70 0.8f, GridLayout::USE_PREF, 0, 0); |
| 67 | 71 |
| 68 auto MakeRow = [layout](View* view1, View* view2) { | 72 auto MakeRow = [layout](View* view1, View* view2) { |
| 69 layout->StartRowWithPadding(0, 0, 0, 5); | 73 layout->StartRowWithPadding(0, 0, 0, 5); |
| 70 layout->AddView(view1); | 74 layout->AddView(view1); |
| 71 if (view2) | 75 if (view2) |
| 72 layout->AddView(view2); | 76 layout->AddView(view2); |
| 73 }; | 77 }; |
| 74 MakeRow(new Label(ASCIIToUTF16("Name:")), name_); | 78 MakeRow(new Label(ASCIIToUTF16("Name:")), name_); |
| 75 MakeRow(new Label(ASCIIToUTF16("Password:")), password_); | 79 MakeRow(new Label(ASCIIToUTF16("Password:")), password_); |
| 80 MakeRow(new Label(ASCIIToUTF16("Disabled:")), disabled_); |
| 76 MakeRow(new Label(ASCIIToUTF16("Read Only:")), read_only_); | 81 MakeRow(new Label(ASCIIToUTF16("Read Only:")), read_only_); |
| 77 MakeRow(new Label(ASCIIToUTF16("Name:")), nullptr); | 82 MakeRow(new Label(ASCIIToUTF16("Name:")), nullptr); |
| 78 MakeRow(show_password_, nullptr); | 83 MakeRow(show_password_, nullptr); |
| 79 MakeRow(set_background_, nullptr); | 84 MakeRow(set_background_, nullptr); |
| 80 MakeRow(clear_all_, nullptr); | 85 MakeRow(clear_all_, nullptr); |
| 81 MakeRow(append_, nullptr); | 86 MakeRow(append_, nullptr); |
| 82 MakeRow(set_, nullptr); | 87 MakeRow(set_, nullptr); |
| 83 MakeRow(set_style_, nullptr); | 88 MakeRow(set_style_, nullptr); |
| 84 } | 89 } |
| 85 | 90 |
| 86 void TextfieldExample::ContentsChanged(Textfield* sender, | 91 void TextfieldExample::ContentsChanged(Textfield* sender, |
| 87 const base::string16& new_contents) { | 92 const base::string16& new_contents) { |
| 88 if (sender == name_) { | 93 if (sender == name_) { |
| 89 PrintStatus("Name [%s]", UTF16ToUTF8(new_contents).c_str()); | 94 PrintStatus("Name [%s]", UTF16ToUTF8(new_contents).c_str()); |
| 90 } else if (sender == password_) { | 95 } else if (sender == password_) { |
| 91 PrintStatus("Password [%s]", UTF16ToUTF8(new_contents).c_str()); | 96 PrintStatus("Password [%s]", UTF16ToUTF8(new_contents).c_str()); |
| 92 } else if (sender == read_only_) { | 97 } else { |
| 93 PrintStatus("Read Only [%s]", UTF16ToUTF8(new_contents).c_str()); | 98 NOTREACHED(); |
| 94 } | 99 } |
| 95 } | 100 } |
| 96 | 101 |
| 97 bool TextfieldExample::HandleKeyEvent(Textfield* sender, | 102 bool TextfieldExample::HandleKeyEvent(Textfield* sender, |
| 98 const ui::KeyEvent& key_event) { | 103 const ui::KeyEvent& key_event) { |
| 99 return false; | 104 return false; |
| 100 } | 105 } |
| 101 | 106 |
| 102 bool TextfieldExample::HandleMouseEvent(Textfield* sender, | 107 bool TextfieldExample::HandleMouseEvent(Textfield* sender, |
| 103 const ui::MouseEvent& mouse_event) { | 108 const ui::MouseEvent& mouse_event) { |
| 104 PrintStatus("HandleMouseEvent click count=%d", mouse_event.GetClickCount()); | 109 PrintStatus("HandleMouseEvent click count=%d", mouse_event.GetClickCount()); |
| 105 return false; | 110 return false; |
| 106 } | 111 } |
| 107 | 112 |
| 108 void TextfieldExample::ButtonPressed(Button* sender, const ui::Event& event) { | 113 void TextfieldExample::ButtonPressed(Button* sender, const ui::Event& event) { |
| 109 if (sender == show_password_) { | 114 if (sender == show_password_) { |
| 110 PrintStatus("Password [%s]", UTF16ToUTF8(password_->text()).c_str()); | 115 PrintStatus("Password [%s]", UTF16ToUTF8(password_->text()).c_str()); |
| 111 } else if (sender == set_background_) { | 116 } else if (sender == set_background_) { |
| 112 password_->SetBackgroundColor(gfx::kGoogleRed300); | 117 password_->SetBackgroundColor(gfx::kGoogleRed300); |
| 113 } else if (sender == clear_all_) { | 118 } else if (sender == clear_all_) { |
| 114 base::string16 empty; | 119 base::string16 empty; |
| 115 name_->SetText(empty); | 120 name_->SetText(empty); |
| 116 password_->SetText(empty); | 121 password_->SetText(empty); |
| 122 disabled_->SetText(empty); |
| 117 read_only_->SetText(empty); | 123 read_only_->SetText(empty); |
| 118 } else if (sender == append_) { | 124 } else if (sender == append_) { |
| 119 name_->AppendText(ASCIIToUTF16("[append]")); | 125 name_->AppendText(ASCIIToUTF16("[append]")); |
| 120 password_->AppendText(ASCIIToUTF16("[append]")); | 126 password_->AppendText(ASCIIToUTF16("[append]")); |
| 127 disabled_->SetText(ASCIIToUTF16("[append]")); |
| 121 read_only_->AppendText(ASCIIToUTF16("[append]")); | 128 read_only_->AppendText(ASCIIToUTF16("[append]")); |
| 122 } else if (sender == set_) { | 129 } else if (sender == set_) { |
| 123 name_->SetText(ASCIIToUTF16("[set]")); | 130 name_->SetText(ASCIIToUTF16("[set]")); |
| 124 password_->SetText(ASCIIToUTF16("[set]")); | 131 password_->SetText(ASCIIToUTF16("[set]")); |
| 132 disabled_->SetText(ASCIIToUTF16("[set]")); |
| 125 read_only_->SetText(ASCIIToUTF16("[set]")); | 133 read_only_->SetText(ASCIIToUTF16("[set]")); |
| 126 } else if (sender == set_style_) { | 134 } else if (sender == set_style_) { |
| 127 if (!name_->text().empty()) { | 135 if (!name_->text().empty()) { |
| 128 name_->SetColor(SK_ColorGREEN); | 136 name_->SetColor(SK_ColorGREEN); |
| 129 | 137 |
| 130 if (name_->text().length() >= 5) { | 138 if (name_->text().length() >= 5) { |
| 131 size_t fifth = name_->text().length() / 5; | 139 size_t fifth = name_->text().length() / 5; |
| 132 const gfx::Range big_range(1 * fifth, 4 * fifth); | 140 const gfx::Range big_range(1 * fifth, 4 * fifth); |
| 133 name_->ApplyStyle(gfx::UNDERLINE, true, big_range); | 141 name_->ApplyStyle(gfx::UNDERLINE, true, big_range); |
| 134 name_->ApplyColor(SK_ColorBLUE, big_range); | 142 name_->ApplyColor(SK_ColorBLUE, big_range); |
| 135 | 143 |
| 136 const gfx::Range small_range(2 * fifth, 3 * fifth); | 144 const gfx::Range small_range(2 * fifth, 3 * fifth); |
| 137 name_->ApplyStyle(gfx::ITALIC, true, small_range); | 145 name_->ApplyStyle(gfx::ITALIC, true, small_range); |
| 138 name_->ApplyStyle(gfx::UNDERLINE, false, small_range); | 146 name_->ApplyStyle(gfx::UNDERLINE, false, small_range); |
| 139 name_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, small_range); | 147 name_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, small_range); |
| 140 name_->ApplyColor(SK_ColorRED, small_range); | 148 name_->ApplyColor(SK_ColorRED, small_range); |
| 141 } | 149 } |
| 142 } | 150 } |
| 143 } | 151 } |
| 144 } | 152 } |
| 145 | 153 |
| 146 } // namespace examples | 154 } // namespace examples |
| 147 } // namespace views | 155 } // namespace views |
| OLD | NEW |