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/widget/widget.h" | 5 #include "views/widget/widget.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "ui/base/l10n/l10n_font_util.h" |
| 11 #include "ui/base/resource/resource_bundle.h" |
10 #include "ui/gfx/compositor/compositor.h" | 12 #include "ui/gfx/compositor/compositor.h" |
11 #include "views/focus/view_storage.h" | 13 #include "views/focus/view_storage.h" |
12 #include "views/ime/input_method.h" | 14 #include "views/ime/input_method.h" |
13 #include "views/views_delegate.h" | 15 #include "views/views_delegate.h" |
14 #include "views/widget/default_theme_provider.h" | 16 #include "views/widget/default_theme_provider.h" |
15 #include "views/widget/root_view.h" | 17 #include "views/widget/root_view.h" |
16 #include "views/widget/native_widget.h" | 18 #include "views/widget/native_widget.h" |
17 #include "views/widget/widget_delegate.h" | 19 #include "views/widget/widget_delegate.h" |
18 #include "views/window/custom_frame_view.h" | 20 #include "views/window/custom_frame_view.h" |
19 | 21 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 : is_mouse_button_pressed_(false), | 93 : is_mouse_button_pressed_(false), |
92 last_mouse_event_was_move_(false), | 94 last_mouse_event_was_move_(false), |
93 native_widget_(NULL), | 95 native_widget_(NULL), |
94 widget_delegate_(NULL), | 96 widget_delegate_(NULL), |
95 non_client_view_(NULL), | 97 non_client_view_(NULL), |
96 dragged_view_(NULL), | 98 dragged_view_(NULL), |
97 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), | 99 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET), |
98 is_secondary_widget_(true), | 100 is_secondary_widget_(true), |
99 frame_type_(FRAME_TYPE_DEFAULT), | 101 frame_type_(FRAME_TYPE_DEFAULT), |
100 disable_inactive_rendering_(false), | 102 disable_inactive_rendering_(false), |
101 widget_closed_(false) { | 103 widget_closed_(false), |
| 104 saved_maximized_state_(false), |
| 105 minimum_size_(100, 100) { |
102 } | 106 } |
103 | 107 |
104 Widget::~Widget() { | 108 Widget::~Widget() { |
105 DestroyRootView(); | 109 DestroyRootView(); |
106 | 110 |
107 if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) | 111 if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) |
108 delete native_widget_; | 112 delete native_widget_; |
109 } | 113 } |
110 | 114 |
111 // static | 115 // static |
112 void Widget::SetPureViews(bool pure) { | 116 void Widget::SetPureViews(bool pure) { |
113 use_pure_views = pure; | 117 use_pure_views = pure; |
114 } | 118 } |
115 | 119 |
116 // static | 120 // static |
117 bool Widget::IsPureViews() { | 121 bool Widget::IsPureViews() { |
118 return use_pure_views; | 122 return use_pure_views; |
119 } | 123 } |
120 | 124 |
121 // static | 125 // static |
122 Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) { | 126 Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) { |
123 NativeWidget* native_widget = | 127 NativeWidget* native_widget = |
124 NativeWidget::GetNativeWidgetForNativeView(native_view); | 128 NativeWidget::GetNativeWidgetForNativeView(native_view); |
125 return native_widget ? native_widget->GetWidget() : NULL; | 129 return native_widget ? native_widget->GetWidget() : NULL; |
126 } | 130 } |
127 | 131 |
| 132 // static |
| 133 int Widget::GetLocalizedContentsWidth(int col_resource_id) { |
| 134 return ui::GetLocalizedContentsWidthForFont(col_resource_id, |
| 135 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont)); |
| 136 } |
| 137 |
| 138 // static |
| 139 int Widget::GetLocalizedContentsHeight(int row_resource_id) { |
| 140 return ui::GetLocalizedContentsHeightForFont(row_resource_id, |
| 141 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont)); |
| 142 } |
| 143 |
| 144 // static |
| 145 gfx::Size Widget::GetLocalizedContentsSize(int col_resource_id, |
| 146 int row_resource_id) { |
| 147 return gfx::Size(GetLocalizedContentsWidth(col_resource_id), |
| 148 GetLocalizedContentsHeight(row_resource_id)); |
| 149 } |
| 150 |
128 void Widget::Init(const InitParams& params) { | 151 void Widget::Init(const InitParams& params) { |
129 widget_delegate_ = | 152 widget_delegate_ = |
130 params.delegate ? params.delegate : new DefaultWidgetDelegate; | 153 params.delegate ? params.delegate : new DefaultWidgetDelegate; |
131 ownership_ = params.ownership; | 154 ownership_ = params.ownership; |
132 native_widget_ = | 155 native_widget_ = |
133 params.native_widget ? params.native_widget | 156 params.native_widget ? params.native_widget |
134 : NativeWidget::CreateNativeWidget(this); | 157 : NativeWidget::CreateNativeWidget(this); |
135 GetRootView(); | 158 GetRootView(); |
136 default_theme_provider_.reset(new DefaultThemeProvider); | 159 default_theme_provider_.reset(new DefaultThemeProvider); |
137 if (params.type == InitParams::TYPE_MENU) | 160 if (params.type == InitParams::TYPE_MENU) |
138 is_mouse_button_pressed_ = native_widget_->IsMouseButtonDown(); | 161 is_mouse_button_pressed_ = native_widget_->IsMouseButtonDown(); |
139 native_widget_->InitNativeWidget(params); | 162 native_widget_->InitNativeWidget(params); |
140 if (params.type == InitParams::TYPE_WINDOW) { | 163 if (params.type == InitParams::TYPE_WINDOW) { |
141 non_client_view_ = new NonClientView; | 164 non_client_view_ = new NonClientView; |
142 non_client_view_->SetFrameView(CreateNonClientFrameView()); | 165 non_client_view_->SetFrameView(CreateNonClientFrameView()); |
143 // Create the ClientView, add it to the NonClientView and add the | 166 // Create the ClientView, add it to the NonClientView and add the |
144 // NonClientView to the RootView. This will cause everything to be parented. | 167 // NonClientView to the RootView. This will cause everything to be parented. |
145 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this)); | 168 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this)); |
146 SetContentsView(non_client_view_); | 169 SetContentsView(non_client_view_); |
| 170 SetInitialBounds(params.bounds); |
| 171 UpdateWindowTitle(); |
147 } | 172 } |
148 } | 173 } |
149 | 174 |
150 // Unconverted methods (see header) -------------------------------------------- | 175 // Unconverted methods (see header) -------------------------------------------- |
151 | 176 |
152 gfx::NativeView Widget::GetNativeView() const { | 177 gfx::NativeView Widget::GetNativeView() const { |
153 return native_widget_->GetNativeView(); | 178 return native_widget_->GetNativeView(); |
154 } | 179 } |
155 | 180 |
156 gfx::NativeWindow Widget::GetNativeWindow() const { | 181 gfx::NativeWindow Widget::GetNativeWindow() const { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 void Widget::CloseNow() { | 302 void Widget::CloseNow() { |
278 native_widget_->CloseNow(); | 303 native_widget_->CloseNow(); |
279 } | 304 } |
280 | 305 |
281 void Widget::EnableClose(bool enable) { | 306 void Widget::EnableClose(bool enable) { |
282 non_client_view_->EnableClose(enable); | 307 non_client_view_->EnableClose(enable); |
283 native_widget_->EnableClose(enable); | 308 native_widget_->EnableClose(enable); |
284 } | 309 } |
285 | 310 |
286 void Widget::Show() { | 311 void Widget::Show() { |
287 native_widget_->Show(); | 312 if (non_client_view_) { |
| 313 native_widget_->ShowNativeWidget( |
| 314 saved_maximized_state_ ? NativeWidget::SHOW_MAXIMIZED |
| 315 : NativeWidget::SHOW_RESTORED); |
| 316 // |saved_maximized_state_| only applies the first time the window is shown. |
| 317 // If we don't reset the value the window will be shown maximized every time |
| 318 // it is subsequently shown after being hidden. |
| 319 saved_maximized_state_ = false; |
| 320 } else { |
| 321 native_widget_->Show(); |
| 322 } |
288 } | 323 } |
289 | 324 |
290 void Widget::Hide() { | 325 void Widget::Hide() { |
291 native_widget_->Hide(); | 326 native_widget_->Hide(); |
292 } | 327 } |
293 | 328 |
294 void Widget::ShowInactive() { | 329 void Widget::ShowInactive() { |
295 native_widget_->ShowNativeWidget(NativeWidget::SHOW_INACTIVE); | 330 native_widget_->ShowNativeWidget(NativeWidget::SHOW_INACTIVE); |
296 } | 331 } |
297 | 332 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 | 461 |
427 void Widget::SetCursor(gfx::NativeCursor cursor) { | 462 void Widget::SetCursor(gfx::NativeCursor cursor) { |
428 native_widget_->SetCursor(cursor); | 463 native_widget_->SetCursor(cursor); |
429 } | 464 } |
430 | 465 |
431 void Widget::ResetLastMouseMoveFlag() { | 466 void Widget::ResetLastMouseMoveFlag() { |
432 last_mouse_event_was_move_ = false; | 467 last_mouse_event_was_move_ = false; |
433 } | 468 } |
434 | 469 |
435 void Widget::UpdateWindowTitle() { | 470 void Widget::UpdateWindowTitle() { |
436 if (non_client_view_) { | 471 if (!non_client_view_) |
437 // If the non-client view is rendering its own title, it'll need to relayout | 472 return; |
438 // now. | 473 |
439 non_client_view_->Layout(); | 474 // If the non-client view is rendering its own title, it'll need to relayout |
440 } | 475 // now. |
| 476 non_client_view_->Layout(); |
441 | 477 |
442 // Update the native frame's text. We do this regardless of whether or not | 478 // Update the native frame's text. We do this regardless of whether or not |
443 // the native frame is being used, since this also updates the taskbar, etc. | 479 // the native frame is being used, since this also updates the taskbar, etc. |
444 string16 window_title; | 480 string16 window_title; |
445 if (native_widget_->IsScreenReaderActive()) { | 481 if (native_widget_->IsScreenReaderActive()) { |
446 window_title = WideToUTF16(widget_delegate_->GetAccessibleWindowTitle()); | 482 window_title = WideToUTF16(widget_delegate_->GetAccessibleWindowTitle()); |
447 } else { | 483 } else { |
448 window_title = WideToUTF16(widget_delegate_->GetWindowTitle()); | 484 window_title = WideToUTF16(widget_delegate_->GetWindowTitle()); |
449 } | 485 } |
450 base::i18n::AdjustStringForLocaleDirection(&window_title); | 486 base::i18n::AdjustStringForLocaleDirection(&window_title); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 if (ViewsDelegate::views_delegate) | 553 if (ViewsDelegate::views_delegate) |
518 ViewsDelegate::views_delegate->NotifyAccessibilityEvent(view, event_type); | 554 ViewsDelegate::views_delegate->NotifyAccessibilityEvent(view, event_type); |
519 | 555 |
520 if (send_native_event) | 556 if (send_native_event) |
521 native_widget_->SendNativeAccessibilityEvent(view, event_type); | 557 native_widget_->SendNativeAccessibilityEvent(view, event_type); |
522 } | 558 } |
523 | 559 |
524 //////////////////////////////////////////////////////////////////////////////// | 560 //////////////////////////////////////////////////////////////////////////////// |
525 // Widget, NativeWidgetDelegate implementation: | 561 // Widget, NativeWidgetDelegate implementation: |
526 | 562 |
| 563 bool Widget::IsModal() const { |
| 564 return widget_delegate_->IsModal(); |
| 565 } |
| 566 |
| 567 bool Widget::IsDialogBox() const { |
| 568 return !!widget_delegate_->AsDialogDelegate(); |
| 569 } |
| 570 |
527 bool Widget::CanActivate() const { | 571 bool Widget::CanActivate() const { |
528 return widget_delegate_->CanActivate(); | 572 return widget_delegate_->CanActivate(); |
529 } | 573 } |
530 | 574 |
531 bool Widget::IsInactiveRenderingDisabled() const { | 575 bool Widget::IsInactiveRenderingDisabled() const { |
532 return disable_inactive_rendering_; | 576 return disable_inactive_rendering_; |
533 } | 577 } |
534 | 578 |
535 void Widget::EnableInactiveRendering() { | 579 void Widget::EnableInactiveRendering() { |
536 disable_inactive_rendering_ = false; | 580 disable_inactive_rendering_ = false; |
(...skipping 26 matching lines...) Expand all Loading... |
563 // Only the top level Widget in a native widget hierarchy has a focus | 607 // Only the top level Widget in a native widget hierarchy has a focus |
564 // manager. | 608 // manager. |
565 focus_manager_.reset(new FocusManager(this)); | 609 focus_manager_.reset(new FocusManager(this)); |
566 } | 610 } |
567 EnsureCompositor(); | 611 EnsureCompositor(); |
568 | 612 |
569 native_widget_->SetAccessibleRole( | 613 native_widget_->SetAccessibleRole( |
570 widget_delegate_->GetAccessibleWindowRole()); | 614 widget_delegate_->GetAccessibleWindowRole()); |
571 native_widget_->SetAccessibleState( | 615 native_widget_->SetAccessibleState( |
572 widget_delegate_->GetAccessibleWindowState()); | 616 widget_delegate_->GetAccessibleWindowState()); |
| 617 |
| 618 if (widget_delegate_->IsModal()) |
| 619 native_widget_->BecomeModal(); |
573 } | 620 } |
574 | 621 |
575 void Widget::OnNativeWidgetDestroying() { | 622 void Widget::OnNativeWidgetDestroying() { |
576 if (non_client_view_) | 623 if (non_client_view_) |
577 non_client_view_->WindowClosing(); | 624 non_client_view_->WindowClosing(); |
578 widget_delegate_->WindowClosing(); | 625 widget_delegate_->WindowClosing(); |
579 } | 626 } |
580 | 627 |
581 void Widget::OnNativeWidgetDestroyed() { | 628 void Widget::OnNativeWidgetDestroyed() { |
582 widget_delegate_->DeleteDelegate(); | 629 widget_delegate_->DeleteDelegate(); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 // bail. | 820 // bail. |
774 if (!widget_delegate_) | 821 if (!widget_delegate_) |
775 return; | 822 return; |
776 | 823 |
777 bool maximized; | 824 bool maximized; |
778 gfx::Rect bounds; | 825 gfx::Rect bounds; |
779 native_widget_->GetWindowBoundsAndMaximizedState(&bounds, &maximized); | 826 native_widget_->GetWindowBoundsAndMaximizedState(&bounds, &maximized); |
780 widget_delegate_->SaveWindowPlacement(bounds, maximized); | 827 widget_delegate_->SaveWindowPlacement(bounds, maximized); |
781 } | 828 } |
782 | 829 |
| 830 void Widget::SetInitialBounds(const gfx::Rect& bounds) { |
| 831 if (!non_client_view_) |
| 832 return; |
| 833 |
| 834 // First we obtain the window's saved show-style and store it. We need to do |
| 835 // this here, rather than in Show() because by the time Show() is called, |
| 836 // the window's size will have been reset (below) and the saved maximized |
| 837 // state will have been lost. Sadly there's no way to tell on Windows when |
| 838 // a window is restored from maximized state, so we can't more accurately |
| 839 // track maximized state independently of sizing information. |
| 840 widget_delegate_->GetSavedMaximizedState( |
| 841 &saved_maximized_state_); |
| 842 |
| 843 // Restore the window's placement from the controller. |
| 844 gfx::Rect saved_bounds = bounds; |
| 845 if (widget_delegate_->GetSavedWindowBounds(&saved_bounds)) { |
| 846 if (!widget_delegate_->ShouldRestoreWindowSize()) { |
| 847 saved_bounds.set_size(non_client_view_->GetPreferredSize()); |
| 848 } else { |
| 849 // Make sure the bounds are at least the minimum size. |
| 850 if (saved_bounds.width() < minimum_size_.width()) { |
| 851 saved_bounds.SetRect(saved_bounds.x(), saved_bounds.y(), |
| 852 saved_bounds.right() + minimum_size_.width() - |
| 853 saved_bounds.width(), |
| 854 saved_bounds.bottom()); |
| 855 } |
| 856 |
| 857 if (saved_bounds.height() < minimum_size_.height()) { |
| 858 saved_bounds.SetRect(saved_bounds.x(), saved_bounds.y(), |
| 859 saved_bounds.right(), |
| 860 saved_bounds.bottom() + minimum_size_.height() - |
| 861 saved_bounds.height()); |
| 862 } |
| 863 } |
| 864 |
| 865 // Widget's SetBounds method does not further modify the bounds that are |
| 866 // passed to it. |
| 867 SetBounds(saved_bounds); |
| 868 } else { |
| 869 if (bounds.IsEmpty()) { |
| 870 // No initial bounds supplied, so size the window to its content and |
| 871 // center over its parent. |
| 872 native_widget_->CenterWindow(non_client_view_->GetPreferredSize()); |
| 873 } else { |
| 874 // Use the supplied initial bounds. |
| 875 SetBoundsConstrained(bounds, NULL); |
| 876 } |
| 877 } |
| 878 } |
783 | 879 |
784 | 880 |
785 } // namespace views | 881 } // namespace views |
OLD | NEW |