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 #include "ui/aura_shell/launcher/tabbed_launcher_button.h" | 5 #include "ui/aura_shell/launcher/tabbed_launcher_button.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "grit/ui_resources.h" | 9 #include "grit/ui_resources.h" |
10 #include "ui/aura_shell/launcher/launcher_button_host.h" | 10 #include "ui/aura_shell/launcher/launcher_button_host.h" |
11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
14 #include "ui/gfx/insets.h" | 14 #include "ui/gfx/insets.h" |
15 #include "views/painter.h" | |
16 | 15 |
17 namespace aura_shell { | 16 namespace aura_shell { |
18 | |
19 namespace internal { | 17 namespace internal { |
20 | 18 |
21 // The images drawn inside the background tab are drawn at this offset from | 19 // The images drawn inside the background tab are drawn at this offset from |
22 // the edge. | 20 // the edge. |
23 const int kBgImageContentInset = 12; | 21 const int kBgImageContentInset = 12; |
24 | 22 |
25 // Padding between each of the images. | 23 // Padding between each of the images. |
26 const int kImagePadding = 8; | 24 const int kImagePadding = 8; |
27 | 25 |
28 // Insets used in painting the background if it's rendered bigger than the size | 26 // Insets used in painting the background if it's rendered bigger than the size |
29 // of the background image. See ImagePainter::CreateImagePainter for how these | 27 // of the background image. See ImagePainter::CreateImagePainter for how these |
30 // are interpreted. | 28 // are interpreted. |
31 const int kBgTopInset = 12; | 29 const int kBgTopInset = 12; |
32 const int kBgLeftInset = 30; | 30 const int kBgLeftInset = 30; |
33 const int kBgBottomInset = 12; | 31 const int kBgBottomInset = 12; |
34 const int kBgRightInset = 8; | 32 const int kBgRightInset = 8; |
35 | 33 |
36 // static | 34 // static |
37 SkBitmap* TabbedLauncherButton::bg_image_ = NULL; | 35 SkBitmap* TabbedLauncherButton::bg_image_1_ = NULL; |
| 36 SkBitmap* TabbedLauncherButton::bg_image_2_ = NULL; |
| 37 SkBitmap* TabbedLauncherButton::bg_image_3_ = NULL; |
38 | 38 |
39 TabbedLauncherButton::TabbedLauncherButton(views::ButtonListener* listener, | 39 TabbedLauncherButton::TabbedLauncherButton(views::ButtonListener* listener, |
40 LauncherButtonHost* host) | 40 LauncherButtonHost* host) |
41 : views::CustomButton(listener), | 41 : views::CustomButton(listener), |
42 host_(host) { | 42 host_(host) { |
43 if (!bg_image_) { | 43 if (!bg_image_1_) { |
44 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 44 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
45 bg_image_ = new SkBitmap( | 45 bg_image_1_ = new SkBitmap( |
46 *rb.GetImageNamed(IDR_AURA_LAUNCHER_TABBED_BROWSER).ToSkBitmap()); | 46 *rb.GetImageNamed(IDR_AURA_LAUNCHER_TABBED_BROWSER_1).ToSkBitmap()); |
| 47 bg_image_2_ = new SkBitmap( |
| 48 *rb.GetImageNamed(IDR_AURA_LAUNCHER_TABBED_BROWSER_2).ToSkBitmap()); |
| 49 bg_image_3_ = new SkBitmap( |
| 50 *rb.GetImageNamed(IDR_AURA_LAUNCHER_TABBED_BROWSER_3).ToSkBitmap()); |
47 } | 51 } |
48 } | 52 } |
49 | 53 |
50 TabbedLauncherButton::~TabbedLauncherButton() { | 54 TabbedLauncherButton::~TabbedLauncherButton() { |
51 } | 55 } |
52 | 56 |
53 void TabbedLauncherButton::SetImages(const LauncherTabbedImages& images) { | 57 void TabbedLauncherButton::SetImages(const LauncherTabbedImages& images) { |
54 images_ = images; | 58 images_ = images; |
55 } | 59 } |
56 | 60 |
57 gfx::Size TabbedLauncherButton::GetPreferredSize() { | 61 gfx::Size TabbedLauncherButton::GetPreferredSize() { |
58 if (images_.empty()) | 62 return gfx::Size(bg_image_1_->width(), bg_image_1_->height()); |
59 return gfx::Size(bg_image_->width(), bg_image_->height()); | |
60 | |
61 // Assume all images are the same size. | |
62 int num_images = static_cast<int>(images_.size()); | |
63 int padding = (num_images - 1) * kImagePadding; | |
64 return gfx::Size( | |
65 std::max(kBgImageContentInset * 2 + | |
66 images_[0].image.width() * num_images + padding, | |
67 bg_image_->width()), | |
68 bg_image_->height()); | |
69 } | 63 } |
70 | 64 |
71 void TabbedLauncherButton::OnPaint(gfx::Canvas* canvas) { | 65 void TabbedLauncherButton::OnPaint(gfx::Canvas* canvas) { |
72 if (width() == bg_image_->width()) { | 66 SkBitmap* bg_image = NULL; |
73 canvas->DrawBitmapInt(*bg_image_, 0, 0); | 67 if (images_.size() <= 1) |
74 } else { | 68 bg_image = bg_image_1_; |
75 scoped_ptr<views::Painter> bg_painter(views::Painter::CreateImagePainter( | 69 else if (images_.size() == 2) |
76 *bg_image_, gfx::Insets(kBgTopInset, kBgLeftInset, kBgBottomInset, | 70 bg_image = bg_image_2_; |
77 kBgRightInset), true)); | 71 else |
78 bg_painter->Paint(width(), height(), canvas); | 72 bg_image = bg_image_3_; |
79 } | 73 canvas->DrawBitmapInt(*bg_image, 0, 0); |
80 | 74 |
81 if (images_.empty()) | 75 if (images_.empty()) |
82 return; | 76 return; |
83 | 77 |
84 int num_images = static_cast<int>(images_.size()); | 78 // Only show the first icon. |
85 int padding = (num_images - 1) * kImagePadding; | 79 // TODO(sky): if we settle on just 1 icon, then we should simplify surrounding |
86 int x = std::max(kBgImageContentInset, | 80 // code (don't use a vector of images). |
87 (width() - num_images * images_[0].image.width() - padding) / 2); | 81 int x = (width() - images_[0].image.width()) / 2; |
88 int y = (height() - images_[0].image.height()) / 2; | 82 int y = (height() - images_[0].image.height()) / 2; |
89 for (LauncherTabbedImages::const_iterator i = images_.begin(); | 83 canvas->DrawBitmapInt(images_[0].image, x, y); |
90 i != images_.end(); ++i) { | |
91 canvas->DrawBitmapInt(i->image, x, y); | |
92 x += i->image.width() + kImagePadding; | |
93 } | |
94 } | 84 } |
95 | 85 |
96 bool TabbedLauncherButton::OnMousePressed(const views::MouseEvent& event) { | 86 bool TabbedLauncherButton::OnMousePressed(const views::MouseEvent& event) { |
97 CustomButton::OnMousePressed(event); | 87 CustomButton::OnMousePressed(event); |
98 host_->MousePressedOnButton(this, event); | 88 host_->MousePressedOnButton(this, event); |
99 return true; | 89 return true; |
100 } | 90 } |
101 | 91 |
102 void TabbedLauncherButton::OnMouseReleased(const views::MouseEvent& event) { | 92 void TabbedLauncherButton::OnMouseReleased(const views::MouseEvent& event) { |
103 host_->MouseReleasedOnButton(this, false); | 93 host_->MouseReleasedOnButton(this, false); |
104 CustomButton::OnMouseReleased(event); | 94 CustomButton::OnMouseReleased(event); |
105 } | 95 } |
106 | 96 |
107 void TabbedLauncherButton::OnMouseCaptureLost() { | 97 void TabbedLauncherButton::OnMouseCaptureLost() { |
108 host_->MouseReleasedOnButton(this, true); | 98 host_->MouseReleasedOnButton(this, true); |
109 CustomButton::OnMouseCaptureLost(); | 99 CustomButton::OnMouseCaptureLost(); |
110 } | 100 } |
111 | 101 |
112 bool TabbedLauncherButton::OnMouseDragged(const views::MouseEvent& event) { | 102 bool TabbedLauncherButton::OnMouseDragged(const views::MouseEvent& event) { |
113 CustomButton::OnMouseDragged(event); | 103 CustomButton::OnMouseDragged(event); |
114 host_->MouseDraggedOnButton(this, event); | 104 host_->MouseDraggedOnButton(this, event); |
115 return true; | 105 return true; |
116 } | 106 } |
117 | 107 |
118 } // namespace internal | 108 } // namespace internal |
119 } // namespace aura_shell | 109 } // namespace aura_shell |
OLD | NEW |