echo_dialog_view.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright 2013 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/chromeos/ui/echo_dialog_view.h"
6
7#include "chrome/browser/chromeos/ui/echo_dialog_listener.h"
8#include "grit/generated_resources.h"
9#include "ui/base/l10n/l10n_util.h"
10#include "ui/views/controls/styled_label.h"
11#include "ui/views/widget/widget.h"
12#include "ui/views/window/dialog_client_view.h"
13
14namespace {
15
16const int kDialogLabelTopInset = 20;
17const int kDialogLabelLeftInset = 20;
18const int kDialogLabelBottomInset = 20;
19const int kDialogLabelRightInset = 100;
20
21const int kDialogLabelPreferredWidth =
22    350 + kDialogLabelLeftInset + kDialogLabelRightInset;
23
24}  // namespace
25
26namespace chromeos {
27
28EchoDialogView::EchoDialogView(EchoDialogListener* listener)
29    : label_(NULL),
30      listener_(listener),
31      ok_button_label_id_(0),
32      cancel_button_label_id_(0) {
33}
34
35EchoDialogView::~EchoDialogView() {}
36
37void EchoDialogView::InitForEnabledEcho(const string16& service_name,
38                                        const string16& origin) {
39  ok_button_label_id_ = IDS_OFFERS_CONSENT_INFOBAR_ENABLE_BUTTON;
40  cancel_button_label_id_ = IDS_OFFERS_CONSENT_INFOBAR_DISABLE_BUTTON;
41
42  string16 link =
43      l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE);
44
45  std::vector<size_t> offsets;
46  string16 text = l10n_util::GetStringFUTF16(IDS_ECHO_CONSENT_DIALOG_TEXT,
47                                             service_name,
48                                             link,
49                                             &offsets);
50
51  // TODO(tbarzic): Set style for service_name substring.
52
53  label_ = new views::StyledLabel(text, this);
54  label_->AddLink(ui::Range(offsets[1], offsets[1] + link.length()));
55
56  SetLabelBorderAndBounds();
57
58  AddChildView(label_);
59}
60
61void EchoDialogView::InitForDisabledEcho() {
62  ok_button_label_id_ = 0;
63  cancel_button_label_id_ = IDS_ECHO_CONSENT_DISMISS_BUTTON;
64
65  string16 link =
66      l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE);
67
68  size_t offset;
69  string16 text = l10n_util::GetStringFUTF16(
70      IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT, link, &offset);
71
72  label_ = new views::StyledLabel(text, this);
73  label_->AddLink(ui::Range(offset, offset + link.length()));
74
75  SetLabelBorderAndBounds();
76
77  AddChildView(label_);
78}
79
80void EchoDialogView::Show(gfx::NativeWindow parent) {
81  DCHECK(cancel_button_label_id_);
82
83  views::DialogDelegateView::CreateDialogWidget(this, parent, parent);
84  GetWidget()->SetSize(GetWidget()->GetRootView()->GetPreferredSize());
85  GetWidget()->Show();
86}
87
88int EchoDialogView::GetDefaultDialogButton() const {
89  return ui::DIALOG_BUTTON_NONE;
90}
91
92int EchoDialogView::GetDialogButtons() const {
93  int buttons = ui::DIALOG_BUTTON_NONE;
94  if (ok_button_label_id_)
95    buttons |= ui::DIALOG_BUTTON_OK;
96  if (cancel_button_label_id_)
97    buttons |= ui::DIALOG_BUTTON_CANCEL;
98  return buttons;
99}
100
101bool EchoDialogView::Accept() {
102  if (listener_) {
103    listener_->OnAccept();
104    listener_ = NULL;
105  }
106  return true;
107}
108
109bool EchoDialogView::Cancel() {
110  if (listener_) {
111    listener_->OnCancel();
112    listener_ = NULL;
113  }
114  return true;
115}
116
117string16 EchoDialogView::GetDialogButtonLabel(ui::DialogButton button) const {
118  if (button == ui::DIALOG_BUTTON_OK && ok_button_label_id_)
119    return l10n_util::GetStringUTF16(ok_button_label_id_);
120  if (button == ui::DIALOG_BUTTON_CANCEL && cancel_button_label_id_)
121    return l10n_util::GetStringUTF16(cancel_button_label_id_);
122  return string16();
123}
124
125ui::ModalType EchoDialogView::GetModalType() const {
126  return ui::MODAL_TYPE_WINDOW;
127}
128
129bool EchoDialogView::ShouldShowWindowTitle() const {
130  return false;
131}
132
133bool EchoDialogView::ShouldShowWindowIcon() const {
134  return false;
135}
136
137void EchoDialogView::StyledLabelLinkClicked(const ui::Range& range,
138                                            int event_flags) {
139  if (!listener_)
140    return;
141  listener_->OnMoreInfoLinkClicked();
142}
143
144gfx::Size EchoDialogView::GetPreferredSize() {
145  gfx::Size size =
146      gfx::Size(kDialogLabelPreferredWidth,
147                label_->GetHeightForWidth(kDialogLabelPreferredWidth));
148  gfx::Insets insets = GetInsets();
149  size.Enlarge(insets.width(), insets.height());
150  return size;
151}
152
153void EchoDialogView::SetLabelBorderAndBounds() {
154  label_->set_border(views::Border::CreateEmptyBorder(
155      kDialogLabelTopInset,
156      kDialogLabelLeftInset,
157      kDialogLabelBottomInset,
158      kDialogLabelRightInset));
159
160  label_->SetBounds(label_->x(),
161                    label_->y(),
162                    kDialogLabelPreferredWidth,
163                    label_->GetHeightForWidth(kDialogLabelPreferredWidth));
164}
165
166}  // namespace chromeos
167