- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_view_gtk.h
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 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_GTK_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_GTK_H_
7
8 #include <gdk/gdk.h>
9
10 #include <string>
11 #include <vector>
12
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "content/browser/accessibility/browser_accessibility_manager.h"
16 #include "content/browser/renderer_host/gtk_plugin_container_manager.h"
17 #include "content/browser/renderer_host/render_widget_host_view_base.h"
18 #include "content/common/content_export.h"
19 #include "ipc/ipc_sender.h"
20 #include "ui/base/gtk/gtk_signal.h"
21 #include "ui/base/gtk/gtk_signal_registrar.h"
22 #include "ui/base/gtk/owned_widget_gtk.h"
23 #include "ui/base/x/active_window_watcher_x_observer.h"
24 #include "ui/gfx/native_widget_types.h"
25 #include "ui/gfx/point.h"
26 #include "ui/gfx/rect.h"
27 #include "webkit/common/cursors/webcursor.h"
28
29 typedef struct _GtkClipboard GtkClipboard;
30 typedef struct _GtkSelectionData GtkSelectionData;
31
32 namespace content {
33 class GtkIMContextWrapper;
34 class GtkKeyBindingsHandler;
35 class RenderWidgetHost;
36 class RenderWidgetHostImpl;
37 struct NativeWebKeyboardEvent;
38
39 // -----------------------------------------------------------------------------
40 // See comments in render_widget_host_view.h about this class and its members.
41 // -----------------------------------------------------------------------------
42 class CONTENT_EXPORT RenderWidgetHostViewGtk
43     : public RenderWidgetHostViewBase,
44       public BrowserAccessibilityDelegate,
45       public ui::ActiveWindowWatcherXObserver,
46       public IPC::Sender {
47  public:
48   virtual ~RenderWidgetHostViewGtk();
49
50   // RenderWidgetHostView implementation.
51   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
52   virtual void InitAsChild(gfx::NativeView parent_view) OVERRIDE;
53   virtual RenderWidgetHost* GetRenderWidgetHost() const OVERRIDE;
54   virtual void SetSize(const gfx::Size& size) OVERRIDE;
55   virtual void SetBounds(const gfx::Rect& rect) OVERRIDE;
56   virtual gfx::NativeView GetNativeView() const OVERRIDE;
57   virtual gfx::NativeViewId GetNativeViewId() const OVERRIDE;
58   virtual gfx::NativeViewAccessible GetNativeViewAccessible() OVERRIDE;
59   virtual bool HasFocus() const OVERRIDE;
60   virtual bool IsSurfaceAvailableForCopy() const OVERRIDE;
61   virtual void Show() OVERRIDE;
62   virtual void Hide() OVERRIDE;
63   virtual bool IsShowing() OVERRIDE;
64   virtual gfx::Rect GetViewBounds() const OVERRIDE;
65   virtual GdkEventButton* GetLastMouseDown() OVERRIDE;
66   virtual gfx::NativeView BuildInputMethodsGtkMenu() OVERRIDE;
67   virtual void SetBackground(const SkBitmap& background) OVERRIDE;
68
69   // RenderWidgetHostViewPort implementation.
70   virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
71                            const gfx::Rect& pos) OVERRIDE;
72   virtual void InitAsFullscreen(
73       RenderWidgetHostView* reference_host_view) OVERRIDE;
74   virtual void WasShown() OVERRIDE;
75   virtual void WasHidden() OVERRIDE;
76   virtual void MovePluginWindows(
77       const gfx::Vector2d& scroll_offset,
78       const std::vector<WebPluginGeometry>& moves) OVERRIDE;
79   virtual void Focus() OVERRIDE;
80   virtual void Blur() OVERRIDE;
81   virtual void UpdateCursor(const WebCursor& cursor) OVERRIDE;
82   virtual void SetIsLoading(bool is_loading) OVERRIDE;
83   virtual void TextInputTypeChanged(ui::TextInputType type,
84                                     ui::TextInputMode input_mode,
85                                     bool can_compose_inline) OVERRIDE;
86   virtual void ImeCancelComposition() OVERRIDE;
87   virtual void DidUpdateBackingStore(
88       const gfx::Rect& scroll_rect,
89       const gfx::Vector2d& scroll_delta,
90       const std::vector<gfx::Rect>& copy_rects,
91       const ui::LatencyInfo& latency_info) OVERRIDE;
92   virtual void RenderProcessGone(base::TerminationStatus status,
93                                  int error_code) OVERRIDE;
94   virtual void Destroy() OVERRIDE;
95   virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh) {}
96   virtual void SetTooltipText(const string16& tooltip_text) OVERRIDE;
97   virtual void SelectionChanged(const string16& text,
98                                 size_t offset,
99                                 const gfx::Range& range) OVERRIDE;
100   virtual void SelectionBoundsChanged(
101       const ViewHostMsg_SelectionBounds_Params& params) OVERRIDE;
102   virtual void ScrollOffsetChanged() OVERRIDE;
103   virtual BackingStore* AllocBackingStore(const gfx::Size& size) OVERRIDE;
104   virtual void CopyFromCompositingSurface(
105       const gfx::Rect& src_subrect,
106       const gfx::Size& dst_size,
107       const base::Callback<void(bool, const SkBitmap&)>& callback) OVERRIDE;
108   virtual void CopyFromCompositingSurfaceToVideoFrame(
109       const gfx::Rect& src_subrect,
110       const scoped_refptr<media::VideoFrame>& target,
111       const base::Callback<void(bool)>& callback) OVERRIDE;
112   virtual bool CanCopyToVideoFrame() const OVERRIDE;
113   virtual void OnAcceleratedCompositingStateChange() OVERRIDE;
114   virtual void AcceleratedSurfaceBuffersSwapped(
115       const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params,
116       int gpu_host_id) OVERRIDE;
117   virtual void AcceleratedSurfacePostSubBuffer(
118       const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params& params,
119       int gpu_host_id) OVERRIDE;
120   virtual void AcceleratedSurfaceSuspend() OVERRIDE;
121   virtual void AcceleratedSurfaceRelease() OVERRIDE;
122   virtual bool HasAcceleratedSurface(const gfx::Size& desired_size) OVERRIDE;
123   virtual void SetHasHorizontalScrollbar(
124       bool has_horizontal_scrollbar) OVERRIDE;
125   virtual void SetScrollOffsetPinning(
126       bool is_pinned_to_left, bool is_pinned_to_right) OVERRIDE;
127   virtual void GetScreenInfo(WebKit::WebScreenInfo* results) OVERRIDE;
128   virtual gfx::Rect GetBoundsInRootWindow() OVERRIDE;
129   virtual gfx::GLSurfaceHandle GetCompositingSurface() OVERRIDE;
130   virtual void ResizeCompositingSurface(const gfx::Size&) OVERRIDE;
131   virtual bool LockMouse() OVERRIDE;
132   virtual void UnlockMouse() OVERRIDE;
133   virtual void OnAccessibilityEvents(
134       const std::vector<AccessibilityHostMsg_EventParams>& params)
135       OVERRIDE;
136
137   // ActiveWindowWatcherXObserver implementation.
138   virtual void ActiveWindowChanged(GdkWindow* active_window) OVERRIDE;
139
140   // IPC::Sender implementation:
141   virtual bool Send(IPC::Message* message) OVERRIDE;
142
143   // If the widget is aligned with an edge of the monitor its on and the user
144   // attempts to drag past that edge we track the number of times it has
145   // occurred, so that we can force the widget to scroll when it otherwise
146   // would be unable to.
147   void ModifyEventForEdgeDragging(GtkWidget* widget, GdkEventMotion* event);
148
149   // Mouse events always provide a movementX/Y which needs to be computed.
150   // Also, mouse lock requires knowledge of last unlocked cursor coordinates.
151   // State is stored on the host view to do this, and the mouse event modified.
152   void ModifyEventMovementAndCoords(WebKit::WebMouseEvent* event);
153
154   void Paint(const gfx::Rect&);
155
156   // Called by GtkIMContextWrapper to forward a keyboard event to renderer.
157   // On Linux (not ChromeOS):
158   // Before calling RenderWidgetHost::ForwardKeyboardEvent(), this method
159   // calls GtkKeyBindingsHandler::Match() against the event and send matched
160   // edit commands to renderer by calling
161   // RenderWidgetHost::ForwardEditCommandsForNextKeyEvent().
162   void ForwardKeyboardEvent(const NativeWebKeyboardEvent& event);
163
164   bool RetrieveSurrounding(std::string* text, size_t* cursor_index);
165
166   // BrowserAccessibilityDelegate implementation.
167   virtual void SetAccessibilityFocus(int acc_obj_id) OVERRIDE;
168   virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
169   virtual void AccessibilityScrollToMakeVisible(
170       int acc_obj_id, gfx::Rect subfocus) OVERRIDE;
171   virtual void AccessibilityScrollToPoint(
172       int acc_obj_id, gfx::Point point) OVERRIDE;
173   virtual void AccessibilitySetTextSelection(
174       int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
175   virtual gfx::Point GetLastTouchEventLocation() const OVERRIDE;
176   virtual void FatalAccessibilityTreeError() OVERRIDE;
177
178   // Get the root of the AtkObject* tree for accessibility.
179   AtkObject* GetAccessible();
180
181  protected:
182   friend class RenderWidgetHostView;
183
184   // Should construct only via RenderWidgetHostView::CreateViewForWidget.
185   explicit RenderWidgetHostViewGtk(RenderWidgetHost* widget);
186
187  private:
188   friend class RenderWidgetHostViewGtkWidget;
189
190   CHROMEGTK_CALLBACK_0(RenderWidgetHostViewGtk,
191                        void,
192                        OnDestroy);
193
194   // Returns whether the widget needs an input grab (GTK+ and X) to work
195   // properly.
196   bool NeedsInputGrab();
197
198   // Returns whether this render view is a popup (<select> dropdown or
199   // autocomplete window).
200   bool IsPopup() const;
201
202   // Do initialization needed by all InitAs*() methods.
203   void DoSharedInit();
204
205   // Do initialization needed just by InitAsPopup() and InitAsFullscreen().
206   // We move and resize |window| to |bounds| and show it and its contents.
207   void DoPopupOrFullscreenInit(GtkWindow* window, const gfx::Rect& bounds);
208
209   // Update the display cursor for the render view.
210   void ShowCurrentCursor();
211
212   void set_last_mouse_down(GdkEventButton* event);
213
214   // Cause the next query for the widget center to recompute the cached value.
215   void MarkCachedWidgetCenterStale();
216
217   void OnCreatePluginContainer(gfx::PluginWindowHandle id);
218   void OnDestroyPluginContainer(gfx::PluginWindowHandle id);
219
220   gfx::Point GetWidgetCenter();
221
222   // The model object.
223   RenderWidgetHostImpl* host_;
224
225   // The native UI widget.
226   ui::OwnedWidgetGtk view_;
227
228   // This is true when we are currently painting and thus should handle extra
229   // paint requests by expanding the invalid rect rather than actually
230   // painting.
231   bool about_to_validate_and_paint_;
232
233   // This is the rectangle which we'll paint.
234   gfx::Rect invalid_rect_;
235
236   // Whether we are currently loading.
237   bool is_loading_;
238
239   // The cursor for the page. This is passed up from the renderer.
240   WebCursor current_cursor_;
241
242   // The time at which this view started displaying white pixels as a result of
243   // not having anything to paint (empty backing store from renderer). This
244   // value returns true for is_null() if we are not recording whiteout times.
245   base::TimeTicks whiteout_start_time_;
246
247   // The time it took after this view was selected for it to be fully painted.
248   base::TimeTicks web_contents_switch_paint_time_;
249
250   // The native view of our parent widget.  Used only for popups.
251   GtkWidget* parent_;
252
253   // We ignore the first mouse release on popups so the popup will remain open.
254   bool is_popup_first_mouse_release_;
255
256   // Whether or not this widget's input context was focused before being
257   // shadowed by another widget. Used in OnGrabNotify() handler to track the
258   // focused state correctly.
259   bool was_imcontext_focused_before_grab_;
260
261   // True if we are responsible for creating an X grab. This will only be used
262   // for <select> dropdowns. It should be true for most such cases, but false
263   // for extension popups.
264   bool do_x_grab_;
265
266   // Is the widget fullscreen?
267   bool is_fullscreen_;
268
269   // Has the window ever been marked active? Only valid for fullscreen or
270   // popup windows.
271   bool made_active_;
272
273   // Used to record the last position of the mouse.
274   // While the mouse is locked, they store the last known position just as mouse
275   // lock was entered.
276   // Relative to the upper-left corner of the view.
277   gfx::Point unlocked_mouse_position_;
278   // Relative to the upper-left corner of the screen.
279   gfx::Point unlocked_global_mouse_position_;
280   // Last hidden cursor position. Relative to screen.
281   gfx::Point global_mouse_position_;
282   // Indicates when mouse motion is valid after the widget has moved.
283   bool mouse_has_been_warped_to_new_center_;
284   // Indicates the cursor has been warped to the unlocked position,
285   // but a move event has not yet been received for it there.
286   bool mouse_is_being_warped_to_unlocked_position_;
287
288   // For full-screen windows we have a OnDestroy handler that we need to remove,
289   // so we keep it ID here.
290   unsigned long destroy_handler_id_;
291
292   // A convenience wrapper object for GtkIMContext;
293   scoped_ptr<GtkIMContextWrapper> im_context_;
294
295   // A convenience object for handling editor key bindings defined in gtk
296   // keyboard theme.
297   scoped_ptr<GtkKeyBindingsHandler> key_bindings_handler_;
298
299   // Helper class that lets us allocate plugin containers and move them.
300   GtkPluginContainerManager plugin_container_manager_;
301
302   // The size that we want the renderer to be.  We keep this in a separate
303   // variable because resizing in GTK+ is async.
304   gfx::Size requested_size_;
305
306   // The latest reported center of the widget, use GetWidgetCenter() to access.
307   gfx::Point widget_center_;
308   // If the window moves the widget_center will not be valid until we recompute.
309   bool widget_center_valid_;
310
311   // The number of times the user has dragged against horizontal edge  of the
312   // monitor (if the widget is aligned with that edge). Negative values
313   // indicate the left edge, positive the right.
314   int dragged_at_horizontal_edge_;
315
316   // The number of times the user has dragged against vertical edge  of the
317   // monitor (if the widget is aligned with that edge). Negative values
318   // indicate the top edge, positive the bottom.
319   int dragged_at_vertical_edge_;
320
321   gfx::PluginWindowHandle compositing_surface_;
322
323   // The event for the last mouse down we handled. We need this for context
324   // menus and drags.
325   GdkEventButton* last_mouse_down_;
326
327   // Instance of accessibility information for the root of the AtkObject
328   // tree representation of the WebKit render tree.
329   scoped_ptr<BrowserAccessibilityManager> browser_accessibility_manager_;
330
331   ui::GtkSignalRegistrar signals_;
332
333   ui::LatencyInfo software_latency_info_;
334 };
335
336 }  // namespace content
337
338 #endif  // CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_GTK_H_