Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: ui/views/bubble/bubble_border.h

Issue 10905311: Consolidate bubble border code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use GetImageSkiaNamed Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/resources/ui_resources.grd ('k') | ui/views/bubble/bubble_border.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 the arrow at the center of its residing edge.
28 enum ArrowLocationMask {
29 RIGHT = 0x01,
30 BOTTOM = 0x02,
31 VERTICAL = 0x04,
32 CENTER = 0x08,
33 };
34
27 enum ArrowLocation { 35 enum ArrowLocation {
28 TOP_LEFT = 0, 36 TOP_LEFT = 0,
29 TOP_RIGHT = 1, 37 TOP_RIGHT = RIGHT,
30 BOTTOM_LEFT = 2, 38 BOTTOM_LEFT = BOTTOM,
31 BOTTOM_RIGHT = 3, 39 BOTTOM_RIGHT = BOTTOM | RIGHT,
32 LEFT_TOP = 4, 40 LEFT_TOP = VERTICAL,
33 RIGHT_TOP = 5, 41 RIGHT_TOP = VERTICAL | RIGHT,
34 LEFT_BOTTOM = 6, 42 LEFT_BOTTOM = VERTICAL | BOTTOM,
35 RIGHT_BOTTOM = 7, 43 RIGHT_BOTTOM = VERTICAL | BOTTOM | RIGHT,
36 NONE = 8, // No arrow. Positioned under the supplied rect. 44 TOP_CENTER = CENTER,
37 FLOAT = 9 // No arrow. Centered over the supplied rect. 45 BOTTOM_CENTER = CENTER | BOTTOM,
46 LEFT_CENTER = CENTER | VERTICAL,
47 RIGHT_CENTER = CENTER | VERTICAL | RIGHT,
48 NONE = 16, // No arrow. Positioned under the supplied rect.
49 FLOAT = 17, // No arrow. Centered over the supplied rect.
38 }; 50 };
39 51
40 enum Shadow { 52 enum Shadow {
41 SHADOW = 0, 53 SHADOW = 0,
42 NO_SHADOW = 1 54 NO_SHADOW,
55 BIG_SHADOW,
msw 2012/09/20 17:40:09 Why did you remove the #if defined(USE_AURA) here?
xiyuan 2012/09/20 17:58:29 Right now app list is in progress of porting to Wi
56 SMALL_SHADOW,
57 SHADOW_COUNT,
43 }; 58 };
44 59
45 // The position of the bubble in relation to the anchor. 60 // The position of the bubble in relation to the anchor.
46 enum BubbleAlignment { 61 enum BubbleAlignment {
47 // The tip of the arrow points to the middle of the anchor. 62 // The tip of the arrow points to the middle of the anchor.
48 ALIGN_ARROW_TO_MID_ANCHOR, 63 ALIGN_ARROW_TO_MID_ANCHOR,
49 // The edge nearest to the arrow is lined up with the edge of the anchor. 64 // The edge nearest to the arrow is lined up with the edge of the anchor.
50 ALIGN_EDGE_TO_ANCHOR_EDGE 65 ALIGN_EDGE_TO_ANCHOR_EDGE
51 }; 66 };
52 67
53 BubbleBorder(ArrowLocation arrow_location, Shadow shadow); 68 BubbleBorder(ArrowLocation arrow_location, Shadow shadow);
54 69
55 // Returns the radius of the corner of the border. 70 // Returns the radius of the corner of the border.
71 // TODO(xiyuan): Get rid of this since it's part of BorderImages now?
56 static int GetCornerRadius() { 72 static int GetCornerRadius() {
57 // We can't safely calculate a border radius by comparing the sizes of the 73 // 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 74 // 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. 75 // directions in order to do more subtle dropshadow fading or other effects.
60 // So we hardcode the most accurate value. 76 // So we hardcode the most accurate value.
61 return 4; 77 return 4;
62 } 78 }
63 79
64 // Sets the location for the arrow. 80 // Sets the location for the arrow.
65 void set_arrow_location(ArrowLocation loc) { arrow_location_ = loc; } 81 void set_arrow_location(ArrowLocation loc) { arrow_location_ = loc; }
66 ArrowLocation arrow_location() const { return arrow_location_; } 82 ArrowLocation arrow_location() const { return arrow_location_; }
67 83
68 // Sets the alignment. 84 // Sets the alignment.
69 void set_alignment(BubbleAlignment alignment) { alignment_ = alignment; } 85 void set_alignment(BubbleAlignment alignment) { alignment_ = alignment; }
70 BubbleAlignment alignment() const { return alignment_; } 86 BubbleAlignment alignment() const { return alignment_; }
71 87
72 static ArrowLocation horizontal_mirror(ArrowLocation loc) { 88 static ArrowLocation horizontal_mirror(ArrowLocation loc) {
73 return loc >= NONE ? loc : static_cast<ArrowLocation>(loc ^ 1); 89 return (loc == TOP_CENTER || loc == BOTTOM_CENTER || loc >= NONE) ?
90 loc : static_cast<ArrowLocation>(loc ^ RIGHT);
74 } 91 }
75 92
76 static ArrowLocation vertical_mirror(ArrowLocation loc) { 93 static ArrowLocation vertical_mirror(ArrowLocation loc) {
77 return loc >= NONE ? loc : static_cast<ArrowLocation>(loc ^ 2); 94 return (loc == LEFT_CENTER || loc == RIGHT_CENTER || loc >= NONE) ?
95 loc : static_cast<ArrowLocation>(loc ^ BOTTOM);
78 } 96 }
79 97
80 static bool has_arrow(ArrowLocation loc) { 98 static bool has_arrow(ArrowLocation loc) {
81 return loc >= NONE ? false : true; 99 return loc >= NONE ? false : true;
82 } 100 }
83 101
84 static bool is_arrow_on_left(ArrowLocation loc) { 102 static bool is_arrow_on_left(ArrowLocation loc) {
85 return loc >= NONE ? false : !(loc & 1); 103 return (loc == TOP_CENTER || loc == BOTTOM_CENTER || loc >= NONE) ?
104 false : !(loc & RIGHT);
86 } 105 }
87 106
88 static bool is_arrow_on_top(ArrowLocation loc) { 107 static bool is_arrow_on_top(ArrowLocation loc) {
89 return loc >= NONE ? false : !(loc & 2); 108 return (loc == LEFT_CENTER || loc == RIGHT_CENTER || loc >= NONE) ?
109 false : !(loc & BOTTOM);
90 } 110 }
91 111
92 static bool is_arrow_on_horizontal(ArrowLocation loc) { 112 static bool is_arrow_on_horizontal(ArrowLocation loc) {
93 return loc >= NONE ? false : !(loc & 4); 113 return loc >= NONE ? false : !(loc & VERTICAL);
114 }
115
116 static bool is_arrow_at_center(ArrowLocation loc) {
117 return has_arrow(loc) && !!(loc & CENTER);
94 } 118 }
95 119
96 // Sets the background color for the arrow body. This is irrelevant if you do 120 // 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. 121 // not also set the arrow location to something other than NONE.
98 void set_background_color(SkColor color) { background_color_ = color; } 122 void set_background_color(SkColor color) { background_color_ = color; }
99 SkColor background_color() const { return background_color_; } 123 SkColor background_color() const { return background_color_; }
100 124
101 void set_client_bounds(const gfx::Rect& bounds) { client_bounds_ = bounds; } 125 void set_client_bounds(const gfx::Rect& bounds) { client_bounds_ = bounds; }
102 const gfx::Rect& client_bounds() const { return client_bounds_; } 126 const gfx::Rect& client_bounds() const { return client_bounds_; }
103 127
128 // Sets a fixed offset for the arrow from the beginning of corresponding edge.
129 // The arrow will still point to the same location but the bubble will shift
130 // location to make that happen.
131 void set_arrow_offset(int offset) { override_arrow_offset_ = offset; }
132
104 // For borders with an arrow, gives the desired bounds (in screen coordinates) 133 // 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 134 // 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 135 // depends on the arrow location, so if you change that, you should call this
107 // again to find out the new coordinates. 136 // again to find out the new coordinates.
108 virtual gfx::Rect GetBounds(const gfx::Rect& position_relative_to, 137 virtual gfx::Rect GetBounds(const gfx::Rect& position_relative_to,
109 const gfx::Size& contents_size) const; 138 const gfx::Size& contents_size) const;
110 139
111 // Sets a fixed offset for the arrow from the beginning of corresponding edge. 140 // Returns the corner radius of the current image set.
112 // The arrow will still point to the same location but the bubble will shift 141 int GetBorderCornerRadius() const;
113 // location to make that happen. Returns actuall arrow offset, in case of 142
114 // overflow it differ from desired. 143 // Gets the arrow offset to use.
115 int SetArrowOffset(int offset, const gfx::Size& contents_size); 144 int GetArrowOffset(const gfx::Size& border_size) const;
116 145
117 // Overridden from Border: 146 // Overridden from Border:
118 virtual void GetInsets(gfx::Insets* insets) const OVERRIDE; 147 virtual void GetInsets(gfx::Insets* insets) const OVERRIDE;
119 148
120 // How many pixels the bubble border is from the edge of the images. 149 // How many pixels the bubble border is from the edge of the images.
121 virtual int GetBorderThickness() const; 150 virtual int GetBorderThickness() const;
122 151
123 protected: 152 protected:
124 virtual ~BubbleBorder(); 153 virtual ~BubbleBorder();
125 154
126 // Calculates the insets for a specific arrow location. Normally called from 155 // Calculates the insets for a specific arrow location. Normally called from
127 // GetInsets(arrow_location()), but may be called by specialized BubbleBorder 156 // GetInsets(arrow_location()), but may be called by specialized BubbleBorder
128 // implementations. 157 // implementations.
129 virtual void GetInsetsForArrowLocation(gfx::Insets* insets, 158 virtual void GetInsetsForArrowLocation(gfx::Insets* insets,
130 ArrowLocation arrow_loc) const; 159 ArrowLocation arrow_loc) const;
131 160
132 private: 161 private:
133 struct BorderImages; 162 struct BorderImages;
134 163
135 // Loads images if necessary. 164 // Loads images if necessary.
136 static BorderImages* GetBorderImages(Shadow shadow); 165 static BorderImages* GetBorderImages(Shadow shadow);
137 166
138 // Overridden from Border: 167 // Overridden from Border:
139 virtual void Paint(const View& view, 168 virtual void Paint(const View& view,
140 gfx::Canvas* canvas) const OVERRIDE; 169 gfx::Canvas* canvas) const OVERRIDE;
141 170
142 void DrawEdgeWithArrow(gfx::Canvas* canvas, 171 void DrawEdgeWithArrow(gfx::Canvas* canvas,
143 bool is_horizontal, 172 bool is_horizontal,
144 gfx::ImageSkia* edge, 173 const gfx::ImageSkia& edge,
145 gfx::ImageSkia* arrow, 174 const gfx::ImageSkia& arrow,
146 int start_x, 175 int start_x,
147 int start_y, 176 int start_y,
148 int before_arrow, 177 int before_arrow,
149 int after_arrow, 178 int after_arrow,
150 int offset) const; 179 int offset) const;
151 180
152 void DrawArrowInterior(gfx::Canvas* canvas, float tip_x, float tip_y) const; 181 void DrawArrowInterior(gfx::Canvas* canvas, float tip_x, float tip_y) const;
153 182
154 // Border graphics. 183 // Border graphics.
155 struct BorderImages* images_; 184 struct BorderImages* images_;
156 185
157 // Image bundles. 186 // Image bundles.
158 static struct BorderImages* normal_images_; 187 static struct BorderImages* border_images_[SHADOW_COUNT];
159 static struct BorderImages* shadow_images_;
160 188
161 // Minimal offset of the arrow from the closet edge of bounding rect. 189 // Minimal offset of the arrow from the closet edge of bounding rect.
162 int arrow_offset_; 190 int arrow_offset_;
163 191
164 // If specified, overrides the pre-calculated |arrow_offset_| of the arrow. 192 // If specified, overrides the pre-calculated |arrow_offset_| of the arrow.
165 int override_arrow_offset_; 193 int override_arrow_offset_;
166 194
167 ArrowLocation arrow_location_; 195 ArrowLocation arrow_location_;
168 BubbleAlignment alignment_; 196 BubbleAlignment alignment_;
169 SkColor background_color_; 197 SkColor background_color_;
(...skipping 16 matching lines...) Expand all
186 214
187 private: 215 private:
188 BubbleBorder* border_; 216 BubbleBorder* border_;
189 217
190 DISALLOW_COPY_AND_ASSIGN(BubbleBackground); 218 DISALLOW_COPY_AND_ASSIGN(BubbleBackground);
191 }; 219 };
192 220
193 } // namespace views 221 } // namespace views
194 222
195 #endif // UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_ 223 #endif // UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_
OLDNEW
« no previous file with comments | « ui/resources/ui_resources.grd ('k') | ui/views/bubble/bubble_border.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698