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 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 observers_
.AddObserver(observer
);
395 void Widget::RemoveObserver(WidgetObserver
* observer
) {
396 observers_
.RemoveObserver(observer
);
399 bool Widget::HasObserver(const WidgetObserver
* observer
) const {
400 return observers_
.HasObserver(observer
);
403 void Widget::AddRemovalsObserver(WidgetRemovalsObserver
* observer
) {
404 removals_observers_
.AddObserver(observer
);
407 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver
* observer
) {
408 removals_observers_
.RemoveObserver(observer
);
411 bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver
* observer
) const {
412 return removals_observers_
.HasObserver(observer
);
415 bool Widget::GetAccelerator(int cmd_id
, ui::Accelerator
* accelerator
) const {
419 void Widget::ViewHierarchyChanged(
420 const View::ViewHierarchyChangedDetails
& details
) {
421 if (!details
.is_add
) {
422 if (details
.child
== dragged_view_
)
423 dragged_view_
= NULL
;
424 FocusManager
* focus_manager
= GetFocusManager();
426 focus_manager
->ViewRemoved(details
.child
);
427 ViewStorage::GetInstance()->ViewRemoved(details
.child
);
428 native_widget_
->ViewRemoved(details
.child
);
432 void Widget::NotifyNativeViewHierarchyWillChange() {
433 FocusManager
* focus_manager
= GetFocusManager();
434 // We are being removed from a window hierarchy. Treat this as
435 // the root_view_ being removed.
437 focus_manager
->ViewRemoved(root_view_
.get());
440 void Widget::NotifyNativeViewHierarchyChanged() {
441 root_view_
->NotifyNativeViewHierarchyChanged();
444 void Widget::NotifyWillRemoveView(View
* view
) {
445 FOR_EACH_OBSERVER(WidgetRemovalsObserver
,
447 OnWillRemoveView(this, view
));
450 // Converted methods (see header) ----------------------------------------------
452 Widget
* Widget::GetTopLevelWidget() {
453 return const_cast<Widget
*>(
454 static_cast<const Widget
*>(this)->GetTopLevelWidget());
457 const Widget
* Widget::GetTopLevelWidget() const {
458 // GetTopLevelNativeWidget doesn't work during destruction because
459 // property is gone after gobject gets deleted. Short circuit here
460 // for toplevel so that InputMethod can remove itself from
462 return is_top_level() ? this : native_widget_
->GetTopLevelWidget();
465 void Widget::SetContentsView(View
* view
) {
466 // Do not SetContentsView() again if it is already set to the same view.
467 if (view
== GetContentsView())
469 root_view_
->SetContentsView(view
);
470 if (non_client_view_
!= view
) {
471 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
472 // was true when the widget was initialized. Creating widgets with non
473 // client views and then setting the contents view can cause subtle
474 // problems on Windows, where the native widget thinks there is still a
475 // |non_client_view_|. If you get this error, either use a different type
476 // when initializing the widget, or don't call SetContentsView().
477 DCHECK(!non_client_view_
);
478 non_client_view_
= NULL
;
482 View
* Widget::GetContentsView() {
483 return root_view_
->GetContentsView();
486 gfx::Rect
Widget::GetWindowBoundsInScreen() const {
487 return native_widget_
->GetWindowBoundsInScreen();
490 gfx::Rect
Widget::GetClientAreaBoundsInScreen() const {
491 return native_widget_
->GetClientAreaBoundsInScreen();
494 gfx::Rect
Widget::GetRestoredBounds() const {
495 return native_widget_
->GetRestoredBounds();
498 void Widget::SetBounds(const gfx::Rect
& bounds
) {
499 native_widget_
->SetBounds(bounds
);
502 void Widget::SetSize(const gfx::Size
& size
) {
503 native_widget_
->SetSize(size
);
506 void Widget::CenterWindow(const gfx::Size
& size
) {
507 native_widget_
->CenterWindow(size
);
510 void Widget::SetBoundsConstrained(const gfx::Rect
& bounds
) {
511 gfx::Rect work_area
=
512 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
513 bounds
.origin()).work_area();
514 if (work_area
.IsEmpty()) {
517 // Inset the work area slightly.
518 work_area
.Inset(10, 10, 10, 10);
519 work_area
.AdjustToFit(bounds
);
520 SetBounds(work_area
);
524 void Widget::SetVisibilityChangedAnimationsEnabled(bool value
) {
525 native_widget_
->SetVisibilityChangedAnimationsEnabled(value
);
528 void Widget::SetVisibilityAnimationDuration(const base::TimeDelta
& duration
) {
529 native_widget_
->SetVisibilityAnimationDuration(duration
);
532 void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition
) {
533 native_widget_
->SetVisibilityAnimationTransition(transition
);
536 Widget::MoveLoopResult
Widget::RunMoveLoop(
537 const gfx::Vector2d
& drag_offset
,
538 MoveLoopSource source
,
539 MoveLoopEscapeBehavior escape_behavior
) {
540 return native_widget_
->RunMoveLoop(drag_offset
, source
, escape_behavior
);
543 void Widget::EndMoveLoop() {
544 native_widget_
->EndMoveLoop();
547 void Widget::StackAboveWidget(Widget
* widget
) {
548 native_widget_
->StackAbove(widget
->GetNativeView());
551 void Widget::StackAbove(gfx::NativeView native_view
) {
552 native_widget_
->StackAbove(native_view
);
555 void Widget::StackAtTop() {
556 native_widget_
->StackAtTop();
559 void Widget::StackBelow(gfx::NativeView native_view
) {
560 native_widget_
->StackBelow(native_view
);
563 void Widget::SetShape(gfx::NativeRegion shape
) {
564 native_widget_
->SetShape(shape
);
567 void Widget::Close() {
568 if (widget_closed_
) {
569 // It appears we can hit this code path if you close a modal dialog then
570 // close the last browser before the destructor is hit, which triggers
571 // invoking Close again.
575 bool can_close
= true;
576 if (non_client_view_
)
577 can_close
= non_client_view_
->CanClose();
579 SaveWindowPlacement();
581 // During tear-down the top-level focus manager becomes unavailable to
582 // GTK tabbed panes and their children, so normal deregistration via
583 // |FormManager::ViewRemoved()| calls are fouled. We clear focus here
584 // to avoid these redundant steps and to avoid accessing deleted views
585 // that may have been in focus.
586 if (is_top_level() && focus_manager_
.get())
587 focus_manager_
->SetFocusedView(NULL
);
589 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
590 native_widget_
->Close();
591 widget_closed_
= true;
595 void Widget::CloseNow() {
596 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
597 native_widget_
->CloseNow();
600 bool Widget::IsClosed() const {
601 return widget_closed_
;
604 void Widget::Show() {
605 const ui::Layer
* layer
= GetLayer();
606 TRACE_EVENT1("views", "Widget::Show", "layer",
607 layer
? layer
->name() : "none");
608 if (non_client_view_
) {
609 // While initializing, the kiosk mode will go to full screen before the
610 // widget gets shown. In that case we stay in full screen mode, regardless
611 // of the |saved_show_state_| member.
612 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
613 !initial_restored_bounds_
.IsEmpty() &&
615 native_widget_
->ShowMaximizedWithBounds(initial_restored_bounds_
);
617 native_widget_
->ShowWithWindowState(
618 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN
: saved_show_state_
);
620 // |saved_show_state_| only applies the first time the window is shown.
621 // If we don't reset the value the window may be shown maximized every time
622 // it is subsequently shown after being hidden.
623 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
626 ? native_widget_
->Show()
627 : native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
631 void Widget::Hide() {
632 native_widget_
->Hide();
635 void Widget::ShowInactive() {
636 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
637 // call SetBounds()with the restored bounds to set the correct size. This
638 // normally should not happen, but if it does we should avoid showing unsized
640 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
641 !initial_restored_bounds_
.IsEmpty()) {
642 SetBounds(initial_restored_bounds_
);
643 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
645 native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
648 void Widget::Activate() {
649 native_widget_
->Activate();
652 void Widget::Deactivate() {
653 native_widget_
->Deactivate();
656 bool Widget::IsActive() const {
657 return native_widget_
->IsActive();
660 void Widget::DisableInactiveRendering() {
661 SetInactiveRenderingDisabled(true);
664 void Widget::SetAlwaysOnTop(bool on_top
) {
665 native_widget_
->SetAlwaysOnTop(on_top
);
668 bool Widget::IsAlwaysOnTop() const {
669 return native_widget_
->IsAlwaysOnTop();
672 void Widget::SetVisibleOnAllWorkspaces(bool always_visible
) {
673 native_widget_
->SetVisibleOnAllWorkspaces(always_visible
);
676 void Widget::Maximize() {
677 native_widget_
->Maximize();
680 void Widget::Minimize() {
681 native_widget_
->Minimize();
684 void Widget::Restore() {
685 native_widget_
->Restore();
688 bool Widget::IsMaximized() const {
689 return native_widget_
->IsMaximized();
692 bool Widget::IsMinimized() const {
693 return native_widget_
->IsMinimized();
696 void Widget::SetFullscreen(bool fullscreen
) {
697 if (IsFullscreen() == fullscreen
)
700 native_widget_
->SetFullscreen(fullscreen
);
702 if (non_client_view_
)
703 non_client_view_
->Layout();
706 bool Widget::IsFullscreen() const {
707 return native_widget_
->IsFullscreen();
710 void Widget::SetOpacity(unsigned char opacity
) {
711 native_widget_
->SetOpacity(opacity
);
714 void Widget::SetUseDragFrame(bool use_drag_frame
) {
715 native_widget_
->SetUseDragFrame(use_drag_frame
);
718 void Widget::FlashFrame(bool flash
) {
719 native_widget_
->FlashFrame(flash
);
722 View
* Widget::GetRootView() {
723 return root_view_
.get();
726 const View
* Widget::GetRootView() const {
727 return root_view_
.get();
730 bool Widget::IsVisible() const {
731 return native_widget_
->IsVisible();
734 ui::ThemeProvider
* Widget::GetThemeProvider() const {
735 const Widget
* root_widget
= GetTopLevelWidget();
736 if (root_widget
&& root_widget
!= this) {
737 // Attempt to get the theme provider, and fall back to the default theme
738 // provider if not found.
739 ui::ThemeProvider
* provider
= root_widget
->GetThemeProvider();
743 provider
= root_widget
->default_theme_provider_
.get();
747 return default_theme_provider_
.get();
750 const ui::NativeTheme
* Widget::GetNativeTheme() const {
751 return native_widget_
->GetNativeTheme();
754 FocusManager
* Widget::GetFocusManager() {
755 Widget
* toplevel_widget
= GetTopLevelWidget();
756 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
759 const FocusManager
* Widget::GetFocusManager() const {
760 const Widget
* toplevel_widget
= GetTopLevelWidget();
761 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
764 ui::TextInputClient
* Widget::GetFocusedTextInputClient() {
765 FocusManager
* focus_manager
= GetFocusManager();
766 View
* view
= focus_manager
? focus_manager
->GetFocusedView() : nullptr;
767 return view
? view
->GetTextInputClient() : nullptr;
770 InputMethod
* Widget::GetInputMethod() {
771 return const_cast<InputMethod
*>(
772 const_cast<const Widget
*>(this)->GetInputMethod());
775 const InputMethod
* Widget::GetInputMethod() const {
776 if (is_top_level()) {
777 if (!input_method_
.get())
778 input_method_
= const_cast<Widget
*>(this)->CreateInputMethod().Pass();
779 return input_method_
.get();
781 const Widget
* toplevel
= GetTopLevelWidget();
782 // If GetTopLevelWidget() returns itself which is not toplevel,
783 // the widget is detached from toplevel widget.
784 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
785 // if there is no toplevel. We probably need to add GetTopMostWidget()
786 // to replace some use cases.
787 return (toplevel
&& toplevel
!= this) ? toplevel
->GetInputMethod() : NULL
;
791 ui::InputMethod
* Widget::GetHostInputMethod() {
792 return native_widget_private()->GetHostInputMethod();
795 void Widget::RunShellDrag(View
* view
,
796 const ui::OSExchangeData
& data
,
797 const gfx::Point
& location
,
799 ui::DragDropTypes::DragEventSource source
) {
800 dragged_view_
= view
;
803 WidgetDeletionObserver
widget_deletion_observer(this);
804 native_widget_
->RunShellDrag(view
, data
, location
, operation
, source
);
806 // The widget may be destroyed during the drag operation.
807 if (!widget_deletion_observer
.IsWidgetAlive())
810 // If the view is removed during the drag operation, dragged_view_ is set to
812 if (view
&& dragged_view_
== view
) {
813 dragged_view_
= NULL
;
819 void Widget::SchedulePaintInRect(const gfx::Rect
& rect
) {
820 native_widget_
->SchedulePaintInRect(rect
);
823 void Widget::SetCursor(gfx::NativeCursor cursor
) {
824 native_widget_
->SetCursor(cursor
);
827 bool Widget::IsMouseEventsEnabled() const {
828 return native_widget_
->IsMouseEventsEnabled();
831 void Widget::SetNativeWindowProperty(const char* name
, void* value
) {
832 native_widget_
->SetNativeWindowProperty(name
, value
);
835 void* Widget::GetNativeWindowProperty(const char* name
) const {
836 return native_widget_
->GetNativeWindowProperty(name
);
839 void Widget::UpdateWindowTitle() {
840 if (!non_client_view_
)
843 // Update the native frame's text. We do this regardless of whether or not
844 // the native frame is being used, since this also updates the taskbar, etc.
845 base::string16 window_title
= widget_delegate_
->GetWindowTitle();
846 base::i18n::AdjustStringForLocaleDirection(&window_title
);
847 if (!native_widget_
->SetWindowTitle(window_title
))
849 non_client_view_
->UpdateWindowTitle();
851 // If the non-client view is rendering its own title, it'll need to relayout
852 // now and to get a paint update later on.
853 non_client_view_
->Layout();
856 void Widget::UpdateWindowIcon() {
857 if (non_client_view_
)
858 non_client_view_
->UpdateWindowIcon();
859 native_widget_
->SetWindowIcons(widget_delegate_
->GetWindowIcon(),
860 widget_delegate_
->GetWindowAppIcon());
863 FocusTraversable
* Widget::GetFocusTraversable() {
864 return static_cast<internal::RootView
*>(root_view_
.get());
867 void Widget::ThemeChanged() {
868 root_view_
->ThemeChanged();
871 void Widget::LocaleChanged() {
872 root_view_
->LocaleChanged();
875 void Widget::DeviceScaleFactorChanged(float device_scale_factor
) {
876 root_view_
->DeviceScaleFactorChanged(device_scale_factor
);
879 void Widget::SetFocusTraversableParent(FocusTraversable
* parent
) {
880 root_view_
->SetFocusTraversableParent(parent
);
883 void Widget::SetFocusTraversableParentView(View
* parent_view
) {
884 root_view_
->SetFocusTraversableParentView(parent_view
);
887 void Widget::ClearNativeFocus() {
888 native_widget_
->ClearNativeFocus();
891 NonClientFrameView
* Widget::CreateNonClientFrameView() {
892 NonClientFrameView
* frame_view
=
893 widget_delegate_
->CreateNonClientFrameView(this);
895 frame_view
= native_widget_
->CreateNonClientFrameView();
896 if (!frame_view
&& ViewsDelegate::views_delegate
) {
898 ViewsDelegate::views_delegate
->CreateDefaultNonClientFrameView(this);
903 CustomFrameView
* custom_frame_view
= new CustomFrameView
;
904 custom_frame_view
->Init(this);
905 return custom_frame_view
;
908 bool Widget::ShouldUseNativeFrame() const {
909 if (frame_type_
!= FRAME_TYPE_DEFAULT
)
910 return frame_type_
== FRAME_TYPE_FORCE_NATIVE
;
911 return native_widget_
->ShouldUseNativeFrame();
914 bool Widget::ShouldWindowContentsBeTransparent() const {
915 return native_widget_
->ShouldWindowContentsBeTransparent();
918 void Widget::DebugToggleFrameType() {
919 if (frame_type_
== FRAME_TYPE_DEFAULT
) {
920 frame_type_
= ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM
:
921 FRAME_TYPE_FORCE_NATIVE
;
923 frame_type_
= frame_type_
== FRAME_TYPE_FORCE_CUSTOM
?
924 FRAME_TYPE_FORCE_NATIVE
: FRAME_TYPE_FORCE_CUSTOM
;
929 void Widget::FrameTypeChanged() {
930 native_widget_
->FrameTypeChanged();
933 const ui::Compositor
* Widget::GetCompositor() const {
934 return native_widget_
->GetCompositor();
937 const ui::Layer
* Widget::GetLayer() const {
938 return native_widget_
->GetLayer();
941 void Widget::ReorderNativeViews() {
942 native_widget_
->ReorderNativeViews();
945 void Widget::UpdateRootLayers() {
946 // Calculate the layers requires traversing the tree, and since nearly any
947 // mutation of the tree can trigger this call we delay until absolutely
949 root_layers_dirty_
= true;
952 const NativeWidget
* Widget::native_widget() const {
953 return native_widget_
;
956 NativeWidget
* Widget::native_widget() {
957 return native_widget_
;
960 void Widget::SetCapture(View
* view
) {
961 if (!native_widget_
->HasCapture()) {
962 native_widget_
->SetCapture();
964 // Early return if setting capture was unsuccessful.
965 if (!native_widget_
->HasCapture())
969 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
970 is_mouse_button_pressed_
= true;
971 root_view_
->SetMouseHandler(view
);
974 void Widget::ReleaseCapture() {
975 if (native_widget_
->HasCapture())
976 native_widget_
->ReleaseCapture();
979 bool Widget::HasCapture() {
980 return native_widget_
->HasCapture();
983 TooltipManager
* Widget::GetTooltipManager() {
984 return native_widget_
->GetTooltipManager();
987 const TooltipManager
* Widget::GetTooltipManager() const {
988 return native_widget_
->GetTooltipManager();
991 gfx::Rect
Widget::GetWorkAreaBoundsInScreen() const {
992 return native_widget_
->GetWorkAreaBoundsInScreen();
995 void Widget::SynthesizeMouseMoveEvent() {
996 last_mouse_event_was_move_
= false;
997 ui::MouseEvent
mouse_event(ui::ET_MOUSE_MOVED
, last_mouse_event_position_
,
998 last_mouse_event_position_
, ui::EventTimeForNow(),
999 ui::EF_IS_SYNTHESIZED
, 0);
1000 root_view_
->OnMouseMoved(mouse_event
);
1003 void Widget::OnRootViewLayout() {
1004 native_widget_
->OnRootViewLayout();
1007 bool Widget::IsTranslucentWindowOpacitySupported() const {
1008 return native_widget_
->IsTranslucentWindowOpacitySupported();
1011 void Widget::OnSizeConstraintsChanged() {
1012 native_widget_
->OnSizeConstraintsChanged();
1013 non_client_view_
->SizeConstraintsChanged();
1016 void Widget::OnOwnerClosing() {
1019 ////////////////////////////////////////////////////////////////////////////////
1020 // Widget, NativeWidgetDelegate implementation:
1022 bool Widget::IsModal() const {
1023 return widget_delegate_
->GetModalType() != ui::MODAL_TYPE_NONE
;
1026 bool Widget::IsDialogBox() const {
1027 return !!widget_delegate_
->AsDialogDelegate();
1030 bool Widget::CanActivate() const {
1031 return widget_delegate_
->CanActivate();
1034 bool Widget::IsInactiveRenderingDisabled() const {
1035 return disable_inactive_rendering_
;
1038 void Widget::EnableInactiveRendering() {
1039 SetInactiveRenderingDisabled(false);
1042 void Widget::OnNativeWidgetActivationChanged(bool active
) {
1043 // On windows we may end up here before we've completed initialization (from
1044 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1045 // the Widget and will crash attempting to access it.
1046 if (!active
&& native_widget_initialized_
)
1047 SaveWindowPlacement();
1049 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1050 OnWidgetActivationChanged(this, active
));
1052 // During window creation, the widget gets focused without activation, and in
1053 // that case, the focus manager cannot set the appropriate text input client
1054 // because the widget is not active. Thus we have to notify the focus manager
1055 // not only when the focus changes but also when the widget gets activated.
1056 // See crbug.com/377479 for details.
1057 views::FocusManager
* focus_manager
= GetFocusManager();
1058 if (focus_manager
) {
1060 focus_manager
->FocusTextInputClient(focus_manager
->GetFocusedView());
1062 focus_manager
->BlurTextInputClient(focus_manager
->GetFocusedView());
1065 if (IsVisible() && non_client_view())
1066 non_client_view()->frame_view()->SchedulePaint();
1069 void Widget::OnNativeFocus() {
1070 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(GetNativeView());
1073 void Widget::OnNativeBlur() {
1074 WidgetFocusManager::GetInstance()->OnNativeFocusChanged(nullptr);
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