Chromium Code Reviews| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 } | 73 } |
| 74 | 74 |
| 75 void DialogClientView::CancelWindow() { | 75 void DialogClientView::CancelWindow() { |
| 76 // Only notify the delegate once. See |notified_delegate_|'s comment. | 76 // Only notify the delegate once. See |notified_delegate_|'s comment. |
| 77 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) { | 77 if (!notified_delegate_ && GetDialogDelegate()->Cancel()) { |
| 78 notified_delegate_ = true; | 78 notified_delegate_ = true; |
| 79 Close(); | 79 Close(); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 void DialogClientView::UpdateDialogButtons() { | 83 void DialogClientView::UpdateDialogButtons() { |
|
sky
2016/02/16 16:47:28
Because UpdateDialogButtons can be called at any t
karandeepb
2016/02/17 09:16:14
Yeah. Thanks for the catch. I am now explicitly se
| |
| 84 const int buttons = GetDialogDelegate()->GetDialogButtons(); | 84 // Add buttons in left to right order, so that the focus order matches. |
| 85 ui::Accelerator escape(ui::VKEY_ESCAPE, ui::EF_NONE); | 85 if (kIsOkButtonOnLeftSide) { |
| 86 | 86 UpdateOKButton(); |
| 87 if (buttons & ui::DIALOG_BUTTON_OK) { | 87 UpdateCancelButton(); |
| 88 if (!ok_button_) { | 88 } else { |
| 89 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | 89 UpdateCancelButton(); |
| 90 if (!(buttons & ui::DIALOG_BUTTON_CANCEL)) | 90 UpdateOKButton(); |
| 91 ok_button_->AddAccelerator(escape); | |
| 92 AddChildView(ok_button_); | |
| 93 } | |
| 94 | |
| 95 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | |
| 96 } else if (ok_button_) { | |
| 97 delete ok_button_; | |
| 98 ok_button_ = NULL; | |
| 99 } | |
| 100 | |
| 101 if (buttons & ui::DIALOG_BUTTON_CANCEL) { | |
| 102 if (!cancel_button_) { | |
| 103 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | |
| 104 cancel_button_->AddAccelerator(escape); | |
| 105 AddChildView(cancel_button_); | |
| 106 } | |
| 107 | |
| 108 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | |
| 109 } else if (cancel_button_) { | |
| 110 delete cancel_button_; | |
| 111 cancel_button_ = NULL; | |
| 112 } | 91 } |
| 113 | 92 |
| 114 // Use the escape key to close the window if there are no dialog buttons. | 93 // Use the escape key to close the window if there are no dialog buttons. |
| 115 if (!has_dialog_buttons()) | 94 if (!has_dialog_buttons()) |
| 116 AddAccelerator(escape); | 95 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 117 else | 96 else |
| 118 ResetAccelerators(); | 97 ResetAccelerators(); |
| 119 } | 98 } |
| 120 | 99 |
| 121 /////////////////////////////////////////////////////////////////////////////// | 100 /////////////////////////////////////////////////////////////////////////////// |
| 122 // DialogClientView, ClientView overrides: | 101 // DialogClientView, ClientView overrides: |
| 123 | 102 |
| 124 bool DialogClientView::CanClose() { | 103 bool DialogClientView::CanClose() { |
| 125 if (notified_delegate_) | 104 if (notified_delegate_) |
| 126 return true; | 105 return true; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 219 bool DialogClientView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 241 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); | 220 DCHECK_EQ(accelerator.key_code(), ui::VKEY_ESCAPE); |
| 242 Close(); | 221 Close(); |
| 243 return true; | 222 return true; |
| 244 } | 223 } |
| 245 | 224 |
| 246 void DialogClientView::ViewHierarchyChanged( | 225 void DialogClientView::ViewHierarchyChanged( |
| 247 const ViewHierarchyChangedDetails& details) { | 226 const ViewHierarchyChangedDetails& details) { |
| 248 ClientView::ViewHierarchyChanged(details); | 227 ClientView::ViewHierarchyChanged(details); |
| 249 if (details.is_add && details.child == this) { | 228 if (details.is_add && details.child == this) { |
| 229 CreateExtraView(); | |
| 250 UpdateDialogButtons(); | 230 UpdateDialogButtons(); |
| 251 CreateExtraView(); | |
| 252 CreateFootnoteView(); | 231 CreateFootnoteView(); |
| 253 } else if (!details.is_add && details.child != this) { | 232 } else if (!details.is_add && details.child != this) { |
| 254 if (details.child == ok_button_) | 233 if (details.child == ok_button_) |
| 255 ok_button_ = NULL; | 234 ok_button_ = NULL; |
| 256 if (details.child == cancel_button_) | 235 if (details.child == cancel_button_) |
| 257 cancel_button_ = NULL; | 236 cancel_button_ = NULL; |
| 258 } | 237 } |
| 259 } | 238 } |
| 260 | 239 |
| 261 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 240 void DialogClientView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 } | 331 } |
| 353 | 332 |
| 354 void DialogClientView::UpdateButton(LabelButton* button, | 333 void DialogClientView::UpdateButton(LabelButton* button, |
| 355 ui::DialogButton type) { | 334 ui::DialogButton type) { |
| 356 DialogDelegate* dialog = GetDialogDelegate(); | 335 DialogDelegate* dialog = GetDialogDelegate(); |
| 357 button->SetText(dialog->GetDialogButtonLabel(type)); | 336 button->SetText(dialog->GetDialogButtonLabel(type)); |
| 358 button->SetEnabled(dialog->IsDialogButtonEnabled(type)); | 337 button->SetEnabled(dialog->IsDialogButtonEnabled(type)); |
| 359 button->SetIsDefault(type == dialog->GetDefaultDialogButton()); | 338 button->SetIsDefault(type == dialog->GetDefaultDialogButton()); |
| 360 } | 339 } |
| 361 | 340 |
| 341 void DialogClientView::UpdateOKButton() { | |
| 342 const int buttons = GetDialogDelegate()->GetDialogButtons(); | |
| 343 if (buttons & ui::DIALOG_BUTTON_OK) { | |
| 344 if (!ok_button_) { | |
| 345 ok_button_ = CreateDialogButton(ui::DIALOG_BUTTON_OK); | |
| 346 if (!(buttons & ui::DIALOG_BUTTON_CANCEL)) | |
| 347 ok_button_->AddAccelerator( | |
| 348 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | |
| 349 AddChildView(ok_button_); | |
| 350 } | |
| 351 | |
| 352 UpdateButton(ok_button_, ui::DIALOG_BUTTON_OK); | |
| 353 } else if (ok_button_) { | |
| 354 delete ok_button_; | |
| 355 ok_button_ = nullptr; | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 void DialogClientView::UpdateCancelButton() { | |
| 360 if (GetDialogDelegate()->GetDialogButtons() & ui::DIALOG_BUTTON_CANCEL) { | |
| 361 if (!cancel_button_) { | |
| 362 cancel_button_ = CreateDialogButton(ui::DIALOG_BUTTON_CANCEL); | |
| 363 cancel_button_->AddAccelerator( | |
| 364 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | |
| 365 AddChildView(cancel_button_); | |
| 366 } | |
| 367 | |
| 368 UpdateButton(cancel_button_, ui::DIALOG_BUTTON_CANCEL); | |
| 369 } else if (cancel_button_) { | |
| 370 delete cancel_button_; | |
| 371 cancel_button_ = nullptr; | |
| 372 } | |
| 373 } | |
| 374 | |
| 362 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { | 375 int DialogClientView::GetButtonsAndExtraViewRowHeight() const { |
| 363 int extra_view_height = ShouldShow(extra_view_) ? | 376 int extra_view_height = ShouldShow(extra_view_) ? |
| 364 extra_view_->GetPreferredSize().height() : 0; | 377 extra_view_->GetPreferredSize().height() : 0; |
| 365 int buttons_height = std::max( | 378 int buttons_height = std::max( |
| 366 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, | 379 ok_button_ ? ok_button_->GetPreferredSize().height() : 0, |
| 367 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); | 380 cancel_button_ ? cancel_button_->GetPreferredSize().height() : 0); |
| 368 return std::max(extra_view_height, buttons_height); | 381 return std::max(extra_view_height, buttons_height); |
| 369 } | 382 } |
| 370 | 383 |
| 371 gfx::Insets DialogClientView::GetButtonRowInsets() const { | 384 gfx::Insets DialogClientView::GetButtonRowInsets() const { |
| 372 // NOTE: The insets only apply to the buttons, extra view, and footnote view. | 385 // NOTE: The insets only apply to the buttons, extra view, and footnote view. |
| 373 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : | 386 return GetButtonsAndExtraViewRowHeight() == 0 ? gfx::Insets() : |
| 374 gfx::Insets(0, kButtonHEdgeMarginNew, | 387 gfx::Insets(0, kButtonHEdgeMarginNew, |
| 375 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew); | 388 kButtonVEdgeMarginNew, kButtonHEdgeMarginNew); |
| 376 } | 389 } |
| 377 | 390 |
| 378 void DialogClientView::Close() { | 391 void DialogClientView::Close() { |
| 379 GetWidget()->Close(); | 392 GetWidget()->Close(); |
| 380 GetDialogDelegate()->OnClosed(); | 393 GetDialogDelegate()->OnClosed(); |
| 381 } | 394 } |
| 382 | 395 |
| 383 } // namespace views | 396 } // namespace views |
| OLD | NEW |