Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / button_decoration.mm
blob65653a544ade9bd5b3cb234f3606a4af57d38b00
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 bool ButtonDecoration::PreventFocus(NSPoint location) const {
68   return false;
71 void ButtonDecoration::SetIcon(ButtonState state, int icon_id) {
72   switch (state) {
73     case kButtonStateNormal:
74       normal_icon_id_ = icon_id;
75       break;
76     case kButtonStateHover:
77       hover_icon_id_ = icon_id;
78       break;
79     case kButtonStatePressed:
80       pressed_icon_id_ = icon_id;
81       break;
82     default:
83       NOTREACHED();
84   }
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 {
106   switch (state_) {
107     case kButtonStateHover:
108       return hover_image_ids_;
109     case kButtonStatePressed:
110       return pressed_image_ids_;
111     case kButtonStateNormal:
112     default:
113       return normal_image_ids_;
114   }
117 NSImage* ButtonDecoration::GetIconImage() const {
118   switch (state_) {
119     case kButtonStateHover:
120       return GetImageFromId(hover_icon_id_);
121     case kButtonStatePressed:
122       return GetImageFromId(pressed_icon_id_);
123     case kButtonStateNormal:
124     default:
125       return GetImageFromId(normal_icon_id_);
126   }
129 CGFloat ButtonDecoration::GetWidthForSpace(CGFloat width) {
130   const ui::NinePartImageIds image_ids = GetBackgroundImageIds();
131   NSImage* icon = GetIconImage();
133   if (!icon)
134     return kOmittedWidth;
136   const CGFloat min_width = [GetImageFromId(image_ids.left) size].width +
137                             [icon 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();
150   if (!icon)
151     return;
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)
161           fromRect:NSZeroRect
162          operation:NSCompositeSourceOver
163           fraction:1.0
164     respectFlipped:YES
165              hints:nil];
168 bool ButtonDecoration::AcceptsMousePress() {
169   return true;
172 bool ButtonDecoration::IsDraggable() {
173   return false;
176 bool ButtonDecoration::OnMousePressed(NSRect frame, NSPoint location) {
177   return false;
180 ButtonDecoration* ButtonDecoration::AsButtonDecoration() {
181   return this;