OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_view.h" | 5 #include "chrome/browser/ui/views/confirm_bubble_view.h" |
6 | 6 |
7 #include "chrome/browser/ui/confirm_bubble_model.h" | 7 #include "chrome/browser/ui/confirm_bubble_model.h" |
8 #include "grit/theme_resources.h" | 8 #include "grit/theme_resources.h" |
9 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
10 #include "ui/gfx/image/image.h" | 10 #include "ui/gfx/image/image.h" |
11 #include "ui/views/controls/button/image_button.h" | 11 #include "ui/views/controls/button/image_button.h" |
12 #include "ui/views/controls/button/text_button.h" | 12 #include "ui/views/controls/button/text_button.h" |
13 #include "ui/views/controls/image_view.h" | 13 #include "ui/views/controls/image_view.h" |
14 #include "ui/views/controls/label.h" | 14 #include "ui/views/controls/label.h" |
15 #include "ui/views/controls/link.h" | 15 #include "ui/views/controls/link.h" |
16 #include "ui/views/layout/grid_layout.h" | 16 #include "ui/views/layout/grid_layout.h" |
17 #include "ui/views/layout/layout_constants.h" | 17 #include "ui/views/layout/layout_constants.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Maximum width for the message field. We will wrap the message text when its | 21 // Maximum width for the message field. We will wrap the message text when its |
22 // width is wider than this. | 22 // width is wider than this. |
23 const int kMaxMessageWidth = 400; | 23 const int kMaxMessageWidth = 400; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 ConfirmBubbleView::ConfirmBubbleView(ConfirmBubbleModel* model) | 27 ConfirmBubbleView::ConfirmBubbleView(const gfx::Point& anchor_point, |
28 : model_(model) { | 28 ConfirmBubbleModel* model) |
| 29 : BubbleDelegateView(NULL, views::BubbleBorder::NONE, SK_ColorWHITE), |
| 30 anchor_point_(anchor_point), |
| 31 model_(model) { |
29 DCHECK(model); | 32 DCHECK(model); |
30 } | 33 } |
31 | 34 |
32 ConfirmBubbleView::~ConfirmBubbleView() { | 35 ConfirmBubbleView::~ConfirmBubbleView() { |
33 } | 36 } |
34 | 37 |
35 void ConfirmBubbleView::BubbleClosing(Bubble* bubble, bool closed_by_escape) { | |
36 } | |
37 | |
38 bool ConfirmBubbleView::CloseOnEscape() { | |
39 return true; | |
40 } | |
41 | |
42 bool ConfirmBubbleView::FadeInOnShow() { | |
43 return false; | |
44 } | |
45 | |
46 void ConfirmBubbleView::ButtonPressed(views::Button* sender, | 38 void ConfirmBubbleView::ButtonPressed(views::Button* sender, |
47 const views::Event& event) { | 39 const views::Event& event) { |
48 if (sender->tag() == ConfirmBubbleModel::BUTTON_OK) | 40 if (sender->tag() == ConfirmBubbleModel::BUTTON_OK) |
49 model_->Accept(); | 41 model_->Accept(); |
50 else if (sender->tag() == ConfirmBubbleModel::BUTTON_CANCEL) | 42 else if (sender->tag() == ConfirmBubbleModel::BUTTON_CANCEL) |
51 model_->Cancel(); | 43 model_->Cancel(); |
52 GetWidget()->Close(); | 44 GetWidget()->Close(); |
53 } | 45 } |
54 | 46 |
55 void ConfirmBubbleView::LinkClicked(views::Link* source, int event_flags) { | 47 void ConfirmBubbleView::LinkClicked(views::Link* source, int event_flags) { |
56 model_->LinkClicked(); | 48 model_->LinkClicked(); |
57 } | 49 } |
58 | 50 |
59 void ConfirmBubbleView::ViewHierarchyChanged(bool is_add, | 51 gfx::Point ConfirmBubbleView::GetAnchorPoint() { |
60 views::View* parent, | 52 return anchor_point_; |
61 views::View* child) { | 53 } |
62 if (!is_add || child != this) | |
63 return; | |
64 | 54 |
| 55 void ConfirmBubbleView::Init() { |
65 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 56 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
66 views::GridLayout* layout = new views::GridLayout(this); | 57 views::GridLayout* layout = new views::GridLayout(this); |
67 SetLayoutManager(layout); | 58 SetLayoutManager(layout); |
68 | 59 |
69 // Add the icon, the title label and the close button to the first row. | 60 // Add the icon, the title label and the close button to the first row. |
70 views::ColumnSet* cs = layout->AddColumnSet(0); | 61 views::ColumnSet* cs = layout->AddColumnSet(0); |
71 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | 62 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, |
72 views::GridLayout::USE_PREF, 0, 0); | 63 views::GridLayout::USE_PREF, 0, 0); |
73 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); | 64 cs->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing); |
74 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | 65 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, |
75 views::GridLayout::USE_PREF, 0, 0); | 66 views::GridLayout::USE_PREF, 0, 0); |
76 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); | 67 cs->AddPaddingColumn(1, views::kRelatedControlHorizontalSpacing); |
77 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, | 68 cs->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER, 0, |
78 views::GridLayout::USE_PREF, 0, 0); | 69 views::GridLayout::USE_PREF, 0, 0); |
79 | 70 |
80 layout->StartRow(0, 0); | 71 layout->StartRow(0, 0); |
81 gfx::Image* icon_image = model_->GetIcon(); | 72 gfx::Image* icon_image = model_->GetIcon(); |
82 DCHECK(icon_image); | 73 DCHECK(icon_image); |
83 views::ImageView* icon_view = new views::ImageView; | 74 views::ImageView* icon_view = new views::ImageView; |
84 icon_view->SetImage(icon_image->ToSkBitmap()); | 75 icon_view->SetImage(icon_image->ToSkBitmap()); |
85 layout->AddView(icon_view); | 76 layout->AddView(icon_view); |
86 | 77 |
87 const string16 title_text = model_->GetTitle(); | 78 const string16 title_text = model_->GetTitle(); |
88 DCHECK(!title_text.empty()); | 79 DCHECK(!title_text.empty()); |
89 views::Label* title_label = new views::Label(title_text); | 80 views::Label* title_label = new views::Label(title_text); |
90 title_label->SetFont(bundle.GetFont(ResourceBundle::MediumFont)); | 81 title_label->SetFont(bundle.GetFont(ResourceBundle::MediumFont)); |
91 title_label->SetBackgroundColor(Bubble::kBackgroundColor); | |
92 layout->AddView(title_label); | 82 layout->AddView(title_label); |
93 | 83 |
94 views::ImageButton* close_button = new views::ImageButton(this); | 84 views::ImageButton* close_button = new views::ImageButton(this); |
95 const SkBitmap* close_image = | 85 const SkBitmap* close_image = |
96 bundle.GetImageNamed(IDR_INFO_BUBBLE_CLOSE).ToSkBitmap(); | 86 bundle.GetImageNamed(IDR_INFO_BUBBLE_CLOSE).ToSkBitmap(); |
97 close_button->SetImage(views::CustomButton::BS_NORMAL, close_image); | 87 close_button->SetImage(views::CustomButton::BS_NORMAL, close_image); |
98 close_button->set_tag(ConfirmBubbleModel::BUTTON_NONE); | 88 close_button->set_tag(ConfirmBubbleModel::BUTTON_NONE); |
99 layout->AddView(close_button); | 89 layout->AddView(close_button); |
100 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 90 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
101 | 91 |
102 // Add the message label to the second row. | 92 // Add the message label to the second row. |
103 cs = layout->AddColumnSet(1); | 93 cs = layout->AddColumnSet(1); |
104 const string16 message_text = model_->GetMessageText(); | 94 const string16 message_text = model_->GetMessageText(); |
105 DCHECK(!message_text.empty()); | 95 DCHECK(!message_text.empty()); |
106 views::Label* message_label = new views::Label(message_text); | 96 views::Label* message_label = new views::Label(message_text); |
107 int message_width = | 97 int message_width = |
108 std::min(kMaxMessageWidth, message_label->GetPreferredSize().width()); | 98 std::min(kMaxMessageWidth, message_label->GetPreferredSize().width()); |
109 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | 99 cs->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, |
110 views::GridLayout::FIXED, message_width, false); | 100 views::GridLayout::FIXED, message_width, false); |
111 message_label->SetBounds(0, 0, message_width, 0); | 101 message_label->SetBounds(0, 0, message_width, 0); |
112 message_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 102 message_label->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
113 message_label->SetMultiLine(true); | 103 message_label->SetMultiLine(true); |
114 message_label->SetBackgroundColor(Bubble::kBackgroundColor); | |
115 layout->StartRow(0, 1); | 104 layout->StartRow(0, 1); |
116 layout->AddView(message_label); | 105 layout->AddView(message_label); |
117 | 106 |
118 // Add the the link label to the third row if it exists. | 107 // Add the the link label to the third row if it exists. |
119 const string16 link_text = model_->GetLinkText(); | 108 const string16 link_text = model_->GetLinkText(); |
120 if (!link_text.empty()) { | 109 if (!link_text.empty()) { |
121 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 110 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
122 layout->StartRow(0, 1); | 111 layout->StartRow(0, 1); |
123 views::Link* link_label = new views::Link(link_text); | 112 views::Link* link_label = new views::Link(link_text); |
124 link_label->set_listener(this); | 113 link_label->set_listener(this); |
125 link_label->SetBackgroundColor(Bubble::kBackgroundColor); | |
126 layout->AddView(link_label); | 114 layout->AddView(link_label); |
127 } | 115 } |
128 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); | 116 layout->AddPaddingRow(0, views::kLabelToControlVerticalSpacing); |
129 | 117 |
130 // Add the ok button and the cancel button to the third (or fourth) row if we | 118 // Add the ok button and the cancel button to the third (or fourth) row if we |
131 // have either of them. | 119 // have either of them. |
132 bool has_ok_button = !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_OK); | 120 bool has_ok_button = !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_OK); |
133 bool has_cancel_button = | 121 bool has_cancel_button = |
134 !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_CANCEL); | 122 !!(model_->GetButtons() & ConfirmBubbleModel::BUTTON_CANCEL); |
135 if (has_ok_button || has_cancel_button) { | 123 if (has_ok_button || has_cancel_button) { |
(...skipping 17 matching lines...) Expand all Loading... |
153 layout->AddView(ok_button); | 141 layout->AddView(ok_button); |
154 } | 142 } |
155 if (has_cancel_button) { | 143 if (has_cancel_button) { |
156 views::TextButton* cancel_button = new views::NativeTextButton( | 144 views::TextButton* cancel_button = new views::NativeTextButton( |
157 this, model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_CANCEL)); | 145 this, model_->GetButtonLabel(ConfirmBubbleModel::BUTTON_CANCEL)); |
158 cancel_button->set_tag(ConfirmBubbleModel::BUTTON_CANCEL); | 146 cancel_button->set_tag(ConfirmBubbleModel::BUTTON_CANCEL); |
159 layout->AddView(cancel_button); | 147 layout->AddView(cancel_button); |
160 } | 148 } |
161 } | 149 } |
162 } | 150 } |
OLD | NEW |