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/gfx/image/image_skia.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/views/controls/menu/menu_controller.h"
22 #include "ui/views/focus/focus_manager.h"
23 #include "ui/views/focus/focus_manager_factory.h"
24 #include "ui/views/focus/view_storage.h"
25 #include "ui/views/focus/widget_focus_manager.h"
26 #include "ui/views/ime/input_method.h"
27 #include "ui/views/views_delegate.h"
28 #include "ui/views/widget/native_widget_private.h"
29 #include "ui/views/widget/root_view.h"
30 #include "ui/views/widget/tooltip_manager.h"
31 #include "ui/views/widget/widget_delegate.h"
32 #include "ui/views/widget/widget_deletion_observer.h"
33 #include "ui/views/widget/widget_observer.h"
34 #include "ui/views/widget/widget_removals_observer.h"
35 #include "ui/views/window/custom_frame_view.h"
36 #include "ui/views/window/dialog_delegate.h"
42 // If |view| has a layer the layer is added to |layers|. Else this recurses
43 // through the children. This is used to build a list of the layers created by
44 // views that are direct children of the Widgets layer.
45 void BuildRootLayers(View
* view
, std::vector
<ui::Layer
*>* layers
) {
47 layers
->push_back(view
->layer());
49 for (int i
= 0; i
< view
->child_count(); ++i
)
50 BuildRootLayers(view
->child_at(i
), layers
);
54 // Create a native widget implementation.
55 // First, use the supplied one if non-NULL.
56 // Finally, make a default one.
57 NativeWidget
* CreateNativeWidget(NativeWidget
* native_widget
,
58 internal::NativeWidgetDelegate
* delegate
) {
61 internal::NativeWidgetPrivate::CreateNativeWidget(delegate
);
68 // A default implementation of WidgetDelegate, used by Widget when no
69 // WidgetDelegate is supplied.
70 class DefaultWidgetDelegate
: public WidgetDelegate
{
72 explicit DefaultWidgetDelegate(Widget
* widget
) : widget_(widget
) {
74 ~DefaultWidgetDelegate() override
{}
76 // Overridden from WidgetDelegate:
77 void DeleteDelegate() override
{ delete this; }
78 Widget
* GetWidget() override
{ return widget_
; }
79 const Widget
* GetWidget() const override
{ return widget_
; }
80 bool ShouldAdvanceFocusToTopLevelWidget() const override
{
81 // In most situations where a Widget is used without a delegate the Widget
82 // is used as a container, so that we want focus to advance to the top-level
83 // widget. A good example of this is the find bar.
90 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate
);
93 ////////////////////////////////////////////////////////////////////////////////
94 // Widget, InitParams:
96 Widget::InitParams::InitParams()
100 opacity(INFER_OPACITY
),
102 activatable(ACTIVATABLE_DEFAULT
),
104 visible_on_all_workspaces(false),
105 ownership(NATIVE_WIDGET_OWNS_WIDGET
),
106 mirror_origin_in_rtl(false),
107 shadow_type(SHADOW_TYPE_DEFAULT
),
108 remove_standard_frame(false),
109 use_system_default_icon(false),
110 show_state(ui::SHOW_STATE_DEFAULT
),
113 desktop_window_tree_host(NULL
),
114 layer_type(aura::WINDOW_LAYER_TEXTURED
),
116 force_show_in_taskbar(false) {
119 Widget::InitParams::InitParams(Type type
)
123 opacity(INFER_OPACITY
),
125 activatable(ACTIVATABLE_DEFAULT
),
126 keep_on_top(type
== TYPE_MENU
|| type
== TYPE_DRAG
),
127 visible_on_all_workspaces(false),
128 ownership(NATIVE_WIDGET_OWNS_WIDGET
),
129 mirror_origin_in_rtl(false),
130 shadow_type(SHADOW_TYPE_DEFAULT
),
131 remove_standard_frame(false),
132 use_system_default_icon(false),
133 show_state(ui::SHOW_STATE_DEFAULT
),
136 desktop_window_tree_host(NULL
),
137 layer_type(aura::WINDOW_LAYER_TEXTURED
),
139 force_show_in_taskbar(false) {
142 Widget::InitParams::~InitParams() {
145 ////////////////////////////////////////////////////////////////////////////////
149 : native_widget_(NULL
),
150 widget_delegate_(NULL
),
151 non_client_view_(NULL
),
153 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET
),
154 is_secondary_widget_(true),
155 frame_type_(FRAME_TYPE_DEFAULT
),
156 disable_inactive_rendering_(false),
157 widget_closed_(false),
158 saved_show_state_(ui::SHOW_STATE_DEFAULT
),
159 focus_on_creation_(true),
160 is_top_level_(false),
161 native_widget_initialized_(false),
162 native_widget_destroyed_(false),
163 is_mouse_button_pressed_(false),
164 ignore_capture_loss_(false),
165 last_mouse_event_was_move_(false),
166 auto_release_capture_(true),
167 root_layers_dirty_(false),
168 movement_disabled_(false),
169 observer_manager_(this) {
174 if (ownership_
== InitParams::WIDGET_OWNS_NATIVE_WIDGET
) {
175 delete native_widget_
;
177 DCHECK(native_widget_destroyed_
)
178 << "Destroying a widget with a live native widget. "
179 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
184 Widget
* Widget::CreateWindow(WidgetDelegate
* delegate
) {
185 return CreateWindowWithBounds(delegate
, gfx::Rect());
189 Widget
* Widget::CreateWindowWithBounds(WidgetDelegate
* delegate
,
190 const gfx::Rect
& bounds
) {
191 Widget
* widget
= new Widget
;
192 Widget::InitParams params
;
193 params
.bounds
= bounds
;
194 params
.delegate
= delegate
;
195 widget
->Init(params
);
200 Widget
* Widget::CreateWindowWithParent(WidgetDelegate
* delegate
,
201 gfx::NativeView parent
) {
202 return CreateWindowWithParentAndBounds(delegate
, parent
, gfx::Rect());
206 Widget
* Widget::CreateWindowWithParentAndBounds(WidgetDelegate
* delegate
,
207 gfx::NativeView parent
,
208 const gfx::Rect
& bounds
) {
209 Widget
* widget
= new Widget
;
210 Widget::InitParams params
;
211 params
.delegate
= delegate
;
212 params
.parent
= parent
;
213 params
.bounds
= bounds
;
214 widget
->Init(params
);
219 Widget
* Widget::CreateWindowWithContext(WidgetDelegate
* delegate
,
220 gfx::NativeWindow context
) {
221 return CreateWindowWithContextAndBounds(delegate
, context
, gfx::Rect());
225 Widget
* Widget::CreateWindowWithContextAndBounds(WidgetDelegate
* delegate
,
226 gfx::NativeWindow context
,
227 const gfx::Rect
& bounds
) {
228 Widget
* widget
= new Widget
;
229 Widget::InitParams params
;
230 params
.delegate
= delegate
;
231 params
.context
= context
;
232 params
.bounds
= bounds
;
233 widget
->Init(params
);
238 Widget
* Widget::GetWidgetForNativeView(gfx::NativeView native_view
) {
239 internal::NativeWidgetPrivate
* native_widget
=
240 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view
);
241 return native_widget
? native_widget
->GetWidget() : NULL
;
245 Widget
* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window
) {
246 internal::NativeWidgetPrivate
* native_widget
=
247 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
249 return native_widget
? native_widget
->GetWidget() : NULL
;
253 Widget
* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view
) {
254 internal::NativeWidgetPrivate
* native_widget
=
255 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view
);
256 return native_widget
? native_widget
->GetWidget() : NULL
;
261 void Widget::GetAllChildWidgets(gfx::NativeView native_view
,
263 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view
, children
);
267 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view
,
269 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view
, owned
);
273 void Widget::ReparentNativeView(gfx::NativeView native_view
,
274 gfx::NativeView new_parent
) {
275 internal::NativeWidgetPrivate::ReparentNativeView(native_view
, new_parent
);
279 int Widget::GetLocalizedContentsWidth(int col_resource_id
) {
280 return ui::GetLocalizedContentsWidthForFont(col_resource_id
,
281 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
));
285 int Widget::GetLocalizedContentsHeight(int row_resource_id
) {
286 return ui::GetLocalizedContentsHeightForFont(row_resource_id
,
287 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
));
291 gfx::Size
Widget::GetLocalizedContentsSize(int col_resource_id
,
292 int row_resource_id
) {
293 return gfx::Size(GetLocalizedContentsWidth(col_resource_id
),
294 GetLocalizedContentsHeight(row_resource_id
));
298 bool Widget::RequiresNonClientView(InitParams::Type type
) {
299 return type
== InitParams::TYPE_WINDOW
||
300 type
== InitParams::TYPE_PANEL
||
301 type
== InitParams::TYPE_BUBBLE
;
304 void Widget::Init(const InitParams
& in_params
) {
305 TRACE_EVENT0("views", "Widget::Init");
306 InitParams params
= in_params
;
308 params
.child
|= (params
.type
== InitParams::TYPE_CONTROL
);
309 is_top_level_
= !params
.child
;
311 if (params
.opacity
== views::Widget::InitParams::INFER_OPACITY
&&
312 params
.type
!= views::Widget::InitParams::TYPE_WINDOW
&&
313 params
.type
!= views::Widget::InitParams::TYPE_PANEL
)
314 params
.opacity
= views::Widget::InitParams::OPAQUE_WINDOW
;
316 if (ViewsDelegate::views_delegate
)
317 ViewsDelegate::views_delegate
->OnBeforeWidgetInit(¶ms
, this);
319 if (params
.opacity
== views::Widget::InitParams::INFER_OPACITY
)
320 params
.opacity
= views::Widget::InitParams::OPAQUE_WINDOW
;
322 bool can_activate
= false;
323 if (params
.activatable
!= InitParams::ACTIVATABLE_DEFAULT
) {
324 can_activate
= (params
.activatable
== InitParams::ACTIVATABLE_YES
);
325 } else if (params
.type
!= InitParams::TYPE_CONTROL
&&
326 params
.type
!= InitParams::TYPE_POPUP
&&
327 params
.type
!= InitParams::TYPE_MENU
&&
328 params
.type
!= InitParams::TYPE_TOOLTIP
&&
329 params
.type
!= InitParams::TYPE_DRAG
) {
331 params
.activatable
= InitParams::ACTIVATABLE_YES
;
333 can_activate
= false;
334 params
.activatable
= InitParams::ACTIVATABLE_NO
;
337 widget_delegate_
= params
.delegate
?
338 params
.delegate
: new DefaultWidgetDelegate(this);
339 widget_delegate_
->set_can_activate(can_activate
);
341 ownership_
= params
.ownership
;
342 native_widget_
= CreateNativeWidget(params
.native_widget
, this)->
343 AsNativeWidgetPrivate();
344 root_view_
.reset(CreateRootView());
345 default_theme_provider_
.reset(new ui::DefaultThemeProvider
);
346 if (params
.type
== InitParams::TYPE_MENU
) {
347 is_mouse_button_pressed_
=
348 internal::NativeWidgetPrivate::IsMouseButtonDown();
350 native_widget_
->InitNativeWidget(params
);
351 if (RequiresNonClientView(params
.type
)) {
352 non_client_view_
= new NonClientView
;
353 non_client_view_
->SetFrameView(CreateNonClientFrameView());
354 // Create the ClientView, add it to the NonClientView and add the
355 // NonClientView to the RootView. This will cause everything to be parented.
356 non_client_view_
->set_client_view(widget_delegate_
->CreateClientView(this));
357 non_client_view_
->SetOverlayView(widget_delegate_
->CreateOverlayView());
358 SetContentsView(non_client_view_
);
359 // Initialize the window's title before setting the window's initial bounds;
360 // the frame view's preferred height may depend on the presence of a title.
362 non_client_view_
->ResetWindowControls();
363 SetInitialBounds(params
.bounds
);
364 if (params
.show_state
== ui::SHOW_STATE_MAXIMIZED
)
366 else if (params
.show_state
== ui::SHOW_STATE_MINIMIZED
)
368 } else if (params
.delegate
) {
369 SetContentsView(params
.delegate
->GetContentsView());
370 SetInitialBoundsForFramelessWindow(params
.bounds
);
372 // This must come after SetContentsView() or it might not be able to find
373 // the correct NativeTheme (on Linux). See http://crbug.com/384492
374 observer_manager_
.Add(GetNativeTheme());
375 native_widget_initialized_
= true;
378 // Unconverted methods (see header) --------------------------------------------
380 gfx::NativeView
Widget::GetNativeView() const {
381 return native_widget_
->GetNativeView();
384 gfx::NativeWindow
Widget::GetNativeWindow() const {
385 return native_widget_
->GetNativeWindow();
388 void Widget::AddObserver(WidgetObserver
* observer
) {
389 observers_
.AddObserver(observer
);
392 void Widget::RemoveObserver(WidgetObserver
* observer
) {
393 observers_
.RemoveObserver(observer
);
396 bool Widget::HasObserver(const WidgetObserver
* observer
) const {
397 return observers_
.HasObserver(observer
);
400 void Widget::AddRemovalsObserver(WidgetRemovalsObserver
* observer
) {
401 removals_observers_
.AddObserver(observer
);
404 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver
* observer
) {
405 removals_observers_
.RemoveObserver(observer
);
408 bool Widget::HasRemovalsObserver(const WidgetRemovalsObserver
* observer
) const {
409 return removals_observers_
.HasObserver(observer
);
412 bool Widget::GetAccelerator(int cmd_id
, ui::Accelerator
* accelerator
) const {
416 void Widget::ViewHierarchyChanged(
417 const View::ViewHierarchyChangedDetails
& details
) {
418 if (!details
.is_add
) {
419 if (details
.child
== dragged_view_
)
420 dragged_view_
= NULL
;
421 FocusManager
* focus_manager
= GetFocusManager();
423 focus_manager
->ViewRemoved(details
.child
);
424 ViewStorage::GetInstance()->ViewRemoved(details
.child
);
425 native_widget_
->ViewRemoved(details
.child
);
429 void Widget::NotifyNativeViewHierarchyWillChange() {
430 FocusManager
* focus_manager
= GetFocusManager();
431 // We are being removed from a window hierarchy. Treat this as
432 // the root_view_ being removed.
434 focus_manager
->ViewRemoved(root_view_
.get());
437 void Widget::NotifyNativeViewHierarchyChanged() {
438 root_view_
->NotifyNativeViewHierarchyChanged();
441 void Widget::NotifyWillRemoveView(View
* view
) {
442 FOR_EACH_OBSERVER(WidgetRemovalsObserver
,
444 OnWillRemoveView(this, view
));
447 // Converted methods (see header) ----------------------------------------------
449 Widget
* Widget::GetTopLevelWidget() {
450 return const_cast<Widget
*>(
451 static_cast<const Widget
*>(this)->GetTopLevelWidget());
454 const Widget
* Widget::GetTopLevelWidget() const {
455 // GetTopLevelNativeWidget doesn't work during destruction because
456 // property is gone after gobject gets deleted. Short circuit here
457 // for toplevel so that InputMethod can remove itself from
459 return is_top_level() ? this : native_widget_
->GetTopLevelWidget();
462 void Widget::SetContentsView(View
* view
) {
463 // Do not SetContentsView() again if it is already set to the same view.
464 if (view
== GetContentsView())
466 root_view_
->SetContentsView(view
);
467 if (non_client_view_
!= view
) {
468 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
469 // was true when the widget was initialized. Creating widgets with non
470 // client views and then setting the contents view can cause subtle
471 // problems on Windows, where the native widget thinks there is still a
472 // |non_client_view_|. If you get this error, either use a different type
473 // when initializing the widget, or don't call SetContentsView().
474 DCHECK(!non_client_view_
);
475 non_client_view_
= NULL
;
479 View
* Widget::GetContentsView() {
480 return root_view_
->GetContentsView();
483 gfx::Rect
Widget::GetWindowBoundsInScreen() const {
484 return native_widget_
->GetWindowBoundsInScreen();
487 gfx::Rect
Widget::GetClientAreaBoundsInScreen() const {
488 return native_widget_
->GetClientAreaBoundsInScreen();
491 gfx::Rect
Widget::GetRestoredBounds() const {
492 return native_widget_
->GetRestoredBounds();
495 void Widget::SetBounds(const gfx::Rect
& bounds
) {
496 native_widget_
->SetBounds(bounds
);
499 void Widget::SetSize(const gfx::Size
& size
) {
500 native_widget_
->SetSize(size
);
503 void Widget::CenterWindow(const gfx::Size
& size
) {
504 native_widget_
->CenterWindow(size
);
507 void Widget::SetBoundsConstrained(const gfx::Rect
& bounds
) {
508 gfx::Rect work_area
=
509 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
510 bounds
.origin()).work_area();
511 if (work_area
.IsEmpty()) {
514 // Inset the work area slightly.
515 work_area
.Inset(10, 10, 10, 10);
516 work_area
.AdjustToFit(bounds
);
517 SetBounds(work_area
);
521 void Widget::SetVisibilityChangedAnimationsEnabled(bool value
) {
522 native_widget_
->SetVisibilityChangedAnimationsEnabled(value
);
525 void Widget::SetVisibilityAnimationDuration(const base::TimeDelta
& duration
) {
526 native_widget_
->SetVisibilityAnimationDuration(duration
);
529 void Widget::SetVisibilityAnimationTransition(VisibilityTransition transition
) {
530 native_widget_
->SetVisibilityAnimationTransition(transition
);
533 Widget::MoveLoopResult
Widget::RunMoveLoop(
534 const gfx::Vector2d
& drag_offset
,
535 MoveLoopSource source
,
536 MoveLoopEscapeBehavior escape_behavior
) {
537 return native_widget_
->RunMoveLoop(drag_offset
, source
, escape_behavior
);
540 void Widget::EndMoveLoop() {
541 native_widget_
->EndMoveLoop();
544 void Widget::StackAboveWidget(Widget
* widget
) {
545 native_widget_
->StackAbove(widget
->GetNativeView());
548 void Widget::StackAbove(gfx::NativeView native_view
) {
549 native_widget_
->StackAbove(native_view
);
552 void Widget::StackAtTop() {
553 native_widget_
->StackAtTop();
556 void Widget::StackBelow(gfx::NativeView native_view
) {
557 native_widget_
->StackBelow(native_view
);
560 void Widget::SetShape(gfx::NativeRegion shape
) {
561 native_widget_
->SetShape(shape
);
564 void Widget::Close() {
565 if (widget_closed_
) {
566 // It appears we can hit this code path if you close a modal dialog then
567 // close the last browser before the destructor is hit, which triggers
568 // invoking Close again.
572 bool can_close
= true;
573 if (non_client_view_
)
574 can_close
= non_client_view_
->CanClose();
576 SaveWindowPlacement();
578 // During tear-down the top-level focus manager becomes unavailable to
579 // GTK tabbed panes and their children, so normal deregistration via
580 // |FormManager::ViewRemoved()| calls are fouled. We clear focus here
581 // to avoid these redundant steps and to avoid accessing deleted views
582 // that may have been in focus.
583 if (is_top_level() && focus_manager_
.get())
584 focus_manager_
->SetFocusedView(NULL
);
586 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
587 native_widget_
->Close();
588 widget_closed_
= true;
592 void Widget::CloseNow() {
593 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetClosing(this));
594 native_widget_
->CloseNow();
597 bool Widget::IsClosed() const {
598 return widget_closed_
;
601 void Widget::Show() {
602 const ui::Layer
* layer
= GetLayer();
603 TRACE_EVENT1("views", "Widget::Show", "layer",
604 layer
? layer
->name() : "none");
605 if (non_client_view_
) {
606 // While initializing, the kiosk mode will go to full screen before the
607 // widget gets shown. In that case we stay in full screen mode, regardless
608 // of the |saved_show_state_| member.
609 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
610 !initial_restored_bounds_
.IsEmpty() &&
612 native_widget_
->ShowMaximizedWithBounds(initial_restored_bounds_
);
614 native_widget_
->ShowWithWindowState(
615 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN
: saved_show_state_
);
617 // |saved_show_state_| only applies the first time the window is shown.
618 // If we don't reset the value the window may be shown maximized every time
619 // it is subsequently shown after being hidden.
620 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
623 ? native_widget_
->Show()
624 : native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
628 void Widget::Hide() {
629 native_widget_
->Hide();
632 void Widget::ShowInactive() {
633 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
634 // call SetBounds()with the restored bounds to set the correct size. This
635 // normally should not happen, but if it does we should avoid showing unsized
637 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
638 !initial_restored_bounds_
.IsEmpty()) {
639 SetBounds(initial_restored_bounds_
);
640 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
642 native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
645 void Widget::Activate() {
646 native_widget_
->Activate();
649 void Widget::Deactivate() {
650 native_widget_
->Deactivate();
653 bool Widget::IsActive() const {
654 return native_widget_
->IsActive();
657 void Widget::DisableInactiveRendering() {
658 SetInactiveRenderingDisabled(true);
661 void Widget::SetAlwaysOnTop(bool on_top
) {
662 native_widget_
->SetAlwaysOnTop(on_top
);
665 bool Widget::IsAlwaysOnTop() const {
666 return native_widget_
->IsAlwaysOnTop();
669 void Widget::SetVisibleOnAllWorkspaces(bool always_visible
) {
670 native_widget_
->SetVisibleOnAllWorkspaces(always_visible
);
673 void Widget::Maximize() {
674 native_widget_
->Maximize();
677 void Widget::Minimize() {
678 native_widget_
->Minimize();
681 void Widget::Restore() {
682 native_widget_
->Restore();
685 bool Widget::IsMaximized() const {
686 return native_widget_
->IsMaximized();
689 bool Widget::IsMinimized() const {
690 return native_widget_
->IsMinimized();
693 void Widget::SetFullscreen(bool fullscreen
) {
694 if (IsFullscreen() == fullscreen
)
697 native_widget_
->SetFullscreen(fullscreen
);
699 if (non_client_view_
)
700 non_client_view_
->Layout();
703 bool Widget::IsFullscreen() const {
704 return native_widget_
->IsFullscreen();
707 void Widget::SetOpacity(unsigned char opacity
) {
708 native_widget_
->SetOpacity(opacity
);
711 void Widget::SetUseDragFrame(bool use_drag_frame
) {
712 native_widget_
->SetUseDragFrame(use_drag_frame
);
715 void Widget::FlashFrame(bool flash
) {
716 native_widget_
->FlashFrame(flash
);
719 View
* Widget::GetRootView() {
720 return root_view_
.get();
723 const View
* Widget::GetRootView() const {
724 return root_view_
.get();
727 bool Widget::IsVisible() const {
728 return native_widget_
->IsVisible();
731 ui::ThemeProvider
* Widget::GetThemeProvider() const {
732 const Widget
* root_widget
= GetTopLevelWidget();
733 if (root_widget
&& root_widget
!= this) {
734 // Attempt to get the theme provider, and fall back to the default theme
735 // provider if not found.
736 ui::ThemeProvider
* provider
= root_widget
->GetThemeProvider();
740 provider
= root_widget
->default_theme_provider_
.get();
744 return default_theme_provider_
.get();
747 const ui::NativeTheme
* Widget::GetNativeTheme() const {
748 return native_widget_
->GetNativeTheme();
751 FocusManager
* Widget::GetFocusManager() {
752 Widget
* toplevel_widget
= GetTopLevelWidget();
753 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
756 const FocusManager
* Widget::GetFocusManager() const {
757 const Widget
* toplevel_widget
= GetTopLevelWidget();
758 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
761 ui::TextInputClient
* Widget::GetFocusedTextInputClient() {
762 FocusManager
* focus_manager
= GetFocusManager();
763 View
* view
= focus_manager
? focus_manager
->GetFocusedView() : nullptr;
764 return view
? view
->GetTextInputClient() : nullptr;
767 InputMethod
* Widget::GetInputMethod() {
768 return const_cast<InputMethod
*>(
769 const_cast<const Widget
*>(this)->GetInputMethod());
772 const InputMethod
* Widget::GetInputMethod() const {
773 if (is_top_level()) {
774 if (!input_method_
.get())
775 input_method_
= const_cast<Widget
*>(this)->CreateInputMethod().Pass();
776 return input_method_
.get();
778 const Widget
* toplevel
= GetTopLevelWidget();
779 // If GetTopLevelWidget() returns itself which is not toplevel,
780 // the widget is detached from toplevel widget.
781 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
782 // if there is no toplevel. We probably need to add GetTopMostWidget()
783 // to replace some use cases.
784 return (toplevel
&& toplevel
!= this) ? toplevel
->GetInputMethod() : NULL
;
788 ui::InputMethod
* Widget::GetHostInputMethod() {
789 return native_widget_private()->GetHostInputMethod();
792 void Widget::RunShellDrag(View
* view
,
793 const ui::OSExchangeData
& data
,
794 const gfx::Point
& location
,
796 ui::DragDropTypes::DragEventSource source
) {
797 dragged_view_
= view
;
800 WidgetDeletionObserver
widget_deletion_observer(this);
801 native_widget_
->RunShellDrag(view
, data
, location
, operation
, source
);
803 // The widget may be destroyed during the drag operation.
804 if (!widget_deletion_observer
.IsWidgetAlive())
807 // If the view is removed during the drag operation, dragged_view_ is set to
809 if (view
&& dragged_view_
== view
) {
810 dragged_view_
= NULL
;
816 void Widget::SchedulePaintInRect(const gfx::Rect
& rect
) {
817 native_widget_
->SchedulePaintInRect(rect
);
820 void Widget::SetCursor(gfx::NativeCursor cursor
) {
821 native_widget_
->SetCursor(cursor
);
824 bool Widget::IsMouseEventsEnabled() const {
825 return native_widget_
->IsMouseEventsEnabled();
828 void Widget::SetNativeWindowProperty(const char* name
, void* value
) {
829 native_widget_
->SetNativeWindowProperty(name
, value
);
832 void* Widget::GetNativeWindowProperty(const char* name
) const {
833 return native_widget_
->GetNativeWindowProperty(name
);
836 void Widget::UpdateWindowTitle() {
837 if (!non_client_view_
)
840 // Update the native frame's text. We do this regardless of whether or not
841 // the native frame is being used, since this also updates the taskbar, etc.
842 base::string16 window_title
= widget_delegate_
->GetWindowTitle();
843 base::i18n::AdjustStringForLocaleDirection(&window_title
);
844 if (!native_widget_
->SetWindowTitle(window_title
))
846 non_client_view_
->UpdateWindowTitle();
848 // If the non-client view is rendering its own title, it'll need to relayout
849 // now and to get a paint update later on.
850 non_client_view_
->Layout();
853 void Widget::UpdateWindowIcon() {
854 if (non_client_view_
)
855 non_client_view_
->UpdateWindowIcon();
856 native_widget_
->SetWindowIcons(widget_delegate_
->GetWindowIcon(),
857 widget_delegate_
->GetWindowAppIcon());
860 FocusTraversable
* Widget::GetFocusTraversable() {
861 return static_cast<internal::RootView
*>(root_view_
.get());
864 void Widget::ThemeChanged() {
865 root_view_
->ThemeChanged();
868 void Widget::LocaleChanged() {
869 root_view_
->LocaleChanged();
872 void Widget::DeviceScaleFactorChanged(float device_scale_factor
) {
873 root_view_
->DeviceScaleFactorChanged(device_scale_factor
);
876 void Widget::SetFocusTraversableParent(FocusTraversable
* parent
) {
877 root_view_
->SetFocusTraversableParent(parent
);
880 void Widget::SetFocusTraversableParentView(View
* parent_view
) {
881 root_view_
->SetFocusTraversableParentView(parent_view
);
884 void Widget::ClearNativeFocus() {
885 native_widget_
->ClearNativeFocus();
888 NonClientFrameView
* Widget::CreateNonClientFrameView() {
889 NonClientFrameView
* frame_view
=
890 widget_delegate_
->CreateNonClientFrameView(this);
892 frame_view
= native_widget_
->CreateNonClientFrameView();
893 if (!frame_view
&& ViewsDelegate::views_delegate
) {
895 ViewsDelegate::views_delegate
->CreateDefaultNonClientFrameView(this);
900 CustomFrameView
* custom_frame_view
= new CustomFrameView
;
901 custom_frame_view
->Init(this);
902 return custom_frame_view
;
905 bool Widget::ShouldUseNativeFrame() const {
906 if (frame_type_
!= FRAME_TYPE_DEFAULT
)
907 return frame_type_
== FRAME_TYPE_FORCE_NATIVE
;
908 return native_widget_
->ShouldUseNativeFrame();
911 bool Widget::ShouldWindowContentsBeTransparent() const {
912 return native_widget_
->ShouldWindowContentsBeTransparent();
915 void Widget::DebugToggleFrameType() {
916 if (frame_type_
== FRAME_TYPE_DEFAULT
) {
917 frame_type_
= ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM
:
918 FRAME_TYPE_FORCE_NATIVE
;
920 frame_type_
= frame_type_
== FRAME_TYPE_FORCE_CUSTOM
?
921 FRAME_TYPE_FORCE_NATIVE
: FRAME_TYPE_FORCE_CUSTOM
;
926 void Widget::FrameTypeChanged() {
927 native_widget_
->FrameTypeChanged();
930 const ui::Compositor
* Widget::GetCompositor() const {
931 return native_widget_
->GetCompositor();
934 const ui::Layer
* Widget::GetLayer() const {
935 return native_widget_
->GetLayer();
938 void Widget::ReorderNativeViews() {
939 native_widget_
->ReorderNativeViews();
942 void Widget::UpdateRootLayers() {
943 // Calculate the layers requires traversing the tree, and since nearly any
944 // mutation of the tree can trigger this call we delay until absolutely
946 root_layers_dirty_
= true;
949 const NativeWidget
* Widget::native_widget() const {
950 return native_widget_
;
953 NativeWidget
* Widget::native_widget() {
954 return native_widget_
;
957 void Widget::SetCapture(View
* view
) {
958 if (!native_widget_
->HasCapture()) {
959 native_widget_
->SetCapture();
961 // Early return if setting capture was unsuccessful.
962 if (!native_widget_
->HasCapture())
966 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
967 is_mouse_button_pressed_
= true;
968 root_view_
->SetMouseHandler(view
);
971 void Widget::ReleaseCapture() {
972 if (native_widget_
->HasCapture())
973 native_widget_
->ReleaseCapture();
976 bool Widget::HasCapture() {
977 return native_widget_
->HasCapture();
980 TooltipManager
* Widget::GetTooltipManager() {
981 return native_widget_
->GetTooltipManager();
984 const TooltipManager
* Widget::GetTooltipManager() const {
985 return native_widget_
->GetTooltipManager();
988 gfx::Rect
Widget::GetWorkAreaBoundsInScreen() const {
989 return native_widget_
->GetWorkAreaBoundsInScreen();
992 void Widget::SynthesizeMouseMoveEvent() {
993 last_mouse_event_was_move_
= false;
994 ui::MouseEvent
mouse_event(ui::ET_MOUSE_MOVED
,
995 last_mouse_event_position_
,
996 last_mouse_event_position_
,
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