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