Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / ui / views / widget / widget.cc
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.
4
5 #include "ui/views/widget/widget.h"
6
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"
37
38 namespace views {
39
40 namespace {
41
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);
51   }
52 }
53
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);
62   }
63   return native_widget;
64 }
65
66 }  // namespace
67
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) {
73   }
74   virtual ~DefaultWidgetDelegate() {}
75
76   // Overridden from WidgetDelegate:
77   virtual void DeleteDelegate() OVERRIDE {
78     delete this;
79   }
80   virtual Widget* GetWidget() OVERRIDE {
81     return widget_;
82   }
83   virtual const Widget* GetWidget() const OVERRIDE {
84     return widget_;
85   }
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;
91   }
92
93  private:
94   Widget* widget_;
95
96   DISALLOW_COPY_AND_ASSIGN(DefaultWidgetDelegate);
97 };
98
99 ////////////////////////////////////////////////////////////////////////////////
100 // Widget, InitParams:
101
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),
124       net_wm_pid(0) {
125 }
126
127 Widget::InitParams::InitParams(Type type)
128     : type(type),
129       delegate(NULL),
130       child(false),
131       opacity(INFER_OPACITY),
132       accept_events(true),
133       activatable(ACTIVATABLE_DEFAULT),
134       keep_on_top(type == TYPE_MENU || type == TYPE_DRAG),
135       visible_on_all_workspaces(false),
136       ownership(NATIVE_WIDGET_OWNS_WIDGET),
137       mirror_origin_in_rtl(false),
138       shadow_type(SHADOW_TYPE_DEFAULT),
139       remove_standard_frame(false),
140       use_system_default_icon(false),
141       show_state(ui::SHOW_STATE_DEFAULT),
142       double_buffer(false),
143       parent(NULL),
144       native_widget(NULL),
145       desktop_window_tree_host(NULL),
146       layer_type(aura::WINDOW_LAYER_TEXTURED),
147       context(NULL),
148       force_show_in_taskbar(false),
149       net_wm_pid(0) {
150 }
151
152 Widget::InitParams::~InitParams() {
153 }
154
155 ////////////////////////////////////////////////////////////////////////////////
156 // Widget, public:
157
158 Widget::Widget()
159     : native_widget_(NULL),
160       widget_delegate_(NULL),
161       non_client_view_(NULL),
162       dragged_view_(NULL),
163       ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET),
164       is_secondary_widget_(true),
165       frame_type_(FRAME_TYPE_DEFAULT),
166       disable_inactive_rendering_(false),
167       widget_closed_(false),
168       saved_show_state_(ui::SHOW_STATE_DEFAULT),
169       focus_on_creation_(true),
170       is_top_level_(false),
171       native_widget_initialized_(false),
172       native_widget_destroyed_(false),
173       is_mouse_button_pressed_(false),
174       is_touch_down_(false),
175       last_mouse_event_was_move_(false),
176       auto_release_capture_(true),
177       root_layers_dirty_(false),
178       movement_disabled_(false),
179       observer_manager_(this) {
180 }
181
182 Widget::~Widget() {
183   DestroyRootView();
184   if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET) {
185     delete native_widget_;
186   } else {
187     DCHECK(native_widget_destroyed_)
188         << "Destroying a widget with a live native widget. "
189         << "Widget probably should use WIDGET_OWNS_NATIVE_WIDGET ownership.";
190   }
191 }
192
193 // static
194 Widget* Widget::CreateWindow(WidgetDelegate* delegate) {
195   return CreateWindowWithBounds(delegate, gfx::Rect());
196 }
197
198 // static
199 Widget* Widget::CreateWindowWithBounds(WidgetDelegate* delegate,
200                                        const gfx::Rect& bounds) {
201   Widget* widget = new Widget;
202   Widget::InitParams params;
203   params.bounds = bounds;
204   params.delegate = delegate;
205   widget->Init(params);
206   return widget;
207 }
208
209 // static
210 Widget* Widget::CreateWindowWithParent(WidgetDelegate* delegate,
211                                        gfx::NativeView parent) {
212   return CreateWindowWithParentAndBounds(delegate, parent, gfx::Rect());
213 }
214
215 // static
216 Widget* Widget::CreateWindowWithParentAndBounds(WidgetDelegate* delegate,
217                                                 gfx::NativeView parent,
218                                                 const gfx::Rect& bounds) {
219   Widget* widget = new Widget;
220   Widget::InitParams params;
221   params.delegate = delegate;
222   params.parent = parent;
223   params.bounds = bounds;
224   widget->Init(params);
225   return widget;
226 }
227
228 // static
229 Widget* Widget::CreateWindowWithContext(WidgetDelegate* delegate,
230                                         gfx::NativeView context) {
231   return CreateWindowWithContextAndBounds(delegate, context, gfx::Rect());
232 }
233
234 // static
235 Widget* Widget::CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
236                                                  gfx::NativeView context,
237                                                  const gfx::Rect& bounds) {
238   Widget* widget = new Widget;
239   Widget::InitParams params;
240   params.delegate = delegate;
241   params.context = context;
242   params.bounds = bounds;
243   widget->Init(params);
244   return widget;
245 }
246
247 // static
248 Widget* Widget::GetWidgetForNativeView(gfx::NativeView native_view) {
249   internal::NativeWidgetPrivate* native_widget =
250       internal::NativeWidgetPrivate::GetNativeWidgetForNativeView(native_view);
251   return native_widget ? native_widget->GetWidget() : NULL;
252 }
253
254 // static
255 Widget* Widget::GetWidgetForNativeWindow(gfx::NativeWindow native_window) {
256   internal::NativeWidgetPrivate* native_widget =
257       internal::NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
258           native_window);
259   return native_widget ? native_widget->GetWidget() : NULL;
260 }
261
262 // static
263 Widget* Widget::GetTopLevelWidgetForNativeView(gfx::NativeView native_view) {
264   internal::NativeWidgetPrivate* native_widget =
265       internal::NativeWidgetPrivate::GetTopLevelNativeWidget(native_view);
266   return native_widget ? native_widget->GetWidget() : NULL;
267 }
268
269
270 // static
271 void Widget::GetAllChildWidgets(gfx::NativeView native_view,
272                                 Widgets* children) {
273   internal::NativeWidgetPrivate::GetAllChildWidgets(native_view, children);
274 }
275
276 // static
277 void Widget::GetAllOwnedWidgets(gfx::NativeView native_view,
278                                 Widgets* owned) {
279   internal::NativeWidgetPrivate::GetAllOwnedWidgets(native_view, owned);
280 }
281
282 // static
283 void Widget::ReparentNativeView(gfx::NativeView native_view,
284                                 gfx::NativeView new_parent) {
285   internal::NativeWidgetPrivate::ReparentNativeView(native_view, new_parent);
286 }
287
288 // static
289 int Widget::GetLocalizedContentsWidth(int col_resource_id) {
290   return ui::GetLocalizedContentsWidthForFont(col_resource_id,
291       ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
292 }
293
294 // static
295 int Widget::GetLocalizedContentsHeight(int row_resource_id) {
296   return ui::GetLocalizedContentsHeightForFont(row_resource_id,
297       ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont));
298 }
299
300 // static
301 gfx::Size Widget::GetLocalizedContentsSize(int col_resource_id,
302                                            int row_resource_id) {
303   return gfx::Size(GetLocalizedContentsWidth(col_resource_id),
304                    GetLocalizedContentsHeight(row_resource_id));
305 }
306
307 // static
308 bool Widget::RequiresNonClientView(InitParams::Type type) {
309   return type == InitParams::TYPE_WINDOW ||
310          type == InitParams::TYPE_PANEL ||
311          type == InitParams::TYPE_BUBBLE;
312 }
313
314 void Widget::Init(const InitParams& in_params) {
315   TRACE_EVENT0("views", "Widget::Init");
316   InitParams params = in_params;
317
318   params.child |= (params.type == InitParams::TYPE_CONTROL);
319   is_top_level_ = !params.child;
320
321   if (params.opacity == views::Widget::InitParams::INFER_OPACITY &&
322       params.type != views::Widget::InitParams::TYPE_WINDOW &&
323       params.type != views::Widget::InitParams::TYPE_PANEL)
324     params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
325
326   if (ViewsDelegate::views_delegate)
327     ViewsDelegate::views_delegate->OnBeforeWidgetInit(&params, this);
328
329   if (params.opacity == views::Widget::InitParams::INFER_OPACITY)
330     params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
331
332   bool can_activate = false;
333   if (params.activatable != InitParams::ACTIVATABLE_DEFAULT) {
334     can_activate = (params.activatable == InitParams::ACTIVATABLE_YES);
335   } else if (params.type != InitParams::TYPE_CONTROL &&
336              params.type != InitParams::TYPE_POPUP &&
337              params.type != InitParams::TYPE_MENU &&
338              params.type != InitParams::TYPE_TOOLTIP &&
339              params.type != InitParams::TYPE_DRAG) {
340     can_activate = true;
341     params.activatable = InitParams::ACTIVATABLE_YES;
342   } else {
343     can_activate = false;
344     params.activatable = InitParams::ACTIVATABLE_NO;
345   }
346
347   widget_delegate_ = params.delegate ?
348       params.delegate : new DefaultWidgetDelegate(this);
349   widget_delegate_->set_can_activate(can_activate);
350
351   ownership_ = params.ownership;
352   native_widget_ = CreateNativeWidget(params.native_widget, this)->
353                    AsNativeWidgetPrivate();
354   root_view_.reset(CreateRootView());
355   default_theme_provider_.reset(new ui::DefaultThemeProvider);
356   if (params.type == InitParams::TYPE_MENU) {
357     is_mouse_button_pressed_ =
358         internal::NativeWidgetPrivate::IsMouseButtonDown();
359   }
360   native_widget_->InitNativeWidget(params);
361   if (RequiresNonClientView(params.type)) {
362     non_client_view_ = new NonClientView;
363     non_client_view_->SetFrameView(CreateNonClientFrameView());
364     // Create the ClientView, add it to the NonClientView and add the
365     // NonClientView to the RootView. This will cause everything to be parented.
366     non_client_view_->set_client_view(widget_delegate_->CreateClientView(this));
367     non_client_view_->SetOverlayView(widget_delegate_->CreateOverlayView());
368     SetContentsView(non_client_view_);
369     // Initialize the window's title before setting the window's initial bounds;
370     // the frame view's preferred height may depend on the presence of a title.
371     UpdateWindowTitle();
372     non_client_view_->ResetWindowControls();
373     SetInitialBounds(params.bounds);
374     if (params.show_state == ui::SHOW_STATE_MAXIMIZED)
375       Maximize();
376     else if (params.show_state == ui::SHOW_STATE_MINIMIZED)
377       Minimize();
378   } else if (params.delegate) {
379     SetContentsView(params.delegate->GetContentsView());
380     SetInitialBoundsForFramelessWindow(params.bounds);
381   }
382   // This must come after SetContentsView() or it might not be able to find
383   // the correct NativeTheme (on Linux). See http://crbug.com/384492
384   observer_manager_.Add(GetNativeTheme());
385   native_widget_initialized_ = true;
386 }
387
388 // Unconverted methods (see header) --------------------------------------------
389
390 gfx::NativeView Widget::GetNativeView() const {
391   return native_widget_->GetNativeView();
392 }
393
394 gfx::NativeWindow Widget::GetNativeWindow() const {
395   return native_widget_->GetNativeWindow();
396 }
397
398 void Widget::AddObserver(WidgetObserver* observer) {
399   observers_.AddObserver(observer);
400 }
401
402 void Widget::RemoveObserver(WidgetObserver* observer) {
403   observers_.RemoveObserver(observer);
404 }
405
406 bool Widget::HasObserver(WidgetObserver* observer) {
407   return observers_.HasObserver(observer);
408 }
409
410 void Widget::AddRemovalsObserver(WidgetRemovalsObserver* observer) {
411   removals_observers_.AddObserver(observer);
412 }
413
414 void Widget::RemoveRemovalsObserver(WidgetRemovalsObserver* observer) {
415   removals_observers_.RemoveObserver(observer);
416 }
417
418 bool Widget::HasRemovalsObserver(WidgetRemovalsObserver* observer) {
419   return removals_observers_.HasObserver(observer);
420 }
421
422 bool Widget::GetAccelerator(int cmd_id, ui::Accelerator* accelerator) const {
423   return false;
424 }
425
426 void Widget::ViewHierarchyChanged(
427     const View::ViewHierarchyChangedDetails& details) {
428   if (!details.is_add) {
429     if (details.child == dragged_view_)
430       dragged_view_ = NULL;
431     FocusManager* focus_manager = GetFocusManager();
432     if (focus_manager)
433       focus_manager->ViewRemoved(details.child);
434     ViewStorage::GetInstance()->ViewRemoved(details.child);
435     native_widget_->ViewRemoved(details.child);
436   }
437 }
438
439 void Widget::NotifyNativeViewHierarchyWillChange() {
440   FocusManager* focus_manager = GetFocusManager();
441   // We are being removed from a window hierarchy.  Treat this as
442   // the root_view_ being removed.
443   if (focus_manager)
444     focus_manager->ViewRemoved(root_view_.get());
445 }
446
447 void Widget::NotifyNativeViewHierarchyChanged() {
448   root_view_->NotifyNativeViewHierarchyChanged();
449 }
450
451 void Widget::NotifyWillRemoveView(View* view) {
452     FOR_EACH_OBSERVER(WidgetRemovalsObserver,
453                       removals_observers_,
454                       OnWillRemoveView(this, view));
455 }
456
457 // Converted methods (see header) ----------------------------------------------
458
459 Widget* Widget::GetTopLevelWidget() {
460   return const_cast<Widget*>(
461       static_cast<const Widget*>(this)->GetTopLevelWidget());
462 }
463
464 const Widget* Widget::GetTopLevelWidget() const {
465   // GetTopLevelNativeWidget doesn't work during destruction because
466   // property is gone after gobject gets deleted. Short circuit here
467   // for toplevel so that InputMethod can remove itself from
468   // focus manager.
469   return is_top_level() ? this : native_widget_->GetTopLevelWidget();
470 }
471
472 void Widget::SetContentsView(View* view) {
473   // Do not SetContentsView() again if it is already set to the same view.
474   if (view == GetContentsView())
475     return;
476   root_view_->SetContentsView(view);
477   if (non_client_view_ != view) {
478     // |non_client_view_| can only be non-NULL here if RequiresNonClientView()
479     // was true when the widget was initialized. Creating widgets with non
480     // client views and then setting the contents view can cause subtle
481     // problems on Windows, where the native widget thinks there is still a
482     // |non_client_view_|. If you get this error, either use a different type
483     // when initializing the widget, or don't call SetContentsView().
484     DCHECK(!non_client_view_);
485     non_client_view_ = NULL;
486   }
487 }
488
489 View* Widget::GetContentsView() {
490   return root_view_->GetContentsView();
491 }
492
493 gfx::Rect Widget::GetWindowBoundsInScreen() const {
494   return native_widget_->GetWindowBoundsInScreen();
495 }
496
497 gfx::Rect Widget::GetClientAreaBoundsInScreen() const {
498   return native_widget_->GetClientAreaBoundsInScreen();
499 }
500
501 gfx::Rect Widget::GetRestoredBounds() const {
502   return native_widget_->GetRestoredBounds();
503 }
504
505 void Widget::SetBounds(const gfx::Rect& bounds) {
506   native_widget_->SetBounds(bounds);
507 }
508
509 void Widget::SetSize(const gfx::Size& size) {
510   native_widget_->SetSize(size);
511 }
512
513 void Widget::CenterWindow(const gfx::Size& size) {
514   native_widget_->CenterWindow(size);
515 }
516
517 void Widget::SetBoundsConstrained(const gfx::Rect& bounds) {
518   gfx::Rect work_area =
519       gfx::Screen::GetScreenFor(GetNativeView())->GetDisplayNearestPoint(
520           bounds.origin()).work_area();
521   if (work_area.IsEmpty()) {
522     SetBounds(bounds);
523   } else {
524     // Inset the work area slightly.
525     work_area.Inset(10, 10, 10, 10);
526     work_area.AdjustToFit(bounds);
527     SetBounds(work_area);
528   }
529 }
530
531 void Widget::SetVisibilityChangedAnimationsEnabled(bool value) {
532   native_widget_->SetVisibilityChangedAnimationsEnabled(value);
533 }
534
535 Widget::MoveLoopResult Widget::RunMoveLoop(
536     const gfx::Vector2d& drag_offset,
537     MoveLoopSource source,
538     MoveLoopEscapeBehavior escape_behavior) {
539   return native_widget_->RunMoveLoop(drag_offset, source, escape_behavior);
540 }
541
542 void Widget::EndMoveLoop() {
543   native_widget_->EndMoveLoop();
544 }
545
546 void Widget::StackAboveWidget(Widget* widget) {
547   native_widget_->StackAbove(widget->GetNativeView());
548 }
549
550 void Widget::StackAbove(gfx::NativeView native_view) {
551   native_widget_->StackAbove(native_view);
552 }
553
554 void Widget::StackAtTop() {
555   native_widget_->StackAtTop();
556 }
557
558 void Widget::StackBelow(gfx::NativeView native_view) {
559   native_widget_->StackBelow(native_view);
560 }
561
562 void Widget::SetShape(gfx::NativeRegion shape) {
563   native_widget_->SetShape(shape);
564 }
565
566 void Widget::Close() {
567   if (widget_closed_) {
568     // It appears we can hit this code path if you close a modal dialog then
569     // close the last browser before the destructor is hit, which triggers
570     // invoking Close again.
571     return;
572   }
573
574   bool can_close = true;
575   if (non_client_view_)
576     can_close = non_client_view_->CanClose();
577   if (can_close) {
578     SaveWindowPlacement();
579
580     // During tear-down the top-level focus manager becomes unavailable to
581     // GTK tabbed panes and their children, so normal deregistration via
582     // |FormManager::ViewRemoved()| calls are fouled.  We clear focus here
583     // to avoid these redundant steps and to avoid accessing deleted views
584     // that may have been in focus.
585     if (is_top_level() && focus_manager_.get())
586       focus_manager_->SetFocusedView(NULL);
587
588     FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetClosing(this));
589     native_widget_->Close();
590     widget_closed_ = true;
591   }
592 }
593
594 void Widget::CloseNow() {
595   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetClosing(this));
596   native_widget_->CloseNow();
597 }
598
599 bool Widget::IsClosed() const {
600   return widget_closed_;
601 }
602
603 void Widget::Show() {
604   TRACE_EVENT0("views", "Widget::Show");
605   if (non_client_view_) {
606     // While initializing, the kiosk mode will go to full screen before the
607     // widget gets shown. In that case we stay in full screen mode, regardless
608     // of the |saved_show_state_| member.
609     if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
610         !initial_restored_bounds_.IsEmpty() &&
611         !IsFullscreen()) {
612       native_widget_->ShowMaximizedWithBounds(initial_restored_bounds_);
613     } else {
614       native_widget_->ShowWithWindowState(
615           IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN : saved_show_state_);
616     }
617     // |saved_show_state_| only applies the first time the window is shown.
618     // If we don't reset the value the window may be shown maximized every time
619     // it is subsequently shown after being hidden.
620     saved_show_state_ = ui::SHOW_STATE_NORMAL;
621   } else {
622     CanActivate()
623         ? native_widget_->Show()
624         : native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
625   }
626 }
627
628 void Widget::Hide() {
629   native_widget_->Hide();
630 }
631
632 void Widget::ShowInactive() {
633   // If this gets called with saved_show_state_ == ui::SHOW_STATE_MAXIMIZED,
634   // call SetBounds()with the restored bounds to set the correct size. This
635   // normally should not happen, but if it does we should avoid showing unsized
636   // windows.
637   if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED &&
638       !initial_restored_bounds_.IsEmpty()) {
639     SetBounds(initial_restored_bounds_);
640     saved_show_state_ = ui::SHOW_STATE_NORMAL;
641   }
642   native_widget_->ShowWithWindowState(ui::SHOW_STATE_INACTIVE);
643 }
644
645 void Widget::Activate() {
646   native_widget_->Activate();
647 }
648
649 void Widget::Deactivate() {
650   native_widget_->Deactivate();
651 }
652
653 bool Widget::IsActive() const {
654   return native_widget_->IsActive();
655 }
656
657 void Widget::DisableInactiveRendering() {
658   SetInactiveRenderingDisabled(true);
659 }
660
661 void Widget::SetAlwaysOnTop(bool on_top) {
662   native_widget_->SetAlwaysOnTop(on_top);
663 }
664
665 bool Widget::IsAlwaysOnTop() const {
666   return native_widget_->IsAlwaysOnTop();
667 }
668
669 void Widget::SetVisibleOnAllWorkspaces(bool always_visible) {
670   native_widget_->SetVisibleOnAllWorkspaces(always_visible);
671 }
672
673 void Widget::Maximize() {
674   native_widget_->Maximize();
675 }
676
677 void Widget::Minimize() {
678   native_widget_->Minimize();
679 }
680
681 void Widget::Restore() {
682   native_widget_->Restore();
683 }
684
685 bool Widget::IsMaximized() const {
686   return native_widget_->IsMaximized();
687 }
688
689 bool Widget::IsMinimized() const {
690   return native_widget_->IsMinimized();
691 }
692
693 void Widget::SetFullscreen(bool fullscreen) {
694   if (IsFullscreen() == fullscreen)
695     return;
696
697   native_widget_->SetFullscreen(fullscreen);
698
699   if (non_client_view_)
700     non_client_view_->Layout();
701 }
702
703 bool Widget::IsFullscreen() const {
704   return native_widget_->IsFullscreen();
705 }
706
707 void Widget::SetOpacity(unsigned char opacity) {
708   native_widget_->SetOpacity(opacity);
709 }
710
711 void Widget::SetUseDragFrame(bool use_drag_frame) {
712   native_widget_->SetUseDragFrame(use_drag_frame);
713 }
714
715 void Widget::FlashFrame(bool flash) {
716   native_widget_->FlashFrame(flash);
717 }
718
719 View* Widget::GetRootView() {
720   return root_view_.get();
721 }
722
723 const View* Widget::GetRootView() const {
724   return root_view_.get();
725 }
726
727 bool Widget::IsVisible() const {
728   return native_widget_->IsVisible();
729 }
730
731 ui::ThemeProvider* Widget::GetThemeProvider() const {
732   const Widget* root_widget = GetTopLevelWidget();
733   if (root_widget && root_widget != this) {
734     // Attempt to get the theme provider, and fall back to the default theme
735     // provider if not found.
736     ui::ThemeProvider* provider = root_widget->GetThemeProvider();
737     if (provider)
738       return provider;
739
740     provider = root_widget->default_theme_provider_.get();
741     if (provider)
742       return provider;
743   }
744   return default_theme_provider_.get();
745 }
746
747 const ui::NativeTheme* Widget::GetNativeTheme() const {
748   return native_widget_->GetNativeTheme();
749 }
750
751 FocusManager* Widget::GetFocusManager() {
752   Widget* toplevel_widget = GetTopLevelWidget();
753   return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
754 }
755
756 const FocusManager* Widget::GetFocusManager() const {
757   const Widget* toplevel_widget = GetTopLevelWidget();
758   return toplevel_widget ? toplevel_widget->focus_manager_.get() : NULL;
759 }
760
761 InputMethod* Widget::GetInputMethod() {
762   return const_cast<InputMethod*>(
763       const_cast<const Widget*>(this)->GetInputMethod());
764 }
765
766 const InputMethod* Widget::GetInputMethod() const {
767   if (is_top_level()) {
768     if (!input_method_.get())
769       input_method_ = const_cast<Widget*>(this)->CreateInputMethod().Pass();
770     return input_method_.get();
771   } else {
772     const Widget* toplevel = GetTopLevelWidget();
773     // If GetTopLevelWidget() returns itself which is not toplevel,
774     // the widget is detached from toplevel widget.
775     // TODO(oshima): Fix GetTopLevelWidget() to return NULL
776     // if there is no toplevel. We probably need to add GetTopMostWidget()
777     // to replace some use cases.
778     return (toplevel && toplevel != this) ? toplevel->GetInputMethod() : NULL;
779   }
780 }
781
782 ui::InputMethod* Widget::GetHostInputMethod() {
783   return native_widget_private()->GetHostInputMethod();
784 }
785
786 void Widget::RunShellDrag(View* view,
787                           const ui::OSExchangeData& data,
788                           const gfx::Point& location,
789                           int operation,
790                           ui::DragDropTypes::DragEventSource source) {
791   dragged_view_ = view;
792   native_widget_->RunShellDrag(view, data, location, operation, source);
793   // If the view is removed during the drag operation, dragged_view_ is set to
794   // NULL.
795   if (view && dragged_view_ == view) {
796     dragged_view_ = NULL;
797     view->OnDragDone();
798   }
799 }
800
801 void Widget::SchedulePaintInRect(const gfx::Rect& rect) {
802   native_widget_->SchedulePaintInRect(rect);
803 }
804
805 void Widget::SetCursor(gfx::NativeCursor cursor) {
806   native_widget_->SetCursor(cursor);
807 }
808
809 bool Widget::IsMouseEventsEnabled() const {
810   return native_widget_->IsMouseEventsEnabled();
811 }
812
813 void Widget::SetNativeWindowProperty(const char* name, void* value) {
814   native_widget_->SetNativeWindowProperty(name, value);
815 }
816
817 void* Widget::GetNativeWindowProperty(const char* name) const {
818   return native_widget_->GetNativeWindowProperty(name);
819 }
820
821 void Widget::UpdateWindowTitle() {
822   if (!non_client_view_)
823     return;
824
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();
832
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();
836 }
837
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());
843 }
844
845 FocusTraversable* Widget::GetFocusTraversable() {
846   return static_cast<internal::RootView*>(root_view_.get());
847 }
848
849 void Widget::ThemeChanged() {
850   root_view_->ThemeChanged();
851 }
852
853 void Widget::LocaleChanged() {
854   root_view_->LocaleChanged();
855 }
856
857 void Widget::SetFocusTraversableParent(FocusTraversable* parent) {
858   root_view_->SetFocusTraversableParent(parent);
859 }
860
861 void Widget::SetFocusTraversableParentView(View* parent_view) {
862   root_view_->SetFocusTraversableParentView(parent_view);
863 }
864
865 void Widget::ClearNativeFocus() {
866   native_widget_->ClearNativeFocus();
867 }
868
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);
877   }
878   if (frame_view)
879     return frame_view;
880
881   CustomFrameView* custom_frame_view = new CustomFrameView;
882   custom_frame_view->Init(this);
883   return custom_frame_view;
884 }
885
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();
890 }
891
892 bool Widget::ShouldWindowContentsBeTransparent() const {
893   return native_widget_->ShouldWindowContentsBeTransparent();
894 }
895
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;
903   }
904   FrameTypeChanged();
905 }
906
907 void Widget::FrameTypeChanged() {
908   native_widget_->FrameTypeChanged();
909 }
910
911 const ui::Compositor* Widget::GetCompositor() const {
912   return native_widget_->GetCompositor();
913 }
914
915 ui::Compositor* Widget::GetCompositor() {
916   return native_widget_->GetCompositor();
917 }
918
919 ui::Layer* Widget::GetLayer() {
920   return native_widget_->GetLayer();
921 }
922
923 void Widget::ReorderNativeViews() {
924   native_widget_->ReorderNativeViews();
925 }
926
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;
932 }
933
934 const NativeWidget* Widget::native_widget() const {
935   return native_widget_;
936 }
937
938 NativeWidget* Widget::native_widget() {
939   return native_widget_;
940 }
941
942 void Widget::SetCapture(View* view) {
943   if (internal::NativeWidgetPrivate::IsMouseButtonDown())
944     is_mouse_button_pressed_ = true;
945   if (internal::NativeWidgetPrivate::IsTouchDown())
946     is_touch_down_ = true;
947   root_view_->SetMouseHandler(view);
948   if (!native_widget_->HasCapture())
949     native_widget_->SetCapture();
950 }
951
952 void Widget::ReleaseCapture() {
953   if (native_widget_->HasCapture())
954     native_widget_->ReleaseCapture();
955 }
956
957 bool Widget::HasCapture() {
958   return native_widget_->HasCapture();
959 }
960
961 TooltipManager* Widget::GetTooltipManager() {
962   return native_widget_->GetTooltipManager();
963 }
964
965 const TooltipManager* Widget::GetTooltipManager() const {
966   return native_widget_->GetTooltipManager();
967 }
968
969 gfx::Rect Widget::GetWorkAreaBoundsInScreen() const {
970   return native_widget_->GetWorkAreaBoundsInScreen();
971 }
972
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);
980 }
981
982 void Widget::OnRootViewLayout() {
983   native_widget_->OnRootViewLayout();
984 }
985
986 bool Widget::IsTranslucentWindowOpacitySupported() const {
987   return native_widget_->IsTranslucentWindowOpacitySupported();
988 }
989
990 void Widget::OnOwnerClosing() {
991 }
992
993 ////////////////////////////////////////////////////////////////////////////////
994 // Widget, NativeWidgetDelegate implementation:
995
996 bool Widget::IsModal() const {
997   return widget_delegate_->GetModalType() != ui::MODAL_TYPE_NONE;
998 }
999
1000 bool Widget::IsDialogBox() const {
1001   return !!widget_delegate_->AsDialogDelegate();
1002 }
1003
1004 bool Widget::CanActivate() const {
1005   return widget_delegate_->CanActivate();
1006 }
1007
1008 bool Widget::IsInactiveRenderingDisabled() const {
1009   return disable_inactive_rendering_;
1010 }
1011
1012 void Widget::EnableInactiveRendering() {
1013   SetInactiveRenderingDisabled(false);
1014 }
1015
1016 void Widget::OnNativeWidgetActivationChanged(bool active) {
1017   // On windows we may end up here before we've completed initialization (from
1018   // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1019   // the Widget and will crash attempting to access it.
1020   if (!active && native_widget_initialized_)
1021     SaveWindowPlacement();
1022
1023   FOR_EACH_OBSERVER(WidgetObserver, observers_,
1024                     OnWidgetActivationChanged(this, active));
1025
1026   // During window creation, the widget gets focused without activation, and in
1027   // that case, the focus manager cannot set the appropriate text input client
1028   // because the widget is not active.  Thus we have to notify the focus manager
1029   // not only when the focus changes but also when the widget gets activated.
1030   // See crbug.com/377479 for details.
1031   views::FocusManager* focus_manager = GetFocusManager();
1032   if (focus_manager) {
1033     if (active)
1034       focus_manager->FocusTextInputClient(focus_manager->GetFocusedView());
1035     else
1036       focus_manager->BlurTextInputClient(focus_manager->GetFocusedView());
1037   }
1038
1039   if (IsVisible() && non_client_view())
1040     non_client_view()->frame_view()->SchedulePaint();
1041 }
1042
1043 void Widget::OnNativeFocus(gfx::NativeView old_focused_view) {
1044   WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(old_focused_view,
1045                                                         GetNativeView());
1046 }
1047
1048 void Widget::OnNativeBlur(gfx::NativeView new_focused_view) {
1049   WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(GetNativeView(),
1050                                                         new_focused_view);
1051 }
1052
1053 void Widget::OnNativeWidgetVisibilityChanging(bool visible) {
1054   FOR_EACH_OBSERVER(WidgetObserver, observers_,
1055                     OnWidgetVisibilityChanging(this, visible));
1056 }
1057
1058 void Widget::OnNativeWidgetVisibilityChanged(bool visible) {
1059   View* root = GetRootView();
1060   if (root)
1061     root->PropagateVisibilityNotifications(root, visible);
1062   FOR_EACH_OBSERVER(WidgetObserver, observers_,
1063                     OnWidgetVisibilityChanged(this, visible));
1064   if (GetCompositor() && root && root->layer())
1065     root->layer()->SetVisible(visible);
1066 }
1067
1068 void Widget::OnNativeWidgetCreated(bool desktop_widget) {
1069   if (is_top_level())
1070     focus_manager_.reset(FocusManagerFactory::Create(this, desktop_widget));
1071
1072   native_widget_->InitModalType(widget_delegate_->GetModalType());
1073
1074   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetCreated(this));
1075 }
1076
1077 void Widget::OnNativeWidgetDestroying() {
1078   // Tell the focus manager (if any) that root_view is being removed
1079   // in case that the focused view is under this root view.
1080   if (GetFocusManager())
1081     GetFocusManager()->ViewRemoved(root_view_.get());
1082   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroying(this));
1083   if (non_client_view_)
1084     non_client_view_->WindowClosing();
1085   widget_delegate_->WindowClosing();
1086 }
1087
1088 void Widget::OnNativeWidgetDestroyed() {
1089   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroyed(this));
1090   widget_delegate_->DeleteDelegate();
1091   widget_delegate_ = NULL;
1092   native_widget_destroyed_ = true;
1093 }
1094
1095 gfx::Size Widget::GetMinimumSize() const {
1096   return non_client_view_ ? non_client_view_->GetMinimumSize() : gfx::Size();
1097 }
1098
1099 gfx::Size Widget::GetMaximumSize() const {
1100   return non_client_view_ ? non_client_view_->GetMaximumSize() : gfx::Size();
1101 }
1102
1103 void Widget::OnNativeWidgetMove() {
1104   widget_delegate_->OnWidgetMove();
1105   View* root = GetRootView();
1106   if (root && root->GetFocusManager()) {
1107     View* focused_view = root->GetFocusManager()->GetFocusedView();
1108     if (focused_view && focused_view->GetInputMethod())
1109       focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1110   }
1111   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1112     this,
1113     GetWindowBoundsInScreen()));
1114 }
1115
1116 void Widget::OnNativeWidgetSizeChanged(const gfx::Size& new_size) {
1117   View* root = GetRootView();
1118   if (root) {
1119     root->SetSize(new_size);
1120     if (root->GetFocusManager()) {
1121       View* focused_view = GetRootView()->GetFocusManager()->GetFocusedView();
1122       if (focused_view && focused_view->GetInputMethod())
1123         focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1124     }
1125   }
1126   // Size changed notifications can fire prior to full initialization
1127   // i.e. during session restore.  Avoid saving session state during these
1128   // startup procedures.
1129   if (native_widget_initialized_)
1130     SaveWindowPlacement();
1131
1132   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1133     this,
1134     GetWindowBoundsInScreen()));
1135 }
1136
1137 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1138   widget_delegate_->OnWindowBeginUserBoundsChange();
1139 }
1140
1141 void Widget::OnNativeWidgetEndUserBoundsChange() {
1142   widget_delegate_->OnWindowEndUserBoundsChange();
1143 }
1144
1145 bool Widget::HasFocusManager() const {
1146   return !!focus_manager_.get();
1147 }
1148
1149 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect& dirty_region) {
1150   ui::Compositor* compositor = GetCompositor();
1151   if (!compositor)
1152     return false;
1153
1154   compositor->ScheduleRedrawRect(dirty_region);
1155   return true;
1156 }
1157
1158 void Widget::OnNativeWidgetPaint(gfx::Canvas* canvas) {
1159   // On Linux Aura, we can get here during Init() because of the
1160   // SetInitialBounds call.
1161   if (native_widget_initialized_)
1162     GetRootView()->Paint(canvas, CullSet());
1163 }
1164
1165 int Widget::GetNonClientComponent(const gfx::Point& point) {
1166   int component = non_client_view_ ?
1167       non_client_view_->NonClientHitTest(point) :
1168       HTNOWHERE;
1169
1170   if (movement_disabled_ && (component == HTCAPTION || component == HTSYSMENU))
1171     return HTNOWHERE;
1172
1173   return component;
1174 }
1175
1176 void Widget::OnKeyEvent(ui::KeyEvent* event) {
1177   SendEventToProcessor(event);
1178 }
1179
1180 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1181 //                   RootView from anywhere in Widget. Use
1182 //                   SendEventToProcessor() instead. See crbug.com/348087.
1183 void Widget::OnMouseEvent(ui::MouseEvent* event) {
1184   View* root_view = GetRootView();
1185   switch (event->type()) {
1186     case ui::ET_MOUSE_PRESSED: {
1187       last_mouse_event_was_move_ = false;
1188
1189       // We may get deleted by the time we return from OnMousePressed. So we
1190       // use an observer to make sure we are still alive.
1191       WidgetDeletionObserver widget_deletion_observer(this);
1192
1193       // Make sure we're still visible before we attempt capture as the mouse
1194       // press processing may have made the window hide (as happens with menus).
1195
1196       // It is possible for a View to show a context menu on mouse-press. Since
1197       // the menu does a capture and starts a nested message-loop, the release
1198       // would go to the menu. The next click (i.e. both mouse-press and release
1199       // events) also go to the menu. The menu (and the nested message-loop)
1200       // gets closed after this second release event. The code then resumes from
1201       // here. So make sure that the mouse-button is still down before doing a
1202       // capture.
1203       if (root_view && root_view->OnMousePressed(*event) &&
1204           widget_deletion_observer.IsWidgetAlive() && IsVisible() &&
1205           internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1206         is_mouse_button_pressed_ = true;
1207         if (!native_widget_->HasCapture())
1208           native_widget_->SetCapture();
1209         event->SetHandled();
1210       }
1211       return;
1212     }
1213
1214     case ui::ET_MOUSE_RELEASED:
1215       last_mouse_event_was_move_ = false;
1216       is_mouse_button_pressed_ = false;
1217       // Release capture first, to avoid confusion if OnMouseReleased blocks.
1218       if (auto_release_capture_ && native_widget_->HasCapture())
1219         native_widget_->ReleaseCapture();
1220       if (root_view)
1221         root_view->OnMouseReleased(*event);
1222       if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0)
1223         event->SetHandled();
1224       return;
1225
1226     case ui::ET_MOUSE_MOVED:
1227     case ui::ET_MOUSE_DRAGGED:
1228       if (native_widget_->HasCapture() && is_mouse_button_pressed_) {
1229         last_mouse_event_was_move_ = false;
1230         if (root_view)
1231           root_view->OnMouseDragged(*event);
1232       } else if (!last_mouse_event_was_move_ ||
1233                  last_mouse_event_position_ != event->location()) {
1234         last_mouse_event_position_ = event->location();
1235         last_mouse_event_was_move_ = true;
1236         if (root_view)
1237           root_view->OnMouseMoved(*event);
1238       }
1239       return;
1240
1241     case ui::ET_MOUSE_EXITED:
1242       last_mouse_event_was_move_ = false;
1243       if (root_view)
1244         root_view->OnMouseExited(*event);
1245       return;
1246
1247     case ui::ET_MOUSEWHEEL:
1248       if (root_view && root_view->OnMouseWheel(
1249           static_cast<const ui::MouseWheelEvent&>(*event)))
1250         event->SetHandled();
1251       return;
1252
1253     default:
1254       return;
1255   }
1256 }
1257
1258 void Widget::OnMouseCaptureLost() {
1259   if (is_mouse_button_pressed_ || is_touch_down_) {
1260     View* root_view = GetRootView();
1261     if (root_view)
1262       root_view->OnMouseCaptureLost();
1263   }
1264   is_touch_down_ = false;
1265   is_mouse_button_pressed_ = false;
1266 }
1267
1268 void Widget::OnScrollEvent(ui::ScrollEvent* event) {
1269   ui::ScrollEvent event_copy(*event);
1270   SendEventToProcessor(&event_copy);
1271
1272   // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1273   if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) {
1274     ui::MouseWheelEvent wheel(*event);
1275     OnMouseEvent(&wheel);
1276   }
1277 }
1278
1279 void Widget::OnGestureEvent(ui::GestureEvent* event) {
1280   switch (event->type()) {
1281     case ui::ET_GESTURE_TAP_DOWN:
1282       is_touch_down_ = true;
1283       // We explicitly don't capture here. Not capturing enables multiple
1284       // widgets to get tap events at the same time. Views (such as tab
1285       // dragging) may explicitly capture.
1286       break;
1287
1288     case ui::ET_GESTURE_END:
1289       if (event->details().touch_points() == 1)
1290         is_touch_down_ = false;
1291       break;
1292
1293     default:
1294       break;
1295   }
1296   SendEventToProcessor(event);
1297 }
1298
1299 bool Widget::ExecuteCommand(int command_id) {
1300   return widget_delegate_->ExecuteWindowsCommand(command_id);
1301 }
1302
1303 InputMethod* Widget::GetInputMethodDirect() {
1304   return input_method_.get();
1305 }
1306
1307 const std::vector<ui::Layer*>& Widget::GetRootLayers() {
1308   if (root_layers_dirty_) {
1309     root_layers_dirty_ = false;
1310     root_layers_.clear();
1311     BuildRootLayers(GetRootView(), &root_layers_);
1312   }
1313   return root_layers_;
1314 }
1315
1316 bool Widget::HasHitTestMask() const {
1317   return widget_delegate_->WidgetHasHitTestMask();
1318 }
1319
1320 void Widget::GetHitTestMask(gfx::Path* mask) const {
1321   DCHECK(mask);
1322   widget_delegate_->GetWidgetHitTestMask(mask);
1323 }
1324
1325 Widget* Widget::AsWidget() {
1326   return this;
1327 }
1328
1329 const Widget* Widget::AsWidget() const {
1330   return this;
1331 }
1332
1333 bool Widget::SetInitialFocus(ui::WindowShowState show_state) {
1334   View* v = widget_delegate_->GetInitiallyFocusedView();
1335   if (!focus_on_creation_ || show_state == ui::SHOW_STATE_INACTIVE ||
1336       show_state == ui::SHOW_STATE_MINIMIZED) {
1337     // If not focusing the window now, tell the focus manager which view to
1338     // focus when the window is restored.
1339     if (v)
1340       focus_manager_->SetStoredFocusView(v);
1341     return true;
1342   }
1343   if (v)
1344     v->RequestFocus();
1345   return !!v;
1346 }
1347
1348 ////////////////////////////////////////////////////////////////////////////////
1349 // Widget, ui::EventSource implementation:
1350 ui::EventProcessor* Widget::GetEventProcessor() {
1351   return root_view_.get();
1352 }
1353
1354 ////////////////////////////////////////////////////////////////////////////////
1355 // Widget, FocusTraversable implementation:
1356
1357 FocusSearch* Widget::GetFocusSearch() {
1358   return root_view_->GetFocusSearch();
1359 }
1360
1361 FocusTraversable* Widget::GetFocusTraversableParent() {
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;
1366 }
1367
1368 View* Widget::GetFocusTraversableParentView() {
1369   // We are a proxy to the root view, so we should be bypassed when traversing
1370   // up and as a result this should not be called.
1371   NOTREACHED();
1372   return NULL;
1373 }
1374
1375 ////////////////////////////////////////////////////////////////////////////////
1376 // Widget, ui::NativeThemeObserver implementation:
1377
1378 void Widget::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
1379   DCHECK(observer_manager_.IsObserving(observed_theme));
1380
1381   ui::NativeTheme* current_native_theme = GetNativeTheme();
1382   if (!observer_manager_.IsObserving(current_native_theme)) {
1383     observer_manager_.RemoveAll();
1384     observer_manager_.Add(current_native_theme);
1385   }
1386
1387   root_view_->PropagateNativeThemeChanged(current_native_theme);
1388 }
1389
1390 ////////////////////////////////////////////////////////////////////////////////
1391 // Widget, protected:
1392
1393 internal::RootView* Widget::CreateRootView() {
1394   return new internal::RootView(this);
1395 }
1396
1397 void Widget::DestroyRootView() {
1398   non_client_view_ = NULL;
1399   root_view_.reset();
1400   // Input method has to be destroyed before focus manager.
1401   input_method_.reset();
1402 }
1403
1404 ////////////////////////////////////////////////////////////////////////////////
1405 // Widget, private:
1406
1407 void Widget::SetInactiveRenderingDisabled(bool value) {
1408   if (value == disable_inactive_rendering_)
1409     return;
1410
1411   disable_inactive_rendering_ = value;
1412   if (non_client_view_)
1413     non_client_view_->SetInactiveRenderingDisabled(value);
1414 }
1415
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;
1423
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);
1428 }
1429
1430 void Widget::SetInitialBounds(const gfx::Rect& bounds) {
1431   if (!non_client_view_)
1432     return;
1433
1434   gfx::Rect saved_bounds;
1435   if (GetSavedWindowPlacement(&saved_bounds, &saved_show_state_)) {
1436     if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED) {
1437       // If we're going to maximize, wait until Show is invoked to set the
1438       // bounds. That way we avoid a noticeable resize.
1439       initial_restored_bounds_ = saved_bounds;
1440     } else if (!saved_bounds.IsEmpty()) {
1441       // If the saved bounds are valid, use them.
1442       SetBounds(saved_bounds);
1443     }
1444   } else {
1445     if (bounds.IsEmpty()) {
1446       // No initial bounds supplied, so size the window to its content and
1447       // center over its parent.
1448       native_widget_->CenterWindow(non_client_view_->GetPreferredSize());
1449     } else {
1450       // Use the supplied initial bounds.
1451       SetBoundsConstrained(bounds);
1452     }
1453   }
1454 }
1455
1456 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds) {
1457   if (bounds.IsEmpty()) {
1458     View* contents_view = GetContentsView();
1459     DCHECK(contents_view);
1460     // No initial bounds supplied, so size the window to its content and
1461     // center over its parent if preferred size is provided.
1462     gfx::Size size = contents_view->GetPreferredSize();
1463     if (!size.IsEmpty())
1464       native_widget_->CenterWindow(size);
1465   } else {
1466     // Use the supplied initial bounds.
1467     SetBoundsConstrained(bounds);
1468   }
1469 }
1470
1471 bool Widget::GetSavedWindowPlacement(gfx::Rect* bounds,
1472                                      ui::WindowShowState* show_state) {
1473   // First we obtain the window's saved show-style and store it. We need to do
1474   // this here, rather than in Show() because by the time Show() is called,
1475   // the window's size will have been reset (below) and the saved maximized
1476   // state will have been lost. Sadly there's no way to tell on Windows when
1477   // a window is restored from maximized state, so we can't more accurately
1478   // track maximized state independently of sizing information.
1479
1480   // Restore the window's placement from the controller.
1481   if (widget_delegate_->GetSavedWindowPlacement(this, bounds, show_state)) {
1482     if (!widget_delegate_->ShouldRestoreWindowSize()) {
1483       bounds->set_size(non_client_view_->GetPreferredSize());
1484     } else {
1485       gfx::Size minimum_size = GetMinimumSize();
1486       // Make sure the bounds are at least the minimum size.
1487       if (bounds->width() < minimum_size.width())
1488         bounds->set_width(minimum_size.width());
1489
1490       if (bounds->height() < minimum_size.height())
1491         bounds->set_height(minimum_size.height());
1492     }
1493     return true;
1494   }
1495   return false;
1496 }
1497
1498 scoped_ptr<InputMethod> Widget::CreateInputMethod() {
1499   scoped_ptr<InputMethod> input_method(native_widget_->CreateInputMethod());
1500   if (input_method.get())
1501     input_method->Init(this);
1502   return input_method.Pass();
1503 }
1504
1505 void Widget::ReplaceInputMethod(InputMethod* input_method) {
1506   input_method_.reset(input_method);
1507   input_method->SetDelegate(native_widget_->GetInputMethodDelegate());
1508   input_method->Init(this);
1509 }
1510
1511 namespace internal {
1512
1513 ////////////////////////////////////////////////////////////////////////////////
1514 // internal::NativeWidgetPrivate, NativeWidget implementation:
1515
1516 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1517   return this;
1518 }
1519
1520 }  // namespace internal
1521 }  // namespace views