bubble_frame_view.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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/chromeos/frame/bubble_frame_view.h"
6
7#include "gfx/canvas_skia.h"
8#include "gfx/font.h"
9#include "gfx/insets.h"
10#include "gfx/path.h"
11#include "gfx/rect.h"
12#include "chrome/browser/chromeos/frame/bubble_window.h"
13#include "chrome/browser/views/bubble_border.h"
14#include "views/controls/label.h"
15#include "views/window/hit_test.h"
16#include "views/window/window.h"
17#include "views/window/window_delegate.h"
18#include "third_party/skia/include/core/SkPaint.h"
19
20namespace {
21
22static const int kTitleTopPadding = 10;
23static const int kTitleContentPadding = 10;
24static const int kHorizontalPadding = 10;
25
26}  // namespace
27
28namespace chromeos {
29
30BubbleFrameView::BubbleFrameView(views::Window* frame)
31    : frame_(frame),
32      title_(NULL) {
33  set_border(new BubbleBorder(BubbleBorder::NONE));
34
35  title_ = new views::Label(frame_->GetDelegate()->GetWindowTitle());
36  title_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
37  title_->SetFont(title_->font().DeriveFont(1, gfx::Font::BOLD));
38  AddChildView(title_);
39}
40
41BubbleFrameView::~BubbleFrameView() {
42}
43
44gfx::Rect BubbleFrameView::GetBoundsForClientView() const {
45  return client_view_bounds_;
46}
47
48gfx::Rect BubbleFrameView::GetWindowBoundsForClientBounds(
49    const gfx::Rect& client_bounds) const {
50  gfx::Insets insets = GetInsets();
51  gfx::Size title_size = title_->GetPreferredSize();
52  int top_height = insets.top() + title_size.height() + kTitleContentPadding;
53  return gfx::Rect(std::max(0, client_bounds.x() - insets.left()),
54                   std::max(0, client_bounds.y() - top_height),
55                   client_bounds.width() + insets.width(),
56                   client_bounds.height() + top_height + insets.bottom());
57}
58
59int BubbleFrameView::NonClientHitTest(const gfx::Point& point) {
60  return HTNOWHERE;
61}
62
63void BubbleFrameView::GetWindowMask(const gfx::Size& size,
64                                    gfx::Path* window_mask) {
65}
66
67void BubbleFrameView::EnableClose(bool enable) {
68}
69
70void BubbleFrameView::ResetWindowControls() {
71}
72
73gfx::Insets BubbleFrameView::GetInsets() const {
74  gfx::Insets border_insets;
75  border()->GetInsets(&border_insets);
76
77  gfx::Insets insets(kTitleTopPadding,
78                     kHorizontalPadding,
79                     0,
80                     kHorizontalPadding);
81  insets += border_insets;
82  return insets;
83}
84
85gfx::Size BubbleFrameView::GetPreferredSize() {
86  gfx::Size pref = frame_->GetClientView()->GetPreferredSize();
87  gfx::Rect bounds(0, 0, pref.width(), pref.height());
88  return frame_->GetNonClientView()->GetWindowBoundsForClientBounds(
89      bounds).size();
90}
91
92void BubbleFrameView::Layout() {
93  gfx::Insets insets = GetInsets();
94
95  gfx::Size title_size = title_->GetPreferredSize();
96  title_->SetBounds(insets.left(), insets.top(),
97      std::max(0, width() - insets.width()),
98      title_size.height());
99
100  int top_height = insets.top() + title_size.height() + kTitleContentPadding;
101  client_view_bounds_.SetRect(insets.left(), top_height,
102      std::max(0, width() - insets.width()),
103      std::max(0, height() - top_height - insets.bottom()));
104}
105
106void BubbleFrameView::Paint(gfx::Canvas* canvas) {
107  // The border of this view creates an anti-aliased round-rect region for the
108  // contents, which we need to fill with the background color.
109  SkPaint paint;
110  paint.setAntiAlias(true);
111  paint.setStyle(SkPaint::kFill_Style);
112  paint.setColor(BubbleWindow::kBackgroundColor);
113  gfx::Path path;
114  gfx::Rect bounds(GetLocalBounds(false));
115  SkRect rect;
116  rect.set(SkIntToScalar(bounds.x()), SkIntToScalar(bounds.y()),
117           SkIntToScalar(bounds.right()), SkIntToScalar(bounds.bottom()));
118  SkScalar radius = SkIntToScalar(BubbleBorder::GetCornerRadius());
119  path.addRoundRect(rect, radius, radius);
120  canvas->AsCanvasSkia()->drawPath(path, paint);
121
122  PaintBorder(canvas);
123}
124
125}  // namespace chromeos
126