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
3 // found in the LICENSE file.
5 #include "ui/views/widget/widget.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/base/cursor/cursor.h"
12 #include "ui/base/default_theme_provider.h"
13 #include "ui/base/hit_test.h"
14 #include "ui/base/l10n/l10n_font_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/compositor/compositor.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/events/event.h"
19 #include "ui/events/event_utils.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/screen.h"
22 #include "ui/views/controls/menu/menu_controller.h"
23 #include "ui/views/focus/focus_manager.h"
24 #include "ui/views/focus/focus_manager_factory.h"
25 #include "ui/views/focus/view_storage.h"
26 #include "ui/views/focus/widget_focus_manager.h"
27 #include "ui/views/ime/input_method.h"
28 #include "ui/views/views_delegate.h"
29 #include "ui/views/widget/native_widget_private.h"
30 #include "ui/views/widget/root_view.h"
31 #include "ui/views/widget/tooltip_manager.h"
32 #include "ui/views/widget/widget_delegate.h"
33 #include "ui/views/widget/widget_deletion_observer.h"
34 #include "ui/views/widget/widget_observer.h"
35 #include "ui/views/widget/widget_removals_observer.h"
36 #include "ui/views/window/custom_frame_view.h"
37 #include "ui/views/window/dialog_delegate.h"
43 // If |view| has a layer the layer is added to |layers|. Else this recurses
44 // through the children. This is used to build a list of the layers created by
45 // views that are direct children of the Widgets layer.
46 void BuildRootLayers(View
* view
, std::vector
<ui::Layer
*>* layers
) {
48 layers
->push_back(view
->layer());
50 for (int i
= 0; i
< view
->child_count(); ++i
)
51 BuildRootLayers(view
->child_at(i
), layers
);
55 // Create a native widget implementation.
56 // First, use the supplied one if non-NULL.
57 // Finally, make a default one.
58 NativeWidget
* CreateNativeWidget(NativeWidget
* native_widget
,
59 internal::NativeWidgetDelegate
* delegate
) {
62 internal::NativeWidgetPrivate::CreateNativeWidget(delegate
);
69 // A default implementation of WidgetDelegate, used by Widget when no
70 // WidgetDelegate is supplied.
71 class DefaultWidgetDelegate
: public WidgetDelegate
{
73 explicit DefaultWidgetDelegate(Widget
* widget
) : widget_(widget
) {
75 ~DefaultWidgetDelegate() override
{}
77 // Overridden from WidgetDelegate:
78 void DeleteDelegate() override
{ delete this; }
79 Widget
* GetWidget() override
{ return widget_
; }
80 const Widget
* GetWidget() const override
{ return widget_
; }
81 bool ShouldAdvanceFocusToTopLevelWidget() const override
{
82 // In most situations where a Widget is used without a delegate the Widget
83 // is used as a container, so that we want focus to advance to the top-level
84 // widget. A good example of this is the find bar.
91 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate
);
94 ////////////////////////////////////////////////////////////////////////////////
95 // Widget, InitParams:
97 Widget::InitParams::InitParams()
101 opacity(INFER_OPACITY
),
103 activatable(ACTIVATABLE_DEFAULT
),
105 visible_on_all_workspaces(false),
106 ownership(NATIVE_WIDGET_OWNS_WIDGET
),
107 mirror_origin_in_rtl(false),
108 shadow_type(SHADOW_TYPE_DEFAULT
),
109 remove_standard_frame(false),
110 use_system_default_icon(false),
111 show_state(ui::SHOW_STATE_DEFAULT
),
114 desktop_window_tree_host(NULL
),
115 layer_type(ui::LAYER_TEXTURED
),
117 force_show_in_taskbar(false) {
120 Widget::InitParams::InitParams(Type type
)
124 opacity(INFER_OPACITY
),
126 activatable(ACTIVATABLE_DEFAULT
),
127 keep_on_top(type
== TYPE_MENU
|| type
== TYPE_DRAG
),
128 visible_on_all_workspaces(false),
129 ownership(NATIVE_WIDGET_OWNS_WIDGET
),
130 mirror_origin_in_rtl(false),
131 shadow_type(SHADOW_TYPE_DEFAULT
),
132 remove_standard_frame(false),
133 use_system_default_icon(false),
134 show_state(ui::SHOW_STATE_DEFAULT
),
137 desktop_window_tree_host(NULL
),
138 layer_type(ui::LAYER_TEXTURED
),
140 force_show_in_taskbar(false) {
143 Widget::InitParams::~InitParams() {
146 ////////////////////////////////////////////////////////////////////////////////
150 : native_widget_(NULL
),
151 widget_delegate_(NULL
),
152 non_client_view_(NULL
),
154 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET
),
155 is_secondary_widget_(true),
156 frame_type_(FRAME_TYPE_DEFAULT
),
157 disable_inactive_rendering_(false),
158 widget_closed_(false),
159 saved_show_state_(ui::SHOW_STATE_DEFAULT
),
160 focus_on_creation_(true),
161 is_top_level_(false),
162 native_widget_initialized_(false),
163 native_widget_destroyed_(false),
164 is_mouse_button_pressed_(false),
165 ignore_capture_loss_(false),
166 last_mouse_event_was_move_(false),
167 auto_release_capture_(true),
168 root_layers_dirty_(false),
169 movement_disabled_(false),
170 observer_manager_(this) {
175 if (ownership_
== InitParams::WIDGET_OWNS_NATIVE_WIDGET
) {
176 delete native_widget_
;
178 DCHECK(native_widget_destroyed_
)
179 << "Destroying a widget with a live native widget. "
180 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
185 Widget
* Widget::CreateWindow(WidgetDelegate
* delegate
) {
186 return CreateWindowWithBounds(delegate
, gfx::Rect());
190 Widget
* Widget::CreateWindowWithBounds(WidgetDelegate
* delegate
,
191 const gfx::Rect
& bounds
) {
192 Widget
* widget
= new Widget
;
193 Widget::InitParams params
;
194 params
.bounds
= bounds
;
195 params
.delegate
= delegate
;
196 widget
->Init(params
);
201 Widget
* Widget::CreateWindowWithParent(WidgetDelegate
* delegate
,
202 gfx::NativeView parent
) {
203 return CreateWindowWithParentAndBounds(delegate
, parent
, gfx::Rect());
207 Widget
* Widget::CreateWindowWithParentAndBounds(WidgetDelegate
* delegate
,
208 gfx::NativeView parent
,
209 const gfx::Rect
& bounds
) {
210 Widget
* widget
= new Widget
;
211 Widget::InitParams params
;
212 params
.delegate
= delegate
;
213 params
.parent
= parent
;
214 params
.bounds
= bounds
;
215 widget
->Init(params
);
220 Widget
* Widget::CreateWindowWithContext(WidgetDelegate
* delegate
,
221 gfx::NativeWindow context
) {
222 return CreateWindowWithContextAndBounds(delegate
, context
, gfx::Rect());
226 Widget
* Widget::CreateWindowWithContextAndBounds(WidgetDelegate
* delegate
,
227 gfx::NativeWindow context
,
228 const gfx::Rect
& bounds
) {
229 Widget
* widget
= new Widget
;
230 Widget::InitParams params
;
231 params
.delegate
= delegate
;
232 params
.context
= context
;
233 params
.bounds
= bounds
;
234 widget
->Init(params
);
239 Widget
* Widget::GetWidgetForNativeView(gfx::NativeView native_view
) {
240 internal::NativeWidgetPrivate
* native_widget
=
241 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view
);
242 return native_widget
? native_widget
->GetWidget() : NULL
;
246 Widget
* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window
) {
247 internal::NativeWidgetPrivate
* native_widget
=
248 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
250 return native_widget
? native_widget
->GetWidget() : NULL
;
254 Widget
* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view
) {
255 internal::NativeWidgetPrivate
* native_widget
=
256 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view
);
257 return native_widget
? native_widget
->GetWidget() : NULL
;
262 void Widget::GetAllChildWidgets(gfx::NativeView native_view
,
264 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view
, children
);
268 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view
,
270 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view
, owned
);
274 void Widget::ReparentNativeView(gfx::NativeView native_view
,
275 gfx::NativeView new_parent
) {
276 internal::NativeWidgetPrivate::ReparentNativeView(native_view
, new_parent
);
280 int Widget::GetLocalizedContentsWidth(int col_resource_id
) {
281 return ui::GetLocalizedContentsWidthForFont(col_resource_id
,
282 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
));
286 int Widget::GetLocalizedContentsHeight(int row_resource_id
) {
287 return ui::GetLocalizedContentsHeightForFont(row_resource_id
,
288 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
));
292 gfx::Size
Widget::GetLocalizedContentsSize(int col_resource_id
,
293 int row_resource_id
) {
294 return gfx::Size(GetLocalizedContentsWidth(col_resource_id
),
295 GetLocalizedContentsHeight(row_resource_id
));
299 bool Widget::RequiresNonClientView(InitParams::Type type
) {
300 return type
== InitParams::TYPE_WINDOW
||
301 type
== InitParams::TYPE_PANEL
||
302 type
== InitParams::TYPE_BUBBLE
;
305 void Widget::Init(const InitParams
& in_params
) {
306 TRACE_EVENT0("views", "Widget::Init");
307 InitParams params
= in_params
;
309 params
.child
|= (params
.type
== InitParams::TYPE_CONTROL
);
310 is_top_level_
= !params
.child
;
312 if (params
.opacity
== views::Widget::InitParams::INFER_OPACITY
&&
313 params
.type
!= views::Widget::InitParams::TYPE_WINDOW
&&
314 params
.type
!= views::Widget::InitParams::TYPE_PANEL
)
315 params
.opacity
= views::Widget::InitParams::OPAQUE_WINDOW
;
317 if (ViewsDelegate::views_delegate
)
318 ViewsDelegate::views_delegate
->OnBeforeWidgetInit(¶ms
, this);
320 if (params
.opacity
== views::Widget::InitParams::INFER_OPACITY
)
321 params
.opacity
= views::Widget::InitParams::OPAQUE_WINDOW
;
323 bool can_activate
= false;
324 if (params
.activatable
!= InitParams::ACTIVATABLE_DEFAULT
) {
325 can_activate
= (params
.activatable
== InitParams::ACTIVATABLE_YES
);
326 } else if (params
.type
!= InitParams::TYPE_CONTROL
&&
327 params
.type
!= InitParams::TYPE_POPUP
&&
328 params
.type
!= InitParams::TYPE_MENU
&&
329 params
.type
!= InitParams::TYPE_TOOLTIP
&&
330 params
.type
!= InitParams::TYPE_DRAG
) {
332 params
.activatable
= InitParams::ACTIVATABLE_YES
;
334 can_activate
= false;
335 params
.activatable
= InitParams::ACTIVATABLE_NO
;
338 widget_delegate_
= params
.delegate
?
339 params
.delegate
: new DefaultWidgetDelegate(this);
340 widget_delegate_
->set_can_activate(can_activate
);
342 ownership_
= params
.ownership
;
343 native_widget_
= CreateNativeWidget(params
.native_widget
, this)->
344 AsNativeWidgetPrivate();
345 root_view_
.reset(CreateRootView());
346 default_theme_provider_
.reset(new ui::DefaultThemeProvider
);
347 if (params
.type
== InitParams::TYPE_MENU
) {
348 is_mouse_button_pressed_
=
349 internal::NativeWidgetPrivate::IsMouseButtonDown();
351 native_widget_
->InitNativeWidget(params
);
352 if (RequiresNonClientView(params
.type
)) {
353 non_client_view_
= new NonClientView
;
354 non_client_view_
->SetFrameView(CreateNonClientFrameView());
355 // Create the ClientView, add it to the NonClientView and add the
356 // NonClientView to the RootView. This will cause everything to be parented.
357 non_client_view_
->set_client_view(widget_delegate_
->CreateClientView(this));
358 non_client_view_
->SetOverlayView(widget_delegate_
->CreateOverlayView());
359 SetContentsView(non_client_view_
);
360 // Initialize the window's icon and title before setting the window's
361 // initial bounds; the frame view's preferred height may depend on the
362 // presence of an icon or a title.
365 non_client_view_
->ResetWindowControls();
366 SetInitialBounds(params
.bounds
);
367 if (params
.show_state
== ui::SHOW_STATE_MAXIMIZED
)
369 else if (params
.show_state
== ui::SHOW_STATE_MINIMIZED
)
371 } else if (params
.delegate
) {
372 SetContentsView(params
.delegate
->GetContentsView());
373 SetInitialBoundsForFramelessWindow(params
.bounds
);
375 // This must come after SetContentsView() or it might not be able to find
376 // the correct NativeTheme (on Linux). See http://crbug.com/384492
377 observer_manager_
.Add(GetNativeTheme());
378 native_widget_initialized_
= true;
381 // Unconverted methods (see header) --------------------------------------------
383 gfx::NativeView
Widget::GetNativeView() const {
384 return native_widget_
->GetNativeView();
387 gfx::NativeWindow
Widget::GetNativeWindow() const {
388 return native_widget_
->GetNativeWindow();
391 void Widget::AddObserver(WidgetObserver
* observer
) {
392 // Make sure that there is no nullptr in observer list. crbug.com/471649.
394 observers_
.AddObserver(observer
);
397 void Widget::RemoveObserver(WidgetObserver
* observer
) {
398 observers_
.RemoveObserver(observer
);
401 bool Widget::HasObserver(const WidgetObserver
* observer
) const {
402 return observers_
.HasObserver(observer
);
405 void Widget::AddRemovalsObserver(WidgetRemovalsObserver
* observer
) {
406 removals_observers_
.AddObserver(observer
);
409 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver
* observer
) {
410 removals_observers_
.RemoveObserver(observer
);
413 bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver
* observer
) const {
414 return removals_observers_
.HasObserver(observer
);
417 bool Widget::GetAccelerator(int cmd_id
, ui::Accelerator
* accelerator
) const {
421 void Widget::ViewHierarchyChanged(
422 const View::ViewHierarchyChangedDetails
& details
) {
423 if (!details
.is_add
) {
424 if (details
.child
== dragged_view_
)
425 dragged_view_
= NULL
;
426 FocusManager
* focus_manager
= GetFocusManager();
428 focus_manager
->ViewRemoved(details
.child
);
429 ViewStorage::GetInstance()->ViewRemoved(details
.child
);
430 native_widget_
->ViewRemoved(details
.child
);
434 void Widget::NotifyNativeViewHierarchyWillChange() {
435 FocusManager
* focus_manager
= GetFocusManager();
436 // We are being removed from a window hierarchy. Treat this as
437 // the root_view_ being removed.
439 focus_manager
->ViewRemoved(root_view_
.get());
442 void Widget::NotifyNativeViewHierarchyChanged() {
443 root_view_
->NotifyNativeViewHierarchyChanged();
446 void Widget::NotifyWillRemoveView(View
* view
) {
447 FOR_EACH_OBSERVER(WidgetRemovalsObserver
,
449 OnWillRemoveView(this, view
));
452 // Converted methods (see header) ----------------------------------------------
454 Widget
* Widget::GetTopLevelWidget() {
455 return const_cast<Widget
*>(
456 static_cast<const Widget
*>(this)->GetTopLevelWidget());
459 const Widget
* Widget::GetTopLevelWidget() const {
460 // GetTopLevelNativeWidget doesn't work during destruction because
461 // property is gone after gobject gets deleted. Short circuit here
462 // for toplevel so that InputMethod can remove itself from
464 return is_top_level() ? this : native_widget_
->GetTopLevelWidget();
467 void Widget::SetContentsView(View
* view
) {
468 // Do not SetContentsView() again if it is already set to the same view.
469 if (view
== GetContentsView())
471 root_view_
->SetContentsView(view
);
472 if (non_client_view_
!= view
) {
473 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
474 // was true when the widget was initialized. Creating widgets with non
475 // client views and then setting the contents view can cause subtle
476 // problems on Windows, where the native widget thinks there is still a
477 // |non_client_view_|. If you get this error, either use a different type
478 // when initializing the widget, or don't call SetContentsView().
479 DCHECK(!non_client_view_
);
480 non_client_view_
= NULL
;
484 View
* Widget::GetContentsView() {
485 return root_view_
->GetContentsView();
488 gfx::Rect
Widget::GetWindowBoundsInScreen() const {
489 return native_widget_
->GetWindowBoundsInScreen();
492 gfx::Rect
Widget::GetClientAreaBoundsInScreen() const {
493 return native_widget_
->GetClientAreaBoundsInScreen();
496 gfx::Rect
Widget::GetRestoredBounds() const {
497 return native_widget_
->GetRestoredBounds();
500 void Widget::SetBounds(const gfx::Rect
& bounds
) {
501 native_widget_
->SetBounds(bounds
);
504 void Widget::SetSize(const gfx::Size
& size
) {
505 native_widget_
->SetSize(size
);
508 void Widget::CenterWindow(const gfx::Size
& size
) {
509 native_widget_
->CenterWindow(size
);
512 void Widget::SetBoundsConstrained(const gfx::Rect
& bounds
) {
513 gfx::Rect work_area
=
514 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
515 bounds
.origin()).work_area();
516 if (work_area
.IsEmpty()) {
519 // Inset the work area slightly.
520 work_area
.Inset(10, 10, 10, 10);
521 work_area
.AdjustToFit(bounds
);
522 SetBounds(work_area
);
526 void Widget::SetVisibilityChangedAnimationsEnabled(bool value
) {
527 native_widget_
->SetVisibilityChangedAnimationsEnabled(value
);
530 void Widget::SetVisibilityAnimationDuration(const base::TimeDelta
& duration
) {
531 native_widget_
->SetVisibilityAnimationDuration(duration
);
534 void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition
) {
535 native_widget_
->SetVisibilityAnimationTransition(transition
);
538 Widget::MoveLoopResult
Widget::RunMoveLoop(
539 const gfx::Vector2d
& drag_offset
,
540 MoveLoopSource source
,
541 MoveLoopEscapeBehavior escape_behavior
) {
542 return native_widget_
->RunMoveLoop(drag_offset
, source
, escape_behavior
);
545 void Widget::EndMoveLoop() {
546 native_widget_
->EndMoveLoop();
549 void Widget::StackAboveWidget(Widget
* widget
) {
550 native_widget_
->StackAbove(widget
->GetNativeView());
553 void Widget::StackAbove(gfx::NativeView native_view
) {
554 native_widget_
->StackAbove(native_view
);
557 void Widget::StackAtTop() {
558 native_widget_
->StackAtTop();
561 void Widget::StackBelow(gfx::NativeView native_view
) {
562 native_widget_
->StackBelow(native_view
);
565 void Widget::SetShape(gfx::NativeRegion shape
) {
566 native_widget_
->SetShape(shape
);
569 void Widget::Close() {
570 if (widget_closed_
) {
571 // It appears we can hit this code path if you close a modal dialog then
572 // close the last browser before the destructor is hit, which triggers
573 // invoking Close again.
577 bool can_close
= true;
578 if (non_client_view_
)
579 can_close
= non_client_view_
->CanClose();
581 SaveWindowPlacement();
583 // During tear-down the top-level focus manager becomes unavailable to
584 // GTK tabbed panes and their children, so normal deregistration via
585 // |FormManager::ViewRemoved()| calls are fouled. We clear focus here
586 // to avoid these redundant steps and to avoid accessing deleted views
587 // that may have been in focus.
588 if (is_top_level() && focus_manager_
.get())
589 focus_manager_
->SetFocusedView(NULL
);
591 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
592 native_widget_
->Close();
593 widget_closed_
= true;
597 void Widget::CloseNow() {
598 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
599 native_widget_
->CloseNow();
602 bool Widget::IsClosed() const {
603 return widget_closed_
;
606 void Widget::Show() {
607 const ui::Layer
* layer
= GetLayer();
608 TRACE_EVENT1("views", "Widget::Show", "layer",
609 layer
? layer
->name() : "none");
610 if (non_client_view_
) {
611 // While initializing, the kiosk mode will go to full screen before the
612 // widget gets shown. In that case we stay in full screen mode, regardless
613 // of the |saved_show_state_| member.
614 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
615 !initial_restored_bounds_
.IsEmpty() &&
617 native_widget_
->ShowMaximizedWithBounds(initial_restored_bounds_
);
619 native_widget_
->ShowWithWindowState(
620 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN
: saved_show_state_
);
622 // |saved_show_state_| only applies the first time the window is shown.
623 // If we don't reset the value the window may be shown maximized every time
624 // it is subsequently shown after being hidden.
625 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
628 ? native_widget_
->Show()
629 : native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
633 void Widget::Hide() {
634 native_widget_
->Hide();
637 void Widget::ShowInactive() {
638 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
639 // call SetBounds()with the restored bounds to set the correct size. This
640 // normally should not happen, but if it does we should avoid showing unsized
642 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
643 !initial_restored_bounds_
.IsEmpty()) {
644 SetBounds(initial_restored_bounds_
);
645 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
647 native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
650 void Widget::Activate() {
651 native_widget_
->Activate();
654 void Widget::Deactivate() {
655 native_widget_
->Deactivate();
658 bool Widget::IsActive() const {
659 return native_widget_
->IsActive();
662 void Widget::DisableInactiveRendering() {
663 SetInactiveRenderingDisabled(true);
666 void Widget::SetAlwaysOnTop(bool on_top
) {
667 native_widget_
->SetAlwaysOnTop(on_top
);
670 bool Widget::IsAlwaysOnTop() const {
671 return native_widget_
->IsAlwaysOnTop();
674 void Widget::SetVisibleOnAllWorkspaces(bool always_visible
) {
675 native_widget_
->SetVisibleOnAllWorkspaces(always_visible
);
678 void Widget::Maximize() {
679 native_widget_
->Maximize();
682 void Widget::Minimize() {
683 native_widget_
->Minimize();
686 void Widget::Restore() {
687 native_widget_
->Restore();
690 bool Widget::IsMaximized() const {
691 return native_widget_
->IsMaximized();
694 bool Widget::IsMinimized() const {
695 return native_widget_
->IsMinimized();
698 void Widget::SetFullscreen(bool fullscreen
) {
699 if (IsFullscreen() == fullscreen
)
702 native_widget_
->SetFullscreen(fullscreen
);
704 if (non_client_view_
)
705 non_client_view_
->Layout();
708 bool Widget::IsFullscreen() const {
709 return native_widget_
->IsFullscreen();
712 void Widget::SetOpacity(unsigned char opacity
) {
713 native_widget_
->SetOpacity(opacity
);
716 void Widget::SetUseDragFrame(bool use_drag_frame
) {
717 native_widget_
->SetUseDragFrame(use_drag_frame
);
720 void Widget::FlashFrame(bool flash
) {
721 native_widget_
->FlashFrame(flash
);
724 View
* Widget::GetRootView() {
725 return root_view_
.get();
728 const View
* Widget::GetRootView() const {
729 return root_view_
.get();
732 bool Widget::IsVisible() const {
733 return native_widget_
->IsVisible();
736 ui::ThemeProvider
* Widget::GetThemeProvider() const {
737 const Widget
* root_widget
= GetTopLevelWidget();
738 if (root_widget
&& root_widget
!= this) {
739 // Attempt to get the theme provider, and fall back to the default theme
740 // provider if not found.
741 ui::ThemeProvider
* provider
= root_widget
->GetThemeProvider();
745 provider
= root_widget
->default_theme_provider_
.get();
749 return default_theme_provider_
.get();
752 const ui::NativeTheme
* Widget::GetNativeTheme() const {
753 return native_widget_
->GetNativeTheme();
756 FocusManager
* Widget::GetFocusManager() {
757 Widget
* toplevel_widget
= GetTopLevelWidget();
758 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
761 const FocusManager
* Widget::GetFocusManager() const {
762 const Widget
* toplevel_widget
= GetTopLevelWidget();
763 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
766 ui::TextInputClient
* Widget::GetFocusedTextInputClient() {
767 FocusManager
* focus_manager
= GetFocusManager();
768 View
* view
= focus_manager
? focus_manager
->GetFocusedView() : nullptr;
769 return view
? view
->GetTextInputClient() : nullptr;
772 InputMethod
* Widget::GetInputMethod() {
773 return const_cast<InputMethod
*>(
774 const_cast<const Widget
*>(this)->GetInputMethod());
777 const InputMethod
* Widget::GetInputMethod() const {
778 if (is_top_level()) {
779 if (!input_method_
.get())
780 input_method_
= const_cast<Widget
*>(this)->CreateInputMethod().Pass();
781 return input_method_
.get();
783 const Widget
* toplevel
= GetTopLevelWidget();
784 // If GetTopLevelWidget() returns itself which is not toplevel,
785 // the widget is detached from toplevel widget.
786 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
787 // if there is no toplevel. We probably need to add GetTopMostWidget()
788 // to replace some use cases.
789 return (toplevel
&& toplevel
!= this) ? toplevel
->GetInputMethod() : NULL
;
793 ui::InputMethod
* Widget::GetHostInputMethod() {
794 return native_widget_private()->GetHostInputMethod();
797 void Widget::RunShellDrag(View
* view
,
798 const ui::OSExchangeData
& data
,
799 const gfx::Point
& location
,
801 ui::DragDropTypes::DragEventSource source
) {
802 dragged_view_
= view
;
805 WidgetDeletionObserver
widget_deletion_observer(this);
806 native_widget_
->RunShellDrag(view
, data
, location
, operation
, source
);
808 // The widget may be destroyed during the drag operation.
809 if (!widget_deletion_observer
.IsWidgetAlive())
812 // If the view is removed during the drag operation, dragged_view_ is set to
814 if (view
&& dragged_view_
== view
) {
815 dragged_view_
= NULL
;
821 void Widget::SchedulePaintInRect(const gfx::Rect
& rect
) {
822 native_widget_
->SchedulePaintInRect(rect
);
825 void Widget::SetCursor(gfx::NativeCursor cursor
) {
826 native_widget_
->SetCursor(cursor
);
829 bool Widget::IsMouseEventsEnabled() const {
830 return native_widget_
->IsMouseEventsEnabled();
833 void Widget::SetNativeWindowProperty(const char* name
, void* value
) {
834 native_widget_
->SetNativeWindowProperty(name
, value
);
837 void* Widget::GetNativeWindowProperty(const char* name
) const {
838 return native_widget_
->GetNativeWindowProperty(name
);
841 void Widget::UpdateWindowTitle() {
842 if (!non_client_view_
)
845 // Update the native frame's text. We do this regardless of whether or not
846 // the native frame is being used, since this also updates the taskbar, etc.
847 base::string16 window_title
= widget_delegate_
->GetWindowTitle();
848 base::i18n::AdjustStringForLocaleDirection(&window_title
);
849 if (!native_widget_
->SetWindowTitle(window_title
))
851 non_client_view_
->UpdateWindowTitle();
853 // If the non-client view is rendering its own title, it'll need to relayout
854 // now and to get a paint update later on.
855 non_client_view_
->Layout();
858 void Widget::UpdateWindowIcon() {
859 if (non_client_view_
)
860 non_client_view_
->UpdateWindowIcon();
861 native_widget_
->SetWindowIcons(widget_delegate_
->GetWindowIcon(),
862 widget_delegate_
->GetWindowAppIcon());
865 FocusTraversable
* Widget::GetFocusTraversable() {
866 return static_cast<internal::RootView
*>(root_view_
.get());
869 void Widget::ThemeChanged() {
870 root_view_
->ThemeChanged();
873 void Widget::LocaleChanged() {
874 root_view_
->LocaleChanged();
877 void Widget::DeviceScaleFactorChanged(float device_scale_factor
) {
878 root_view_
->DeviceScaleFactorChanged(device_scale_factor
);
881 void Widget::SetFocusTraversableParent(FocusTraversable
* parent
) {
882 root_view_
->SetFocusTraversableParent(parent
);
885 void Widget::SetFocusTraversableParentView(View
* parent_view
) {
886 root_view_
->SetFocusTraversableParentView(parent_view
);
889 void Widget::ClearNativeFocus() {
890 native_widget_
->ClearNativeFocus();
893 NonClientFrameView
* Widget::CreateNonClientFrameView() {
894 NonClientFrameView
* frame_view
=
895 widget_delegate_
->CreateNonClientFrameView(this);
897 frame_view
= native_widget_
->CreateNonClientFrameView();
898 if (!frame_view
&& ViewsDelegate::views_delegate
) {
900 ViewsDelegate::views_delegate
->CreateDefaultNonClientFrameView(this);
905 CustomFrameView
* custom_frame_view
= new CustomFrameView
;
906 custom_frame_view
->Init(this);
907 return custom_frame_view
;
910 bool Widget::ShouldUseNativeFrame() const {
911 if (frame_type_
!= FRAME_TYPE_DEFAULT
)
912 return frame_type_
== FRAME_TYPE_FORCE_NATIVE
;
913 return native_widget_
->ShouldUseNativeFrame();
916 bool Widget::ShouldWindowContentsBeTransparent() const {
917 return native_widget_
->ShouldWindowContentsBeTransparent();
920 void Widget::DebugToggleFrameType() {
921 if (frame_type_
== FRAME_TYPE_DEFAULT
) {
922 frame_type_
= ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM
:
923 FRAME_TYPE_FORCE_NATIVE
;
925 frame_type_
= frame_type_
== FRAME_TYPE_FORCE_CUSTOM
?
926 FRAME_TYPE_FORCE_NATIVE
: FRAME_TYPE_FORCE_CUSTOM
;
931 void Widget::FrameTypeChanged() {
932 native_widget_
->FrameTypeChanged();
935 const ui::Compositor
* Widget::GetCompositor() const {
936 return native_widget_
->GetCompositor();
939 const ui::Layer
* Widget::GetLayer() const {
940 return native_widget_
->GetLayer();
943 void Widget::ReorderNativeViews() {
944 native_widget_
->ReorderNativeViews();
947 void Widget::UpdateRootLayers() {
948 // Calculate the layers requires traversing the tree, and since nearly any
949 // mutation of the tree can trigger this call we delay until absolutely
951 root_layers_dirty_
= true;
954 const NativeWidget
* Widget::native_widget() const {
955 return native_widget_
;
958 NativeWidget
* Widget::native_widget() {
959 return native_widget_
;
962 void Widget::SetCapture(View
* view
) {
963 if (!native_widget_
->HasCapture()) {
964 native_widget_
->SetCapture();
966 // Early return if setting capture was unsuccessful.
967 if (!native_widget_
->HasCapture())
971 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
972 is_mouse_button_pressed_
= true;
973 root_view_
->SetMouseHandler(view
);
976 void Widget::ReleaseCapture() {
977 if (native_widget_
->HasCapture())
978 native_widget_
->ReleaseCapture();
981 bool Widget::HasCapture() {
982 return native_widget_
->HasCapture();
985 TooltipManager
* Widget::GetTooltipManager() {
986 return native_widget_
->GetTooltipManager();
989 const TooltipManager
* Widget::GetTooltipManager() const {
990 return native_widget_
->GetTooltipManager();
993 gfx::Rect
Widget::GetWorkAreaBoundsInScreen() const {
994 return native_widget_
->GetWorkAreaBoundsInScreen();
997 void Widget::SynthesizeMouseMoveEvent() {
998 last_mouse_event_was_move_
= false;
999 ui::MouseEvent
mouse_event(ui::ET_MOUSE_MOVED
, last_mouse_event_position_
,
1000 last_mouse_event_position_
, ui::EventTimeForNow(),
1001 ui::EF_IS_SYNTHESIZED
, 0);
1002 root_view_
->OnMouseMoved(mouse_event
);
1005 void Widget::OnRootViewLayout() {
1006 native_widget_
->OnRootViewLayout();
1009 bool Widget::IsTranslucentWindowOpacitySupported() const {
1010 return native_widget_
->IsTranslucentWindowOpacitySupported();
1013 void Widget::OnSizeConstraintsChanged() {
1014 native_widget_
->OnSizeConstraintsChanged();
1015 non_client_view_
->SizeConstraintsChanged();
1018 void Widget::OnOwnerClosing() {
1021 ////////////////////////////////////////////////////////////////////////////////
1022 // Widget, NativeWidgetDelegate implementation:
1024 bool Widget::IsModal() const {
1025 return widget_delegate_
->GetModalType() != ui::MODAL_TYPE_NONE
;
1028 bool Widget::IsDialogBox() const {
1029 return !!widget_delegate_
->AsDialogDelegate();
1032 bool Widget::CanActivate() const {
1033 return widget_delegate_
->CanActivate();
1036 bool Widget::IsInactiveRenderingDisabled() const {
1037 return disable_inactive_rendering_
;
1040 void Widget::EnableInactiveRendering() {
1041 SetInactiveRenderingDisabled(false);
1044 void Widget::OnNativeWidgetActivationChanged(bool active
) {
1045 // On windows we may end up here before we've completed initialization (from
1046 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1047 // the Widget and will crash attempting to access it.
1048 if (!active
&& native_widget_initialized_
)
1049 SaveWindowPlacement();
1051 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1052 OnWidgetActivationChanged(this, active
));
1054 // During window creation, the widget gets focused without activation, and in
1055 // that case, the focus manager cannot set the appropriate text input client
1056 // because the widget is not active. Thus we have to notify the focus manager
1057 // not only when the focus changes but also when the widget gets activated.
1058 // See crbug.com/377479 for details.
1059 views::FocusManager
* focus_manager
= GetFocusManager();
1060 if (focus_manager
) {
1062 focus_manager
->FocusTextInputClient(focus_manager
->GetFocusedView());
1064 focus_manager
->BlurTextInputClient(focus_manager
->GetFocusedView());
1067 if (IsVisible() && non_client_view())
1068 non_client_view()->frame_view()->SchedulePaint();
1071 void Widget::OnNativeFocus() {
1072 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(GetNativeView());
1075 void Widget::OnNativeBlur() {
1076 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(nullptr);
1079 void Widget::OnNativeWidgetVisibilityChanging(bool visible
) {
1080 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1081 OnWidgetVisibilityChanging(this, visible
));
1084 void Widget::OnNativeWidgetVisibilityChanged(bool visible
) {
1085 View
* root
= GetRootView();
1087 root
->PropagateVisibilityNotifications(root
, visible
);
1088 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1089 OnWidgetVisibilityChanged(this, visible
));
1090 if (GetCompositor() && root
&& root
->layer())
1091 root
->layer()->SetVisible(visible
);
1094 void Widget::OnNativeWidgetCreated(bool desktop_widget
) {
1096 focus_manager_
.reset(FocusManagerFactory::Create(this, desktop_widget
));
1098 native_widget_
->InitModalType(widget_delegate_
->GetModalType());
1100 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetCreated(this));
1103 void Widget::OnNativeWidgetDestroying() {
1104 // Tell the focus manager (if any) that root_view is being removed
1105 // in case that the focused view is under this root view.
1106 if (GetFocusManager())
1107 GetFocusManager()->ViewRemoved(root_view_
.get());
1108 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetDestroying(this));
1109 if (non_client_view_
)
1110 non_client_view_
->WindowClosing();
1111 widget_delegate_
->WindowClosing();
1114 void Widget::OnNativeWidgetDestroyed() {
1115 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetDestroyed(this));
1116 widget_delegate_
->DeleteDelegate();
1117 widget_delegate_
= NULL
;
1118 native_widget_destroyed_
= true;
1121 gfx::Size
Widget::GetMinimumSize() const {
1122 return non_client_view_
? non_client_view_
->GetMinimumSize() : gfx::Size();
1125 gfx::Size
Widget::GetMaximumSize() const {
1126 return non_client_view_
? non_client_view_
->GetMaximumSize() : gfx::Size();
1129 void Widget::OnNativeWidgetMove() {
1130 widget_delegate_
->OnWidgetMove();
1131 View
* root
= GetRootView();
1132 if (root
&& root
->GetFocusManager()) {
1133 View
* focused_view
= root
->GetFocusManager()->GetFocusedView();
1134 if (focused_view
&& focused_view
->GetInputMethod())
1135 focused_view
->GetInputMethod()->OnCaretBoundsChanged(focused_view
);
1137 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetBoundsChanged(
1139 GetWindowBoundsInScreen()));
1142 void Widget::OnNativeWidgetSizeChanged(const gfx::Size
& new_size
) {
1143 View
* root
= GetRootView();
1145 root
->SetSize(new_size
);
1146 if (root
->GetFocusManager()) {
1147 View
* focused_view
= GetRootView()->GetFocusManager()->GetFocusedView();
1148 if (focused_view
&& focused_view
->GetInputMethod())
1149 focused_view
->GetInputMethod()->OnCaretBoundsChanged(focused_view
);
1152 SaveWindowPlacementIfInitialized();
1154 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetBoundsChanged(
1156 GetWindowBoundsInScreen()));
1159 void Widget::OnNativeWidgetWindowShowStateChanged() {
1160 SaveWindowPlacementIfInitialized();
1163 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1164 widget_delegate_
->OnWindowBeginUserBoundsChange();
1167 void Widget::OnNativeWidgetEndUserBoundsChange() {
1168 widget_delegate_
->OnWindowEndUserBoundsChange();
1171 bool Widget::HasFocusManager() const {
1172 return !!focus_manager_
.get();
1175 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect
& dirty_region
) {
1176 ui::Compositor
* compositor
= GetCompositor();
1180 compositor
->ScheduleRedrawRect(dirty_region
);
1184 void Widget::OnNativeWidgetPaint(const ui::PaintContext
& context
) {
1185 // On Linux Aura, we can get here during Init() because of the
1186 // SetInitialBounds call.
1187 if (!native_widget_initialized_
)
1189 GetRootView()->Paint(context
);
1192 int Widget::GetNonClientComponent(const gfx::Point
& point
) {
1193 int component
= non_client_view_
?
1194 non_client_view_
->NonClientHitTest(point
) :
1197 if (movement_disabled_
&& (component
== HTCAPTION
|| component
== HTSYSMENU
))
1203 void Widget::OnKeyEvent(ui::KeyEvent
* event
) {
1204 SendEventToProcessor(event
);
1207 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1208 // RootView from anywhere in Widget. Use
1209 // SendEventToProcessor() instead. See crbug.com/348087.
1210 void Widget::OnMouseEvent(ui::MouseEvent
* event
) {
1211 View
* root_view
= GetRootView();
1212 switch (event
->type()) {
1213 case ui::ET_MOUSE_PRESSED
: {
1214 last_mouse_event_was_move_
= false;
1216 // We may get deleted by the time we return from OnMousePressed. So we
1217 // use an observer to make sure we are still alive.
1218 WidgetDeletionObserver
widget_deletion_observer(this);
1220 // Make sure we're still visible before we attempt capture as the mouse
1221 // press processing may have made the window hide (as happens with menus).
1223 // It is possible for a View to show a context menu on mouse-press. Since
1224 // the menu does a capture and starts a nested message-loop, the release
1225 // would go to the menu. The next click (i.e. both mouse-press and release
1226 // events) also go to the menu. The menu (and the nested message-loop)
1227 // gets closed after this second release event. The code then resumes from
1228 // here. So make sure that the mouse-button is still down before doing a
1230 if (root_view
&& root_view
->OnMousePressed(*event
) &&
1231 widget_deletion_observer
.IsWidgetAlive() && IsVisible() &&
1232 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1233 is_mouse_button_pressed_
= true;
1234 if (!native_widget_
->HasCapture())
1235 native_widget_
->SetCapture();
1236 event
->SetHandled();
1241 case ui::ET_MOUSE_RELEASED
:
1242 last_mouse_event_was_move_
= false;
1243 is_mouse_button_pressed_
= false;
1244 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1245 if (auto_release_capture_
&& native_widget_
->HasCapture()) {
1246 base::AutoReset
<bool> resetter(&ignore_capture_loss_
, true);
1247 native_widget_
->ReleaseCapture();
1250 root_view
->OnMouseReleased(*event
);
1251 if ((event
->flags() & ui::EF_IS_NON_CLIENT
) == 0)
1252 event
->SetHandled();
1255 case ui::ET_MOUSE_MOVED
:
1256 case ui::ET_MOUSE_DRAGGED
:
1257 if (native_widget_
->HasCapture() && is_mouse_button_pressed_
) {
1258 last_mouse_event_was_move_
= false;
1260 root_view
->OnMouseDragged(*event
);
1261 } else if (!last_mouse_event_was_move_
||
1262 last_mouse_event_position_
!= event
->location()) {
1263 last_mouse_event_position_
= event
->location();
1264 last_mouse_event_was_move_
= true;
1266 root_view
->OnMouseMoved(*event
);
1270 case ui::ET_MOUSE_EXITED
:
1271 last_mouse_event_was_move_
= false;
1273 root_view
->OnMouseExited(*event
);
1276 case ui::ET_MOUSEWHEEL
:
1277 if (root_view
&& root_view
->OnMouseWheel(
1278 static_cast<const ui::MouseWheelEvent
&>(*event
)))
1279 event
->SetHandled();
1287 void Widget::OnMouseCaptureLost() {
1288 if (ignore_capture_loss_
)
1291 View
* root_view
= GetRootView();
1293 root_view
->OnMouseCaptureLost();
1294 is_mouse_button_pressed_
= false;
1297 void Widget::OnScrollEvent(ui::ScrollEvent
* event
) {
1298 ui::ScrollEvent
event_copy(*event
);
1299 SendEventToProcessor(&event_copy
);
1301 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1302 if (!event_copy
.handled() && event_copy
.type() == ui::ET_SCROLL
) {
1303 ui::MouseWheelEvent
wheel(*event
);
1304 OnMouseEvent(&wheel
);
1308 void Widget::OnGestureEvent(ui::GestureEvent
* event
) {
1309 // We explicitly do not capture here. Not capturing enables multiple widgets
1310 // to get tap events at the same time. Views (such as tab dragging) may
1311 // explicitly capture.
1312 SendEventToProcessor(event
);
1315 bool Widget::ExecuteCommand(int command_id
) {
1316 return widget_delegate_
->ExecuteWindowsCommand(command_id
);
1319 InputMethod
* Widget::GetInputMethodDirect() {
1320 return input_method_
.get();
1323 const std::vector
<ui::Layer
*>& Widget::GetRootLayers() {
1324 if (root_layers_dirty_
) {
1325 root_layers_dirty_
= false;
1326 root_layers_
.clear();
1327 BuildRootLayers(GetRootView(), &root_layers_
);
1329 return root_layers_
;
1332 bool Widget::HasHitTestMask() const {
1333 return widget_delegate_
->WidgetHasHitTestMask();
1336 void Widget::GetHitTestMask(gfx::Path
* mask
) const {
1338 widget_delegate_
->GetWidgetHitTestMask(mask
);
1341 Widget
* Widget::AsWidget() {
1345 const Widget
* Widget::AsWidget() const {
1349 bool Widget::SetInitialFocus(ui::WindowShowState show_state
) {
1350 View
* v
= widget_delegate_
->GetInitiallyFocusedView();
1351 if (!focus_on_creation_
|| show_state
== ui::SHOW_STATE_INACTIVE
||
1352 show_state
== ui::SHOW_STATE_MINIMIZED
) {
1353 // If not focusing the window now, tell the focus manager which view to
1354 // focus when the window is restored.
1356 focus_manager_
->SetStoredFocusView(v
);
1364 ////////////////////////////////////////////////////////////////////////////////
1365 // Widget, ui::EventSource implementation:
1366 ui::EventProcessor
* Widget::GetEventProcessor() {
1367 return root_view_
.get();
1370 ////////////////////////////////////////////////////////////////////////////////
1371 // Widget, FocusTraversable implementation:
1373 FocusSearch
* Widget::GetFocusSearch() {
1374 return root_view_
->GetFocusSearch();
1377 FocusTraversable
* Widget::GetFocusTraversableParent() {
1378 // We are a proxy to the root view, so we should be bypassed when traversing
1379 // up and as a result this should not be called.
1384 View
* Widget::GetFocusTraversableParentView() {
1385 // We are a proxy to the root view, so we should be bypassed when traversing
1386 // up and as a result this should not be called.
1391 ////////////////////////////////////////////////////////////////////////////////
1392 // Widget, ui::NativeThemeObserver implementation:
1394 void Widget::OnNativeThemeUpdated(ui::NativeTheme
* observed_theme
) {
1395 DCHECK(observer_manager_
.IsObserving(observed_theme
));
1397 ui::NativeTheme
* current_native_theme
= GetNativeTheme();
1398 if (!observer_manager_
.IsObserving(current_native_theme
)) {
1399 observer_manager_
.RemoveAll();
1400 observer_manager_
.Add(current_native_theme
);
1403 root_view_
->PropagateNativeThemeChanged(current_native_theme
);
1406 ////////////////////////////////////////////////////////////////////////////////
1407 // Widget, protected:
1409 internal::RootView
* Widget::CreateRootView() {
1410 return new internal::RootView(this);
1413 void Widget::DestroyRootView() {
1414 non_client_view_
= NULL
;
1416 // Input method has to be destroyed before focus manager.
1417 input_method_
.reset();
1420 void Widget::OnDragWillStart() {
1423 void Widget::OnDragComplete() {
1426 ////////////////////////////////////////////////////////////////////////////////
1429 void Widget::SetInactiveRenderingDisabled(bool value
) {
1430 if (value
== disable_inactive_rendering_
)
1433 disable_inactive_rendering_
= value
;
1434 if (non_client_view_
)
1435 non_client_view_
->SetInactiveRenderingDisabled(value
);
1438 void Widget::SaveWindowPlacement() {
1439 // The window delegate does the actual saving for us. It seems like (judging
1440 // by go/crash) that in some circumstances we can end up here after
1441 // WM_DESTROY, at which point the window delegate is likely gone. So just
1443 if (!widget_delegate_
)
1446 ui::WindowShowState show_state
= ui::SHOW_STATE_NORMAL
;
1448 native_widget_
->GetWindowPlacement(&bounds
, &show_state
);
1449 widget_delegate_
->SaveWindowPlacement(bounds
, show_state
);
1452 void Widget::SaveWindowPlacementIfInitialized() {
1453 if (native_widget_initialized_
)
1454 SaveWindowPlacement();
1457 void Widget::SetInitialBounds(const gfx::Rect
& bounds
) {
1458 if (!non_client_view_
)
1461 gfx::Rect saved_bounds
;
1462 if (GetSavedWindowPlacement(&saved_bounds
, &saved_show_state_
)) {
1463 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
) {
1464 // If we're going to maximize, wait until Show is invoked to set the
1465 // bounds. That way we avoid a noticeable resize.
1466 initial_restored_bounds_
= saved_bounds
;
1467 } else if (!saved_bounds
.IsEmpty()) {
1468 // If the saved bounds are valid, use them.
1469 SetBounds(saved_bounds
);
1472 if (bounds
.IsEmpty()) {
1473 // No initial bounds supplied, so size the window to its content and
1474 // center over its parent.
1475 native_widget_
->CenterWindow(non_client_view_
->GetPreferredSize());
1477 // Use the supplied initial bounds.
1478 SetBoundsConstrained(bounds
);
1483 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect
& bounds
) {
1484 if (bounds
.IsEmpty()) {
1485 View
* contents_view
= GetContentsView();
1486 DCHECK(contents_view
);
1487 // No initial bounds supplied, so size the window to its content and
1488 // center over its parent if preferred size is provided.
1489 gfx::Size size
= contents_view
->GetPreferredSize();
1490 if (!size
.IsEmpty())
1491 native_widget_
->CenterWindow(size
);
1493 // Use the supplied initial bounds.
1494 SetBoundsConstrained(bounds
);
1498 bool Widget::GetSavedWindowPlacement(gfx::Rect
* bounds
,
1499 ui::WindowShowState
* show_state
) {
1500 // First we obtain the window's saved show-style and store it. We need to do
1501 // this here, rather than in Show() because by the time Show() is called,
1502 // the window's size will have been reset (below) and the saved maximized
1503 // state will have been lost. Sadly there's no way to tell on Windows when
1504 // a window is restored from maximized state, so we can't more accurately
1505 // track maximized state independently of sizing information.
1507 // Restore the window's placement from the controller.
1508 if (widget_delegate_
->GetSavedWindowPlacement(this, bounds
, show_state
)) {
1509 if (!widget_delegate_
->ShouldRestoreWindowSize()) {
1510 bounds
->set_size(non_client_view_
->GetPreferredSize());
1512 gfx::Size minimum_size
= GetMinimumSize();
1513 // Make sure the bounds are at least the minimum size.
1514 if (bounds
->width() < minimum_size
.width())
1515 bounds
->set_width(minimum_size
.width());
1517 if (bounds
->height() < minimum_size
.height())
1518 bounds
->set_height(minimum_size
.height());
1525 scoped_ptr
<InputMethod
> Widget::CreateInputMethod() {
1526 scoped_ptr
<InputMethod
> input_method(native_widget_
->CreateInputMethod());
1527 if (input_method
.get())
1528 input_method
->Init(this);
1529 return input_method
.Pass();
1532 void Widget::ReplaceInputMethod(InputMethod
* input_method
) {
1533 input_method_
.reset(input_method
);
1534 input_method
->SetDelegate(native_widget_
->GetInputMethodDelegate());
1535 input_method
->Init(this);
1538 namespace internal
{
1540 ////////////////////////////////////////////////////////////////////////////////
1541 // internal::NativeWidgetPrivate, NativeWidget implementation:
1543 internal::NativeWidgetPrivate
* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1547 } // namespace internal
1548 } // namespace views