1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/tab_contents/tab_contents_view_gtk.h"
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include "app/gtk_dnd_util.h"
#include "base/gfx/point.h"
#include "base/gfx/rect.h"
#include "base/gfx/size.h"
#include "base/pickle.h"
#include "base/string_util.h"
#include "build/build_config.h"
#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/gtk/blocked_popup_container_view_gtk.h"
#include "chrome/browser/gtk/browser_window_gtk.h"
#include "chrome/browser/gtk/constrained_window_gtk.h"
#include "chrome/browser/gtk/gtk_floating_container.h"
#include "chrome/browser/gtk/gtk_theme_provider.h"
#include "chrome/browser/gtk/sad_tab_gtk.h"
#include "chrome/browser/gtk/tab_contents_drag_source.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_view_host_factory.h"
#include "chrome/browser/renderer_host/render_widget_host_view_gtk.h"
#include "chrome/browser/tab_contents/interstitial_page.h"
#include "chrome/browser/tab_contents/render_view_context_menu_gtk.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_delegate.h"
#include "chrome/browser/tab_contents/web_drag_dest_gtk.h"
#include "chrome/common/gtk_util.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_source.h"
#include "chrome/common/notification_type.h"
#include "webkit/glue/webdropdata.h"
using WebKit::WebDragOperation;
using WebKit::WebDragOperationsMask;
namespace {
// TODO(erg): I have no idea how to programatically figure out how wide the
// vertical scrollbar is. Hack it with a hardcoded value for now.
const int kScrollbarWidthHack = 25;
// Called when the content view gtk widget is tabbed to, or after the call to
// gtk_widget_child_focus() in TakeFocus(). We return true
// and grab focus if we don't have it. The call to
// FocusThroughTabTraversal(bool) forwards the "move focus forward" effect to
// webkit.
gboolean OnFocus(GtkWidget* widget, GtkDirectionType focus,
TabContents* tab_contents) {
// If we already have focus, let the next widget have a shot at it. We will
// reach this situation after the call to gtk_widget_child_focus() in
// TakeFocus().
if (gtk_widget_is_focus(widget))
return FALSE;
gtk_widget_grab_focus(widget);
bool reverse = focus == GTK_DIR_TAB_BACKWARD;
tab_contents->FocusThroughTabTraversal(reverse);
return TRUE;
}
// Called when the mouse leaves the widget. We notify our delegate.
gboolean OnLeaveNotify(GtkWidget* widget, GdkEventCrossing* event,
TabContents* tab_contents) {
if (tab_contents->delegate())
tab_contents->delegate()->ContentsMouseEvent(
tab_contents, gfx::Point(event->x_root, event->y_root), false);
return FALSE;
}
// Called when the mouse moves within the widget. We notify our delegate.
gboolean OnMouseMove(GtkWidget* widget, GdkEventMotion* event,
TabContents* tab_contents) {
if (tab_contents->delegate())
tab_contents->delegate()->ContentsMouseEvent(
tab_contents, gfx::Point(event->x_root, event->y_root), true);
return FALSE;
}
// See tab_contents_view_win.cc for discussion of mouse scroll zooming.
gboolean OnMouseScroll(GtkWidget* widget, GdkEventScroll* event,
TabContents* tab_contents) {
if ((event->state & gtk_accelerator_get_default_mod_mask()) ==
GDK_CONTROL_MASK) {
if (event->direction == GDK_SCROLL_DOWN) {
tab_contents->delegate()->ContentsZoomChange(false);
return TRUE;
} else if (event->direction == GDK_SCROLL_UP) {
tab_contents->delegate()->ContentsZoomChange(true);
return TRUE;
}
}
return FALSE;
}
// Used with gtk_container_foreach to change the sizes of the children of
// |fixed_|.
void SetSizeRequest(GtkWidget* widget, gpointer userdata) {
gfx::Size* size = static_cast<gfx::Size*>(userdata);
if (widget->allocation.width != size->width() ||
widget->allocation.height != size->height()) {
gtk_widget_set_size_request(widget, size->width(), size->height());
}
}
} // namespace
// static
TabContentsView* TabContentsView::Create(TabContents* tab_contents) {
return new TabContentsViewGtk(tab_contents);
}
TabContentsViewGtk::TabContentsViewGtk(TabContents* tab_contents)
: TabContentsView(tab_contents),
floating_(gtk_floating_container_new()),
fixed_(gtk_fixed_new()),
popup_view_(NULL) {
gtk_widget_set_name(fixed_, "chrome-tab-contents-view");
g_signal_connect(fixed_, "size-allocate",
G_CALLBACK(OnSizeAllocate), this);
g_signal_connect(floating_.get(), "set-floating-position",
G_CALLBACK(OnSetFloatingPosition), this);
gtk_container_add(GTK_CONTAINER(floating_.get()), fixed_);
gtk_widget_show(fixed_);
gtk_widget_show(floating_.get());
registrar_.Add(this, NotificationType::TAB_CONTENTS_CONNECTED,
Source<TabContents>(tab_contents));
drag_source_.reset(new TabContentsDragSource(this));
}
TabContentsViewGtk::~TabContentsViewGtk() {
floating_.Destroy();
}
void TabContentsViewGtk::AttachBlockedPopupView(
BlockedPopupContainerViewGtk* popup_view) {
DCHECK(popup_view_ == NULL);
popup_view_ = popup_view;
gtk_floating_container_add_floating(GTK_FLOATING_CONTAINER(floating_.get()),
popup_view->widget());
}
void TabContentsViewGtk::RemoveBlockedPopupView(
BlockedPopupContainerViewGtk* popup_view) {
DCHECK(popup_view_ == popup_view);
gtk_container_remove(GTK_CONTAINER(floating_.get()), popup_view->widget());
popup_view_ = NULL;
}
void TabContentsViewGtk::AttachConstrainedWindow(
ConstrainedWindowGtk* constrained_window) {
DCHECK(find(constrained_windows_.begin(), constrained_windows_.end(),
constrained_window) == constrained_windows_.end());
constrained_windows_.push_back(constrained_window);
gtk_floating_container_add_floating(GTK_FLOATING_CONTAINER(floating_.get()),
constrained_window->widget());
}
void TabContentsViewGtk::RemoveConstrainedWindow(
ConstrainedWindowGtk* constrained_window) {
std::vector<ConstrainedWindowGtk*>::iterator item =
find(constrained_windows_.begin(), constrained_windows_.end(),
constrained_window);
DCHECK(item != constrained_windows_.end());
gtk_container_remove(GTK_CONTAINER(floating_.get()),
constrained_window->widget());
constrained_windows_.erase(item);
}
void TabContentsViewGtk::CreateView(const gfx::Size& initial_size) {
requested_size_ = initial_size;
gtk_widget_set_size_request(fixed_, requested_size_.width(),
requested_size_.height());
}
RenderWidgetHostView* TabContentsViewGtk::CreateViewForWidget(
RenderWidgetHost* render_widget_host) {
if (render_widget_host->view()) {
// During testing, the view will already be set up in most cases to the
// test view, so we don't want to clobber it with a real one. To verify that
// this actually is happening (and somebody isn't accidentally creating the
// view twice), we check for the RVH Factory, which will be set when we're
// making special ones (which go along with the special views).
DCHECK(RenderViewHostFactory::has_factory());
return render_widget_host->view();
}
RenderWidgetHostViewGtk* view =
new RenderWidgetHostViewGtk(render_widget_host);
view->InitAsChild();
gfx::NativeView content_view = view->native_view();
g_signal_connect(content_view, "focus",
G_CALLBACK(OnFocus), tab_contents());
g_signal_connect(content_view, "leave-notify-event",
G_CALLBACK(OnLeaveNotify), tab_contents());
g_signal_connect(content_view, "motion-notify-event",
G_CALLBACK(OnMouseMove), tab_contents());
g_signal_connect(content_view, "scroll-event",
G_CALLBACK(OnMouseScroll), tab_contents());
gtk_widget_add_events(content_view, GDK_LEAVE_NOTIFY_MASK |
GDK_POINTER_MOTION_MASK);
g_signal_connect(content_view, "button-press-event",
G_CALLBACK(OnMouseDown), this);
InsertIntoContentArea(content_view);
// Renderer target DnD.
drag_dest_.reset(new WebDragDestGtk(tab_contents(), content_view));
return view;
}
gfx::NativeView TabContentsViewGtk::GetNativeView() const {
return floating_.get();
}
gfx::NativeView TabContentsViewGtk::GetContentNativeView() const {
if (!tab_contents()->render_widget_host_view())
return NULL;
return tab_contents()->render_widget_host_view()->GetNativeView();
}
gfx::NativeWindow TabContentsViewGtk::GetTopLevelNativeWindow() const {
GtkWidget* window = gtk_widget_get_ancestor(GetNativeView(), GTK_TYPE_WINDOW);
return window ? GTK_WINDOW(window) : NULL;
}
void TabContentsViewGtk::GetContainerBounds(gfx::Rect* out) const {
// This is used for positioning the download shelf arrow animation,
// as well as sizing some other widgets in Windows. In GTK the size is
// managed for us, so it appears to be only used for the download shelf
// animation.
int x = 0;
int y = 0;
if (fixed_->window)
gdk_window_get_origin(fixed_->window, &x, &y);
out->SetRect(x + fixed_->allocation.x, y + fixed_->allocation.y,
requested_size_.width(), requested_size_.height());
}
void TabContentsViewGtk::SetPageTitle(const std::wstring& title) {
// Set the window name to include the page title so it's easier to spot
// when debugging (e.g. via xwininfo -tree).
gfx::NativeView content_view = GetContentNativeView();
if (content_view && content_view->window)
gdk_window_set_title(content_view->window, WideToUTF8(title).c_str());
}
void TabContentsViewGtk::OnTabCrashed() {
if (tab_contents() != NULL && !sad_tab_.get()) {
sad_tab_.reset(new SadTabGtk(tab_contents()));
InsertIntoContentArea(sad_tab_->widget());
gtk_widget_show(sad_tab_->widget());
}
}
void TabContentsViewGtk::SizeContents(const gfx::Size& size) {
// We don't need to manually set the size of of widgets in GTK+, but we do
// need to pass the sizing information on to the RWHV which will pass the
// sizing information on to the renderer.
requested_size_ = size;
if (tab_contents()->render_widget_host_view())
tab_contents()->render_widget_host_view()->SetSize(size);
}
void TabContentsViewGtk::Focus() {
if (tab_contents()->showing_interstitial_page()) {
tab_contents()->interstitial_page()->Focus();
} else {
GtkWidget* widget = GetContentNativeView();
if (widget)
gtk_widget_grab_focus(widget);
}
}
void TabContentsViewGtk::SetInitialFocus() {
if (tab_contents()->FocusLocationBarByDefault())
tab_contents()->delegate()->SetFocusToLocationBar();
else
Focus();
}
void TabContentsViewGtk::StoreFocus() {
focus_store_.Store(GetNativeView());
}
void TabContentsViewGtk::RestoreFocus() {
if (focus_store_.widget())
gtk_widget_grab_focus(focus_store_.widget());
else
SetInitialFocus();
}
void TabContentsViewGtk::UpdateDragCursor(WebDragOperation operation) {
drag_dest_->UpdateDragStatus(operation);
}
void TabContentsViewGtk::GotFocus() {
// This is only used in the views FocusManager stuff but it bleeds through
// all subclasses. http://crbug.com/21875
}
// This is called when we the renderer asks us to take focus back (i.e., it has
// iterated past the last focusable element on the page).
void TabContentsViewGtk::TakeFocus(bool reverse) {
if (!tab_contents()->delegate()->TakeFocus(reverse)) {
gtk_widget_child_focus(GTK_WIDGET(GetTopLevelNativeWindow()),
reverse ? GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
}
}
bool TabContentsViewGtk::HandleKeyboardEvent(
const NativeWebKeyboardEvent& event) {
// This may be an accelerator. Try to pass it on to our browser window
// to handle.
GtkWindow* window = GetTopLevelNativeWindow();
if (!window) {
NOTREACHED();
return false;
}
// Filter out pseudo key events created by GtkIMContext signal handlers.
// Since GtkIMContext signal handlers don't use GdkEventKey objects, its
// |event.os_event| values are dummy values (or NULL.)
// We should filter out these pseudo key events to prevent unexpected
// behaviors caused by them.
// We should also filter out the KeyUp event as it should not be processed
// as an accelerator.
if (event.type == WebKit::WebInputEvent::Char ||
event.type == WebKit::WebInputEvent::KeyUp)
return false;
BrowserWindowGtk* browser_window =
BrowserWindowGtk::GetBrowserWindowForNativeWindow(window);
// If this is a dialog, the top level window isn't a browser. In this case,
// we just return false.
return browser_window ?
browser_window->HandleKeyboardEvent(event.os_event) : false;
}
void TabContentsViewGtk::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::TAB_CONTENTS_CONNECTED: {
// No need to remove the SadTabGtk's widget from the container since
// the new RenderWidgetHostViewGtk instance already removed all the
// vbox's children.
sad_tab_.reset();
break;
}
default:
NOTREACHED() << "Got a notification we didn't register for.";
break;
}
}
void TabContentsViewGtk::ShowContextMenu(const ContextMenuParams& params) {
context_menu_.reset(new RenderViewContextMenuGtk(tab_contents(), params,
last_mouse_down_.time));
context_menu_->Init();
gfx::Rect bounds;
GetContainerBounds(&bounds);
gfx::Point point = bounds.origin();
point.Offset(params.x, params.y);
context_menu_->Popup(point);
}
// Render view DnD -------------------------------------------------------------
void TabContentsViewGtk::StartDragging(const WebDropData& drop_data,
WebDragOperationsMask ops) {
DCHECK(GetContentNativeView());
drag_source_->StartDragging(drop_data, &last_mouse_down_);
// TODO(snej): Make use of the WebDragOperationsMask somehow
}
// -----------------------------------------------------------------------------
void TabContentsViewGtk::InsertIntoContentArea(GtkWidget* widget) {
gtk_fixed_put(GTK_FIXED(fixed_), widget, 0, 0);
}
gboolean TabContentsViewGtk::OnMouseDown(GtkWidget* widget,
GdkEventButton* event, TabContentsViewGtk* view) {
view->last_mouse_down_ = *event;
return FALSE;
}
gboolean TabContentsViewGtk::OnSizeAllocate(GtkWidget* widget,
GtkAllocation* allocation,
TabContentsViewGtk* view) {
int width = allocation->width;
int height = allocation->height;
// |delegate()| can be NULL here during browser teardown.
if (view->tab_contents()->delegate())
height += view->tab_contents()->delegate()->GetExtraRenderViewHeight();
gfx::Size size(width, height);
view->requested_size_ = size;
gtk_container_foreach(GTK_CONTAINER(widget), SetSizeRequest, &size);
// We manually tell our RWHV to resize the renderer content. This avoids
// spurious resizes from GTK+.
if (view->tab_contents()->render_widget_host_view())
view->tab_contents()->render_widget_host_view()->SetSize(size);
if (view->tab_contents()->interstitial_page())
view->tab_contents()->interstitial_page()->SetSize(size);
return FALSE;
}
// static
void TabContentsViewGtk::OnSetFloatingPosition(
GtkFloatingContainer* floating_container, GtkAllocation* allocation,
TabContentsViewGtk* tab_contents_view) {
if (tab_contents_view->popup_view_) {
GtkWidget* widget = tab_contents_view->popup_view_->widget();
// Look at the size request of the status bubble and tell the
// GtkFloatingContainer where we want it positioned.
GtkRequisition requisition;
gtk_widget_size_request(widget, &requisition);
GValue value = { 0, };
g_value_init(&value, G_TYPE_INT);
int child_x = std::max(
allocation->x + allocation->width - requisition.width -
kScrollbarWidthHack, 0);
g_value_set_int(&value, child_x);
gtk_container_child_set_property(GTK_CONTAINER(floating_container),
widget, "x", &value);
int child_y = std::max(
allocation->y + allocation->height - requisition.height, 0);
g_value_set_int(&value, child_y);
gtk_container_child_set_property(GTK_CONTAINER(floating_container),
widget, "y", &value);
g_value_unset(&value);
}
// Place each ConstrainedWindow in the center of the view.
int half_view_width = std::max((allocation->x + allocation->width) / 2, 0);
int half_view_height = std::max((allocation->y + allocation->height) / 2, 0);
std::vector<ConstrainedWindowGtk*>::iterator it =
tab_contents_view->constrained_windows_.begin();
std::vector<ConstrainedWindowGtk*>::iterator end =
tab_contents_view->constrained_windows_.end();
for (; it != end; ++it) {
GtkWidget* widget = (*it)->widget();
DCHECK(widget->parent == tab_contents_view->floating_.get());
GtkRequisition requisition;
gtk_widget_size_request(widget, &requisition);
GValue value = { 0, };
g_value_init(&value, G_TYPE_INT);
int child_x = std::max(half_view_width - (requisition.width / 2), 0);
g_value_set_int(&value, child_x);
gtk_container_child_set_property(GTK_CONTAINER(floating_container),
widget, "x", &value);
int child_y = std::max(half_view_height - (requisition.height / 2), 0);
g_value_set_int(&value, child_y);
gtk_container_child_set_property(GTK_CONTAINER(floating_container),
widget, "y", &value);
g_value_unset(&value);
}
}
|