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