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 CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_VIEWS_H_
6#define CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_VIEWS_H_
7#pragma once
8
9#include <map>
10#include <string>
11#include <vector>
12
13#include "base/memory/scoped_ptr.h"
14#include "base/time.h"
15#include "content/browser/renderer_host/render_widget_host_view.h"
16#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
17#include "ui/gfx/native_widget_types.h"
18#include "views/controls/native/native_view_host.h"
19#include "views/events/event.h"
20#include "views/ime/text_input_client.h"
21#include "views/view.h"
22#include "webkit/glue/webcursor.h"
23
24class RenderWidgetHost;
25struct NativeWebKeyboardEvent;
26
27// -----------------------------------------------------------------------------
28// See comments in render_widget_host_view.h about this class and its members.
29// -----------------------------------------------------------------------------
30class RenderWidgetHostViewViews : public RenderWidgetHostView,
31                                  public views::View,
32                                  public views::TextInputClient {
33 public:
34  // Internal class name.
35  static const char kViewClassName[];
36
37  explicit RenderWidgetHostViewViews(RenderWidgetHost* widget);
38  virtual ~RenderWidgetHostViewViews();
39
40  // Initialize this object for use as a drawing area.
41  void InitAsChild();
42
43  // RenderWidgetHostView implementation.
44  virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
45                           const gfx::Rect& pos) OVERRIDE;
46  virtual void InitAsFullscreen() OVERRIDE;
47  virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
48  virtual void DidBecomeSelected() OVERRIDE;
49  virtual void WasHidden() OVERRIDE;
50  virtual void SetSize(const gfx::Size& size) OVERRIDE;
51  virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
52  virtual gfx::NativeView GetNativeView() OVERRIDE;
53  virtual void MovePluginWindows(
54      const std::vector<webkit::npapi::WebPluginGeometry>& moves) OVERRIDE;
55  virtual bool HasFocus() OVERRIDE;
56  virtual void Show() OVERRIDE;
57  virtual void Hide() OVERRIDE;
58  virtual bool IsShowing() OVERRIDE;
59  virtual gfx::Rect GetViewBounds() const OVERRIDE;
60  virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
61  virtual void SetIsLoading(bool is_loading) OVERRIDE;
62  virtual void ImeUpdateTextInputState(WebKit::WebTextInputType type,
63                                       const gfx::Rect& caret_rect) OVERRIDE;
64  virtual void ImeCancelComposition() OVERRIDE;
65  virtual void DidUpdateBackingStore(
66      const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy,
67      const std::vector<gfx::Rect>& copy_rects) OVERRIDE;
68  virtual void RenderViewGone(base::TerminationStatus status,
69                              int error_code) OVERRIDE;
70  virtual void Destroy() OVERRIDE;
71  virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh) OVERRIDE {}
72  virtual void SetTooltipText(const std::wstring& tooltip_text) OVERRIDE;
73  virtual void SelectionChanged(const std::string& text) OVERRIDE;
74  virtual void ShowingContextMenu(bool showing) OVERRIDE;
75  virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
76  virtual void SetBackground(const SkBitmap& background) OVERRIDE;
77  virtual void CreatePluginContainer(gfx::PluginWindowHandle id) OVERRIDE;
78  virtual void DestroyPluginContainer(gfx::PluginWindowHandle id) OVERRIDE;
79  virtual void SetVisuallyDeemphasized(const SkColor* color,
80                                       bool animate) OVERRIDE;
81  virtual bool ContainsNativeView(gfx::NativeView native_view) const OVERRIDE;
82  virtual void AcceleratedCompositingActivated(bool activated) OVERRIDE;
83  virtual gfx::PluginWindowHandle GetCompositingSurface() OVERRIDE;
84
85  // On some systems, there can be two native views, where an outer native view
86  // contains the inner native view (e.g. when using GTK+). This returns the
87  // inner view. This can return NULL when it's not attached to a view.
88  gfx::NativeView GetInnerNativeView() const;
89
90  // Overridden from views::View.
91  virtual std::string GetClassName() const OVERRIDE;
92  virtual gfx::NativeCursor GetCursorForPoint(ui::EventType type,
93                                              const gfx::Point& point) OVERRIDE;
94  virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
95  virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
96  virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
97  virtual void OnMouseMoved(const views::MouseEvent& event) OVERRIDE;
98  virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE;
99  virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE;
100  virtual View::TouchStatus OnTouchEvent(
101      const views::TouchEvent& event) OVERRIDE;
102  virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE;
103  virtual bool OnKeyReleased(const views::KeyEvent& event) OVERRIDE;
104  virtual bool OnMouseWheel(const views::MouseWheelEvent& event) OVERRIDE;
105  virtual views::TextInputClient* GetTextInputClient() OVERRIDE;
106
107  // Overridden from TextInputClient:
108  virtual void SetCompositionText(
109      const ui::CompositionText& composition) OVERRIDE;
110  virtual void ConfirmCompositionText() OVERRIDE;
111  virtual void ClearCompositionText() OVERRIDE;
112  virtual void InsertText(const string16& text) OVERRIDE;
113  virtual void InsertChar(char16 ch, int flags) OVERRIDE;
114  virtual ui::TextInputType GetTextInputType() OVERRIDE;
115  virtual gfx::Rect GetCaretBounds() OVERRIDE;
116  virtual bool HasCompositionText() OVERRIDE;
117  virtual bool GetTextRange(ui::Range* range) OVERRIDE;
118  virtual bool GetCompositionTextRange(ui::Range* range) OVERRIDE;
119  virtual bool GetSelectionRange(ui::Range* range) OVERRIDE;
120  virtual bool SetSelectionRange(const ui::Range& range) OVERRIDE;
121  virtual bool DeleteRange(const ui::Range& range) OVERRIDE;
122  virtual bool GetTextFromRange(
123      const ui::Range& range,
124      const base::Callback<void(const string16&)>& callback) OVERRIDE;
125  virtual void OnInputMethodChanged() OVERRIDE;
126  virtual bool ChangeTextDirectionAndLayoutAlignment(
127      base::i18n::TextDirection direction) OVERRIDE;
128  virtual views::View* GetOwnerViewOfTextInputClient() OVERRIDE;
129
130 protected:
131  // Overridden from RenderWidgetHostView / views::View.
132  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
133  virtual void Focus() OVERRIDE;
134  virtual void Blur() OVERRIDE;
135  virtual void OnFocus() OVERRIDE;
136  virtual void OnBlur() OVERRIDE;
137
138 private:
139  friend class RenderWidgetHostViewViewsWidget;
140
141  // Returns whether the widget needs an input grab to work
142  // properly.
143  bool NeedsInputGrab();
144
145  // Returns whether this render view is a popup (<select> dropdown or
146  // autocomplete window).
147  bool IsPopup();
148
149  // Update the display cursor for the render view.
150  void ShowCurrentCursor();
151
152  // Translate a views::MouseEvent into a WebKit::WebMouseEvent.
153  WebKit::WebMouseEvent WebMouseEventFromViewsEvent(
154      const views::MouseEvent& event);
155
156  // Confirm existing composition text in the webpage and ask the input method
157  // to cancel its ongoing composition sesstion.
158  void FinishImeCompositionSession();
159
160  // The model object.
161  RenderWidgetHost* host_;
162
163  // This is true when we are currently painting and thus should handle extra
164  // paint requests by expanding the invalid rect rather than actually
165  // painting.
166  bool about_to_validate_and_paint_;
167
168  // This is the rectangle which we'll paint.
169  gfx::Rect invalid_rect_;
170
171  // Whether or not this widget is hidden.
172  bool is_hidden_;
173
174  // Whether we are currently loading.
175  bool is_loading_;
176
177  // The cursor for the page. This is passed up from the renderer.
178  WebCursor current_cursor_;
179
180  // The native cursor.
181  gfx::NativeCursor native_cursor_;
182
183  // Whether we are showing a context menu.
184  bool is_showing_context_menu_;
185
186  // The time at which this view started displaying white pixels as a result of
187  // not having anything to paint (empty backing store from renderer). This
188  // value returns true for is_null() if we are not recording whiteout times.
189  base::TimeTicks whiteout_start_time_;
190
191  // The time it took after this view was selected for it to be fully painted.
192  base::TimeTicks tab_switch_paint_time_;
193
194  // If true, fade the render widget when painting it.
195  bool visually_deemphasized_;
196
197  // The size that we want the renderer to be.
198  gfx::Size requested_size_;
199
200  // The touch-event. Its touch-points are updated as necessary. A new
201  // touch-point is added from an ET_TOUCH_PRESSED event, and a touch-point is
202  // removed from the list on an ET_TOUCH_RELEASED event.
203  WebKit::WebTouchEvent touch_event_;
204
205  // The current text input type.
206  ui::TextInputType text_input_type_;
207
208  // The current caret bounds.
209  gfx::Rect caret_bounds_;
210
211  // Indicates if there is onging composition text.
212  bool has_composition_text_;
213
214  DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewViews);
215};
216
217#endif  // CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_VIEWS_H_
218