dropdown_bar_view.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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.
4
5#include "chrome/browser/ui/views/dropdown_bar_view.h"
6
7#include "chrome/browser/themes/theme_service.h"
8#include "chrome/browser/ui/view_ids.h"
9#include "chrome/browser/ui/views/frame/browser_view.h"
10#include "grit/theme_resources.h"
11#include "third_party/skia/include/core/SkCanvas.h"
12#include "third_party/skia/include/core/SkRect.h"
13#include "ui/base/resource/resource_bundle.h"
14#include "ui/gfx/canvas.h"
15#include "ui/gfx/image/image_skia.h"
16#include "ui/views/background.h"
17#include "ui/views/border.h"
18#include "ui/views/painter.h"
19#include "ui/views/widget/widget.h"
20
21namespace {
22
23// When we are animating, we draw only the top part of the left and right
24// edges to give the illusion that the find dialog is attached to the
25// window during this animation; this is the height of the items we draw.
26const int kAnimatingEdgeHeight = 5;
27
28// Background to paint toolbar background with rounded corners.
29class DropdownBackground : public views::Background {
30 public:
31  explicit DropdownBackground(BrowserView* browser,
32                              const gfx::ImageSkia* left_alpha_mask,
33                              const gfx::ImageSkia* right_alpha_mask);
34  virtual ~DropdownBackground() {}
35
36  // Overridden from views::Background.
37  virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE;
38
39 private:
40  BrowserView* browser_view_;
41  const gfx::ImageSkia* left_alpha_mask_;
42  const gfx::ImageSkia* right_alpha_mask_;
43
44  DISALLOW_COPY_AND_ASSIGN(DropdownBackground);
45};
46
47DropdownBackground::DropdownBackground(BrowserView* browser_view,
48                                     const gfx::ImageSkia* left_alpha_mask,
49                                     const gfx::ImageSkia* right_alpha_mask)
50    : browser_view_(browser_view),
51      left_alpha_mask_(left_alpha_mask),
52      right_alpha_mask_(right_alpha_mask) {
53}
54
55void DropdownBackground::Paint(gfx::Canvas* canvas, views::View* view) const {
56  // Find the offset from which to tile the toolbar background image.
57  // First, get the origin with respect to the screen.
58  gfx::Point origin = view->GetWidget()->GetWindowBoundsInScreen().origin();
59  // Now convert from screen to parent coordinates.
60  views::View::ConvertPointFromScreen(browser_view_, &origin);
61  // Finally, calculate the background image tiling offset.
62  origin = browser_view_->OffsetPointForToolbarBackgroundImage(origin);
63
64  ui::ThemeProvider* tp = view->GetThemeProvider();
65  gfx::ImageSkia background = *tp->GetImageSkiaNamed(IDR_THEME_TOOLBAR);
66
67  int left_edge_width = left_alpha_mask_->width();
68  int right_edge_width = right_alpha_mask_->width();
69  int mask_height = left_alpha_mask_->height();
70  int height = view->bounds().height();
71
72  // Stretch the middle background to cover the entire area.
73  canvas->TileImageInt(background, origin.x(), origin.y(),
74      0, 0, view->bounds().width(), height);
75
76  SkPaint paint;
77  paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
78  // Draw left edge.
79  canvas->DrawImageInt(*left_alpha_mask_, 0, 0, left_edge_width, mask_height,
80      0, 0, left_edge_width, height, false, paint);
81
82  // Draw right edge.
83  int x_right_edge = view->bounds().width() - right_edge_width;
84  canvas->DrawImageInt(*right_alpha_mask_, 0, 0, right_edge_width,
85      mask_height, x_right_edge, 0, right_edge_width, height, false, paint);
86}
87
88}  // namespace
89
90DropdownBarView::DropdownBarView(DropdownBarHost* host)
91    : host_(host),
92      animation_offset_(0) {
93}
94
95DropdownBarView::~DropdownBarView() {
96}
97
98////////////////////////////////////////////////////////////////////////////////
99// DropDownBarView, public:
100
101void DropdownBarView::SetAnimationOffset(int offset) {
102  animation_offset_ = offset;
103  set_clip_insets(gfx::Insets(animation_offset_, 0, 0, 0));
104}
105
106// DropDownBarView, views::View overrides:
107void DropdownBarView::OnPaint(gfx::Canvas* canvas) {
108  OnPaintBackground(canvas);
109  OnPaintBorder(canvas);
110
111  if (animation_offset() > 0) {
112     gfx::Canvas animating_edges(
113         gfx::Size(bounds().width(), kAnimatingEdgeHeight),
114         canvas->image_scale(),
115         false);
116     canvas->Translate(bounds().OffsetFromOrigin());
117     OnPaintBackground(&animating_edges);
118     OnPaintBorder(&animating_edges);
119     canvas->DrawImageInt(gfx::ImageSkia(animating_edges.ExtractImageRep()),
120         bounds().x(), animation_offset());
121  }
122}
123
124////////////////////////////////////////////////////////////////////////////////
125// DropDownBarView, protected:
126
127void DropdownBarView::SetBackground(const gfx::ImageSkia* left_alpha_mask,
128                                    const gfx::ImageSkia* right_alpha_mask) {
129  set_background(new DropdownBackground(host()->browser_view(), left_alpha_mask,
130      right_alpha_mask));
131}
132
133void DropdownBarView::SetBorderFromIds(int left_border_image_id,
134                                       int middle_border_image_id,
135                                       int right_border_image_id) {
136  int border_image_ids[3] = {left_border_image_id, middle_border_image_id,
137      right_border_image_id};
138  SetBorder(views::Border::CreateBorderPainter(
139      new views::HorizontalPainter(border_image_ids), gfx::Insets()));
140}
141