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