OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 VIEWS_BUBBLE_BUBBLE_BORDER_H_ | 5 #ifndef VIEWS_BUBBLE_BUBBLE_BORDER_H_ |
6 #define VIEWS_BUBBLE_BUBBLE_BORDER_H_ | 6 #define VIEWS_BUBBLE_BUBBLE_BORDER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "views/background.h" | 9 #include "ui/views/bubble/bubble_border.h" |
10 #include "views/border.h" | 10 // TODO(tfarina): remove this file once all includes have been updated. |
11 | |
12 class SkBitmap; | |
13 | |
14 namespace views { | |
15 | |
16 // Renders a border, with optional arrow, and a custom dropshadow. | |
17 // This can be used to produce floating "bubble" objects with rounded corners. | |
18 class VIEWS_EXPORT BubbleBorder : public views::Border { | |
19 public: | |
20 // Possible locations for the (optional) arrow. | |
21 // 0 bit specifies left or right. | |
22 // 1 bit specifies top or bottom. | |
23 // 2 bit specifies horizontal or vertical. | |
24 enum ArrowLocation { | |
25 TOP_LEFT = 0, | |
26 TOP_RIGHT = 1, | |
27 BOTTOM_LEFT = 2, | |
28 BOTTOM_RIGHT = 3, | |
29 LEFT_TOP = 4, | |
30 RIGHT_TOP = 5, | |
31 LEFT_BOTTOM = 6, | |
32 RIGHT_BOTTOM = 7, | |
33 NONE = 8, // No arrow. Positioned under the supplied rect. | |
34 FLOAT = 9 // No arrow. Centered over the supplied rect. | |
35 }; | |
36 | |
37 enum Shadow { | |
38 SHADOW = 0, | |
39 NO_SHADOW = 1 | |
40 }; | |
41 | |
42 // The position of the bubble in relation to the anchor. | |
43 enum BubbleAlignment { | |
44 // The tip of the arrow points to the middle of the anchor. | |
45 ALIGN_ARROW_TO_MID_ANCHOR, | |
46 // The edge nearest to the arrow is lined up with the edge of the anchor. | |
47 ALIGN_EDGE_TO_ANCHOR_EDGE | |
48 }; | |
49 | |
50 BubbleBorder(ArrowLocation arrow_location, Shadow shadow); | |
51 | |
52 // Returns the radius of the corner of the border. | |
53 static int GetCornerRadius() { | |
54 // We can't safely calculate a border radius by comparing the sizes of the | |
55 // side and corner images, because either may have been extended in various | |
56 // directions in order to do more subtle dropshadow fading or other effects. | |
57 // So we hardcode the most accurate value. | |
58 return 4; | |
59 } | |
60 | |
61 // Sets the location for the arrow. | |
62 void set_arrow_location(ArrowLocation arrow_location) { | |
63 arrow_location_ = arrow_location; | |
64 } | |
65 ArrowLocation arrow_location() const { return arrow_location_; } | |
66 | |
67 // Sets the alignment. | |
68 void set_alignment(BubbleAlignment alignment) { alignment_ = alignment; } | |
69 BubbleAlignment alignment() const { return alignment_; } | |
70 | |
71 static ArrowLocation horizontal_mirror(ArrowLocation loc) { | |
72 return loc >= NONE ? loc : static_cast<ArrowLocation>(loc ^ 1); | |
73 } | |
74 | |
75 static ArrowLocation vertical_mirror(ArrowLocation loc) { | |
76 return loc >= NONE ? loc : static_cast<ArrowLocation>(loc ^ 2); | |
77 } | |
78 | |
79 static bool has_arrow(ArrowLocation loc) { | |
80 return loc >= NONE ? false : true; | |
81 } | |
82 | |
83 static bool is_arrow_on_left(ArrowLocation loc) { | |
84 return loc >= NONE ? false : !(loc & 1); | |
85 } | |
86 | |
87 static bool is_arrow_on_top(ArrowLocation loc) { | |
88 return loc >= NONE ? false : !(loc & 2); | |
89 } | |
90 | |
91 static bool is_arrow_on_horizontal(ArrowLocation loc) { | |
92 return loc >= NONE ? false : !(loc & 4); | |
93 } | |
94 | |
95 // Sets the background color for the arrow body. This is irrelevant if you do | |
96 // not also set the arrow location to something other than NONE. | |
97 void set_background_color(SkColor background_color) { | |
98 background_color_ = background_color; | |
99 } | |
100 SkColor background_color() const { return background_color_; } | |
101 | |
102 // For borders with an arrow, gives the desired bounds (in screen coordinates) | |
103 // given the rect to point to and the size of the contained contents. This | |
104 // depends on the arrow location, so if you change that, you should call this | |
105 // again to find out the new coordinates. | |
106 gfx::Rect GetBounds(const gfx::Rect& position_relative_to, | |
107 const gfx::Size& contents_size) const; | |
108 | |
109 // Sets a fixed offset for the arrow from the beginning of corresponding edge. | |
110 // The arrow will still point to the same location but the bubble will shift | |
111 // location to make that happen. Returns actuall arrow offset, in case of | |
112 // overflow it differ from desired. | |
113 int SetArrowOffset(int offset, const gfx::Size& contents_size); | |
114 | |
115 // Overridden from views::Border: | |
116 virtual void GetInsets(gfx::Insets* insets) const; | |
117 | |
118 private: | |
119 struct BorderImages; | |
120 | |
121 // Loads images if necessary. | |
122 static BorderImages* GetBorderImages(Shadow shadow); | |
123 | |
124 virtual ~BubbleBorder(); | |
125 | |
126 // Overridden from views::Border: | |
127 virtual void Paint(const views::View& view, gfx::Canvas* canvas) const; | |
128 | |
129 void DrawEdgeWithArrow(gfx::Canvas* canvas, | |
130 bool is_horizontal, | |
131 SkBitmap* edge, | |
132 SkBitmap* arrow, | |
133 int start_x, | |
134 int start_y, | |
135 int before_arrow, | |
136 int after_arrow, | |
137 int offset) const; | |
138 | |
139 void DrawArrowInterior(gfx::Canvas* canvas, | |
140 bool is_horizontal, | |
141 int tip_x, | |
142 int tip_y, | |
143 int shift_x, | |
144 int shift_y) const; | |
145 | |
146 // Border graphics. | |
147 struct BorderImages* images_; | |
148 | |
149 // Image bundles. | |
150 static struct BorderImages* normal_images_; | |
151 static struct BorderImages* shadow_images_; | |
152 | |
153 // Minimal offset of the arrow from the closet edge of bounding rect. | |
154 int arrow_offset_; | |
155 | |
156 // If specified, overrides the pre-calculated |arrow_offset_| of the arrow. | |
157 int override_arrow_offset_; | |
158 | |
159 ArrowLocation arrow_location_; | |
160 BubbleAlignment alignment_; | |
161 SkColor background_color_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(BubbleBorder); | |
164 }; | |
165 | |
166 // A Background that clips itself to the specified BubbleBorder and uses | |
167 // the background color of the BubbleBorder. | |
168 class VIEWS_EXPORT BubbleBackground : public views::Background { | |
169 public: | |
170 explicit BubbleBackground(BubbleBorder* border) : border_(border) {} | |
171 | |
172 // Background overrides. | |
173 virtual void Paint(gfx::Canvas* canvas, views::View* view) const; | |
174 | |
175 private: | |
176 BubbleBorder* border_; | |
177 | |
178 DISALLOW_COPY_AND_ASSIGN(BubbleBackground); | |
179 }; | |
180 | |
181 } // namespace views | |
182 | 11 |
183 #endif // VIEWS_BUBBLE_BUBBLE_BORDER_H_ | 12 #endif // VIEWS_BUBBLE_BUBBLE_BORDER_H_ |
OLD | NEW |