| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 /////////////////////////////////////////////////////////////////////////////// | 52 /////////////////////////////////////////////////////////////////////////////// |
| 53 // DialogClientView, public: | 53 // DialogClientView, public: |
| 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 ok_button_(NULL), | 57 ok_button_(NULL), |
| 58 cancel_button_(NULL), | 58 cancel_button_(NULL), |
| 59 extra_view_(NULL), | 59 extra_view_(NULL), |
| 60 footnote_view_(NULL), | 60 footnote_view_(NULL), |
| 61 delegate_allowed_close_(false) {} | 61 notified_delegate_(false) { |
| 62 } |
| 62 | 63 |
| 63 DialogClientView::~DialogClientView() { | 64 DialogClientView::~DialogClientView() { |
| 64 } | 65 } |
| 65 | 66 |
| 66 void DialogClientView::AcceptWindow() { | 67 void DialogClientView::AcceptWindow() { |
| 67 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 68 // Only notify the delegate once. See |notified_delegate_|'s comment. |
| 68 if (!delegate_allowed_close_ && GetDialogDelegate()->Accept(false)) { | 69 if (!notified_delegate_ && GetDialogDelegate()->Accept(false)) { |
| 69 delegate_allowed_close_ = true; | 70 notified_delegate_ = true; |
| 70 GetWidget()->Close(); | 71 Close(); |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 | 74 |
| 74 void DialogClientView::CancelWindow() { | 75 void DialogClientView::CancelWindow() { |
| 75 // Only notify the delegate once. See |delegate_allowed_close_|'s comment. | 76 // Only notify the delegate once. See |notified_delegate_|'s comment. |
| 76 if (!delegate_allowed_close_ && GetDialogDelegate()->Cancel()) { | 77 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) { |
| 77 delegate_allowed_close_ = true; | 78 notified_delegate_ = true; |
| 78 GetWidget()->Close(); | 79 Close(); |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 void DialogClientView::UpdateDialogButtons() { | 83 void DialogClientView::UpdateDialogButtons() { |
| 83 const int buttons = GetDialogDelegate()->GetDialogButtons(); | 84 const int buttons = GetDialogDelegate()->GetDialogButtons(); |
| 84 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 85 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); |
| 85 | 86 |
| 86 if (buttons & ui::DIALOG_BUTTON_OK) { | 87 if (buttons & ui::DIALOG_BUTTON_OK) { |
| 87 if (!ok_button_) { | 88 if (!ok_button_) { |
| 88 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | 89 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 114 if (!has_dialog_buttons()) | 115 if (!has_dialog_buttons()) |
| 115 AddAccelerator(escape); | 116 AddAccelerator(escape); |
| 116 else | 117 else |
| 117 ResetAccelerators(); | 118 ResetAccelerators(); |
| 118 } | 119 } |
| 119 | 120 |
| 120 /////////////////////////////////////////////////////////////////////////////// | 121 /////////////////////////////////////////////////////////////////////////////// |
| 121 // DialogClientView, ClientView overrides: | 122 // DialogClientView, ClientView overrides: |
| 122 | 123 |
| 123 bool DialogClientView::CanClose() { | 124 bool DialogClientView::CanClose() { |
| 124 // If the dialog is closing but no Accept or Cancel action has been performed | 125 if (notified_delegate_) |
| 125 // before, it's a Close action. | 126 return true; |
| 126 if (!delegate_allowed_close_) | 127 |
| 127 delegate_allowed_close_ = GetDialogDelegate()->Close(); | 128 // The dialog is closing but no Accept or Cancel action has been performed |
| 128 return delegate_allowed_close_; | 129 // before: it's a Close action. |
| 130 if (GetDialogDelegate()->Close()) { |
| 131 notified_delegate_ = true; |
| 132 GetDialogDelegate()->OnClosed(); |
| 133 return true; |
| 134 } |
| 135 return false; |
| 129 } | 136 } |
| 130 | 137 |
| 131 DialogClientView* DialogClientView::AsDialogClientView() { | 138 DialogClientView* DialogClientView::AsDialogClientView() { |
| 132 return this; | 139 return this; |
| 133 } | 140 } |
| 134 | 141 |
| 135 const DialogClientView* DialogClientView::AsDialogClientView() const { | 142 const DialogClientView* DialogClientView::AsDialogClientView() const { |
| 136 return this; | 143 return this; |
| 137 } | 144 } |
| 138 | 145 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 232 |
| 226 // Layout the contents view to the top and side edges of the contents bounds. | 233 // Layout the contents view to the top and side edges of the contents bounds. |
| 227 // NOTE: The local insets do not apply to the contents view sides or top. | 234 // NOTE: The local insets do not apply to the contents view sides or top. |
| 228 const gfx::Rect contents_bounds = GetContentsBounds(); | 235 const gfx::Rect contents_bounds = GetContentsBounds(); |
| 229 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(), | 236 contents_view()->SetBounds(contents_bounds.x(), contents_bounds.y(), |
| 230 contents_bounds.width(), bounds.bottom() - contents_bounds.y()); | 237 contents_bounds.width(), bounds.bottom() - contents_bounds.y()); |
| 231 } | 238 } |
| 232 | 239 |
| 233 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 240 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 234 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); | 241 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); |
| 235 GetWidget()->Close(); | 242 Close(); |
| 236 return true; | 243 return true; |
| 237 } | 244 } |
| 238 | 245 |
| 239 void DialogClientView::ViewHierarchyChanged( | 246 void DialogClientView::ViewHierarchyChanged( |
| 240 const ViewHierarchyChangedDetails& details) { | 247 const ViewHierarchyChangedDetails& details) { |
| 241 ClientView::ViewHierarchyChanged(details); | 248 ClientView::ViewHierarchyChanged(details); |
| 242 if (details.is_add && details.child == this) { | 249 if (details.is_add && details.child == this) { |
| 243 UpdateDialogButtons(); | 250 UpdateDialogButtons(); |
| 244 CreateExtraView(); | 251 CreateExtraView(); |
| 245 CreateFootnoteView(); | 252 CreateFootnoteView(); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 | 293 |
| 287 //////////////////////////////////////////////////////////////////////////////// | 294 //////////////////////////////////////////////////////////////////////////////// |
| 288 // DialogClientView, protected: | 295 // DialogClientView, protected: |
| 289 | 296 |
| 290 DialogClientView::DialogClientView(View* contents_view) | 297 DialogClientView::DialogClientView(View* contents_view) |
| 291 : ClientView(NULL, contents_view), | 298 : ClientView(NULL, contents_view), |
| 292 ok_button_(NULL), | 299 ok_button_(NULL), |
| 293 cancel_button_(NULL), | 300 cancel_button_(NULL), |
| 294 extra_view_(NULL), | 301 extra_view_(NULL), |
| 295 footnote_view_(NULL), | 302 footnote_view_(NULL), |
| 296 delegate_allowed_close_(false) {} | 303 notified_delegate_(false) {} |
| 297 | 304 |
| 298 DialogDelegate* DialogClientView::GetDialogDelegate() const { | 305 DialogDelegate* DialogClientView::GetDialogDelegate() const { |
| 299 return GetWidget()->widget_delegate()->AsDialogDelegate(); | 306 return GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 300 } | 307 } |
| 301 | 308 |
| 302 void DialogClientView::CreateExtraView() { | 309 void DialogClientView::CreateExtraView() { |
| 303 if (extra_view_) | 310 if (extra_view_) |
| 304 return; | 311 return; |
| 305 | 312 |
| 306 extra_view_ = GetDialogDelegate()->CreateExtraView(); | 313 extra_view_ = GetDialogDelegate()->CreateExtraView(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 return std::max(extra_view_height, buttons_height); | 374 return std::max(extra_view_height, buttons_height); |
| 368 } | 375 } |
| 369 | 376 |
| 370 gfx::Insets DialogClientView::GetButtonRowInsets() const { | 377 gfx::Insets DialogClientView::GetButtonRowInsets() const { |
| 371 // NOTE: The insets only apply to the buttons, extra view, and footnote view. | 378 // NOTE: The insets only apply to the buttons, extra view, and footnote view. |
| 372 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : | 379 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : |
| 373 gfx::Insets(0, kButtonHEdgeMarginNew, | 380 gfx::Insets(0, kButtonHEdgeMarginNew, |
| 374 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew); | 381 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew); |
| 375 } | 382 } |
| 376 | 383 |
| 377 | 384 void DialogClientView::Close() { |
| 385 GetWidget()->Close(); |
| 386 GetDialogDelegate()->OnClosed(); |
| 387 } |
| 378 | 388 |
| 379 void DialogClientView::SetupFocusChain() { | 389 void DialogClientView::SetupFocusChain() { |
| 380 // Create a vector of child views in the order of intended focus. | 390 // Create a vector of child views in the order of intended focus. |
| 381 std::vector<View*> child_views; | 391 std::vector<View*> child_views; |
| 382 child_views.push_back(contents_view()); | 392 child_views.push_back(contents_view()); |
| 383 child_views.push_back(extra_view_); | 393 child_views.push_back(extra_view_); |
| 384 if (kIsOkButtonOnLeftSide) { | 394 if (kIsOkButtonOnLeftSide) { |
| 385 child_views.push_back(ok_button_); | 395 child_views.push_back(ok_button_); |
| 386 child_views.push_back(cancel_button_); | 396 child_views.push_back(cancel_button_); |
| 387 } else { | 397 } else { |
| 388 child_views.push_back(cancel_button_); | 398 child_views.push_back(cancel_button_); |
| 389 child_views.push_back(ok_button_); | 399 child_views.push_back(ok_button_); |
| 390 } | 400 } |
| 391 child_views.push_back(footnote_view_); | 401 child_views.push_back(footnote_view_); |
| 392 | 402 |
| 393 // Remove all null views from the vector. | 403 // Remove all null views from the vector. |
| 394 child_views.erase( | 404 child_views.erase( |
| 395 std::remove(child_views.begin(), child_views.end(), nullptr), | 405 std::remove(child_views.begin(), child_views.end(), nullptr), |
| 396 child_views.end()); | 406 child_views.end()); |
| 397 | 407 |
| 398 // Setup focus. | 408 // Setup focus. |
| 399 for (size_t i = 0; i < child_views.size(); i++) { | 409 for (size_t i = 0; i < child_views.size(); i++) { |
| 400 child_views[i]->SetNextFocusableView( | 410 child_views[i]->SetNextFocusableView( |
| 401 i + 1 != child_views.size() ? child_views[i + 1] : nullptr); | 411 i + 1 != child_views.size() ? child_views[i + 1] : nullptr); |
| 402 } | 412 } |
| 403 } | 413 } |
| 404 | 414 |
| 405 } // namespace views | 415 } // namespace views |
| OLD | NEW |