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.
5 #include "chrome/browser/chromeos/ui/echo_dialog_view.h"
7 #include "chrome/browser/chromeos/ui/echo_dialog_listener.h"
8 #include "chrome/grit/generated_resources.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/gfx/font.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/styled_label.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_client_view.h"
18 const int kDialogLabelTopInset
= 20;
19 const int kDialogLabelLeftInset
= 20;
20 const int kDialogLabelBottomInset
= 20;
21 const int kDialogLabelRightInset
= 100;
23 const int kDialogLabelPreferredWidth
=
24 350 + kDialogLabelLeftInset
+ kDialogLabelRightInset
;
30 EchoDialogView::EchoDialogView(EchoDialogListener
* listener
)
33 ok_button_label_id_(0),
34 cancel_button_label_id_(0) {
37 EchoDialogView::~EchoDialogView() {}
39 void EchoDialogView::InitForEnabledEcho(const base::string16
& service_name
,
40 const base::string16
& origin
) {
41 ok_button_label_id_
= IDS_OFFERS_CONSENT_INFOBAR_ENABLE_BUTTON
;
42 cancel_button_label_id_
= IDS_OFFERS_CONSENT_INFOBAR_DISABLE_BUTTON
;
45 l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE
);
47 std::vector
<size_t> offsets
;
48 base::string16 text
= l10n_util::GetStringFUTF16(IDS_ECHO_CONSENT_DIALOG_TEXT
,
53 label_
= new views::StyledLabel(text
, this);
55 views::StyledLabel::RangeStyleInfo service_name_style
;
56 service_name_style
.font_style
= gfx::Font::UNDERLINE
;
57 service_name_style
.tooltip
= origin
;
58 label_
->AddStyleRange(
59 gfx::Range(offsets
[0], offsets
[0] + service_name
.length()),
62 views::StyledLabel::RangeStyleInfo link_style
=
63 views::StyledLabel::RangeStyleInfo::CreateForLink();
64 link_style
.font_style
= gfx::Font::NORMAL
;
65 label_
->AddStyleRange(gfx::Range(offsets
[1], offsets
[1] + link
.length()),
68 SetLabelBorderAndBounds();
73 void EchoDialogView::InitForDisabledEcho() {
74 ok_button_label_id_
= 0;
75 cancel_button_label_id_
= IDS_ECHO_CONSENT_DISMISS_BUTTON
;
78 l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE
);
81 base::string16 text
= l10n_util::GetStringFUTF16(
82 IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT
, link
, &offset
);
84 label_
= new views::StyledLabel(text
, this);
86 views::StyledLabel::RangeStyleInfo link_style
=
87 views::StyledLabel::RangeStyleInfo::CreateForLink();
88 link_style
.font_style
= gfx::Font::NORMAL
;
89 label_
->AddStyleRange(gfx::Range(offset
, offset
+ link
.length()), link_style
);
91 SetLabelBorderAndBounds();
96 void EchoDialogView::Show(gfx::NativeWindow parent
) {
97 DCHECK(cancel_button_label_id_
);
99 views::DialogDelegate::CreateDialogWidget(this, parent
, parent
);
100 GetWidget()->SetSize(GetWidget()->GetRootView()->GetPreferredSize());
104 int EchoDialogView::GetDefaultDialogButton() const {
105 return ui::DIALOG_BUTTON_NONE
;
108 int EchoDialogView::GetDialogButtons() const {
109 int buttons
= ui::DIALOG_BUTTON_NONE
;
110 if (ok_button_label_id_
)
111 buttons
|= ui::DIALOG_BUTTON_OK
;
112 if (cancel_button_label_id_
)
113 buttons
|= ui::DIALOG_BUTTON_CANCEL
;
117 bool EchoDialogView::Accept() {
119 listener_
->OnAccept();
125 bool EchoDialogView::Cancel() {
127 listener_
->OnCancel();
133 base::string16
EchoDialogView::GetDialogButtonLabel(
134 ui::DialogButton button
) const {
135 if (button
== ui::DIALOG_BUTTON_OK
&& ok_button_label_id_
)
136 return l10n_util::GetStringUTF16(ok_button_label_id_
);
137 if (button
== ui::DIALOG_BUTTON_CANCEL
&& cancel_button_label_id_
)
138 return l10n_util::GetStringUTF16(cancel_button_label_id_
);
139 return base::string16();
142 ui::ModalType
EchoDialogView::GetModalType() const {
143 return ui::MODAL_TYPE_WINDOW
;
146 bool EchoDialogView::ShouldShowWindowTitle() const {
150 bool EchoDialogView::ShouldShowWindowIcon() const {
154 void EchoDialogView::StyledLabelLinkClicked(const gfx::Range
& range
,
158 listener_
->OnMoreInfoLinkClicked();
161 gfx::Size
EchoDialogView::GetPreferredSize() const {
163 gfx::Size(kDialogLabelPreferredWidth
,
164 label_
->GetHeightForWidth(kDialogLabelPreferredWidth
));
165 gfx::Insets insets
= GetInsets();
166 size
.Enlarge(insets
.width(), insets
.height());
170 void EchoDialogView::SetLabelBorderAndBounds() {
171 label_
->SetBorder(views::Border::CreateEmptyBorder(kDialogLabelTopInset
,
172 kDialogLabelLeftInset
,
173 kDialogLabelBottomInset
,
174 kDialogLabelRightInset
));
176 label_
->SetBounds(label_
->x(),
178 kDialogLabelPreferredWidth
,
179 label_
->GetHeightForWidth(kDialogLabelPreferredWidth
));
182 } // namespace chromeos