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.
5 #include "chrome/browser/ui/views/login_view.h"
7 #include "grit/generated_resources.h"
8 #include "ui/base/l10n/l10n_util.h"
9 #include "ui/views/controls/label.h"
10 #include "ui/views/controls/textfield/textfield.h"
11 #include "ui/views/layout/grid_layout.h"
12 #include "ui/views/layout/layout_constants.h"
14 static const int kMessageWidth
= 320;
15 static const int kTextfieldStackHorizontalSpacing
= 30;
17 using password_manager::LoginModel
;
18 using views::GridLayout
;
20 ///////////////////////////////////////////////////////////////////////////////
23 LoginView::LoginView(const base::string16
& explanation
,
25 : username_field_(new views::Textfield()),
26 password_field_(new views::Textfield()),
27 username_label_(new views::Label(
28 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD
))),
29 password_label_(new views::Label(
30 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD
))),
31 message_label_(new views::Label(explanation
)),
33 password_field_
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
34 message_label_
->SetMultiLine(true);
35 message_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
36 message_label_
->SetAllowCharacterBreak(true);
38 // Initialize the Grid Layout Manager used for this dialog box.
39 GridLayout
* layout
= GridLayout::CreatePanel(this);
40 SetLayoutManager(layout
);
42 // Add the column set for the information message at the top of the dialog
44 const int single_column_view_set_id
= 0;
45 views::ColumnSet
* column_set
=
46 layout
->AddColumnSet(single_column_view_set_id
);
47 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
48 GridLayout::FIXED
, kMessageWidth
, 0);
50 // Add the column set for the user name and password fields and labels.
51 const int labels_column_set_id
= 1;
52 column_set
= layout
->AddColumnSet(labels_column_set_id
);
53 column_set
->AddPaddingColumn(0, kTextfieldStackHorizontalSpacing
);
54 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::CENTER
, 0,
55 GridLayout::USE_PREF
, 0, 0);
56 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
57 column_set
->AddColumn(GridLayout::FILL
, GridLayout::CENTER
, 1,
58 GridLayout::USE_PREF
, 0, 0);
59 column_set
->AddPaddingColumn(0, kTextfieldStackHorizontalSpacing
);
61 layout
->StartRow(0, single_column_view_set_id
);
62 layout
->AddView(message_label_
);
64 layout
->AddPaddingRow(0, views::kUnrelatedControlLargeVerticalSpacing
);
66 layout
->StartRow(0, labels_column_set_id
);
67 layout
->AddView(username_label_
);
68 layout
->AddView(username_field_
);
70 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
72 layout
->StartRow(0, labels_column_set_id
);
73 layout
->AddView(password_label_
);
74 layout
->AddView(password_field_
);
76 layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
79 login_model_
->AddObserver(this);
82 LoginView::~LoginView() {
84 login_model_
->RemoveObserver(this);
87 const base::string16
& LoginView::GetUsername() const {
88 return username_field_
->text();
91 const base::string16
& LoginView::GetPassword() const {
92 return password_field_
->text();
95 views::View
* LoginView::GetInitiallyFocusedView() {
96 return username_field_
;
99 ///////////////////////////////////////////////////////////////////////////////
100 // LoginView, views::View, password_manager::LoginModelObserver overrides:
102 void LoginView::OnAutofillDataAvailable(const base::string16
& username
,
103 const base::string16
& password
) {
104 if (username_field_
->text().empty()) {
105 username_field_
->SetText(username
);
106 password_field_
->SetText(password
);
107 username_field_
->SelectAll(true);
111 void LoginView::OnLoginModelDestroying() {
112 login_model_
->RemoveObserver(this);