NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / ui / views / window / dialog_client_view.cc
blobd75ae1f235f069952be58e2b4fa7fe2ebb44628b
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.
5 #include "ui/views/window/dialog_client_view.h"
7 #include <algorithm>
9 #include "ui/events/keycodes/keyboard_codes.h"
10 #include "ui/views/background.h"
11 #include "ui/views/controls/button/blue_button.h"
12 #include "ui/views/controls/button/label_button.h"
13 #include "ui/views/layout/layout_constants.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/window/dialog_delegate.h"
17 namespace views {
19 namespace {
21 // The group used by the buttons. This name is chosen voluntarily big not to
22 // conflict with other groups that could be in the dialog content.
23 const int kButtonGroup = 6666;
25 #if defined(OS_WIN) || defined(OS_CHROMEOS)
26 const bool kIsOkButtonOnLeftSide = true;
27 #else
28 const bool kIsOkButtonOnLeftSide = false;
29 #endif
31 // Returns true if the given view should be shown (i.e. exists and is
32 // visible).
33 bool ShouldShow(View* view) {
34 return view && view->visible();
37 // Do the layout for a button.
38 void LayoutButton(LabelButton* button, gfx::Rect* row_bounds) {
39 if (!button)
40 return;
42 const gfx::Size size = button->GetPreferredSize();
43 row_bounds->set_width(row_bounds->width() - size.width());
44 button->SetBounds(row_bounds->right(), row_bounds->y(),
45 size.width(), row_bounds->height());
46 row_bounds->set_width(row_bounds->width() - kRelatedButtonHSpacing);
49 } // namespace
51 ///////////////////////////////////////////////////////////////////////////////
52 // DialogClientView, public:
54 DialogClientView::DialogClientView(Widget* owner, View* contents_view)
55 : ClientView(owner, contents_view),
56 ok_button_(NULL),
57 cancel_button_(NULL),
58 default_button_(NULL),
59 focus_manager_(NULL),
60 extra_view_(NULL),
61 footnote_view_(NULL),
62 notified_delegate_(false) {
65 DialogClientView::~DialogClientView() {
68 void DialogClientView::AcceptWindow() {
69 // Only notify the delegate once. See |notified_delegate_|'s comment.
70 if (!notified_delegate_ && GetDialogDelegate()->Accept(false)) {
71 notified_delegate_ = true;
72 Close();
76 void DialogClientView::CancelWindow() {
77 // Only notify the delegate once. See |notified_delegate_|'s comment.
78 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) {
79 notified_delegate_ = true;
80 Close();
84 void DialogClientView::UpdateDialogButtons() {
85 const int buttons = GetDialogDelegate()->GetDialogButtons();
86 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE);
87 if (default_button_)
88 default_button_->SetIsDefault(false);
89 default_button_ = NULL;
91 if (buttons & ui::DIALOG_BUTTON_OK) {
92 if (!ok_button_) {
93 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK);
94 if (!(buttons & ui::DIALOG_BUTTON_CANCEL))
95 ok_button_->AddAccelerator(escape);
96 AddChildView(ok_button_);
99 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK);
100 } else if (ok_button_) {
101 delete ok_button_;
102 ok_button_ = NULL;
105 if (buttons & ui::DIALOG_BUTTON_CANCEL) {
106 if (!cancel_button_) {
107 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL);
108 cancel_button_->AddAccelerator(escape);
109 AddChildView(cancel_button_);
112 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL);
113 } else if (cancel_button_) {
114 delete cancel_button_;
115 cancel_button_ = NULL;
118 // Use the escape key to close the window if there are no dialog buttons.
119 if (!has_dialog_buttons())
120 AddAccelerator(escape);
121 else
122 ResetAccelerators();
125 ///////////////////////////////////////////////////////////////////////////////
126 // DialogClientView, ClientView overrides:
128 bool DialogClientView::CanClose() {
129 if (notified_delegate_)
130 return true;
132 // The dialog is closing but no Accept or Cancel action has been performed
133 // before: it's a Close action.
134 if (GetDialogDelegate()->Close()) {
135 notified_delegate_ = true;
136 GetDialogDelegate()->OnClosed();
137 return true;
139 return false;
142 DialogClientView* DialogClientView::AsDialogClientView() {
143 return this;
146 const DialogClientView* DialogClientView::AsDialogClientView() const {
147 return this;
150 void DialogClientView::OnWillChangeFocus(View* focused_before,
151 View* focused_now) {
152 // Make the newly focused button default or restore the dialog's default.
153 const int default_button = GetDialogDelegate()->GetDefaultDialogButton();
154 LabelButton* new_default_button = NULL;
155 if (focused_now &&
156 !strcmp(focused_now->GetClassName(), LabelButton::kViewClassName)) {
157 new_default_button = static_cast<LabelButton*>(focused_now);
158 } else if (default_button == ui::DIALOG_BUTTON_OK && ok_button_) {
159 new_default_button = ok_button_;
160 } else if (default_button == ui::DIALOG_BUTTON_CANCEL && cancel_button_) {
161 new_default_button = cancel_button_;
164 if (default_button_ && default_button_ != new_default_button)
165 default_button_->SetIsDefault(false);
166 default_button_ = new_default_button;
167 if (default_button_ && !default_button_->is_default())
168 default_button_->SetIsDefault(true);
171 void DialogClientView::OnDidChangeFocus(View* focused_before,
172 View* focused_now) {
175 ////////////////////////////////////////////////////////////////////////////////
176 // DialogClientView, View overrides:
178 gfx::Size DialogClientView::GetPreferredSize() {
179 // Initialize the size to fit the buttons and extra view row.
180 gfx::Size size(
181 (ok_button_ ? ok_button_->GetPreferredSize().width() : 0) +
182 (cancel_button_ ? cancel_button_->GetPreferredSize().width() : 0) +
183 (cancel_button_ && ok_button_ ? kRelatedButtonHSpacing : 0) +
184 (ShouldShow(extra_view_) ? extra_view_->GetPreferredSize().width() : 0) +
185 (ShouldShow(extra_view_) && has_dialog_buttons() ?
186 kRelatedButtonHSpacing : 0),
189 int buttons_height = GetButtonsAndExtraViewRowHeight();
190 if (buttons_height != 0) {
191 size.Enlarge(0, buttons_height + kRelatedControlVerticalSpacing);
192 // Inset the buttons and extra view.
193 const gfx::Insets insets = GetButtonRowInsets();
194 size.Enlarge(insets.width(), insets.height());
197 // Increase the size as needed to fit the contents view.
198 // NOTE: The contents view is not inset on the top or side client view edges.
199 gfx::Size contents_size = contents_view()->GetPreferredSize();
200 size.Enlarge(0, contents_size.height());
201 size.set_width(std::max(size.width(), contents_size.width()));
203 // Increase the size as needed to fit the footnote view.
204 if (ShouldShow(footnote_view_)) {
205 gfx::Size footnote_size = footnote_view_->GetPreferredSize();
206 if (!footnote_size.IsEmpty())
207 size.set_width(std::max(size.width(), footnote_size.width()));
209 int footnote_height = footnote_view_->GetHeightForWidth(size.width());
210 size.Enlarge(0, footnote_height);
213 return size;
216 void DialogClientView::Layout() {
217 gfx::Rect bounds = GetContentsBounds();
219 // Layout the footnote view.
220 if (ShouldShow(footnote_view_)) {
221 const int height = footnote_view_->GetHeightForWidth(bounds.width());
222 footnote_view_->SetBounds(bounds.x(), bounds.bottom() - height,
223 bounds.width(), height);
224 if (height != 0)
225 bounds.Inset(0, 0, 0, height);
228 // Layout the row containing the buttons and the extra view.
229 if (has_dialog_buttons() || ShouldShow(extra_view_)) {
230 bounds.Inset(GetButtonRowInsets());
231 const int height = GetButtonsAndExtraViewRowHeight();
232 gfx::Rect row_bounds(bounds.x(), bounds.bottom() - height,
233 bounds.width(), height);
234 if (kIsOkButtonOnLeftSide) {
235 LayoutButton(cancel_button_, &row_bounds);
236 LayoutButton(ok_button_, &row_bounds);
237 } else {
238 LayoutButton(ok_button_, &row_bounds);
239 LayoutButton(cancel_button_, &row_bounds);
241 if (extra_view_) {
242 row_bounds.set_width(std::min(row_bounds.width(),
243 extra_view_->GetPreferredSize().width()));
244 extra_view_->SetBoundsRect(row_bounds);
247 if (height > 0)
248 bounds.Inset(0, 0, 0, height + kRelatedControlVerticalSpacing);
251 // Layout the contents view to the top and side edges of the contents bounds.
252 // NOTE: The local insets do not apply to the contents view sides or top.
253 const gfx::Rect contents_bounds = GetContentsBounds();
254 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(),
255 contents_bounds.width(), bounds.bottom() - contents_bounds.y());
258 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) {
259 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE);
260 Close();
261 return true;
264 void DialogClientView::ViewHierarchyChanged(
265 const ViewHierarchyChangedDetails& details) {
266 ClientView::ViewHierarchyChanged(details);
267 if (details.is_add && details.child == this) {
268 // The old dialog style needs an explicit background color, while the new
269 // dialog style simply inherits the bubble's frame view color.
270 const DialogDelegate* dialog = GetDialogDelegate();
271 if (dialog && !dialog->UseNewStyleForThisDialog())
272 set_background(views::Background::CreateSolidBackground(GetNativeTheme()->
273 GetSystemColor(ui::NativeTheme::kColorId_DialogBackground)));
275 focus_manager_ = GetFocusManager();
276 if (focus_manager_)
277 GetFocusManager()->AddFocusChangeListener(this);
279 UpdateDialogButtons();
280 CreateExtraView();
281 CreateFootnoteView();
282 } else if (!details.is_add && details.child == this) {
283 if (focus_manager_)
284 focus_manager_->RemoveFocusChangeListener(this);
285 focus_manager_ = NULL;
286 } else if (!details.is_add) {
287 if (details.child == default_button_)
288 default_button_ = NULL;
289 if (details.child == ok_button_)
290 ok_button_ = NULL;
291 if (details.child == cancel_button_)
292 cancel_button_ = NULL;
296 void DialogClientView::NativeViewHierarchyChanged() {
297 FocusManager* focus_manager = GetFocusManager();
298 if (focus_manager_ != focus_manager) {
299 if (focus_manager_)
300 focus_manager_->RemoveFocusChangeListener(this);
301 focus_manager_ = focus_manager;
302 if (focus_manager_)
303 focus_manager_->AddFocusChangeListener(this);
307 ////////////////////////////////////////////////////////////////////////////////
308 // DialogClientView, ButtonListener implementation:
310 void DialogClientView::ButtonPressed(Button* sender, const ui::Event& event) {
311 // Check for a valid delegate to avoid handling events after destruction.
312 if (!GetDialogDelegate())
313 return;
315 if (sender == ok_button_)
316 AcceptWindow();
317 else if (sender == cancel_button_)
318 CancelWindow();
319 else
320 NOTREACHED();
323 ////////////////////////////////////////////////////////////////////////////////
324 // DialogClientView, protected:
326 DialogClientView::DialogClientView(View* contents_view)
327 : ClientView(NULL, contents_view),
328 ok_button_(NULL),
329 cancel_button_(NULL),
330 default_button_(NULL),
331 focus_manager_(NULL),
332 extra_view_(NULL),
333 footnote_view_(NULL),
334 notified_delegate_(false) {}
336 DialogDelegate* DialogClientView::GetDialogDelegate() const {
337 return GetWidget()->widget_delegate()->AsDialogDelegate();
340 void DialogClientView::CreateExtraView() {
341 if (extra_view_)
342 return;
344 extra_view_ = GetDialogDelegate()->CreateExtraView();
345 if (extra_view_) {
346 extra_view_->SetGroup(kButtonGroup);
347 AddChildView(extra_view_);
351 void DialogClientView::CreateFootnoteView() {
352 if (footnote_view_)
353 return;
355 footnote_view_ = GetDialogDelegate()->CreateFootnoteView();
356 if (footnote_view_)
357 AddChildView(footnote_view_);
360 void DialogClientView::ChildPreferredSizeChanged(View* child) {
361 if (child == footnote_view_ || child == extra_view_)
362 Layout();
365 void DialogClientView::ChildVisibilityChanged(View* child) {
366 ChildPreferredSizeChanged(child);
369 ////////////////////////////////////////////////////////////////////////////////
370 // DialogClientView, private:
372 LabelButton* DialogClientView::CreateDialogButton(ui::DialogButton type) {
373 const base::string16 title = GetDialogDelegate()->GetDialogButtonLabel(type);
374 LabelButton* button = NULL;
375 if (GetDialogDelegate()->UseNewStyleForThisDialog() &&
376 GetDialogDelegate()->GetDefaultDialogButton() == type &&
377 GetDialogDelegate()->ShouldDefaultButtonBeBlue()) {
378 button = new BlueButton(this, title);
379 } else {
380 button = new LabelButton(this, title);
381 button->SetStyle(Button::STYLE_BUTTON);
383 button->SetFocusable(true);
385 const int kDialogMinButtonWidth = 75;
386 button->set_min_width(kDialogMinButtonWidth);
387 button->SetGroup(kButtonGroup);
388 return button;
391 void DialogClientView::UpdateButton(LabelButton* button,
392 ui::DialogButton type) {
393 DialogDelegate* dialog = GetDialogDelegate();
394 button->SetText(dialog->GetDialogButtonLabel(type));
395 button->SetEnabled(dialog->IsDialogButtonEnabled(type));
397 if (type == dialog->GetDefaultDialogButton()) {
398 default_button_ = button;
399 button->SetIsDefault(true);
403 int DialogClientView::GetButtonsAndExtraViewRowHeight() const {
404 int extra_view_height = ShouldShow(extra_view_) ?
405 extra_view_->GetPreferredSize().height() : 0;
406 int buttons_height = std::max(
407 ok_button_ ? ok_button_->GetPreferredSize().height() : 0,
408 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0);
409 return std::max(extra_view_height, buttons_height);
412 gfx::Insets DialogClientView::GetButtonRowInsets() const {
413 // NOTE: The insets only apply to the buttons, extra view, and footnote view.
414 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() :
415 gfx::Insets(0, kButtonHEdgeMarginNew,
416 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew);
419 void DialogClientView::Close() {
420 GetWidget()->Close();
421 GetDialogDelegate()->OnClosed();
424 } // namespace views