Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / widget / widget.cc
blobd4b16bb769a39863a114f6d8f2104972f99eeb83
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 virtual ~DefaultWidgetDelegate() {}
76 // Overridden from WidgetDelegate:
77 virtual void DeleteDelegate() OVERRIDE {
78 delete this;
80 virtual Widget* GetWidget() OVERRIDE {
81 return widget_;
83 virtual const Widget* GetWidget() const OVERRIDE {
84 return widget_;
86 virtual bool ShouldAdvanceFocusToTopLevelWidget() const OVERRIDE {
87 // In most situations where a Widget is used without a delegate the Widget
88 // is used as a container, so that we want focus to advance to the top-level
89 // widget. A good example of this is the find bar.
90 return true;
93 private:
94 Widget* widget_;
96 DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate);
99 ////////////////////////////////////////////////////////////////////////////////
100 // Widget, InitParams:
102 Widget::InitParams::InitParams()
103 : type(TYPE_WINDOW),
104 delegate(NULL),
105 child(false),
106 opacity(INFER_OPACITY),
107 accept_events(true),
108 activatable(ACTIVATABLE_DEFAULT),
109 keep_on_top(false),
110 visible_on_all_workspaces(false),
111 ownership(NATIVE_WIDGET_OWNS_WIDGET),
112 mirror_origin_in_rtl(false),
113 shadow_type(SHADOW_TYPE_DEFAULT),
114 remove_standard_frame(false),
115 use_system_default_icon(false),
116 show_state(ui::SHOW_STATE_DEFAULT),
117 double_buffer(false),
118 parent(NULL),
119 native_widget(NULL),
120 desktop_window_tree_host(NULL),
121 layer_type(aura::WINDOW_LAYER_TEXTURED),
122 context(NULL),
123 force_show_in_taskbar(false) {
126 Widget::InitParams::InitParams(Type type)
127 : type(type),
128 delegate(NULL),
129 child(false),
130 opacity(INFER_OPACITY),
131 accept_events(true),
132 activatable(ACTIVATABLE_DEFAULT),
133 keep_on_top(type == TYPE_MENU || type == TYPE_DRAG),
134 visible_on_all_workspaces(false),
135 ownership(NATIVE_WIDGET_OWNS_WIDGET),
136 mirror_origin_in_rtl(false),
137 shadow_type(SHADOW_TYPE_DEFAULT),
138 remove_standard_frame(false),
139 use_system_default_icon(false),
140 show_state(ui::SHOW_STATE_DEFAULT),
141 double_buffer(false),
142 parent(NULL),
143 native_widget(NULL),
144 desktop_window_tree_host(NULL),
145 layer_type(aura::WINDOW_LAYER_TEXTURED),
146 context(NULL),
147 force_show_in_taskbar(false) {
150 Widget::InitParams::~InitParams() {
153 ////////////////////////////////////////////////////////////////////////////////
154 // Widget, public:
156 Widget::Widget()
157 : native_widget_(NULL),
158 widget_delegate_(NULL),
159 non_client_view_(NULL),
160 dragged_view_(NULL),
161 ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET),
162 is_secondary_widget_(true),
163 frame_type_(FRAME_TYPE_DEFAULT),
164 disable_inactive_rendering_(false),
165 widget_closed_(false),
166 saved_show_state_(ui::SHOW_STATE_DEFAULT),
167 focus_on_creation_(true),
168 is_top_level_(false),
169 native_widget_initialized_(false),
170 native_widget_destroyed_(false),
171 is_mouse_button_pressed_(false),
172 ignore_capture_loss_(false),
173 last_mouse_event_was_move_(false),
174 auto_release_capture_(true),
175 root_layers_dirty_(false),
176 movement_disabled_(false),
177 observer_manager_(this) {
180 Widget::~Widget() {
181 DestroyRootView();
182 if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) {
183 delete native_widget_;
184 } else {
185 DCHECK(native_widget_destroyed_)
186 << "Destroying a widget with a live native widget. "
187 << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
191 // static
192 Widget* Widget::CreateWindow(WidgetDelegate* delegate) {
193 return CreateWindowWithBounds(delegate, gfx::Rect());
196 // static
197 Widget* Widget::CreateWindowWithBounds(WidgetDelegate* delegate,
198 const gfx::Rect& bounds) {
199 Widget* widget = new Widget;
200 Widget::InitParams params;
201 params.bounds = bounds;
202 params.delegate = delegate;
203 widget->Init(params);
204 return widget;
207 // static
208 Widget* Widget::CreateWindowWithParent(WidgetDelegate* delegate,
209 gfx::NativeView parent) {
210 return CreateWindowWithParentAndBounds(delegate, parent, gfx::Rect());
213 // static
214 Widget* Widget::CreateWindowWithParentAndBounds(WidgetDelegate* delegate,
215 gfx::NativeView parent,
216 const gfx::Rect& bounds) {
217 Widget* widget = new Widget;
218 Widget::InitParams params;
219 params.delegate = delegate;
220 params.parent = parent;
221 params.bounds = bounds;
222 widget->Init(params);
223 return widget;
226 // static
227 Widget* Widget::CreateWindowWithContext(WidgetDelegate* delegate,
228 gfx::NativeWindow context) {
229 return CreateWindowWithContextAndBounds(delegate, context, gfx::Rect());
232 // static
233 Widget* Widget::CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
234 gfx::NativeWindow context,
235 const gfx::Rect& bounds) {
236 Widget* widget = new Widget;
237 Widget::InitParams params;
238 params.delegate = delegate;
239 params.context = context;
240 params.bounds = bounds;
241 widget->Init(params);
242 return widget;
245 // static
246 Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) {
247 internal::NativeWidgetPrivate* native_widget =
248 internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view);
249 return native_widget ? native_widget->GetWidget() : NULL;
252 // static
253 Widget* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window) {
254 internal::NativeWidgetPrivate* native_widget =
255 internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
256 native_window);
257 return native_widget ? native_widget->GetWidget() : NULL;
260 // static
261 Widget* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view) {
262 internal::NativeWidgetPrivate* native_widget =
263 internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view);
264 return native_widget ? native_widget->GetWidget() : NULL;
268 // static
269 void Widget::GetAllChildWidgets(gfx::NativeView native_view,
270 Widgets* children) {
271 internal::NativeWidgetPrivate::GetAllChildWidgets(native_view, children);
274 // static
275 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view,
276 Widgets* owned) {
277 internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view, owned);
280 // static
281 void Widget::ReparentNativeView(gfx::NativeView native_view,
282 gfx::NativeView new_parent) {
283 internal::NativeWidgetPrivate::ReparentNativeView(native_view, new_parent);
286 // static
287 int Widget::GetLocalizedContentsWidth(int col_resource_id) {
288 return ui::GetLocalizedContentsWidthForFont(col_resource_id,
289 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
292 // static
293 int Widget::GetLocalizedContentsHeight(int row_resource_id) {
294 return ui::GetLocalizedContentsHeightForFont(row_resource_id,
295 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
298 // static
299 gfx::Size Widget::GetLocalizedContentsSize(int col_resource_id,
300 int row_resource_id) {
301 return gfx::Size(GetLocalizedContentsWidth(col_resource_id),
302 GetLocalizedContentsHeight(row_resource_id));
305 // static
306 bool Widget::RequiresNonClientView(InitParams::Type type) {
307 return type == InitParams::TYPE_WINDOW ||
308 type == InitParams::TYPE_PANEL ||
309 type == InitParams::TYPE_BUBBLE;
312 void Widget::Init(const InitParams& in_params) {
313 TRACE_EVENT0("views", "Widget::Init");
314 InitParams params = in_params;
316 params.child |= (params.type == InitParams::TYPE_CONTROL);
317 is_top_level_ = !params.child;
319 if (params.opacity == views::Widget::InitParams::INFER_OPACITY &&
320 params.type != views::Widget::InitParams::TYPE_WINDOW &&
321 params.type != views::Widget::InitParams::TYPE_PANEL)
322 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
324 if (ViewsDelegate::views_delegate)
325 ViewsDelegate::views_delegate->OnBeforeWidgetInit(&params, this);
327 if (params.opacity == views::Widget::InitParams::INFER_OPACITY)
328 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
330 bool can_activate = false;
331 if (params.activatable != InitParams::ACTIVATABLE_DEFAULT) {
332 can_activate = (params.activatable == InitParams::ACTIVATABLE_YES);
333 } else if (params.type != InitParams::TYPE_CONTROL &&
334 params.type != InitParams::TYPE_POPUP &&
335 params.type != InitParams::TYPE_MENU &&
336 params.type != InitParams::TYPE_TOOLTIP &&
337 params.type != InitParams::TYPE_DRAG) {
338 can_activate = true;
339 params.activatable = InitParams::ACTIVATABLE_YES;
340 } else {
341 can_activate = false;
342 params.activatable = InitParams::ACTIVATABLE_NO;
345 widget_delegate_ = params.delegate ?
346 params.delegate : new DefaultWidgetDelegate(this);
347 widget_delegate_->set_can_activate(can_activate);
349 ownership_ = params.ownership;
350 native_widget_ = CreateNativeWidget(params.native_widget, this)->
351 AsNativeWidgetPrivate();
352 root_view_.reset(CreateRootView());
353 default_theme_provider_.reset(new ui::DefaultThemeProvider);
354 if (params.type == InitParams::TYPE_MENU) {
355 is_mouse_button_pressed_ =
356 internal::NativeWidgetPrivate::IsMouseButtonDown();
358 native_widget_->InitNativeWidget(params);
359 if (RequiresNonClientView(params.type)) {
360 non_client_view_ = new NonClientView;
361 non_client_view_->SetFrameView(CreateNonClientFrameView());
362 // Create the ClientView, add it to the NonClientView and add the
363 // NonClientView to the RootView. This will cause everything to be parented.
364 non_client_view_->set_client_view(widget_delegate_->CreateClientView(this));
365 non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView());
366 SetContentsView(non_client_view_);
367 // Initialize the window's title before setting the window's initial bounds;
368 // the frame view's preferred height may depend on the presence of a title.
369 UpdateWindowTitle();
370 non_client_view_->ResetWindowControls();
371 SetInitialBounds(params.bounds);
372 if (params.show_state == ui::SHOW_STATE_MAXIMIZED)
373 Maximize();
374 else if (params.show_state == ui::SHOW_STATE_MINIMIZED)
375 Minimize();
376 } else if (params.delegate) {
377 SetContentsView(params.delegate->GetContentsView());
378 SetInitialBoundsForFramelessWindow(params.bounds);
380 // This must come after SetContentsView() or it might not be able to find
381 // the correct NativeTheme (on Linux). See http://crbug.com/384492
382 observer_manager_.Add(GetNativeTheme());
383 native_widget_initialized_ = true;
386 // Unconverted methods (see header) --------------------------------------------
388 gfx::NativeView Widget::GetNativeView() const {
389 return native_widget_->GetNativeView();
392 gfx::NativeWindow Widget::GetNativeWindow() const {
393 return native_widget_->GetNativeWindow();
396 void Widget::AddObserver(WidgetObserver* observer) {
397 observers_.AddObserver(observer);
400 void Widget::RemoveObserver(WidgetObserver* observer) {
401 observers_.RemoveObserver(observer);
404 bool Widget::HasObserver(WidgetObserver* observer) {
405 return observers_.HasObserver(observer);
408 void Widget::AddRemovalsObserver(WidgetRemovalsObserver* observer) {
409 removals_observers_.AddObserver(observer);
412 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver* observer) {
413 removals_observers_.RemoveObserver(observer);
416 bool Widget::HasRemovalsObserver(WidgetRemovalsObserver* observer) {
417 return removals_observers_.HasObserver(observer);
420 bool Widget::GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const {
421 return false;
424 void Widget::ViewHierarchyChanged(
425 const View::ViewHierarchyChangedDetails& details) {
426 if (!details.is_add) {
427 if (details.child == dragged_view_)
428 dragged_view_ = NULL;
429 FocusManager* focus_manager = GetFocusManager();
430 if (focus_manager)
431 focus_manager->ViewRemoved(details.child);
432 ViewStorage::GetInstance()->ViewRemoved(details.child);
433 native_widget_->ViewRemoved(details.child);
437 void Widget::NotifyNativeViewHierarchyWillChange() {
438 FocusManager* focus_manager = GetFocusManager();
439 // We are being removed from a window hierarchy. Treat this as
440 // the root_view_ being removed.
441 if (focus_manager)
442 focus_manager->ViewRemoved(root_view_.get());
445 void Widget::NotifyNativeViewHierarchyChanged() {
446 root_view_->NotifyNativeViewHierarchyChanged();
449 void Widget::NotifyWillRemoveView(View* view) {
450 FOR_EACH_OBSERVER(WidgetRemovalsObserver,
451 removals_observers_,
452 OnWillRemoveView(this, view));
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
466 // focus manager.
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())
473 return;
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()) {
520 SetBounds(bounds);
521 } else {
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.
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 ui::Compositor* Widget::GetCompositor() {
916 return native_widget_->GetCompositor();
919 ui::Layer* Widget::GetLayer() {
920 return native_widget_->GetLayer();
923 void Widget::ReorderNativeViews() {
924 native_widget_->ReorderNativeViews();
927 void Widget::UpdateRootLayers() {
928 // Calculate the layers requires traversing the tree, and since nearly any
929 // mutation of the tree can trigger this call we delay until absolutely
930 // necessary.
931 root_layers_dirty_ = true;
934 const NativeWidget* Widget::native_widget() const {
935 return native_widget_;
938 NativeWidget* Widget::native_widget() {
939 return native_widget_;
942 void Widget::SetCapture(View* view) {
943 if (!native_widget_->HasCapture()) {
944 native_widget_->SetCapture();
946 // Early return if setting capture was unsuccessful.
947 if (!native_widget_->HasCapture())
948 return;
951 if (internal::NativeWidgetPrivate::IsMouseButtonDown())
952 is_mouse_button_pressed_ = true;
953 root_view_->SetMouseHandler(view);
956 void Widget::ReleaseCapture() {
957 if (native_widget_->HasCapture())
958 native_widget_->ReleaseCapture();
961 bool Widget::HasCapture() {
962 return native_widget_->HasCapture();
965 TooltipManager* Widget::GetTooltipManager() {
966 return native_widget_->GetTooltipManager();
969 const TooltipManager* Widget::GetTooltipManager() const {
970 return native_widget_->GetTooltipManager();
973 gfx::Rect Widget::GetWorkAreaBoundsInScreen() const {
974 return native_widget_->GetWorkAreaBoundsInScreen();
977 void Widget::SynthesizeMouseMoveEvent() {
978 last_mouse_event_was_move_ = false;
979 ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED,
980 last_mouse_event_position_,
981 last_mouse_event_position_,
982 ui::EF_IS_SYNTHESIZED, 0);
983 root_view_->OnMouseMoved(mouse_event);
986 void Widget::OnRootViewLayout() {
987 native_widget_->OnRootViewLayout();
990 bool Widget::IsTranslucentWindowOpacitySupported() const {
991 return native_widget_->IsTranslucentWindowOpacitySupported();
994 void Widget::OnOwnerClosing() {
997 ////////////////////////////////////////////////////////////////////////////////
998 // Widget, NativeWidgetDelegate implementation:
1000 bool Widget::IsModal() const {
1001 return widget_delegate_->GetModalType() != ui::MODAL_TYPE_NONE;
1004 bool Widget::IsDialogBox() const {
1005 return !!widget_delegate_->AsDialogDelegate();
1008 bool Widget::CanActivate() const {
1009 return widget_delegate_->CanActivate();
1012 bool Widget::IsInactiveRenderingDisabled() const {
1013 return disable_inactive_rendering_;
1016 void Widget::EnableInactiveRendering() {
1017 SetInactiveRenderingDisabled(false);
1020 void Widget::OnNativeWidgetActivationChanged(bool active) {
1021 // On windows we may end up here before we've completed initialization (from
1022 // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1023 // the Widget and will crash attempting to access it.
1024 if (!active && native_widget_initialized_)
1025 SaveWindowPlacement();
1027 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1028 OnWidgetActivationChanged(this, active));
1030 // During window creation, the widget gets focused without activation, and in
1031 // that case, the focus manager cannot set the appropriate text input client
1032 // because the widget is not active. Thus we have to notify the focus manager
1033 // not only when the focus changes but also when the widget gets activated.
1034 // See crbug.com/377479 for details.
1035 views::FocusManager* focus_manager = GetFocusManager();
1036 if (focus_manager) {
1037 if (active)
1038 focus_manager->FocusTextInputClient(focus_manager->GetFocusedView());
1039 else
1040 focus_manager->BlurTextInputClient(focus_manager->GetFocusedView());
1043 if (IsVisible() && non_client_view())
1044 non_client_view()->frame_view()->SchedulePaint();
1047 void Widget::OnNativeFocus(gfx::NativeView old_focused_view) {
1048 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(old_focused_view,
1049 GetNativeView());
1052 void Widget::OnNativeBlur(gfx::NativeView new_focused_view) {
1053 WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(GetNativeView(),
1054 new_focused_view);
1057 void Widget::OnNativeWidgetVisibilityChanging(bool visible) {
1058 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1059 OnWidgetVisibilityChanging(this, visible));
1062 void Widget::OnNativeWidgetVisibilityChanged(bool visible) {
1063 View* root = GetRootView();
1064 if (root)
1065 root->PropagateVisibilityNotifications(root, visible);
1066 FOR_EACH_OBSERVER(WidgetObserver, observers_,
1067 OnWidgetVisibilityChanged(this, visible));
1068 if (GetCompositor() && root && root->layer())
1069 root->layer()->SetVisible(visible);
1072 void Widget::OnNativeWidgetCreated(bool desktop_widget) {
1073 if (is_top_level())
1074 focus_manager_.reset(FocusManagerFactory::Create(this, desktop_widget));
1076 native_widget_->InitModalType(widget_delegate_->GetModalType());
1078 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetCreated(this));
1081 void Widget::OnNativeWidgetDestroying() {
1082 // Tell the focus manager (if any) that root_view is being removed
1083 // in case that the focused view is under this root view.
1084 if (GetFocusManager())
1085 GetFocusManager()->ViewRemoved(root_view_.get());
1086 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroying(this));
1087 if (non_client_view_)
1088 non_client_view_->WindowClosing();
1089 widget_delegate_->WindowClosing();
1092 void Widget::OnNativeWidgetDestroyed() {
1093 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroyed(this));
1094 widget_delegate_->DeleteDelegate();
1095 widget_delegate_ = NULL;
1096 native_widget_destroyed_ = true;
1099 gfx::Size Widget::GetMinimumSize() const {
1100 return non_client_view_ ? non_client_view_->GetMinimumSize() : gfx::Size();
1103 gfx::Size Widget::GetMaximumSize() const {
1104 return non_client_view_ ? non_client_view_->GetMaximumSize() : gfx::Size();
1107 void Widget::OnNativeWidgetMove() {
1108 widget_delegate_->OnWidgetMove();
1109 View* root = GetRootView();
1110 if (root && root->GetFocusManager()) {
1111 View* focused_view = root->GetFocusManager()->GetFocusedView();
1112 if (focused_view && focused_view->GetInputMethod())
1113 focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1115 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1116 this,
1117 GetWindowBoundsInScreen()));
1120 void Widget::OnNativeWidgetSizeChanged(const gfx::Size& new_size) {
1121 View* root = GetRootView();
1122 if (root) {
1123 root->SetSize(new_size);
1124 if (root->GetFocusManager()) {
1125 View* focused_view = GetRootView()->GetFocusManager()->GetFocusedView();
1126 if (focused_view && focused_view->GetInputMethod())
1127 focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1130 SaveWindowPlacementIfInitialized();
1132 FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1133 this,
1134 GetWindowBoundsInScreen()));
1137 void Widget::OnNativeWidgetWindowShowStateChanged() {
1138 SaveWindowPlacementIfInitialized();
1141 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1142 widget_delegate_->OnWindowBeginUserBoundsChange();
1145 void Widget::OnNativeWidgetEndUserBoundsChange() {
1146 widget_delegate_->OnWindowEndUserBoundsChange();
1149 bool Widget::HasFocusManager() const {
1150 return !!focus_manager_.get();
1153 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect& dirty_region) {
1154 ui::Compositor* compositor = GetCompositor();
1155 if (!compositor)
1156 return false;
1158 compositor->ScheduleRedrawRect(dirty_region);
1159 return true;
1162 void Widget::OnNativeWidgetPaint(gfx::Canvas* canvas) {
1163 // On Linux Aura, we can get here during Init() because of the
1164 // SetInitialBounds call.
1165 if (native_widget_initialized_)
1166 GetRootView()->Paint(canvas, CullSet());
1169 int Widget::GetNonClientComponent(const gfx::Point& point) {
1170 int component = non_client_view_ ?
1171 non_client_view_->NonClientHitTest(point) :
1172 HTNOWHERE;
1174 if (movement_disabled_ && (component == HTCAPTION || component == HTSYSMENU))
1175 return HTNOWHERE;
1177 return component;
1180 void Widget::OnKeyEvent(ui::KeyEvent* event) {
1181 SendEventToProcessor(event);
1184 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1185 // RootView from anywhere in Widget. Use
1186 // SendEventToProcessor() instead. See crbug.com/348087.
1187 void Widget::OnMouseEvent(ui::MouseEvent* event) {
1188 View* root_view = GetRootView();
1189 switch (event->type()) {
1190 case ui::ET_MOUSE_PRESSED: {
1191 last_mouse_event_was_move_ = false;
1193 // We may get deleted by the time we return from OnMousePressed. So we
1194 // use an observer to make sure we are still alive.
1195 WidgetDeletionObserver widget_deletion_observer(this);
1197 // Make sure we're still visible before we attempt capture as the mouse
1198 // press processing may have made the window hide (as happens with menus).
1200 // It is possible for a View to show a context menu on mouse-press. Since
1201 // the menu does a capture and starts a nested message-loop, the release
1202 // would go to the menu. The next click (i.e. both mouse-press and release
1203 // events) also go to the menu. The menu (and the nested message-loop)
1204 // gets closed after this second release event. The code then resumes from
1205 // here. So make sure that the mouse-button is still down before doing a
1206 // capture.
1207 if (root_view && root_view->OnMousePressed(*event) &&
1208 widget_deletion_observer.IsWidgetAlive() && IsVisible() &&
1209 internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1210 is_mouse_button_pressed_ = true;
1211 if (!native_widget_->HasCapture())
1212 native_widget_->SetCapture();
1213 event->SetHandled();
1215 return;
1218 case ui::ET_MOUSE_RELEASED:
1219 last_mouse_event_was_move_ = false;
1220 is_mouse_button_pressed_ = false;
1221 // Release capture first, to avoid confusion if OnMouseReleased blocks.
1222 if (auto_release_capture_ && native_widget_->HasCapture()) {
1223 base::AutoReset<bool> resetter(&ignore_capture_loss_, true);
1224 native_widget_->ReleaseCapture();
1226 if (root_view)
1227 root_view->OnMouseReleased(*event);
1228 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0)
1229 event->SetHandled();
1230 return;
1232 case ui::ET_MOUSE_MOVED:
1233 case ui::ET_MOUSE_DRAGGED:
1234 if (native_widget_->HasCapture() && is_mouse_button_pressed_) {
1235 last_mouse_event_was_move_ = false;
1236 if (root_view)
1237 root_view->OnMouseDragged(*event);
1238 } else if (!last_mouse_event_was_move_ ||
1239 last_mouse_event_position_ != event->location()) {
1240 last_mouse_event_position_ = event->location();
1241 last_mouse_event_was_move_ = true;
1242 if (root_view)
1243 root_view->OnMouseMoved(*event);
1245 return;
1247 case ui::ET_MOUSE_EXITED:
1248 last_mouse_event_was_move_ = false;
1249 if (root_view)
1250 root_view->OnMouseExited(*event);
1251 return;
1253 case ui::ET_MOUSEWHEEL:
1254 if (root_view && root_view->OnMouseWheel(
1255 static_cast<const ui::MouseWheelEvent&>(*event)))
1256 event->SetHandled();
1257 return;
1259 default:
1260 return;
1264 void Widget::OnMouseCaptureLost() {
1265 if (ignore_capture_loss_)
1266 return;
1268 View* root_view = GetRootView();
1269 if (root_view)
1270 root_view->OnMouseCaptureLost();
1271 is_mouse_button_pressed_ = false;
1274 void Widget::OnScrollEvent(ui::ScrollEvent* event) {
1275 ui::ScrollEvent event_copy(*event);
1276 SendEventToProcessor(&event_copy);
1278 // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1279 if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) {
1280 ui::MouseWheelEvent wheel(*event);
1281 OnMouseEvent(&wheel);
1285 void Widget::OnGestureEvent(ui::GestureEvent* event) {
1286 // We explicitly do not capture here. Not capturing enables multiple widgets
1287 // to get tap events at the same time. Views (such as tab dragging) may
1288 // explicitly capture.
1289 SendEventToProcessor(event);
1292 bool Widget::ExecuteCommand(int command_id) {
1293 return widget_delegate_->ExecuteWindowsCommand(command_id);
1296 InputMethod* Widget::GetInputMethodDirect() {
1297 return input_method_.get();
1300 const std::vector<ui::Layer*>& Widget::GetRootLayers() {
1301 if (root_layers_dirty_) {
1302 root_layers_dirty_ = false;
1303 root_layers_.clear();
1304 BuildRootLayers(GetRootView(), &root_layers_);
1306 return root_layers_;
1309 bool Widget::HasHitTestMask() const {
1310 return widget_delegate_->WidgetHasHitTestMask();
1313 void Widget::GetHitTestMask(gfx::Path* mask) const {
1314 DCHECK(mask);
1315 widget_delegate_->GetWidgetHitTestMask(mask);
1318 Widget* Widget::AsWidget() {
1319 return this;
1322 const Widget* Widget::AsWidget() const {
1323 return this;
1326 bool Widget::SetInitialFocus(ui::WindowShowState show_state) {
1327 View* v = widget_delegate_->GetInitiallyFocusedView();
1328 if (!focus_on_creation_ || show_state == ui::SHOW_STATE_INACTIVE ||
1329 show_state == ui::SHOW_STATE_MINIMIZED) {
1330 // If not focusing the window now, tell the focus manager which view to
1331 // focus when the window is restored.
1332 if (v)
1333 focus_manager_->SetStoredFocusView(v);
1334 return true;
1336 if (v)
1337 v->RequestFocus();
1338 return !!v;
1341 ////////////////////////////////////////////////////////////////////////////////
1342 // Widget, ui::EventSource implementation:
1343 ui::EventProcessor* Widget::GetEventProcessor() {
1344 return root_view_.get();
1347 ////////////////////////////////////////////////////////////////////////////////
1348 // Widget, FocusTraversable implementation:
1350 FocusSearch* Widget::GetFocusSearch() {
1351 return root_view_->GetFocusSearch();
1354 FocusTraversable* Widget::GetFocusTraversableParent() {
1355 // We are a proxy to the root view, so we should be bypassed when traversing
1356 // up and as a result this should not be called.
1357 NOTREACHED();
1358 return NULL;
1361 View* Widget::GetFocusTraversableParentView() {
1362 // We are a proxy to the root view, so we should be bypassed when traversing
1363 // up and as a result this should not be called.
1364 NOTREACHED();
1365 return NULL;
1368 ////////////////////////////////////////////////////////////////////////////////
1369 // Widget, ui::NativeThemeObserver implementation:
1371 void Widget::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
1372 DCHECK(observer_manager_.IsObserving(observed_theme));
1374 ui::NativeTheme* current_native_theme = GetNativeTheme();
1375 if (!observer_manager_.IsObserving(current_native_theme)) {
1376 observer_manager_.RemoveAll();
1377 observer_manager_.Add(current_native_theme);
1380 root_view_->PropagateNativeThemeChanged(current_native_theme);
1383 ////////////////////////////////////////////////////////////////////////////////
1384 // Widget, protected:
1386 internal::RootView* Widget::CreateRootView() {
1387 return new internal::RootView(this);
1390 void Widget::DestroyRootView() {
1391 non_client_view_ = NULL;
1392 root_view_.reset();
1393 // Input method has to be destroyed before focus manager.
1394 input_method_.reset();
1397 void Widget::OnDragWillStart() {
1400 void Widget::OnDragComplete() {
1403 ////////////////////////////////////////////////////////////////////////////////
1404 // Widget, private:
1406 void Widget::SetInactiveRenderingDisabled(bool value) {
1407 if (value == disable_inactive_rendering_)
1408 return;
1410 disable_inactive_rendering_ = value;
1411 if (non_client_view_)
1412 non_client_view_->SetInactiveRenderingDisabled(value);
1415 void Widget::SaveWindowPlacement() {
1416 // The window delegate does the actual saving for us. It seems like (judging
1417 // by go/crash) that in some circumstances we can end up here after
1418 // WM_DESTROY, at which point the window delegate is likely gone. So just
1419 // bail.
1420 if (!widget_delegate_)
1421 return;
1423 ui::WindowShowState show_state = ui::SHOW_STATE_NORMAL;
1424 gfx::Rect bounds;
1425 native_widget_->GetWindowPlacement(&bounds, &show_state);
1426 widget_delegate_->SaveWindowPlacement(bounds, show_state);
1429 void Widget::SaveWindowPlacementIfInitialized() {
1430 if (native_widget_initialized_)
1431 SaveWindowPlacement();
1434 void Widget::SetInitialBounds(const gfx::Rect& bounds) {
1435 if (!non_client_view_)
1436 return;
1438 gfx::Rect saved_bounds;
1439 if (GetSavedWindowPlacement(&saved_bounds, &saved_show_state_)) {
1440 if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED) {
1441 // If we're going to maximize, wait until Show is invoked to set the
1442 // bounds. That way we avoid a noticeable resize.
1443 initial_restored_bounds_ = saved_bounds;
1444 } else if (!saved_bounds.IsEmpty()) {
1445 // If the saved bounds are valid, use them.
1446 SetBounds(saved_bounds);
1448 } else {
1449 if (bounds.IsEmpty()) {
1450 // No initial bounds supplied, so size the window to its content and
1451 // center over its parent.
1452 native_widget_->CenterWindow(non_client_view_->GetPreferredSize());
1453 } else {
1454 // Use the supplied initial bounds.
1455 SetBoundsConstrained(bounds);
1460 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds) {
1461 if (bounds.IsEmpty()) {
1462 View* contents_view = GetContentsView();
1463 DCHECK(contents_view);
1464 // No initial bounds supplied, so size the window to its content and
1465 // center over its parent if preferred size is provided.
1466 gfx::Size size = contents_view->GetPreferredSize();
1467 if (!size.IsEmpty())
1468 native_widget_->CenterWindow(size);
1469 } else {
1470 // Use the supplied initial bounds.
1471 SetBoundsConstrained(bounds);
1475 bool Widget::GetSavedWindowPlacement(gfx::Rect* bounds,
1476 ui::WindowShowState* show_state) {
1477 // First we obtain the window's saved show-style and store it. We need to do
1478 // this here, rather than in Show() because by the time Show() is called,
1479 // the window's size will have been reset (below) and the saved maximized
1480 // state will have been lost. Sadly there's no way to tell on Windows when
1481 // a window is restored from maximized state, so we can't more accurately
1482 // track maximized state independently of sizing information.
1484 // Restore the window's placement from the controller.
1485 if (widget_delegate_->GetSavedWindowPlacement(this, bounds, show_state)) {
1486 if (!widget_delegate_->ShouldRestoreWindowSize()) {
1487 bounds->set_size(non_client_view_->GetPreferredSize());
1488 } else {
1489 gfx::Size minimum_size = GetMinimumSize();
1490 // Make sure the bounds are at least the minimum size.
1491 if (bounds->width() < minimum_size.width())
1492 bounds->set_width(minimum_size.width());
1494 if (bounds->height() < minimum_size.height())
1495 bounds->set_height(minimum_size.height());
1497 return true;
1499 return false;
1502 scoped_ptr<InputMethod> Widget::CreateInputMethod() {
1503 scoped_ptr<InputMethod> input_method(native_widget_->CreateInputMethod());
1504 if (input_method.get())
1505 input_method->Init(this);
1506 return input_method.Pass();
1509 void Widget::ReplaceInputMethod(InputMethod* input_method) {
1510 input_method_.reset(input_method);
1511 input_method->SetDelegate(native_widget_->GetInputMethodDelegate());
1512 input_method->Init(this);
1515 namespace internal {
1517 ////////////////////////////////////////////////////////////////////////////////
1518 // internal::NativeWidgetPrivate, NativeWidget implementation:
1520 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1521 return this;
1524 } // namespace internal
1525 } // namespace views