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 "chrome/browser/ui/views/login_view.h" 6 7 #include <string> 8 9 #include "base/compiler_specific.h" 10 #include "base/message_loop.h" 11 #include "base/utf_string_conversions.h" 12 #include "grit/generated_resources.h" 13 #include "ui/base/l10n/l10n_util.h" 14 #include "views/controls/label.h" 15 #include "views/controls/textfield/textfield.h" 16 #include "views/layout/grid_layout.h" 17 #include "views/layout/layout_constants.h" 18 #include "views/widget/root_view.h" 19 20 static const int kMessageWidth = 320; 21 static const int kTextfieldStackHorizontalSpacing = 30; 22 23 using views::GridLayout; 24 25 /////////////////////////////////////////////////////////////////////////////// 26 // LoginView, public: 27 28 LoginView::LoginView(const std::wstring& explanation, LoginModel* model) 29 : username_field_(new views::Textfield), 30 password_field_(new views::Textfield(views::Textfield::STYLE_PASSWORD)), 31 username_label_(new views::Label(UTF16ToWide( 32 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD)))), 33 password_label_(new views::Label(UTF16ToWide( 34 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD)))), 35 message_label_(new views::Label(explanation)), 36 login_model_(model) { 37 message_label_->SetMultiLine(true); 38 message_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); 39 message_label_->SetAllowCharacterBreak(true); 40 41 // Initialize the Grid Layout Manager used for this dialog box. 42 GridLayout* layout = GridLayout::CreatePanel(this); 43 SetLayoutManager(layout); 44 45 // Add the column set for the information message at the top of the dialog 46 // box. 47 const int single_column_view_set_id = 0; 48 views::ColumnSet* column_set = 49 layout->AddColumnSet(single_column_view_set_id); 50 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, 51 GridLayout::FIXED, kMessageWidth, 0); 52 53 // Add the column set for the user name and password fields and labels. 54 const int labels_column_set_id = 1; 55 column_set = layout->AddColumnSet(labels_column_set_id); 56 column_set->AddPaddingColumn(0, kTextfieldStackHorizontalSpacing); 57 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, 58 GridLayout::USE_PREF, 0, 0); 59 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); 60 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, 61 GridLayout::USE_PREF, 0, 0); 62 column_set->AddPaddingColumn(0, kTextfieldStackHorizontalSpacing); 63 64 layout->StartRow(0, single_column_view_set_id); 65 layout->AddView(message_label_); 66 67 layout->AddPaddingRow(0, views::kUnrelatedControlLargeVerticalSpacing); 68 69 layout->StartRow(0, labels_column_set_id); 70 layout->AddView(username_label_); 71 layout->AddView(username_field_); 72 73 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 74 75 layout->StartRow(0, labels_column_set_id); 76 layout->AddView(password_label_); 77 layout->AddView(password_field_); 78 79 layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing); 80 81 if (login_model_) 82 login_model_->SetObserver(this); 83 } 84 85 LoginView::~LoginView() { 86 if (login_model_) 87 login_model_->SetObserver(NULL); 88 } 89 90 std::wstring LoginView::GetUsername() { 91 return username_field_->text(); 92 } 93 94 std::wstring LoginView::GetPassword() { 95 return password_field_->text(); 96 } 97 98 views::View* LoginView::GetInitiallyFocusedView() { 99 return username_field_; 100 } 101 102 /////////////////////////////////////////////////////////////////////////////// 103 // LoginView, views::View, views::LoginModelObserver overrides: 104 105 void LoginView::OnAutofillDataAvailable(const std::wstring& username, 106 const std::wstring& password) { 107 if (username_field_->text().empty()) { 108 username_field_->SetText(username); 109 password_field_->SetText(password); 110 username_field_->SelectAll(); 111 } 112 } 113