| 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 "ui/views/window/dialog_client_view.h" | 5 #include "ui/views/window/dialog_client_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "ui/events/keycodes/keyboard_codes.h" | 10 #include "ui/events/keycodes/keyboard_codes.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 DialogClientView::DialogClientView(Widget* owner, View* contents_view) | 55 DialogClientView::DialogClientView(Widget* owner, View* contents_view) |
| 56 : ClientView(owner, contents_view), | 56 : ClientView(owner, contents_view), |
| 57 button_row_insets_(0, | 57 button_row_insets_(0, |
| 58 kButtonHEdgeMarginNew, | 58 kButtonHEdgeMarginNew, |
| 59 kButtonVEdgeMarginNew, | 59 kButtonVEdgeMarginNew, |
| 60 kButtonHEdgeMarginNew), | 60 kButtonHEdgeMarginNew), |
| 61 ok_button_(NULL), | 61 ok_button_(NULL), |
| 62 cancel_button_(NULL), | 62 cancel_button_(NULL), |
| 63 extra_view_(NULL), | 63 extra_view_(NULL), |
| 64 delegate_allowed_close_(false) {} | 64 delegate_allowed_close_(false) { |
| 65 // Doing this now ensures this accelerator will have lower priority than |
| 66 // one set by the contents view. |
| 67 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 68 } |
| 65 | 69 |
| 66 DialogClientView::~DialogClientView() { | 70 DialogClientView::~DialogClientView() { |
| 67 } | 71 } |
| 68 | 72 |
| 69 void DialogClientView::AcceptWindow() { | 73 void DialogClientView::AcceptWindow() { |
| 70 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 74 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. |
| 71 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept()) { | 75 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept()) { |
| 72 delegate_allowed_close_ = true; | 76 delegate_allowed_close_ = true; |
| 73 GetWidget()->Close(); | 77 GetWidget()->Close(); |
| 74 } | 78 } |
| 75 } | 79 } |
| 76 | 80 |
| 77 void DialogClientView::CancelWindow() { | 81 void DialogClientView::CancelWindow() { |
| 78 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 82 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. |
| 79 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { | 83 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { |
| 80 delegate_allowed_close_ = true; | 84 delegate_allowed_close_ = true; |
| 81 GetWidget()->Close(); | 85 GetWidget()->Close(); |
| 82 } | 86 } |
| 83 } | 87 } |
| 84 | 88 |
| 85 void DialogClientView::UpdateDialogButtons() { | 89 void DialogClientView::UpdateDialogButtons() { |
| 86 const int buttons = GetDialogDelegate()->GetDialogButtons(); | 90 const int buttons = GetDialogDelegate()->GetDialogButtons(); |
| 87 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | |
| 88 | 91 |
| 89 if (buttons & ui::DIALOG_BUTTON_OK) { | 92 if (buttons & ui::DIALOG_BUTTON_OK) { |
| 90 if (!ok_button_) { | 93 if (!ok_button_) { |
| 91 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | 94 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); |
| 92 AddChildView(ok_button_); | 95 AddChildView(ok_button_); |
| 93 } | 96 } |
| 94 | 97 |
| 95 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | 98 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); |
| 96 } else if (ok_button_) { | 99 } else if (ok_button_) { |
| 97 delete ok_button_; | 100 delete ok_button_; |
| 98 ok_button_ = NULL; | 101 ok_button_ = NULL; |
| 99 } | 102 } |
| 100 | 103 |
| 101 if (buttons & ui::DIALOG_BUTTON_CANCEL) { | 104 if (buttons & ui::DIALOG_BUTTON_CANCEL) { |
| 102 if (!cancel_button_) { | 105 if (!cancel_button_) { |
| 103 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | 106 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); |
| 104 cancel_button_->AddAccelerator(escape); | |
| 105 AddChildView(cancel_button_); | 107 AddChildView(cancel_button_); |
| 106 } | 108 } |
| 107 | 109 |
| 108 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | 110 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); |
| 109 } else if (cancel_button_) { | 111 } else if (cancel_button_) { |
| 110 delete cancel_button_; | 112 delete cancel_button_; |
| 111 cancel_button_ = NULL; | 113 cancel_button_ = NULL; |
| 112 } | 114 } |
| 113 | |
| 114 // Use the escape key to close the window if there is no cancel button. | |
| 115 if (!cancel_button_) | |
| 116 AddAccelerator(escape); | |
| 117 else | |
| 118 ResetAccelerators(); | |
| 119 } | 115 } |
| 120 | 116 |
| 121 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
| 122 // DialogClientView, ClientView overrides: | 118 // DialogClientView, ClientView overrides: |
| 123 | 119 |
| 124 bool DialogClientView::CanClose() { | 120 bool DialogClientView::CanClose() { |
| 125 // If the dialog is closing but no Accept or Cancel action has been performed | 121 // If the dialog is closing but no Accept or Cancel action has been performed |
| 126 // before, it's a Close action. | 122 // before, it's a Close action. |
| 127 if (!delegate_allowed_close_) | 123 if (!delegate_allowed_close_) |
| 128 delegate_allowed_close_ = GetDialogDelegate()->Close(); | 124 delegate_allowed_close_ = GetDialogDelegate()->Close(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 | 203 |
| 208 // Layout the contents view to the top and side edges of the contents bounds. | 204 // Layout the contents view to the top and side edges of the contents bounds. |
| 209 // NOTE: The local insets do not apply to the contents view sides or top. | 205 // NOTE: The local insets do not apply to the contents view sides or top. |
| 210 const gfx::Rect contents_bounds = GetContentsBounds(); | 206 const gfx::Rect contents_bounds = GetContentsBounds(); |
| 211 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(), | 207 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(), |
| 212 contents_bounds.width(), bounds.bottom() - contents_bounds.y()); | 208 contents_bounds.width(), bounds.bottom() - contents_bounds.y()); |
| 213 } | 209 } |
| 214 | 210 |
| 215 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 211 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 216 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); | 212 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); |
| 213 |
| 214 // If there's a cancel button, it handles escape. |
| 215 if (cancel_button_) |
| 216 return cancel_button_->AcceleratorPressed(accelerator); |
| 217 |
| 217 GetWidget()->Close(); | 218 GetWidget()->Close(); |
| 218 return true; | 219 return true; |
| 219 } | 220 } |
| 220 | 221 |
| 221 void DialogClientView::ViewHierarchyChanged( | 222 void DialogClientView::ViewHierarchyChanged( |
| 222 const ViewHierarchyChangedDetails& details) { | 223 const ViewHierarchyChangedDetails& details) { |
| 223 ClientView::ViewHierarchyChanged(details); | 224 ClientView::ViewHierarchyChanged(details); |
| 224 if (details.is_add && details.child == this) { | 225 if (details.is_add && details.child == this) { |
| 225 UpdateDialogButtons(); | 226 UpdateDialogButtons(); |
| 226 CreateExtraView(); | 227 CreateExtraView(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 child_views.end()); | 361 child_views.end()); |
| 361 | 362 |
| 362 // Setup focus. | 363 // Setup focus. |
| 363 for (size_t i = 0; i < child_views.size(); i++) { | 364 for (size_t i = 0; i < child_views.size(); i++) { |
| 364 child_views[i]->SetNextFocusableView( | 365 child_views[i]->SetNextFocusableView( |
| 365 i + 1 != child_views.size() ? child_views[i + 1] : nullptr); | 366 i + 1 != child_views.size() ? child_views[i + 1] : nullptr); |
| 366 } | 367 } |
| 367 } | 368 } |
| 368 | 369 |
| 369 } // namespace views | 370 } // namespace views |
| OLD | NEW |