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
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_
6 #define UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "ui/views/background.h"
11 #include "ui/views/border.h"
24 // Renders a border, with optional arrow, and a custom dropshadow.
25 // This can be used to produce floating "bubble" objects with rounded corners.
26 class VIEWS_EXPORT BubbleBorder
: public Border
{
28 // Possible locations for the (optional) arrow.
29 // 0 bit specifies left or right.
30 // 1 bit specifies top or bottom.
31 // 2 bit specifies horizontal or vertical.
32 // 3 bit specifies whether the arrow at the center of its residing edge.
44 BOTTOM_RIGHT
= BOTTOM
| RIGHT
,
46 RIGHT_TOP
= VERTICAL
| RIGHT
,
47 LEFT_BOTTOM
= VERTICAL
| BOTTOM
,
48 RIGHT_BOTTOM
= VERTICAL
| BOTTOM
| RIGHT
,
50 BOTTOM_CENTER
= CENTER
| BOTTOM
,
51 LEFT_CENTER
= CENTER
| VERTICAL
,
52 RIGHT_CENTER
= CENTER
| VERTICAL
| RIGHT
,
53 NONE
= 16, // No arrow. Positioned under the supplied rect.
54 FLOAT
= 17, // No arrow. Centered over the supplied rect.
59 NO_SHADOW_OPAQUE_BORDER
,
65 // The position of the bubble in relation to the anchor.
66 enum BubbleAlignment
{
67 // The tip of the arrow points to the middle of the anchor.
68 ALIGN_ARROW_TO_MID_ANCHOR
,
69 // The edge nearest to the arrow is lined up with the edge of the anchor.
70 ALIGN_EDGE_TO_ANCHOR_EDGE
,
73 // The way the arrow should be painted.
75 // Fully render the arrow.
77 // Leave space for the arrow, but do not paint it.
79 // Neither paint nor leave space for the arrow.
83 BubbleBorder(Arrow arrow
, Shadow shadow
, SkColor color
);
84 virtual ~BubbleBorder();
86 // Returns the radius of the corner of the border.
87 // TODO(xiyuan): Get rid of this since it's part of BorderImages now?
88 static int GetCornerRadius() {
89 // We can't safely calculate a border radius by comparing the sizes of the
90 // side and corner images, because either may have been extended in various
91 // directions in order to do more subtle dropshadow fading or other effects.
92 // So we hardcode the most accurate value.
96 static bool has_arrow(Arrow a
) { return a
< NONE
; }
98 static bool is_arrow_on_left(Arrow a
) {
99 return has_arrow(a
) && (a
== LEFT_CENTER
|| !(a
& (RIGHT
| CENTER
)));
102 static bool is_arrow_on_top(Arrow a
) {
103 return has_arrow(a
) && (a
== TOP_CENTER
|| !(a
& (BOTTOM
| CENTER
)));
106 static bool is_arrow_on_horizontal(Arrow a
) {
107 return a
>= NONE
? false : !(a
& VERTICAL
);
110 static bool is_arrow_at_center(Arrow a
) {
111 return has_arrow(a
) && !!(a
& CENTER
);
114 static Arrow
horizontal_mirror(Arrow a
) {
115 return (a
== TOP_CENTER
|| a
== BOTTOM_CENTER
|| a
>= NONE
) ?
116 a
: static_cast<Arrow
>(a
^ RIGHT
);
119 static Arrow
vertical_mirror(Arrow a
) {
120 return (a
== LEFT_CENTER
|| a
== RIGHT_CENTER
|| a
>= NONE
) ?
121 a
: static_cast<Arrow
>(a
^ BOTTOM
);
124 // Get or set the arrow type.
125 void set_arrow(Arrow arrow
) { arrow_
= arrow
; }
126 Arrow
arrow() const { return arrow_
; }
128 // Get or set the bubble alignment.
129 void set_alignment(BubbleAlignment alignment
) { alignment_
= alignment
; }
130 BubbleAlignment
alignment() const { return alignment_
; }
132 // Get the shadow type.
133 Shadow
shadow() const { return shadow_
; }
135 // Get or set the background color for the bubble and arrow body.
136 void set_background_color(SkColor color
) { background_color_
= color
; }
137 SkColor
background_color() const { return background_color_
; }
139 // If true, the background color should be determined by the host's
141 void set_use_theme_background_color(bool use_theme_background_color
) {
142 use_theme_background_color_
= use_theme_background_color
;
144 bool use_theme_background_color() { return use_theme_background_color_
; }
146 // Sets a desired pixel distance between the arrow tip and the outside edge of
147 // the neighboring border image. For example: |----offset----|
148 // '(' represents shadow around the '{' edge: ((({ ^ })))
149 // The arrow will still anchor to the same location but the bubble will shift
150 // location to place the arrow |offset| pixels from the perpendicular edge.
151 void set_arrow_offset(int offset
) { arrow_offset_
= offset
; }
153 // Sets the way the arrow is actually painted. Default is PAINT_NORMAL.
154 void set_paint_arrow(ArrowPaintType value
) { arrow_paint_type_
= value
; }
156 // Get the desired widget bounds (in screen coordinates) given the anchor rect
157 // and bubble content size; calculated from shadow and arrow image dimensions.
158 virtual gfx::Rect
GetBounds(const gfx::Rect
& anchor_rect
,
159 const gfx::Size
& contents_size
) const;
161 // Get the border exterior thickness, including stroke and shadow, in pixels.
162 int GetBorderThickness() const;
164 // Returns the corner radius of the current image set.
165 int GetBorderCornerRadius() const;
167 // Gets the arrow offset to use.
168 int GetArrowOffset(const gfx::Size
& border_size
) const;
170 // Overridden from Border:
171 virtual void Paint(const View
& view
, gfx::Canvas
* canvas
) OVERRIDE
;
172 virtual gfx::Insets
GetInsets() const OVERRIDE
;
173 virtual gfx::Size
GetMinimumSize() const OVERRIDE
;
176 gfx::Size
GetSizeForContentsSize(const gfx::Size
& contents_size
) const;
177 gfx::ImageSkia
* GetArrowImage() const;
178 gfx::Rect
GetArrowRect(const gfx::Rect
& bounds
) const;
179 void DrawArrow(gfx::Canvas
* canvas
, const gfx::Rect
& arrow_bounds
) const;
183 ArrowPaintType arrow_paint_type_
;
184 BubbleAlignment alignment_
;
186 internal::BorderImages
* images_
;
187 SkColor background_color_
;
188 bool use_theme_background_color_
;
190 DISALLOW_COPY_AND_ASSIGN(BubbleBorder
);
193 // A Background that clips itself to the specified BubbleBorder and uses
194 // the background color of the BubbleBorder.
195 class VIEWS_EXPORT BubbleBackground
: public Background
{
197 explicit BubbleBackground(BubbleBorder
* border
) : border_(border
) {}
199 // Overridden from Background:
200 virtual void Paint(gfx::Canvas
* canvas
, View
* view
) const OVERRIDE
;
203 BubbleBorder
* border_
;
205 DISALLOW_COPY_AND_ASSIGN(BubbleBackground
);
210 #endif // UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_