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
|
// Copyright (c) 2011 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/chromeos/views/webui_menu_widget.h"
#include <algorithm>
#include "base/stringprintf.h"
#include "base/singleton.h"
#include "base/task.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/views/menu_locator.h"
#include "chrome/browser/chromeos/views/native_menu_webui.h"
#include "chrome/browser/chromeos/wm_ipc.h"
#include "chrome/browser/ui/views/dom_view.h"
#include "chrome/common/url_constants.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/render_widget_host_view.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "googleurl/src/gurl.h"
#include "third_party/cros/chromeos_wm_ipc_enums.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/gfx/canvas_skia.h"
#include "views/border.h"
#include "views/layout/layout_manager.h"
#include "views/widget/root_view.h"
namespace {
// Colors for the menu's gradient background.
const SkColor kMenuStartColor = SK_ColorWHITE;
const SkColor kMenuEndColor = 0xFFEEEEEE;
// Rounded border for menu. This draws three types of rounded border,
// for context menu, dropdown menu and submenu. Please see
// menu_locator.cc for details.
class RoundedBorder : public views::Border {
public:
explicit RoundedBorder(chromeos::MenuLocator* locator)
: menu_locator_(locator) {
}
private:
// views::Border implementations.
virtual void Paint(const views::View& view, gfx::Canvas* canvas) const {
const SkScalar* corners = menu_locator_->GetCorners();
// The menu is in off screen so no need to draw corners.
if (!corners)
return;
int w = view.width();
int h = view.height();
SkRect rect = {0, 0, w, h};
SkPath path;
path.addRoundRect(rect, corners);
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setFlags(SkPaint::kAntiAlias_Flag);
SkPoint p[2] = { {0, 0}, {0, h} };
SkColor colors[2] = {kMenuStartColor, kMenuEndColor};
SkShader* s = SkGradientShader::CreateLinear(
p, colors, NULL, 2, SkShader::kClamp_TileMode, NULL);
paint.setShader(s);
// Need to unref shader, otherwise never deleted.
s->unref();
canvas->AsCanvasSkia()->drawPath(path, paint);
}
virtual void GetInsets(gfx::Insets* insets) const {
DCHECK(insets);
menu_locator_->GetInsets(insets);
}
chromeos::MenuLocator* menu_locator_; // not owned
DISALLOW_COPY_AND_ASSIGN(RoundedBorder);
};
class InsetsLayout : public views::LayoutManager {
public:
InsetsLayout() : views::LayoutManager() {}
private:
// views::LayoutManager implementations.
virtual void Layout(views::View* host) {
if (!host->has_children())
return;
gfx::Insets insets = host->GetInsets();
views::View* view = host->GetChildViewAt(0);
view->SetBounds(insets.left(), insets.top(),
host->width() - insets.width(),
host->height() - insets.height());
}
virtual gfx::Size GetPreferredSize(views::View* host) {
DCHECK(host->child_count() == 1);
gfx::Insets insets = host->GetInsets();
gfx::Size size = host->GetChildViewAt(0)->GetPreferredSize();
return gfx::Size(size.width() + insets.width(),
size.height() + insets.height());
}
DISALLOW_COPY_AND_ASSIGN(InsetsLayout);
};
// A gtk widget key used to test if a given WidgetGtk instance is
// a WebUIMenuWidgetKey.
const char* kWebUIMenuWidgetKey = "__WEBUI_MENU_WIDGET__";
} // namespace
namespace chromeos {
// static
WebUIMenuWidget* WebUIMenuWidget::FindWebUIMenuWidget(gfx::NativeView native) {
DCHECK(native);
native = gtk_widget_get_toplevel(native);
if (!native)
return NULL;
return static_cast<chromeos::WebUIMenuWidget*>(
g_object_get_data(G_OBJECT(native), kWebUIMenuWidgetKey));
}
///////////////////////////////////////////////////////////////////////////////
// WebUIMenuWidget public:
WebUIMenuWidget::WebUIMenuWidget(chromeos::NativeMenuWebUI* webui_menu,
bool root)
: views::WidgetGtk(views::WidgetGtk::TYPE_POPUP),
webui_menu_(webui_menu),
dom_view_(NULL),
did_input_grab_(false),
is_root_(root) {
DCHECK(webui_menu_);
// TODO(oshima): Disabling transparent until we migrate bookmark
// menus to WebUI. See crosbug.com/7718.
// MakeTransparent();
}
WebUIMenuWidget::~WebUIMenuWidget() {
}
void WebUIMenuWidget::Init(gfx::NativeView parent, const gfx::Rect& bounds) {
WidgetGtk::Init(parent, bounds);
gtk_window_set_destroy_with_parent(GTK_WINDOW(GetNativeView()), TRUE);
gtk_window_set_type_hint(GTK_WINDOW(GetNativeView()),
GDK_WINDOW_TYPE_HINT_MENU);
g_object_set_data(G_OBJECT(GetNativeView()), kWebUIMenuWidgetKey, this);
}
void WebUIMenuWidget::Hide() {
ReleaseGrab();
WidgetGtk::Hide();
// Clears the content.
ExecuteJavascript(L"updateModel({'items':[]})");
}
void WebUIMenuWidget::Close() {
if (dom_view_ != NULL) {
dom_view_->parent()->RemoveChildView(dom_view_);
delete dom_view_;
dom_view_ = NULL;
}
// Detach the webui_menu_ which is being deleted.
webui_menu_ = NULL;
views::WidgetGtk::Close();
}
void WebUIMenuWidget::ReleaseGrab() {
WidgetGtk::ReleaseGrab();
if (did_input_grab_) {
did_input_grab_ = false;
gdk_pointer_ungrab(GDK_CURRENT_TIME);
gdk_keyboard_ungrab(GDK_CURRENT_TIME);
ClearGrabWidget();
}
}
gboolean WebUIMenuWidget::OnGrabBrokeEvent(GtkWidget* widget,
GdkEvent* event) {
did_input_grab_ = false;
Hide();
return WidgetGtk::OnGrabBrokeEvent(widget, event);
}
void WebUIMenuWidget::OnSizeAllocate(GtkWidget* widget,
GtkAllocation* allocation) {
views::WidgetGtk::OnSizeAllocate(widget, allocation);
// Adjust location when menu gets resized.
gfx::Rect bounds = GetClientAreaScreenBounds();
// Don't move until the menu gets contents.
if (bounds.height() > 1) {
menu_locator_->Move(this);
webui_menu_->InputIsReady();
}
}
gboolean MapToFocus(GtkWidget* widget, GdkEvent* event, gpointer data) {
WebUIMenuWidget* menu_widget = WebUIMenuWidget::FindWebUIMenuWidget(widget);
if (menu_widget) {
// See EnableInput for the meaning of data.
bool select_item = data != NULL;
menu_widget->EnableInput(select_item);
}
return true;
}
void WebUIMenuWidget::EnableScroll(bool enable) {
ExecuteJavascript(StringPrintf(
L"enableScroll(%ls)", enable ? L"true" : L"false"));
}
void WebUIMenuWidget::EnableInput(bool select_item) {
if (!dom_view_)
return;
DCHECK(dom_view_->tab_contents()->render_view_host());
DCHECK(dom_view_->tab_contents()->render_view_host()->view());
GtkWidget* target =
dom_view_->tab_contents()->render_view_host()->view()->GetNativeView();
DCHECK(target);
// Skip if the widget already own the input.
if (gtk_grab_get_current() == target)
return;
ClearGrabWidget();
if (!GTK_WIDGET_REALIZED(target)) {
// Wait grabbing widget if the widget is not yet realized.
// Using data as a flag. |select_item| is false if data is NULL,
// or true otherwise.
g_signal_connect(G_OBJECT(target), "map-event",
G_CALLBACK(&MapToFocus),
select_item ? this : NULL);
return;
}
gtk_grab_add(target);
dom_view_->tab_contents()->Focus();
if (select_item) {
ExecuteJavascript(L"selectItem()");
}
}
void WebUIMenuWidget::ExecuteJavascript(const std::wstring& script) {
// Don't execute if there is no DOMView associated. This is fine because
// 1) selectItem make sense only when DOMView is associated.
// 2) updateModel will be called again when a DOMView is created/assigned.
if (!dom_view_)
return;
DCHECK(dom_view_->tab_contents()->render_view_host());
dom_view_->tab_contents()->render_view_host()->
ExecuteJavascriptInWebFrame(string16(), WideToUTF16Hack(script));
}
void WebUIMenuWidget::ShowAt(chromeos::MenuLocator* locator) {
DCHECK(webui_menu_);
menu_locator_.reset(locator);
if (!dom_view_) {
// TODO(oshima): Replace DOMView with direct use of RVH for beta.
// DOMView should be refactored to use RVH directly, but
// it'll require a lot of change and will take time.
dom_view_ = new DOMView();
dom_view_->Init(webui_menu_->GetProfile(), NULL);
// TODO(oshima): remove extra view to draw rounded corner.
views::View* container = new views::View();
container->AddChildView(dom_view_);
container->set_border(new RoundedBorder(locator));
container->SetLayoutManager(new InsetsLayout());
SetContentsView(container);
dom_view_->LoadURL(webui_menu_->menu_url());
} else {
webui_menu_->UpdateStates();
dom_view_->parent()->set_border(new RoundedBorder(locator));
menu_locator_->Move(this);
}
Show();
// The pointer grab is captured only on the top level menu,
// all mouse event events are delivered to submenu using gtk_add_grab.
if (is_root_) {
CaptureGrab();
}
}
void WebUIMenuWidget::SetSize(const gfx::Size& new_size) {
DCHECK(webui_menu_);
// Ignore the empty new_size request which is called when
// menu.html is loaded.
if (new_size.IsEmpty())
return;
int width, height;
gtk_widget_get_size_request(GetNativeView(), &width, &height);
gfx::Size real_size(std::max(new_size.width(), width),
new_size.height());
// Ignore the size request with the same size.
gfx::Rect bounds = GetClientAreaScreenBounds();
if (bounds.size() == real_size)
return;
menu_locator_->SetBounds(this, real_size);
}
///////////////////////////////////////////////////////////////////////////////
// WebUIMenuWidget private:
void WebUIMenuWidget::CaptureGrab() {
// Release the current grab.
ClearGrabWidget();
// NOTE: we do this to ensure we get mouse/keyboard events from
// other apps, a grab done with gtk_grab_add doesn't get events from
// other apps.
GdkGrabStatus pointer_grab_status =
gdk_pointer_grab(window_contents()->window, FALSE,
static_cast<GdkEventMask>(
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK),
NULL, NULL, GDK_CURRENT_TIME);
GdkGrabStatus keyboard_grab_status =
gdk_keyboard_grab(window_contents()->window, FALSE,
GDK_CURRENT_TIME);
did_input_grab_ = pointer_grab_status == GDK_GRAB_SUCCESS &&
keyboard_grab_status == GDK_GRAB_SUCCESS;
DCHECK(did_input_grab_);
EnableInput(false /* no selection */);
}
void WebUIMenuWidget::ClearGrabWidget() {
GtkWidget* grab_widget;
while ((grab_widget = gtk_grab_get_current()))
gtk_grab_remove(grab_widget);
}
} // namespace chromeos
|