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"
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"
20 static const int kMessageWidth
= 320;
21 static const int kTextfieldStackHorizontalSpacing
= 30;
23 using views::GridLayout
;
25 ///////////////////////////////////////////////////////////////////////////////
28 LoginView::LoginView(const base::string16
& explanation
,
30 : username_field_(new views::Textfield()),
31 password_field_(new views::Textfield()),
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
)),
38 password_field_
->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD
);
39 message_label_
->SetMultiLine(true);
40 message_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
41 message_label_
->SetAllowCharacterBreak(true);
43 // Initialize the Grid Layout Manager used for this dialog box.
44 GridLayout
* layout
= GridLayout::CreatePanel(this);
45 SetLayoutManager(layout
);
47 // Add the column set for the information message at the top of the dialog
49 const int single_column_view_set_id
= 0;
50 views::ColumnSet
* column_set
=
51 layout
->AddColumnSet(single_column_view_set_id
);
52 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
53 GridLayout::FIXED
, kMessageWidth
, 0);
55 // Add the column set for the user name and password fields and labels.
56 const int labels_column_set_id
= 1;
57 column_set
= layout
->AddColumnSet(labels_column_set_id
);
58 column_set
->AddPaddingColumn(0, kTextfieldStackHorizontalSpacing
);
59 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::CENTER
, 0,
60 GridLayout::USE_PREF
, 0, 0);
61 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
62 column_set
->AddColumn(GridLayout::FILL
, GridLayout::CENTER
, 1,
63 GridLayout::USE_PREF
, 0, 0);
64 column_set
->AddPaddingColumn(0, kTextfieldStackHorizontalSpacing
);
66 layout
->StartRow(0, single_column_view_set_id
);
67 layout
->AddView(message_label_
);
69 layout
->AddPaddingRow(0, views::kUnrelatedControlLargeVerticalSpacing
);
71 layout
->StartRow(0, labels_column_set_id
);
72 layout
->AddView(username_label_
);
73 layout
->AddView(username_field_
);
75 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
77 layout
->StartRow(0, labels_column_set_id
);
78 layout
->AddView(password_label_
);
79 layout
->AddView(password_field_
);
81 layout
->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing
);
84 login_model_
->AddObserver(this);
87 LoginView::~LoginView() {
89 login_model_
->RemoveObserver(this);
92 base::string16
LoginView::GetUsername() {
93 return username_field_
->text();
96 base::string16
LoginView::GetPassword() {
97 return password_field_
->text();
100 views::View
* LoginView::GetInitiallyFocusedView() {
101 return username_field_
;
104 ///////////////////////////////////////////////////////////////////////////////
105 // LoginView, views::View, views::LoginModelObserver overrides:
107 void LoginView::OnAutofillDataAvailable(const base::string16
& username
,
108 const base::string16
& password
) {
109 if (username_field_
->text().empty()) {
110 username_field_
->SetText(username
);
111 password_field_
->SetText(password
);
112 username_field_
->SelectAll(true);
116 void LoginView::OnLoginModelDestroying() {
117 login_model_
->RemoveObserver(this);