cb854339a81fc75da133221fc90a0f9c5a2ba33d
[platform/framework/web/crosswalk.git] / src / ui / base / cocoa / controls / blue_label_button.mm
1 // Copyright 2013 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.
4
5 #import "ui/base/cocoa/controls/blue_label_button.h"
6
7 #include "base/mac/foundation_util.h"
8 #include "skia/ext/skia_utils_mac.h"
9 #include "third_party/skia/include/core/SkColor.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
12
13 const CGFloat kCornerRadius = 2;
14
15 const CGFloat kButtonFontSizeDelta = -1;
16 const CGFloat kTopBottomTextPadding = 7;
17 const CGFloat kLeftRightTextPadding = 15;
18 const SkColor kTextShadowColor = SkColorSetRGB(0x53, 0x8c, 0xea);
19
20 const SkColor kShadowColor = SkColorSetRGB(0xe9, 0xe9, 0xe9);
21 const SkColor kDefaultColor = SkColorSetRGB(0x5a, 0x97, 0xff);
22 const SkColor kHoverColor = SkColorSetRGB(0x55, 0x8f, 0xf3);
23 const SkColor kPressedColor = SkColorSetRGB(0x42, 0x79, 0xd8);
24
25 const SkColor kInnerRingColor = SkColorSetRGB(0x64, 0x9e, 0xff);
26 const SkColor kFocusInnerRingColor = SkColorSetRGB(0xad, 0xcc, 0xff);
27 const SkColor kPressInnerRingColor = SkColorSetRGB(0x3f, 0x73, 0xcd);
28
29 const SkColor kOuterRingColor = SkColorSetRGB(0x2b, 0x67, 0xce);
30 const SkColor kPressOuterRingColor = SkColorSetRGB(0x23, 0x52, 0xa2);
31
32 @interface BlueLabelButtonCell : NSButtonCell
33
34 + (NSAttributedString*)generateAttributedString:(NSString*)buttonText;
35
36 @end
37
38 @implementation BlueLabelButton
39
40 + (Class)cellClass {
41   return [BlueLabelButtonCell class];
42 }
43
44 - (id)initWithFrame:(NSRect)frameRect {
45   if ((self = [super initWithFrame:frameRect])) {
46     [self setBezelStyle:NSSmallSquareBezelStyle];
47   }
48   return self;
49 }
50
51 @end
52
53 @implementation BlueLabelButtonCell
54
55 + (NSAttributedString*)generateAttributedString:(NSString*)buttonText {
56   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
57   NSFont* buttonFont = rb.GetFont(ui::ResourceBundle::BaseFont).
58       DeriveFont(kButtonFontSizeDelta).GetNativeFont();
59   base::scoped_nsobject<NSMutableParagraphStyle> buttonTextParagraphStyle(
60       [[NSMutableParagraphStyle alloc] init]);
61   [buttonTextParagraphStyle setAlignment:NSCenterTextAlignment];
62
63   base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
64   [shadow setShadowOffset:NSMakeSize(0, -1)];
65   [shadow setShadowBlurRadius:0];
66   [shadow setShadowColor:gfx::SkColorToSRGBNSColor(kTextShadowColor)];
67
68   NSDictionary* buttonTextAttributes = @{
69     NSParagraphStyleAttributeName : buttonTextParagraphStyle,
70     NSFontAttributeName : buttonFont,
71     NSForegroundColorAttributeName : [NSColor whiteColor],
72     NSShadowAttributeName : shadow.get()
73   };
74   base::scoped_nsobject<NSAttributedString> attributedButtonText(
75       [[NSAttributedString alloc] initWithString:buttonText
76                                       attributes:buttonTextAttributes]);
77   return attributedButtonText.autorelease();
78 }
79
80 - (NSSize)cellSize {
81   NSAttributedString* attributedTitle =
82       [[self class] generateAttributedString:[self title]];
83   NSSize textSize = [attributedTitle size];
84
85   // Add 1 to maintain identical height w/ previous drawing code.
86   textSize.height += 2 * kTopBottomTextPadding + 1;
87   textSize.width += 2 * kLeftRightTextPadding;
88   return textSize;
89 }
90
91 - (NSRect)drawTitle:(NSAttributedString*)title
92           withFrame:(NSRect)frame
93              inView:(NSView*)controlView {
94   // Fuzz factor to adjust for the drop shadow. Based on visual inspection.
95   frame.origin.y -= 1;
96
97   NSAttributedString* attributedTitle =
98       [[self class] generateAttributedString:[self title]];
99   gfx::ScopedNSGraphicsContextSaveGState context;
100   [attributedTitle drawInRect:frame];
101   return frame;
102 }
103
104 - (void)drawBezelWithFrame:(NSRect)frame
105                     inView:(NSView*)controlView {
106   NSColor* centerColor;
107   NSColor* innerColor;
108   NSColor* outerColor;
109   HoverState hoverState =
110       [base::mac::ObjCCastStrict<HoverButton>(controlView) hoverState];
111   // Leave a sliver of height 1 for the button drop shadow.
112   frame.size.height -= 1;
113
114   if (hoverState == kHoverStateMouseDown && [self isHighlighted]) {
115     centerColor = gfx::SkColorToSRGBNSColor(kPressedColor);
116     innerColor = gfx::SkColorToSRGBNSColor(kPressInnerRingColor);
117     outerColor = gfx::SkColorToSRGBNSColor(kPressOuterRingColor);
118   } else {
119     centerColor = hoverState == kHoverStateMouseOver ?
120         gfx::SkColorToSRGBNSColor(kHoverColor) :
121         gfx::SkColorToSRGBNSColor(kDefaultColor);
122     innerColor = [self showsFirstResponder] ?
123         gfx::SkColorToSRGBNSColor(kFocusInnerRingColor) :
124         gfx::SkColorToSRGBNSColor(kInnerRingColor);
125     outerColor = gfx::SkColorToSRGBNSColor(kOuterRingColor);
126   }
127   {
128     gfx::ScopedNSGraphicsContextSaveGState context;
129     base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
130     [shadow setShadowOffset:NSMakeSize(0, -1)];
131     [shadow setShadowBlurRadius:1.0];
132     [shadow setShadowColor:gfx::SkColorToSRGBNSColor(kShadowColor)];
133     [shadow set];
134     [outerColor set];
135
136     [[NSBezierPath bezierPathWithRoundedRect:frame
137                                      xRadius:kCornerRadius
138                                      yRadius:kCornerRadius] fill];
139   }
140
141   [innerColor set];
142   [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 1, 1)
143                                    xRadius:kCornerRadius
144                                    yRadius:kCornerRadius] fill];
145   [centerColor set];
146   [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 2, 2)
147                                    xRadius:kCornerRadius
148                                    yRadius:kCornerRadius] fill];
149 }
150
151 @end