render_widget_host_view_gtk.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
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_GTK_H_
6#define CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_GTK_H_
7#pragma once
8
9#include <gdk/gdk.h>
10
11#include <string>
12#include <vector>
13
14#include "base/memory/scoped_ptr.h"
15#include "base/time.h"
16#include "chrome/browser/ui/gtk/owned_widget_gtk.h"
17#include "content/browser/renderer_host/render_widget_host_view.h"
18#include "ui/base/animation/animation_delegate.h"
19#include "ui/base/animation/slide_animation.h"
20#include "ui/base/gtk/gtk_signal.h"
21#include "ui/gfx/native_widget_types.h"
22#include "ui/gfx/rect.h"
23#include "webkit/glue/webcursor.h"
24#include "webkit/plugins/npapi/gtk_plugin_container_manager.h"
25
26class RenderWidgetHost;
27class GtkIMContextWrapper;
28class GtkKeyBindingsHandler;
29#if !defined(TOOLKIT_VIEWS)
30class MenuGtk;
31#endif
32struct NativeWebKeyboardEvent;
33
34#if defined(OS_CHROMEOS)
35namespace views {
36class TooltipWindowGtk;
37}
38#endif  // defined(OS_CHROMEOS)
39
40typedef struct _GtkClipboard GtkClipboard;
41typedef struct _GtkSelectionData GtkSelectionData;
42
43// -----------------------------------------------------------------------------
44// See comments in render_widget_host_view.h about this class and its members.
45// -----------------------------------------------------------------------------
46class RenderWidgetHostViewGtk : public RenderWidgetHostView,
47                                public ui::AnimationDelegate {
48 public:
49  explicit RenderWidgetHostViewGtk(RenderWidgetHost* widget);
50  ~RenderWidgetHostViewGtk();
51
52  // Initialize this object for use as a drawing area.
53  void InitAsChild();
54
55  // RenderWidgetHostView implementation.
56  virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
57                           const gfx::Rect& pos);
58  virtual void InitAsFullscreen();
59  virtual RenderWidgetHost* GetRenderWidgetHost() const;
60  virtual void DidBecomeSelected();
61  virtual void WasHidden();
62  virtual void SetSize(const gfx::Size& size);
63  virtual void SetBounds(const gfx::Rect& rect);
64  virtual gfx::NativeView GetNativeView();
65  virtual void MovePluginWindows(
66      const std::vector<webkit::npapi::WebPluginGeometry>& moves);
67  virtual void Focus();
68  virtual void Blur();
69  virtual bool HasFocus();
70  virtual void Show();
71  virtual void Hide();
72  virtual bool IsShowing();
73  virtual gfx::Rect GetViewBounds() const;
74  virtual void UpdateCursor(const WebCursor& cursor);
75  virtual void SetIsLoading(bool is_loading);
76  virtual void ImeUpdateTextInputState(WebKit::WebTextInputType type,
77                                       const gfx::Rect& caret_rect);
78  virtual void ImeCancelComposition();
79  virtual void DidUpdateBackingStore(
80      const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy,
81      const std::vector<gfx::Rect>& copy_rects);
82  virtual void RenderViewGone(base::TerminationStatus status,
83                              int error_code);
84  virtual void Destroy();
85  virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh) {}
86  virtual void SetTooltipText(const std::wstring& tooltip_text);
87  virtual void SelectionChanged(const std::string& text);
88  virtual void ShowingContextMenu(bool showing);
89  virtual BackingStore* AllocBackingStore(const gfx::Size& size);
90  virtual void SetBackground(const SkBitmap& background);
91  virtual void CreatePluginContainer(gfx::PluginWindowHandle id);
92  virtual void DestroyPluginContainer(gfx::PluginWindowHandle id);
93  virtual void SetVisuallyDeemphasized(const SkColor* color, bool animate);
94  virtual bool ContainsNativeView(gfx::NativeView native_view) const;
95
96  virtual void AcceleratedCompositingActivated(bool activated);
97  virtual gfx::PluginWindowHandle GetCompositingSurface();
98
99  // ui::AnimationDelegate implementation.
100  virtual void AnimationEnded(const ui::Animation* animation);
101  virtual void AnimationProgressed(const ui::Animation* animation);
102  virtual void AnimationCanceled(const ui::Animation* animation);
103
104  gfx::NativeView native_view() const { return view_.get(); }
105
106  // If the widget is aligned with an edge of the monitor its on and the user
107  // attempts to drag past that edge we track the number of times it has
108  // occurred, so that we can force the widget to scroll when it otherwise
109  // would be unable to.
110  void ModifyEventForEdgeDragging(GtkWidget* widget, GdkEventMotion* event);
111  void Paint(const gfx::Rect&);
112
113  // Called by GtkIMContextWrapper to forward a keyboard event to renderer.
114  // Before calling RenderWidgetHost::ForwardKeyboardEvent(), this method
115  // calls GtkKeyBindingsHandler::Match() against the event and send matched
116  // edit commands to renderer by calling
117  // RenderWidgetHost::ForwardEditCommandsForNextKeyEvent().
118  void ForwardKeyboardEvent(const NativeWebKeyboardEvent& event);
119
120  GdkEventButton* last_mouse_down() const {
121    return last_mouse_down_;
122  }
123
124#if !defined(TOOLKIT_VIEWS)
125  // Appends the input methods context menu to the specified |menu| object as a
126  // submenu.
127  void AppendInputMethodsContextMenu(MenuGtk* menu);
128#endif
129
130 private:
131  friend class RenderWidgetHostViewGtkWidget;
132
133  CHROMEGTK_CALLBACK_1(RenderWidgetHostViewGtk,
134                       gboolean,
135                       OnWindowStateEvent,
136                       GdkEventWindowState*);
137
138  CHROMEGTK_CALLBACK_0(RenderWidgetHostViewGtk,
139                       void,
140                       OnDestroy);
141
142  // Returns whether the widget needs an input grab (GTK+ and X) to work
143  // properly.
144  bool NeedsInputGrab();
145
146  // Returns whether this render view is a popup (<select> dropdown or
147  // autocomplete window).
148  bool IsPopup() const;
149
150  // Do initialization needed by all InitAs*() methods.
151  void DoSharedInit();
152
153  // Do initialization needed just by InitAsPopup() and InitAsFullscreen().
154  // We move and resize |window| to |bounds| and show it and its contents.
155  void DoPopupOrFullscreenInit(GtkWindow* window, const gfx::Rect& bounds);
156
157  // Update the display cursor for the render view.
158  void ShowCurrentCursor();
159
160  void set_last_mouse_down(GdkEventButton* event);
161
162  // The model object.
163  RenderWidgetHost* host_;
164
165  // The native UI widget.
166  OwnedWidgetGtk view_;
167
168  // This is true when we are currently painting and thus should handle extra
169  // paint requests by expanding the invalid rect rather than actually
170  // painting.
171  bool about_to_validate_and_paint_;
172
173  // This is the rectangle which we'll paint.
174  gfx::Rect invalid_rect_;
175
176  // Whether or not this widget is hidden.
177  bool is_hidden_;
178
179  // Whether we are currently loading.
180  bool is_loading_;
181
182  // The cursor for the page. This is passed up from the renderer.
183  WebCursor current_cursor_;
184
185  // Whether we are showing a context menu.
186  bool is_showing_context_menu_;
187
188  // The time at which this view started displaying white pixels as a result of
189  // not having anything to paint (empty backing store from renderer). This
190  // value returns true for is_null() if we are not recording whiteout times.
191  base::TimeTicks whiteout_start_time_;
192
193  // The time it took after this view was selected for it to be fully painted.
194  base::TimeTicks tab_switch_paint_time_;
195
196  // A color we use to shade the entire render view. If 100% transparent, we do
197  // not shade the render view.
198  SkColor overlay_color_;
199
200  // The animation used for the abovementioned shade effect. The animation's
201  // value affects the alpha we use for |overlay_color_|.
202  ui::SlideAnimation overlay_animation_;
203
204  // The native view of our parent widget.  Used only for popups.
205  GtkWidget* parent_;
206
207  // We ignore the first mouse release on popups so the popup will remain open.
208  bool is_popup_first_mouse_release_;
209
210  // Whether or not this widget was focused before shadowed by another widget.
211  // Used in OnGrabNotify() handler to track the focused state correctly.
212  bool was_focused_before_grab_;
213
214  // True if we are responsible for creating an X grab. This will only be used
215  // for <select> dropdowns. It should be true for most such cases, but false
216  // for extension popups.
217  bool do_x_grab_;
218
219  // Is the widget fullscreen?
220  bool is_fullscreen_;
221
222  // For full-screen windows we have a OnDestroy handler that we need to remove,
223  // so we keep it ID here.
224  unsigned long destroy_handler_id_;
225
226  // A convenience wrapper object for GtkIMContext;
227  scoped_ptr<GtkIMContextWrapper> im_context_;
228
229  // A convenience object for handling editor key bindings defined in gtk
230  // keyboard theme.
231  scoped_ptr<GtkKeyBindingsHandler> key_bindings_handler_;
232
233  // Helper class that lets us allocate plugin containers and move them.
234  webkit::npapi::GtkPluginContainerManager plugin_container_manager_;
235
236  // The size that we want the renderer to be.  We keep this in a separate
237  // variable because resizing in GTK+ is async.
238  gfx::Size requested_size_;
239
240  // The number of times the user has dragged against horizontal edge  of the
241  // monitor (if the widget is aligned with that edge). Negative values
242  // indicate the left edge, positive the right.
243  int dragged_at_horizontal_edge_;
244
245  // The number of times the user has dragged against vertical edge  of the
246  // monitor (if the widget is aligned with that edge). Negative values
247  // indicate the top edge, positive the bottom.
248  int dragged_at_vertical_edge_;
249
250  gfx::PluginWindowHandle compositing_surface_;
251
252  // The event for the last mouse down we handled. We need this for context
253  // menus and drags.
254  GdkEventButton* last_mouse_down_;
255
256#if defined(OS_CHROMEOS)
257  // Custimized tooltip window.
258  scoped_ptr<views::TooltipWindowGtk> tooltip_window_;
259#endif  // defined(OS_CHROMEOS)
260};
261
262#endif  // CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_GTK_H_
263