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) 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 "views/widget/root_view.h"
#include <algorithm>
#include "base/logging.h"
#include "base/message_loop.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas_skia.h"
#include "views/focus/view_storage.h"
#include "views/layout/fill_layout.h"
#include "views/widget/widget.h"
#include "views/window/window.h"
#if defined(TOUCH_UI)
#include "views/touchui/gesture_manager.h"
#endif
namespace views {
// static
const char RootView::kViewClassName[] = "views/RootView";
////////////////////////////////////////////////////////////////////////////////
// RootView, public:
// Creation and lifetime -------------------------------------------------------
RootView::RootView(Widget* widget)
: widget_(widget),
mouse_pressed_handler_(NULL),
mouse_move_handler_(NULL),
last_click_handler_(NULL),
explicit_mouse_handler_(false),
last_mouse_event_flags_(0),
last_mouse_event_x_(-1),
last_mouse_event_y_(-1),
#if defined(TOUCH_UI)
gesture_manager_(GestureManager::GetInstance()),
touch_pressed_handler_(NULL),
#endif
ALLOW_THIS_IN_INITIALIZER_LIST(focus_search_(this, false, false)),
focus_traversable_parent_(NULL),
focus_traversable_parent_view_(NULL) {
}
RootView::~RootView() {
// If we have children remove them explicitly so to make sure a remove
// notification is sent for each one of them.
if (has_children())
RemoveAllChildViews(true);
}
// Tree operations -------------------------------------------------------------
void RootView::SetContentsView(View* contents_view) {
DCHECK(contents_view && GetWidget()->GetNativeView()) <<
"Can't be called until after the native view is created!";
// The ContentsView must be set up _after_ the window is created so that its
// Widget pointer is valid.
SetLayoutManager(new FillLayout);
if (has_children())
RemoveAllChildViews(true);
AddChildView(contents_view);
// Force a layout now, since the attached hierarchy won't be ready for the
// containing window's bounds. Note that we call Layout directly rather than
// calling the widget's size changed handler, since the RootView's bounds may
// not have changed, which will cause the Layout not to be done otherwise.
Layout();
}
void RootView::NotifyNativeViewHierarchyChanged(bool attached,
gfx::NativeView native_view) {
PropagateNativeViewHierarchyChanged(attached, native_view, this);
}
// Input -----------------------------------------------------------------------
bool RootView::ProcessKeyEvent(const KeyEvent& event) {
bool consumed = false;
View* v = GetFocusManager()->GetFocusedView();
// Special case to handle right-click context menus triggered by the
// keyboard.
if (v && v->IsEnabled() && ((event.key_code() == ui::VKEY_APPS) ||
(event.key_code() == ui::VKEY_F10 && event.IsShiftDown()))) {
v->ShowContextMenu(v->GetKeyboardContextMenuLocation(), false);
return true;
}
for (; v && v != this && !consumed; v = v->parent()) {
consumed = (event.type() == ui::ET_KEY_PRESSED) ?
v->OnKeyPressed(event) : v->OnKeyReleased(event);
}
return consumed;
}
// Focus -----------------------------------------------------------------------
void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) {
DCHECK(focus_traversable != this);
focus_traversable_parent_ = focus_traversable;
}
void RootView::SetFocusTraversableParentView(View* view) {
focus_traversable_parent_view_ = view;
}
// System events ---------------------------------------------------------------
void RootView::ThemeChanged() {
View::PropagateThemeChanged();
}
void RootView::LocaleChanged() {
View::PropagateLocaleChanged();
}
////////////////////////////////////////////////////////////////////////////////
// RootView, FocusTraversable implementation:
FocusSearch* RootView::GetFocusSearch() {
return &focus_search_;
}
FocusTraversable* RootView::GetFocusTraversableParent() {
return focus_traversable_parent_;
}
View* RootView::GetFocusTraversableParentView() {
return focus_traversable_parent_view_;
}
////////////////////////////////////////////////////////////////////////////////
// RootView, View overrides:
const Widget* RootView::GetWidget() const {
return widget_;
}
Widget* RootView::GetWidget() {
return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget());
}
bool RootView::IsVisibleInRootView() const {
return IsVisible();
}
std::string RootView::GetClassName() const {
return kViewClassName;
}
void RootView::SchedulePaintInRect(const gfx::Rect& rect) {
gfx::Rect xrect = ConvertRectToParent(rect);
gfx::Rect invalid_rect = GetLocalBounds().Intersect(xrect);
if (!invalid_rect.IsEmpty())
widget_->SchedulePaintInRect(invalid_rect);
}
bool RootView::OnMousePressed(const MouseEvent& event) {
MouseEvent e(event, this);
UpdateCursor(e);
SetMouseLocationAndFlags(e);
// If mouse_pressed_handler_ is non null, we are currently processing
// a pressed -> drag -> released session. In that case we send the
// event to mouse_pressed_handler_
if (mouse_pressed_handler_) {
MouseEvent mouse_pressed_event(e, this, mouse_pressed_handler_);
drag_info.Reset();
mouse_pressed_handler_->ProcessMousePressed(mouse_pressed_event,
&drag_info);
return true;
}
DCHECK(!explicit_mouse_handler_);
bool hit_disabled_view = false;
// Walk up the tree until we find a view that wants the mouse event.
for (mouse_pressed_handler_ = GetEventHandlerForPoint(e.location());
mouse_pressed_handler_ && (mouse_pressed_handler_ != this);
mouse_pressed_handler_ = mouse_pressed_handler_->parent()) {
if (!mouse_pressed_handler_->IsEnabled()) {
// Disabled views should eat events instead of propagating them upwards.
hit_disabled_view = true;
break;
}
// See if this view wants to handle the mouse press.
MouseEvent mouse_pressed_event(e, this, mouse_pressed_handler_);
// Remove the double-click flag if the handler is different than the
// one which got the first click part of the double-click.
if (mouse_pressed_handler_ != last_click_handler_)
mouse_pressed_event.set_flags(e.flags() &
~ui::EF_IS_DOUBLE_CLICK);
drag_info.Reset();
bool handled = mouse_pressed_handler_->ProcessMousePressed(
mouse_pressed_event, &drag_info);
// The view could have removed itself from the tree when handling
// OnMousePressed(). In this case, the removal notification will have
// reset mouse_pressed_handler_ to NULL out from under us. Detect this
// case and stop. (See comments in view.h.)
//
// NOTE: Don't return true here, because we don't want the frame to
// forward future events to us when there's no handler.
if (!mouse_pressed_handler_)
break;
// If the view handled the event, leave mouse_pressed_handler_ set and
// return true, which will cause subsequent drag/release events to get
// forwarded to that view.
if (handled) {
last_click_handler_ = mouse_pressed_handler_;
return true;
}
}
// Reset mouse_pressed_handler_ to indicate that no processing is occurring.
mouse_pressed_handler_ = NULL;
// In the event that a double-click is not handled after traversing the
// entire hierarchy (even as a single-click when sent to a different view),
// it must be marked as handled to avoid anything happening from default
// processing if it the first click-part was handled by us.
if (last_click_handler_ && e.flags() & ui::EF_IS_DOUBLE_CLICK)
hit_disabled_view = true;
last_click_handler_ = NULL;
return hit_disabled_view;
}
bool RootView::OnMouseDragged(const MouseEvent& event) {
MouseEvent e(event, this);
UpdateCursor(e);
if (mouse_pressed_handler_) {
SetMouseLocationAndFlags(e);
gfx::Point p;
ConvertPointToMouseHandler(e.location(), &p);
MouseEvent mouse_event(e.type(), p.x(), p.y(), e.flags());
return mouse_pressed_handler_->ProcessMouseDragged(mouse_event, &drag_info);
}
return false;
}
void RootView::OnMouseReleased(const MouseEvent& event) {
MouseEvent e(event, this);
UpdateCursor(e);
if (mouse_pressed_handler_) {
gfx::Point p;
ConvertPointToMouseHandler(e.location(), &p);
MouseEvent mouse_released(e.type(), p.x(), p.y(), e.flags());
// We allow the view to delete us from ProcessMouseReleased. As such,
// configure state such that we're done first, then call View.
View* mouse_pressed_handler = mouse_pressed_handler_;
SetMouseHandler(NULL);
mouse_pressed_handler->ProcessMouseReleased(mouse_released);
// WARNING: we may have been deleted.
}
}
void RootView::OnMouseCaptureLost() {
if (mouse_pressed_handler_) {
// Synthesize a release event for UpdateCursor.
MouseEvent release_event(ui::ET_MOUSE_RELEASED, last_mouse_event_x_,
last_mouse_event_y_, last_mouse_event_flags_);
UpdateCursor(release_event);
// We allow the view to delete us from OnMouseCaptureLost. As such,
// configure state such that we're done first, then call View.
View* mouse_pressed_handler = mouse_pressed_handler_;
SetMouseHandler(NULL);
mouse_pressed_handler->OnMouseCaptureLost();
// WARNING: we may have been deleted.
}
}
void RootView::OnMouseMoved(const MouseEvent& event) {
MouseEvent e(event, this);
View* v = GetEventHandlerForPoint(e.location());
// Find the first enabled view, or the existing move handler, whichever comes
// first. The check for the existing handler is because if a view becomes
// disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED
// and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet.
while (v && !v->IsEnabled() && (v != mouse_move_handler_))
v = v->parent();
if (v && v != this) {
if (v != mouse_move_handler_) {
if (mouse_move_handler_ != NULL)
mouse_move_handler_->OnMouseExited(e);
mouse_move_handler_ = v;
MouseEvent entered_event(e, this, mouse_move_handler_);
mouse_move_handler_->OnMouseEntered(entered_event);
}
MouseEvent moved_event(e, this, mouse_move_handler_);
mouse_move_handler_->OnMouseMoved(moved_event);
gfx::NativeCursor cursor = mouse_move_handler_->GetCursorForPoint(
moved_event.type(), moved_event.location());
widget_->SetCursor(cursor);
} else if (mouse_move_handler_ != NULL) {
mouse_move_handler_->OnMouseExited(e);
widget_->SetCursor(NULL);
}
}
void RootView::OnMouseExited(const MouseEvent& event) {
if (mouse_move_handler_ != NULL) {
mouse_move_handler_->OnMouseExited(event);
mouse_move_handler_ = NULL;
}
}
bool RootView::OnMouseWheel(const MouseWheelEvent& event) {
MouseWheelEvent e(event, this);
bool consumed = false;
View* v = GetFocusManager()->GetFocusedView();
for (; v && v != this && !consumed; v = v->parent())
consumed = v->OnMouseWheel(e);
return consumed;
}
#if defined(TOUCH_UI)
View::TouchStatus RootView::OnTouchEvent(const TouchEvent& event) {
TouchEvent e(event, this);
// If touch_pressed_handler_ is non null, we are currently processing
// a touch down on the screen situation. In that case we send the
// event to touch_pressed_handler_
View::TouchStatus status = TOUCH_STATUS_UNKNOWN;
if (touch_pressed_handler_) {
TouchEvent touch_event(e, this, touch_pressed_handler_);
status = touch_pressed_handler_->ProcessTouchEvent(touch_event);
gesture_manager_->ProcessTouchEventForGesture(e, this, status);
if (status == TOUCH_STATUS_END)
touch_pressed_handler_ = NULL;
return status;
}
// Walk up the tree until we find a view that wants the touch event.
for (touch_pressed_handler_ = GetEventHandlerForPoint(e.location());
touch_pressed_handler_ && (touch_pressed_handler_ != this);
touch_pressed_handler_ = touch_pressed_handler_->parent()) {
if (!touch_pressed_handler_->IsEnabled()) {
// Disabled views eat events but are treated as not handled by the
// the GestureManager.
status = TOUCH_STATUS_UNKNOWN;
break;
}
// See if this view wants to handle the touch
TouchEvent touch_event(e, this, touch_pressed_handler_);
status = touch_pressed_handler_->ProcessTouchEvent(touch_event);
// The view could have removed itself from the tree when handling
// OnTouchEvent(). So handle as per OnMousePressed. NB: we
// assume that the RootView itself cannot be so removed.
if (!touch_pressed_handler_)
break;
// The touch event wasn't processed. Go up the view hierarchy and dispatch
// the touch event.
if (status == TOUCH_STATUS_UNKNOWN)
continue;
// If the touch didn't initiate a touch-sequence, then reset the touch event
// handler. Otherwise, leave it set so that subsequent touch events are
// dispatched to the same handler.
if (status != TOUCH_STATUS_START)
touch_pressed_handler_ = NULL;
gesture_manager_->ProcessTouchEventForGesture(e, this, status);
return status;
}
// Reset touch_pressed_handler_ to indicate that no processing is occurring.
touch_pressed_handler_ = NULL;
// Give the touch event to the gesture manager.
gesture_manager_->ProcessTouchEventForGesture(e, this, status);
return status;
}
#endif
void RootView::SetMouseHandler(View *new_mh) {
// If we're clearing the mouse handler, clear explicit_mouse_handler as well.
explicit_mouse_handler_ = (new_mh != NULL);
mouse_pressed_handler_ = new_mh;
}
void RootView::GetAccessibleState(ui::AccessibleViewState* state) {
state->role = ui::AccessibilityTypes::ROLE_APPLICATION;
}
#if defined(TOUCH_UI)
// Always show the mouse cursor, useful when debugging touch builds
static bool keep_mouse_cursor;
void RootView::SetKeepMouseCursor(bool keep) {
keep_mouse_cursor = keep;
}
bool RootView::GetKeepMouseCursor() {
return keep_mouse_cursor;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// RootView, protected:
void RootView::ViewHierarchyChanged(bool is_add, View* parent, View* child) {
widget_->ViewHierarchyChanged(is_add, parent, child);
if (!is_add) {
if (!explicit_mouse_handler_ && mouse_pressed_handler_ == child)
mouse_pressed_handler_ = NULL;
if (mouse_move_handler_ == child)
mouse_move_handler_ = NULL;
#if defined(TOUCH_UI)
if (touch_pressed_handler_)
touch_pressed_handler_ = NULL;
#endif
}
}
void RootView::OnPaint(gfx::Canvas* canvas) {
canvas->AsCanvasSkia()->drawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
}
////////////////////////////////////////////////////////////////////////////////
// RootView, private:
// Coordinate conversion -------------------------------------------------------
bool RootView::ConvertPointToMouseHandler(const gfx::Point& l, gfx::Point* p) {
// If the mouse_handler was set explicitly, keep sending events even if it was
// re-parented in a different window. (a non explicit mouse handler is
// automatically cleared when the control is removed from the hierarchy)
*p = l;
if (explicit_mouse_handler_) {
// If the mouse_pressed_handler_ is not connected, we send the
// event in screen coordinate system
ConvertPointToScreen(this, p);
if (mouse_pressed_handler_->GetWidget())
ConvertPointToView(NULL, mouse_pressed_handler_, p);
} else
ConvertPointToView(this, mouse_pressed_handler_, p);
return true;
}
// Input -----------------------------------------------------------------------
void RootView::UpdateCursor(const MouseEvent& event) {
gfx::NativeCursor cursor = NULL;
View* v = GetEventHandlerForPoint(event.location());
if (v && v != this) {
gfx::Point l(event.location());
View::ConvertPointToView(this, v, &l);
cursor = v->GetCursorForPoint(event.type(), l);
}
widget_->SetCursor(cursor);
}
void RootView::SetMouseLocationAndFlags(const MouseEvent& event) {
last_mouse_event_flags_ = event.flags();
last_mouse_event_x_ = event.x();
last_mouse_event_y_ = event.y();
}
} // namespace views
|