summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/bubble_border.h
blob: 3670503fba94506be54e3dc14f8fdfc6aede217c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_VIEWS_BUBBLE_BORDER_H_
#define CHROME_BROWSER_VIEWS_BUBBLE_BORDER_H_

#include "third_party/skia/include/core/SkColor.h"
#include "views/background.h"
#include "views/border.h"

class SkBitmap;

// Renders a border, with optional arrow (off by default), and a custom
// dropshadow.  This can be used to produce floating "bubble" objects.
//
// If the arrow is on, the bubble has four round corner.  If not, it has round
// corners on the bottom and square corners on the top, and lacks a top border.
class BubbleBorder : public views::Border {
 public:
  // Possible locations for the (optional) arrow.
  enum ArrowLocation {
    NONE,
    TOP_LEFT,
    TOP_RIGHT,
    BOTTOM_LEFT,
    BOTTOM_RIGHT
  };

  BubbleBorder() : override_arrow_x_offset_(0),
                   arrow_location_(NONE),
                   background_color_(SK_ColorWHITE) {
    InitClass();
  }

  // Returns the radius of the corner of the border.
  static int GetCornerRadius() {
    // We can't safely calculate a border radius by comparing the sizes of the
    // side and corner images, because either may have been extended in various
    // directions in order to do more subtle dropshadow fading or other effects.
    // So we hardcode the most accurate value.
    return 4;
  }

  // Sets the location for the arrow.
  void set_arrow_location(ArrowLocation arrow_location) {
    arrow_location_ = arrow_location;
  }

  // Sets a fixed x offset for the arrow. The arrow will still point to the
  // same location but the bubble will shift horizontally to make that happen.
  void set_arrow_offset(int offset) {
    override_arrow_x_offset_ = offset;
  }

  // Sets the background color for the arrow body.  This is irrelevant if you do
  // not also set the arrow location to something other than NONE.
  void set_background_color(SkColor background_color) {
    background_color_ = background_color;
  }
  SkColor background_color() const { return background_color_; }

  // For borders with an arrow, gives the desired bounds (in screen coordinates)
  // given the rect to point to and the size of the contained contents.  This
  // depends on the arrow location, so if you change that, you should call this
  // again to find out the new coordinates.
  //
  // For borders without an arrow, gives the bounds with the content centered
  // underneath the supplied rect.
  gfx::Rect GetBounds(const gfx::Rect& position_relative_to,
                      const gfx::Size& contents_size) const;

  // Overridden from views::Border:
  virtual void GetInsets(gfx::Insets* insets) const;

 private:
  // Loads images if necessary.
  static void InitClass();

  virtual ~BubbleBorder() { }

  // Returns true if there is an arrow and it is positioned on the bottom edge.
  bool arrow_is_bottom() const {
    return (arrow_location_ == BOTTOM_LEFT) ||
           (arrow_location_ == BOTTOM_RIGHT);
  }

  // Returns true if there is an arrow and it is positioned on the left side.
  bool arrow_is_left() const {
    return (arrow_location_ == TOP_LEFT) || (arrow_location_ == BOTTOM_LEFT);
  }

  // Overridden from views::Border:
  virtual void Paint(const views::View& view, gfx::Canvas* canvas) const;

  // Border graphics.
  static SkBitmap* left_;
  static SkBitmap* top_left_;
  static SkBitmap* top_;
  static SkBitmap* top_right_;
  static SkBitmap* right_;
  static SkBitmap* bottom_right_;
  static SkBitmap* bottom_;
  static SkBitmap* bottom_left_;
  static SkBitmap* top_arrow_;
  static SkBitmap* bottom_arrow_;

  static int arrow_x_offset_;

  // If specified, overrides the pre-calculated |arrow_x_offset_| of the arrow.
  int override_arrow_x_offset_;

  ArrowLocation arrow_location_;
  SkColor background_color_;

  DISALLOW_COPY_AND_ASSIGN(BubbleBorder);
};

// A Background that clips itself to the specified BubbleBorder and uses
// the background color of the BubbleBorder.
class BubbleBackground : public views::Background {
 public:
  explicit BubbleBackground(BubbleBorder* border) : border_(border) {}

  // Background overrides.
  virtual void Paint(gfx::Canvas* canvas, views::View* view) const;

 private:
  BubbleBorder* border_;

  DISALLOW_COPY_AND_ASSIGN(BubbleBackground);
};

#endif  // CHROME_BROWSER_VIEWS_BUBBLE_BORDER_H_