OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/confirm_bubble_views.h" | 5 #include "chrome/browser/ui/views/confirm_bubble_views.h" |
6 | 6 |
7 #include "chrome/browser/ui/confirm_bubble.h" | 7 #include "chrome/browser/ui/confirm_bubble.h" |
8 #include "chrome/browser/ui/confirm_bubble_model.h" | 8 #include "chrome/browser/ui/confirm_bubble_model.h" |
9 #include "grit/ui_resources.h" | |
10 #include "ui/base/resource/resource_bundle.h" | |
11 #include "ui/gfx/image/image.h" | |
12 #include "ui/views/controls/button/image_button.h" | |
13 #include "ui/views/controls/button/text_button.h" | |
14 #include "ui/views/controls/image_view.h" | |
15 #include "ui/views/controls/label.h" | 9 #include "ui/views/controls/label.h" |
16 #include "ui/views/controls/link.h" | 10 #include "ui/views/controls/link.h" |
17 #include "ui/views/layout/grid_layout.h" | 11 #include "ui/views/layout/grid_layout.h" |
18 #include "ui/views/layout/layout_constants.h" | 12 #include "ui/views/layout/layout_constants.h" |
19 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
20 | 14 |
21 namespace { | 15 ConfirmBubbleViews::ConfirmBubbleViews(ConfirmBubbleModel* model) |
| 16 : model_(model), |
| 17 link_(NULL) { |
| 18 views::GridLayout* layout = new views::GridLayout(this); |
| 19 // TODO(msw): Use layout constants and fix the new-style sizing. |
| 20 layout->SetInsets(UseNewStyle() ? gfx::Insets(0, 0, 40, 0) : |
| 21 gfx::Insets(views::kUnrelatedControlVerticalSpacing, |
| 22 views::kUnrelatedControlHorizontalSpacing, |
| 23 views::kUnrelatedControlVerticalSpacing, |
| 24 views::kUnrelatedControlHorizontalSpacing)); |
| 25 SetLayoutManager(layout); |
22 | 26 |
23 // Maximum width for the message field. We will wrap the message text when its | 27 // Use a fixed maximum message width, so longer messages will wrap. |
24 // width is wider than this. | 28 const int kMaxMessageWidth = 400; |
25 const int kMaxMessageWidth = 400; | 29 views::ColumnSet* cs = layout->AddColumnSet(0); |
| 30 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
| 31 views::GridLayout::FIXED, kMaxMessageWidth, false); |
26 | 32 |
27 } // namespace | 33 // Add the message label. |
| 34 views::Label* label = new views::Label(model_->GetMessageText()); |
| 35 DCHECK(!label->text().empty()); |
| 36 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 37 label->SetMultiLine(true); |
| 38 label->SizeToFit(kMaxMessageWidth); |
| 39 layout->StartRow(0, 0); |
| 40 layout->AddView(label); |
28 | 41 |
29 ConfirmBubbleViews::ConfirmBubbleViews(gfx::NativeView parent, | 42 // Initialize the link. |
30 const gfx::Point& anchor_point, | 43 link_ = new views::Link(model_->GetLinkText()); |
31 ConfirmBubbleModel* model) | 44 link_->set_listener(this); |
32 : BubbleDelegateView(NULL, views::BubbleBorder::NONE), | 45 link_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
33 model_(model) { | |
34 DCHECK(model); | |
35 set_anchor_point(anchor_point); | |
36 set_parent_window(parent); | |
37 } | 46 } |
38 | 47 |
39 ConfirmBubbleViews::~ConfirmBubbleViews() { | 48 ConfirmBubbleViews::~ConfirmBubbleViews() { |
40 } | 49 } |
41 | 50 |
42 void ConfirmBubbleViews::ButtonPressed(views::Button* sender, | 51 string16 ConfirmBubbleViews::GetDialogButtonLabel( |
43 const ui::Event& event) { | 52 ui::DialogButton button) const { |
44 if (sender->tag() == ConfirmBubbleModel::BUTTON_OK) | 53 switch (button) { |
45 model_->Accept(); | 54 case ui::DIALOG_BUTTON_OK: |
46 else if (sender->tag() == ConfirmBubbleModel::BUTTON_CANCEL) | 55 return model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_OK); |
47 model_->Cancel(); | 56 case ui::DIALOG_BUTTON_CANCEL: |
48 GetWidget()->Close(); | 57 return model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_CANCEL); |
| 58 default: |
| 59 NOTREACHED(); |
| 60 return DialogDelegateView::GetDialogButtonLabel(button); |
| 61 } |
| 62 } |
| 63 |
| 64 bool ConfirmBubbleViews::IsDialogButtonEnabled(ui::DialogButton button) const { |
| 65 return IsDialogButtonVisible(button); |
| 66 } |
| 67 |
| 68 bool ConfirmBubbleViews::IsDialogButtonVisible(ui::DialogButton button) const { |
| 69 switch (button) { |
| 70 case ui::DIALOG_BUTTON_OK: |
| 71 return !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_OK); |
| 72 case ui::DIALOG_BUTTON_CANCEL: |
| 73 return !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_CANCEL); |
| 74 default: |
| 75 NOTREACHED(); |
| 76 return false; |
| 77 } |
| 78 } |
| 79 |
| 80 views::View* ConfirmBubbleViews::GetExtraView() { |
| 81 return link_; |
| 82 } |
| 83 |
| 84 bool ConfirmBubbleViews::Cancel() { |
| 85 model_->Cancel(); |
| 86 return true; |
| 87 } |
| 88 |
| 89 bool ConfirmBubbleViews::Accept() { |
| 90 model_->Accept(); |
| 91 return true; |
| 92 } |
| 93 |
| 94 ui::ModalType ConfirmBubbleViews::GetModalType() const { |
| 95 return ui::MODAL_TYPE_WINDOW; |
| 96 } |
| 97 |
| 98 string16 ConfirmBubbleViews::GetWindowTitle() const { |
| 99 return model_->GetTitle(); |
49 } | 100 } |
50 | 101 |
51 void ConfirmBubbleViews::LinkClicked(views::Link* source, int event_flags) { | 102 void ConfirmBubbleViews::LinkClicked(views::Link* source, int event_flags) { |
52 model_->LinkClicked(); | 103 if (source == link_) { |
53 } | 104 model_->LinkClicked(); |
54 | 105 GetWidget()->Close(); |
55 gfx::Rect ConfirmBubbleViews::GetAnchorRect() { | |
56 return gfx::Rect(anchor_point(), gfx::Size()); | |
57 } | |
58 | |
59 void ConfirmBubbleViews::Init() { | |
60 views::GridLayout* layout = new views::GridLayout(this); | |
61 SetLayoutManager(layout); | |
62 | |
63 // Add the icon, the title label and the close button to the first row. | |
64 views::ColumnSet* cs = layout->AddColumnSet(0); | |
65 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
66 views::GridLayout::USE_PREF, 0, 0); | |
67 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | |
68 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
69 views::GridLayout::USE_PREF, 0, 0); | |
70 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); | |
71 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
72 views::GridLayout::USE_PREF, 0, 0); | |
73 | |
74 layout->StartRow(0, 0); | |
75 gfx::Image* icon_image = model_->GetIcon(); | |
76 DCHECK(icon_image); | |
77 views::ImageView* icon_view = new views::ImageView; | |
78 icon_view->SetImage(icon_image->ToImageSkia()); | |
79 layout->AddView(icon_view); | |
80 | |
81 const string16 title_text = model_->GetTitle(); | |
82 DCHECK(!title_text.empty()); | |
83 views::Label* title_label = new views::Label(title_text); | |
84 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
85 title_label->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); | |
86 layout->AddView(title_label); | |
87 | |
88 views::ImageButton* close_button = new views::ImageButton(this); | |
89 close_button->SetImage(views::CustomButton::STATE_NORMAL, | |
90 rb.GetImageSkiaNamed(IDR_CLOSE_DIALOG)); | |
91 close_button->SetImage(views::CustomButton::STATE_HOVERED, | |
92 rb.GetImageSkiaNamed(IDR_CLOSE_DIALOG_H)); | |
93 close_button->SetImage(views::CustomButton::STATE_PRESSED, | |
94 rb.GetImageSkiaNamed(IDR_CLOSE_DIALOG_P)); | |
95 close_button->set_tag(ConfirmBubbleModel::BUTTON_NONE); | |
96 layout->AddView(close_button); | |
97 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
98 | |
99 // Add the message label to the second row. | |
100 cs = layout->AddColumnSet(1); | |
101 const string16 message_text = model_->GetMessageText(); | |
102 DCHECK(!message_text.empty()); | |
103 views::Label* message_label = new views::Label(message_text); | |
104 int message_width = | |
105 std::min(kMaxMessageWidth, message_label->GetPreferredSize().width()); | |
106 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | |
107 views::GridLayout::FIXED, message_width, false); | |
108 message_label->SetBounds(0, 0, message_width, 0); | |
109 message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
110 message_label->SetMultiLine(true); | |
111 layout->StartRow(0, 1); | |
112 layout->AddView(message_label); | |
113 | |
114 // Add the the link label to the third row if it exists. | |
115 const string16 link_text = model_->GetLinkText(); | |
116 if (!link_text.empty()) { | |
117 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
118 layout->StartRow(0, 1); | |
119 views::Link* link_label = new views::Link(link_text); | |
120 link_label->set_listener(this); | |
121 layout->AddView(link_label); | |
122 } | |
123 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); | |
124 | |
125 // Add the ok button and the cancel button to the third (or fourth) row if we | |
126 // have either of them. | |
127 bool has_ok_button = !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_OK); | |
128 bool has_cancel_button = | |
129 !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_CANCEL); | |
130 if (has_ok_button || has_cancel_button) { | |
131 cs = layout->AddColumnSet(2); | |
132 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); | |
133 if (has_ok_button) { | |
134 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
135 views::GridLayout::USE_PREF, 0, 0); | |
136 if (has_cancel_button) | |
137 cs->AddPaddingColumn(0, views::kRelatedButtonHSpacing); | |
138 } | |
139 if (has_cancel_button) { | |
140 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | |
141 views::GridLayout::USE_PREF, 0, 0); | |
142 } | |
143 layout->StartRow(0, 2); | |
144 if (has_ok_button) { | |
145 views::TextButton* ok_button = new views::NativeTextButton( | |
146 this, model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_OK)); | |
147 ok_button->set_tag(ConfirmBubbleModel::BUTTON_OK); | |
148 layout->AddView(ok_button); | |
149 } | |
150 if (has_cancel_button) { | |
151 views::TextButton* cancel_button = new views::NativeTextButton( | |
152 this, model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_CANCEL)); | |
153 cancel_button->set_tag(ConfirmBubbleModel::BUTTON_CANCEL); | |
154 layout->AddView(cancel_button); | |
155 } | |
156 } | 106 } |
157 } | 107 } |
158 | 108 |
159 namespace chrome { | 109 namespace chrome { |
160 | 110 |
161 void ShowConfirmBubble(gfx::NativeView view, | 111 void ShowConfirmBubble(gfx::NativeView view, |
162 const gfx::Point& origin, | 112 const gfx::Point& origin, |
163 ConfirmBubbleModel* model) { | 113 ConfirmBubbleModel* model) { |
164 ConfirmBubbleViews* bubble = new ConfirmBubbleViews(view, origin, model); | 114 views::DialogDelegateView::CreateDialogWidget( |
165 views::BubbleDelegateView::CreateBubble(bubble); | 115 new ConfirmBubbleViews(model), NULL, view)->Show(); |
166 bubble->Show(); | |
167 } | 116 } |
168 | 117 |
169 } // namespace chrome | 118 } // namespace chrome |
OLD | NEW |