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