| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/window/dialog_client_view.h" | 5 #include "views/window/dialog_client_view.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <uxtheme.h> | 9 #include <uxtheme.h> |
| 10 #include <vsstyle.h> | 10 #include <vsstyle.h> |
| 11 #else | 11 #else |
| 12 #include <gtk/gtk.h> | 12 #include <gtk/gtk.h> |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 | 16 |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "grit/app_strings.h" | 18 #include "grit/app_strings.h" |
| 19 #include "ui/base/keycodes/keyboard_codes.h" | 19 #include "ui/base/keycodes/keyboard_codes.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/canvas_skia.h" | 22 #include "ui/gfx/canvas_skia.h" |
| 23 #include "ui/gfx/font.h" | 23 #include "ui/gfx/font.h" |
| 24 #include "views/controls/button/native_button.h" | 24 #include "views/controls/button/native_button.h" |
| 25 #include "views/layout/layout_constants.h" | 25 #include "views/layout/layout_constants.h" |
| 26 #include "views/widget/root_view.h" | 26 #include "views/widget/root_view.h" |
| 27 #include "views/widget/widget.h" |
| 27 #include "views/window/dialog_delegate.h" | 28 #include "views/window/dialog_delegate.h" |
| 28 #include "views/window/window.h" | |
| 29 | 29 |
| 30 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 31 #include "ui/gfx/native_theme.h" | 31 #include "ui/gfx/native_theme.h" |
| 32 #else | 32 #else |
| 33 #include "ui/gfx/skia_utils_gtk.h" | 33 #include "ui/gfx/skia_utils_gtk.h" |
| 34 #include "views/widget/widget.h" | 34 #include "views/widget/widget.h" |
| 35 #include "views/window/hit_test.h" | 35 #include "views/window/hit_test.h" |
| 36 #endif | 36 #endif |
| 37 | 37 |
| 38 using ui::MessageBoxFlags; | 38 using ui::MessageBoxFlags; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 61 #endif | 61 #endif |
| 62 | 62 |
| 63 // DialogButton ---------------------------------------------------------------- | 63 // DialogButton ---------------------------------------------------------------- |
| 64 | 64 |
| 65 // DialogButtons is used for the ok/cancel buttons of the window. DialogButton | 65 // DialogButtons is used for the ok/cancel buttons of the window. DialogButton |
| 66 // forwards AcceleratorPressed to the delegate. | 66 // forwards AcceleratorPressed to the delegate. |
| 67 | 67 |
| 68 class DialogButton : public NativeButton { | 68 class DialogButton : public NativeButton { |
| 69 public: | 69 public: |
| 70 DialogButton(ButtonListener* listener, | 70 DialogButton(ButtonListener* listener, |
| 71 Window* owner, | 71 Widget* owner, |
| 72 MessageBoxFlags::DialogButton type, | 72 MessageBoxFlags::DialogButton type, |
| 73 const std::wstring& title, | 73 const std::wstring& title, |
| 74 bool is_default) | 74 bool is_default) |
| 75 : NativeButton(listener, title), | 75 : NativeButton(listener, title), |
| 76 owner_(owner), | 76 owner_(owner), |
| 77 type_(type) { | 77 type_(type) { |
| 78 SetIsDefault(is_default); | 78 SetIsDefault(is_default); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Overridden to forward to the delegate. | 81 // Overridden to forward to the delegate. |
| 82 virtual bool AcceleratorPressed(const Accelerator& accelerator) { | 82 virtual bool AcceleratorPressed(const Accelerator& accelerator) { |
| 83 if (!owner_->window_delegate()->AsDialogDelegate()-> | 83 if (!owner_->widget_delegate()->AsDialogDelegate()-> |
| 84 AreAcceleratorsEnabled(type_)) { | 84 AreAcceleratorsEnabled(type_)) { |
| 85 return false; | 85 return false; |
| 86 } | 86 } |
| 87 return NativeButton::AcceleratorPressed(accelerator); | 87 return NativeButton::AcceleratorPressed(accelerator); |
| 88 } | 88 } |
| 89 | 89 |
| 90 private: | 90 private: |
| 91 Window* owner_; | 91 Widget* owner_; |
| 92 const MessageBoxFlags::DialogButton type_; | 92 const MessageBoxFlags::DialogButton type_; |
| 93 | 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(DialogButton); | 94 DISALLOW_COPY_AND_ASSIGN(DialogButton); |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 } // namespace | 97 } // namespace |
| 98 | 98 |
| 99 // static | 99 // static |
| 100 gfx::Font* DialogClientView::dialog_button_font_ = NULL; | 100 gfx::Font* DialogClientView::dialog_button_font_ = NULL; |
| 101 static const int kDialogMinButtonWidth = 75; | 101 static const int kDialogMinButtonWidth = 75; |
| 102 static const int kDialogButtonLabelSpacing = 16; | 102 static const int kDialogButtonLabelSpacing = 16; |
| 103 static const int kDialogButtonContentSpacing = 5; | 103 static const int kDialogButtonContentSpacing = 5; |
| 104 | 104 |
| 105 // The group used by the buttons. This name is chosen voluntarily big not to | 105 // The group used by the buttons. This name is chosen voluntarily big not to |
| 106 // conflict with other groups that could be in the dialog content. | 106 // conflict with other groups that could be in the dialog content. |
| 107 static const int kButtonGroup = 6666; | 107 static const int kButtonGroup = 6666; |
| 108 | 108 |
| 109 /////////////////////////////////////////////////////////////////////////////// | 109 /////////////////////////////////////////////////////////////////////////////// |
| 110 // DialogClientView, public: | 110 // DialogClientView, public: |
| 111 | 111 |
| 112 DialogClientView::DialogClientView(Window* owner, View* contents_view) | 112 DialogClientView::DialogClientView(Widget* owner, View* contents_view) |
| 113 : ClientView(owner, contents_view), | 113 : ClientView(owner, contents_view), |
| 114 ok_button_(NULL), | 114 ok_button_(NULL), |
| 115 cancel_button_(NULL), | 115 cancel_button_(NULL), |
| 116 default_button_(NULL), | 116 default_button_(NULL), |
| 117 extra_view_(NULL), | 117 extra_view_(NULL), |
| 118 size_extra_view_height_to_buttons_(false), | 118 size_extra_view_height_to_buttons_(false), |
| 119 notified_delegate_(false), | 119 notified_delegate_(false), |
| 120 listening_to_focus_(false), | 120 listening_to_focus_(false), |
| 121 saved_focus_manager_(NULL), | 121 saved_focus_manager_(NULL), |
| 122 bottom_view_(NULL) { | 122 bottom_view_(NULL) { |
| 123 InitClass(); | 123 InitClass(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 DialogClientView::~DialogClientView() { | 126 DialogClientView::~DialogClientView() { |
| 127 } | 127 } |
| 128 | 128 |
| 129 void DialogClientView::ShowDialogButtons() { | 129 void DialogClientView::ShowDialogButtons() { |
| 130 DialogDelegate* dd = GetDialogDelegate(); | 130 DialogDelegate* dd = GetDialogDelegate(); |
| 131 int buttons = dd->GetDialogButtons(); | 131 int buttons = dd->GetDialogButtons(); |
| 132 if (buttons & MessageBoxFlags::DIALOGBUTTON_OK && !ok_button_) { | 132 if (buttons & MessageBoxFlags::DIALOGBUTTON_OK && !ok_button_) { |
| 133 std::wstring label = | 133 std::wstring label = |
| 134 dd->GetDialogButtonLabel(MessageBoxFlags::DIALOGBUTTON_OK); | 134 dd->GetDialogButtonLabel(MessageBoxFlags::DIALOGBUTTON_OK); |
| 135 if (label.empty()) | 135 if (label.empty()) |
| 136 label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_APP_OK)); | 136 label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_APP_OK)); |
| 137 bool is_default_button = | 137 bool is_default_button = |
| 138 (dd->GetDefaultDialogButton() & MessageBoxFlags::DIALOGBUTTON_OK) != 0; | 138 (dd->GetDefaultDialogButton() & MessageBoxFlags::DIALOGBUTTON_OK) != 0; |
| 139 ok_button_ = new DialogButton(this, window(), | 139 ok_button_ = new DialogButton(this, GetWidget(), |
| 140 MessageBoxFlags::DIALOGBUTTON_OK, label, | 140 MessageBoxFlags::DIALOGBUTTON_OK, label, |
| 141 is_default_button); | 141 is_default_button); |
| 142 ok_button_->SetGroup(kButtonGroup); | 142 ok_button_->SetGroup(kButtonGroup); |
| 143 if (is_default_button) | 143 if (is_default_button) |
| 144 default_button_ = ok_button_; | 144 default_button_ = ok_button_; |
| 145 if (!(buttons & MessageBoxFlags::DIALOGBUTTON_CANCEL)) | 145 if (!(buttons & MessageBoxFlags::DIALOGBUTTON_CANCEL)) |
| 146 ok_button_->AddAccelerator(Accelerator(ui::VKEY_ESCAPE, | 146 ok_button_->AddAccelerator(Accelerator(ui::VKEY_ESCAPE, |
| 147 false, false, false)); | 147 false, false, false)); |
| 148 AddChildView(ok_button_); | 148 AddChildView(ok_button_); |
| 149 } | 149 } |
| 150 if (buttons & MessageBoxFlags::DIALOGBUTTON_CANCEL && !cancel_button_) { | 150 if (buttons & MessageBoxFlags::DIALOGBUTTON_CANCEL && !cancel_button_) { |
| 151 std::wstring label = | 151 std::wstring label = |
| 152 dd->GetDialogButtonLabel(MessageBoxFlags::DIALOGBUTTON_CANCEL); | 152 dd->GetDialogButtonLabel(MessageBoxFlags::DIALOGBUTTON_CANCEL); |
| 153 if (label.empty()) { | 153 if (label.empty()) { |
| 154 if (buttons & MessageBoxFlags::DIALOGBUTTON_OK) { | 154 if (buttons & MessageBoxFlags::DIALOGBUTTON_OK) { |
| 155 label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_APP_CANCEL)); | 155 label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_APP_CANCEL)); |
| 156 } else { | 156 } else { |
| 157 label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_APP_CLOSE)); | 157 label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_APP_CLOSE)); |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 bool is_default_button = | 160 bool is_default_button = |
| 161 (dd->GetDefaultDialogButton() & MessageBoxFlags::DIALOGBUTTON_CANCEL) | 161 (dd->GetDefaultDialogButton() & MessageBoxFlags::DIALOGBUTTON_CANCEL) |
| 162 != 0; | 162 != 0; |
| 163 cancel_button_ = new DialogButton(this, window(), | 163 cancel_button_ = new DialogButton(this, GetWidget(), |
| 164 MessageBoxFlags::DIALOGBUTTON_CANCEL, | 164 MessageBoxFlags::DIALOGBUTTON_CANCEL, |
| 165 label, is_default_button); | 165 label, is_default_button); |
| 166 cancel_button_->SetGroup(kButtonGroup); | 166 cancel_button_->SetGroup(kButtonGroup); |
| 167 cancel_button_->AddAccelerator(Accelerator(ui::VKEY_ESCAPE, | 167 cancel_button_->AddAccelerator(Accelerator(ui::VKEY_ESCAPE, |
| 168 false, false, false)); | 168 false, false, false)); |
| 169 if (is_default_button) | 169 if (is_default_button) |
| 170 default_button_ = ok_button_; | 170 default_button_ = ok_button_; |
| 171 AddChildView(cancel_button_); | 171 AddChildView(cancel_button_); |
| 172 } | 172 } |
| 173 if (!buttons) { | 173 if (!buttons) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 int buttons = dd->GetDialogButtons(); | 277 int buttons = dd->GetDialogButtons(); |
| 278 bool close = true; | 278 bool close = true; |
| 279 if (buttons & MessageBoxFlags::DIALOGBUTTON_CANCEL) | 279 if (buttons & MessageBoxFlags::DIALOGBUTTON_CANCEL) |
| 280 close = dd->Cancel(); | 280 close = dd->Cancel(); |
| 281 else if (buttons & MessageBoxFlags::DIALOGBUTTON_OK) | 281 else if (buttons & MessageBoxFlags::DIALOGBUTTON_OK) |
| 282 close = dd->Accept(true); | 282 close = dd->Accept(true); |
| 283 notified_delegate_ = close; | 283 notified_delegate_ = close; |
| 284 return close; | 284 return close; |
| 285 } | 285 } |
| 286 | 286 |
| 287 void DialogClientView::WindowClosing() { | 287 void DialogClientView::WidgetClosing() { |
| 288 if (listening_to_focus_) { | 288 if (listening_to_focus_) { |
| 289 DCHECK(saved_focus_manager_); | 289 DCHECK(saved_focus_manager_); |
| 290 if (saved_focus_manager_) | 290 if (saved_focus_manager_) |
| 291 saved_focus_manager_->RemoveFocusChangeListener(this); | 291 saved_focus_manager_->RemoveFocusChangeListener(this); |
| 292 } | 292 } |
| 293 } | 293 } |
| 294 | 294 |
| 295 int DialogClientView::NonClientHitTest(const gfx::Point& point) { | 295 int DialogClientView::NonClientHitTest(const gfx::Point& point) { |
| 296 if (size_box_bounds_.Contains(point.x() - x(), point.y() - y())) | 296 if (size_box_bounds_.Contains(point.x() - x(), point.y() - y())) |
| 297 return HTBOTTOMRIGHT; | 297 return HTBOTTOMRIGHT; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 314 GtkStyle* window_style = gtk_widget_get_style(widget); | 314 GtkStyle* window_style = gtk_widget_get_style(widget); |
| 315 canvas->FillRectInt(gfx::GdkColorToSkColor( | 315 canvas->FillRectInt(gfx::GdkColorToSkColor( |
| 316 window_style->bg[GTK_STATE_NORMAL]), | 316 window_style->bg[GTK_STATE_NORMAL]), |
| 317 0, 0, width(), height()); | 317 0, 0, width(), height()); |
| 318 } | 318 } |
| 319 #endif | 319 #endif |
| 320 } | 320 } |
| 321 | 321 |
| 322 void DialogClientView::PaintChildren(gfx::Canvas* canvas) { | 322 void DialogClientView::PaintChildren(gfx::Canvas* canvas) { |
| 323 View::PaintChildren(canvas); | 323 View::PaintChildren(canvas); |
| 324 if (!window()->IsMaximized() && !window()->IsMinimized()) | 324 if (!GetWidget()->IsMaximized() && !GetWidget()->IsMinimized()) |
| 325 PaintSizeBox(canvas); | 325 PaintSizeBox(canvas); |
| 326 } | 326 } |
| 327 | 327 |
| 328 void DialogClientView::Layout() { | 328 void DialogClientView::Layout() { |
| 329 if (has_dialog_buttons()) | 329 if (has_dialog_buttons()) |
| 330 LayoutDialogButtons(); | 330 LayoutDialogButtons(); |
| 331 if (bottom_view_) { | 331 if (bottom_view_) { |
| 332 gfx::Rect bounds = GetContentsBounds(); | 332 gfx::Rect bounds = GetContentsBounds(); |
| 333 gfx::Size pref = bottom_view_->GetPreferredSize(); | 333 gfx::Size pref = bottom_view_->GetPreferredSize(); |
| 334 bottom_view_->SetBounds(bounds.x(), | 334 bottom_view_->SetBounds(bounds.x(), |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 CancelWindow(); | 419 CancelWindow(); |
| 420 } else { | 420 } else { |
| 421 NOTREACHED(); | 421 NOTREACHED(); |
| 422 } | 422 } |
| 423 } | 423 } |
| 424 | 424 |
| 425 //////////////////////////////////////////////////////////////////////////////// | 425 //////////////////////////////////////////////////////////////////////////////// |
| 426 // DialogClientView, private: | 426 // DialogClientView, private: |
| 427 | 427 |
| 428 void DialogClientView::PaintSizeBox(gfx::Canvas* canvas) { | 428 void DialogClientView::PaintSizeBox(gfx::Canvas* canvas) { |
| 429 if (window()->window_delegate()->CanResize() || | 429 if (GetWidget()->widget_delegate()->CanResize() || |
| 430 window()->window_delegate()->CanMaximize()) { | 430 GetWidget()->widget_delegate()->CanMaximize()) { |
| 431 #if defined(OS_WIN) | 431 #if defined(OS_WIN) |
| 432 gfx::NativeTheme::ExtraParams extra; | 432 gfx::NativeTheme::ExtraParams extra; |
| 433 gfx::Size gripper_size = gfx::NativeTheme::instance()->GetPartSize( | 433 gfx::Size gripper_size = gfx::NativeTheme::instance()->GetPartSize( |
| 434 gfx::NativeTheme::kWindowResizeGripper, gfx::NativeTheme::kNormal, | 434 gfx::NativeTheme::kWindowResizeGripper, gfx::NativeTheme::kNormal, |
| 435 extra); | 435 extra); |
| 436 | 436 |
| 437 // TODO(beng): (http://b/1085509) In "classic" rendering mode, there isn't | 437 // TODO(beng): (http://b/1085509) In "classic" rendering mode, there isn't |
| 438 // a theme-supplied gripper. We should probably improvise | 438 // a theme-supplied gripper. We should probably improvise |
| 439 // something, which would also require changing |gripper_size| | 439 // something, which would also require changing |gripper_size| |
| 440 // to have different default values, too... | 440 // to have different default values, too... |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 if (extra_view && !extra_view_) { | 532 if (extra_view && !extra_view_) { |
| 533 extra_view_ = extra_view; | 533 extra_view_ = extra_view; |
| 534 extra_view_->SetGroup(kButtonGroup); | 534 extra_view_->SetGroup(kButtonGroup); |
| 535 AddChildView(extra_view_); | 535 AddChildView(extra_view_); |
| 536 size_extra_view_height_to_buttons_ = | 536 size_extra_view_height_to_buttons_ = |
| 537 GetDialogDelegate()->GetSizeExtraViewHeightToButtons(); | 537 GetDialogDelegate()->GetSizeExtraViewHeightToButtons(); |
| 538 } | 538 } |
| 539 } | 539 } |
| 540 | 540 |
| 541 DialogDelegate* DialogClientView::GetDialogDelegate() const { | 541 DialogDelegate* DialogClientView::GetDialogDelegate() const { |
| 542 return window()->window_delegate()->AsDialogDelegate(); | 542 return GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 543 } | 543 } |
| 544 | 544 |
| 545 void DialogClientView::Close() { | 545 void DialogClientView::Close() { |
| 546 window()->Close(); | 546 GetWidget()->Close(); |
| 547 GetDialogDelegate()->OnClose(); | 547 GetDialogDelegate()->OnClose(); |
| 548 } | 548 } |
| 549 | 549 |
| 550 void DialogClientView::UpdateFocusListener() { | 550 void DialogClientView::UpdateFocusListener() { |
| 551 FocusManager* focus_manager = GetFocusManager(); | 551 FocusManager* focus_manager = GetFocusManager(); |
| 552 // Listen for focus change events so we can update the default button. | 552 // Listen for focus change events so we can update the default button. |
| 553 // focus_manager can be NULL when the dialog is created on un-shown view. | 553 // focus_manager can be NULL when the dialog is created on un-shown view. |
| 554 // We start listening for focus changes when the page is visible. | 554 // We start listening for focus changes when the page is visible. |
| 555 // Focus manager could also change if window host changes a parent. | 555 // Focus manager could also change if window host changes a parent. |
| 556 if (listening_to_focus_) { | 556 if (listening_to_focus_) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 573 void DialogClientView::InitClass() { | 573 void DialogClientView::InitClass() { |
| 574 static bool initialized = false; | 574 static bool initialized = false; |
| 575 if (!initialized) { | 575 if (!initialized) { |
| 576 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 576 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 577 dialog_button_font_ = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont)); | 577 dialog_button_font_ = new gfx::Font(rb.GetFont(ResourceBundle::BaseFont)); |
| 578 initialized = true; | 578 initialized = true; |
| 579 } | 579 } |
| 580 } | 580 } |
| 581 | 581 |
| 582 } // namespace views | 582 } // namespace views |
| OLD | NEW |