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.
7 #import "chrome/browser/ui/cocoa/location_bar/button_decoration.h"
9 #include "base/logging.h"
10 #import "base/mac/scoped_nsobject.h"
11 #import "ui/base/cocoa/appkit_utils.h"
12 #include "ui/base/resource/resource_bundle.h"
16 NSImage* GetImageFromId(int image_id) {
17 return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(image_id)
21 bool IsValidNinePartImageIds(const ui::NinePartImageIds& object) {
22 return (object.top_left >= 0) &&
24 (object.top_right >= 0) &&
26 (object.center >= 0) &&
27 (object.right >= 0) &&
28 (object.bottom_left >= 0) &&
29 (object.bottom >= 0) &&
30 (object.bottom_right >= 0);
35 ButtonDecoration::ButtonDecoration(ui::NinePartImageIds normal_image_ids,
37 ui::NinePartImageIds hover_image_ids,
39 ui::NinePartImageIds pressed_image_ids,
41 CGFloat max_inner_padding)
42 : normal_image_ids_(normal_image_ids),
43 hover_image_ids_(hover_image_ids),
44 pressed_image_ids_(pressed_image_ids),
45 normal_icon_id_(normal_icon_id),
46 hover_icon_id_(hover_icon_id),
47 pressed_icon_id_(pressed_icon_id),
48 state_(kButtonStateNormal),
49 max_inner_padding_(max_inner_padding) {
50 DCHECK(IsValidNinePartImageIds(normal_image_ids_) && normal_icon_id_ >= 0);
51 DCHECK(IsValidNinePartImageIds(hover_image_ids_) && hover_icon_id_ >= 0);
52 DCHECK(IsValidNinePartImageIds(pressed_image_ids_) && pressed_icon_id_ >= 0);
53 DCHECK_GE(max_inner_padding_, 0);
56 ButtonDecoration::~ButtonDecoration() {
59 void ButtonDecoration::SetButtonState(ButtonDecoration::ButtonState state) {
63 ButtonDecoration::ButtonState ButtonDecoration::GetButtonState() const {
67 bool ButtonDecoration::PreventFocus(NSPoint location) const {
71 void ButtonDecoration::SetIcon(ButtonState state, int icon_id) {
73 case kButtonStateNormal:
74 normal_icon_id_ = icon_id;
76 case kButtonStateHover:
77 hover_icon_id_ = icon_id;
79 case kButtonStatePressed:
80 pressed_icon_id_ = icon_id;
87 void ButtonDecoration::SetIcon(int icon_id) {
88 normal_icon_id_ = icon_id;
89 hover_icon_id_ = icon_id;
90 pressed_icon_id_ = icon_id;
93 void ButtonDecoration::SetBackgroundImageIds(
94 ui::NinePartImageIds normal_image_ids,
95 ui::NinePartImageIds hover_image_ids,
96 ui::NinePartImageIds pressed_image_ids) {
97 DCHECK(IsValidNinePartImageIds(normal_image_ids));
98 DCHECK(IsValidNinePartImageIds(hover_image_ids));
99 DCHECK(IsValidNinePartImageIds(pressed_image_ids));
100 normal_image_ids_ = normal_image_ids;
101 hover_image_ids_ = hover_image_ids;
102 pressed_image_ids_ = pressed_image_ids;
105 ui::NinePartImageIds ButtonDecoration::GetBackgroundImageIds() const {
107 case kButtonStateHover:
108 return hover_image_ids_;
109 case kButtonStatePressed:
110 return pressed_image_ids_;
111 case kButtonStateNormal:
113 return normal_image_ids_;
117 NSImage* ButtonDecoration::GetIconImage() const {
119 case kButtonStateHover:
120 return GetImageFromId(hover_icon_id_);
121 case kButtonStatePressed:
122 return GetImageFromId(pressed_icon_id_);
123 case kButtonStateNormal:
125 return GetImageFromId(normal_icon_id_);
129 CGFloat ButtonDecoration::GetWidthForSpace(CGFloat width) {
130 const ui::NinePartImageIds image_ids = GetBackgroundImageIds();
131 NSImage* icon = GetIconImage();
134 return kOmittedWidth;
136 const CGFloat min_width = [GetImageFromId(image_ids.left) size].width +
138 [GetImageFromId(image_ids.right) size].width;
139 if (width < min_width)
140 return kOmittedWidth;
142 const CGFloat max_width = min_width + 2 * max_inner_padding_;
143 return std::min(width, max_width);
146 void ButtonDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
147 const ui::NinePartImageIds image_ids = GetBackgroundImageIds();
148 NSImage* icon = GetIconImage();
153 ui::DrawNinePartImage(frame, image_ids, NSCompositeSourceOver, 1.0, YES);
155 const CGFloat x_inset =
156 std::floor((NSWidth(frame) - [icon size].width) / 2.0);
157 const CGFloat y_inset =
158 std::floor((NSHeight(frame) - [icon size].height) / 2.0);
160 [icon drawInRect:NSInsetRect(frame, x_inset, y_inset)
162 operation:NSCompositeSourceOver
168 bool ButtonDecoration::AcceptsMousePress() {
172 bool ButtonDecoration::IsDraggable() {
176 bool ButtonDecoration::OnMousePressed(NSRect frame, NSPoint location) {
180 ButtonDecoration* ButtonDecoration::AsButtonDecoration() {