Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / views / widget / widget.cc
blob7f649c04025bffe95c5f1ab444279baf94f77572
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/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"
38 namespace views {
40 namespace {
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) {
46 if (view->layer()) {
47 layers->push_back(view->layer());
48 } else {
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) {
59 if (!native_widget) {
60 native_widget =
61 internal::NativeWidgetPrivate::CreateNativeWidget(delegate);
63 return native_widget;
66 } // namespace
68 // A default implementation of WidgetDelegate, used by Widget when no
69 // WidgetDelegate is supplied.
70 class DefaultWidgetDelegate : public WidgetDelegate {
71 public:
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.
84 return true;
87 private:
88 Widget* widget_;
90 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate);
93 ////////////////////////////////////////////////////////////////////////////////
94 // Widget, InitParams:
96 Widget::InitParams::InitParams()
97 : type(TYPE_WINDOW),
98 delegate(NULL),
99 child(false),
100 opacity(INFER_OPACITY),
101 accept_events(true),
102 activatable(ACTIVATABLE_DEFAULT),
103 keep_on_top(false),
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),
111 parent(NULL),
112 native_widget(NULL),
113 desktop_window_tree_host(NULL),
114 layer_type(aura::WINDOW_LAYER_TEXTURED),
115 context(NULL),
116 force_show_in_taskbar(false) {
119 Widget::InitParams::InitParams(Type type)
120 : type(type),
121 delegate(NULL),
122 child(false),
123 opacity(INFER_OPACITY),
124 accept_events(true),
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),
134 parent(NULL),
135 native_widget(NULL),
136 desktop_window_tree_host(NULL),
137 layer_type(aura::WINDOW_LAYER_TEXTURED),
138 context(NULL),
139 force_show_in_taskbar(false) {
142 Widget::InitParams::~InitParams() {
145 ////////////////////////////////////////////////////////////////////////////////
146 // Widget, public:
148 Widget::Widget()
149 : native_widget_(NULL),
150 widget_delegate_(NULL),
151 non_client_view_(NULL),
152 dragged_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) {
172 Widget::~Widget() {
173 DestroyRootView();
174 if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) {
175 delete native_widget_;
176 } else {
177 DCHECK(native_widget_destroyed_)
178 << "Destroying a widget with a live native widget. "
179 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
183 // static
184 Widget* Widget::CreateWindow(WidgetDelegate* delegate) {
185 return CreateWindowWithBounds(delegate, gfx::Rect());
188 // static
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);
196 return widget;
199 // static
200 Widget* Widget::CreateWindowWithParent(WidgetDelegate* delegate,
201 gfx::NativeView parent) {
202 return CreateWindowWithParentAndBounds(delegate, parent, gfx::Rect());
205 // static
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);
215 return widget;
218 // static
219 Widget* Widget::CreateWindowWithContext(WidgetDelegate* delegate,
220 gfx::NativeWindow context) {
221 return CreateWindowWithContextAndBounds(delegate, context, gfx::Rect());
224 // static
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);
234 return widget;
237 // static
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;
244 // static
245 Widget* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window) {
246 internal::NativeWidgetPrivate* native_widget =
247 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
248 native_window);
249 return native_widget ? native_widget->GetWidget() : NULL;
252 // static
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;
260 // static
261 void Widget::GetAllChildWidgets(gfx::NativeView native_view,
262 Widgets* children) {
263 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view, children);
266 // static
267 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view,
268 Widgets* owned) {
269 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view, owned);
272 // static
273 void Widget::ReparentNativeView(gfx::NativeView native_view,
274 gfx::NativeView new_parent) {
275 internal::NativeWidgetPrivate::ReparentNativeView(native_view, new_parent);
278 // static
279 int Widget::GetLocalizedContentsWidth(int col_resource_id) {
280 return ui::GetLocalizedContentsWidthForFont(col_resource_id,
281 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
284 // static
285 int Widget::GetLocalizedContentsHeight(int row_resource_id) {
286 return ui::GetLocalizedContentsHeightForFont(row_resource_id,
287 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
290 // static
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));
297 // static
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(&params, 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) {
330 can_activate = true;
331 params.activatable = InitParams::ACTIVATABLE_YES;
332 } else {
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.
361 UpdateWindowTitle();
362 non_client_view_->ResetWindowControls();
363 SetInitialBounds(params.bounds);
364 if (params.show_state == ui::SHOW_STATE_MAXIMIZED)
365 Maximize();
366 else if (params.show_state == ui::SHOW_STATE_MINIMIZED)
367 Minimize();
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 {
413 return false;
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();
422 if (focus_manager)
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.
433 if (focus_manager)
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,
443 removals_observers_,
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
458 // focus manager.
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())
465 return;
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()) {
512 SetBounds(bounds);
513 } else {
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.
569 return;
572 bool can_close = true;
573 if (non_client_view_)
574 can_close = non_client_view_->CanClose();
575 if (can_close) {
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() &&
609 !IsFullscreen()) {
610 native_widget_->ShowMaximizedWithBounds(initial_restored_bounds_);
611 } else {
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;
619 } else {
620 CanActivate()
621 ? native_widget_->Show()
622 : native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
626 void Widget::Hide() {
627 native_widget_->Hide();
630 void Widget::ShowInactive() {
631 // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
632 // call SetBounds()with the restored bounds to set the correct size. This
633 // normally should not happen, but if it does we should avoid showing unsized
634 // windows.
635 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
636 !initial_restored_bounds_.IsEmpty()) {
637 SetBounds(initial_restored_bounds_);
638 saved_show_state_ = ui::SHOW_STATE_NORMAL;
640 native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
643 void Widget::Activate() {
644 native_widget_->Activate();
647 void Widget::Deactivate() {
648 native_widget_->Deactivate();
651 bool Widget::IsActive() const {
652 return native_widget_->IsActive();
655 void Widget::DisableInactiveRendering() {
656 SetInactiveRenderingDisabled(true);
659 void Widget::SetAlwaysOnTop(bool on_top) {
660 native_widget_->SetAlwaysOnTop(on_top);
663 bool Widget::IsAlwaysOnTop() const {
664 return native_widget_->IsAlwaysOnTop();
667 void Widget::SetVisibleOnAllWorkspaces(bool always_visible) {
668 native_widget_->SetVisibleOnAllWorkspaces(always_visible);
671 void Widget::Maximize() {
672 native_widget_->Maximize();
675 void Widget::Minimize() {
676 native_widget_->Minimize();
679 void Widget::Restore() {
680 native_widget_->Restore();
683 bool Widget::IsMaximized() const {
684 return native_widget_->IsMaximized();
687 bool Widget::IsMinimized() const {
688 return native_widget_->IsMinimized();
691 void Widget::SetFullscreen(bool fullscreen) {
692 if (IsFullscreen() == fullscreen)
693 return;
695 native_widget_->SetFullscreen(fullscreen);
697 if (non_client_view_)
698 non_client_view_->Layout();
701 bool Widget::IsFullscreen() const {
702 return native_widget_->IsFullscreen();
705 void Widget::SetOpacity(unsigned char opacity) {
706 native_widget_->SetOpacity(opacity);
709 void Widget::SetUseDragFrame(bool use_drag_frame) {
710 native_widget_->SetUseDragFrame(use_drag_frame);
713 void Widget::FlashFrame(bool flash) {
714 native_widget_->FlashFrame(flash);
717 View* Widget::GetRootView() {
718 return root_view_.get();
721 const View* Widget::GetRootView() const {
722 return root_view_.get();
725 bool Widget::IsVisible() const {
726 return native_widget_->IsVisible();
729 ui::ThemeProvider* Widget::GetThemeProvider() const {
730 const Widget* root_widget = GetTopLevelWidget();
731 if (root_widget && root_widget != this) {
732 // Attempt to get the theme provider, and fall back to the default theme
733 // provider if not found.
734 ui::ThemeProvider* provider = root_widget->GetThemeProvider();
735 if (provider)
736 return provider;
738 provider = root_widget->default_theme_provider_.get();
739 if (provider)
740 return provider;
742 return default_theme_provider_.get();
745 const ui::NativeTheme* Widget::GetNativeTheme() const {
746 return native_widget_->GetNativeTheme();
749 FocusManager* Widget::GetFocusManager() {
750 Widget* toplevel_widget = GetTopLevelWidget();
751 return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
754 const FocusManager* Widget::GetFocusManager() const {
755 const Widget* toplevel_widget = GetTopLevelWidget();
756 return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
759 InputMethod* Widget::GetInputMethod() {
760 return const_cast<InputMethod*>(
761 const_cast<const Widget*>(this)->GetInputMethod());
764 const InputMethod* Widget::GetInputMethod() const {
765 if (is_top_level()) {
766 if (!input_method_.get())
767 input_method_ = const_cast<Widget*>(this)->CreateInputMethod().Pass();
768 return input_method_.get();
769 } else {
770 const Widget* toplevel = GetTopLevelWidget();
771 // If GetTopLevelWidget() returns itself which is not toplevel,
772 // the widget is detached from toplevel widget.
773 // TODO(oshima): Fix GetTopLevelWidget() to return NULL
774 // if there is no toplevel. We probably need to add GetTopMostWidget()
775 // to replace some use cases.
776 return (toplevel && toplevel != this) ? toplevel->GetInputMethod() : NULL;
780 ui::InputMethod* Widget::GetHostInputMethod() {
781 return native_widget_private()->GetHostInputMethod();
784 void Widget::RunShellDrag(View* view,
785 const ui::OSExchangeData& data,
786 const gfx::Point& location,
787 int operation,
788 ui::DragDropTypes::DragEventSource source) {
789 dragged_view_ = view;
790 OnDragWillStart();
791 native_widget_->RunShellDrag(view, data, location, operation, source);
792 // If the view is removed during the drag operation, dragged_view_ is set to
793 // NULL.
794 if (view && dragged_view_ == view) {
795 dragged_view_ = NULL;
796 view->OnDragDone();
798 OnDragComplete();
801 void Widget::SchedulePaintInRect(const gfx::Rect& rect) {
802 native_widget_->SchedulePaintInRect(rect);
805 void Widget::SetCursor(gfx::NativeCursor cursor) {
806 native_widget_->SetCursor(cursor);
809 bool Widget::IsMouseEventsEnabled() const {
810 return native_widget_->IsMouseEventsEnabled();
813 void Widget::SetNativeWindowProperty(const char* name, void* value) {
814 native_widget_->SetNativeWindowProperty(name, value);
817 void* Widget::GetNativeWindowProperty(const char* name) const {
818 return native_widget_->GetNativeWindowProperty(name);
821 void Widget::UpdateWindowTitle() {
822 if (!non_client_view_)
823 return;
825 // Update the native frame's text. We do this regardless of whether or not
826 // the native frame is being used, since this also updates the taskbar, etc.
827 base::string16 window_title = widget_delegate_->GetWindowTitle();
828 base::i18n::AdjustStringForLocaleDirection(&window_title);
829 if (!native_widget_->SetWindowTitle(window_title))
830 return;
831 non_client_view_->UpdateWindowTitle();
833 // If the non-client view is rendering its own title, it'll need to relayout
834 // now and to get a paint update later on.
835 non_client_view_->Layout();
838 void Widget::UpdateWindowIcon() {
839 if (non_client_view_)
840 non_client_view_->UpdateWindowIcon();
841 native_widget_->SetWindowIcons(widget_delegate_->GetWindowIcon(),
842 widget_delegate_->GetWindowAppIcon());
845 FocusTraversable* Widget::GetFocusTraversable() {
846 return static_cast<internal::RootView*>(root_view_.get());
849 void Widget::ThemeChanged() {
850 root_view_->ThemeChanged();
853 void Widget::LocaleChanged() {
854 root_view_->LocaleChanged();
857 void Widget::SetFocusTraversableParent(FocusTraversable* parent) {
858 root_view_->SetFocusTraversableParent(parent);
861 void Widget::SetFocusTraversableParentView(View* parent_view) {
862 root_view_->SetFocusTraversableParentView(parent_view);
865 void Widget::ClearNativeFocus() {
866 native_widget_->ClearNativeFocus();
869 NonClientFrameView* Widget::CreateNonClientFrameView() {
870 NonClientFrameView* frame_view =
871 widget_delegate_->CreateNonClientFrameView(this);
872 if (!frame_view)
873 frame_view = native_widget_->CreateNonClientFrameView();
874 if (!frame_view && ViewsDelegate::views_delegate) {
875 frame_view =
876 ViewsDelegate::views_delegate->CreateDefaultNonClientFrameView(this);
878 if (frame_view)
879 return frame_view;
881 CustomFrameView* custom_frame_view = new CustomFrameView;
882 custom_frame_view->Init(this);
883 return custom_frame_view;
886 bool Widget::ShouldUseNativeFrame() const {
887 if (frame_type_ != FRAME_TYPE_DEFAULT)
888 return frame_type_ == FRAME_TYPE_FORCE_NATIVE;
889 return native_widget_->ShouldUseNativeFrame();
892 bool Widget::ShouldWindowContentsBeTransparent() const {
893 return native_widget_->ShouldWindowContentsBeTransparent();
896 void Widget::DebugToggleFrameType() {
897 if (frame_type_ == FRAME_TYPE_DEFAULT) {
898 frame_type_ = ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM :
899 FRAME_TYPE_FORCE_NATIVE;
900 } else {
901 frame_type_ = frame_type_ == FRAME_TYPE_FORCE_CUSTOM ?
902 FRAME_TYPE_FORCE_NATIVE : FRAME_TYPE_FORCE_CUSTOM;
904 FrameTypeChanged();
907 void Widget::FrameTypeChanged() {
908 native_widget_->FrameTypeChanged();
911 const ui::Compositor* Widget::GetCompositor() const {
912 return native_widget_->GetCompositor();
915 const ui::Layer* Widget::GetLayer() const {
916 return native_widget_->GetLayer();
919 void Widget::ReorderNativeViews() {
920 native_widget_->ReorderNativeViews();
923 void Widget::UpdateRootLayers() {
924 // Calculate the layers requires traversing the tree, and since nearly any
925 // mutation of the tree can trigger this call we delay until absolutely
926 // necessary.
927 root_layers_dirty_ = true;
930 const NativeWidget* Widget::native_widget() const {
931 return native_widget_;
934 NativeWidget* Widget::native_widget() {
935 return native_widget_;
938 void Widget::SetCapture(View* view) {
939 if (!native_widget_->HasCapture()) {
940 native_widget_->SetCapture();
942 // Early return if setting capture was unsuccessful.
943 if (!native_widget_->HasCapture())
944 return;
947 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
948 is_mouse_button_pressed_ = true;
949 root_view_->SetMouseHandler(view);
952 void Widget::ReleaseCapture() {
953 if (native_widget_->HasCapture())
954 native_widget_->ReleaseCapture();
957 bool Widget::HasCapture() {
958 return native_widget_->HasCapture();
961 TooltipManager* Widget::GetTooltipManager() {
962 return native_widget_->GetTooltipManager();
965 const TooltipManager* Widget::GetTooltipManager() const {
966 return native_widget_->GetTooltipManager();
969 gfx::Rect Widget::GetWorkAreaBoundsInScreen() const {
970 return native_widget_->GetWorkAreaBoundsInScreen();
973 void Widget::SynthesizeMouseMoveEvent() {
974 last_mouse_event_was_move_ = false;
975 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED,
976 last_mouse_event_position_,
977 last_mouse_event_position_,
978 ui::EF_IS_SYNTHESIZED, 0);
979 root_view_->OnMouseMoved(mouse_event);
982 void Widget::OnRootViewLayout() {
983 native_widget_->OnRootViewLayout();
986 bool Widget::IsTranslucentWindowOpacitySupported() const {
987 return native_widget_->IsTranslucentWindowOpacitySupported();
990 void Widget::OnSizeConstraintsChanged() {
991 native_widget_->OnSizeConstraintsChanged();
992 non_client_view_->SizeConstraintsChanged();
995 void Widget::OnOwnerClosing() {
998 ////////////////////////////////////////////////////////////////////////////////
999 // Widget, NativeWidgetDelegate implementation:
1001 bool Widget::IsModal() const {
1002 return widget_delegate_->GetModalType() != ui::MODAL_TYPE_NONE;
1005 bool Widget::IsDialogBox() const {
1006 return !!widget_delegate_->AsDialogDelegate();
1009 bool Widget::CanActivate() const {
1010 return widget_delegate_->CanActivate();
1013 bool Widget::IsInactiveRenderingDisabled() const {
1014 return disable_inactive_rendering_;
1017 void Widget::EnableInactiveRendering() {
1018 SetInactiveRenderingDisabled(false);
1021 void Widget::OnNativeWidgetActivationChanged(bool active) {
1022 // On windows we may end up here before we've completed initialization (from
1023 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1024 // the Widget and will crash attempting to access it.
1025 if (!active && native_widget_initialized_)
1026 SaveWindowPlacement();
1028 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1029 OnWidgetActivationChanged(this, active));
1031 // During window creation, the widget gets focused without activation, and in
1032 // that case, the focus manager cannot set the appropriate text input client
1033 // because the widget is not active. Thus we have to notify the focus manager
1034 // not only when the focus changes but also when the widget gets activated.
1035 // See crbug.com/377479 for details.
1036 views::FocusManager* focus_manager = GetFocusManager();
1037 if (focus_manager) {
1038 if (active)
1039 focus_manager->FocusTextInputClient(focus_manager->GetFocusedView());
1040 else
1041 focus_manager->BlurTextInputClient(focus_manager->GetFocusedView());
1044 if (IsVisible() && non_client_view())
1045 non_client_view()->frame_view()->SchedulePaint();
1048 void Widget::OnNativeFocus(gfx::NativeView old_focused_view) {
1049 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(old_focused_view,
1050 GetNativeView());
1053 void Widget::OnNativeBlur(gfx::NativeView new_focused_view) {
1054 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(GetNativeView(),
1055 new_focused_view);
1058 void Widget::OnNativeWidgetVisibilityChanging(bool visible) {
1059 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1060 OnWidgetVisibilityChanging(this, visible));
1063 void Widget::OnNativeWidgetVisibilityChanged(bool visible) {
1064 View* root = GetRootView();
1065 if (root)
1066 root->PropagateVisibilityNotifications(root, visible);
1067 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1068 OnWidgetVisibilityChanged(this, visible));
1069 if (GetCompositor() && root && root->layer())
1070 root->layer()->SetVisible(visible);
1073 void Widget::OnNativeWidgetCreated(bool desktop_widget) {
1074 if (is_top_level())
1075 focus_manager_.reset(FocusManagerFactory::Create(this, desktop_widget));
1077 native_widget_->InitModalType(widget_delegate_->GetModalType());
1079 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetCreated(this));
1082 void Widget::OnNativeWidgetDestroying() {
1083 // Tell the focus manager (if any) that root_view is being removed
1084 // in case that the focused view is under this root view.
1085 if (GetFocusManager())
1086 GetFocusManager()->ViewRemoved(root_view_.get());
1087 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroying(this));
1088 if (non_client_view_)
1089 non_client_view_->WindowClosing();
1090 widget_delegate_->WindowClosing();
1093 void Widget::OnNativeWidgetDestroyed() {
1094 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroyed(this));
1095 widget_delegate_->DeleteDelegate();
1096 widget_delegate_ = NULL;
1097 native_widget_destroyed_ = true;
1100 gfx::Size Widget::GetMinimumSize() const {
1101 return non_client_view_ ? non_client_view_->GetMinimumSize() : gfx::Size();
1104 gfx::Size Widget::GetMaximumSize() const {
1105 return non_client_view_ ? non_client_view_->GetMaximumSize() : gfx::Size();
1108 void Widget::OnNativeWidgetMove() {
1109 widget_delegate_->OnWidgetMove();
1110 View* root = GetRootView();
1111 if (root && root->GetFocusManager()) {
1112 View* focused_view = root->GetFocusManager()->GetFocusedView();
1113 if (focused_view && focused_view->GetInputMethod())
1114 focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1116 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1117 this,
1118 GetWindowBoundsInScreen()));
1121 void Widget::OnNativeWidgetSizeChanged(const gfx::Size& new_size) {
1122 View* root = GetRootView();
1123 if (root) {
1124 root->SetSize(new_size);
1125 if (root->GetFocusManager()) {
1126 View* focused_view = GetRootView()->GetFocusManager()->GetFocusedView();
1127 if (focused_view && focused_view->GetInputMethod())
1128 focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1131 SaveWindowPlacementIfInitialized();
1133 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1134 this,
1135 GetWindowBoundsInScreen()));
1138 void Widget::OnNativeWidgetWindowShowStateChanged() {
1139 SaveWindowPlacementIfInitialized();
1142 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1143 widget_delegate_->OnWindowBeginUserBoundsChange();
1146 void Widget::OnNativeWidgetEndUserBoundsChange() {
1147 widget_delegate_->OnWindowEndUserBoundsChange();
1150 bool Widget::HasFocusManager() const {
1151 return !!focus_manager_.get();
1154 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect& dirty_region) {
1155 ui::Compositor* compositor = GetCompositor();
1156 if (!compositor)
1157 return false;
1159 compositor->ScheduleRedrawRect(dirty_region);
1160 return true;
1163 void Widget::OnNativeWidgetPaint(gfx::Canvas* canvas) {
1164 // On Linux Aura, we can get here during Init() because of the
1165 // SetInitialBounds call.
1166 if (native_widget_initialized_)
1167 GetRootView()->Paint(canvas, CullSet());
1170 int Widget::GetNonClientComponent(const gfx::Point& point) {
1171 int component = non_client_view_ ?
1172 non_client_view_->NonClientHitTest(point) :
1173 HTNOWHERE;
1175 if (movement_disabled_ && (component == HTCAPTION || component == HTSYSMENU))
1176 return HTNOWHERE;
1178 return component;
1181 void Widget::OnKeyEvent(ui::KeyEvent* event) {
1182 SendEventToProcessor(event);
1185 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1186 // RootView from anywhere in Widget. Use
1187 // SendEventToProcessor() instead. See crbug.com/348087.
1188 void Widget::OnMouseEvent(ui::MouseEvent* event) {
1189 View* root_view = GetRootView();
1190 switch (event->type()) {
1191 case ui::ET_MOUSE_PRESSED: {
1192 last_mouse_event_was_move_ = false;
1194 // We may get deleted by the time we return from OnMousePressed. So we
1195 // use an observer to make sure we are still alive.
1196 WidgetDeletionObserver widget_deletion_observer(this);
1198 // Make sure we're still visible before we attempt capture as the mouse
1199 // press processing may have made the window hide (as happens with menus).
1201 // It is possible for a View to show a context menu on mouse-press. Since
1202 // the menu does a capture and starts a nested message-loop, the release
1203 // would go to the menu. The next click (i.e. both mouse-press and release
1204 // events) also go to the menu. The menu (and the nested message-loop)
1205 // gets closed after this second release event. The code then resumes from
1206 // here. So make sure that the mouse-button is still down before doing a
1207 // capture.
1208 if (root_view && root_view->OnMousePressed(*event) &&
1209 widget_deletion_observer.IsWidgetAlive() && IsVisible() &&
1210 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1211 is_mouse_button_pressed_ = true;
1212 if (!native_widget_->HasCapture())
1213 native_widget_->SetCapture();
1214 event->SetHandled();
1216 return;
1219 case ui::ET_MOUSE_RELEASED:
1220 last_mouse_event_was_move_ = false;
1221 is_mouse_button_pressed_ = false;
1222 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1223 if (auto_release_capture_ && native_widget_->HasCapture()) {
1224 base::AutoReset<bool> resetter(&ignore_capture_loss_, true);
1225 native_widget_->ReleaseCapture();
1227 if (root_view)
1228 root_view->OnMouseReleased(*event);
1229 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0)
1230 event->SetHandled();
1231 return;
1233 case ui::ET_MOUSE_MOVED:
1234 case ui::ET_MOUSE_DRAGGED:
1235 if (native_widget_->HasCapture() && is_mouse_button_pressed_) {
1236 last_mouse_event_was_move_ = false;
1237 if (root_view)
1238 root_view->OnMouseDragged(*event);
1239 } else if (!last_mouse_event_was_move_ ||
1240 last_mouse_event_position_ != event->location()) {
1241 last_mouse_event_position_ = event->location();
1242 last_mouse_event_was_move_ = true;
1243 if (root_view)
1244 root_view->OnMouseMoved(*event);
1246 return;
1248 case ui::ET_MOUSE_EXITED:
1249 last_mouse_event_was_move_ = false;
1250 if (root_view)
1251 root_view->OnMouseExited(*event);
1252 return;
1254 case ui::ET_MOUSEWHEEL:
1255 if (root_view && root_view->OnMouseWheel(
1256 static_cast<const ui::MouseWheelEvent&>(*event)))
1257 event->SetHandled();
1258 return;
1260 default:
1261 return;
1265 void Widget::OnMouseCaptureLost() {
1266 if (ignore_capture_loss_)
1267 return;
1269 View* root_view = GetRootView();
1270 if (root_view)
1271 root_view->OnMouseCaptureLost();
1272 is_mouse_button_pressed_ = false;
1275 void Widget::OnScrollEvent(ui::ScrollEvent* event) {
1276 ui::ScrollEvent event_copy(*event);
1277 SendEventToProcessor(&event_copy);
1279 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1280 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) {
1281 ui::MouseWheelEvent wheel(*event);
1282 OnMouseEvent(&wheel);
1286 void Widget::OnGestureEvent(ui::GestureEvent* event) {
1287 // We explicitly do not capture here. Not capturing enables multiple widgets
1288 // to get tap events at the same time. Views (such as tab dragging) may
1289 // explicitly capture.
1290 SendEventToProcessor(event);
1293 bool Widget::ExecuteCommand(int command_id) {
1294 return widget_delegate_->ExecuteWindowsCommand(command_id);
1297 InputMethod* Widget::GetInputMethodDirect() {
1298 return input_method_.get();
1301 const std::vector<ui::Layer*>& Widget::GetRootLayers() {
1302 if (root_layers_dirty_) {
1303 root_layers_dirty_ = false;
1304 root_layers_.clear();
1305 BuildRootLayers(GetRootView(), &root_layers_);
1307 return root_layers_;
1310 bool Widget::HasHitTestMask() const {
1311 return widget_delegate_->WidgetHasHitTestMask();
1314 void Widget::GetHitTestMask(gfx::Path* mask) const {
1315 DCHECK(mask);
1316 widget_delegate_->GetWidgetHitTestMask(mask);
1319 Widget* Widget::AsWidget() {
1320 return this;
1323 const Widget* Widget::AsWidget() const {
1324 return this;
1327 bool Widget::SetInitialFocus(ui::WindowShowState show_state) {
1328 View* v = widget_delegate_->GetInitiallyFocusedView();
1329 if (!focus_on_creation_ || show_state == ui::SHOW_STATE_INACTIVE ||
1330 show_state == ui::SHOW_STATE_MINIMIZED) {
1331 // If not focusing the window now, tell the focus manager which view to
1332 // focus when the window is restored.
1333 if (v)
1334 focus_manager_->SetStoredFocusView(v);
1335 return true;
1337 if (v)
1338 v->RequestFocus();
1339 return !!v;
1342 ////////////////////////////////////////////////////////////////////////////////
1343 // Widget, ui::EventSource implementation:
1344 ui::EventProcessor* Widget::GetEventProcessor() {
1345 return root_view_.get();
1348 ////////////////////////////////////////////////////////////////////////////////
1349 // Widget, FocusTraversable implementation:
1351 FocusSearch* Widget::GetFocusSearch() {
1352 return root_view_->GetFocusSearch();
1355 FocusTraversable* Widget::GetFocusTraversableParent() {
1356 // We are a proxy to the root view, so we should be bypassed when traversing
1357 // up and as a result this should not be called.
1358 NOTREACHED();
1359 return NULL;
1362 View* Widget::GetFocusTraversableParentView() {
1363 // We are a proxy to the root view, so we should be bypassed when traversing
1364 // up and as a result this should not be called.
1365 NOTREACHED();
1366 return NULL;
1369 ////////////////////////////////////////////////////////////////////////////////
1370 // Widget, ui::NativeThemeObserver implementation:
1372 void Widget::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
1373 DCHECK(observer_manager_.IsObserving(observed_theme));
1375 ui::NativeTheme* current_native_theme = GetNativeTheme();
1376 if (!observer_manager_.IsObserving(current_native_theme)) {
1377 observer_manager_.RemoveAll();
1378 observer_manager_.Add(current_native_theme);
1381 root_view_->PropagateNativeThemeChanged(current_native_theme);
1384 ////////////////////////////////////////////////////////////////////////////////
1385 // Widget, protected:
1387 internal::RootView* Widget::CreateRootView() {
1388 return new internal::RootView(this);
1391 void Widget::DestroyRootView() {
1392 non_client_view_ = NULL;
1393 root_view_.reset();
1394 // Input method has to be destroyed before focus manager.
1395 input_method_.reset();
1398 void Widget::OnDragWillStart() {
1401 void Widget::OnDragComplete() {
1404 ////////////////////////////////////////////////////////////////////////////////
1405 // Widget, private:
1407 void Widget::SetInactiveRenderingDisabled(bool value) {
1408 if (value == disable_inactive_rendering_)
1409 return;
1411 disable_inactive_rendering_ = value;
1412 if (non_client_view_)
1413 non_client_view_->SetInactiveRenderingDisabled(value);
1416 void Widget::SaveWindowPlacement() {
1417 // The window delegate does the actual saving for us. It seems like (judging
1418 // by go/crash) that in some circumstances we can end up here after
1419 // WM_DESTROY, at which point the window delegate is likely gone. So just
1420 // bail.
1421 if (!widget_delegate_)
1422 return;
1424 ui::WindowShowState show_state = ui::SHOW_STATE_NORMAL;
1425 gfx::Rect bounds;
1426 native_widget_->GetWindowPlacement(&bounds, &show_state);
1427 widget_delegate_->SaveWindowPlacement(bounds, show_state);
1430 void Widget::SaveWindowPlacementIfInitialized() {
1431 if (native_widget_initialized_)
1432 SaveWindowPlacement();
1435 void Widget::SetInitialBounds(const gfx::Rect& bounds) {
1436 if (!non_client_view_)
1437 return;
1439 gfx::Rect saved_bounds;
1440 if (GetSavedWindowPlacement(&saved_bounds, &saved_show_state_)) {
1441 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED) {
1442 // If we're going to maximize, wait until Show is invoked to set the
1443 // bounds. That way we avoid a noticeable resize.
1444 initial_restored_bounds_ = saved_bounds;
1445 } else if (!saved_bounds.IsEmpty()) {
1446 // If the saved bounds are valid, use them.
1447 SetBounds(saved_bounds);
1449 } else {
1450 if (bounds.IsEmpty()) {
1451 // No initial bounds supplied, so size the window to its content and
1452 // center over its parent.
1453 native_widget_->CenterWindow(non_client_view_->GetPreferredSize());
1454 } else {
1455 // Use the supplied initial bounds.
1456 SetBoundsConstrained(bounds);
1461 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds) {
1462 if (bounds.IsEmpty()) {
1463 View* contents_view = GetContentsView();
1464 DCHECK(contents_view);
1465 // No initial bounds supplied, so size the window to its content and
1466 // center over its parent if preferred size is provided.
1467 gfx::Size size = contents_view->GetPreferredSize();
1468 if (!size.IsEmpty())
1469 native_widget_->CenterWindow(size);
1470 } else {
1471 // Use the supplied initial bounds.
1472 SetBoundsConstrained(bounds);
1476 bool Widget::GetSavedWindowPlacement(gfx::Rect* bounds,
1477 ui::WindowShowState* show_state) {
1478 // First we obtain the window's saved show-style and store it. We need to do
1479 // this here, rather than in Show() because by the time Show() is called,
1480 // the window's size will have been reset (below) and the saved maximized
1481 // state will have been lost. Sadly there's no way to tell on Windows when
1482 // a window is restored from maximized state, so we can't more accurately
1483 // track maximized state independently of sizing information.
1485 // Restore the window's placement from the controller.
1486 if (widget_delegate_->GetSavedWindowPlacement(this, bounds, show_state)) {
1487 if (!widget_delegate_->ShouldRestoreWindowSize()) {
1488 bounds->set_size(non_client_view_->GetPreferredSize());
1489 } else {
1490 gfx::Size minimum_size = GetMinimumSize();
1491 // Make sure the bounds are at least the minimum size.
1492 if (bounds->width() < minimum_size.width())
1493 bounds->set_width(minimum_size.width());
1495 if (bounds->height() < minimum_size.height())
1496 bounds->set_height(minimum_size.height());
1498 return true;
1500 return false;
1503 scoped_ptr<InputMethod> Widget::CreateInputMethod() {
1504 scoped_ptr<InputMethod> input_method(native_widget_->CreateInputMethod());
1505 if (input_method.get())
1506 input_method->Init(this);
1507 return input_method.Pass();
1510 void Widget::ReplaceInputMethod(InputMethod* input_method) {
1511 input_method_.reset(input_method);
1512 input_method->SetDelegate(native_widget_->GetInputMethodDelegate());
1513 input_method->Init(this);
1516 namespace internal {
1518 ////////////////////////////////////////////////////////////////////////////////
1519 // internal::NativeWidgetPrivate, NativeWidget implementation:
1521 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1522 return this;
1525 } // namespace internal
1526 } // namespace views