Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/view.h" | 5 #include "ui/views/view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "third_party/skia/include/core/SkRect.h" | 15 #include "third_party/skia/include/core/SkRect.h" |
| 16 #include "ui/base/accessibility/accessibility_types.h" | 16 #include "ui/base/accessibility/accessibility_types.h" |
| 17 #include "ui/base/dragdrop/drag_drop_types.h" | 17 #include "ui/base/dragdrop/drag_drop_types.h" |
| 18 #include "ui/compositor/compositor.h" | 18 #include "ui/compositor/compositor.h" |
| 19 #include "ui/compositor/layer.h" | 19 #include "ui/compositor/layer.h" |
| 20 #include "ui/compositor/layer_animator.h" | 20 #include "ui/compositor/layer_animator.h" |
| 21 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 22 #include "ui/gfx/interpolated_transform.h" | 22 #include "ui/gfx/interpolated_transform.h" |
| 23 #include "ui/gfx/path.h" | 23 #include "ui/gfx/path.h" |
| 24 #include "ui/gfx/point3.h" | 24 #include "ui/gfx/point3.h" |
| 25 #include "ui/gfx/transform.h" | 25 #include "ui/gfx/transform.h" |
| 26 #include "ui/views/background.h" | 26 #include "ui/views/background.h" |
| 27 #include "ui/views/context_menu_controller.h" | 27 #include "ui/views/context_menu_controller.h" |
| 28 #include "ui/views/drag_controller.h" | 28 #include "ui/views/drag_controller.h" |
| 29 #include "ui/views/layout/layout_manager.h" | 29 #include "ui/views/layout/layout_manager.h" |
| 30 #include "ui/views/view_constants.h" | |
| 30 #include "ui/views/views_delegate.h" | 31 #include "ui/views/views_delegate.h" |
| 31 #include "ui/views/widget/native_widget_private.h" | 32 #include "ui/views/widget/native_widget_private.h" |
| 32 #include "ui/views/widget/root_view.h" | 33 #include "ui/views/widget/root_view.h" |
| 33 #include "ui/views/widget/tooltip_manager.h" | 34 #include "ui/views/widget/tooltip_manager.h" |
| 34 #include "ui/views/widget/widget.h" | 35 #include "ui/views/widget/widget.h" |
| 35 | 36 |
| 36 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 37 #include "base/win/scoped_gdi_object.h" | 38 #include "base/win/scoped_gdi_object.h" |
| 38 #include "ui/views/accessibility/native_view_accessibility_win.h" | 39 #include "ui/views/accessibility/native_view_accessibility_win.h" |
| 39 #endif | 40 #endif |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 // Returns the top view in |view|'s hierarchy. | 76 // Returns the top view in |view|'s hierarchy. |
| 76 const views::View* GetHierarchyRoot(const views::View* view) { | 77 const views::View* GetHierarchyRoot(const views::View* view) { |
| 77 const views::View* root = view; | 78 const views::View* root = view; |
| 78 while (root && root->parent()) | 79 while (root && root->parent()) |
| 79 root = root->parent(); | 80 root = root->parent(); |
| 80 return root; | 81 return root; |
| 81 } | 82 } |
| 82 | 83 |
| 84 // Returns the percentage of |rect_1|'s area that is covered by |rect_2|. | |
| 85 float PercentCoveredBy(const gfx::Rect& rect_1, const gfx::Rect& rect_2) { | |
| 86 gfx::Rect intersection(rect_1.Intersect(rect_2)); | |
| 87 float intersection_area = intersection.size().GetArea(); | |
| 88 float rect_1_area = rect_1.size().GetArea(); | |
| 89 return rect_1_area ? intersection_area / rect_1_area : 0; | |
| 90 } | |
| 91 | |
| 92 // The positive distance from |pos| to the nearest endpoint of the interval | |
| 93 // [start, end] is returned if |pos| lies within the interval, otherwise | |
| 94 // 0 is returned. | |
| 95 int DistanceToInterval(int pos, int start, int end) { | |
| 96 if (pos < start) | |
| 97 return start - pos; | |
| 98 if (pos > end) | |
| 99 return pos - end; | |
| 100 return 0; | |
| 101 } | |
| 102 | |
| 103 // Returns the square of the distance from |point| to the center line of | |
| 104 // |target_rect|. The center line of a rectangle is obtained by repeatedly | |
| 105 // stripping away 1px borders around the rectangle until a line remains. | |
| 106 int DistanceSquaredFromCenterLineToPoint(const gfx::Point& point, | |
| 107 const gfx::Rect& target_rect) { | |
| 108 gfx::Point center_point = target_rect.CenterPoint(); | |
| 109 int dx = center_point.x() - point.x(); | |
| 110 int dy = center_point.y() - point.y(); | |
| 111 | |
| 112 if (target_rect.width() > target_rect.height()) { | |
| 113 dx = DistanceToInterval(point.x(), | |
| 114 target_rect.x() + (target_rect.height() / 2), | |
| 115 target_rect.right() - (target_rect.height() / 2)); | |
| 116 } else { | |
| 117 dy = DistanceToInterval(point.y(), | |
| 118 target_rect.y() + (target_rect.width() / 2), | |
| 119 target_rect.bottom() - (target_rect.width() / 2)); | |
| 120 } | |
| 121 return (dx * dx) + (dy * dy); | |
| 122 } | |
| 123 | |
| 83 } // namespace | 124 } // namespace |
| 84 | 125 |
| 85 namespace views { | 126 namespace views { |
| 86 | 127 |
| 87 // static | 128 // static |
| 88 ViewsDelegate* ViewsDelegate::views_delegate = NULL; | 129 ViewsDelegate* ViewsDelegate::views_delegate = NULL; |
| 89 | 130 |
| 90 // static | 131 // static |
| 91 const char View::kViewClassName[] = "views/View"; | 132 const char View::kViewClassName[] = "views/View"; |
| 92 | 133 |
| (...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 619 target->ConvertPointFromAncestor(root, point); | 660 target->ConvertPointFromAncestor(root, point); |
| 620 | 661 |
| 621 // API defines NULL |source| as returning the point in screen coordinates. | 662 // API defines NULL |source| as returning the point in screen coordinates. |
| 622 if (!source) { | 663 if (!source) { |
| 623 *point = point->Subtract( | 664 *point = point->Subtract( |
| 624 root->GetWidget()->GetClientAreaBoundsInScreen().origin()); | 665 root->GetWidget()->GetClientAreaBoundsInScreen().origin()); |
| 625 } | 666 } |
| 626 } | 667 } |
| 627 | 668 |
| 628 // static | 669 // static |
| 670 void View::ConvertRectToView(const View* source, | |
| 671 const View* target, | |
| 672 gfx::Rect* rect) { | |
| 673 gfx::Point rect_location(rect->x(), rect->y()); | |
| 674 View::ConvertPointToView(source, target, &rect_location); | |
| 675 rect->set_origin(rect_location); | |
| 676 } | |
| 677 | |
| 678 // static | |
| 629 void View::ConvertPointToWidget(const View* src, gfx::Point* p) { | 679 void View::ConvertPointToWidget(const View* src, gfx::Point* p) { |
| 630 DCHECK(src); | 680 DCHECK(src); |
| 631 DCHECK(p); | 681 DCHECK(p); |
| 632 | 682 |
| 633 src->ConvertPointForAncestor(NULL, p); | 683 src->ConvertPointForAncestor(NULL, p); |
| 634 } | 684 } |
| 635 | 685 |
| 636 // static | 686 // static |
| 637 void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) { | 687 void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) { |
| 638 DCHECK(dest); | 688 DCHECK(dest); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 756 continue; | 806 continue; |
| 757 | 807 |
| 758 gfx::Point point_in_child_coords(point); | 808 gfx::Point point_in_child_coords(point); |
| 759 View::ConvertPointToView(this, child, &point_in_child_coords); | 809 View::ConvertPointToView(this, child, &point_in_child_coords); |
| 760 if (child->HitTest(point_in_child_coords)) | 810 if (child->HitTest(point_in_child_coords)) |
| 761 return child->GetEventHandlerForPoint(point_in_child_coords); | 811 return child->GetEventHandlerForPoint(point_in_child_coords); |
| 762 } | 812 } |
| 763 return this; | 813 return this; |
| 764 } | 814 } |
| 765 | 815 |
| 816 View* View::GetEventHandlerForRect(const gfx::Rect& rect) { | |
|
sky
2012/08/03 23:46:36
I'm hoping its possible to combine this and GetEve
sky
2012/08/03 23:52:57
Gah, I guess I can't nuke my comments.
Here's my s
| |
| 817 View* closest_view = NULL; | |
| 818 int best_dist_so_far = INT_MAX; | |
| 819 for (int i = child_count() - 1; i >= 0; --i) { | |
| 820 View* child = child_at(i); | |
| 821 if (!child->visible()) | |
| 822 continue; | |
| 823 | |
| 824 gfx::Rect child_rect(child->bounds()); | |
| 825 if (!child_rect.Intersects(rect)) | |
| 826 continue; | |
| 827 | |
| 828 gfx::Rect rect_in_child_coords(rect); | |
| 829 View::ConvertRectToView(this, child, &rect_in_child_coords); | |
| 830 View* cur_view = child->GetEventHandlerForRect(rect_in_child_coords); | |
| 831 if (!cur_view) | |
|
sky
2012/08/03 23:46:36
between 830-831 something like:
if (rect.IsEmpty(
sky
2012/08/03 23:52:57
if (type == MOUSE)
return cur_view;
| |
| 832 continue; | |
| 833 | |
| 834 gfx::Point touch_center(rect.CenterPoint()); | |
| 835 gfx::Rect cur_rect(cur_view->GetLocalBounds()); | |
| 836 View::ConvertRectToView(cur_view, this, &cur_rect); | |
| 837 int cur_dist = DistanceSquaredFromCenterLineToPoint(touch_center, | |
|
sky
2012/08/03 23:46:36
If GetEventHandlerForRect returns a view, don't we
| |
| 838 cur_rect); | |
| 839 if (!closest_view || cur_dist < best_dist_so_far) { | |
| 840 closest_view = cur_view; | |
|
sky
2012/08/03 23:46:36
result->best_view = closest_view;
| |
| 841 best_dist_so_far = cur_dist; | |
| 842 } | |
| 843 } | |
| 844 | |
|
sky
2012/08/03 23:46:36
if (!result->view_at_center && bounds().contains(c
| |
| 845 if (closest_view) | |
| 846 return closest_view; | |
|
sky
2012/08/03 23:46:36
This would become return true.
| |
| 847 | |
| 848 gfx::Rect view_rect(GetLocalBounds()); | |
| 849 if (PercentCoveredBy(view_rect, rect) >= kFuzzingOverlapPercentage) | |
| 850 return this; | |
|
sky
2012/08/03 23:46:36
result->best_view = this; return true;
| |
| 851 return NULL; | |
|
sky
2012/08/03 23:46:36
return false;
sky
2012/08/03 23:52:57
return type == MOUSE ? this : NULL;
| |
| 852 } | |
| 853 | |
| 766 gfx::NativeCursor View::GetCursor(const MouseEvent& event) { | 854 gfx::NativeCursor View::GetCursor(const MouseEvent& event) { |
| 767 #if defined(OS_WIN) && !defined(USE_AURA) | 855 #if defined(OS_WIN) && !defined(USE_AURA) |
| 768 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); | 856 static HCURSOR arrow = LoadCursor(NULL, IDC_ARROW); |
| 769 return arrow; | 857 return arrow; |
| 770 #else | 858 #else |
| 771 return gfx::kNullCursor; | 859 return gfx::kNullCursor; |
| 772 #endif | 860 #endif |
| 773 } | 861 } |
| 774 | 862 |
| 775 bool View::HitTest(const gfx::Point& l) const { | 863 bool View::HitTest(const gfx::Point& l) const { |
| (...skipping 1330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2106 gfx::Point widget_location(event.location()); | 2194 gfx::Point widget_location(event.location()); |
| 2107 ConvertPointToWidget(this, &widget_location); | 2195 ConvertPointToWidget(this, &widget_location); |
| 2108 GetWidget()->RunShellDrag(this, data, widget_location, drag_operations); | 2196 GetWidget()->RunShellDrag(this, data, widget_location, drag_operations); |
| 2109 return true; | 2197 return true; |
| 2110 #else | 2198 #else |
| 2111 return false; | 2199 return false; |
| 2112 #endif // !defined(OS_MACOSX) | 2200 #endif // !defined(OS_MACOSX) |
| 2113 } | 2201 } |
| 2114 | 2202 |
| 2115 } // namespace views | 2203 } // namespace views |
| OLD | NEW |