Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / native / native_view_host.h
1 // Copyright (c) 2011 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 #ifndef UI_VIEWS_CONTROLS_NATIVE_NATIVE_VIEW_HOST_H_
6 #define UI_VIEWS_CONTROLS_NATIVE_NATIVE_VIEW_HOST_H_
7
8 #include <string>
9
10 #include "ui/gfx/native_widget_types.h"
11 #include "ui/views/view.h"
12
13 namespace views {
14 namespace test {
15 class NativeViewHostTestBase;
16 }
17
18 class NativeViewHostWrapper;
19
20 // If a NativeViewHost's native view is a Widget, this native window
21 // property is set on the widget, pointing to the owning NativeViewHost.
22 extern const char kWidgetNativeViewHostKey[];
23
24 // A View type that hosts a gfx::NativeView. The bounds of the native view are
25 // kept in sync with the bounds of this view as it is moved and sized.
26 // Under the hood, a platform-specific NativeViewHostWrapper implementation does
27 // the platform-specific work of manipulating the underlying OS widget type.
28 class VIEWS_EXPORT NativeViewHost : public View {
29  public:
30   // The NativeViewHost's class name.
31   static const char kViewClassName[];
32
33   NativeViewHost();
34   virtual ~NativeViewHost();
35
36   // Attach a gfx::NativeView to this View. Its bounds will be kept in sync
37   // with the bounds of this View until Detach is called.
38   //
39   // Because native views are positioned in the coordinates of their parent
40   // native view, this function should only be called after this View has been
41   // added to a View hierarchy hosted within a valid Widget.
42   void Attach(gfx::NativeView native_view);
43
44   // Detach the attached native view. Its bounds and visibility will no
45   // longer be manipulated by this View. The native view may be destroyed and
46   // detached before calling this function, and this has no effect in that case.
47   void Detach();
48
49   // Sets a preferred size for the native view attached to this View.
50   void SetPreferredSize(const gfx::Size& size);
51
52   // A NativeViewHost has an associated focus View so that the focus of the
53   // native control and of the View are kept in sync. In simple cases where the
54   // NativeViewHost directly wraps a native window as is, the associated view
55   // is this View. In other cases where the NativeViewHost is part of another
56   // view (such as TextField), the actual View is not the NativeViewHost and
57   // this method must be called to set that.
58   // This method must be called before Attach().
59   void set_focus_view(View* view) { focus_view_ = view; }
60   View* focus_view() { return focus_view_; }
61
62   // Fast resizing will move the native view and clip its visible region, this
63   // will result in white areas and will not resize the content (so scrollbars
64   // will be all wrong and content will flow offscreen). Only use this
65   // when you're doing extremely quick, high-framerate vertical resizes
66   // and don't care about accuracy. Make sure you do a real resize at the
67   // end. USE WITH CAUTION.
68   void set_fast_resize(bool fast_resize) { fast_resize_ = fast_resize; }
69   bool fast_resize() const { return fast_resize_; }
70
71   // Value of fast_resize() the last time Layout() was invoked.
72   bool fast_resize_at_last_layout() const {
73     return fast_resize_at_last_layout_;
74   }
75
76   // Accessor for |native_view_|.
77   gfx::NativeView native_view() const { return native_view_; }
78
79   void NativeViewDestroyed();
80
81   // Overridden from View:
82   virtual gfx::Size GetPreferredSize() const OVERRIDE;
83   virtual void Layout() OVERRIDE;
84   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
85   virtual void VisibilityChanged(View* starting_from, bool is_visible) OVERRIDE;
86   virtual void OnFocus() OVERRIDE;
87   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
88   virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE;
89
90  protected:
91   virtual bool GetNeedsNotificationWhenVisibleBoundsChange() const OVERRIDE;
92   virtual void OnVisibleBoundsChanged() OVERRIDE;
93   virtual void ViewHierarchyChanged(
94       const ViewHierarchyChangedDetails& details) OVERRIDE;
95   virtual const char* GetClassName() const OVERRIDE;
96
97  private:
98   friend class test::NativeViewHostTestBase;
99
100   // Detach the native view. |destroyed| is true if the native view is
101   // detached because it's being destroyed, or false otherwise.
102   void Detach(bool destroyed);
103
104   // Invokes ViewRemoved() on the FocusManager for all the child Widgets of our
105   // NativeView. This is used when detaching to ensure the FocusManager doesn't
106   // have a reference to a View that is no longer reachable.
107   void ClearFocus();
108
109   // The attached native view. There is exactly one native_view_ attached.
110   gfx::NativeView native_view_;
111
112   // A platform-specific wrapper that does the OS-level manipulation of the
113   // attached gfx::NativeView.
114   scoped_ptr<NativeViewHostWrapper> native_wrapper_;
115
116   // The preferred size of this View
117   gfx::Size preferred_size_;
118
119   // True if the native view is being resized using the fast method described
120   // in the setter/accessor above.
121   bool fast_resize_;
122
123   // Value of |fast_resize_| during the last call to Layout.
124   bool fast_resize_at_last_layout_;
125
126   // The view that should be given focus when this NativeViewHost is focused.
127   View* focus_view_;
128
129   DISALLOW_COPY_AND_ASSIGN(NativeViewHost);
130 };
131
132 }  // namespace views
133
134 #endif  // UI_VIEWS_CONTROLS_NATIVE_NATIVE_VIEW_HOST_H_