diff options
author | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-24 01:43:09 +0000 |
---|---|---|
committer | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-24 01:43:09 +0000 |
commit | 9d4cd7f9f4fd0ee265fb0f7043f77a1e0b81b340 (patch) | |
tree | d08be6e0bb31056893111b92fea06ea4c53f080c /ui/views | |
parent | 5b0a9be7992210aa5040b09242c06196120117a0 (diff) | |
download | chromium_src-9d4cd7f9f4fd0ee265fb0f7043f77a1e0b81b340.zip chromium_src-9d4cd7f9f4fd0ee265fb0f7043f77a1e0b81b340.tar.gz chromium_src-9d4cd7f9f4fd0ee265fb0f7043f77a1e0b81b340.tar.bz2 |
Refine Views textfield state interaction, testing, and examples.
Ignore Views textfield CTRL+<key> shortcuts with Alt pressed.
Matches Win and fixes ctrl+alt+[a/z/etc.] AltGr input for Polish, etc.
(alternate grapheme input was triggering undo/select-all/etc.)
Enforce and test read-only and obscured states more rigorously.
(prevent cut/copy/paste as needed, this was not always checked before)
Do not change the focusable state of textfields when changing read-only.
(this matches windows native behavior better)
Expand and refine a bunch of unit tests and the Views Example for textfields.
BUG=238105,131660
TEST=Read-only and obscured Textfields work as intended. Polish input (right-Alt)+a, (right-Alt)+z, etc. work as expected. Ctrl+Alt+a, Ctrl+Alt+z, etc. are no-op on views textfields.
R=oshima@chromium.org
TBR=sky@chromium.org
Review URL: https://chromiumcodereview.appspot.com/15684004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@201961 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/views')
-rw-r--r-- | ui/views/controls/textfield/native_textfield_views.cc | 36 | ||||
-rw-r--r-- | ui/views/controls/textfield/native_textfield_views_unittest.cc | 211 | ||||
-rw-r--r-- | ui/views/controls/textfield/textfield.cc | 2 | ||||
-rw-r--r-- | ui/views/examples/textfield_example.cc | 24 | ||||
-rw-r--r-- | ui/views/examples/textfield_example.h | 1 |
5 files changed, 176 insertions, 98 deletions
diff --git a/ui/views/controls/textfield/native_textfield_views.cc b/ui/views/controls/textfield/native_textfield_views.cc index 724ad68..5da334d 100644 --- a/ui/views/controls/textfield/native_textfield_views.cc +++ b/ui/views/controls/textfield/native_textfield_views.cc @@ -1168,39 +1168,40 @@ bool NativeTextfieldViews::HandleKeyEvent(const ui::KeyEvent& key_event) { return false; OnBeforeUserAction(); - bool editable = !textfield_->read_only(); - bool readable = !textfield_->IsObscured(); - bool shift = key_event.IsShiftDown(); - bool control = key_event.IsControlDown(); + const bool editable = !textfield_->read_only(); + const bool readable = !textfield_->IsObscured(); + const bool shift = key_event.IsShiftDown(); + const bool control = key_event.IsControlDown(); + const bool alt = key_event.IsAltDown(); bool text_changed = false; bool cursor_changed = false; switch (key_code) { case ui::VKEY_Z: - if (control && !shift && editable) + if (control && !shift && !alt && editable) cursor_changed = text_changed = model_->Undo(); - else if (control && shift && editable) + else if (control && shift && !alt && editable) cursor_changed = text_changed = model_->Redo(); break; case ui::VKEY_Y: - if (control && editable) + if (control && !alt && editable) cursor_changed = text_changed = model_->Redo(); break; case ui::VKEY_A: - if (control) { + if (control && !alt) { model_->SelectAll(false); cursor_changed = true; } break; case ui::VKEY_X: - if (control && editable && readable) + if (control && !alt && editable && readable) cursor_changed = text_changed = Cut(); break; case ui::VKEY_C: - if (control && readable) + if (control && !alt && readable) Copy(); break; case ui::VKEY_V: - if (control && editable) + if (control && !alt && editable) cursor_changed = text_changed = Paste(); break; case ui::VKEY_RIGHT: @@ -1208,7 +1209,7 @@ bool NativeTextfieldViews::HandleKeyEvent(const ui::KeyEvent& key_event) { // We should ignore the alt-left/right keys because alt key doesn't make // any special effects for them and they can be shortcut keys such like // forward/back of the browser history. - if (key_event.IsAltDown()) + if (alt) break; size_t cursor_position = model_->GetCursorPosition(); model_->MoveCursor( @@ -1258,9 +1259,9 @@ bool NativeTextfieldViews::HandleKeyEvent(const ui::KeyEvent& key_event) { text_changed = true; break; case ui::VKEY_INSERT: - if (control && !shift) + if (control && !shift && readable) Copy(); - else if (shift && !control) + else if (shift && !control && editable) cursor_changed = text_changed = Paste(); break; default: @@ -1363,7 +1364,7 @@ void NativeTextfieldViews::OnAfterUserAction() { } bool NativeTextfieldViews::Cut() { - if (model_->Cut()) { + if (!textfield_->read_only() && !textfield_->IsObscured() && model_->Cut()) { TextfieldController* controller = textfield_->GetController(); if (controller) controller->OnAfterCutOrCopy(); @@ -1373,7 +1374,7 @@ bool NativeTextfieldViews::Cut() { } bool NativeTextfieldViews::Copy() { - if (model_->Copy()) { + if (!textfield_->IsObscured() && model_->Copy()) { TextfieldController* controller = textfield_->GetController(); if (controller) controller->OnAfterCutOrCopy(); @@ -1383,6 +1384,9 @@ bool NativeTextfieldViews::Copy() { } bool NativeTextfieldViews::Paste() { + if (textfield_->read_only()) + return false; + const string16 original_text = GetText(); const bool success = model_->Paste(); diff --git a/ui/views/controls/textfield/native_textfield_views_unittest.cc b/ui/views/controls/textfield/native_textfield_views_unittest.cc index bd46850..dd10ff7 100644 --- a/ui/views/controls/textfield/native_textfield_views_unittest.cc +++ b/ui/views/controls/textfield/native_textfield_views_unittest.cc @@ -143,7 +143,9 @@ class NativeTextfieldViewsTest : public ViewsTestBase, // TextfieldController: virtual void ContentsChanged(Textfield* sender, const string16& new_contents) OVERRIDE { - ASSERT_NE(last_contents_, new_contents); + // Paste calls TextfieldController::ContentsChanged() explicitly even if the + // paste action did not change the content. So |new_contents| may match + // |last_contents_|. For more info, see http://crbug.com/79002 last_contents_ = new_contents; } @@ -215,18 +217,20 @@ class NativeTextfieldViewsTest : public ViewsTestBase, protected: void SendKeyEvent(ui::KeyboardCode key_code, + bool alt, bool shift, bool control, bool caps_lock) { - int flags = (shift ? ui::EF_SHIFT_DOWN : 0) | - (control ? ui::EF_CONTROL_DOWN : 0) | - (caps_lock ? ui::EF_CAPS_LOCK_DOWN : 0); + int flags = (alt ? ui::EF_ALT_DOWN : 0) | + (shift ? ui::EF_SHIFT_DOWN : 0) | + (control ? ui::EF_CONTROL_DOWN : 0) | + (caps_lock ? ui::EF_CAPS_LOCK_DOWN : 0); ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, flags, false); input_method_->DispatchKeyEvent(event); } void SendKeyEvent(ui::KeyboardCode key_code, bool shift, bool control) { - SendKeyEvent(key_code, shift, control, false); + SendKeyEvent(key_code, false, shift, control, false); } void SendKeyEvent(ui::KeyboardCode key_code) { @@ -452,23 +456,16 @@ TEST_F(NativeTextfieldViewsTest, ModelChangesTestLowerCaseWithLocale) { TEST_F(NativeTextfieldViewsTest, KeyTest) { InitTextfield(Textfield::STYLE_DEFAULT); - SendKeyEvent(ui::VKEY_C, true, false); - EXPECT_STR_EQ("C", textfield_->text()); - EXPECT_STR_EQ("C", last_contents_); - last_contents_.clear(); - - SendKeyEvent(ui::VKEY_R, false, false); - EXPECT_STR_EQ("Cr", textfield_->text()); - EXPECT_STR_EQ("Cr", last_contents_); - - textfield_->SetText(string16()); - SendKeyEvent(ui::VKEY_C, true, false, true); - SendKeyEvent(ui::VKEY_C, false, false, true); - SendKeyEvent(ui::VKEY_1, false, false, true); - SendKeyEvent(ui::VKEY_1, true, false, true); - SendKeyEvent(ui::VKEY_1, true, false, false); - EXPECT_STR_EQ("cC1!!", textfield_->text()); - EXPECT_STR_EQ("cC1!!", last_contents_); + // Event flags: key, alt, shift, ctrl, caps-lock. + SendKeyEvent(ui::VKEY_T, false, true, false, false); + SendKeyEvent(ui::VKEY_E, false, false, false, false); + SendKeyEvent(ui::VKEY_X, false, true, false, true); + SendKeyEvent(ui::VKEY_T, false, false, false, true); + SendKeyEvent(ui::VKEY_1, false, true, false, false); + SendKeyEvent(ui::VKEY_1, false, false, false, false); + SendKeyEvent(ui::VKEY_1, false, true, false, true); + SendKeyEvent(ui::VKEY_1, false, false, false, true); + EXPECT_STR_EQ("TexT!1!1", textfield_->text()); } TEST_F(NativeTextfieldViewsTest, ControlAndSelectTest) { @@ -545,13 +542,13 @@ TEST_F(NativeTextfieldViewsTest, InsertionDeletionTest) { // Delete the previous word from cursor. textfield_->SetText(ASCIIToUTF16("one two three four")); SendKeyEvent(ui::VKEY_END); - SendKeyEvent(ui::VKEY_BACK, false, true, false); + SendKeyEvent(ui::VKEY_BACK, false, false, true, false); EXPECT_STR_EQ("one two three ", textfield_->text()); // Delete upto the beginning of the buffer from cursor in chromeos, do nothing // in windows. - SendKeyEvent(ui::VKEY_LEFT, false, true, false); - SendKeyEvent(ui::VKEY_BACK, true, true, false); + SendKeyEvent(ui::VKEY_LEFT, false, false, true, false); + SendKeyEvent(ui::VKEY_BACK, false, true, true, false); #if defined(OS_WIN) EXPECT_STR_EQ("one two three ", textfield_->text()); #else @@ -561,13 +558,13 @@ TEST_F(NativeTextfieldViewsTest, InsertionDeletionTest) { // Delete the next word from cursor. textfield_->SetText(ASCIIToUTF16("one two three four")); SendKeyEvent(ui::VKEY_HOME); - SendKeyEvent(ui::VKEY_DELETE, false, true, false); + SendKeyEvent(ui::VKEY_DELETE, false, false, true, false); EXPECT_STR_EQ(" two three four", textfield_->text()); // Delete upto the end of the buffer from cursor in chromeos, do nothing // in windows. - SendKeyEvent(ui::VKEY_RIGHT, false, true, false); - SendKeyEvent(ui::VKEY_DELETE, true, true, false); + SendKeyEvent(ui::VKEY_RIGHT, false, false, true, false); + SendKeyEvent(ui::VKEY_DELETE, false, true, true, false); #if defined(OS_WIN) EXPECT_STR_EQ(" two three four", textfield_->text()); #else @@ -577,29 +574,36 @@ TEST_F(NativeTextfieldViewsTest, InsertionDeletionTest) { TEST_F(NativeTextfieldViewsTest, PasswordTest) { InitTextfield(Textfield::STYLE_OBSCURED); - EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, GetTextInputType()); + EXPECT_TRUE(textfield_->enabled()); + EXPECT_TRUE(textfield_->focusable()); last_contents_.clear(); - textfield_->SetText(ASCIIToUTF16("my password")); - // Just to make sure the text() and callback returns - // the actual text instead of "*". - EXPECT_STR_EQ("my password", textfield_->text()); + textfield_->SetText(ASCIIToUTF16("password")); + // Ensure text() and the callback returns the actual text instead of "*". + EXPECT_STR_EQ("password", textfield_->text()); EXPECT_TRUE(last_contents_.empty()); - - // Cut and copy should be disabled in the context menu. model_->SelectAll(false); - EXPECT_FALSE(IsCommandIdEnabled(IDS_APP_CUT)); - EXPECT_FALSE(IsCommandIdEnabled(IDS_APP_COPY)); - - // Cut and copy keyboard shortcuts and menu commands should do nothing. SetClipboardText("foo"); - SendKeyEvent(ui::VKEY_C, false, true); + + // Cut and copy should be disabled. + EXPECT_FALSE(textfield_view_->IsCommandIdEnabled(IDS_APP_CUT)); + textfield_view_->ExecuteCommand(IDS_APP_CUT, 0); SendKeyEvent(ui::VKEY_X, false, true); - ExecuteCommand(IDS_APP_COPY, 0); - ExecuteCommand(IDS_APP_CUT, 0); + EXPECT_FALSE(textfield_view_->IsCommandIdEnabled(IDS_APP_COPY)); + textfield_view_->ExecuteCommand(IDS_APP_COPY, 0); + SendKeyEvent(ui::VKEY_C, false, true); + SendKeyEvent(ui::VKEY_INSERT, false, true); + EXPECT_STR_EQ("foo", string16(GetClipboardText())); + EXPECT_STR_EQ("password", textfield_->text()); + + // Paste should work normally. + EXPECT_TRUE(textfield_view_->IsCommandIdEnabled(IDS_APP_PASTE)); + textfield_view_->ExecuteCommand(IDS_APP_PASTE, 0); + SendKeyEvent(ui::VKEY_V, false, true); + SendKeyEvent(ui::VKEY_INSERT, true, false); EXPECT_STR_EQ("foo", string16(GetClipboardText())); - EXPECT_STR_EQ("my password", textfield_->text()); + EXPECT_STR_EQ("foofoofoo", textfield_->text()); } TEST_F(NativeTextfieldViewsTest, InputTypeSetsObscured) { @@ -1146,48 +1150,57 @@ TEST_F(NativeTextfieldViewsTest, MAYBE_DragAndDrop_Canceled) { TEST_F(NativeTextfieldViewsTest, ReadOnlyTest) { InitTextfield(Textfield::STYLE_DEFAULT); - textfield_->SetText(ASCIIToUTF16(" one two three ")); + textfield_->SetText(ASCIIToUTF16("read only")); textfield_->SetReadOnly(true); + EXPECT_TRUE(textfield_->enabled()); + EXPECT_TRUE(textfield_->focusable()); + SendKeyEvent(ui::VKEY_HOME); EXPECT_EQ(0U, textfield_->GetCursorPosition()); - SendKeyEvent(ui::VKEY_END); - EXPECT_EQ(15U, textfield_->GetCursorPosition()); + EXPECT_EQ(9U, textfield_->GetCursorPosition()); SendKeyEvent(ui::VKEY_LEFT, false, false); - EXPECT_EQ(14U, textfield_->GetCursorPosition()); - + EXPECT_EQ(8U, textfield_->GetCursorPosition()); SendKeyEvent(ui::VKEY_LEFT, false, true); - EXPECT_EQ(9U, textfield_->GetCursorPosition()); - - SendKeyEvent(ui::VKEY_LEFT, true, true); EXPECT_EQ(5U, textfield_->GetCursorPosition()); - EXPECT_STR_EQ("two ", textfield_->GetSelectedText()); - + SendKeyEvent(ui::VKEY_LEFT, true, true); + EXPECT_EQ(0U, textfield_->GetCursorPosition()); + EXPECT_STR_EQ("read ", textfield_->GetSelectedText()); textfield_->SelectAll(false); - EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); + EXPECT_STR_EQ("read only", textfield_->GetSelectedText()); - // CUT&PASTE does not work, but COPY works + // Cut should be disabled. SetClipboardText("Test"); + EXPECT_FALSE(textfield_view_->IsCommandIdEnabled(IDS_APP_CUT)); + textfield_view_->ExecuteCommand(IDS_APP_CUT, 0); SendKeyEvent(ui::VKEY_X, false, true); - EXPECT_STR_EQ(" one two three ", textfield_->GetSelectedText()); - string16 str(GetClipboardText()); - EXPECT_STR_NE(" one two three ", str); + EXPECT_STR_EQ("Test", string16(GetClipboardText())); + EXPECT_STR_EQ("read only", textfield_->text()); + + // Paste should be disabled. + EXPECT_FALSE(textfield_view_->IsCommandIdEnabled(IDS_APP_PASTE)); + textfield_view_->ExecuteCommand(IDS_APP_PASTE, 0); + SendKeyEvent(ui::VKEY_V, false, true); + SendKeyEvent(ui::VKEY_INSERT, true, false); + EXPECT_STR_EQ("read only", textfield_->text()); + // Copy should work normally. + SetClipboardText("Test"); + EXPECT_TRUE(textfield_view_->IsCommandIdEnabled(IDS_APP_COPY)); + textfield_view_->ExecuteCommand(IDS_APP_COPY, 0); + EXPECT_STR_EQ("read only", string16(GetClipboardText())); + SetClipboardText("Test"); SendKeyEvent(ui::VKEY_C, false, true); - ui::Clipboard::GetForCurrentThread()-> - ReadText(ui::Clipboard::BUFFER_STANDARD, &str); - EXPECT_STR_EQ(" one two three ", str); + EXPECT_STR_EQ("read only", string16(GetClipboardText())); + SetClipboardText("Test"); + SendKeyEvent(ui::VKEY_INSERT, false, true); + EXPECT_STR_EQ("read only", string16(GetClipboardText())); // SetText should work even in read only mode. textfield_->SetText(ASCIIToUTF16(" four five six ")); EXPECT_STR_EQ(" four five six ", textfield_->text()); - // Paste shouldn't work. - SendKeyEvent(ui::VKEY_V, false, true); - EXPECT_STR_EQ(" four five six ", textfield_->text()); - EXPECT_TRUE(textfield_->GetSelectedText().empty()); - textfield_->SelectAll(false); EXPECT_STR_EQ(" four five six ", textfield_->GetSelectedText()); @@ -1400,30 +1413,68 @@ TEST_F(NativeTextfieldViewsTest, UndoRedoTest) { EXPECT_STR_EQ("", textfield_->text()); } -TEST_F(NativeTextfieldViewsTest, CopyPasteShortcuts) { +TEST_F(NativeTextfieldViewsTest, CutCopyPaste) { InitTextfield(Textfield::STYLE_DEFAULT); - // Ensure [Ctrl]+[c] copies and [Ctrl]+[v] pastes. - textfield_->SetText(ASCIIToUTF16("abc")); + + // Ensure IDS_APP_CUT cuts. + textfield_->SetText(ASCIIToUTF16("123")); textfield_->SelectAll(false); + EXPECT_TRUE(textfield_view_->IsCommandIdEnabled(IDS_APP_CUT)); + textfield_view_->ExecuteCommand(IDS_APP_CUT, 0); + EXPECT_STR_EQ("123", string16(GetClipboardText())); + EXPECT_STR_EQ("", textfield_->text()); + + // Ensure [Ctrl]+[x] cuts and [Ctrl]+[Alt][x] does nothing. + textfield_->SetText(ASCIIToUTF16("456")); + textfield_->SelectAll(false); + SendKeyEvent(ui::VKEY_X, true, false, true, false); + EXPECT_STR_EQ("123", string16(GetClipboardText())); + EXPECT_STR_EQ("456", textfield_->text()); + SendKeyEvent(ui::VKEY_X, false, true); + EXPECT_STR_EQ("456", string16(GetClipboardText())); + EXPECT_STR_EQ("", textfield_->text()); + + // Ensure IDS_APP_COPY copies. + textfield_->SetText(ASCIIToUTF16("789")); + textfield_->SelectAll(false); + EXPECT_TRUE(textfield_view_->IsCommandIdEnabled(IDS_APP_COPY)); + textfield_view_->ExecuteCommand(IDS_APP_COPY, 0); + EXPECT_STR_EQ("789", string16(GetClipboardText())); + + // Ensure [Ctrl]+[c] copies and [Ctrl]+[Alt][c] does nothing. + textfield_->SetText(ASCIIToUTF16("012")); + textfield_->SelectAll(false); + SendKeyEvent(ui::VKEY_C, true, false, true, false); + EXPECT_STR_EQ("789", string16(GetClipboardText())); SendKeyEvent(ui::VKEY_C, false, true); - EXPECT_STR_EQ("abc", string16(GetClipboardText())); - SendKeyEvent(ui::VKEY_HOME); - SendKeyEvent(ui::VKEY_V, false, true); - EXPECT_STR_EQ("abcabc", textfield_->text()); + EXPECT_STR_EQ("012", string16(GetClipboardText())); - // Ensure [Ctrl]+[Insert] copies and [Shift]+[Insert] pastes. - textfield_->SetText(ASCIIToUTF16("123")); + // Ensure [Ctrl]+[Insert] copies. + textfield_->SetText(ASCIIToUTF16("345")); textfield_->SelectAll(false); SendKeyEvent(ui::VKEY_INSERT, false, true); - EXPECT_STR_EQ("123", string16(GetClipboardText())); - SendKeyEvent(ui::VKEY_HOME); + EXPECT_STR_EQ("345", string16(GetClipboardText())); + EXPECT_STR_EQ("345", textfield_->text()); + + // Ensure IDS_APP_PASTE, [Ctrl]+[V], and [Shift]+[Insert] pastes; + // also ensure that [Ctrl]+[Alt]+[V] does nothing. + SetClipboardText("abc"); + textfield_->SetText(string16()); + EXPECT_TRUE(textfield_view_->IsCommandIdEnabled(IDS_APP_PASTE)); + textfield_view_->ExecuteCommand(IDS_APP_PASTE, 0); + EXPECT_STR_EQ("abc", textfield_->text()); + SendKeyEvent(ui::VKEY_V, false, true); + EXPECT_STR_EQ("abcabc", textfield_->text()); SendKeyEvent(ui::VKEY_INSERT, true, false); - EXPECT_STR_EQ("123123", textfield_->text()); + EXPECT_STR_EQ("abcabcabc", textfield_->text()); + SendKeyEvent(ui::VKEY_V, true, false, true, false); + EXPECT_STR_EQ("abcabcabc", textfield_->text()); + // Ensure [Ctrl]+[Shift]+[Insert] is a no-op. textfield_->SelectAll(false); SendKeyEvent(ui::VKEY_INSERT, true, true); - EXPECT_STR_EQ("123", string16(GetClipboardText())); - EXPECT_STR_EQ("123123", textfield_->text()); + EXPECT_STR_EQ("abc", string16(GetClipboardText())); + EXPECT_STR_EQ("abcabcabc", textfield_->text()); } TEST_F(NativeTextfieldViewsTest, OvertypeMode) { diff --git a/ui/views/controls/textfield/textfield.cc b/ui/views/controls/textfield/textfield.cc index 73cdb48..b5aa80b 100644 --- a/ui/views/controls/textfield/textfield.cc +++ b/ui/views/controls/textfield/textfield.cc @@ -128,8 +128,8 @@ TextfieldController* Textfield::GetController() const { } void Textfield::SetReadOnly(bool read_only) { + // Update read-only without changing the focusable state (or active, etc.). read_only_ = read_only; - set_focusable(!read_only); if (native_wrapper_) { native_wrapper_->UpdateReadOnly(); native_wrapper_->UpdateTextColor(); diff --git a/ui/views/examples/textfield_example.cc b/ui/views/examples/textfield_example.cc index 62d70a0..6d9c505 100644 --- a/ui/views/examples/textfield_example.cc +++ b/ui/views/examples/textfield_example.cc @@ -17,7 +17,16 @@ namespace views { namespace examples { -TextfieldExample::TextfieldExample() : ExampleBase("Textfield") { +TextfieldExample::TextfieldExample() + : ExampleBase("Textfield"), + name_(NULL), + password_(NULL), + read_only_(NULL), + show_password_(NULL), + clear_all_(NULL), + append_(NULL), + set_(NULL), + set_style_(NULL) { } TextfieldExample::~TextfieldExample() { @@ -27,6 +36,9 @@ void TextfieldExample::CreateExampleView(View* container) { name_ = new Textfield(); password_ = new Textfield(Textfield::STYLE_OBSCURED); password_->set_placeholder_text(ASCIIToUTF16("password")); + read_only_ = new Textfield(); + read_only_->SetReadOnly(true); + read_only_->SetText(ASCIIToUTF16("read only")); show_password_ = new LabelButton(this, ASCIIToUTF16("Show password")); clear_all_ = new LabelButton(this, ASCIIToUTF16("Clear All")); append_ = new LabelButton(this, ASCIIToUTF16("Append")); @@ -50,6 +62,9 @@ void TextfieldExample::CreateExampleView(View* container) { layout->AddView(new Label(ASCIIToUTF16("Password:"))); layout->AddView(password_); layout->StartRow(0, 0); + layout->AddView(new Label(ASCIIToUTF16("Read Only:"))); + layout->AddView(read_only_); + layout->StartRow(0, 0); layout->AddView(show_password_); layout->StartRow(0, 0); layout->AddView(clear_all_); @@ -67,6 +82,8 @@ void TextfieldExample::ContentsChanged(Textfield* sender, PrintStatus("Name [%s]", UTF16ToUTF8(new_contents).c_str()); } else if (sender == password_) { PrintStatus("Password [%s]", UTF16ToUTF8(new_contents).c_str()); + } else if (sender == read_only_) { + PrintStatus("Read Only [%s]", UTF16ToUTF8(new_contents).c_str()); } } @@ -88,10 +105,15 @@ void TextfieldExample::ButtonPressed(Button* sender, const ui::Event& event) { string16 empty; name_->SetText(empty); password_->SetText(empty); + read_only_->SetText(empty); } else if (sender == append_) { name_->AppendText(ASCIIToUTF16("[append]")); + password_->AppendText(ASCIIToUTF16("[append]")); + read_only_->AppendText(ASCIIToUTF16("[append]")); } else if (sender == set_) { name_->SetText(ASCIIToUTF16("[set]")); + password_->SetText(ASCIIToUTF16("[set]")); + read_only_->SetText(ASCIIToUTF16("[set]")); } else if (sender == set_style_) { if (!name_->text().empty()) { name_->SetColor(SK_ColorGREEN); diff --git a/ui/views/examples/textfield_example.h b/ui/views/examples/textfield_example.h index 9b994ef..2f3b382 100644 --- a/ui/views/examples/textfield_example.h +++ b/ui/views/examples/textfield_example.h @@ -46,6 +46,7 @@ class TextfieldExample : public ExampleBase, // Textfields for name and password. Textfield* name_; Textfield* password_; + Textfield* read_only_; // Various buttons to control textfield. LabelButton* show_password_; |