Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / button_decoration.mm
blob06b8f6e60fabe4e06444c4fee9215ba5d0f67501
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.
5 #include <cmath>
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"
14 namespace {
16 NSImage* GetImageFromId(int image_id) {
17   return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(image_id)
18       .ToNSImage();
21 bool IsValidNinePartImageIds(const ui::NinePartImageIds& object) {
22   return (object.top_left >= 0) &&
23          (object.top >= 0) &&
24          (object.top_right >= 0) &&
25          (object.left >= 0) &&
26          (object.center >= 0) &&
27          (object.right >= 0) &&
28          (object.bottom_left >= 0) &&
29          (object.bottom >= 0) &&
30          (object.bottom_right >= 0);
33 }  // namespace
35 ButtonDecoration::ButtonDecoration(ui::NinePartImageIds normal_image_ids,
36                                    int normal_icon_id,
37                                    ui::NinePartImageIds hover_image_ids,
38                                    int hover_icon_id,
39                                    ui::NinePartImageIds pressed_image_ids,
40                                    int pressed_icon_id,
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) {
60   state_ = state;
63 ButtonDecoration::ButtonState ButtonDecoration::GetButtonState() const {
64   return state_;
67 void ButtonDecoration::SetIcon(ButtonState state, int icon_id) {
68   switch (state) {
69     case kButtonStateNormal:
70       normal_icon_id_ = icon_id;
71       break;
72     case kButtonStateHover:
73       hover_icon_id_ = icon_id;
74       break;
75     case kButtonStatePressed:
76       pressed_icon_id_ = icon_id;
77       break;
78     default:
79       NOTREACHED();
80   }
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());
93   if (!icon)
94     return kOmittedWidth;
96   const CGFloat min_width = [GetImageFromId(image_ids.left) size].width +
97                             [icon 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());
110   if (!icon)
111     return;
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)
121           fromRect:NSZeroRect
122          operation:NSCompositeSourceOver
123           fraction:1.0
124     respectFlipped:YES
125              hints:nil];
128 bool ButtonDecoration::AcceptsMousePress() {
129   return true;
132 bool ButtonDecoration::IsDraggable() {
133   return false;
136 bool ButtonDecoration::OnMousePressed(NSRect frame) {
137   return false;
140 ButtonDecoration* ButtonDecoration::AsButtonDecoration() {
141   return this;
144 ui::NinePartImageIds ButtonDecoration::GetImageIds() const {
145   switch (state_) {
146     case kButtonStateHover:
147       return hover_image_ids_;
148     case kButtonStatePressed:
149       return pressed_image_ids_;
150     case kButtonStateNormal:
151     default:
152       return normal_image_ids_;
153   }
156 int ButtonDecoration::GetIconId() const {
157   switch (state_) {
158     case kButtonStateHover:
159       return hover_icon_id_;
160     case kButtonStatePressed:
161       return pressed_icon_id_;
162     case kButtonStateNormal:
163     default:
164       return normal_icon_id_;
165   }