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 void ButtonDecoration::SetIcon(ButtonState state, int icon_id) {
69 case kButtonStateNormal:
70 normal_icon_id_ = icon_id;
72 case kButtonStateHover:
73 hover_icon_id_ = icon_id;
75 case kButtonStatePressed:
76 pressed_icon_id_ = icon_id;
83 void ButtonDecoration::SetIcon(int icon_id) {
84 normal_icon_id_ = icon_id;
85 hover_icon_id_ = icon_id;
86 pressed_icon_id_ = icon_id;
89 CGFloat ButtonDecoration::GetWidthForSpace(CGFloat width) {
90 const ui::NinePartImageIds image_ids = GetImageIds();
91 NSImage* icon = GetImageFromId(GetIconId());
96 const CGFloat min_width = [GetImageFromId(image_ids.left) size].width +
98 [GetImageFromId(image_ids.right) size].width;
99 if (width < min_width)
100 return kOmittedWidth;
102 const CGFloat max_width = min_width + 2 * max_inner_padding_;
103 return std::min(width, max_width);
106 void ButtonDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
107 const ui::NinePartImageIds image_ids = GetImageIds();
108 NSImage* icon = GetImageFromId(GetIconId());
113 ui::DrawNinePartImage(frame, image_ids, NSCompositeSourceOver, 1.0, YES);
115 const CGFloat x_inset =
116 std::floor((NSWidth(frame) - [icon size].width) / 2.0);
117 const CGFloat y_inset =
118 std::floor((NSHeight(frame) - [icon size].height) / 2.0);
120 [icon drawInRect:NSInsetRect(frame, x_inset, y_inset)
122 operation:NSCompositeSourceOver
128 bool ButtonDecoration::AcceptsMousePress() {
132 bool ButtonDecoration::IsDraggable() {
136 bool ButtonDecoration::OnMousePressed(NSRect frame) {
140 ButtonDecoration* ButtonDecoration::AsButtonDecoration() {
144 ui::NinePartImageIds ButtonDecoration::GetImageIds() const {
146 case kButtonStateHover:
147 return hover_image_ids_;
148 case kButtonStatePressed:
149 return pressed_image_ids_;
150 case kButtonStateNormal:
152 return normal_image_ids_;
156 int ButtonDecoration::GetIconId() const {
158 case kButtonStateHover:
159 return hover_icon_id_;
160 case kButtonStatePressed:
161 return pressed_icon_id_;
162 case kButtonStateNormal:
164 return normal_icon_id_;