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