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