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 #ifndef UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ | 5 #ifndef UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ |
| 6 #define UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ | 6 #define UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "ui/views/background.h" | 10 #include "ui/views/background.h" |
| 11 #include "ui/views/border.h" | 11 #include "ui/views/border.h" |
| 12 | 12 |
| 13 namespace gfx { | 13 namespace gfx { |
| 14 class ImageSkia; | 14 class ImageSkia; |
| 15 } | 15 } |
| 16 | 16 |
| 17 namespace views { | 17 namespace views { |
| 18 | 18 |
| 19 // Renders a border, with optional arrow, and a custom dropshadow. | 19 // Renders a border, with optional arrow, and a custom dropshadow. |
| 20 // This can be used to produce floating "bubble" objects with rounded corners. | 20 // This can be used to produce floating "bubble" objects with rounded corners. |
| 21 class VIEWS_EXPORT BubbleBorder : public Border { | 21 class VIEWS_EXPORT BubbleBorder : public Border { |
| 22 public: | 22 public: |
| 23 // Possible locations for the (optional) arrow. | 23 // Possible locations for the (optional) arrow. |
| 24 // 0 bit specifies left or right. | 24 // 0 bit specifies left or right. |
| 25 // 1 bit specifies top or bottom. | 25 // 1 bit specifies top or bottom. |
| 26 // 2 bit specifies horizontal or vertical. | 26 // 2 bit specifies horizontal or vertical. |
| 27 // 3 bit specifies whether arrow at the center of its residing edge. | |
|
msw
2012/09/19 20:12:32
grammar nit: "*the* arrow *is*"
xiyuan
2012/09/19 22:51:36
Done.
| |
| 27 enum ArrowLocation { | 28 enum ArrowLocation { |
| 28 TOP_LEFT = 0, | 29 TOP_LEFT = 0, |
| 29 TOP_RIGHT = 1, | 30 TOP_RIGHT = 1, |
| 30 BOTTOM_LEFT = 2, | 31 BOTTOM_LEFT = 2, |
| 31 BOTTOM_RIGHT = 3, | 32 BOTTOM_RIGHT = 3, |
| 32 LEFT_TOP = 4, | 33 LEFT_TOP = 4, |
| 33 RIGHT_TOP = 5, | 34 RIGHT_TOP = 5, |
| 34 LEFT_BOTTOM = 6, | 35 LEFT_BOTTOM = 6, |
| 35 RIGHT_BOTTOM = 7, | 36 RIGHT_BOTTOM = 7, |
| 36 NONE = 8, // No arrow. Positioned under the supplied rect. | 37 TOP_CENTER = 8, |
| 37 FLOAT = 9 // No arrow. Centered over the supplied rect. | 38 BOTTOM_CENTER = 10, |
| 39 LEFT_CENTER = 12, | |
| 40 RIGHT_CENTER = 13, | |
| 41 NONE = 16, // No arrow. Positioned under the supplied rect. | |
| 42 FLOAT = 17, // No arrow. Centered over the supplied rect. | |
| 38 }; | 43 }; |
| 39 | 44 |
| 40 enum Shadow { | 45 enum Shadow { |
| 41 SHADOW = 0, | 46 SHADOW = 0, |
| 42 NO_SHADOW = 1 | 47 NO_SHADOW, |
| 48 #if defined(USE_AURA) | |
| 49 BIG_SHADOW, | |
| 50 SMALL_SHADOW, | |
| 51 #endif | |
| 52 SHADOW_COUNT, | |
| 43 }; | 53 }; |
| 44 | 54 |
| 45 // The position of the bubble in relation to the anchor. | 55 // The position of the bubble in relation to the anchor. |
| 46 enum BubbleAlignment { | 56 enum BubbleAlignment { |
| 47 // The tip of the arrow points to the middle of the anchor. | 57 // The tip of the arrow points to the middle of the anchor. |
| 48 ALIGN_ARROW_TO_MID_ANCHOR, | 58 ALIGN_ARROW_TO_MID_ANCHOR, |
| 49 // The edge nearest to the arrow is lined up with the edge of the anchor. | 59 // The edge nearest to the arrow is lined up with the edge of the anchor. |
| 50 ALIGN_EDGE_TO_ANCHOR_EDGE | 60 ALIGN_EDGE_TO_ANCHOR_EDGE |
| 51 }; | 61 }; |
| 52 | 62 |
| 53 BubbleBorder(ArrowLocation arrow_location, Shadow shadow); | 63 BubbleBorder(ArrowLocation arrow_location, Shadow shadow); |
| 54 | 64 |
| 55 // Returns the radius of the corner of the border. | 65 // Returns the radius of the corner of the border. |
| 66 // TODO(xiyuan): Get rid of this since it's part of BorderImages now? | |
| 56 static int GetCornerRadius() { | 67 static int GetCornerRadius() { |
| 57 // We can't safely calculate a border radius by comparing the sizes of the | 68 // We can't safely calculate a border radius by comparing the sizes of the |
| 58 // side and corner images, because either may have been extended in various | 69 // side and corner images, because either may have been extended in various |
| 59 // directions in order to do more subtle dropshadow fading or other effects. | 70 // directions in order to do more subtle dropshadow fading or other effects. |
| 60 // So we hardcode the most accurate value. | 71 // So we hardcode the most accurate value. |
| 61 return 4; | 72 return 4; |
| 62 } | 73 } |
| 63 | 74 |
| 64 // Sets the location for the arrow. | 75 // Sets the location for the arrow. |
| 65 void set_arrow_location(ArrowLocation loc) { arrow_location_ = loc; } | 76 void set_arrow_location(ArrowLocation loc) { arrow_location_ = loc; } |
| 66 ArrowLocation arrow_location() const { return arrow_location_; } | 77 ArrowLocation arrow_location() const { return arrow_location_; } |
| 67 | 78 |
| 68 // Sets the alignment. | 79 // Sets the alignment. |
| 69 void set_alignment(BubbleAlignment alignment) { alignment_ = alignment; } | 80 void set_alignment(BubbleAlignment alignment) { alignment_ = alignment; } |
| 70 BubbleAlignment alignment() const { return alignment_; } | 81 BubbleAlignment alignment() const { return alignment_; } |
| 71 | 82 |
| 72 static ArrowLocation horizontal_mirror(ArrowLocation loc) { | 83 static ArrowLocation horizontal_mirror(ArrowLocation loc) { |
| 73 return loc >= NONE ? loc : static_cast<ArrowLocation>(loc ^ 1); | 84 return (loc == TOP_CENTER || loc == BOTTOM_CENTER || loc >= NONE) ? |
| 85 loc : static_cast<ArrowLocation>(loc ^ 1); | |
| 74 } | 86 } |
| 75 | 87 |
| 76 static ArrowLocation vertical_mirror(ArrowLocation loc) { | 88 static ArrowLocation vertical_mirror(ArrowLocation loc) { |
| 77 return loc >= NONE ? loc : static_cast<ArrowLocation>(loc ^ 2); | 89 return (loc == LEFT_CENTER || loc == BOTTOM_CENTER || loc >= NONE) ? |
|
msw
2012/09/19 20:12:32
s/BOTTOM_CENTER/RIGHT_CENTER/
xiyuan
2012/09/19 22:51:36
Oops. Fixed now. :p
| |
| 90 loc : static_cast<ArrowLocation>(loc ^ 2); | |
| 78 } | 91 } |
| 79 | 92 |
| 80 static bool has_arrow(ArrowLocation loc) { | 93 static bool has_arrow(ArrowLocation loc) { |
| 81 return loc >= NONE ? false : true; | 94 return loc >= NONE ? false : true; |
| 82 } | 95 } |
| 83 | 96 |
| 84 static bool is_arrow_on_left(ArrowLocation loc) { | 97 static bool is_arrow_on_left(ArrowLocation loc) { |
| 85 return loc >= NONE ? false : !(loc & 1); | 98 return loc >= NONE ? false : !(loc & 1); |
| 86 } | 99 } |
| 87 | 100 |
| 88 static bool is_arrow_on_top(ArrowLocation loc) { | 101 static bool is_arrow_on_top(ArrowLocation loc) { |
| 89 return loc >= NONE ? false : !(loc & 2); | 102 return loc >= NONE ? false : !(loc & 2); |
| 90 } | 103 } |
| 91 | 104 |
| 92 static bool is_arrow_on_horizontal(ArrowLocation loc) { | 105 static bool is_arrow_on_horizontal(ArrowLocation loc) { |
| 93 return loc >= NONE ? false : !(loc & 4); | 106 return loc >= NONE ? false : !(loc & 4); |
| 94 } | 107 } |
| 95 | 108 |
| 109 static bool is_arrow_at_center(ArrowLocation loc) { | |
| 110 return has_arrow(loc) && !!(loc & 8); | |
| 111 } | |
| 112 | |
| 96 // Sets the background color for the arrow body. This is irrelevant if you do | 113 // Sets the background color for the arrow body. This is irrelevant if you do |
| 97 // not also set the arrow location to something other than NONE. | 114 // not also set the arrow location to something other than NONE. |
| 98 void set_background_color(SkColor color) { background_color_ = color; } | 115 void set_background_color(SkColor color) { background_color_ = color; } |
| 99 SkColor background_color() const { return background_color_; } | 116 SkColor background_color() const { return background_color_; } |
| 100 | 117 |
| 101 void set_client_bounds(const gfx::Rect& bounds) { client_bounds_ = bounds; } | 118 void set_client_bounds(const gfx::Rect& bounds) { client_bounds_ = bounds; } |
| 102 const gfx::Rect& client_bounds() const { return client_bounds_; } | 119 const gfx::Rect& client_bounds() const { return client_bounds_; } |
| 103 | 120 |
| 121 // Sets a fixed offset for the arrow from the beginning of corresponding edge. | |
| 122 // The arrow will still point to the same location but the bubble will shift | |
| 123 // location to make that happen. | |
|
msw
2012/09/19 20:12:32
good explanation!
xiyuan
2012/09/19 22:51:36
hehe... :) I wish I could write something this goo
| |
| 124 void set_arrow_offset(int offset) { override_arrow_offset_ = offset; } | |
| 125 | |
| 104 // For borders with an arrow, gives the desired bounds (in screen coordinates) | 126 // For borders with an arrow, gives the desired bounds (in screen coordinates) |
| 105 // given the rect to point to and the size of the contained contents. This | 127 // given the rect to point to and the size of the contained contents. This |
| 106 // depends on the arrow location, so if you change that, you should call this | 128 // depends on the arrow location, so if you change that, you should call this |
| 107 // again to find out the new coordinates. | 129 // again to find out the new coordinates. |
| 108 virtual gfx::Rect GetBounds(const gfx::Rect& position_relative_to, | 130 virtual gfx::Rect GetBounds(const gfx::Rect& position_relative_to, |
| 109 const gfx::Size& contents_size) const; | 131 const gfx::Size& contents_size) const; |
| 110 | 132 |
| 111 // Sets a fixed offset for the arrow from the beginning of corresponding edge. | 133 // Returns the corner radius of the current image set. |
| 112 // The arrow will still point to the same location but the bubble will shift | 134 int GetBorderCornerRadius() const; |
| 113 // location to make that happen. Returns actuall arrow offset, in case of | 135 |
| 114 // overflow it differ from desired. | 136 // Gets the arrow offset to use. |
| 115 int SetArrowOffset(int offset, const gfx::Size& contents_size); | 137 int GetArrowOffset(const gfx::Size& border_size) const; |
| 116 | 138 |
| 117 // Overridden from Border: | 139 // Overridden from Border: |
| 118 virtual void GetInsets(gfx::Insets* insets) const OVERRIDE; | 140 virtual void GetInsets(gfx::Insets* insets) const OVERRIDE; |
| 119 | 141 |
| 120 // How many pixels the bubble border is from the edge of the images. | 142 // How many pixels the bubble border is from the edge of the images. |
| 121 virtual int GetBorderThickness() const; | 143 virtual int GetBorderThickness() const; |
| 122 | 144 |
| 123 protected: | 145 protected: |
| 124 virtual ~BubbleBorder(); | 146 virtual ~BubbleBorder(); |
| 125 | 147 |
| 126 // Calculates the insets for a specific arrow location. Normally called from | 148 // Calculates the insets for a specific arrow location. Normally called from |
| 127 // GetInsets(arrow_location()), but may be called by specialized BubbleBorder | 149 // GetInsets(arrow_location()), but may be called by specialized BubbleBorder |
| 128 // implementations. | 150 // implementations. |
| 129 virtual void GetInsetsForArrowLocation(gfx::Insets* insets, | 151 virtual void GetInsetsForArrowLocation(gfx::Insets* insets, |
| 130 ArrowLocation arrow_loc) const; | 152 ArrowLocation arrow_loc) const; |
| 131 | 153 |
| 132 private: | 154 private: |
| 133 struct BorderImages; | 155 struct BorderImages; |
| 134 | 156 |
| 135 // Loads images if necessary. | 157 // Loads images if necessary. |
| 136 static BorderImages* GetBorderImages(Shadow shadow); | 158 static BorderImages* GetBorderImages(Shadow shadow); |
| 137 | 159 |
| 138 // Overridden from Border: | 160 // Overridden from Border: |
| 139 virtual void Paint(const View& view, | 161 virtual void Paint(const View& view, |
| 140 gfx::Canvas* canvas) const OVERRIDE; | 162 gfx::Canvas* canvas) const OVERRIDE; |
| 141 | 163 |
| 142 void DrawEdgeWithArrow(gfx::Canvas* canvas, | 164 void DrawEdgeWithArrow(gfx::Canvas* canvas, |
| 143 bool is_horizontal, | 165 bool is_horizontal, |
| 144 gfx::ImageSkia* edge, | 166 const gfx::ImageSkia& edge, |
| 145 gfx::ImageSkia* arrow, | 167 const gfx::ImageSkia& arrow, |
| 146 int start_x, | 168 int start_x, |
| 147 int start_y, | 169 int start_y, |
| 148 int before_arrow, | 170 int before_arrow, |
| 149 int after_arrow, | 171 int after_arrow, |
| 150 int offset) const; | 172 int offset) const; |
| 151 | 173 |
| 152 void DrawArrowInterior(gfx::Canvas* canvas, float tip_x, float tip_y) const; | 174 void DrawArrowInterior(gfx::Canvas* canvas, float tip_x, float tip_y) const; |
| 153 | 175 |
| 154 // Border graphics. | 176 // Border graphics. |
| 155 struct BorderImages* images_; | 177 struct BorderImages* images_; |
| 156 | 178 |
| 157 // Image bundles. | 179 // Image bundles. |
| 158 static struct BorderImages* normal_images_; | 180 static struct BorderImages* border_images_[SHADOW_COUNT]; |
| 159 static struct BorderImages* shadow_images_; | |
| 160 | 181 |
| 161 // Minimal offset of the arrow from the closet edge of bounding rect. | 182 // Minimal offset of the arrow from the closet edge of bounding rect. |
| 162 int arrow_offset_; | 183 int arrow_offset_; |
| 163 | 184 |
| 164 // If specified, overrides the pre-calculated |arrow_offset_| of the arrow. | 185 // If specified, overrides the pre-calculated |arrow_offset_| of the arrow. |
| 165 int override_arrow_offset_; | 186 int override_arrow_offset_; |
| 166 | 187 |
| 167 ArrowLocation arrow_location_; | 188 ArrowLocation arrow_location_; |
| 168 BubbleAlignment alignment_; | 189 BubbleAlignment alignment_; |
| 169 SkColor background_color_; | 190 SkColor background_color_; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 186 | 207 |
| 187 private: | 208 private: |
| 188 BubbleBorder* border_; | 209 BubbleBorder* border_; |
| 189 | 210 |
| 190 DISALLOW_COPY_AND_ASSIGN(BubbleBackground); | 211 DISALLOW_COPY_AND_ASSIGN(BubbleBackground); |
| 191 }; | 212 }; |
| 192 | 213 |
| 193 } // namespace views | 214 } // namespace views |
| 194 | 215 |
| 195 #endif // UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ | 216 #endif // UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ |
| OLD | NEW |