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/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/base/default_theme_provider.h"
12 #include "ui/base/hit_test.h"
13 #include "ui/base/l10n/l10n_font_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/compositor/compositor.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/events/event.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/views/controls/menu/menu_controller.h"
20 #include "ui/views/focus/focus_manager.h"
21 #include "ui/views/focus/focus_manager_factory.h"
22 #include "ui/views/focus/view_storage.h"
23 #include "ui/views/focus/widget_focus_manager.h"
24 #include "ui/views/ime/input_method.h"
25 #include "ui/views/views_delegate.h"
26 #include "ui/views/widget/native_widget_private.h"
27 #include "ui/views/widget/root_view.h"
28 #include "ui/views/widget/tooltip_manager.h"
29 #include "ui/views/widget/widget_delegate.h"
30 #include "ui/views/widget/widget_deletion_observer.h"
31 #include "ui/views/widget/widget_observer.h"
32 #include "ui/views/window/custom_frame_view.h"
33 #include "ui/views/window/dialog_delegate.h"
36 #include "ui/base/cursor/cursor.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 DefaultWidgetDelegate(Widget
* widget
, const Widget::InitParams
& params
)
75 can_activate_(!params
.child
&&
76 params
.type
!= Widget::InitParams::TYPE_POPUP
) {
78 virtual ~DefaultWidgetDelegate() {}
80 // Overridden from WidgetDelegate:
81 virtual void DeleteDelegate() OVERRIDE
{
84 virtual Widget
* GetWidget() OVERRIDE
{
87 virtual const Widget
* GetWidget() const OVERRIDE
{
90 virtual bool CanActivate() const OVERRIDE
{
93 virtual bool ShouldAdvanceFocusToTopLevelWidget() const OVERRIDE
{
94 // In most situations where a Widget is used without a delegate the Widget
95 // is used as a container, so that we want focus to advance to the top-level
96 // widget. A good example of this is the find bar.
104 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate
);
107 ////////////////////////////////////////////////////////////////////////////////
108 // Widget, InitParams:
110 Widget::InitParams::InitParams()
114 opacity((ViewsDelegate::views_delegate
&&
115 ViewsDelegate::views_delegate
->UseTransparentWindows()) ?
116 TRANSLUCENT_WINDOW
: INFER_OPACITY
),
120 ownership(NATIVE_WIDGET_OWNS_WIDGET
),
121 mirror_origin_in_rtl(false),
122 has_dropshadow(false),
123 remove_standard_frame(false),
124 use_system_default_icon(false),
125 show_state(ui::SHOW_STATE_DEFAULT
),
126 double_buffer(false),
129 desktop_root_window_host(NULL
),
131 layer_type(ui::LAYER_TEXTURED
),
135 Widget::InitParams::InitParams(Type type
)
138 child(type
== TYPE_CONTROL
),
139 opacity(((type
== TYPE_WINDOW
|| type
== TYPE_PANEL
) &&
140 ViewsDelegate::views_delegate
&&
141 ViewsDelegate::views_delegate
->UseTransparentWindows()) ?
142 TRANSLUCENT_WINDOW
: INFER_OPACITY
),
144 can_activate(type
!= TYPE_POPUP
&& type
!= TYPE_MENU
),
145 keep_on_top(type
== TYPE_MENU
),
146 ownership(NATIVE_WIDGET_OWNS_WIDGET
),
147 mirror_origin_in_rtl(false),
148 has_dropshadow(false),
149 remove_standard_frame(false),
150 use_system_default_icon(false),
151 show_state(ui::SHOW_STATE_DEFAULT
),
152 double_buffer(false),
155 desktop_root_window_host(NULL
),
157 layer_type(ui::LAYER_TEXTURED
),
161 ////////////////////////////////////////////////////////////////////////////////
165 : native_widget_(NULL
),
166 widget_delegate_(NULL
),
167 non_client_view_(NULL
),
169 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET
),
170 is_secondary_widget_(true),
171 frame_type_(FRAME_TYPE_DEFAULT
),
172 disable_inactive_rendering_(false),
173 widget_closed_(false),
174 saved_show_state_(ui::SHOW_STATE_DEFAULT
),
175 focus_on_creation_(true),
176 is_top_level_(false),
177 native_widget_initialized_(false),
178 native_widget_destroyed_(false),
179 is_mouse_button_pressed_(false),
180 is_touch_down_(false),
181 last_mouse_event_was_move_(false),
182 auto_release_capture_(true),
183 root_layers_dirty_(false),
184 movement_disabled_(false) {
189 if (ownership_
== InitParams::WIDGET_OWNS_NATIVE_WIDGET
) {
190 delete native_widget_
;
192 DCHECK(native_widget_destroyed_
)
193 << "Destroying a widget with a live native widget. "
194 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
199 Widget
* Widget::CreateWindow(WidgetDelegate
* delegate
) {
200 return CreateWindowWithBounds(delegate
, gfx::Rect());
204 Widget
* Widget::CreateWindowWithBounds(WidgetDelegate
* delegate
,
205 const gfx::Rect
& bounds
) {
206 Widget
* widget
= new Widget
;
207 Widget::InitParams params
;
208 params
.bounds
= bounds
;
209 params
.delegate
= delegate
;
210 params
.top_level
= true;
211 widget
->Init(params
);
216 Widget
* Widget::CreateWindowWithParent(WidgetDelegate
* delegate
,
217 gfx::NativeWindow parent
) {
218 return CreateWindowWithParentAndBounds(delegate
, parent
, gfx::Rect());
222 Widget
* Widget::CreateWindowWithParentAndBounds(WidgetDelegate
* delegate
,
223 gfx::NativeWindow parent
,
224 const gfx::Rect
& bounds
) {
225 Widget
* widget
= new Widget
;
226 Widget::InitParams params
;
227 params
.delegate
= delegate
;
228 params
.parent
= parent
;
229 params
.bounds
= bounds
;
230 widget
->Init(params
);
235 Widget
* Widget::CreateWindowWithContext(WidgetDelegate
* delegate
,
236 gfx::NativeView context
) {
237 return CreateWindowWithContextAndBounds(delegate
, context
, gfx::Rect());
241 Widget
* Widget::CreateWindowWithContextAndBounds(WidgetDelegate
* delegate
,
242 gfx::NativeView context
,
243 const gfx::Rect
& bounds
) {
244 Widget
* widget
= new Widget
;
245 Widget::InitParams params
;
246 params
.delegate
= delegate
;
247 params
.context
= context
;
248 params
.bounds
= bounds
;
249 widget
->Init(params
);
254 Widget
* Widget::CreateWindowAsFramelessChild(WidgetDelegate
* widget_delegate
,
255 gfx::NativeView parent
,
256 gfx::NativeView new_style_parent
) {
257 views::Widget
* widget
= new views::Widget
;
259 views::Widget::InitParams params
;
260 params
.delegate
= widget_delegate
;
262 if (views::DialogDelegate::UseNewStyle()) {
263 params
.parent
= new_style_parent
;
264 params
.remove_standard_frame
= true;
265 #if defined(USE_AURA)
266 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
269 params
.parent
= parent
;
272 widget
->Init(params
);
278 Widget
* Widget::GetWidgetForNativeView(gfx::NativeView native_view
) {
279 internal::NativeWidgetPrivate
* native_widget
=
280 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view
);
281 return native_widget
? native_widget
->GetWidget() : NULL
;
285 Widget
* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window
) {
286 internal::NativeWidgetPrivate
* native_widget
=
287 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
289 return native_widget
? native_widget
->GetWidget() : NULL
;
293 Widget
* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view
) {
294 internal::NativeWidgetPrivate
* native_widget
=
295 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view
);
296 return native_widget
? native_widget
->GetWidget() : NULL
;
301 void Widget::GetAllChildWidgets(gfx::NativeView native_view
,
303 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view
, children
);
307 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view
,
309 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view
, owned
);
313 void Widget::ReparentNativeView(gfx::NativeView native_view
,
314 gfx::NativeView new_parent
) {
315 internal::NativeWidgetPrivate::ReparentNativeView(native_view
, new_parent
);
319 int Widget::GetLocalizedContentsWidth(int col_resource_id
) {
320 return ui::GetLocalizedContentsWidthForFont(col_resource_id
,
321 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
));
325 int Widget::GetLocalizedContentsHeight(int row_resource_id
) {
326 return ui::GetLocalizedContentsHeightForFont(row_resource_id
,
327 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont
));
331 gfx::Size
Widget::GetLocalizedContentsSize(int col_resource_id
,
332 int row_resource_id
) {
333 return gfx::Size(GetLocalizedContentsWidth(col_resource_id
),
334 GetLocalizedContentsHeight(row_resource_id
));
338 bool Widget::RequiresNonClientView(InitParams::Type type
) {
339 return type
== InitParams::TYPE_WINDOW
||
340 type
== InitParams::TYPE_PANEL
||
341 type
== InitParams::TYPE_BUBBLE
;
344 void Widget::Init(const InitParams
& in_params
) {
345 TRACE_EVENT0("views", "Widget::Init");
346 InitParams params
= in_params
;
348 is_top_level_
= params
.top_level
||
350 params
.type
!= InitParams::TYPE_CONTROL
&&
351 params
.type
!= InitParams::TYPE_TOOLTIP
);
352 params
.top_level
= is_top_level_
;
353 if (params
.opacity
== InitParams::INFER_OPACITY
) {
354 #if defined(OS_WIN) && defined(USE_AURA)
355 // By default, make all top-level windows but the main window transparent
356 // initially so that they can be made to fade in.
357 if (is_top_level_
&& params
.type
!= InitParams::TYPE_WINDOW
)
358 params
.opacity
= InitParams::TRANSLUCENT_WINDOW
;
360 params
.opacity
= InitParams::OPAQUE_WINDOW
;
362 params
.opacity
= InitParams::OPAQUE_WINDOW
;
366 if (ViewsDelegate::views_delegate
)
367 ViewsDelegate::views_delegate
->OnBeforeWidgetInit(¶ms
, this);
369 widget_delegate_
= params
.delegate
?
370 params
.delegate
: new DefaultWidgetDelegate(this, params
);
371 ownership_
= params
.ownership
;
372 native_widget_
= CreateNativeWidget(params
.native_widget
, this)->
373 AsNativeWidgetPrivate();
374 root_view_
.reset(CreateRootView());
375 default_theme_provider_
.reset(new ui::DefaultThemeProvider
);
376 if (params
.type
== InitParams::TYPE_MENU
) {
377 is_mouse_button_pressed_
=
378 internal::NativeWidgetPrivate::IsMouseButtonDown();
380 native_widget_
->InitNativeWidget(params
);
381 if (RequiresNonClientView(params
.type
)) {
382 non_client_view_
= new NonClientView
;
383 non_client_view_
->SetFrameView(CreateNonClientFrameView());
384 // Create the ClientView, add it to the NonClientView and add the
385 // NonClientView to the RootView. This will cause everything to be parented.
386 non_client_view_
->set_client_view(widget_delegate_
->CreateClientView(this));
387 non_client_view_
->SetOverlayView(widget_delegate_
->CreateOverlayView());
388 SetContentsView(non_client_view_
);
389 // Initialize the window's title before setting the window's initial bounds;
390 // the frame view's preferred height may depend on the presence of a title.
392 SetInitialBounds(params
.bounds
);
393 if (params
.show_state
== ui::SHOW_STATE_MAXIMIZED
)
395 else if (params
.show_state
== ui::SHOW_STATE_MINIMIZED
)
397 } else if (params
.delegate
) {
398 SetContentsView(params
.delegate
->GetContentsView());
399 SetInitialBoundsForFramelessWindow(params
.bounds
);
401 native_widget_initialized_
= true;
404 // Unconverted methods (see header) --------------------------------------------
406 gfx::NativeView
Widget::GetNativeView() const {
407 return native_widget_
->GetNativeView();
410 gfx::NativeWindow
Widget::GetNativeWindow() const {
411 return native_widget_
->GetNativeWindow();
414 void Widget::AddObserver(WidgetObserver
* observer
) {
415 observers_
.AddObserver(observer
);
418 void Widget::RemoveObserver(WidgetObserver
* observer
) {
419 observers_
.RemoveObserver(observer
);
422 bool Widget::HasObserver(WidgetObserver
* observer
) {
423 return observers_
.HasObserver(observer
);
426 bool Widget::GetAccelerator(int cmd_id
, ui::Accelerator
* accelerator
) {
430 void Widget::ViewHierarchyChanged(
431 const View::ViewHierarchyChangedDetails
& details
) {
432 if (!details
.is_add
) {
433 if (details
.child
== dragged_view_
)
434 dragged_view_
= NULL
;
435 FocusManager
* focus_manager
= GetFocusManager();
437 focus_manager
->ViewRemoved(details
.child
);
438 ViewStorage::GetInstance()->ViewRemoved(details
.child
);
439 native_widget_
->ViewRemoved(details
.child
);
443 void Widget::NotifyNativeViewHierarchyWillChange() {
444 FocusManager
* focus_manager
= GetFocusManager();
445 // We are being removed from a window hierarchy. Treat this as
446 // the root_view_ being removed.
448 focus_manager
->ViewRemoved(root_view_
.get());
451 void Widget::NotifyNativeViewHierarchyChanged() {
452 root_view_
->NotifyNativeViewHierarchyChanged();
455 // Converted methods (see header) ----------------------------------------------
457 Widget
* Widget::GetTopLevelWidget() {
458 return const_cast<Widget
*>(
459 static_cast<const Widget
*>(this)->GetTopLevelWidget());
462 const Widget
* Widget::GetTopLevelWidget() const {
463 // GetTopLevelNativeWidget doesn't work during destruction because
464 // property is gone after gobject gets deleted. Short circuit here
465 // for toplevel so that InputMethod can remove itself from
467 return is_top_level() ? this : native_widget_
->GetTopLevelWidget();
470 void Widget::SetContentsView(View
* view
) {
471 // Do not SetContentsView() again if it is already set to the same view.
472 if (view
== GetContentsView())
474 root_view_
->SetContentsView(view
);
475 if (non_client_view_
!= view
) {
476 // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
477 // was true when the widget was initialized. Creating widgets with non
478 // client views and then setting the contents view can cause subtle
479 // problems on Windows, where the native widget thinks there is still a
480 // |non_client_view_|. If you get this error, either use a different type
481 // when initializing the widget, or don't call SetContentsView().
482 DCHECK(!non_client_view_
);
483 non_client_view_
= NULL
;
487 View
* Widget::GetContentsView() {
488 return root_view_
->GetContentsView();
491 gfx::Rect
Widget::GetWindowBoundsInScreen() const {
492 return native_widget_
->GetWindowBoundsInScreen();
495 gfx::Rect
Widget::GetClientAreaBoundsInScreen() const {
496 return native_widget_
->GetClientAreaBoundsInScreen();
499 gfx::Rect
Widget::GetRestoredBounds() const {
500 return native_widget_
->GetRestoredBounds();
503 void Widget::SetBounds(const gfx::Rect
& bounds
) {
504 native_widget_
->SetBounds(bounds
);
507 void Widget::SetSize(const gfx::Size
& size
) {
508 native_widget_
->SetSize(size
);
511 void Widget::CenterWindow(const gfx::Size
& size
) {
512 native_widget_
->CenterWindow(size
);
515 void Widget::SetBoundsConstrained(const gfx::Rect
& bounds
) {
516 gfx::Rect work_area
=
517 gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
518 bounds
.origin()).work_area();
519 if (work_area
.IsEmpty()) {
522 // Inset the work area slightly.
523 work_area
.Inset(10, 10, 10, 10);
524 work_area
.AdjustToFit(bounds
);
525 SetBounds(work_area
);
529 void Widget::SetVisibilityChangedAnimationsEnabled(bool value
) {
530 native_widget_
->SetVisibilityChangedAnimationsEnabled(value
);
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 TRACE_EVENT0("views", "Widget::Show");
603 if (non_client_view_
) {
604 // While initializing, the kiosk mode will go to full screen before the
605 // widget gets shown. In that case we stay in full screen mode, regardless
606 // of the |saved_show_state_| member.
607 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
608 !initial_restored_bounds_
.IsEmpty() &&
610 native_widget_
->ShowMaximizedWithBounds(initial_restored_bounds_
);
612 native_widget_
->ShowWithWindowState(
613 IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN
: saved_show_state_
);
615 // |saved_show_state_| only applies the first time the window is shown.
616 // If we don't reset the value the window may be shown maximized every time
617 // it is subsequently shown after being hidden.
618 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
620 native_widget_
->Show();
624 void Widget::Hide() {
625 native_widget_
->Hide();
628 void Widget::ShowInactive() {
629 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
630 // call SetBounds()with the restored bounds to set the correct size. This
631 // normally should not happen, but if it does we should avoid showing unsized
633 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
&&
634 !initial_restored_bounds_
.IsEmpty()) {
635 SetBounds(initial_restored_bounds_
);
636 saved_show_state_
= ui::SHOW_STATE_NORMAL
;
638 native_widget_
->ShowWithWindowState(ui::SHOW_STATE_INACTIVE
);
641 void Widget::Activate() {
642 native_widget_
->Activate();
645 void Widget::Deactivate() {
646 native_widget_
->Deactivate();
649 bool Widget::IsActive() const {
650 return native_widget_
->IsActive();
653 void Widget::DisableInactiveRendering() {
654 SetInactiveRenderingDisabled(true);
657 void Widget::SetAlwaysOnTop(bool on_top
) {
658 native_widget_
->SetAlwaysOnTop(on_top
);
661 bool Widget::IsAlwaysOnTop() const {
662 return native_widget_
->IsAlwaysOnTop();
665 void Widget::Maximize() {
666 native_widget_
->Maximize();
669 void Widget::Minimize() {
670 native_widget_
->Minimize();
673 void Widget::Restore() {
674 native_widget_
->Restore();
677 bool Widget::IsMaximized() const {
678 return native_widget_
->IsMaximized();
681 bool Widget::IsMinimized() const {
682 return native_widget_
->IsMinimized();
685 void Widget::SetFullscreen(bool fullscreen
) {
686 native_widget_
->SetFullscreen(fullscreen
);
689 bool Widget::IsFullscreen() const {
690 return native_widget_
->IsFullscreen();
693 void Widget::SetOpacity(unsigned char opacity
) {
694 native_widget_
->SetOpacity(opacity
);
697 void Widget::SetUseDragFrame(bool use_drag_frame
) {
698 native_widget_
->SetUseDragFrame(use_drag_frame
);
701 void Widget::FlashFrame(bool flash
) {
702 native_widget_
->FlashFrame(flash
);
705 View
* Widget::GetRootView() {
706 return root_view_
.get();
709 const View
* Widget::GetRootView() const {
710 return root_view_
.get();
713 bool Widget::IsVisible() const {
714 return native_widget_
->IsVisible();
717 ui::ThemeProvider
* Widget::GetThemeProvider() const {
718 const Widget
* root_widget
= GetTopLevelWidget();
719 if (root_widget
&& root_widget
!= this) {
720 // Attempt to get the theme provider, and fall back to the default theme
721 // provider if not found.
722 ui::ThemeProvider
* provider
= root_widget
->GetThemeProvider();
726 provider
= root_widget
->default_theme_provider_
.get();
730 return default_theme_provider_
.get();
733 const ui::NativeTheme
* Widget::GetNativeTheme() const {
734 return native_widget_
->GetNativeTheme();
737 FocusManager
* Widget::GetFocusManager() {
738 Widget
* toplevel_widget
= GetTopLevelWidget();
739 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
742 const FocusManager
* Widget::GetFocusManager() const {
743 const Widget
* toplevel_widget
= GetTopLevelWidget();
744 return toplevel_widget
? toplevel_widget
->focus_manager_
.get() : NULL
;
747 InputMethod
* Widget::GetInputMethod() {
748 return const_cast<InputMethod
*>(
749 const_cast<const Widget
*>(this)->GetInputMethod());
752 const InputMethod
* Widget::GetInputMethod() const {
753 if (is_top_level()) {
754 if (!input_method_
.get())
755 input_method_
= const_cast<Widget
*>(this)->CreateInputMethod().Pass();
756 return input_method_
.get();
758 const Widget
* toplevel
= GetTopLevelWidget();
759 // If GetTopLevelWidget() returns itself which is not toplevel,
760 // the widget is detached from toplevel widget.
761 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
762 // if there is no toplevel. We probably need to add GetTopMostWidget()
763 // to replace some use cases.
764 return (toplevel
&& toplevel
!= this) ? toplevel
->GetInputMethod() : NULL
;
768 void Widget::RunShellDrag(View
* view
,
769 const ui::OSExchangeData
& data
,
770 const gfx::Point
& location
,
772 ui::DragDropTypes::DragEventSource source
) {
773 dragged_view_
= view
;
774 native_widget_
->RunShellDrag(view
, data
, location
, operation
, source
);
775 // If the view is removed during the drag operation, dragged_view_ is set to
777 if (view
&& dragged_view_
== view
) {
778 dragged_view_
= NULL
;
783 void Widget::SchedulePaintInRect(const gfx::Rect
& rect
) {
784 native_widget_
->SchedulePaintInRect(rect
);
787 void Widget::SetCursor(gfx::NativeCursor cursor
) {
788 native_widget_
->SetCursor(cursor
);
791 bool Widget::IsMouseEventsEnabled() const {
792 return native_widget_
->IsMouseEventsEnabled();
795 void Widget::SetNativeWindowProperty(const char* name
, void* value
) {
796 native_widget_
->SetNativeWindowProperty(name
, value
);
799 void* Widget::GetNativeWindowProperty(const char* name
) const {
800 return native_widget_
->GetNativeWindowProperty(name
);
803 void Widget::UpdateWindowTitle() {
804 if (!non_client_view_
)
807 // Update the native frame's text. We do this regardless of whether or not
808 // the native frame is being used, since this also updates the taskbar, etc.
809 string16 window_title
= widget_delegate_
->GetWindowTitle();
810 base::i18n::AdjustStringForLocaleDirection(&window_title
);
811 native_widget_
->SetWindowTitle(window_title
);
812 non_client_view_
->UpdateWindowTitle();
814 // If the non-client view is rendering its own title, it'll need to relayout
815 // now and to get a paint update later on.
816 non_client_view_
->Layout();
819 void Widget::UpdateWindowIcon() {
820 if (non_client_view_
)
821 non_client_view_
->UpdateWindowIcon();
822 native_widget_
->SetWindowIcons(widget_delegate_
->GetWindowIcon(),
823 widget_delegate_
->GetWindowAppIcon());
826 FocusTraversable
* Widget::GetFocusTraversable() {
827 return static_cast<internal::RootView
*>(root_view_
.get());
830 void Widget::ThemeChanged() {
831 root_view_
->ThemeChanged();
834 void Widget::LocaleChanged() {
835 root_view_
->LocaleChanged();
838 void Widget::SetFocusTraversableParent(FocusTraversable
* parent
) {
839 root_view_
->SetFocusTraversableParent(parent
);
842 void Widget::SetFocusTraversableParentView(View
* parent_view
) {
843 root_view_
->SetFocusTraversableParentView(parent_view
);
846 void Widget::ClearNativeFocus() {
847 native_widget_
->ClearNativeFocus();
850 NonClientFrameView
* Widget::CreateNonClientFrameView() {
851 NonClientFrameView
* frame_view
=
852 widget_delegate_
->CreateNonClientFrameView(this);
854 frame_view
= native_widget_
->CreateNonClientFrameView();
855 if (!frame_view
&& ViewsDelegate::views_delegate
) {
857 ViewsDelegate::views_delegate
->CreateDefaultNonClientFrameView(this);
862 CustomFrameView
* custom_frame_view
= new CustomFrameView
;
863 custom_frame_view
->Init(this);
864 return custom_frame_view
;
867 bool Widget::ShouldUseNativeFrame() const {
868 if (frame_type_
!= FRAME_TYPE_DEFAULT
)
869 return frame_type_
== FRAME_TYPE_FORCE_NATIVE
;
870 return native_widget_
->ShouldUseNativeFrame();
873 void Widget::DebugToggleFrameType() {
874 if (frame_type_
== FRAME_TYPE_DEFAULT
) {
875 frame_type_
= ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM
:
876 FRAME_TYPE_FORCE_NATIVE
;
878 frame_type_
= frame_type_
== FRAME_TYPE_FORCE_CUSTOM
?
879 FRAME_TYPE_FORCE_NATIVE
: FRAME_TYPE_FORCE_CUSTOM
;
884 void Widget::FrameTypeChanged() {
885 native_widget_
->FrameTypeChanged();
888 const ui::Compositor
* Widget::GetCompositor() const {
889 return native_widget_
->GetCompositor();
892 ui::Compositor
* Widget::GetCompositor() {
893 return native_widget_
->GetCompositor();
896 ui::Layer
* Widget::GetLayer() {
897 return native_widget_
->GetLayer();
900 void Widget::ReorderNativeViews() {
901 native_widget_
->ReorderNativeViews();
904 void Widget::UpdateRootLayers() {
905 // Calculate the layers requires traversing the tree, and since nearly any
906 // mutation of the tree can trigger this call we delay until absolutely
908 root_layers_dirty_
= true;
911 const NativeWidget
* Widget::native_widget() const {
912 return native_widget_
;
915 NativeWidget
* Widget::native_widget() {
916 return native_widget_
;
919 void Widget::SetCapture(View
* view
) {
920 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
921 is_mouse_button_pressed_
= true;
922 if (internal::NativeWidgetPrivate::IsTouchDown())
923 is_touch_down_
= true;
924 root_view_
->SetMouseHandler(view
);
925 if (!native_widget_
->HasCapture())
926 native_widget_
->SetCapture();
929 void Widget::ReleaseCapture() {
930 if (native_widget_
->HasCapture())
931 native_widget_
->ReleaseCapture();
934 bool Widget::HasCapture() {
935 return native_widget_
->HasCapture();
938 TooltipManager
* Widget::GetTooltipManager() {
939 return native_widget_
->GetTooltipManager();
942 const TooltipManager
* Widget::GetTooltipManager() const {
943 return native_widget_
->GetTooltipManager();
946 bool Widget::SetInitialFocus() {
947 View
* v
= widget_delegate_
->GetInitiallyFocusedView();
948 if (!focus_on_creation_
) {
949 // If not focusing the window now, tell the focus manager which view to
950 // focus when the window is restored.
952 focus_manager_
->SetStoredFocusView(v
);
960 gfx::Rect
Widget::GetWorkAreaBoundsInScreen() const {
961 return native_widget_
->GetWorkAreaBoundsInScreen();
964 void Widget::SynthesizeMouseMoveEvent() {
965 last_mouse_event_was_move_
= false;
966 ui::MouseEvent
mouse_event(ui::ET_MOUSE_MOVED
,
967 last_mouse_event_position_
,
968 last_mouse_event_position_
,
969 ui::EF_IS_SYNTHESIZED
);
970 root_view_
->OnMouseMoved(mouse_event
);
973 void Widget::OnRootViewLayout() {
974 native_widget_
->OnRootViewLayout();
977 void Widget::OnOwnerClosing() {
980 ////////////////////////////////////////////////////////////////////////////////
981 // Widget, NativeWidgetDelegate implementation:
983 bool Widget::IsModal() const {
984 return widget_delegate_
->GetModalType() != ui::MODAL_TYPE_NONE
;
987 bool Widget::IsDialogBox() const {
988 return !!widget_delegate_
->AsDialogDelegate();
991 bool Widget::CanActivate() const {
992 return widget_delegate_
->CanActivate();
995 bool Widget::IsInactiveRenderingDisabled() const {
996 return disable_inactive_rendering_
;
999 void Widget::EnableInactiveRendering() {
1000 SetInactiveRenderingDisabled(false);
1003 void Widget::OnNativeWidgetActivationChanged(bool active
) {
1004 // On windows we may end up here before we've completed initialization (from
1005 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1006 // the Widget and will crash attempting to access it.
1007 if (!active
&& native_widget_initialized_
)
1008 SaveWindowPlacement();
1010 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1011 OnWidgetActivationChanged(this, active
));
1014 void Widget::OnNativeFocus(gfx::NativeView old_focused_view
) {
1015 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(old_focused_view
,
1019 void Widget::OnNativeBlur(gfx::NativeView new_focused_view
) {
1020 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(GetNativeView(),
1024 void Widget::OnNativeWidgetVisibilityChanging(bool visible
) {
1025 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1026 OnWidgetVisibilityChanging(this, visible
));
1029 void Widget::OnNativeWidgetVisibilityChanged(bool visible
) {
1030 View
* root
= GetRootView();
1032 root
->PropagateVisibilityNotifications(root
, visible
);
1033 FOR_EACH_OBSERVER(WidgetObserver
, observers_
,
1034 OnWidgetVisibilityChanged(this, visible
));
1035 if (GetCompositor() && root
&& root
->layer())
1036 root
->layer()->SetVisible(visible
);
1039 void Widget::OnNativeWidgetCreated(bool desktop_widget
) {
1041 focus_manager_
.reset(FocusManagerFactory::Create(this, desktop_widget
));
1043 native_widget_
->InitModalType(widget_delegate_
->GetModalType());
1045 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetCreated(this));
1048 void Widget::OnNativeWidgetDestroying() {
1049 // Tell the focus manager (if any) that root_view is being removed
1050 // in case that the focused view is under this root view.
1051 if (GetFocusManager())
1052 GetFocusManager()->ViewRemoved(root_view_
.get());
1053 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetDestroying(this));
1054 if (non_client_view_
)
1055 non_client_view_
->WindowClosing();
1056 widget_delegate_
->WindowClosing();
1059 void Widget::OnNativeWidgetDestroyed() {
1060 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetDestroyed(this));
1061 widget_delegate_
->DeleteDelegate();
1062 widget_delegate_
= NULL
;
1063 native_widget_destroyed_
= true;
1066 gfx::Size
Widget::GetMinimumSize() {
1067 return non_client_view_
? non_client_view_
->GetMinimumSize() : gfx::Size();
1070 gfx::Size
Widget::GetMaximumSize() {
1071 return non_client_view_
? non_client_view_
->GetMaximumSize() : gfx::Size();
1074 void Widget::OnNativeWidgetMove() {
1075 widget_delegate_
->OnWidgetMove();
1076 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetBoundsChanged(
1078 GetWindowBoundsInScreen()));
1081 void Widget::OnNativeWidgetSizeChanged(const gfx::Size
& new_size
) {
1082 root_view_
->SetSize(new_size
);
1084 // Size changed notifications can fire prior to full initialization
1085 // i.e. during session restore. Avoid saving session state during these
1086 // startup procedures.
1087 if (native_widget_initialized_
)
1088 SaveWindowPlacement();
1090 FOR_EACH_OBSERVER(WidgetObserver
, observers_
, OnWidgetBoundsChanged(
1092 GetWindowBoundsInScreen()));
1095 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1096 widget_delegate_
->OnWindowBeginUserBoundsChange();
1099 void Widget::OnNativeWidgetEndUserBoundsChange() {
1100 widget_delegate_
->OnWindowEndUserBoundsChange();
1103 bool Widget::HasFocusManager() const {
1104 return !!focus_manager_
.get();
1107 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect
& dirty_region
) {
1108 ui::Compositor
* compositor
= GetCompositor();
1112 compositor
->ScheduleRedrawRect(dirty_region
);
1116 void Widget::OnNativeWidgetPaint(gfx::Canvas
* canvas
) {
1117 // On Linux Aura, we can get here during Init() because of the
1118 // SetInitialBounds call.
1119 if (native_widget_initialized_
)
1120 GetRootView()->Paint(canvas
);
1123 int Widget::GetNonClientComponent(const gfx::Point
& point
) {
1124 int component
= non_client_view_
?
1125 non_client_view_
->NonClientHitTest(point
) :
1128 if (movement_disabled_
&& (component
== HTCAPTION
|| component
== HTSYSMENU
))
1134 void Widget::OnKeyEvent(ui::KeyEvent
* event
) {
1135 static_cast<internal::RootView
*>(GetRootView())->
1136 DispatchKeyEvent(event
);
1139 void Widget::OnMouseEvent(ui::MouseEvent
* event
) {
1140 View
* root_view
= GetRootView();
1141 switch (event
->type()) {
1142 case ui::ET_MOUSE_PRESSED
: {
1143 last_mouse_event_was_move_
= false;
1145 // We may get deleted by the time we return from OnMousePressed. So we
1146 // use an observer to make sure we are still alive.
1147 WidgetDeletionObserver
widget_deletion_observer(this);
1149 // Make sure we're still visible before we attempt capture as the mouse
1150 // press processing may have made the window hide (as happens with menus).
1152 // It is possible for a View to show a context menu on mouse-press. Since
1153 // the menu does a capture and starts a nested message-loop, the release
1154 // would go to the menu. The next click (i.e. both mouse-press and release
1155 // events) also go to the menu. The menu (and the nested message-loop)
1156 // gets closed after this second release event. The code then resumes from
1157 // here. So make sure that the mouse-button is still down before doing a
1159 if (root_view
&& root_view
->OnMousePressed(*event
) &&
1160 widget_deletion_observer
.IsWidgetAlive() && IsVisible() &&
1161 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1162 is_mouse_button_pressed_
= true;
1163 if (!native_widget_
->HasCapture())
1164 native_widget_
->SetCapture();
1165 event
->SetHandled();
1169 case ui::ET_MOUSE_RELEASED
:
1170 last_mouse_event_was_move_
= false;
1171 is_mouse_button_pressed_
= false;
1172 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1173 if (auto_release_capture_
&& native_widget_
->HasCapture())
1174 native_widget_
->ReleaseCapture();
1176 root_view
->OnMouseReleased(*event
);
1177 if ((event
->flags() & ui::EF_IS_NON_CLIENT
) == 0)
1178 event
->SetHandled();
1180 case ui::ET_MOUSE_MOVED
:
1181 case ui::ET_MOUSE_DRAGGED
:
1182 if (native_widget_
->HasCapture() && is_mouse_button_pressed_
) {
1183 last_mouse_event_was_move_
= false;
1185 root_view
->OnMouseDragged(*event
);
1186 } else if (!last_mouse_event_was_move_
||
1187 last_mouse_event_position_
!= event
->location()) {
1188 last_mouse_event_position_
= event
->location();
1189 last_mouse_event_was_move_
= true;
1191 root_view
->OnMouseMoved(*event
);
1194 case ui::ET_MOUSE_EXITED
:
1195 last_mouse_event_was_move_
= false;
1197 root_view
->OnMouseExited(*event
);
1199 case ui::ET_MOUSEWHEEL
:
1200 if (root_view
&& root_view
->OnMouseWheel(
1201 static_cast<const ui::MouseWheelEvent
&>(*event
)))
1202 event
->SetHandled();
1207 event
->SetHandled();
1210 void Widget::OnMouseCaptureLost() {
1211 if (is_mouse_button_pressed_
|| is_touch_down_
) {
1212 View
* root_view
= GetRootView();
1214 root_view
->OnMouseCaptureLost();
1216 is_touch_down_
= false;
1217 is_mouse_button_pressed_
= false;
1220 void Widget::OnTouchEvent(ui::TouchEvent
* event
) {
1221 static_cast<internal::RootView
*>(GetRootView())->
1222 DispatchTouchEvent(event
);
1225 void Widget::OnScrollEvent(ui::ScrollEvent
* event
) {
1226 static_cast<internal::RootView
*>(GetRootView())->
1227 DispatchScrollEvent(event
);
1230 void Widget::OnGestureEvent(ui::GestureEvent
* event
) {
1231 switch (event
->type()) {
1232 case ui::ET_GESTURE_TAP_DOWN
:
1233 is_touch_down_
= true;
1234 // We explicitly don't capture here. Not capturing enables multiple
1235 // widgets to get tap events at the same time. Views (such as tab
1236 // dragging) may explicitly capture.
1239 case ui::ET_GESTURE_END
:
1240 if (event
->details().touch_points() == 1) {
1241 is_touch_down_
= false;
1242 if (auto_release_capture_
)
1250 static_cast<internal::RootView
*>(GetRootView())->DispatchGestureEvent(event
);
1253 bool Widget::ExecuteCommand(int command_id
) {
1254 return widget_delegate_
->ExecuteWindowsCommand(command_id
);
1257 InputMethod
* Widget::GetInputMethodDirect() {
1258 return input_method_
.get();
1261 const std::vector
<ui::Layer
*>& Widget::GetRootLayers() {
1262 if (root_layers_dirty_
) {
1263 root_layers_dirty_
= false;
1264 root_layers_
.clear();
1265 BuildRootLayers(GetRootView(), &root_layers_
);
1267 return root_layers_
;
1270 bool Widget::HasHitTestMask() const {
1271 return widget_delegate_
->WidgetHasHitTestMask();
1274 void Widget::GetHitTestMask(gfx::Path
* mask
) const {
1276 widget_delegate_
->GetWidgetHitTestMask(mask
);
1279 Widget
* Widget::AsWidget() {
1283 const Widget
* Widget::AsWidget() const {
1287 ////////////////////////////////////////////////////////////////////////////////
1288 // Widget, FocusTraversable implementation:
1290 FocusSearch
* Widget::GetFocusSearch() {
1291 return root_view_
->GetFocusSearch();
1294 FocusTraversable
* Widget::GetFocusTraversableParent() {
1295 // We are a proxy to the root view, so we should be bypassed when traversing
1296 // up and as a result this should not be called.
1301 View
* Widget::GetFocusTraversableParentView() {
1302 // We are a proxy to the root view, so we should be bypassed when traversing
1303 // up and as a result this should not be called.
1308 ////////////////////////////////////////////////////////////////////////////////
1309 // Widget, protected:
1311 internal::RootView
* Widget::CreateRootView() {
1312 return new internal::RootView(this);
1315 void Widget::DestroyRootView() {
1316 non_client_view_
= NULL
;
1318 // Input method has to be destroyed before focus manager.
1319 input_method_
.reset();
1322 ////////////////////////////////////////////////////////////////////////////////
1325 void Widget::SetInactiveRenderingDisabled(bool value
) {
1326 if (value
== disable_inactive_rendering_
)
1329 disable_inactive_rendering_
= value
;
1330 if (non_client_view_
)
1331 non_client_view_
->SetInactiveRenderingDisabled(value
);
1334 void Widget::SaveWindowPlacement() {
1335 // The window delegate does the actual saving for us. It seems like (judging
1336 // by go/crash) that in some circumstances we can end up here after
1337 // WM_DESTROY, at which point the window delegate is likely gone. So just
1339 if (!widget_delegate_
)
1342 ui::WindowShowState show_state
= ui::SHOW_STATE_NORMAL
;
1344 native_widget_
->GetWindowPlacement(&bounds
, &show_state
);
1345 widget_delegate_
->SaveWindowPlacement(bounds
, show_state
);
1348 void Widget::SetInitialBounds(const gfx::Rect
& bounds
) {
1349 if (!non_client_view_
)
1352 gfx::Rect saved_bounds
;
1353 if (GetSavedWindowPlacement(&saved_bounds
, &saved_show_state_
)) {
1354 if (saved_show_state_
== ui::SHOW_STATE_MAXIMIZED
) {
1355 // If we're going to maximize, wait until Show is invoked to set the
1356 // bounds. That way we avoid a noticeable resize.
1357 initial_restored_bounds_
= saved_bounds
;
1358 } else if (!saved_bounds
.IsEmpty()) {
1359 // If the saved bounds are valid, use them.
1360 SetBounds(saved_bounds
);
1363 if (bounds
.IsEmpty()) {
1364 // No initial bounds supplied, so size the window to its content and
1365 // center over its parent.
1366 native_widget_
->CenterWindow(non_client_view_
->GetPreferredSize());
1368 // Use the supplied initial bounds.
1369 SetBoundsConstrained(bounds
);
1374 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect
& bounds
) {
1375 if (bounds
.IsEmpty()) {
1376 View
* contents_view
= GetContentsView();
1377 DCHECK(contents_view
);
1378 // No initial bounds supplied, so size the window to its content and
1379 // center over its parent if preferred size is provided.
1380 gfx::Size size
= contents_view
->GetPreferredSize();
1381 if (!size
.IsEmpty())
1382 native_widget_
->CenterWindow(size
);
1384 // Use the supplied initial bounds.
1385 SetBoundsConstrained(bounds
);
1389 bool Widget::GetSavedWindowPlacement(gfx::Rect
* bounds
,
1390 ui::WindowShowState
* show_state
) {
1391 // First we obtain the window's saved show-style and store it. We need to do
1392 // this here, rather than in Show() because by the time Show() is called,
1393 // the window's size will have been reset (below) and the saved maximized
1394 // state will have been lost. Sadly there's no way to tell on Windows when
1395 // a window is restored from maximized state, so we can't more accurately
1396 // track maximized state independently of sizing information.
1398 // Restore the window's placement from the controller.
1399 if (widget_delegate_
->GetSavedWindowPlacement(this, bounds
, show_state
)) {
1400 if (!widget_delegate_
->ShouldRestoreWindowSize()) {
1401 bounds
->set_size(non_client_view_
->GetPreferredSize());
1403 gfx::Size minimum_size
= GetMinimumSize();
1404 // Make sure the bounds are at least the minimum size.
1405 if (bounds
->width() < minimum_size
.width())
1406 bounds
->set_width(minimum_size
.width());
1408 if (bounds
->height() < minimum_size
.height())
1409 bounds
->set_height(minimum_size
.height());
1416 scoped_ptr
<InputMethod
> Widget::CreateInputMethod() {
1417 scoped_ptr
<InputMethod
> input_method(native_widget_
->CreateInputMethod());
1418 if (input_method
.get())
1419 input_method
->Init(this);
1420 return input_method
.Pass();
1423 void Widget::ReplaceInputMethod(InputMethod
* input_method
) {
1424 input_method_
.reset(input_method
);
1425 input_method
->SetDelegate(native_widget_
->GetInputMethodDelegate());
1426 input_method
->Init(this);
1429 namespace internal
{
1431 ////////////////////////////////////////////////////////////////////////////////
1432 // internal::NativeWidgetPrivate, NativeWidget implementation:
1434 internal::NativeWidgetPrivate
* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1438 } // namespace internal
1439 } // namespace views