summaryrefslogtreecommitdiffstats
path: root/views/controls/native/native_view_host.h
blob: d27a816e88784f4360733a0193cbe370f9617691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_CONTROLS_NATIVE_VIEW_HOST_H_
#define VIEWS_CONTROLS_NATIVE_VIEW_HOST_H_

#include <string>

#include "views/view.h"

#include "base/gfx/native_widget_types.h"

namespace views {

class NativeViewHostWrapper;

// A View type that hosts a gfx::NativeView. The bounds of the native view are
// kept in sync with the bounds of this view as it is moved and sized.
// Under the hood, a platform-specific NativeViewHostWrapper implementation does
// the platform-specific work of manipulating the underlying OS widget type.
class NativeViewHost : public View {
 public:
  // The NativeViewHost's class name.
  static const char kViewClassName[];

  NativeViewHost();
  virtual ~NativeViewHost();

  // Attach a gfx::NativeView to this View. Its bounds will be kept in sync
  // with the bounds of this View until Detach is called.
  //
  // Because native views are positioned in the coordinates of their parent
  // native view, this function should only be called after this View has been
  // added to a View hierarchy hosted within a valid Widget.
  void Attach(gfx::NativeView native_view);

  // Detach the attached window handle. Its bounds and visibility will no longer
  // be manipulated by this View.
  void Detach();

  // Sets a preferred size for the native view attached to this View.
  void SetPreferredSize(const gfx::Size& size);

  // A NativeViewHost must keep the focused view in the focus manager and the
  // native focus with in sync . There are 2 aspects to this:
  // - when the native view receives focus, the focus manager must be notified
  //   that the associated view is now the focused view.
  //   In simple cases where the NativeViewHost directly wraps a native window
  //   as is, the associated view is this NativeViewHost. In other cases where
  //   the NativeViewHost is part of another view (such as for the TextField
  //   class), the actual View is not the NativeViewHost and set_focus_view()
  //   must be called to set the associated view before Attach() is called.
  // - when the view is focused (by calling View::RequestFocus()), it must focus
  //   the appropriate native view.  In simple cases where the native view does
  //   not have children or is the native view that should really get the focus,
  //   this works without doing anything.  In case where the native view that
  //   should get the focus is not the native view passed to Attach(), then
  //   the appropriate native view should be specified to
  //   set_focus_native_view() before Attach() is called.
  void set_focus_view(View* view) { focus_view_ = view; }
  void set_focus_native_view(gfx::NativeView native_view) {
    focus_native_view_ = native_view;
  }
  gfx::NativeView focus_native_view() const { return focus_native_view_; }

  // Fast resizing will move the native view and clip its visible region, this
  // will result in white areas and will not resize the content (so scrollbars
  // will be all wrong and content will flow offscreen). Only use this
  // when you're doing extremely quick, high-framerate vertical resizes
  // and don't care about accuracy. Make sure you do a real resize at the
  // end. USE WITH CAUTION.
  void set_fast_resize(bool fast_resize) { fast_resize_ = fast_resize; }
  bool fast_resize() const { return fast_resize_; }

  // Accessor for |native_view_|.
  gfx::NativeView native_view() const { return native_view_; }

  // Called by the NativeViewHostWrapper to notify that the |focus_native_view_|
  // got focus.
  void GotNativeFocus();

  void NativeViewDestroyed();

  // Overridden from View:
  virtual gfx::Size GetPreferredSize();
  virtual void Layout();
  virtual void Paint(gfx::Canvas* canvas);
  virtual void VisibilityChanged(View* starting_from, bool is_visible);
  virtual void Focus();

 protected:
  virtual void VisibleBoundsInRootChanged();
  virtual void ViewHierarchyChanged(bool is_add, View* parent, View* child);
  virtual std::string GetClassName() const;

 private:
  // The attached native view.
  gfx::NativeView native_view_;

  // A platform-specific wrapper that does the OS-level manipulation of the
  // attached gfx::NativeView.
  scoped_ptr<NativeViewHostWrapper> native_wrapper_;

  // The preferred size of this View
  gfx::Size preferred_size_;

  // True if the native view is being resized using the fast method described
  // in the setter/accessor above.
  bool fast_resize_;

  // The view that should be given focus when this NativeViewHost is focused.
  View* focus_view_;

  // The native view that should get the focus when this View gets focused.
  gfx::NativeView focus_native_view_;

  DISALLOW_COPY_AND_ASSIGN(NativeViewHost);
};

}  // namespace views

#endif  // VIEWS_CONTROLS_NATIVE_VIEW_HOST_H_