Home | History | Annotate | Download | only in ui
      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/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"
     15 
     16 namespace {
     17 
     18 const int kDialogLabelTopInset = 20;
     19 const int kDialogLabelLeftInset = 20;
     20 const int kDialogLabelBottomInset = 20;
     21 const int kDialogLabelRightInset = 100;
     22 
     23 const int kDialogLabelPreferredWidth =
     24     350 + kDialogLabelLeftInset + kDialogLabelRightInset;
     25 
     26 }  // namespace
     27 
     28 namespace chromeos {
     29 
     30 EchoDialogView::EchoDialogView(EchoDialogListener* listener)
     31     : label_(NULL),
     32       listener_(listener),
     33       ok_button_label_id_(0),
     34       cancel_button_label_id_(0) {
     35 }
     36 
     37 EchoDialogView::~EchoDialogView() {}
     38 
     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;
     43 
     44   base::string16 link =
     45       l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE);
     46 
     47   std::vector<size_t> offsets;
     48   base::string16 text = l10n_util::GetStringFUTF16(IDS_ECHO_CONSENT_DIALOG_TEXT,
     49                                              service_name,
     50                                              link,
     51                                              &offsets);
     52 
     53   label_ = new views::StyledLabel(text, this);
     54 
     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()),
     60       service_name_style);
     61 
     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()),
     66                         link_style);
     67 
     68   SetLabelBorderAndBounds();
     69 
     70   AddChildView(label_);
     71 }
     72 
     73 void EchoDialogView::InitForDisabledEcho() {
     74   ok_button_label_id_ = 0;
     75   cancel_button_label_id_ = IDS_ECHO_CONSENT_DISMISS_BUTTON;
     76 
     77   base::string16 link =
     78       l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL_LEARN_MORE);
     79 
     80   size_t offset;
     81   base::string16 text = l10n_util::GetStringFUTF16(
     82       IDS_ECHO_DISABLED_CONSENT_DIALOG_TEXT, link, &offset);
     83 
     84   label_ = new views::StyledLabel(text, this);
     85 
     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);
     90 
     91   SetLabelBorderAndBounds();
     92 
     93   AddChildView(label_);
     94 }
     95 
     96 void EchoDialogView::Show(gfx::NativeWindow parent) {
     97   DCHECK(cancel_button_label_id_);
     98 
     99   views::DialogDelegate::CreateDialogWidget(this, parent, parent);
    100   GetWidget()->SetSize(GetWidget()->GetRootView()->GetPreferredSize());
    101   GetWidget()->Show();
    102 }
    103 
    104 int EchoDialogView::GetDefaultDialogButton() const {
    105   return ui::DIALOG_BUTTON_NONE;
    106 }
    107 
    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;
    114   return buttons;
    115 }
    116 
    117 bool EchoDialogView::Accept() {
    118   if (listener_) {
    119     listener_->OnAccept();
    120     listener_ = NULL;
    121   }
    122   return true;
    123 }
    124 
    125 bool EchoDialogView::Cancel() {
    126   if (listener_) {
    127     listener_->OnCancel();
    128     listener_ = NULL;
    129   }
    130   return true;
    131 }
    132 
    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();
    140 }
    141 
    142 ui::ModalType EchoDialogView::GetModalType() const {
    143   return ui::MODAL_TYPE_WINDOW;
    144 }
    145 
    146 bool EchoDialogView::ShouldShowWindowTitle() const {
    147   return false;
    148 }
    149 
    150 bool EchoDialogView::ShouldShowWindowIcon() const {
    151   return false;
    152 }
    153 
    154 void EchoDialogView::StyledLabelLinkClicked(const gfx::Range& range,
    155                                             int event_flags) {
    156   if (!listener_)
    157     return;
    158   listener_->OnMoreInfoLinkClicked();
    159 }
    160 
    161 gfx::Size EchoDialogView::GetPreferredSize() {
    162   gfx::Size size =
    163       gfx::Size(kDialogLabelPreferredWidth,
    164                 label_->GetHeightForWidth(kDialogLabelPreferredWidth));
    165   gfx::Insets insets = GetInsets();
    166   size.Enlarge(insets.width(), insets.height());
    167   return size;
    168 }
    169 
    170 void EchoDialogView::SetLabelBorderAndBounds() {
    171   label_->set_border(views::Border::CreateEmptyBorder(
    172       kDialogLabelTopInset,
    173       kDialogLabelLeftInset,
    174       kDialogLabelBottomInset,
    175       kDialogLabelRightInset));
    176 
    177   label_->SetBounds(label_->x(),
    178                     label_->y(),
    179                     kDialogLabelPreferredWidth,
    180                     label_->GetHeightForWidth(kDialogLabelPreferredWidth));
    181 }
    182 
    183 }  // namespace chromeos
    184