Upstream version 9.38.198.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       ignore_capture_loss_(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::NativeWindow context) {
231   return CreateWindowWithContextAndBounds(delegate, context, gfx::Rect());
232 }
233
234 // static
235 Widget* Widget::CreateWindowWithContextAndBounds(WidgetDelegate* delegate,
236                                                  gfx::NativeWindow 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   OnDragWillStart();
793   native_widget_->RunShellDrag(view, data, location, operation, source);
794   // If the view is removed during the drag operation, dragged_view_ is set to
795   // NULL.
796   if (view && dragged_view_ == view) {
797     dragged_view_ = NULL;
798     view->OnDragDone();
799   }
800   OnDragComplete();
801 }
802
803 void Widget::SchedulePaintInRect(const gfx::Rect& rect) {
804   native_widget_->SchedulePaintInRect(rect);
805 }
806
807 void Widget::SetCursor(gfx::NativeCursor cursor) {
808   native_widget_->SetCursor(cursor);
809 }
810
811 bool Widget::IsMouseEventsEnabled() const {
812   return native_widget_->IsMouseEventsEnabled();
813 }
814
815 void Widget::SetNativeWindowProperty(const char* name, void* value) {
816   native_widget_->SetNativeWindowProperty(name, value);
817 }
818
819 void* Widget::GetNativeWindowProperty(const char* name) const {
820   return native_widget_->GetNativeWindowProperty(name);
821 }
822
823 void Widget::UpdateWindowTitle() {
824   if (!non_client_view_)
825     return;
826
827   // Update the native frame's text. We do this regardless of whether or not
828   // the native frame is being used, since this also updates the taskbar, etc.
829   base::string16 window_title = widget_delegate_->GetWindowTitle();
830   base::i18n::AdjustStringForLocaleDirection(&window_title);
831   if (!native_widget_->SetWindowTitle(window_title))
832     return;
833   non_client_view_->UpdateWindowTitle();
834
835   // If the non-client view is rendering its own title, it'll need to relayout
836   // now and to get a paint update later on.
837   non_client_view_->Layout();
838 }
839
840 void Widget::UpdateWindowIcon() {
841   if (non_client_view_)
842     non_client_view_->UpdateWindowIcon();
843   native_widget_->SetWindowIcons(widget_delegate_->GetWindowIcon(),
844                                  widget_delegate_->GetWindowAppIcon());
845 }
846
847 FocusTraversable* Widget::GetFocusTraversable() {
848   return static_cast<internal::RootView*>(root_view_.get());
849 }
850
851 void Widget::ThemeChanged() {
852   root_view_->ThemeChanged();
853 }
854
855 void Widget::LocaleChanged() {
856   root_view_->LocaleChanged();
857 }
858
859 void Widget::SetFocusTraversableParent(FocusTraversable* parent) {
860   root_view_->SetFocusTraversableParent(parent);
861 }
862
863 void Widget::SetFocusTraversableParentView(View* parent_view) {
864   root_view_->SetFocusTraversableParentView(parent_view);
865 }
866
867 void Widget::ClearNativeFocus() {
868   native_widget_->ClearNativeFocus();
869 }
870
871 NonClientFrameView* Widget::CreateNonClientFrameView() {
872   NonClientFrameView* frame_view =
873       widget_delegate_->CreateNonClientFrameView(this);
874   if (!frame_view)
875     frame_view = native_widget_->CreateNonClientFrameView();
876   if (!frame_view && ViewsDelegate::views_delegate) {
877     frame_view =
878         ViewsDelegate::views_delegate->CreateDefaultNonClientFrameView(this);
879   }
880   if (frame_view)
881     return frame_view;
882
883   CustomFrameView* custom_frame_view = new CustomFrameView;
884   custom_frame_view->Init(this);
885   return custom_frame_view;
886 }
887
888 bool Widget::ShouldUseNativeFrame() const {
889   if (frame_type_ != FRAME_TYPE_DEFAULT)
890     return frame_type_ == FRAME_TYPE_FORCE_NATIVE;
891   return native_widget_->ShouldUseNativeFrame();
892 }
893
894 bool Widget::ShouldWindowContentsBeTransparent() const {
895   return native_widget_->ShouldWindowContentsBeTransparent();
896 }
897
898 void Widget::DebugToggleFrameType() {
899   if (frame_type_ == FRAME_TYPE_DEFAULT) {
900     frame_type_ = ShouldUseNativeFrame() ? FRAME_TYPE_FORCE_CUSTOM :
901         FRAME_TYPE_FORCE_NATIVE;
902   } else {
903     frame_type_ = frame_type_ == FRAME_TYPE_FORCE_CUSTOM ?
904         FRAME_TYPE_FORCE_NATIVE : FRAME_TYPE_FORCE_CUSTOM;
905   }
906   FrameTypeChanged();
907 }
908
909 void Widget::FrameTypeChanged() {
910   native_widget_->FrameTypeChanged();
911 }
912
913 const ui::Compositor* Widget::GetCompositor() const {
914   return native_widget_->GetCompositor();
915 }
916
917 ui::Compositor* Widget::GetCompositor() {
918   return native_widget_->GetCompositor();
919 }
920
921 ui::Layer* Widget::GetLayer() {
922   return native_widget_->GetLayer();
923 }
924
925 void Widget::ReorderNativeViews() {
926   native_widget_->ReorderNativeViews();
927 }
928
929 void Widget::UpdateRootLayers() {
930   // Calculate the layers requires traversing the tree, and since nearly any
931   // mutation of the tree can trigger this call we delay until absolutely
932   // necessary.
933   root_layers_dirty_ = true;
934 }
935
936 const NativeWidget* Widget::native_widget() const {
937   return native_widget_;
938 }
939
940 NativeWidget* Widget::native_widget() {
941   return native_widget_;
942 }
943
944 void Widget::SetCapture(View* view) {
945   if (!native_widget_->HasCapture()) {
946     native_widget_->SetCapture();
947
948     // Early return if setting capture was unsuccessful.
949     if (!native_widget_->HasCapture())
950       return;
951   }
952
953   if (internal::NativeWidgetPrivate::IsMouseButtonDown())
954     is_mouse_button_pressed_ = true;
955   root_view_->SetMouseHandler(view);
956 }
957
958 void Widget::ReleaseCapture() {
959   if (native_widget_->HasCapture())
960     native_widget_->ReleaseCapture();
961 }
962
963 bool Widget::HasCapture() {
964   return native_widget_->HasCapture();
965 }
966
967 TooltipManager* Widget::GetTooltipManager() {
968   return native_widget_->GetTooltipManager();
969 }
970
971 const TooltipManager* Widget::GetTooltipManager() const {
972   return native_widget_->GetTooltipManager();
973 }
974
975 gfx::Rect Widget::GetWorkAreaBoundsInScreen() const {
976   return native_widget_->GetWorkAreaBoundsInScreen();
977 }
978
979 void Widget::SynthesizeMouseMoveEvent() {
980   last_mouse_event_was_move_ = false;
981   ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED,
982                              last_mouse_event_position_,
983                              last_mouse_event_position_,
984                              ui::EF_IS_SYNTHESIZED, 0);
985   root_view_->OnMouseMoved(mouse_event);
986 }
987
988 void Widget::OnRootViewLayout() {
989   native_widget_->OnRootViewLayout();
990 }
991
992 bool Widget::IsTranslucentWindowOpacitySupported() const {
993   return native_widget_->IsTranslucentWindowOpacitySupported();
994 }
995
996 void Widget::OnOwnerClosing() {
997 }
998
999 ////////////////////////////////////////////////////////////////////////////////
1000 // Widget, NativeWidgetDelegate implementation:
1001
1002 bool Widget::IsModal() const {
1003   return widget_delegate_->GetModalType() != ui::MODAL_TYPE_NONE;
1004 }
1005
1006 bool Widget::IsDialogBox() const {
1007   return !!widget_delegate_->AsDialogDelegate();
1008 }
1009
1010 bool Widget::CanActivate() const {
1011   return widget_delegate_->CanActivate();
1012 }
1013
1014 bool Widget::IsInactiveRenderingDisabled() const {
1015   return disable_inactive_rendering_;
1016 }
1017
1018 void Widget::EnableInactiveRendering() {
1019   SetInactiveRenderingDisabled(false);
1020 }
1021
1022 void Widget::OnNativeWidgetActivationChanged(bool active) {
1023   // On windows we may end up here before we've completed initialization (from
1024   // an WM_NCACTIVATE). If that happens the WidgetDelegate likely doesn't know
1025   // the Widget and will crash attempting to access it.
1026   if (!active && native_widget_initialized_)
1027     SaveWindowPlacement();
1028
1029   FOR_EACH_OBSERVER(WidgetObserver, observers_,
1030                     OnWidgetActivationChanged(this, active));
1031
1032   // During window creation, the widget gets focused without activation, and in
1033   // that case, the focus manager cannot set the appropriate text input client
1034   // because the widget is not active.  Thus we have to notify the focus manager
1035   // not only when the focus changes but also when the widget gets activated.
1036   // See crbug.com/377479 for details.
1037   views::FocusManager* focus_manager = GetFocusManager();
1038   if (focus_manager) {
1039     if (active)
1040       focus_manager->FocusTextInputClient(focus_manager->GetFocusedView());
1041     else
1042       focus_manager->BlurTextInputClient(focus_manager->GetFocusedView());
1043   }
1044
1045   if (IsVisible() && non_client_view())
1046     non_client_view()->frame_view()->SchedulePaint();
1047 }
1048
1049 void Widget::OnNativeFocus(gfx::NativeView old_focused_view) {
1050   WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(old_focused_view,
1051                                                         GetNativeView());
1052 }
1053
1054 void Widget::OnNativeBlur(gfx::NativeView new_focused_view) {
1055   WidgetFocusManager::GetInstance()->OnWidgetFocusEvent(GetNativeView(),
1056                                                         new_focused_view);
1057 }
1058
1059 void Widget::OnNativeWidgetVisibilityChanging(bool visible) {
1060   FOR_EACH_OBSERVER(WidgetObserver, observers_,
1061                     OnWidgetVisibilityChanging(this, visible));
1062 }
1063
1064 void Widget::OnNativeWidgetVisibilityChanged(bool visible) {
1065   View* root = GetRootView();
1066   if (root)
1067     root->PropagateVisibilityNotifications(root, visible);
1068   FOR_EACH_OBSERVER(WidgetObserver, observers_,
1069                     OnWidgetVisibilityChanged(this, visible));
1070   if (GetCompositor() && root && root->layer())
1071     root->layer()->SetVisible(visible);
1072 }
1073
1074 void Widget::OnNativeWidgetCreated(bool desktop_widget) {
1075   if (is_top_level())
1076     focus_manager_.reset(FocusManagerFactory::Create(this, desktop_widget));
1077
1078   native_widget_->InitModalType(widget_delegate_->GetModalType());
1079
1080   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetCreated(this));
1081 }
1082
1083 void Widget::OnNativeWidgetDestroying() {
1084   // Tell the focus manager (if any) that root_view is being removed
1085   // in case that the focused view is under this root view.
1086   if (GetFocusManager())
1087     GetFocusManager()->ViewRemoved(root_view_.get());
1088   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroying(this));
1089   if (non_client_view_)
1090     non_client_view_->WindowClosing();
1091   widget_delegate_->WindowClosing();
1092 }
1093
1094 void Widget::OnNativeWidgetDestroyed() {
1095   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetDestroyed(this));
1096   widget_delegate_->DeleteDelegate();
1097   widget_delegate_ = NULL;
1098   native_widget_destroyed_ = true;
1099 }
1100
1101 gfx::Size Widget::GetMinimumSize() const {
1102   return non_client_view_ ? non_client_view_->GetMinimumSize() : gfx::Size();
1103 }
1104
1105 gfx::Size Widget::GetMaximumSize() const {
1106   return non_client_view_ ? non_client_view_->GetMaximumSize() : gfx::Size();
1107 }
1108
1109 void Widget::OnNativeWidgetMove() {
1110   widget_delegate_->OnWidgetMove();
1111   View* root = GetRootView();
1112   if (root && root->GetFocusManager()) {
1113     View* focused_view = root->GetFocusManager()->GetFocusedView();
1114     if (focused_view && focused_view->GetInputMethod())
1115       focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1116   }
1117   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1118     this,
1119     GetWindowBoundsInScreen()));
1120 }
1121
1122 void Widget::OnNativeWidgetSizeChanged(const gfx::Size& new_size) {
1123   View* root = GetRootView();
1124   if (root) {
1125     root->SetSize(new_size);
1126     if (root->GetFocusManager()) {
1127       View* focused_view = GetRootView()->GetFocusManager()->GetFocusedView();
1128       if (focused_view && focused_view->GetInputMethod())
1129         focused_view->GetInputMethod()->OnCaretBoundsChanged(focused_view);
1130     }
1131   }
1132   SaveWindowPlacementIfInitialized();
1133
1134   FOR_EACH_OBSERVER(WidgetObserver, observers_, OnWidgetBoundsChanged(
1135     this,
1136     GetWindowBoundsInScreen()));
1137 }
1138
1139 void Widget::OnNativeWidgetWindowShowStateChanged() {
1140   SaveWindowPlacementIfInitialized();
1141 }
1142
1143 void Widget::OnNativeWidgetBeginUserBoundsChange() {
1144   widget_delegate_->OnWindowBeginUserBoundsChange();
1145 }
1146
1147 void Widget::OnNativeWidgetEndUserBoundsChange() {
1148   widget_delegate_->OnWindowEndUserBoundsChange();
1149 }
1150
1151 bool Widget::HasFocusManager() const {
1152   return !!focus_manager_.get();
1153 }
1154
1155 bool Widget::OnNativeWidgetPaintAccelerated(const gfx::Rect& dirty_region) {
1156   ui::Compositor* compositor = GetCompositor();
1157   if (!compositor)
1158     return false;
1159
1160   compositor->ScheduleRedrawRect(dirty_region);
1161   return true;
1162 }
1163
1164 void Widget::OnNativeWidgetPaint(gfx::Canvas* canvas) {
1165   // On Linux Aura, we can get here during Init() because of the
1166   // SetInitialBounds call.
1167   if (native_widget_initialized_)
1168     GetRootView()->Paint(canvas, CullSet());
1169 }
1170
1171 int Widget::GetNonClientComponent(const gfx::Point& point) {
1172   int component = non_client_view_ ?
1173       non_client_view_->NonClientHitTest(point) :
1174       HTNOWHERE;
1175
1176   if (movement_disabled_ && (component == HTCAPTION || component == HTSYSMENU))
1177     return HTNOWHERE;
1178
1179   return component;
1180 }
1181
1182 void Widget::OnKeyEvent(ui::KeyEvent* event) {
1183   SendEventToProcessor(event);
1184 }
1185
1186 // TODO(tdanderson): We should not be calling the OnMouse*() functions on
1187 //                   RootView from anywhere in Widget. Use
1188 //                   SendEventToProcessor() instead. See crbug.com/348087.
1189 void Widget::OnMouseEvent(ui::MouseEvent* event) {
1190   View* root_view = GetRootView();
1191   switch (event->type()) {
1192     case ui::ET_MOUSE_PRESSED: {
1193       last_mouse_event_was_move_ = false;
1194
1195       // We may get deleted by the time we return from OnMousePressed. So we
1196       // use an observer to make sure we are still alive.
1197       WidgetDeletionObserver widget_deletion_observer(this);
1198
1199       // Make sure we're still visible before we attempt capture as the mouse
1200       // press processing may have made the window hide (as happens with menus).
1201
1202       // It is possible for a View to show a context menu on mouse-press. Since
1203       // the menu does a capture and starts a nested message-loop, the release
1204       // would go to the menu. The next click (i.e. both mouse-press and release
1205       // events) also go to the menu. The menu (and the nested message-loop)
1206       // gets closed after this second release event. The code then resumes from
1207       // here. So make sure that the mouse-button is still down before doing a
1208       // capture.
1209       if (root_view && root_view->OnMousePressed(*event) &&
1210           widget_deletion_observer.IsWidgetAlive() && IsVisible() &&
1211           internal::NativeWidgetPrivate::IsMouseButtonDown()) {
1212         is_mouse_button_pressed_ = true;
1213         if (!native_widget_->HasCapture())
1214           native_widget_->SetCapture();
1215         event->SetHandled();
1216       }
1217       return;
1218     }
1219
1220     case ui::ET_MOUSE_RELEASED:
1221       last_mouse_event_was_move_ = false;
1222       is_mouse_button_pressed_ = false;
1223       // Release capture first, to avoid confusion if OnMouseReleased blocks.
1224       if (auto_release_capture_ && native_widget_->HasCapture()) {
1225         base::AutoReset<bool> resetter(&ignore_capture_loss_, true);
1226         native_widget_->ReleaseCapture();
1227       }
1228       if (root_view)
1229         root_view->OnMouseReleased(*event);
1230       if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0)
1231         event->SetHandled();
1232       return;
1233
1234     case ui::ET_MOUSE_MOVED:
1235     case ui::ET_MOUSE_DRAGGED:
1236       if (native_widget_->HasCapture() && is_mouse_button_pressed_) {
1237         last_mouse_event_was_move_ = false;
1238         if (root_view)
1239           root_view->OnMouseDragged(*event);
1240       } else if (!last_mouse_event_was_move_ ||
1241                  last_mouse_event_position_ != event->location()) {
1242         last_mouse_event_position_ = event->location();
1243         last_mouse_event_was_move_ = true;
1244         if (root_view)
1245           root_view->OnMouseMoved(*event);
1246       }
1247       return;
1248
1249     case ui::ET_MOUSE_EXITED:
1250       last_mouse_event_was_move_ = false;
1251       if (root_view)
1252         root_view->OnMouseExited(*event);
1253       return;
1254
1255     case ui::ET_MOUSEWHEEL:
1256       if (root_view && root_view->OnMouseWheel(
1257           static_cast<const ui::MouseWheelEvent&>(*event)))
1258         event->SetHandled();
1259       return;
1260
1261     default:
1262       return;
1263   }
1264 }
1265
1266 void Widget::OnMouseCaptureLost() {
1267   if (ignore_capture_loss_)
1268     return;
1269
1270   View* root_view = GetRootView();
1271   if (root_view)
1272     root_view->OnMouseCaptureLost();
1273   is_mouse_button_pressed_ = false;
1274 }
1275
1276 void Widget::OnScrollEvent(ui::ScrollEvent* event) {
1277   ui::ScrollEvent event_copy(*event);
1278   SendEventToProcessor(&event_copy);
1279
1280   // Convert unhandled ui::ET_SCROLL events into ui::ET_MOUSEWHEEL events.
1281   if (!event_copy.handled() && event_copy.type() == ui::ET_SCROLL) {
1282     ui::MouseWheelEvent wheel(*event);
1283     OnMouseEvent(&wheel);
1284   }
1285 }
1286
1287 void Widget::OnGestureEvent(ui::GestureEvent* event) {
1288   // We explicitly do not capture here. Not capturing enables multiple widgets
1289   // to get tap events at the same time. Views (such as tab dragging) may
1290   // explicitly capture.
1291   SendEventToProcessor(event);
1292 }
1293
1294 bool Widget::ExecuteCommand(int command_id) {
1295   return widget_delegate_->ExecuteWindowsCommand(command_id);
1296 }
1297
1298 InputMethod* Widget::GetInputMethodDirect() {
1299   return input_method_.get();
1300 }
1301
1302 const std::vector<ui::Layer*>& Widget::GetRootLayers() {
1303   if (root_layers_dirty_) {
1304     root_layers_dirty_ = false;
1305     root_layers_.clear();
1306     BuildRootLayers(GetRootView(), &root_layers_);
1307   }
1308   return root_layers_;
1309 }
1310
1311 bool Widget::HasHitTestMask() const {
1312   return widget_delegate_->WidgetHasHitTestMask();
1313 }
1314
1315 void Widget::GetHitTestMask(gfx::Path* mask) const {
1316   DCHECK(mask);
1317   widget_delegate_->GetWidgetHitTestMask(mask);
1318 }
1319
1320 Widget* Widget::AsWidget() {
1321   return this;
1322 }
1323
1324 const Widget* Widget::AsWidget() const {
1325   return this;
1326 }
1327
1328 bool Widget::SetInitialFocus(ui::WindowShowState show_state) {
1329   View* v = widget_delegate_->GetInitiallyFocusedView();
1330   if (!focus_on_creation_ || show_state == ui::SHOW_STATE_INACTIVE ||
1331       show_state == ui::SHOW_STATE_MINIMIZED) {
1332     // If not focusing the window now, tell the focus manager which view to
1333     // focus when the window is restored.
1334     if (v)
1335       focus_manager_->SetStoredFocusView(v);
1336     return true;
1337   }
1338   if (v)
1339     v->RequestFocus();
1340   return !!v;
1341 }
1342
1343 ////////////////////////////////////////////////////////////////////////////////
1344 // Widget, ui::EventSource implementation:
1345 ui::EventProcessor* Widget::GetEventProcessor() {
1346   return root_view_.get();
1347 }
1348
1349 ////////////////////////////////////////////////////////////////////////////////
1350 // Widget, FocusTraversable implementation:
1351
1352 FocusSearch* Widget::GetFocusSearch() {
1353   return root_view_->GetFocusSearch();
1354 }
1355
1356 FocusTraversable* Widget::GetFocusTraversableParent() {
1357   // We are a proxy to the root view, so we should be bypassed when traversing
1358   // up and as a result this should not be called.
1359   NOTREACHED();
1360   return NULL;
1361 }
1362
1363 View* Widget::GetFocusTraversableParentView() {
1364   // We are a proxy to the root view, so we should be bypassed when traversing
1365   // up and as a result this should not be called.
1366   NOTREACHED();
1367   return NULL;
1368 }
1369
1370 ////////////////////////////////////////////////////////////////////////////////
1371 // Widget, ui::NativeThemeObserver implementation:
1372
1373 void Widget::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
1374   DCHECK(observer_manager_.IsObserving(observed_theme));
1375
1376   ui::NativeTheme* current_native_theme = GetNativeTheme();
1377   if (!observer_manager_.IsObserving(current_native_theme)) {
1378     observer_manager_.RemoveAll();
1379     observer_manager_.Add(current_native_theme);
1380   }
1381
1382   root_view_->PropagateNativeThemeChanged(current_native_theme);
1383 }
1384
1385 ////////////////////////////////////////////////////////////////////////////////
1386 // Widget, protected:
1387
1388 internal::RootView* Widget::CreateRootView() {
1389   return new internal::RootView(this);
1390 }
1391
1392 void Widget::DestroyRootView() {
1393   non_client_view_ = NULL;
1394   root_view_.reset();
1395   // Input method has to be destroyed before focus manager.
1396   input_method_.reset();
1397 }
1398
1399 void Widget::OnDragWillStart() {
1400 }
1401
1402 void Widget::OnDragComplete() {
1403 }
1404
1405 ////////////////////////////////////////////////////////////////////////////////
1406 // Widget, private:
1407
1408 void Widget::SetInactiveRenderingDisabled(bool value) {
1409   if (value == disable_inactive_rendering_)
1410     return;
1411
1412   disable_inactive_rendering_ = value;
1413   if (non_client_view_)
1414     non_client_view_->SetInactiveRenderingDisabled(value);
1415 }
1416
1417 void Widget::SaveWindowPlacement() {
1418   // The window delegate does the actual saving for us. It seems like (judging
1419   // by go/crash) that in some circumstances we can end up here after
1420   // WM_DESTROY, at which point the window delegate is likely gone. So just
1421   // bail.
1422   if (!widget_delegate_)
1423     return;
1424
1425   ui::WindowShowState show_state = ui::SHOW_STATE_NORMAL;
1426   gfx::Rect bounds;
1427   native_widget_->GetWindowPlacement(&bounds, &show_state);
1428   widget_delegate_->SaveWindowPlacement(bounds, show_state);
1429 }
1430
1431 void Widget::SaveWindowPlacementIfInitialized() {
1432   if (native_widget_initialized_)
1433     SaveWindowPlacement();
1434 }
1435
1436 void Widget::SetInitialBounds(const gfx::Rect& bounds) {
1437   if (!non_client_view_)
1438     return;
1439
1440   gfx::Rect saved_bounds;
1441   if (GetSavedWindowPlacement(&saved_bounds, &saved_show_state_)) {
1442     if (saved_show_state_ == ui::SHOW_STATE_MAXIMIZED) {
1443       // If we're going to maximize, wait until Show is invoked to set the
1444       // bounds. That way we avoid a noticeable resize.
1445       initial_restored_bounds_ = saved_bounds;
1446     } else if (!saved_bounds.IsEmpty()) {
1447       // If the saved bounds are valid, use them.
1448       SetBounds(saved_bounds);
1449     }
1450   } else {
1451     if (bounds.IsEmpty()) {
1452       // No initial bounds supplied, so size the window to its content and
1453       // center over its parent.
1454       native_widget_->CenterWindow(non_client_view_->GetPreferredSize());
1455     } else {
1456       // Use the supplied initial bounds.
1457       SetBoundsConstrained(bounds);
1458     }
1459   }
1460 }
1461
1462 void Widget::SetInitialBoundsForFramelessWindow(const gfx::Rect& bounds) {
1463   if (bounds.IsEmpty()) {
1464     View* contents_view = GetContentsView();
1465     DCHECK(contents_view);
1466     // No initial bounds supplied, so size the window to its content and
1467     // center over its parent if preferred size is provided.
1468     gfx::Size size = contents_view->GetPreferredSize();
1469     if (!size.IsEmpty())
1470       native_widget_->CenterWindow(size);
1471   } else {
1472     // Use the supplied initial bounds.
1473     SetBoundsConstrained(bounds);
1474   }
1475 }
1476
1477 bool Widget::GetSavedWindowPlacement(gfx::Rect* bounds,
1478                                      ui::WindowShowState* show_state) {
1479   // First we obtain the window's saved show-style and store it. We need to do
1480   // this here, rather than in Show() because by the time Show() is called,
1481   // the window's size will have been reset (below) and the saved maximized
1482   // state will have been lost. Sadly there's no way to tell on Windows when
1483   // a window is restored from maximized state, so we can't more accurately
1484   // track maximized state independently of sizing information.
1485
1486   // Restore the window's placement from the controller.
1487   if (widget_delegate_->GetSavedWindowPlacement(this, bounds, show_state)) {
1488     if (!widget_delegate_->ShouldRestoreWindowSize()) {
1489       bounds->set_size(non_client_view_->GetPreferredSize());
1490     } else {
1491       gfx::Size minimum_size = GetMinimumSize();
1492       // Make sure the bounds are at least the minimum size.
1493       if (bounds->width() < minimum_size.width())
1494         bounds->set_width(minimum_size.width());
1495
1496       if (bounds->height() < minimum_size.height())
1497         bounds->set_height(minimum_size.height());
1498     }
1499     return true;
1500   }
1501   return false;
1502 }
1503
1504 scoped_ptr<InputMethod> Widget::CreateInputMethod() {
1505   scoped_ptr<InputMethod> input_method(native_widget_->CreateInputMethod());
1506   if (input_method.get())
1507     input_method->Init(this);
1508   return input_method.Pass();
1509 }
1510
1511 void Widget::ReplaceInputMethod(InputMethod* input_method) {
1512   input_method_.reset(input_method);
1513   input_method->SetDelegate(native_widget_->GetInputMethodDelegate());
1514   input_method->Init(this);
1515 }
1516
1517 namespace internal {
1518
1519 ////////////////////////////////////////////////////////////////////////////////
1520 // internal::NativeWidgetPrivate, NativeWidget implementation:
1521
1522 internal::NativeWidgetPrivate* NativeWidgetPrivate::AsNativeWidgetPrivate() {
1523   return this;
1524 }
1525
1526 }  // namespace internal
1527 }  // namespace views