Home | History | Annotate | Download | only in views
      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/confirm_bubble_views.h"
      6 
      7 #include "chrome/browser/ui/confirm_bubble.h"
      8 #include "chrome/browser/ui/confirm_bubble_model.h"
      9 #include "chrome/browser/ui/views/constrained_window_views.h"
     10 #include "ui/views/controls/label.h"
     11 #include "ui/views/controls/link.h"
     12 #include "ui/views/layout/grid_layout.h"
     13 #include "ui/views/layout/layout_constants.h"
     14 #include "ui/views/widget/widget.h"
     15 
     16 ConfirmBubbleViews::ConfirmBubbleViews(ConfirmBubbleModel* model)
     17     : model_(model),
     18       link_(NULL) {
     19   views::GridLayout* layout = views::GridLayout::CreatePanel(this);
     20   SetLayoutManager(layout);
     21 
     22   // Use a fixed maximum message width, so longer messages will wrap.
     23   const int kMaxMessageWidth = 400;
     24   views::ColumnSet* cs = layout->AddColumnSet(0);
     25   cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
     26                 views::GridLayout::FIXED, kMaxMessageWidth, false);
     27 
     28   // Add the message label.
     29   views::Label* label = new views::Label(model_->GetMessageText());
     30   DCHECK(!label->text().empty());
     31   label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     32   label->SetMultiLine(true);
     33   label->SizeToFit(kMaxMessageWidth);
     34   layout->StartRow(0, 0);
     35   layout->AddView(label);
     36 
     37   // Initialize the link.
     38   link_ = new views::Link(model_->GetLinkText());
     39   link_->set_listener(this);
     40   link_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     41 }
     42 
     43 ConfirmBubbleViews::~ConfirmBubbleViews() {
     44 }
     45 
     46 base::string16 ConfirmBubbleViews::GetDialogButtonLabel(
     47     ui::DialogButton button) const {
     48   switch (button) {
     49     case ui::DIALOG_BUTTON_OK:
     50       return model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_OK);
     51     case ui::DIALOG_BUTTON_CANCEL:
     52       return model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_CANCEL);
     53     default:
     54       NOTREACHED();
     55       return DialogDelegateView::GetDialogButtonLabel(button);
     56   }
     57 }
     58 
     59 bool ConfirmBubbleViews::IsDialogButtonEnabled(ui::DialogButton button) const {
     60   switch (button) {
     61     case ui::DIALOG_BUTTON_OK:
     62       return !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_OK);
     63     case ui::DIALOG_BUTTON_CANCEL:
     64       return !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_CANCEL);
     65     default:
     66       NOTREACHED();
     67       return false;
     68   }
     69 }
     70 
     71 views::View* ConfirmBubbleViews::CreateExtraView() {
     72   return link_;
     73 }
     74 
     75 bool ConfirmBubbleViews::Cancel() {
     76   model_->Cancel();
     77   return true;
     78 }
     79 
     80 bool ConfirmBubbleViews::Accept() {
     81   model_->Accept();
     82   return true;
     83 }
     84 
     85 ui::ModalType ConfirmBubbleViews::GetModalType() const {
     86   return ui::MODAL_TYPE_WINDOW;
     87 }
     88 
     89 base::string16 ConfirmBubbleViews::GetWindowTitle() const {
     90   return model_->GetTitle();
     91 }
     92 
     93 void ConfirmBubbleViews::LinkClicked(views::Link* source, int event_flags) {
     94   if (source == link_) {
     95     model_->LinkClicked();
     96     GetWidget()->Close();
     97   }
     98 }
     99 
    100 namespace chrome {
    101 
    102 void ShowConfirmBubble(gfx::NativeWindow window,
    103                        gfx::NativeView anchor_view,
    104                        const gfx::Point& origin,
    105                        ConfirmBubbleModel* model) {
    106   CreateBrowserModalDialogViews(new ConfirmBubbleViews(model), window)->Show();
    107 }
    108 
    109 }  // namespace chrome
    110