OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "views/examples/textfield_example.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "ui/base/range/range.h" | |
9 #include "ui/gfx/render_text.h" | |
10 #include "views/controls/label.h" | |
11 #include "views/controls/textfield/textfield.h" | |
12 #include "views/layout/grid_layout.h" | |
13 #include "views/view.h" | |
14 | |
15 namespace examples { | |
16 | |
17 TextfieldExample::TextfieldExample(ExamplesMain* main) | |
18 : ExampleBase(main, "Textfield") { | |
19 } | |
20 | |
21 TextfieldExample::~TextfieldExample() { | |
22 } | |
23 | |
24 void TextfieldExample::CreateExampleView(views::View* container) { | |
25 name_ = new views::Textfield(); | |
26 password_ = new views::Textfield(views::Textfield::STYLE_PASSWORD); | |
27 password_->set_text_to_display_when_empty(ASCIIToUTF16("password")); | |
28 show_password_ = new views::TextButton(this, ASCIIToUTF16("Show password")); | |
29 clear_all_ = new views::TextButton(this, ASCIIToUTF16("Clear All")); | |
30 append_ = new views::TextButton(this, ASCIIToUTF16("Append")); | |
31 set_ = new views::TextButton(this, ASCIIToUTF16("Set")); | |
32 set_style_ = new views::TextButton(this, ASCIIToUTF16("Set Styles")); | |
33 name_->SetController(this); | |
34 password_->SetController(this); | |
35 | |
36 views::GridLayout* layout = new views::GridLayout(container); | |
37 container->SetLayoutManager(layout); | |
38 | |
39 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
40 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, | |
41 0.2f, views::GridLayout::USE_PREF, 0, 0); | |
42 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, | |
43 0.8f, views::GridLayout::USE_PREF, 0, 0); | |
44 layout->StartRow(0, 0); | |
45 layout->AddView(new views::Label(ASCIIToUTF16("Name:"))); | |
46 layout->AddView(name_); | |
47 layout->StartRow(0, 0); | |
48 layout->AddView(new views::Label(ASCIIToUTF16("Password:"))); | |
49 layout->AddView(password_); | |
50 layout->StartRow(0, 0); | |
51 layout->AddView(show_password_); | |
52 layout->StartRow(0, 0); | |
53 layout->AddView(clear_all_); | |
54 layout->StartRow(0, 0); | |
55 layout->AddView(append_); | |
56 layout->StartRow(0, 0); | |
57 layout->AddView(set_); | |
58 layout->StartRow(0, 0); | |
59 layout->AddView(set_style_); | |
60 } | |
61 | |
62 void TextfieldExample::ContentsChanged(views::Textfield* sender, | |
63 const string16& new_contents) { | |
64 if (sender == name_) { | |
65 PrintStatus("Name [%s]", UTF16ToUTF8(new_contents).c_str()); | |
66 } else if (sender == password_) { | |
67 PrintStatus("Password [%s]", UTF16ToUTF8(new_contents).c_str()); | |
68 } | |
69 } | |
70 | |
71 bool TextfieldExample::HandleKeyEvent(views::Textfield* sender, | |
72 const views::KeyEvent& key_event) { | |
73 return false; | |
74 } | |
75 | |
76 void TextfieldExample::ButtonPressed(views::Button* sender, | |
77 const views::Event& event) { | |
78 if (sender == show_password_) { | |
79 PrintStatus("Password [%s]", UTF16ToUTF8(password_->text()).c_str()); | |
80 } else if (sender == clear_all_) { | |
81 string16 empty; | |
82 name_->SetText(empty); | |
83 password_->SetText(empty); | |
84 } else if (sender == append_) { | |
85 name_->AppendText(WideToUTF16(L"[append]")); | |
86 } else if (sender == set_) { | |
87 name_->SetText(WideToUTF16(L"[set]")); | |
88 } else if (sender == set_style_) { | |
89 if (!name_->text().empty()) { | |
90 gfx::StyleRange color; | |
91 color.foreground = SK_ColorYELLOW; | |
92 color.range = ui::Range(0, name_->text().length()); | |
93 name_->ApplyStyleRange(color); | |
94 | |
95 if (name_->text().length() >= 5) { | |
96 size_t fifth = name_->text().length() / 5; | |
97 gfx::StyleRange underline; | |
98 underline.underline = true; | |
99 underline.foreground = SK_ColorBLUE; | |
100 underline.range = ui::Range(1 * fifth, 4 * fifth); | |
101 name_->ApplyStyleRange(underline); | |
102 | |
103 gfx::StyleRange strike; | |
104 strike.strike = true; | |
105 strike.foreground = SK_ColorRED; | |
106 strike.range = ui::Range(2 * fifth, 3 * fifth); | |
107 name_->ApplyStyleRange(strike); | |
108 } | |
109 } | |
110 } | |
111 } | |
112 | |
113 } // namespace examples | |
OLD | NEW |