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(aura::WINDOW_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(aura::WINDOW_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 title before setting the window's initial bounds;
361 // the frame view's preferred height may depend on the presence of a title.
363 non_client_view_
->ResetWindowControls();
364 SetInitialBounds(params
.bounds
);
365 if (params
.show_state
== ui::SHOW_STATE_MAXIMIZED
)
367 else if (params
.show_state
== ui::SHOW_STATE_MINIMIZED
)
369 } else if (params
.delegate
) {
370 SetContentsView(params
.delegate
->GetContentsView());
371 SetInitialBoundsForFramelessWindow(params
.bounds
);
373 // This must come after SetContentsView() or it might not be able to find
374 // the correct NativeTheme (on Linux). See http://crbug.com/384492
375 observer_manager_
.Add(GetNativeTheme());
376 native_widget_initialized_
= true;
379 // Unconverted methods (see header) --------------------------------------------
381 gfx::NativeView
Widget::GetNativeView() const {
382 return native_widget_
->GetNativeView();
385 gfx::NativeWindow
Widget::GetNativeWindow() const {
386 return native_widget_
->GetNativeWindow();
389 void Widget::AddObserver(WidgetObserver
* observer
) {
390 observers_
.AddObserver(observer
);
393 void Widget::RemoveObserver(WidgetObserver
* observer
) {
394 observers_
.RemoveObserver(observer
);
397 bool Widget::HasObserver(const WidgetObserver
* observer
) const {
398 return observers_
.HasObserver(observer
);
401 void Widget::AddRemovalsObserver(WidgetRemovalsObserver
* observer
) {
402 removals_observers_
.AddObserver(observer
);
405 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver
* observer
) {
406 removals_observers_
.RemoveObserver(observer
);
409 bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver
* observer
) const {
410 return removals_observers_
.HasObserver(observer
);
413 bool Widget::GetAccelerator(int cmd_id
, ui::Accelerator
* accelerator
) const {
417 void Widget::ViewHierarchyChanged(
418 const View::ViewHierarchyChangedDetails
& details
) {
419 if (!details
.is_add
) {
420 if (details
.child
== dragged_view_
)
421 dragged_view_
= NULL
;
422 FocusManager
* focus_manager
= GetFocusManager();
424 focus_manager
->ViewRemoved(details
.child
);
425 ViewStorage::GetInstance()->ViewRemoved(details
.child
);
426 native_widget_
->ViewRemoved(details
.child
);
430 void Widget::NotifyNativeViewHierarchyWillChange() {
431 FocusManager
* focus_manager
= GetFocusManager();
432 // We are being removed from a window hierarchy. Treat this as
433 // the root_view_ being removed.
435 focus_manager
->ViewRemoved(root_view_
.get());
438 void Widget::NotifyNativeViewHierarchyChanged() {
439 root_view_
->NotifyNativeViewHierarchyChanged();
442 void Widget::NotifyWillRemoveView(View
* view
) {
443 FOR_EACH_OBSERVER(WidgetRemovalsObserver
,
445 OnWillRemoveView(this, view
));
448 // Converted methods (see header) ----------------------------------------------
450 Widget
* Widget::GetTopLevelWidget() {
451 return const_cast<Widget
*>(
452 static_cast<const Widget
*>(this)->GetTopLevelWidget());
455 const Widget
* Widget::GetTopLevelWidget() const {
456 // GetTopLevelNativeWidget doesn't work during destruction because
457 // property is gone after gobject gets deleted. Short circuit here
458 // for toplevel so that InputMethod can remove itself from
460 return is_top_level() ? this : native_widget_
->GetTopLevelWidget();
463 void Widget::SetContentsView(View
* view
) {
464 // Do not SetContentsView() again if it is already set to the same view.
465 if (view
== GetContentsView())
467 root_view_
->SetContentsView(view
);
468 if (non_client_view_
!= view
) {
469 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
470 // was true when the widget was initialized. Creating widgets with non
471 // client views and then setting the contents view can cause subtle
472 // problems on Windows, where the native widget thinks there is still a
473 // |non_client_view_|. If you get this error, either use a different type
474 // when initializing the widget, or don't call SetContentsView().
475 DCHECK(!non_client_view_
);
476 non_client_view_
= NULL
;
480 View
* Widget::GetContentsView() {
481 return root_view_
->GetContentsView();
484 gfx::Rect
Widget::GetWindowBoundsInScreen() const {
485 return native_widget_
->GetWindowBoundsInScreen();
488 gfx::Rect
Widget::GetClientAreaBoundsInScreen() const {
489 return native_widget_
->GetClientAreaBoundsInScreen();
492 gfx::Rect
Widget::GetRestoredBounds() const {
493 return native_widget_
->GetRestoredBounds();
496 void Widget::SetBounds(const gfx::Rect
& bounds
) {
497 native_widget_
->SetBounds(bounds
);
500 void Widget::SetSize(const gfx::Size
& size
) {
501 native_widget_
->SetSize(size
);
504 void Widget::CenterWindow(const gfx::Size
& size
) {
505 native_widget_
->CenterWindow(size
);
508 void Widget::SetBoundsConstrained(const gfx::Rect
& bounds
) {
509 gfx::Rect work_area
=
510 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
511 bounds
.origin()).work_area();
512 if (work_area
.IsEmpty()) {
515 // Inset the work area slightly.
516 work_area
.Inset(10, 10, 10, 10);
517 work_area
.AdjustToFit(bounds
);
518 SetBounds(work_area
);
522 void Widget::SetVisibilityChangedAnimationsEnabled(bool value
) {
523 native_widget_
->SetVisibilityChangedAnimationsEnabled(value
);
526 void Widget::SetVisibilityAnimationDuration(const base::TimeDelta
& duration
) {
527 native_widget_
->SetVisibilityAnimationDuration(duration
);
530 void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition
) {
531 native_widget_
->SetVisibilityAnimationTransition(transition
);
534 Widget::MoveLoopResult
Widget::RunMoveLoop(
535 const gfx::Vector2d
& drag_offset
,
536 MoveLoopSource source
,
537 MoveLoopEscapeBehavior escape_behavior
) {
538 return native_widget_
->RunMoveLoop(drag_offset
, source
, escape_behavior
);
541 void Widget::EndMoveLoop() {
542 native_widget_
->EndMoveLoop();
545 void Widget::StackAboveWidget(Widget
* widget
) {
546 native_widget_
->StackAbove(widget
->GetNativeView());
549 void Widget::StackAbove(gfx::NativeView native_view
) {
550 native_widget_
->StackAbove(native_view
);
553 void Widget::StackAtTop() {
554 native_widget_
->StackAtTop();
557 void Widget::StackBelow(gfx::NativeView native_view
) {
558 native_widget_
->StackBelow(native_view
);
561 void Widget::SetShape(gfx::NativeRegion shape
) {
562 native_widget_
->SetShape(shape
);
565 void Widget::Close() {
566 if (widget_closed_
) {
567 // It appears we can hit this code path if you close a modal dialog then
568 // close the last browser before the destructor is hit, which triggers
569 // invoking Close again.
573 bool can_close
= true;
574 if (non_client_view_
)
575 can_close
= non_client_view_
->CanClose();
577 SaveWindowPlacement();
579 // During tear-down the top-level focus manager becomes unavailable to
580 // GTK tabbed panes and their children, so normal deregistration via
581 // |FormManager::ViewRemoved()| calls are fouled. We clear focus here
582 // to avoid these redundant steps and to avoid accessing deleted views
583 // that may have been in focus.
584 if (is_top_level() && focus_manager_
.get())
585 focus_manager_
->SetFocusedView(NULL
);
587 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
588 native_widget_
->Close();
589 widget_closed_
= true;
593 void Widget::CloseNow() {
594 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
595 native_widget_
->CloseNow();
598 bool Widget::IsClosed() const {
599 return widget_closed_
;
602 void Widget::Show() {
603 const ui::Layer
* layer
= GetLayer();
604 TRACE_EVENT1("views", "Widget::Show", "layer",
605 layer
? layer
->name() : "none");
606 if (non_client_view_
) {
607 // While initializing, the kiosk mode will go to full screen before the
608 // widget gets shown. In that case we stay in full screen mode, regardless
609 // of the |saved_show_state_| member.
610 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
611 !initial_restored_bounds_
.IsEmpty() &&
613 native_widget_
->ShowMaximizedWithBounds(initial_restored_bounds_
);
615 native_widget_
->ShowWithWindowState(
616 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN
: saved_show_state_
);
618 // |saved_show_state_| only applies the first time the window is shown.
619 // If we don't reset the value the window may be shown maximized every time
620 // it is subsequently shown after being hidden.
621 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
624 ? native_widget_
->Show()
625 : native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
629 void Widget::Hide() {
630 native_widget_
->Hide();
633 void Widget::ShowInactive() {
634 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
635 // call SetBounds()with the restored bounds to set the correct size. This
636 // normally should not happen, but if it does we should avoid showing unsized
638 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
639 !initial_restored_bounds_
.IsEmpty()) {
640 SetBounds(initial_restored_bounds_
);
641 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
643 native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
646 void Widget::Activate() {
647 native_widget_
->Activate();
650 void Widget::Deactivate() {
651 native_widget_
->Deactivate();
654 bool Widget::IsActive() const {
655 return native_widget_
->IsActive();
658 void Widget::DisableInactiveRendering() {
659 SetInactiveRenderingDisabled(true);
662 void Widget::SetAlwaysOnTop(bool on_top
) {
663 native_widget_
->SetAlwaysOnTop(on_top
);
666 bool Widget::IsAlwaysOnTop() const {
667 return native_widget_
->IsAlwaysOnTop();
670 void Widget::SetVisibleOnAllWorkspaces(bool always_visible
) {
671 native_widget_
->SetVisibleOnAllWorkspaces(always_visible
);
674 void Widget::Maximize() {
675 native_widget_
->Maximize();
678 void Widget::Minimize() {
679 native_widget_
->Minimize();
682 void Widget::Restore() {
683 native_widget_
->Restore();
686 bool Widget::IsMaximized() const {
687 return native_widget_
->IsMaximized();
690 bool Widget::IsMinimized() const {
691 return native_widget_
->IsMinimized();
694 void Widget::SetFullscreen(bool fullscreen
) {
695 if (IsFullscreen() == fullscreen
)
698 native_widget_
->SetFullscreen(fullscreen
);
700 if (non_client_view_
)
701 non_client_view_
->Layout();
704 bool Widget::IsFullscreen() const {
705 return native_widget_
->IsFullscreen();
708 void Widget::SetOpacity(unsigned char opacity
) {
709 native_widget_
->SetOpacity(opacity
);
712 void Widget::SetUseDragFrame(bool use_drag_frame
) {
713 native_widget_
->SetUseDragFrame(use_drag_frame
);
716 void Widget::FlashFrame(bool flash
) {
717 native_widget_
->FlashFrame(flash
);
720 View
* Widget::GetRootView() {
721 return root_view_
.get();
724 const View
* Widget::GetRootView() const {
725 return root_view_
.get();
728 bool Widget::IsVisible() const {
729 return native_widget_
->IsVisible();
732 ui::ThemeProvider
* Widget::GetThemeProvider() const {
733 const Widget
* root_widget
= GetTopLevelWidget();
734 if (root_widget
&& root_widget
!= this) {
735 // Attempt to get the theme provider, and fall back to the default theme
736 // provider if not found.
737 ui::ThemeProvider
* provider
= root_widget
->GetThemeProvider();
741 provider
= root_widget
->default_theme_provider_
.get();
745 return default_theme_provider_
.get();
748 const ui::NativeTheme
* Widget::GetNativeTheme() const {
749 return native_widget_
->GetNativeTheme();
752 FocusManager
* Widget::GetFocusManager() {
753 Widget
* toplevel_widget
= GetTopLevelWidget();
754 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
757 const FocusManager
* Widget::GetFocusManager() const {
758 const Widget
* toplevel_widget
= GetTopLevelWidget();
759 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
762 ui::TextInputClient
* Widget::GetFocusedTextInputClient() {
763 FocusManager
* focus_manager
= GetFocusManager();
764 View
* view
= focus_manager
? focus_manager
->GetFocusedView() : nullptr;
765 return view
? view
->GetTextInputClient() : nullptr;
768 InputMethod
* Widget::GetInputMethod() {
769 return const_cast<InputMethod
*>(
770 const_cast<const Widget
*>(this)->GetInputMethod());
773 const InputMethod
* Widget::GetInputMethod() const {
774 if (is_top_level()) {
775 if (!input_method_
.get())
776 input_method_
= const_cast<Widget
*>(this)->CreateInputMethod().Pass();
777 return input_method_
.get();
779 const Widget
* toplevel
= GetTopLevelWidget();
780 // If GetTopLevelWidget() returns itself which is not toplevel,
781 // the widget is detached from toplevel widget.
782 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
783 // if there is no toplevel. We probably need to add GetTopMostWidget()
784 // to replace some use cases.
785 return (toplevel
&& toplevel
!= this) ? toplevel
->GetInputMethod() : NULL
;
789 ui::InputMethod
* Widget::GetHostInputMethod() {
790 return native_widget_private()->GetHostInputMethod();
793 void Widget::RunShellDrag(View
* view
,
794 const ui::OSExchangeData
& data
,
795 const gfx::Point
& location
,
797 ui::DragDropTypes::DragEventSource source
) {
798 dragged_view_
= view
;
801 WidgetDeletionObserver
widget_deletion_observer(this);
802 native_widget_
->RunShellDrag(view
, data
, location
, operation
, source
);
804 // The widget may be destroyed during the drag operation.
805 if (!widget_deletion_observer
.IsWidgetAlive())
808 // If the view is removed during the drag operation, dragged_view_ is set to
810 if (view
&& dragged_view_
== view
) {
811 dragged_view_
= NULL
;
817 void Widget::SchedulePaintInRect(const gfx::Rect
& rect
) {
818 native_widget_
->SchedulePaintInRect(rect
);
821 void Widget::SetCursor(gfx::NativeCursor cursor
) {
822 native_widget_
->SetCursor(cursor
);
825 bool Widget::IsMouseEventsEnabled() const {
826 return native_widget_
->IsMouseEventsEnabled();
829 void Widget::SetNativeWindowProperty(const char* name
, void* value
) {
830 native_widget_
->SetNativeWindowProperty(name
, value
);
833 void* Widget::GetNativeWindowProperty(const char* name
) const {
834 return native_widget_
->GetNativeWindowProperty(name
);
837 void Widget::UpdateWindowTitle() {
838 if (!non_client_view_
)
841 // Update the native frame's text. We do this regardless of whether or not
842 // the native frame is being used, since this also updates the taskbar, etc.
843 base::string16 window_title
= widget_delegate_
->GetWindowTitle();
844 base::i18n::AdjustStringForLocaleDirection(&window_title
);
845 if (!native_widget_
->SetWindowTitle(window_title
))
847 non_client_view_
->UpdateWindowTitle();
849 // If the non-client view is rendering its own title, it'll need to relayout
850 // now and to get a paint update later on.
851 non_client_view_
->Layout();
854 void Widget::UpdateWindowIcon() {
855 if (non_client_view_
)
856 non_client_view_
->UpdateWindowIcon();
857 native_widget_
->SetWindowIcons(widget_delegate_
->GetWindowIcon(),
858 widget_delegate_
->GetWindowAppIcon());
861 FocusTraversable
* Widget::GetFocusTraversable() {
862 return static_cast<internal::RootView
*>(root_view_
.get());
865 void Widget::ThemeChanged() {
866 root_view_
->ThemeChanged();
869 void Widget::LocaleChanged() {
870 root_view_
->LocaleChanged();
873 void Widget::DeviceScaleFactorChanged(float device_scale_factor
) {
874 root_view_
->DeviceScaleFactorChanged(device_scale_factor
);
877 void Widget::SetFocusTraversableParent(FocusTraversable
* parent
) {
878 root_view_
->SetFocusTraversableParent(parent
);
881 void Widget::SetFocusTraversableParentView(View
* parent_view
) {
882 root_view_
->SetFocusTraversableParentView(parent_view
);
885 void Widget::ClearNativeFocus() {
886 native_widget_
->ClearNativeFocus();
889 NonClientFrameView
* Widget::CreateNonClientFrameView() {
890 NonClientFrameView
* frame_view
=
891 widget_delegate_
->CreateNonClientFrameView(this);
893 frame_view
= native_widget_
->CreateNonClientFrameView();
894 if (!frame_view
&& ViewsDelegate::views_delegate
) {
896 ViewsDelegate::views_delegate
->CreateDefaultNonClientFrameView(this);
901 CustomFrameView
* custom_frame_view
= new CustomFrameView
;
902 custom_frame_view
->Init(this);
903 return custom_frame_view
;
906 bool Widget::ShouldUseNativeFrame() const {
907 if (frame_type_
!= FRAME_TYPE_DEFAULT
)
908 return frame_type_
== FRAME_TYPE_FORCE_NATIVE
;
909 return native_widget_
->ShouldUseNativeFrame();
912 bool Widget::ShouldWindowContentsBeTransparent() const {
913 return native_widget_
->ShouldWindowContentsBeTransparent();
916 void Widget::DebugToggleFrameType() {
917 if (frame_type_
== FRAME_TYPE_DEFAULT
) {
918 frame_type_
= ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM
:
919 FRAME_TYPE_FORCE_NATIVE
;
921 frame_type_
= frame_type_
== FRAME_TYPE_FORCE_CUSTOM
?
922 FRAME_TYPE_FORCE_NATIVE
: FRAME_TYPE_FORCE_CUSTOM
;
927 void Widget::FrameTypeChanged() {
928 native_widget_
->FrameTypeChanged();
931 const ui::Compositor
* Widget::GetCompositor() const {
932 return native_widget_
->GetCompositor();
935 const ui::Layer
* Widget::GetLayer() const {
936 return native_widget_
->GetLayer();
939 void Widget::ReorderNativeViews() {
940 native_widget_
->ReorderNativeViews();
943 void Widget::UpdateRootLayers() {
944 // Calculate the layers requires traversing the tree, and since nearly any
945 // mutation of the tree can trigger this call we delay until absolutely
947 root_layers_dirty_
= true;
950 const NativeWidget
* Widget::native_widget() const {
951 return native_widget_
;
954 NativeWidget
* Widget::native_widget() {
955 return native_widget_
;
958 void Widget::SetCapture(View
* view
) {
959 if (!native_widget_
->HasCapture()) {
960 native_widget_
->SetCapture();
962 // Early return if setting capture was unsuccessful.
963 if (!native_widget_
->HasCapture())
967 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
968 is_mouse_button_pressed_
= true;
969 root_view_
->SetMouseHandler(view
);
972 void Widget::ReleaseCapture() {
973 if (native_widget_
->HasCapture())
974 native_widget_
->ReleaseCapture();
977 bool Widget::HasCapture() {
978 return native_widget_
->HasCapture();
981 TooltipManager
* Widget::GetTooltipManager() {
982 return native_widget_
->GetTooltipManager();
985 const TooltipManager
* Widget::GetTooltipManager() const {
986 return native_widget_
->GetTooltipManager();
989 gfx::Rect
Widget::GetWorkAreaBoundsInScreen() const {
990 return native_widget_
->GetWorkAreaBoundsInScreen();
993 void Widget::SynthesizeMouseMoveEvent() {
994 last_mouse_event_was_move_
= false;
995 ui::MouseEvent
mouse_event(ui::ET_MOUSE_MOVED
, last_mouse_event_position_
,
996 last_mouse_event_position_
, ui::EventTimeForNow(),
997 ui::EF_IS_SYNTHESIZED
, 0);
998 root_view_
->OnMouseMoved(mouse_event
);
1001 void Widget::OnRootViewLayout() {
1002 native_widget_
->OnRootViewLayout();
1005 bool Widget::IsTranslucentWindowOpacitySupported() const {
1006 return native_widget_
->IsTranslucentWindowOpacitySupported();
1009 void Widget::OnSizeConstraintsChanged() {
1010 native_widget_
->OnSizeConstraintsChanged();
1011 non_client_view_
->SizeConstraintsChanged();
1014 void Widget::OnOwnerClosing() {
1017 ////////////////////////////////////////////////////////////////////////////////
1018 // Widget, NativeWidgetDelegate implementation:
1020 bool Widget::IsModal() const {
1021 return widget_delegate_
->GetModalType() != ui::MODAL_TYPE_NONE
;
1024 bool Widget::IsDialogBox() const {
1025 return !!widget_delegate_
->AsDialogDelegate();
1028 bool Widget::CanActivate() const {
1029 return widget_delegate_
->CanActivate();
1032 bool Widget::IsInactiveRenderingDisabled() const {
1033 return disable_inactive_rendering_
;
1036 void Widget::EnableInactiveRendering() {
1037 SetInactiveRenderingDisabled(false);
1040 void Widget::OnNativeWidgetActivationChanged(bool active
) {
1041 // On windows we may end up here before we've completed initialization (from
1042 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1043 // the Widget and will crash attempting to access it.
1044 if (!active
&& native_widget_initialized_
)
1045 SaveWindowPlacement();
1047 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1048 OnWidgetActivationChanged(this, active
));
1050 // During window creation, the widget gets focused without activation, and in
1051 // that case, the focus manager cannot set the appropriate text input client
1052 // because the widget is not active. Thus we have to notify the focus manager
1053 // not only when the focus changes but also when the widget gets activated.
1054 // See crbug.com/377479 for details.
1055 views::FocusManager
* focus_manager
= GetFocusManager();
1056 if (focus_manager
) {
1058 focus_manager
->FocusTextInputClient(focus_manager
->GetFocusedView());
1060 focus_manager
->BlurTextInputClient(focus_manager
->GetFocusedView());
1063 if (IsVisible() && non_client_view())
1064 non_client_view()->frame_view()->SchedulePaint();
1067 void Widget::OnNativeFocus(gfx::NativeView old_focused_view
) {
1068 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(old_focused_view
,
1072 void Widget::OnNativeBlur(gfx::NativeView new_focused_view
) {
1073 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(GetNativeView(),
1077 void Widget::OnNativeWidgetVisibilityChanging(bool visible
) {
1078 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1079 OnWidgetVisibilityChanging(this, visible
));
1082 void Widget::OnNativeWidgetVisibilityChanged(bool visible
) {
1083 View
* root
= GetRootView();
1085 root
->PropagateVisibilityNotifications(root
, visible
);
1086 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1087 OnWidgetVisibilityChanged(this, visible
));
1088 if (GetCompositor() && root
&& root
->layer())
1089 root
->layer()->SetVisible(visible
);
1092 void Widget::OnNativeWidgetCreated(bool desktop_widget
) {
1094 focus_manager_
.reset(FocusManagerFactory::Create(this, desktop_widget
));
1096 native_widget_
->InitModalType(widget_delegate_
->GetModalType());
1098 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetCreated(this));
1101 void Widget::OnNativeWidgetDestroying() {
1102 // Tell the focus manager (if any) that root_view is being removed
1103 // in case that the focused view is under this root view.
1104 if (GetFocusManager())
1105 GetFocusManager()->ViewRemoved(root_view_
.get());
1106 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetDestroying(this));
1107 if (non_client_view_
)
1108 non_client_view_
->WindowClosing();
1109 widget_delegate_
->WindowClosing();
1112 void Widget::OnNativeWidgetDestroyed() {
1113 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetDestroyed(this));
1114 widget_delegate_
->DeleteDelegate();
1115 widget_delegate_
= NULL
;
1116 native_widget_destroyed_
= true;
1119 gfx::Size
Widget::GetMinimumSize() const {
1120 return non_client_view_
? non_client_view_
->GetMinimumSize() : gfx::Size();
1123 gfx::Size
Widget::GetMaximumSize() const {
1124 return non_client_view_
? non_client_view_
->GetMaximumSize() : gfx::Size();
1127 void Widget::OnNativeWidgetMove() {
1128 widget_delegate_
->OnWidgetMove();
1129 View
* root
= GetRootView();
1130 if (root
&& root
->GetFocusManager()) {
1131 View
* focused_view
= root
->GetFocusManager()->GetFocusedView();
1132 if (focused_view
&& focused_view
->GetInputMethod())
1133 focused_view
->GetInputMethod()->OnCaretBoundsChanged(focused_view
);
1135 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetBoundsChanged(
1137 GetWindowBoundsInScreen()));
1140 void Widget::OnNativeWidgetSizeChanged(const gfx::Size
& new_size
) {
1141 View
* root
= GetRootView();
1143 root
->SetSize(new_size
);
1144 if (root
->GetFocusManager()) {
1145 View
* focused_view
= GetRootView()->GetFocusManager()->GetFocusedView();
1146 if (focused_view
&& focused_view
->GetInputMethod())
1147 focused_view
->GetInputMethod()->OnCaretBoundsChanged(focused_view
);
1150 SaveWindowPlacementIfInitialized();
1152 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetBoundsChanged(
1154 GetWindowBoundsInScreen()));
1157 void Widget::OnNativeWidgetWindowShowStateChanged() {
1158 SaveWindowPlacementIfInitialized();
1161 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1162 widget_delegate_
->OnWindowBeginUserBoundsChange();
1165 void Widget::OnNativeWidgetEndUserBoundsChange() {
1166 widget_delegate_
->OnWindowEndUserBoundsChange();
1169 bool Widget::HasFocusManager() const {
1170 return !!focus_manager_
.get();
1173 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect
& dirty_region
) {
1174 ui::Compositor
* compositor
= GetCompositor();
1178 compositor
->ScheduleRedrawRect(dirty_region
);
1182 void Widget::OnNativeWidgetPaint(gfx::Canvas
* canvas
) {
1183 // On Linux Aura, we can get here during Init() because of the
1184 // SetInitialBounds call.
1185 if (native_widget_initialized_
)
1186 GetRootView()->Paint(canvas
, CullSet());
1189 int Widget::GetNonClientComponent(const gfx::Point
& point
) {
1190 int component
= non_client_view_
?
1191 non_client_view_
->NonClientHitTest(point
) :
1194 if (movement_disabled_
&& (component
== HTCAPTION
|| component
== HTSYSMENU
))
1200 void Widget::OnKeyEvent(ui::KeyEvent
* event
) {
1201 SendEventToProcessor(event
);
1204 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1205 // RootView from anywhere in Widget. Use
1206 // SendEventToProcessor() instead. See crbug.com/348087.
1207 void Widget::OnMouseEvent(ui::MouseEvent
* event
) {
1208 View
* root_view
= GetRootView();
1209 switch (event
->type()) {
1210 case ui::ET_MOUSE_PRESSED
: {
1211 last_mouse_event_was_move_
= false;
1213 // We may get deleted by the time we return from OnMousePressed. So we
1214 // use an observer to make sure we are still alive.
1215 WidgetDeletionObserver
widget_deletion_observer(this);
1217 // Make sure we're still visible before we attempt capture as the mouse
1218 // press processing may have made the window hide (as happens with menus).
1220 // It is possible for a View to show a context menu on mouse-press. Since
1221 // the menu does a capture and starts a nested message-loop, the release
1222 // would go to the menu. The next click (i.e. both mouse-press and release
1223 // events) also go to the menu. The menu (and the nested message-loop)
1224 // gets closed after this second release event. The code then resumes from
1225 // here. So make sure that the mouse-button is still down before doing a
1227 if (root_view
&& root_view
->OnMousePressed(*event
) &&
1228 widget_deletion_observer
.IsWidgetAlive() && IsVisible() &&
1229 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1230 is_mouse_button_pressed_
= true;
1231 if (!native_widget_
->HasCapture())
1232 native_widget_
->SetCapture();
1233 event
->SetHandled();
1238 case ui::ET_MOUSE_RELEASED
:
1239 last_mouse_event_was_move_
= false;
1240 is_mouse_button_pressed_
= false;
1241 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1242 if (auto_release_capture_
&& native_widget_
->HasCapture()) {
1243 base::AutoReset
<bool> resetter(&ignore_capture_loss_
, true);
1244 native_widget_
->ReleaseCapture();
1247 root_view
->OnMouseReleased(*event
);
1248 if ((event
->flags() & ui::EF_IS_NON_CLIENT
) == 0)
1249 event
->SetHandled();
1252 case ui::ET_MOUSE_MOVED
:
1253 case ui::ET_MOUSE_DRAGGED
:
1254 if (native_widget_
->HasCapture() && is_mouse_button_pressed_
) {
1255 last_mouse_event_was_move_
= false;
1257 root_view
->OnMouseDragged(*event
);
1258 } else if (!last_mouse_event_was_move_
||
1259 last_mouse_event_position_
!= event
->location()) {
1260 last_mouse_event_position_
= event
->location();
1261 last_mouse_event_was_move_
= true;
1263 root_view
->OnMouseMoved(*event
);
1267 case ui::ET_MOUSE_EXITED
:
1268 last_mouse_event_was_move_
= false;
1270 root_view
->OnMouseExited(*event
);
1273 case ui::ET_MOUSEWHEEL
:
1274 if (root_view
&& root_view
->OnMouseWheel(
1275 static_cast<const ui::MouseWheelEvent
&>(*event
)))
1276 event
->SetHandled();
1284 void Widget::OnMouseCaptureLost() {
1285 if (ignore_capture_loss_
)
1288 View
* root_view
= GetRootView();
1290 root_view
->OnMouseCaptureLost();
1291 is_mouse_button_pressed_
= false;
1294 void Widget::OnScrollEvent(ui::ScrollEvent
* event
) {
1295 ui::ScrollEvent
event_copy(*event
);
1296 SendEventToProcessor(&event_copy
);
1298 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1299 if (!event_copy
.handled() && event_copy
.type() == ui::ET_SCROLL
) {
1300 ui::MouseWheelEvent
wheel(*event
);
1301 OnMouseEvent(&wheel
);
1305 void Widget::OnGestureEvent(ui::GestureEvent
* event
) {
1306 // We explicitly do not capture here. Not capturing enables multiple widgets
1307 // to get tap events at the same time. Views (such as tab dragging) may
1308 // explicitly capture.
1309 SendEventToProcessor(event
);
1312 bool Widget::ExecuteCommand(int command_id
) {
1313 return widget_delegate_
->ExecuteWindowsCommand(command_id
);
1316 InputMethod
* Widget::GetInputMethodDirect() {
1317 return input_method_
.get();
1320 const std::vector
<ui::Layer
*>& Widget::GetRootLayers() {
1321 if (root_layers_dirty_
) {
1322 root_layers_dirty_
= false;
1323 root_layers_
.clear();
1324 BuildRootLayers(GetRootView(), &root_layers_
);
1326 return root_layers_
;
1329 bool Widget::HasHitTestMask() const {
1330 return widget_delegate_
->WidgetHasHitTestMask();
1333 void Widget::GetHitTestMask(gfx::Path
* mask
) const {
1335 widget_delegate_
->GetWidgetHitTestMask(mask
);
1338 Widget
* Widget::AsWidget() {
1342 const Widget
* Widget::AsWidget() const {
1346 bool Widget::SetInitialFocus(ui::WindowShowState show_state
) {
1347 View
* v
= widget_delegate_
->GetInitiallyFocusedView();
1348 if (!focus_on_creation_
|| show_state
== ui::SHOW_STATE_INACTIVE
||
1349 show_state
== ui::SHOW_STATE_MINIMIZED
) {
1350 // If not focusing the window now, tell the focus manager which view to
1351 // focus when the window is restored.
1353 focus_manager_
->SetStoredFocusView(v
);
1361 ////////////////////////////////////////////////////////////////////////////////
1362 // Widget, ui::EventSource implementation:
1363 ui::EventProcessor
* Widget::GetEventProcessor() {
1364 return root_view_
.get();
1367 ////////////////////////////////////////////////////////////////////////////////
1368 // Widget, FocusTraversable implementation:
1370 FocusSearch
* Widget::GetFocusSearch() {
1371 return root_view_
->GetFocusSearch();
1374 FocusTraversable
* Widget::GetFocusTraversableParent() {
1375 // We are a proxy to the root view, so we should be bypassed when traversing
1376 // up and as a result this should not be called.
1381 View
* Widget::GetFocusTraversableParentView() {
1382 // We are a proxy to the root view, so we should be bypassed when traversing
1383 // up and as a result this should not be called.
1388 ////////////////////////////////////////////////////////////////////////////////
1389 // Widget, ui::NativeThemeObserver implementation:
1391 void Widget::OnNativeThemeUpdated(ui::NativeTheme
* observed_theme
) {
1392 DCHECK(observer_manager_
.IsObserving(observed_theme
));
1394 ui::NativeTheme
* current_native_theme
= GetNativeTheme();
1395 if (!observer_manager_
.IsObserving(current_native_theme
)) {
1396 observer_manager_
.RemoveAll();
1397 observer_manager_
.Add(current_native_theme
);
1400 root_view_
->PropagateNativeThemeChanged(current_native_theme
);
1403 ////////////////////////////////////////////////////////////////////////////////
1404 // Widget, protected:
1406 internal::RootView
* Widget::CreateRootView() {
1407 return new internal::RootView(this);
1410 void Widget::DestroyRootView() {
1411 non_client_view_
= NULL
;
1413 // Input method has to be destroyed before focus manager.
1414 input_method_
.reset();
1417 void Widget::OnDragWillStart() {
1420 void Widget::OnDragComplete() {
1423 ////////////////////////////////////////////////////////////////////////////////
1426 void Widget::SetInactiveRenderingDisabled(bool value
) {
1427 if (value
== disable_inactive_rendering_
)
1430 disable_inactive_rendering_
= value
;
1431 if (non_client_view_
)
1432 non_client_view_
->SetInactiveRenderingDisabled(value
);
1435 void Widget::SaveWindowPlacement() {
1436 // The window delegate does the actual saving for us. It seems like (judging
1437 // by go/crash) that in some circumstances we can end up here after
1438 // WM_DESTROY, at which point the window delegate is likely gone. So just
1440 if (!widget_delegate_
)
1443 ui::WindowShowState show_state
= ui::SHOW_STATE_NORMAL
;
1445 native_widget_
->GetWindowPlacement(&bounds
, &show_state
);
1446 widget_delegate_
->SaveWindowPlacement(bounds
, show_state
);
1449 void Widget::SaveWindowPlacementIfInitialized() {
1450 if (native_widget_initialized_
)
1451 SaveWindowPlacement();
1454 void Widget::SetInitialBounds(const gfx::Rect
& bounds
) {
1455 if (!non_client_view_
)
1458 gfx::Rect saved_bounds
;
1459 if (GetSavedWindowPlacement(&saved_bounds
, &saved_show_state_
)) {
1460 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
) {
1461 // If we're going to maximize, wait until Show is invoked to set the
1462 // bounds. That way we avoid a noticeable resize.
1463 initial_restored_bounds_
= saved_bounds
;
1464 } else if (!saved_bounds
.IsEmpty()) {
1465 // If the saved bounds are valid, use them.
1466 SetBounds(saved_bounds
);
1469 if (bounds
.IsEmpty()) {
1470 // No initial bounds supplied, so size the window to its content and
1471 // center over its parent.
1472 native_widget_
->CenterWindow(non_client_view_
->GetPreferredSize());
1474 // Use the supplied initial bounds.
1475 SetBoundsConstrained(bounds
);
1480 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect
& bounds
) {
1481 if (bounds
.IsEmpty()) {
1482 View
* contents_view
= GetContentsView();
1483 DCHECK(contents_view
);
1484 // No initial bounds supplied, so size the window to its content and
1485 // center over its parent if preferred size is provided.
1486 gfx::Size size
= contents_view
->GetPreferredSize();
1487 if (!size
.IsEmpty())
1488 native_widget_
->CenterWindow(size
);
1490 // Use the supplied initial bounds.
1491 SetBoundsConstrained(bounds
);
1495 bool Widget::GetSavedWindowPlacement(gfx::Rect
* bounds
,
1496 ui::WindowShowState
* show_state
) {
1497 // First we obtain the window's saved show-style and store it. We need to do
1498 // this here, rather than in Show() because by the time Show() is called,
1499 // the window's size will have been reset (below) and the saved maximized
1500 // state will have been lost. Sadly there's no way to tell on Windows when
1501 // a window is restored from maximized state, so we can't more accurately
1502 // track maximized state independently of sizing information.
1504 // Restore the window's placement from the controller.
1505 if (widget_delegate_
->GetSavedWindowPlacement(this, bounds
, show_state
)) {
1506 if (!widget_delegate_
->ShouldRestoreWindowSize()) {
1507 bounds
->set_size(non_client_view_
->GetPreferredSize());
1509 gfx::Size minimum_size
= GetMinimumSize();
1510 // Make sure the bounds are at least the minimum size.
1511 if (bounds
->width() < minimum_size
.width())
1512 bounds
->set_width(minimum_size
.width());
1514 if (bounds
->height() < minimum_size
.height())
1515 bounds
->set_height(minimum_size
.height());
1522 scoped_ptr
<InputMethod
> Widget::CreateInputMethod() {
1523 scoped_ptr
<InputMethod
> input_method(native_widget_
->CreateInputMethod());
1524 if (input_method
.get())
1525 input_method
->Init(this);
1526 return input_method
.Pass();
1529 void Widget::ReplaceInputMethod(InputMethod
* input_method
) {
1530 input_method_
.reset(input_method
);
1531 input_method
->SetDelegate(native_widget_
->GetInputMethodDelegate());
1532 input_method
->Init(this);
1535 namespace internal
{
1537 ////////////////////////////////////////////////////////////////////////////////
1538 // internal::NativeWidgetPrivate, NativeWidget implementation:
1540 internal::NativeWidgetPrivate
* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1544 } // namespace internal
1545 } // namespace views